632 lines
13 KiB
JavaScript
632 lines
13 KiB
JavaScript
console.log(">>begin load wallet main file");
|
|
|
|
/**
|
|
* init wallet, must call this before all other method
|
|
* @param {string} type: wallet type, 0: internal wallet, 1: third party wallet
|
|
|
|
*/
|
|
function initWallet(funId, type, chain, channel) {
|
|
type = parseInt(type)
|
|
chain = parseInt(chain)
|
|
channel = channel || 0;
|
|
channel = parseInt(channel);
|
|
try {
|
|
var wallet;
|
|
if (!window.jc || !jc.wallet) {
|
|
wallet = new jcwallet.default({
|
|
chain,
|
|
type
|
|
});
|
|
} else {
|
|
wallet = jc.wallet;
|
|
}
|
|
type = parseInt(type);
|
|
if (type === 1) {
|
|
console.log("wallet init success, begin connect");
|
|
wallet
|
|
.initThirdPartyWallet()
|
|
.then(() => {
|
|
console.log("walletconnect connect success");
|
|
var account = jc.wallet.currentAccount();
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: account}));
|
|
})
|
|
.catch((err) => {
|
|
console.log("walletconnect connect error: " + JSON.stringify(err));
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
} else {
|
|
wallet.initInternalWallet(channel)
|
|
.then(() => {
|
|
console.log("internal init success");
|
|
var address = jc.wallet.nativeAccount;
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: address}));
|
|
})
|
|
.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 account for internal wallet
|
|
*/
|
|
function accountList(funId) {
|
|
try {
|
|
let data = jc.wallet.accounts;
|
|
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);
|
|
chainId = 80001;
|
|
jc.wallet
|
|
.updateCurrentChain(chainId)
|
|
.then((result) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: result}));
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
/**
|
|
* [BOTH] get sign for login
|
|
* @param {string} nonce: nonce from server
|
|
* @param {string} tips: tips message when sign
|
|
*/
|
|
function loginSign(funId, nonce, tips) {
|
|
jc.wallet
|
|
.loginSign(nonce, tips)
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
/**
|
|
* add one new account, then active this account
|
|
* @return {string} account actived
|
|
*/
|
|
function createAccount(funId) {
|
|
try {
|
|
let result = jc.wallet.createAccount();
|
|
return JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
});
|
|
} catch (err) {
|
|
return JSON.stringify({
|
|
errcode: 1,
|
|
errmsg: err,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* import account with private key
|
|
* @return {string} account actived
|
|
*/
|
|
function importAccount(funId, privateKey) {
|
|
try {
|
|
let address = jc.wallet.importAccount(privateKey);
|
|
return JSON.stringify({
|
|
errcode: 0,
|
|
data: address,
|
|
});
|
|
} catch (err) {
|
|
return JSON.stringify({
|
|
errcode: 1,
|
|
errmsg: err,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* active one account
|
|
*/
|
|
function selectAccount(funId, address) {
|
|
try {
|
|
let result = jc.wallet.selectAccount(address);
|
|
return JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
});
|
|
} catch (err) {
|
|
return JSON.stringify({
|
|
errcode: 1,
|
|
errmsg: err,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* get balance of ETH
|
|
* @param {string} address: account
|
|
* if account is null, we`ll query for current account of wallet
|
|
*/
|
|
function getEthBalance(funId, address) {
|
|
jc.wallet
|
|
.getBalance(address)
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
/**
|
|
* send ETH from current account
|
|
* @param {string} to: target account
|
|
* @param {string} amount:
|
|
*/
|
|
function sendEth(funId, to, amount) {
|
|
jc.wallet
|
|
.sendEth(to, amount)
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* [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) {
|
|
jc.wallet
|
|
.erc20Info(address)
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
/**
|
|
* get balance of ERC20
|
|
* @param {string} address:
|
|
* @param {string} account:
|
|
*/
|
|
function erc20Balance(funId, address, account) {
|
|
jc.wallet
|
|
.erc20Balance(address, account)
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function sendErc20(funId, address, to, amount) {
|
|
jc.wallet
|
|
.sendErc20(address, to, amount)
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function restoreFromMnemonic(funId, mnemonic, password) {
|
|
try {
|
|
let result = jc.wallet.restoreFromMnemonic(mnemonic, password);
|
|
return JSON.stringify({
|
|
errcode: 0,
|
|
data: result,
|
|
});
|
|
} catch (err) {
|
|
return JSON.stringify({
|
|
errcode: 1,
|
|
errmsg: err,
|
|
});
|
|
}
|
|
}
|
|
|
|
function scanQRCode(funId, title) {
|
|
try {
|
|
console.log('scanQRCode: ' + title)
|
|
jsb.scanQRCode(funId, title);
|
|
} catch (err) {
|
|
return JSON.stringify({
|
|
errcode: 1,
|
|
errmsg: err,
|
|
});
|
|
}
|
|
}
|
|
|
|
function signWithGoogle(funId) {
|
|
jc.wallet.nativeSvr.signWithGoogle(funId)
|
|
.then(res => {
|
|
console.log(`google sign result: ${typeof res}`)
|
|
console.log(res)
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: res}));
|
|
})
|
|
.catch(err => {
|
|
console.log('google sign error: ' + err);
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
})
|
|
}
|
|
|
|
function signOutGoogle(funId) {
|
|
jc.wallet.nativeSvr.signOutGoogle(funId)
|
|
then(res => {
|
|
console.log(`google sign out result: ${typeof res}`)
|
|
console.log(res)
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: res}));
|
|
})
|
|
.catch(err => {
|
|
console.log('google sign out error: ' + err);
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
})
|
|
}
|
|
|
|
//function toWalletJNI(funId, url) {
|
|
// try {
|
|
// jsb.toWallet(url);
|
|
// return JSON.stringify({errcode: 0});
|
|
// } catch(err) {
|
|
// return JSON.stringify({errcode: 1, errmsg: err});
|
|
// }
|
|
//}
|
|
|
|
function buyNft721(funId, addresses, values, signature) {
|
|
addresses = JSON.parse(addresses)
|
|
values = JSON.parse(values)
|
|
jc.wallet.jcStandard
|
|
.buyNft721({
|
|
addresses,
|
|
values,
|
|
signature,
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: JSON.stringify(result),
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function buyNft1155(funId, addresses, values, ids, amounts, signature) {
|
|
addresses = JSON.parse(addresses)
|
|
values = JSON.parse(values)
|
|
ids = JSON.parse(ids)
|
|
amounts = JSON.parse(amounts)
|
|
|
|
jc.wallet.jcStandard
|
|
.buyNft1155({
|
|
addresses,
|
|
values,
|
|
ids,
|
|
amounts,
|
|
signature,
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: JSON.stringify(result),
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function evolveNft721(
|
|
funId,
|
|
nftAddress,
|
|
tokenIds,
|
|
startTime,
|
|
nonce,
|
|
signature
|
|
) {
|
|
tokenIds = JSON.parse(tokenIds)
|
|
jc.wallet.jcStandard
|
|
.evolve721NFT({
|
|
nftAddress,
|
|
tokenIds,
|
|
startTime,
|
|
nonce,
|
|
signature,
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: JSON.stringify(result),
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function evolveChip(funId, tokenIds, startTime, nonce, signature) {
|
|
tokenIds = JSON.parse(tokenIds)
|
|
jc.wallet.jcStandard
|
|
.evolveChip({
|
|
tokenIds,
|
|
startTime,
|
|
nonce,
|
|
signature,
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: JSON.stringify(result),
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function mintShardBatchUser(
|
|
funId,
|
|
tokenIds,
|
|
amounts,
|
|
startTime,
|
|
nonce,
|
|
signature
|
|
) {
|
|
tokenIds = JSON.parse(tokenIds)
|
|
amounts = JSON.parse(amounts)
|
|
|
|
jc.wallet.jcStandard
|
|
.mintShardBatchUser({
|
|
tokenIds,
|
|
amounts,
|
|
startTime,
|
|
nonce,
|
|
signature,
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: JSON.stringify(result),
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
function shardMixByUser(
|
|
funId,
|
|
tokenId,
|
|
nftType,
|
|
payToken,
|
|
payAmount,
|
|
ids,
|
|
amounts,
|
|
startTime,
|
|
nonce,
|
|
signature
|
|
) {
|
|
ids = JSON.parse(ids)
|
|
amounts = JSON.parse(amounts)
|
|
|
|
jc.wallet.jcStandard
|
|
.shardMixByUser({
|
|
tokenId,
|
|
nftType,
|
|
payToken,
|
|
payAmount,
|
|
ids,
|
|
amounts,
|
|
startTime,
|
|
nonce,
|
|
signature,
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(
|
|
funId,
|
|
JSON.stringify({
|
|
errcode: 0,
|
|
data: JSON.stringify(result),
|
|
})
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
|
|
// addresses: [nftId, chip, sign_address]
|
|
// values: [token_id,salt_nonce,startTime]
|
|
// chipIds: [...chipIds]
|
|
function pluginChip(
|
|
funId,
|
|
addresses,
|
|
values,
|
|
chipIds,
|
|
slots,
|
|
signature
|
|
) {
|
|
console.log('addresses:' + addresses)
|
|
console.log('values:' + values)
|
|
console.log('chipIds:' + chipIds)
|
|
console.log('slots:' + slots)
|
|
console.log('signature:' + signature)
|
|
|
|
addresses = JSON.parse(addresses)
|
|
values = JSON.parse(values)
|
|
chipIds = JSON.parse(chipIds)
|
|
slots = JSON.parse(slots)
|
|
jc.wallet.jcStandard
|
|
.pluginChip({
|
|
addresses,
|
|
values,
|
|
chipIds,
|
|
slots,
|
|
signature
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: JSON.stringify(result)}));
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|
|
// addresses: [nftId, chip, sign_address]
|
|
// values: [token_id,salt_nonce,startTime]
|
|
// chipIds: [...chipIds]
|
|
function unplugChip(
|
|
funId,
|
|
addresses,
|
|
values,
|
|
chipIds,
|
|
slots,
|
|
signature
|
|
) {
|
|
|
|
addresses = JSON.parse(addresses)
|
|
values = JSON.parse(values)
|
|
chipIds = JSON.parse(chipIds)
|
|
slots = JSON.parse(slots)
|
|
|
|
jc.wallet.jcStandard
|
|
.unplugChip({
|
|
addresses,
|
|
values,
|
|
chipIds,
|
|
slots,
|
|
signature
|
|
})
|
|
.then((result) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: JSON.stringify(result)}));
|
|
})
|
|
.catch((err) => {
|
|
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
|
|
});
|
|
}
|
|
|