console.log('>>hi tiny wallet3') if (window.JavascriptJavaBridge) { console.log('regist android jsb.reflection') jsb.reflection = new JavascriptJavaBridge(); } else if (window.JavaScriptObjCBridge) { jsb.reflection = new JavaScriptObjCBridge(); } /** * 初始化钱包, 所有操作进行前, 必须调用此方法 * @param {string} password: 用于加密钱包数据的密码 */ function initWallet(funId,password) { try { if ( !window.jc || !jc.wallet ) { var wallet = new jcwallet.default(password); } let address = jc.wallet.currentAccount().address return JSON.stringify({errcode: 0, data: address}); } catch(err) { return 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}); } } /** * 切换当前链 * 切换链需要调用currentAccount方法, 以刷新界面显示 */ function changeChain(funId,chainId) { try { chainId = parseInt(chainId); let data = jc.wallet.updateCurrentChain(chainId); return JSON.stringify({errcode: 0, data}); }catch(err) { return JSON.stringify({errcode: 1, errmsg: err}); } } /** * 获取当前帐户的登录签名 * @param {string} nonce: 从服务端获取的nonce * @param {string} tips: 签名时的提示 */ function loginSign(funId, nonce, tips) { try { let result = jc.wallet.loginSign(nonce, tips); return JSON.stringify({errcode: 0, data: result}); } catch(err) { return 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})); }) } /** * 生成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 { diameter = parseFloat(diameter); let result = jc.wallet.restoreFromMnemonic(mnemonic, password); return JSON.stringify({errcode: 0, data: result}); } catch(err) { return JSON.stringify({errcode: 1, errmsg: err}); } } function _subscribeToEvents(connector) { // Subscribe to connection events connector.on("connect", (error, payload) => { if (error) { console.log('connect wallet error: ' + error); return; } // Get provided accounts and chainId const { accounts, chainId } = payload.params[0]; console.log('connected: ' + accounts[0] + ' ' + chainId); window.account = accounts[0]; }); connector.on("session_update", (error, payload) => { if (error) { console.log('session_update error: ' + error); return; } // Get updated accounts and chainId const { accounts, chainId } = payload.params[0]; console.log("session_update", accounts, chainId); this.node.emit("client_session_update", { account: accounts[0], chainId, }); }); connector.on("disconnect", (error, payload) => { if (error) { console.log('disconnect error: ' + error); return; } // Delete connector console.log("disconnect"); }); } function initWalletConnect() { try { const connector = jcwallet.WalletConnect.getInstance({ bridge: "https://bridge.walletconnect.org" }); console.log(connector); window.connector = connector; this._subscribeToEvents(connector); if (!connector.connected) { // create new session connector.createSession({chainId: 1}) .then(() => { console.log("wc uri: " + connector.uri); setTimeout(function() { jsb.reflection.callStaticMethod( 'com/jc/jcfw/JcSDK', 'connectwallet', '(Ljava/lang/String;)V', connector.uri ) }, 50); }) } else { console.log("already connected. wc uri:" + connector.uri); console.log("account: " + connector.accounts[0]); window.account = connector.accounts[0]; } return JSON.stringify({errcode: 0}); } catch(err) { return JSON.stringify({errcode: 1, errmsg: err}); } } function _buildSingMsg(nonce) { nonce += ""; const signMsg = { tips: 'login tip', nonce, }; const signObj = { types: { EIP712Domain: [ { name: "name", type: "string" }, { name: "version", type: "string" }, ], set: [ { name: "tips", type: "string" }, { name: "nonce", type: "string" }, ], }, primaryType: "set", domain: { name: "Auth", version: "1", }, message: signMsg, }; return signObj; } function signWithExternal() { var nonce = (Math.random() * 100000) | 0; let singObj = _buildSingMsg(nonce); let params = [window.account, JSON.stringify(singObj)]; console.log(params); window.connector.signTypedData(params).then((signature)=> { console.log("signature: " + signature); }).catch (err => { console.log('sign error: ' + err); }) setTimeout(function() { jsb.reflection.callStaticMethod( 'com/jc/jcfw/JcSDK', 'toWallet', '(Ljava/lang/String;)V', 'wc://' ) }, 10); }