调整walletconnect相关逻辑
This commit is contained in:
parent
15409bf5e6
commit
2126bbf3a9
@ -67,6 +67,7 @@ bool AppDelegate::applicationDidFinishLaunching()
|
|||||||
se::AutoHandleScope hs;
|
se::AutoHandleScope hs;
|
||||||
jsb_run_script("js/jsb-adapter/jsb-builtin.js");
|
jsb_run_script("js/jsb-adapter/jsb-builtin.js");
|
||||||
jsb_run_script("js/jcwallet.js");
|
jsb_run_script("js/jcwallet.js");
|
||||||
|
jsb_run_script("js/platform.js");
|
||||||
jsb_run_script("js/main.js");
|
jsb_run_script("js/main.js");
|
||||||
|
|
||||||
se->addAfterCleanupHook([]() {
|
se->addAfterCleanupHook([]() {
|
||||||
|
@ -5,6 +5,7 @@ ext{
|
|||||||
// js钱包项目路径
|
// js钱包项目路径
|
||||||
jsFilePath: '/Users/zhl/Documents/workspace/android/HeadlessCocos',
|
jsFilePath: '/Users/zhl/Documents/workspace/android/HeadlessCocos',
|
||||||
// unity export的android项目的根路径
|
// unity export的android项目的根路径
|
||||||
unityAndroidProject: '/Users/zhl/Documents/workspace/unity/first/android/tebg'
|
// unityAndroidProject: '/Users/zhl/Documents/workspace/unity/first/android/tebg'
|
||||||
|
unityAndroidProject: '/Users/zhl/Documents/workspace/unity/first/first/target/android/tebg'
|
||||||
]
|
]
|
||||||
}
|
}
|
File diff suppressed because one or more lines are too long
180
js/main.js
180
js/main.js
@ -1,22 +1,25 @@
|
|||||||
console.log('>>hi tiny wallet3')
|
console.log('>>hi tiny wallet3')
|
||||||
if (window.JavascriptJavaBridge) {
|
|
||||||
console.log('regist android jsb.reflection')
|
|
||||||
jsb.reflection = new JavascriptJavaBridge();
|
|
||||||
} else if (window.JavaScriptObjCBridge) {
|
|
||||||
jsb.reflection = new JavaScriptObjCBridge();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* 初始化钱包, 所有操作进行前, 必须调用此方法
|
* 初始化钱包, 所有操作进行前, 必须调用此方法
|
||||||
* @param {string} password: 用于加密钱包数据的密码
|
* @param {string} password: 用于加密钱包数据的密码
|
||||||
*/
|
*/
|
||||||
function initWallet(funId,password) {
|
function initWallet(funId, password) {
|
||||||
try {
|
try {
|
||||||
|
var wallet;
|
||||||
if ( !window.jc || !jc.wallet ) {
|
if ( !window.jc || !jc.wallet ) {
|
||||||
var wallet = new jcwallet.default(password);
|
wallet = new jcwallet.default({chain: 322, type: 1, password});
|
||||||
|
} else {
|
||||||
|
wallet = jc.wallet;
|
||||||
}
|
}
|
||||||
let address = jc.wallet.currentAccount().address
|
wallet.wConnect.connect()
|
||||||
return JSON.stringify({errcode: 0, data: address});
|
.then(() => {
|
||||||
|
console.log(' walletconnect inited')
|
||||||
|
})
|
||||||
|
// let address = jc.wallet.currentAccount().address
|
||||||
|
return JSON.stringify({errcode: 0, data: ''});
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
|
console.log(JSON.stringify(err));
|
||||||
return JSON.stringify({errcode: 1, errmsg: err});
|
return JSON.stringify({errcode: 1, errmsg: err});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,30 +69,33 @@ function currentChain(funId) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 切换当前链
|
* [BOTH]切换当前链
|
||||||
* 切换链需要调用currentAccount方法, 以刷新界面显示
|
* 切换链需要调用currentAccount方法, 以刷新界面显示
|
||||||
*/
|
*/
|
||||||
function changeChain(funId,chainId) {
|
function changeChain(funId, chainId) {
|
||||||
try {
|
// chainId = parseInt(chainId);
|
||||||
chainId = parseInt(chainId);
|
chainId = 80001;
|
||||||
let data = jc.wallet.updateCurrentChain(chainId);
|
jc.wallet.updateCurrentChain(chainId)
|
||||||
return JSON.stringify({errcode: 0, data});
|
.then(result => {
|
||||||
}catch(err) {
|
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||||
return JSON.stringify({errcode: 1, errmsg: err});
|
})
|
||||||
}
|
.catch(err => {
|
||||||
|
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 获取当前帐户的登录签名
|
* [BOTH]获取当前帐户的登录签名
|
||||||
* @param {string} nonce: 从服务端获取的nonce
|
* @param {string} nonce: 从服务端获取的nonce
|
||||||
* @param {string} tips: 签名时的提示
|
* @param {string} tips: 签名时的提示
|
||||||
*/
|
*/
|
||||||
function loginSign(funId, nonce, tips) {
|
function loginSign(funId, nonce, tips) {
|
||||||
try {
|
jc.wallet.loginSign(nonce, tips)
|
||||||
let result = jc.wallet.loginSign(nonce, tips);
|
.then(result => {
|
||||||
return JSON.stringify({errcode: 0, data: result});
|
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||||
} catch(err) {
|
})
|
||||||
return JSON.stringify({errcode: 1, errmsg: err});
|
.catch(err => {
|
||||||
}
|
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 创建一个新帐号, 并将新建帐号设为当前激活帐号
|
* 创建一个新帐号, 并将新建帐号设为当前激活帐号
|
||||||
@ -154,7 +160,7 @@ function sendEth(funId, to, amount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成hash图片
|
* [BOTH]生成hash图片
|
||||||
* @param {string} msg: 要生成图片的内容
|
* @param {string} msg: 要生成图片的内容
|
||||||
* @param {string} diameter: 图片尺寸
|
* @param {string} diameter: 图片尺寸
|
||||||
*/
|
*/
|
||||||
@ -214,123 +220,3 @@ function restoreFromMnemonic(funId, mnemonic, password) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function _subscribeToEvents(connector) {
|
|
||||||
// Subscribe to connection events
|
|
||||||
connector.on("connect", (error, payload) => {
|
|
||||||
if (error) {
|
|
||||||
console.log('connect wallet error: ' + error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get provided accounts and chainId
|
|
||||||
const { accounts, chainId } = payload.params[0];
|
|
||||||
console.log('connected: ' + accounts[0] + ' ' + chainId);
|
|
||||||
window.account = accounts[0];
|
|
||||||
});
|
|
||||||
|
|
||||||
connector.on("session_update", (error, payload) => {
|
|
||||||
if (error) {
|
|
||||||
console.log('session_update error: ' + error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get updated accounts and chainId
|
|
||||||
const { accounts, chainId } = payload.params[0];
|
|
||||||
console.log("session_update", accounts, chainId);
|
|
||||||
this.node.emit("client_session_update", {
|
|
||||||
account: accounts[0],
|
|
||||||
chainId,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
connector.on("disconnect", (error, payload) => {
|
|
||||||
if (error) {
|
|
||||||
console.log('disconnect error: ' + error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Delete connector
|
|
||||||
console.log("disconnect");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function initWalletConnect() {
|
|
||||||
try {
|
|
||||||
const connector = jcwallet.WalletConnect.getInstance({
|
|
||||||
bridge: "https://bridge.walletconnect.org"
|
|
||||||
});
|
|
||||||
console.log(connector);
|
|
||||||
window.connector = connector;
|
|
||||||
this._subscribeToEvents(connector);
|
|
||||||
if (!connector.connected) {
|
|
||||||
// create new session
|
|
||||||
connector.createSession({chainId: 1})
|
|
||||||
.then(() => {
|
|
||||||
console.log("wc uri: " + connector.uri);
|
|
||||||
setTimeout(function() {
|
|
||||||
jsb.reflection.callStaticMethod(
|
|
||||||
'com/jc/jcfw/JcSDK',
|
|
||||||
'connectwallet',
|
|
||||||
'(Ljava/lang/String;)V',
|
|
||||||
connector.uri
|
|
||||||
)
|
|
||||||
}, 50);
|
|
||||||
})
|
|
||||||
|
|
||||||
} else {
|
|
||||||
console.log("already connected. wc uri:" + connector.uri);
|
|
||||||
console.log("account: " + connector.accounts[0]);
|
|
||||||
window.account = connector.accounts[0];
|
|
||||||
}
|
|
||||||
return JSON.stringify({errcode: 0});
|
|
||||||
} catch(err) {
|
|
||||||
return JSON.stringify({errcode: 1, errmsg: err});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _buildSingMsg(nonce) {
|
|
||||||
nonce += "";
|
|
||||||
const signMsg = {
|
|
||||||
tips: 'login tip',
|
|
||||||
nonce,
|
|
||||||
};
|
|
||||||
|
|
||||||
const signObj = {
|
|
||||||
types: {
|
|
||||||
EIP712Domain: [
|
|
||||||
{ name: "name", type: "string" },
|
|
||||||
{ name: "version", type: "string" },
|
|
||||||
],
|
|
||||||
set: [
|
|
||||||
{ name: "tips", type: "string" },
|
|
||||||
{ name: "nonce", type: "string" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
primaryType: "set",
|
|
||||||
domain: {
|
|
||||||
name: "Auth",
|
|
||||||
version: "1",
|
|
||||||
},
|
|
||||||
message: signMsg,
|
|
||||||
};
|
|
||||||
return signObj;
|
|
||||||
}
|
|
||||||
function signWithExternal() {
|
|
||||||
var nonce = (Math.random() * 100000) | 0;
|
|
||||||
let singObj = _buildSingMsg(nonce);
|
|
||||||
let params = [window.account, JSON.stringify(singObj)];
|
|
||||||
console.log(params);
|
|
||||||
window.connector.signTypedData(params).then((signature)=> {
|
|
||||||
console.log("signature: " + signature);
|
|
||||||
}).catch (err => {
|
|
||||||
console.log('sign error: ' + err);
|
|
||||||
})
|
|
||||||
|
|
||||||
setTimeout(function() {
|
|
||||||
jsb.reflection.callStaticMethod(
|
|
||||||
'com/jc/jcfw/JcSDK',
|
|
||||||
'toWallet',
|
|
||||||
'(Ljava/lang/String;)V',
|
|
||||||
'wc://'
|
|
||||||
)
|
|
||||||
}, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
18
js/platform.js
Normal file
18
js/platform.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
if (window.JavascriptJavaBridge) {
|
||||||
|
console.log('regist android jsb.reflection')
|
||||||
|
jsb.reflection = new JavascriptJavaBridge();
|
||||||
|
} else if (window.JavaScriptObjCBridge) {
|
||||||
|
jsb.reflection = new JavaScriptObjCBridge();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.jumpToWallet = function(url) {
|
||||||
|
console.log(url);
|
||||||
|
setTimeout(function() {
|
||||||
|
jsb.reflection.callStaticMethod(
|
||||||
|
'com/jc/jcfw/JcSDK',
|
||||||
|
'toWallet',
|
||||||
|
'(Ljava/lang/String;)V',
|
||||||
|
url || 'wc://'
|
||||||
|
)
|
||||||
|
}, 6000)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user