增加一些常用的链交互方法

This commit is contained in:
cebgcontract 2022-10-20 16:46:05 +08:00
parent 8a5a002430
commit 6b20fe9bea
3 changed files with 469 additions and 88 deletions

View File

@ -142,11 +142,12 @@ public class MainActivity extends Activity
mGoogleSignInClient = GoogleSignIn.getClient(this, gso); mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// end of google sign // end of google sign
// begin of google oauth sign
mExecutor = Executors.newSingleThreadExecutor(); mExecutor = Executors.newSingleThreadExecutor();
mAuthStateManager = AuthStateManager.getInstance(this); mAuthStateManager = AuthStateManager.getInstance(this);
mConfiguration = JConfiguration.getInstance(this); mConfiguration = JConfiguration.getInstance(this);
mExecutor.submit(this::initializeAppAuth); mExecutor.submit(this::initializeAppAuth);
// end of google oauth sign
} }
@Override @Override

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
console.log('>>begin load wallet main file') console.log(">>begin load wallet main file");
/** /**
* 初始化钱包, 所有操作进行前, 必须调用此方法 * 初始化钱包, 所有操作进行前, 必须调用此方法
@ -6,35 +6,63 @@ console.log('>>begin load wallet main file')
* @param {string} password: 用于加密钱包数据的密码 * @param {string} password: 用于加密钱包数据的密码
*/ */
function initWallet(funId, type, password) { function initWallet(funId, type, password) {
type = 0 type = 1;
try { try {
var wallet var wallet;
if (!window.jc || !jc.wallet) { if (!window.jc || !jc.wallet) {
wallet = new jcwallet.default({ chain: 322, type, password }) wallet = new jcwallet.default({
chain: 322,
type,
password,
});
} else { } else {
wallet = jc.wallet wallet = jc.wallet;
} }
type = parseInt(type) type = parseInt(type);
if (type === 1) { if (type === 1) {
console.log('wallet init success, begin connect') console.log("wallet init success, begin connect");
wallet wallet
.initThirdPartyWallet() .initThirdPartyWallet()
.then(() => { .then(() => {
console.log('walletconnect connect success') console.log("walletconnect connect success");
var account = jc.wallet.currentAccount() var account = jc.wallet.currentAccount();
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: account })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: account,
})
);
}) })
.catch((err) => { .catch((err) => {
console.log('walletconnect connect error: ' + JSON.stringify(err)) console.log("walletconnect connect error: " + JSON.stringify(err));
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} else { } else {
let address = jc.wallet.currentAccount().address let address = jc.wallet.currentAccount().address;
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: address })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: address,
})
);
} }
} catch (err) { } catch (err) {
console.error('wallet init with error: ' + JSON.stringify(err)) console.error("wallet init with error: " + JSON.stringify(err));
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
} }
} }
/** /**
@ -42,10 +70,16 @@ function initWallet(funId, type, password) {
*/ */
function currentAccount(funId) { function currentAccount(funId) {
try { try {
let data = jc.wallet.currentAccountData let data = jc.wallet.currentAccountData;
return JSON.stringify({ errcode: 0, data }) return JSON.stringify({
errcode: 0,
data,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -53,10 +87,16 @@ function currentAccount(funId) {
*/ */
function accountList(funId) { function accountList(funId) {
try { try {
let data = jc.wallet.accounts let data = jc.wallet.accounts;
return JSON.stringify({ errcode: 0, data }) return JSON.stringify({
errcode: 0,
data,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -64,10 +104,16 @@ function accountList(funId) {
*/ */
function chainList(funId) { function chainList(funId) {
try { try {
let data = jc.wallet.chainList let data = jc.wallet.chainList;
return JSON.stringify({ errcode: 0, data }) return JSON.stringify({
errcode: 0,
data,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -75,10 +121,16 @@ function chainList(funId) {
*/ */
function currentChain(funId) { function currentChain(funId) {
try { try {
let data = jc.wallet.currentChain let data = jc.wallet.currentChain;
return JSON.stringify({ errcode: 0, data }) return JSON.stringify({
errcode: 0,
data,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -87,15 +139,27 @@ function currentChain(funId) {
*/ */
function changeChain(funId, chainId) { function changeChain(funId, chainId) {
// chainId = parseInt(chainId); // chainId = parseInt(chainId);
chainId = 80001 chainId = 80001;
jc.wallet jc.wallet
.updateCurrentChain(chainId) .updateCurrentChain(chainId)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
/** /**
* [BOTH]获取当前帐户的登录签名 * [BOTH]获取当前帐户的登录签名
@ -106,11 +170,23 @@ function loginSign(funId, nonce, tips) {
jc.wallet jc.wallet
.loginSign(nonce, tips) .loginSign(nonce, tips)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
/** /**
* 创建一个新帐号, 并将新建帐号设为当前激活帐号 * 创建一个新帐号, 并将新建帐号设为当前激活帐号
@ -118,10 +194,16 @@ function loginSign(funId, nonce, tips) {
*/ */
function createAccount(funId) { function createAccount(funId) {
try { try {
let result = jc.wallet.createAccount() let result = jc.wallet.createAccount();
return JSON.stringify({ errcode: 0, data: result }) return JSON.stringify({
errcode: 0,
data: result,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -130,10 +212,16 @@ function createAccount(funId) {
*/ */
function importAccount(funId, privateKey) { function importAccount(funId, privateKey) {
try { try {
let address = jc.wallet.importAccount(privateKey) let address = jc.wallet.importAccount(privateKey);
return JSON.stringify({ errcode: 0, data: address }) return JSON.stringify({
errcode: 0,
data: address,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -141,10 +229,16 @@ function importAccount(funId, privateKey) {
*/ */
function selectAccount(funId, address) { function selectAccount(funId, address) {
try { try {
let result = jc.wallet.selectAccount(address) let result = jc.wallet.selectAccount(address);
return JSON.stringify({ errcode: 0, data: result }) return JSON.stringify({
errcode: 0,
data: result,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
@ -156,11 +250,23 @@ function getEthBalance(funId, address) {
jc.wallet jc.wallet
.getBalance(address) .getBalance(address)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
/** /**
* 将当前帐户里的基础代币转账给别人 * 将当前帐户里的基础代币转账给别人
@ -171,11 +277,23 @@ function sendEth(funId, to, amount) {
jc.wallet jc.wallet
.sendEth(to, amount) .sendEth(to, amount)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
/** /**
@ -185,28 +303,46 @@ function sendEth(funId, to, amount) {
*/ */
function generateIcon(funId, msg, diameter) { function generateIcon(funId, msg, diameter) {
try { try {
diameter = parseFloat(diameter) diameter = parseFloat(diameter);
let result = jc.wallet.generateIconData(msg, diameter) let result = jc.wallet.generateIconData(msg, diameter);
return JSON.stringify({ errcode: 0, data: result }) return JSON.stringify({
errcode: 0,
data: result,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
/** /**
* 获取ERC20代币的基本信息, 包括symbol和decimal * 获取ERC20代币的基本信息, 包括symbol和decimal
* 这些信息一般都不会变化, 客户端需缓存这些信息 * 这些信息一般都不会变化, 客户端需缓存这些信息
* @param {string} address: 代币的地址 * @param {string} address: 代币的地址initThirdPartyWallet
*/ */
function erc20Info(funId, address) { function erc20Info(funId, address) {
jc.wallet jc.wallet
.erc20Info(address) .erc20Info(address)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
/** /**
* 获取erc20代币的余额 * 获取erc20代币的余额
@ -217,55 +353,97 @@ function erc20Balance(funId, address, account) {
jc.wallet jc.wallet
.erc20Balance(address, account) .erc20Balance(address, account)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
function sendErc20(funId, address, to, amount) { function sendErc20(funId, address, to, amount) {
jc.wallet jc.wallet
.sendErc20(address, to, amount) .sendErc20(address, to, amount)
.then((result) => { .then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
}) })
.catch((err) => { .catch((err) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })) jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
}) })
);
});
} }
function restoreFromMnemonic(funId, mnemonic, password) { function restoreFromMnemonic(funId, mnemonic, password) {
try { try {
let result = jc.wallet.restoreFromMnemonic(mnemonic, password) let result = jc.wallet.restoreFromMnemonic(mnemonic, password);
return JSON.stringify({ errcode: 0, data: result }) return JSON.stringify({
errcode: 0,
data: result,
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
function scanQRCode(funId, title) { function scanQRCode(funId, title) {
try { try {
jsb.scanQRCode(funId, title) jsb.scanQRCode(funId, title);
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
function signWithGoogle(funId) { function signWithGoogle(funId) {
try { try {
jsb.signWithGoogle(funId) jsb.signWithGoogle(funId);
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
function signOutGoogle(funId) { function signOutGoogle(funId) {
try { try {
jsb.signOutGoogle(funId) jsb.signOutGoogle(funId);
return JSON.stringify({ errcode: 0, data: 'success' }) return JSON.stringify({
errcode: 0,
data: "success",
});
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }) return JSON.stringify({
errcode: 1,
errmsg: err,
});
} }
} }
@ -277,3 +455,205 @@ function signOutGoogle(funId) {
// return JSON.stringify({errcode: 1, errmsg: err}); // return JSON.stringify({errcode: 1, errmsg: err});
// } // }
//} //}
function buyNft721(funId, addresses, values, signature) {
jc.wallet.jcStandard
.buyNft721({
addresses,
values,
signature,
})
.then((result) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
})
.catch((err) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
});
}
function buyNft1155(funId, addresses, values, ids, amounts, signature) {
jc.wallet.jcStandard
.buyNft1155({
addresses,
values,
ids,
amounts,
signature,
})
.then((result) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
})
.catch((err) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
});
}
function evolveNft721(
funId,
nftAddress,
tokenIds,
startTime,
nonce,
signature
) {
{
jc.wallet.jcStandard
.evolve721NFT({
nftAddress,
tokenIds,
startTime,
nonce,
signature,
})
.then((result) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
})
.catch((err) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
});
}
}
function evolveChip(funId, tokenIds, startTime, nonce, signature) {
jc.wallet.jcStandard
.evolveChip({
tokenIds,
startTime,
nonce,
signature,
})
.then((result) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
})
.catch((err) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
});
}
function mintShardBatchUser(
funId,
tokenIds,
amounts,
startTime,
nonce,
signature
) {
jc.wallet.jcStandard
.mintShardBatchUser({
tokenIds,
amounts,
startTime,
nonce,
signature,
})
.then((result) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
})
.catch((err) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
});
}
function shardMixByUser(
funId,
tokenId,
nftType,
payToken,
payAmount,
ids,
amounts,
startTime,
nonce,
signature
) {
jc.wallet.jcStandard
.shardMixByUser({
tokenId,
nftType,
payToken,
payAmount,
ids,
amounts,
startTime,
nonce,
signature,
})
.then((result) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: result,
})
);
})
.catch((err) => {
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 1,
errmsg: err,
})
);
});
}