console.log('>>begin load wallet main file') /** * 初始化钱包, 所有操作进行前, 必须调用此方法 * @param {string} type: 钱包类型, 0: 内置钱包, 1: 第三方钱包 * @param {string} password: 用于加密钱包数据的密码 */ function initWallet(funId, type, password) { type = 0 try { var wallet if (!window.jc || !jc.wallet) { wallet = new jcwallet.default({ chain: 322, type, password }) } else { wallet = jc.wallet } type = parseInt(type) if (type === 1) { console.log('wallet init success, begin connect') wallet .initThirdPartyWallet() .then(() => { console.log('walletconnect connect success') var account = jc.wallet.currentAccount() jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: account })) }) .catch((err) => { console.log('walletconnect connect error: ' + JSON.stringify(err)) jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } else { let address = jc.wallet.currentAccount().address jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: address })) } } catch (err) { console.error('wallet init with error: ' + JSON.stringify(err)) jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) } } /** * 钱包当前激活的帐号的详细信息 */ function currentAccount(funId) { try { let data = jc.wallet.currentAccountData return JSON.stringify({ errcode: 0, data }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 获取当前链所有帐号列表 */ function accountList(funId) { try { let data = jc.wallet.accounts return JSON.stringify({ errcode: 0, data }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 获取所有支持的链的列表 */ function chainList(funId) { try { let data = jc.wallet.chainList return JSON.stringify({ errcode: 0, data }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 当前链的信息 */ function currentChain(funId) { try { let data = jc.wallet.currentChain return JSON.stringify({ errcode: 0, data }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * [BOTH]切换当前链 * 切换链需要调用currentAccount方法, 以刷新界面显示 */ function changeChain(funId, chainId) { // chainId = parseInt(chainId); chainId = 80001 jc.wallet .updateCurrentChain(chainId) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } /** * [BOTH]获取当前帐户的登录签名 * @param {string} nonce: 从服务端获取的nonce * @param {string} tips: 签名时的提示 */ function loginSign(funId, nonce, tips) { jc.wallet .loginSign(nonce, tips) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } /** * 创建一个新帐号, 并将新建帐号设为当前激活帐号 * @return {string} 当前激活帐户的地址 */ function createAccount(funId) { try { let result = jc.wallet.createAccount() return JSON.stringify({ errcode: 0, data: result }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 用导入一个密钥, 并将导入的帐号设为当前激活帐号 * @return {string} 当前激活帐户的地址 */ function importAccount(funId, privateKey) { try { let address = jc.wallet.importAccount(privateKey) return JSON.stringify({ errcode: 0, data: address }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 将一个帐号地址设为当前激活帐号 */ function selectAccount(funId, address) { try { let result = jc.wallet.selectAccount(address) return JSON.stringify({ errcode: 0, data: result }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 获取当前链上基础代币的余额 * @param {string} address: 待查询的帐户地址 * 不传的话, 则获取当前帐户的余额 */ function getEthBalance(funId, address) { jc.wallet .getBalance(address) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } /** * 将当前帐户里的基础代币转账给别人 * @param {string} to: 转账目标地址 * @param {string} amount: 转账数量 */ function sendEth(funId, to, amount) { jc.wallet .sendEth(to, amount) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } /** * [BOTH]生成hash图片 * @param {string} msg: 要生成图片的内容 * @param {string} diameter: 图片尺寸 */ function generateIcon(funId, msg, diameter) { try { diameter = parseFloat(diameter) let result = jc.wallet.generateIconData(msg, diameter) return JSON.stringify({ errcode: 0, data: result }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } /** * 获取ERC20代币的基本信息, 包括symbol和decimal * 这些信息一般都不会变化, 客户端需缓存这些信息 * @param {string} address: 代币的地址 */ function erc20Info(funId, address) { jc.wallet .erc20Info(address) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } /** * 获取erc20代币的余额 * @param {string} address: 代币地址 * @param {string} account: 所属帐户的地址, 不传该参数的话, 获取当前钱包激活帐户的余额 */ function erc20Balance(funId, address, account) { jc.wallet .erc20Balance(address, account) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } function sendErc20(funId, address, to, amount) { jc.wallet .sendErc20(address, to, amount) .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) }) .catch((err) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) }) } function restoreFromMnemonic(funId, mnemonic, password) { try { let result = jc.wallet.restoreFromMnemonic(mnemonic, password) return JSON.stringify({ errcode: 0, data: result }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } function scanQRCode(funId, title) { try { jsb.scanQRCode(funId, title) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } function signWithGoogle(funId) { try { jsb.signWithGoogle(funId) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } function signOutGoogle(funId) { try { jsb.signOutGoogle(funId) return JSON.stringify({ errcode: 0, data: 'success' }) } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err }) } } //function toWalletJNI(funId, url) { // try { // jsb.toWallet(url); // return JSON.stringify({errcode: 0}); // } catch(err) { // return JSON.stringify({errcode: 1, errmsg: err}); // } //}