2023-06-15 19:00:07 +08:00

476 lines
11 KiB
JavaScript

console.log(">> begin load wallet main file.");
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 {*} channel 0: google, 1: apple, 2: tiktok, 3: facebook, 4: twitter 5: tg
*/
function walletLogin(funId, channel) {
channel = parseInt(channel);
console.log("walletLogin: " + channel);
const wallet =
!window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.preLogin(channel));
}
/**
* init internal wallet with password
* @param {number | string} chain chain id
* @param {string} pass
*/
function initInternalWallet(funId, chain, pass, env) {
chain = parseInt(chain);
const wallet =
!window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.initInternalWallet(chain, pass, env), () => {
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));
}
/**
* init third party wallet
* @param {number | string} chain chain id
*/
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), () => {
return jc.wallet.currentAccount();
});
}
/**
* all chain list we supported
*/
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 });
}
}
/**
* chain active
*/
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 });
}
}
/**
* [BOTH]change 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.getBalance(account));
}
/**
* send ETH from current account
* @param {string} to: target account
* @param {string} amount:
* @param {number} estimate: 1: only estimate gas price
*/
function sendEth(funId, to, amount, estimate) {
estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.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.erc20Balance(address, account));
}
/**
* send ERC20 token to to
*/
function sendErc20(funId, address, to, amount, estimate) {
estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.sendErc20(address, to, amount, estimate));
}
/**
* send ERC721 NFT to to
*/
function sendErc721(funId, address, to, tokenId, estimate) {
estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.sendNFT(address, to, tokenId, estimate));
}
/**
* get balance of ERC1155
* @param {string} address:
* @param {string} account:
* @param {string} tokenId:
*/
function erc1155Balance(funId, address, account, tokenId) {
promiseCb(funId, jc.wallet.erc1155Balance(address, account, tokenId));
}
/**
* send ERC1155 to to
*/
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)
);
}
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 });
}
}
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 });
}
}
function scanQRCode(funId, title) {
console.log("scanQRCode: " + title);
promiseCb(funId, jc.wallet.nativeSvr.scanQRCode(title));
}
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 });
}
}
// ======= begin of interact with contract =======
function mintNFT(funId, address, tokenIds, startTime, saltNonce, signature) {
tokenIds = JSON.parse(tokenIds);
promiseCb(
funId,
jc.wallet.jcStandard.mintNFT({
address,
tokenIds,
startTime,
saltNonce,
signature,
}),
(v) => JSON.stringify(v)
);
}
// ======= end of interact with contract =======
// ======= begin of pay =======
/**
* crypto: 'CEC' or 'CEG', 'ETH'
* 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,
crypto,
address,
fiat,
fiatAmount,
payWayCode,
country,
accountId,
orderId
) {
promiseCb(
funId,
jc.wallet.paySvr.alchemyPrePay({
crypto,
address,
fiat,
fiatAmount,
payWayCode,
country,
accountId,
orderId,
})
);
}
// ======= end of pay =======
// ======= begin of market =======
// TODO::
// ======= end of market =======
// ======= begin of transaction history =======
function ethHistory(funId, start, limit) {
promiseCb(funId, jc.wallet.historySvr.ethRecords(start, limit));
}
function tokenHistory(funId, start, limit, address, tokenId) {
var data = { start, limit, address, tokenId };
promiseCb(funId, jc.wallet.historySvr.tokenRecords(data));
}
// ======= end of transaction history =======
/**
* get email info of current user
*/
function emailInfo(funId) {
promiseCb(funId, jc.wallet.emailVerifySvr.checkEmailVerified());
}
/**
* send code with email
* @param {*} email
* @param {*} 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));
}
/**
* verify email with code, and update email
* @param {*} email
* @param {*} code
*/
function verifyEmail(funId, email, code) {
promiseCb(funId, jc.wallet.emailVerifySvr.updateEmailVerify(email, code));
}
/**
* check if email had already been registed
* @param {*} email
*/
function checkEmailExists(funId, email) {
const wallet =
!window.jc || !jc.wallet ? new jcwallet.default({ type: 0 }) : jc.wallet;
promiseCb(funId, wallet.emailVerifySvr.isEmailRegister(email));
}
/**
* regist with email
* @param {*} email
* @param {*} password
* @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));
}
/**
* login with email
* @param {*} email
* @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));
}
/**
* 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 });
}
}
/**
* 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());
}
/**
* format 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 });
}
}
// begin of market
// begin sell nft with market
function marketSellNFT(
funId,
nftToken,
currency,
tokenId,
price,
amount,
estimate
) {
promiseCb(
funId,
jc.wallet.jcStandard.marketSellNFT({
nftToken,
currency,
tokenId,
price,
amount,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// update price of order
function marketUpdatePrice(funId, orderId, price, estimate) {
promiseCb(
funId,
jc.wallet.jcStandard.marketUpdatePrice({
orderId,
price,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// cancel order
function marketCancelOrder(funId, orderId, estimate) {
promiseCb(
funId,
jc.wallet.jcStandard.marketCancelOrder({
orderId,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// buy order
function marketBuy(funId, orderId, estimate) {
promiseCb(
funId,
jc.wallet.jcStandard.marketBuy({
orderId,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// end of market
// begin of mall
// buy item of game from mall
function gameMallBuy(
funId,
orderId,
currency,
price,
startTime,
saltNonce,
signature,
estimate
) {
promiseCb(
funId,
jc.wallet.jcStandard.gameMallBuy({
orderId,
currency,
price,
startTime,
saltNonce,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// end of mall