@laverna_ritchie5/wallet
v0.3.10
Published
- 错误码有6位组成:3位模块代码+3位错误码 - 所有的错误码定义在ts_src/error.ts中 - 原始接口如果已定义错误码,需要保留原有错误码,方便调试查询错误
Downloads
59
Readme
wallet-lib
1、概览
1.1 概念说明
1.2 错误码定义
- 错误码有6位组成:3位模块代码+3位错误码
- 所有的错误码定义在ts_src/error.ts中
- 原始接口如果已定义错误码,需要保留原有错误码,方便调试查询错误
1.3 代码结构说明
ts_src typescript主代码位置
| index.ts 导出供RN引用的包 | wallet.ts 钱包主类,涉及助记词,加密,导入,导出 | account.ts 钱包中的账户管理 | network.ts 网络节点管理 | chainProvider.ts 对接各个链定义的接口 | btc,eth 对应链实现的provider
test 单元测试
src typescript编译后生成的javascript代码
types typescript导出的定义,方便ts项目引用
2.根据助记词创建钱包
/**
* 通过助记词创建钱包
* @param password 钱包密码|交易密码,本地存储
* @param passwordHint 钱包密码提示
* @param words 助记词为空时自动随机生成助记词
*/
export declare function fromMnemonic(password: string, passwordHint: string, words?: string): Wallet;
//示例
import * as api from "wallet";
await api.wallet.fromMnemonic("12345", "12345", words);
3 钱包持久化
//通过json加载钱包
let wallet = api.wallet.fromJson("{\"accounts\":[{\"name\":\"ETH\",\"chainType\":\"eth\",\"chainName\":\"ethereum\",\"networkType\":1,\"privateKey\":\"KQFhWTnrmxTcVEkcDKFT3AN4TYQeZI7b8vYaJ7tBaLbKM59J9WrFhqnd7Hx8fvPpHj5ohJvoMQYNSabeUOMmRf06JmPVU+I0QcVC/bG4/os=\",\"network\":\"\",\"address\":\"0xC9A4DE3Ab9FEd03fA450654Cd6ceffB36CA5c0D4\",\"type\":\"\",\"balance\":\"0.999369999999999998\",\"tokens\":[{\"icon\":\"\",\"type\":\"ERC20\",\"name\":\"BarnBridge cDAI\",\"symbol\":\"BarnBridge cDAI\",\"address\":\"0xebf32075b5ee6e9aff265d3ec6c69a2b381b61b1\",\"decimals\":18,\"qrcode\":\"ethereum:0xC9A4DE3Ab9FEd03fA450654Cd6ceffB36CA5c0D4?contract=0xebf32075b5ee6e9aff265d3ec6c69a2b381b61b1&type=ERC20\",\"balance\":\"0\"}],\"decimals\":18,\"qrcode\":\"ethereum:0xC9A4DE3Ab9FEd03fA450654Cd6ceffB36CA5c0D4\"}],\"id\":\"12dZ2D692fojjjxmUwJqrdL7Jgy4RfkWyn\",\"name\":\"wallet\",\"mnemonic\":\"O9UoyZby6/7ssdvnu+pZIexylAF1SHLeGC+/ixeXE809vzMv66JQCQAwJvqDWr6Is7Q703Y0wk1xu13TKLv93VMGOvw/jTd10jFLutd4S7U=\",\"encoding\":\"string\",\"createdAt\":\"2021-06-29T09:06:34.711Z\",\"password\":\"0xa39fca4511cdae3c56f195d7a28b57883e42812197db0204c41188b5cf19c433\",\"passwordHint\":\"12345\",\"source\":2}");
//导出json
wallet.toString(password);//默认json不加密,只对私钥加密
4.API Reference
创建及恢复钱包
/**
* 生成助记词
* @example let wallet = api.wallet.generateMnemonic();
*/
export declare function generateMnemonic(): string;
/**
* 是否是有效助记词
* @param words 助记词
* @example let wallet = api.wallet.isValidateMnemonic(words);
*/
export declare function isValidateMnemonic(words: string): boolean;
/**
* 通过助记词创建钱包
* @param password 钱包密码|交易密码,本地存储
* @param passwordHint 钱包密码提示
* @param words 助记词为空时自动随机生成助记词
* @example let wallet = await api.wallet.fromMnemonic("12345", "12345", words);
*/
export declare function fromMnemonic(password: string, passwordHint: string, words?: string): Wallet;
/**
* 验证密码是否有效
* @param password
* @param shaPassword
*/
export declare function isValidPassword(password: string, shaPassword: string): boolean;
/**
* 通过json加载钱包
* @param json
* @param password
*/
export declare function fromJson(json: string, password?: string): Wallet;
/**
* 解析二维码
* @param qrcode
* @example let res= api.wallet.parseQrcodeAddress("bitcoin:1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH?amount=20.3&label=Foobar")
*/
export declare function parseQrcodeAddress(qrcode: string): QrcodeAddress;
/**
* 判断是否是二维码地址
* @param address
*/
export declare function isQrcodeAddress(address: string): boolean;
Wallet
/**
* 检查钱包密码是否正确
* @param password
* @example let valid = wallet.checkPassword("12345");
*/
checkPassword(password: string): void;
isValidPassword(password: string): boolean;
/**
* 获取链提供者,方便构建交易
* @param chainName
* @param network
*/
getChainProvider(chainName: string, network?: string): ChainProvider;
/**
* 添加其它钱包
* @param wallet
* @example s
let otherWallet = await api.wallet.fromMnemonic("12345", "12345", words);
await otherWallet.addAccount("ETHEREUM", other_wallet_password)
mainWallet.addOtherWallet(otherWallet);
*/
addOtherWallet(wallet: Wallet): void;
/**
* 删除其它钱包
* @param id
* @example mainWallet.removeOtherWallet(otherWallet.id);
*/
removeOtherWallet(id: String): void;
/**
* 添加账户到钱包
* @param chainName 链名称
* @param password 必须要输入密码
* @param network 网络名称,为空默认创建主链
* @example let account = await wallet.addAccount("ETHEREUM", password, "kovan");
*/
addAccount(chainName: string, password: string, network?: string): Promise<Account>;
/**
* 获取已存在的钱包并绑定相关对象,显示账户的相关信息
* @param chainName
* @param network
* @example let account = wallet.getAccount("ETHEREUM", "kovan");
*/
getAccount(chainName: string, network?: string): Account;
/**
* 获取所有账户
* @param symbol bitcoin/ethereum/...
* @example wallet.getAllAccounts("bitcoin");
* wallet.getAllAccounts();
*/
getAllAccounts(symbol?: string): AccountSummary[];
/**
* 刷新钱包下的所有账户,主要同步账户余额。
* @example wallet.refreshAccounts();
*/
refreshAccounts(): void;
/**
* 修改法币类型
* @param currency
* @example wallet.changeCurrency("USD");
*/
changeCurrency(currency: string): void;
/**
* 获取助记词
* @param password
* @example mainWallet.getMnemonic(wallet_password);
*/
getMnemonic(password: string): string;
/**
* 转为json字符串
* @param password
*/
toString(password?: string): string;
Account
/**
* 获取二维码
* @param options
*/
getQrcodeAddress(options?: QrcodeOptions): string;
/**
* 获取账户余额
* @param includeTokens 是否包括代币
*/
getBalance(includeTokens?: boolean): Promise<BigNumber>;
/**
* 获取可以切换的网络列表,前端需要自己进行分组
*/
getChainNetworks(): ChainNetwork[];
/**
* 批量新增代币,默认不进行余额查询,只做关联
* @param tokens
*/
addTokens(tokens: TokenInterface[]): Account;
/**
* 是否已经存在代币
* @param address
*/
hasToken(address: string): boolean;
/**
* 估算gas费用
* @param transaction
*/
estimateGas(transaction: Transaction): Promise<string>;
/**
* 获取钱包当前网络支持的代币列表
*/
getTokenList(): TokenInfo[];
/**
* 发送交易
* @param transaction
* @param password
*/
sendTransaction(transaction: Transaction, password: string): Promise<any>;
refresh(): Promise<void>;
/**
* 查询交易历史
* @param contract
*/
queryTransactions(contract?: string): void;
/**
* 处理事件监听
* @param event
* @param fn
*/
on(event: AccountEvent, fn: Function): void;
4.创建不同链及交易所的地址或者钱包
5.转账
5、交易查询
6、交易监听
7、兑换(swap)
8、委托交易
9、已支持的链
- BTC
- ETH