import init, { wget_address, hash_pass_svr, sign, sign_for_tran, generate_sec_key, generate_scrypt_hash, } from '../../wasm/rustwallet.js'; import { GoogleClient } from './google.client.js'; // call native method let googleClient = null; let walletCache = []; async function initWasm() { if (jc.wallet.platform === 'web') { googleClient = new GoogleClient(); await init(); await googleClient.initGoolgeClient(); window.dispatchEvent(new CustomEvent('envready')); } } function callNative(data) { let dataStr = JSON.stringify(data); console.log('call native: ' + dataStr); if (jc.wallet.platform === 'android_game_web') { cfwallet_JuEd8Ql5over8kneww.pageCall(dataStr); } else if (jc.wallet.platform === 'ios_game_web') { webkit.messageHandlers.pageCall.postMessage(dataStr); } } // for native call js function nativeCall(funid, val) { console.log('from native: ' + val); if (!val) { return; } jc.wallet.nativeSvr.handleNativeCallback(funid, val); } // for proxy method callback function proxyCallback(funid, data) { window.dispatchEvent( new CustomEvent('walletcb', { detail: { funid, data }, }) ); } window.jsb = { jcCallback: function (funid, data) { window.dispatchEvent( new CustomEvent('walletcb', { detail: { funid, data }, }) ); }, // BEGIN:: only for web signWithApple: function (funid) { callNative({ action: 'signWithApple', funid }); }, signWithGoogle: function (funid) { googleClient.login(funid); }, signWithTikTok: function (funid) { callNative({ action: 'signWithTikTok', funid }); }, signWithFacebook: function (funid) { callNative({ action: 'signWithFacebook', funid }); }, // method that returns immediately // id, openid, mast-key, salt, pass prepareWallet: function (...args) { let address = wget_address.apply(this, args); walletCache = args; return address; }, walletSecKey: function (...args) { let address = wget_address.apply(this, args); let key = generate_sec_key.apply(this, args); return JSON.stringify({ address, key }); }, hashSvrPass: function (msg) { return hash_pass_svr(msg); }, walletSign: function (msg) { let args = [...walletCache, msg]; return sign.apply(this, args); }, walletSignTran: function (msg) { let args = [...walletCache, msg]; return sign_for_tran.apply(this, args); }, jcCryptoScrypt: function (...args) { return generate_scrypt_hash.apply(this, args); }, // END:: only for web // BEGIN:: native method scanQRCode: function (funid, title) { callNative({ action: 'scanQRCode', funid, title }); }, buyProduct: function (funid, productId, orderId) { callNative({ action: 'buyProduct', funid, productId, orderId }); }, queryPurchase: function (funid) { callNative({ action: 'queryPurchase', funid }); }, queryProducts: function (funid, productIds) { callNative({ action: 'queryProducts', funid, productIds }); }, finishTransaction: function (funid, transactionId) { callNative({ action: 'finishTransaction', funid, transactionId }); }, authGetStoragePass: function (funid, key) { callNative({ action: 'authGetStoragePass', funid, key }); }, passStorageState: function (funid, key) { callNative({ action: 'passStorageState', funid, key }); }, storagePass: function (funid, key, pass) { callNative({ action: 'storagePass', funid, key, pass }); }, // END:: native method }; function callProxyMethod(...args) { var methodname = args.shift(); var funid = args.shift(); // var params = JSON.stringify(args); callNative({ action: 'proxyMethod', methodname, funid, params: args }); } class Deferred { constructor() { this.promise = new Promise((resolve, reject) => { this._resolve = resolve; this._reject = reject; }); } resolve(value) { this._resolve(value); } reject(reason) { this._reject(reason); } then(onfulfilled, onrejected) { return this.promise.then(onfulfilled, onrejected); } catch(onrejected) { return this.promise.catch(onrejected); } } var cbmap = new Map(); window.addEventListener('walletcb', (e) => { console.log('receive walletcb: ' + JSON.stringify(e.detail)); var data = e.detail; if (cbmap.has(data.funid)) { cbmap.get(data.funid).resolve(data.data); cbmap.delete(data.funid); } }); const directMethod = new Set(['chainList', 'currentChain', 'exportWalletSecKey', 'formatPrice', 'tokenList']); function callMethod(...args) { var methodName = args.shift(); let funid = ((Math.random() * 100000 + Math.random() * 1000) | 0) + ''; args.unshift(funid); if (jc.wallet.platform === 'web' && directMethod.has(methodName)) { return window[methodName].apply(this, args); } let deferred = new Deferred(); cbmap.set(funid, deferred); window[methodName].apply(this, args); return deferred.promise; } window.callMethod = callMethod; window.callProxyMethod = callProxyMethod; window.proxyCallback = proxyCallback; window.nativeCall = nativeCall; window.callNative = callNative; (async () => { await initWasm(); })();