console.log('>> begin load wallet main file.'); !window.jc || !jc.wallet ? new jcwallet.default() : jc.wallet; function promiseCb(funId, promiseFun, dataParser) { dataParser = dataParser || ((v) => v); promiseFun .then((result) => { jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: dataParser(result) })); }) .catch((err) => { let code = err.statusCode || 1; jsb.jcCallback(funId, JSON.stringify({ errcode: code, errmsg: err.message || err })); }); } /** * 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 * 10: client * @param {string} env: dev release * @param {string} account: guest account to bind * @return {string} {token: string, address: string | null} * token: token for wallet services * address: address of wallet if already created (optional) */ function walletLogin(funId, channel, env, account) { channel = parseInt(channel); env = env || 'dev'; console.log('walletLogin: ' + channel); promiseCb(funId, jc.wallet.preLogin(channel, env, account)); } function logout(funId, channel) { channel = parseInt(channel || '0'); promiseCb(funId, jc.wallet.logout(channel)); } 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, useApi) { chain = parseInt(chain); useApi = (useApi && useApi == '1') ? true: false promiseCb(funId, jc.wallet.initInternalWallet(chain, pass, env, useApi), () => { return jc.wallet.nativeAccount; }); } /** * verify if password is correct * compare with local password hash * @param {string} pass password for wallet */ function verifyPassword(funId, pass) { promiseCb(funId, jc.wallet.verifyLocalPass(pass)); } /** * @Deprecated * init third party wallet * @param {number | string} chain chain id */ function initThirdPartyWallet(funId, chain, env, wallettype, provider) { chain = parseInt(chain); wallettype = parseInt(wallettype || '1'); promiseCb(funId, jc.wallet.initThirdPartyWallet(chain, env, wallettype, provider)); } 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 * [{ name: string * type: string * rpc: string * id: number * network?: string * symbol?: string * explorerurl?: string * decimals?: number * }] */ function chainList(funId) { let data = jc.wallet.chainList; promiseCb(funId, Promise.resolve(data)); } /** * current actived chain info * @return {string} JSON string of * { name: string * type: string * rpc: string * id: number * network?: string * symbol?: string * explorerurl?: string * decimals?: number * } */ function currentChain(funId) { let data = jc.wallet.currentChain; promiseCb(funId, Promise.resolve(data)); } /** * change current actived chain */ function changeChain(funId, chainId) { chainId = parseInt(chainId); promiseCb(funId, jc.wallet.updateCurrentChain(chainId)); } /** * [BOTH] get sign for login * @param {string} nonce: nonce from server * @param {string} tips: tips message when sign */ function loginSign(funId, nonce, tips) { promiseCb(funId, jc.wallet.loginSign(nonce, tips)); } /** * get balance of ETH * @param {string} account: account * if account is null, we`ll query for current account of wallet */ function getEthBalance(funId, account) { promiseCb(funId, jc.wallet.chainCommon.getBalance(account)); } /** * send ETH from current account * @param {string} to: target account * @param {string} amount: * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function sendEth(funId, to, amount, estimate) { estimate = (estimate || '0') | 0; promiseCb(funId, jc.wallet.chainCommon.sendEth(to, amount, estimate)); } /** * [BOTH] generate ICON with hashed message * @param {string} msg: * @param {string} diameter: size of icon */ 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.message || err }); } } /** * get symbol and decimal of ERC20, symbol and decimal * @param {string} address: address of ERC20 */ function erc20Info(funId, address) { promiseCb(funId, jc.wallet.erc20Info(address)); } /** * get balance of ERC20 * @param {string} address: * @param {string} account: */ function erc20Balance(funId, address, account) { promiseCb(funId, jc.wallet.erc20Standard.getBalanceOf(address, account)); } /** * send ERC20 token to to * @param {string} address: contract address of ERC20 * @param {string} to: target account * @param {string} amount: amount of token to send * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function sendErc20(funId, address, to, amount, estimate) { estimate = (estimate || '0') | 0; promiseCb(funId, jc.wallet.erc20Standard.transfer({address, to, amount, estimate})); } /** * send ERC721 NFT to to * @param {string} address: contract address of NFT * @param {string} to: target account * @param {string} tokenId: nft id of NFT * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function sendErc721(funId, address, to, tokenId, estimate) { estimate = (estimate || '0') | 0; promiseCb(funId, jc.wallet.erc721Standard.transfer({address, to, tokenId, estimate})); } /** * get balance of ERC721 * @param {string} address: contract address of NFT * @param {string} account: wallet address * @param {string} chainId: chain id, number string */ function erc721Balance(funId, address, account, chainId) { promiseCb(funId, jc.wallet.erc721Standard.getBalance(address, account, chainId)); } /** * get balance of ERC1155 * @param {string} address: contract address of NFT * @param {string} account: wallet address * @param {string} tokenId: nft id of NFT */ function erc1155Balance(funId, address, account, tokenId) { promiseCb(funId, jc.wallet.erc1155Standard.getBalanceOf(address, account, tokenId)); } /** * send ERC1155 to to * @param {string} address: contract address of NFT * @param {string} to: target account * @param {string} tokenIds: nft id of NFT, json string of array * @param {string} amounts: amount of token to send, json string of array * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function sendErc1155(funId, address, to, tokenIds, amounts, estimate) { tokenIds = JSON.parse(tokenIds); amounts = JSON.parse(amounts); estimate = (estimate || '0') | 0; promiseCb(funId, jc.wallet.erc1155Standard.transferBatch({address, to, tokenIds, amounts, estimate})); } /** * show QRCode for content * @param {string} content: content to show */ function showQRCode(funId, content) { jsb.showQRCode(funId, content); promiseCb(funId, Promise.resolve(1)); } /** * show webpage * don't call this when in web page * @param {string} url: url to show */ function showWebPage(funId, url) { try { jsb.showWebPage(funId, url); // jsb.openURL(url); return JSON.stringify({ errcode: 0, data: 1 }); } catch (err) { return JSON.stringify({ errcode: 1, errmsg: err.message || err }); } } /** * show QRCode scaner * @param {string} title: title of scaner */ function scanQRCode(funId, title) { console.log('scanQRCode: ' + title); promiseCb(funId, jc.wallet.nativeSvr.scanQRCode(title)); } /** * export wallet private key * @param {string} pass: password of wallet */ function exportWalletSecKey(funId, pass) { 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 * @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 * @param {string} signature: signature * @param {string} estimate: 1: only estimate gas price */ function mintNFT(funId, address, tokenIds, startTime, saltNonce, signature, estimate) { tokenIds = JSON.parse(tokenIds); estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.mintNFT({ address, tokenIds, startTime, saltNonce, signature, estimate, }), (v) => JSON.stringify(v) ); } // ======= end of interact with contract ======= // ======= begin of pay ======= /** * begin buy crypto with alchemy * @param {string} network: 'mainnet' or 'testnet' * @param {string} crypto: 'CEC' or 'CEG', 'ETH' * @param {string} address: wallet address of user * fiat: 'USD' or 'CNY' * fiatAmount: '100' * payWayCode: '10001' * country: 'US' * accountId: account id of game user * orderId: from pre pay */ function beginPay( funId, network, crypto, address, fiat, fiatAmount, payWayCode, country, accountId, orderId, timestamp, salt, sign ) { promiseCb( funId, jc.wallet.paySvr.alchemyPrePay({ crypto, address, fiat, fiatAmount, payWayCode, country, accountId, orderId, network, timestamp, salt, sign, }) ); } // ======= end of pay ======= // ======= begin of transaction history ======= /** * query eth transaction history * @param {string} start * @param {string} limit * @param {JSON string} moreParam e.g. {timeBegin: 1655716867832, timeEnd: 1655716867832} */ function ethHistory(funId, start, limit, moreParam) { moreParam = moreParam ? JSON.parse(moreParam) : {}; promiseCb(funId, jc.wallet.historySvr.ethRecords(start, limit, moreParam)); } /** * query token transaction history * @param {string} start * @param {string} limit * @param {string} address * @param {string} tokenId * @param {JSON string} moreParam e.g. {timeBegin: 1655716867832, timeEnd: 1655716867832} */ function tokenHistory(funId, start, limit, address, tokenId, moreParam) { moreParam = moreParam ? JSON.parse(moreParam) : {}; var data = { start, limit, address, tokenId }; promiseCb(funId, jc.wallet.historySvr.tokenRecords(data, moreParam)); } // ======= end of transaction history ======= /** * get email info of current user */ function emailInfo(funId) { promiseCb(funId, jc.wallet.emailVerifySvr.checkEmailVerified()); } /** * send code with email * @param {string} email * @param {string} type */ function sendEmailCode(funId, email, type) { promiseCb(funId, jc.wallet.emailVerifySvr.sendEmailCode(email, type)); } /** * verify email with code, and update email * @param {string} email * @param {string} code */ function verifyEmail(funId, email, code) { promiseCb(funId, jc.wallet.emailVerifySvr.updateEmailVerify(email, code)); } /** * check if email had already been registed * @param {string} email */ function checkEmailExists(funId, email) { promiseCb(funId, jc.wallet.emailVerifySvr.isEmailRegister(email)); } /** * regist with email * @param {*} email * @param {*} password * @param {*} code */ function emailRegist(funId, email, password, code) { promiseCb(funId, jc.wallet.emailVerifySvr.registByEmail(email, password, code)); } /** * login with email * @param {*} email * @param {*} password */ function emailLogin(funId, email, password) { promiseCb(funId, jc.wallet.emailLogin(email, password)); } /** * token list of current chain */ function tokenList(funId) { let data = jc.wallet.currentChainCfg.tokens; promiseCb(funId, Promise.resolve(data)); } /** * calc token price of USD * token must already be in DEX * @param {*} tokenName * @param {*} amount */ function tokenPrice(funId, tokenName, amount) { promiseCb(funId, jc.wallet.calcTokenPrice(tokenName, amount)); } /** * fiat list */ function fiatList(funId) { promiseCb(funId, jc.wallet.paySvr.fetchFiatList()); } /** * query price of crypto -> usd * @param {string} crypto * @param {number} chain chain id, */ function getCryptoPriceOfUSD(funId, crypto, chain) { let chainData = jc.wallet.currentChain; if (chain) { chainData = jc.wallet.chainList.find((v) => v.chainId === +chain); } let network = chainData.type !== 'Testnet' ? chainData.network || chainData.symbol : 'ARBITRUM'; network = network || 'ARBITRUM'; promiseCb(funId, jc.wallet.paySvr.queryTokenPrice(network, crypto)); } /** * format price * @param {string} value * @param {string} decimal: decimal of price * @param {string} fixed: fixed of price */ function formatPrice(funId, value, decimal, fixed) { let data = jc.wallet.formatPrice(value, decimal, fixed); promiseCb(funId, Promise.resolve(data)); } // begin of market // begin sell nft with market /** * sell nft with market * @param {string} nftToken: address of nft token to sell * @param {string} currency: address of currency * @param {string} tokenId: token id of nft to sell * @param {string} price: price of nft * @param {string} amount: amount of nft to sell, must be 1 for ERC721 * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function marketSellNFT(funId, nftToken, currency, tokenId, price, amount, estimate) { estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.marketSellNFT({ nftToken, currency, tokenId, price, amount, estimate, }), (v) => JSON.stringify(v) ); } /** * update price of existed order * @param {string} orderId: order id * @param {string} price: new price * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function marketUpdatePrice(funId, orderId, price, estimate) { estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.marketUpdatePrice({ orderId, price, estimate, }), (v) => JSON.stringify(v) ); } /** * cancel order * @param {string} orderId: order id * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function marketCancelOrder(funId, orderId, estimate) { estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.marketCancelOrder({ orderId, estimate, }), (v) => JSON.stringify(v) ); } /** * buy order * @param {string} orderId: order id * @param {string} price: price of order * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function marketBuy(funId, orderId, price, estimate) { estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.marketBuy({ orderId, price, estimate, }), (v) => JSON.stringify(v) ); } /** * get order info from chain * @param {string} orderId: order id */ function marketOrderInfo(funId, orderId) { promiseCb(funId, jc.wallet.jcStandard.marketOrderInfo(orderId)); } /** * buy item of game from market * @param {string} orderId: order id * @param {string} seller: seller address * @param {string} currency: address of currency * @param {string} price: price of order * @param {string} startTime: time for signature * @param {string} saltNonce: nonce for signature * @param {string} signature: signature * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function gameMarketBuy(funId, orderId, seller, currency, price, startTime, saltNonce, signature, estimate) { estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.gameMarketBuy({ orderId, seller, currency, price, startTime, saltNonce, signature, estimate, }), (v) => JSON.stringify(v) ); } // end of market // begin of mall /** * buy item of game from mall * @param {string} orderId: order id * @param {string} currency: address of currency * @param {string} price: price of order * @param {string} startTime: time for signature * @param {string} saltNonce: nonce for signature * @param {string} signature: signature * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function gameMallBuy(funId, orderId, currency, price, startTime, saltNonce, signature, estimate) { estimate = (estimate || '0') | 0; promiseCb( funId, jc.wallet.jcStandard.gameMallBuy({ orderId, currency, price, startTime, saltNonce, signature, estimate, }), (v) => JSON.stringify(v) ); } /** * buy nft from mall * @param {string} currency: address of currency * @param {string} addresses: address of nft token, JSON string of array * @param {string} ids: token id of nft, JSON string of array * @param {string} amounts: amount of nft, JSON string of array * @param {string} values: JSON string, e.g. [orderId, price, startTime, saltNonce] * orderId: order id * price: price of order * startTime: time for signature * saltNonce: nonce for signature * @param {string} signature: signature * @param {string} gas: gas price * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function nftMallBuy(funId, currency, addresses, ids, amounts, values, signature, gas, estimate) { estimate = (estimate || '0') | 0; addresses = JSON.parse(addresses); ids = JSON.parse(ids); amounts = JSON.parse(amounts); values = JSON.parse(values); promiseCb( funId, jc.wallet.jcStandard.mallBuy({ currency, addresses, ids, amounts, values, signature, gas, estimate, }), (v) => JSON.stringify(v) ); } /** * buy ceg with usdt, usdc * @param {string} currency: address of currency * @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( funId, jc.wallet.jcStandard.buyTokenWithErc20({ currency, amount, estimate, }), (v) => JSON.stringify(v) ); } // end of mall // begin of in-app pay /** * query google or ios products with product ids * @param {string} productIds: product id for query, JSON string of array */ function queryGoogleProducts(funId, productIds) { let ids = JSON.parse(productIds); console.log('queryGoogleProducts:: ' + productIds); if (window.JavascriptJavaBridge) { promiseCb(funId, jc.wallet.paySvr.queryGoogleProducts(ids)); } else { promiseCb(funId, jc.wallet.paySvr.queryIOSProducts(ids)); } } /** * query google or ios purchases unfinished */ function queryGooglePurchases(funId) { if (window.JavascriptJavaBridge) { promiseCb(funId, jc.wallet.paySvr.queryGooglePurchases()); } else { promiseCb(funId, jc.wallet.paySvr.queryIOSPurchases()); } } /** * begin google or ios pay * @param {string} productId: product id * @param {string} orderId: order id */ function beginGoogleBuy(funId, productId, orderId) { if (window.JavascriptJavaBridge) { promiseCb(funId, jc.wallet.paySvr.buyGoogleProduct(productId, orderId)); } else { promiseCb(funId, jc.wallet.paySvr.beginIOSPurchase(productId, orderId)); } } // end of in-app pay // begin of staking /** * stake nft * @param {string} nfts: address of nft token, JSON string of array * @param {string} tokenIds: token id of nft, JSON string of array * @param {string} staketimes: staking time of nft, JSON string of array * @param {string} gas: gas price * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function stakeNfts(funId, nfts, tokenIds, staketimes, gas, estimate) { estimate = (estimate || '0') | 0; nfts = JSON.parse(nfts); tokenIds = JSON.parse(tokenIds); staketimes = JSON.parse(staketimes); promiseCb(funId, jc.wallet.jcStandard.stakeNfts({ nfts, tokenIds, staketimes, gas, estimate }), (v) => JSON.stringify(v) ); } /** * redeem nft * @param {string} nfts: address of nft token, JSON string of array * @param {string} tokenIds: token id of nft, JSON string of array * @param {string} gas: gas price * @param {string} estimate: 0: execute; 1: only estimate gas price; default: 0 */ function redeemNfts(funId, nfts, tokenIds, gas, estimate) { estimate = (estimate || '0') | 0; nfts = JSON.parse(nfts); tokenIds = JSON.parse(tokenIds); promiseCb(funId, jc.wallet.jcStandard.redeemNfts({ nfts, tokenIds, gas, estimate }), (v) => JSON.stringify(v)); } /** * query nft stake info * @param {string} nft: address of nft token * @param {string} tokenId: token id of nft */ function nftStakeInfo(funId, nft, tokenId) { promiseCb(funId, jc.wallet.jcStandard.nftStakeInfo({ nft, tokenId })); } // end of staking /** * delete account * delete account will delete game data for current account * wallet for current account will be remained */ function deleteAccount(funId) { promiseCb(funId, jc.wallet.deleteAccount()); } /** * reset wallet address for current account */ function resetWalletAddress(funId) { promiseCb(funId, jc.wallet.resetWalletAddress()); } /** * storage pass with google drive * @param {string} key: current account address * @param {string} val: pass for current account */ function storePassLocal(funId, key, val) { promiseCb(funId, jc.wallet.nativeSvr.storagePass(key, val)); } /** * restore pass from google drive * @param {string} key: current account address */ function restorePassLocal(funId, key) { promiseCb(funId, jc.wallet.nativeSvr.authGetStoragePass(key)); } function getLocalPassState(funId, key) { promiseCb(funId, jc.wallet.nativeSvr.passStorageState(key)); }