2023-03-29 10:50:46 +08:00

460 lines
10 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: result }));
})
.catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err }));
});
}
/**
* init wallet, must call this before all other method
* @param {string} type: wallet type, 0: internal wallet, 1: third party wallet
* @param {string} chain: chain type, 1: ETH, 2: ICON
* @param {string} channel: channel type, 0: google, 1: apple, 2: tiktok, 3: facebook, 4: twitter
*/
function initWallet(funId, type, chain, channel = 0) {
chain = parseInt(chain);
console.log(
`initWallet:: type: ${type}, chain: ${chain}, channel: ${channel}`
);
try {
const wallet =
!window.jc || !jc.wallet
? new jcwallet.default({ chain, type })
: jc.wallet;
if (parseInt(type) === 1) {
console.log("wallet init success, begin connect");
wallet
.initThirdPartyWallet()
.then(() => {
console.log("walletconnect connect success");
jsb.jcCallback(
funId,
JSON.stringify({ errcode: 0, data: jc.wallet.currentAccount() })
);
})
.catch((err) => {
console.log("walletconnect connect error: " + JSON.stringify(err));
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err }));
});
} else {
wallet
.initInternalWallet(parseInt(channel))
.then(() => {
console.log("internal init success");
jsb.jcCallback(
funId,
JSON.stringify({ errcode: 0, data: jc.wallet.nativeAccount })
);
})
.catch((err) => {
console.log("internal wallet error: " + JSON.stringify(err));
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err }));
});
}
} catch (err) {
console.error("wallet init with error: " + JSON.stringify(err));
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err }));
}
}
/**
* current account for internal wallet
*/
function currentAccount(funId) {
try {
let data = jc.wallet.currentAccountData;
return JSON.stringify({ errcode: 0, data });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err });
}
}
/**
* 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 });
}
}
/**
* 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,
});
}
}
/**
* [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 });
}
}
/**
* 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 });
}
}
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 });
}
}
function scanQRCode(funId, title) {
console.log("scanQRCode: " + title);
promiseCb(funId, jc.wallet.nativeSvr.scanQRCode(title));
}
function exportWalletSecKey(funId) {
try {
let key = jsb.walletSecKey(funId);
return JSON.stringify({ errcode: 0, data: key });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err });
}
}
// ======= begin of interact with contract =======
function buyNft721(funId, addresses, values, signature, estimate) {
addresses = JSON.parse(addresses);
values = JSON.parse(values);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.buyNft721({
addresses,
values,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
function buyNft1155(
funId,
addresses,
values,
ids,
amounts,
signature,
estimate
) {
addresses = JSON.parse(addresses);
values = JSON.parse(values);
ids = JSON.parse(ids);
amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.buyNft1155({
addresses,
values,
ids,
amounts,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
function evolveNft721(
funId,
nftAddress,
tokenIds,
startTime,
nonce,
signature,
estimate
) {
tokenIds = JSON.parse(tokenIds);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.evolve721NFT({
nftAddress,
tokenIds,
startTime,
nonce,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
function evolveChip(funId, tokenIds, startTime, nonce, signature, estimate) {
tokenIds = JSON.parse(tokenIds);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.evolveChip({
tokenIds,
startTime,
nonce,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
function mintShardBatchUser(
funId,
tokenIds,
amounts,
startTime,
nonce,
signature,
estimate
) {
tokenIds = JSON.parse(tokenIds);
amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.mintShardBatchUser({
tokenIds,
amounts,
startTime,
nonce,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
function shardMixByUser(
funId,
tokenId,
nftType,
payToken,
payAmount,
ids,
amounts,
startTime,
nonce,
signature,
estimate
) {
ids = JSON.parse(ids);
amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.shardMixByUser({
tokenId,
nftType,
payToken,
payAmount,
ids,
amounts,
startTime,
nonce,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// addresses: [nftId, chip, sign_address]
// values: [token_id,salt_nonce,startTime]
// chipIds: [...chipIds]
function pluginChip(
funId,
addresses,
values,
chipIds,
slots,
signature,
estimate
) {
estimate = (estimate || "0") | 0;
addresses = JSON.parse(addresses);
values = JSON.parse(values);
chipIds = JSON.parse(chipIds);
slots = JSON.parse(slots);
promiseCb(
funId,
jc.wallet.jcStandard.pluginChip({
addresses,
values,
chipIds,
slots,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// addresses: [nftId, chip, sign_address]
// values: [token_id,salt_nonce,startTime]
// chipIds: [...chipIds]
function unplugChip(
funId,
addresses,
values,
chipIds,
slots,
signature,
estimate
) {
addresses = JSON.parse(addresses);
values = JSON.parse(values);
chipIds = JSON.parse(chipIds);
slots = JSON.parse(slots);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.unplugChip({
addresses,
values,
chipIds,
slots,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
}
// ======= end of interact with contract =======
// ======= 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 =======