230 lines
6.4 KiB
JavaScript
230 lines
6.4 KiB
JavaScript
import init, {
|
|
wget_address,
|
|
hash_pass_svr,
|
|
sign,
|
|
sign_for_tran,
|
|
generate_sec_key,
|
|
generate_scrypt_hash,
|
|
generate_qr,
|
|
encrypt_content,
|
|
decrypt_content,
|
|
} from '../../wasm/rustwallet.js';
|
|
import { QrScanner, showQr } from './qr_reading.js';
|
|
import { GoogleClient } from './google.client.js';
|
|
import { AppleClient } from './apple.client.js';
|
|
// call native method
|
|
|
|
let googleClient = null;
|
|
let appleClient = null;
|
|
let walletCache = [];
|
|
|
|
async function initWasm() {
|
|
if (jc.wallet.platform === 'web') {
|
|
googleClient = new GoogleClient();
|
|
await init();
|
|
await googleClient.initGoolgeClient();
|
|
appleClient = new AppleClient();
|
|
await appleClient.initAppleClient();
|
|
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 });
|
|
appleClient.login(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) {
|
|
if (jc.wallet.platform === 'web') {
|
|
this.scaner = this.scaner || new QrScanner();
|
|
this.scaner.start(title,(result) => {
|
|
this.scaner.stop();
|
|
let data = {errcode: 0, data: result}
|
|
nativeCall(funid, JSON.stringify(data));
|
|
});
|
|
} else {
|
|
callNative({ action: 'scanQRCode', funid, title });
|
|
}
|
|
},
|
|
showQRCode: function (funid, title) {
|
|
if (jc.wallet.platform === 'web') {
|
|
let str = generate_qr(title);
|
|
showQr(str);
|
|
} else {
|
|
callNative({ action: 'showQRCode', 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) {
|
|
if (jc.wallet.platform === 'web') {
|
|
googleClient.downloadCfg(key).then((data) => {
|
|
console.log(data);
|
|
let r = decrypt_content(data.pass, key);
|
|
let result = {errcode: 0, data: r};
|
|
nativeCall(funid, JSON.stringify(result));
|
|
});
|
|
} else {
|
|
callNative({ action: 'authGetStoragePass', funid, key });
|
|
}
|
|
},
|
|
passStorageState: function (funid, key) {
|
|
callNative({ action: 'passStorageState', funid, key });
|
|
},
|
|
storagePass: function (funid, key, pass) {
|
|
if (jc.wallet.platform === 'web') {
|
|
pass = encrypt_content(pass, key);
|
|
googleClient.uploadCfg(key, pass).then((data) => {
|
|
let result = {errcode: 0, data: data.id};
|
|
nativeCall(funid, JSON.stringify(result));
|
|
});
|
|
} else {
|
|
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']);
|
|
const directMethod = new Set([]);
|
|
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();
|
|
})();
|