update sth

This commit is contained in:
CounterFire2023 2023-12-13 14:25:19 +08:00
parent 39af283b07
commit 9daf77c83a
3 changed files with 218 additions and 147 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
console.log('>> begin load wallet main file.');
!window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
!window.jc || !jc.wallet ? new jcwallet.default() : jc.wallet;
function promiseCb(funId, promiseFun, dataParser) {
dataParser = dataParser || ((v) => v);
promiseFun
@ -13,15 +13,15 @@ function promiseCb(funId, promiseFun, dataParser) {
}
/**
* oauth login before init internal wallet
* @param {string} channel:
* 0: google,
* 1: apple,
* 2: tiktok,
* 3: facebook,
* 4: twitter
* 5: tg,
* 6: email,
* 7: discord
* @param {string} channel:
* 0: google,
* 1: apple,
* 2: tiktok,
* 3: facebook,
* 4: twitter
* 5: tg,
* 6: email,
* 7: discord
* 10: client
* @param {string} env: dev release
* @param {string} account: guest account to bind
@ -33,21 +33,26 @@ function walletLogin(funId, channel, env, account) {
channel = parseInt(channel);
env = env || 'dev';
console.log('walletLogin: ' + channel);
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.preLogin(channel, env, account));
promiseCb(funId, jc.wallet.preLogin(channel, env, account));
}
function updateGameInfo(funId, info) {
jsb.updateGameInfo(funId, info);
promiseCb(funId, Promise.resolve(1));
}
/**
* init internal wallet with password
* @param {string} chain: chain id
* @param {string} pass: password for wallet
* @param {string} env: dev release
* @param {string} useApi: 1: yes, 0: no
* @return {string} address
* @throws {Error} if password is wrong
*/
function initInternalWallet(funId, chain, pass, env) {
function initInternalWallet(funId, chain, pass, env, useApi) {
chain = parseInt(chain);
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.initInternalWallet(chain, pass, env), () => {
useApi = (useApi && useApi == '1') ? true: false
promiseCb(funId, jc.wallet.initInternalWallet(chain, pass, env, useApi), () => {
return jc.wallet.nativeAccount;
});
}
@ -66,15 +71,20 @@ function verifyPassword(funId, pass) {
*/
function initThirdPartyWallet(funId, chain, env) {
chain = parseInt(chain);
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 1 }) : jc.wallet;
promiseCb(funId, wallet.initThirdPartyWallet(chain, env), () => {
promiseCb(funId, jc.wallet.initThirdPartyWallet(chain, env), () => {
return jc.wallet.currentAccount();
});
}
function initRelayWallet(funId, chain, env) {
chain = parseInt(chain);
promiseCb(funId, jc.wallet.initRelayWallet(chain, env));
}
/**
* all chain list we supported
* @return {string} JSON string of
* @return {string} JSON string of
* [{ name: string
* type: string
* rpc: string
@ -86,12 +96,8 @@ function initThirdPartyWallet(funId, chain, env) {
* }]
*/
function chainList(funId) {
try {
let data = jc.wallet.chainList;
return JSON.stringify({ errcode: 0, data });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err.message || err });
}
let data = jc.wallet.chainList;
promiseCb(funId, Promise.resolve(data));
}
/**
* current actived chain info
@ -107,12 +113,8 @@ function chainList(funId) {
* }
*/
function currentChain(funId) {
try {
let data = jc.wallet.currentChain;
return JSON.stringify({ errcode: 0, data });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err.message || err });
}
let data = jc.wallet.currentChain;
promiseCb(funId, Promise.resolve(data));
}
/**
* change current actived chain
@ -136,7 +138,7 @@ function loginSign(funId, nonce, tips) {
* if account is null, we`ll query for current account of wallet
*/
function getEthBalance(funId, account) {
promiseCb(funId, jc.wallet.getBalance(account));
promiseCb(funId, jc.wallet.chainCommon.getBalance(account));
}
/**
@ -147,7 +149,7 @@ function getEthBalance(funId, account) {
*/
function sendEth(funId, to, amount, estimate) {
estimate = (estimate || '0') | 0;
promiseCb(funId, jc.wallet.sendEth(to, amount, estimate));
promiseCb(funId, jc.wallet.chainCommon.sendEth(to, amount, estimate));
}
/**
@ -178,7 +180,7 @@ function erc20Info(funId, address) {
* @param {string} account:
*/
function erc20Balance(funId, address, account) {
promiseCb(funId, jc.wallet.erc20Balance(address, account));
promiseCb(funId, jc.wallet.erc20Standard.getBalanceOf(address, account));
}
/**
* send ERC20 token to to
@ -189,7 +191,7 @@ function erc20Balance(funId, address, account) {
*/
function sendErc20(funId, address, to, amount, estimate) {
estimate = (estimate || '0') | 0;
promiseCb(funId, jc.wallet.sendErc20(address, to, amount, estimate));
promiseCb(funId, jc.wallet.erc20Standard.transfer({address, to, amount, estimate}));
}
/**
@ -201,7 +203,7 @@ function sendErc20(funId, address, to, amount, estimate) {
*/
function sendErc721(funId, address, to, tokenId, estimate) {
estimate = (estimate || '0') | 0;
promiseCb(funId, jc.wallet.sendNFT(address, to, tokenId, estimate));
promiseCb(funId, jc.wallet.erc721Standard.transfer({address, to, tokenId, estimate}));
}
/**
@ -221,7 +223,7 @@ function erc721Balance(funId, address, account, chainId) {
* @param {string} tokenId: nft id of NFT
*/
function erc1155Balance(funId, address, account, tokenId) {
promiseCb(funId, jc.wallet.erc1155Balance(address, account, tokenId));
promiseCb(funId, jc.wallet.erc1155Standard.getBalanceOf(address, account, tokenId));
}
/**
@ -236,7 +238,7 @@ function sendErc1155(funId, address, to, tokenIds, amounts, estimate) {
tokenIds = JSON.parse(tokenIds);
amounts = JSON.parse(amounts);
estimate = (estimate || '0') | 0;
promiseCb(funId, jc.wallet.sendErc1155(address, to, tokenIds, amounts, estimate));
promiseCb(funId, jc.wallet.erc1155Standard.transferBatch({address, to, tokenIds, amounts, estimate}));
}
/**
@ -244,12 +246,8 @@ function sendErc1155(funId, address, to, tokenIds, amounts, estimate) {
* @param {string} content: content to show
*/
function showQRCode(funId, content) {
try {
jsb.showQRCode(funId, content);
return JSON.stringify({ errcode: 0, data: 1 });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err.message || err });
}
jsb.showQRCode(funId, content);
promiseCb(funId, Promise.resolve(1));
}
/**
* show webpage
@ -280,18 +278,14 @@ function scanQRCode(funId, title) {
* @param {string} pass: password of wallet
*/
function exportWalletSecKey(funId, pass) {
try {
let key = jc.wallet.exportPrivateKey(pass);
return JSON.stringify({ errcode: 0, data: key });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err.message || err });
}
let data = jc.wallet.exportPrivateKey(pass);
promiseCb(funId, Promise.resolve(data));
}
// ======= begin of interact with contract =======
/**
* mint NFT
* @param {string} address: contract address of NFT
* mint NFT
* @param {string} address: contract address of NFT
* @param {string} tokenIds: token id of NFT, JSON string of string array
* @parsm {string} startTime: time of signature generation
* @param {string} saltNonce: nonce of signature
@ -400,8 +394,7 @@ function emailInfo(funId) {
* @param {string} type
*/
function sendEmailCode(funId, email, type) {
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.emailVerifySvr.sendEmailCode(email, type));
promiseCb(funId, jc.wallet.emailVerifySvr.sendEmailCode(email, type));
}
/**
* verify email with code, and update email
@ -417,8 +410,7 @@ function verifyEmail(funId, email, code) {
* @param {string} email
*/
function checkEmailExists(funId, email) {
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.emailVerifySvr.isEmailRegister(email));
promiseCb(funId, jc.wallet.emailVerifySvr.isEmailRegister(email));
}
/**
* regist with email
@ -427,8 +419,7 @@ function checkEmailExists(funId, email) {
* @param {*} code
*/
function emailRegist(funId, email, password, code) {
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.emailVerifySvr.registByEmail(email, password, code));
promiseCb(funId, jc.wallet.emailVerifySvr.registByEmail(email, password, code));
}
/**
@ -437,20 +428,15 @@ function emailRegist(funId, email, password, code) {
* @param {*} password
*/
function emailLogin(funId, email, password) {
const wallet = !window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.emailLogin(email, password));
promiseCb(funId, jc.wallet.emailLogin(email, password));
}
/**
* token list of current chain
*/
function tokenList(funId) {
try {
let data = jc.wallet.currentChainCfg.tokens;
return JSON.stringify({ errcode: 0, data });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err.message || err });
}
let data = jc.wallet.currentChainCfg.tokens;
promiseCb(funId, Promise.resolve(data));
}
/**
* calc token price of USD
@ -490,12 +476,8 @@ function getCryptoPriceOfUSD(funId, crypto, chain) {
* @param {string} fixed: fixed of price
*/
function formatPrice(funId, value, decimal, fixed) {
try {
let data = jc.wallet.formatPrice(value, decimal, fixed);
return JSON.stringify({ errcode: 0, data });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err.message || err });
}
let data = jc.wallet.formatPrice(value, decimal, fixed);
promiseCb(funId, Promise.resolve(data));
}
// begin of market
@ -641,7 +623,7 @@ function gameMallBuy(funId, orderId, currency, price, startTime, saltNonce, sign
);
}
/**
/**
* buy nft from mall
* @param {string} currency: address of currency
* @param {string} addresses: address of nft token, JSON string of array
@ -684,7 +666,7 @@ function nftMallBuy(funId, currency, addresses, ids, amounts, values, signature,
* @param {string} amount: amount of currency
* @param {string} gas: gas price
* @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0
*/
*/
function buyTokenWithErc20(funId, currency, amount, gas, estimate) {
estimate = (estimate || '0') | 0;
promiseCb(
@ -795,7 +777,7 @@ function resetWalletAddress(funId) {
}
/**
* storage pass with google drive
* @param {string} key: current account address
* @param {string} key: current account address
* @param {string} val: pass for current account
*/
function storePassLocal(funId, key, val) {
@ -812,3 +794,4 @@ function restorePassLocal(funId, key) {
function getLocalPassState(funId, key) {
promiseCb(funId, jc.wallet.nativeSvr.passStorageState(key));
}

View File

@ -18,15 +18,15 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
body{background-color:#FFFFFF;padding: 20px;}
button{
padding: 10px 20px;
}
body{background-color:#FFFFFF;padding: 20px;}
button{
padding: 10px 20px;
}
.app{
display: flex;
width: 100%;
height: 100%;
justify-content: flex-start;
justify-content: space-between;
flex-wrap: wrap;
}
.app button {
@ -36,7 +36,6 @@ button{
max-height: 50px;
}
</style>
</head>
<body>
<div class='app' id='app'>
@ -46,13 +45,12 @@ button{
/**
* 加载外部js
*/
var loadSingleScript = function (sub) {
var loadSingleScript = function (src, isModule) {
return new Promise((resolve, reject) => {
let src = sub[0];
console.log(`>> begin load script: ${src}`);
let s = document.createElement('script');
s.async = true;
if (sub[1] == 1)s.type = 'module';
if (isModule)s.type = 'module';
s.src =src;
s.addEventListener('load', function () {
console.log(`<< finish load script: ${src}`);
@ -64,22 +62,29 @@ button{
})
};
var scripts = [
['assets/scripts/libs/jcwallet.js'],
['assets/scripts/libs/main.js'],
['assets/scripts/libs/utils.js'],
'assets/scripts/libs/jcwallet.js',
'assets/scripts/libs/main.js',
'assets/scripts/libs/utils.js',
['assets/scripts/libs/native_bridge.js', 1],
['assets/scripts/libs/main_native_inject.js'],
['assets/scripts/run_sample.js'],
'assets/scripts/libs/main_native_inject.js',
'assets/scripts/run_sample.js',
]
var loadScripts = function (list, refresh = false) {
var loaded = 0;
return new Promise((resolve, reject) => {
var loadNext = function () {
if (refresh) {
list[loaded][0] = list[loaded][0] + (list[loaded][0].indexOf("?") == -1 ? "?" : "&") + "t=" + Date.now();
var current = list[loaded];
var src = current;
var isModule = false;
if (typeof current === 'object' && current instanceof Array) {
src = current[0];
isModule = current.length > 1 && current[1] == 1
}
loadSingleScript(list[loaded]).then(() => {
if (refresh) {
src = src + (src.indexOf("?") == -1 ? "?" : "&") + "t=" + Date.now();
}
loadSingleScript(src, isModule).then(() => {
if (++loaded >= list.length) {
resolve && resolve();
} else {
@ -90,7 +95,7 @@ button{
loadNext();
})
};
loadScripts(scripts);
loadScripts(scripts, false);
</script>
</body>
</html>