260 lines
7.8 KiB
JavaScript
260 lines
7.8 KiB
JavaScript
//require('./plugin/zclient')
|
|
|
|
const EIP721_DOMAIN_DATA = [
|
|
{ name: "name", type: "string" },
|
|
{ name: "version", type: "string" },
|
|
];
|
|
/**
|
|
* 1. 监听事件,
|
|
* wc_uri_change: 生成二维码或者android选择钱包
|
|
* client_connect: 连接成功
|
|
* 2. 调用connectWallet(), 注意: 如果本地有保存session, 那么会直接抛出client_connect事件
|
|
* 3. 在收到client_connect事件后, 调用signForLogin获取签名
|
|
*
|
|
*/
|
|
cc.Class({
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
loginTips: {
|
|
default: "This signature is only used for verify your account",
|
|
tooltip: 'Tip message show on wallet when sign login message',
|
|
},
|
|
showLog: {
|
|
default: true,
|
|
tooltip: "Show debug log?",
|
|
}
|
|
},
|
|
|
|
start() {
|
|
cc.changChainResult = function(resultStr) {
|
|
console.log(resultStr);
|
|
try {
|
|
let result = JSON.parse(resultStr)
|
|
console.log(result)
|
|
} catch(err) {
|
|
console.log('result parse json error: ', err)
|
|
}
|
|
}
|
|
if (this.showLog) {
|
|
// this.log = console.log.bind(window.console, '%c[ChainComp] ', `color: #000; background: #3ABAC9`, ...arguments);
|
|
this.log = console.log.bind(window.console, '[ChainComp] ', ...arguments);
|
|
} else {
|
|
this.log = () => {}
|
|
}
|
|
console.log('start, typeof window:'+ (typeof window !== "undefined") +' addEventListener: '+ (typeof window.addEventListener !== "undefined"))
|
|
console.log(localStorage.getItem('walletconnect'))
|
|
|
|
var pemUrl = cc.url.raw("resources/cacert.pem");
|
|
if (cc.loader.md5Pipe) {
|
|
pemUrl = cc.loader.md5Pipe.transformURL(pemUrl);
|
|
}
|
|
// add cc.ws for zclient
|
|
// zclient use cc.ws for check is current env is ccc
|
|
cc.ws = (url) => {
|
|
if (cc.sys.platform === cc.sys.ANDROID) {
|
|
this.log(
|
|
`: create websocket with ${url}, CA file: ${pemUrl}`
|
|
);
|
|
return new WebSocket(url, null, pemUrl);
|
|
} else {
|
|
this.log(`: create websocket for ${url}`);
|
|
return new WebSocket(url);
|
|
}
|
|
};
|
|
},
|
|
onDestroy() {
|
|
this.connector.off("connect");
|
|
this.connector.off("session_update");
|
|
this.connector.off("disconnect");
|
|
this.connector.off("need_to_wallet");
|
|
},
|
|
_buildSingMsg(nonce) {
|
|
nonce += "";
|
|
const signMsg = {
|
|
tips: this.loginTips,
|
|
nonce,
|
|
};
|
|
|
|
const signObj = {
|
|
types: {
|
|
EIP712Domain: EIP721_DOMAIN_DATA,
|
|
set: [
|
|
{ name: "tips", type: "string" },
|
|
{ name: "nonce", type: "string" },
|
|
],
|
|
},
|
|
primaryType: "set",
|
|
domain: {
|
|
name: "Auth",
|
|
version: "1",
|
|
},
|
|
message: signMsg,
|
|
};
|
|
return signObj;
|
|
},
|
|
_subscribeToEvents(connector) {
|
|
// Subscribe to connection events
|
|
connector.on("connect", (error, payload) => {
|
|
if (error) {
|
|
this.node.emit("client_error", error);
|
|
return;
|
|
}
|
|
|
|
// Get provided accounts and chainId
|
|
const { accounts, chainId } = payload.params[0];
|
|
this.account = accounts[0];
|
|
this.chainId = chainId;
|
|
this.log("connect", accounts, chainId);
|
|
this.node.emit("client_connect", { account: accounts[0], chainId });
|
|
});
|
|
|
|
connector.on('need_to_wallet', (error, payload) => {
|
|
setTimeout(()=> {
|
|
this.jumpNativeWallet();
|
|
}, 2000)
|
|
});
|
|
|
|
connector.on("session_update", (error, payload) => {
|
|
if (error) {
|
|
this.node.emit("client_error", error);
|
|
return;
|
|
}
|
|
|
|
// Get updated accounts and chainId
|
|
const { accounts, chainId } = payload.params[0];
|
|
this.account = accounts[0];
|
|
this.chainId = chainId;
|
|
this.log("session_update", accounts, chainId);
|
|
this.node.emit("client_session_update", {
|
|
account: accounts[0],
|
|
chainId,
|
|
});
|
|
});
|
|
|
|
connector.on("disconnect", (error, payload) => {
|
|
if (error) {
|
|
this.node.emit("client_error", error);
|
|
return;
|
|
}
|
|
// Delete connector
|
|
this.log("disconnect");
|
|
this.node.emit("client_disconnet");
|
|
});
|
|
},
|
|
/**
|
|
* bind this method to login button
|
|
*/
|
|
// async connectWallet() {
|
|
// const connector = zclient.WalletConnect.getInstance({
|
|
// bridge: "https://bridge.walletconnect.org",
|
|
// });
|
|
// this.connector = connector;
|
|
// cc.wc = connector;
|
|
// this._subscribeToEvents(connector);
|
|
// // Check if connection is already established
|
|
// if (!connector.connected) {
|
|
// // create new session
|
|
// await connector.createSession()
|
|
// this.log("wc uri: " + connector.uri);
|
|
// this.node.emit("wc_uri_change", connector.uri);
|
|
// setTimeout(()=> {
|
|
// this.callNativeWallet(connector.uri);
|
|
// }, 5000)
|
|
|
|
|
|
// } else {
|
|
// this.log("already connected. wc uri:" + connector.uri);
|
|
// this.account = connector.accounts[0];
|
|
// this.chainId = connector.chainId;
|
|
// this.node.emit("wc_uri_change", connector.uri);
|
|
// this.node.emit("client_connect", {
|
|
// account: this.account,
|
|
// chainId: this.chainId,
|
|
// });
|
|
// }
|
|
// },
|
|
connectWallet() {
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "connectwallet", "(Ljava/lang/String;)V", 'sth');
|
|
},
|
|
callNativeWallet(url) {
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "connectWallet", "(Ljava/lang/String;)V", url);
|
|
},
|
|
changeChain() {
|
|
let param = {chainId: '0x141'}
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "changeChain", "(Ljava/lang/String;)V", JSON.stringify(param));
|
|
},
|
|
addChain() {
|
|
let params = {
|
|
chainId: "0x141",
|
|
chainName: "KCC Mainnet",
|
|
nativeCurrency: {
|
|
name: "kccToken",
|
|
symbol: "KCS",
|
|
decimals: 18
|
|
},
|
|
blockExplorerUrls: ["https://explorer.kcc.io/en"],
|
|
rpcUrls: ["https://rpc-mainnet.kcc.network"]
|
|
}
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "addChain", "(Ljava/lang/String;)V", JSON.stringify(params));
|
|
},
|
|
jumpNativeWallet() {
|
|
this.log('jump to wallet')
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "connectwallet", "(Ljava/lang/String;)V", `wc:${cc.wc.handshakeTopic}@${cc.wc.version}`);
|
|
},
|
|
/**
|
|
* 断开连接, 并清除本地保存的session
|
|
*/
|
|
disconnect() {
|
|
return this.connector.rejectSession();
|
|
},
|
|
/**
|
|
* 获取签名, 须在client_connect事件后才能执行
|
|
* @param {*} nonce
|
|
* @returns
|
|
*/
|
|
signForLogin(nonce) {
|
|
// console.log(`sign: ${this.connector.connected}`)
|
|
// if (!this.connector || !this.connector.connected) {
|
|
// return false;
|
|
// }
|
|
// // let signObj = this._buildSingMsg(nonce);
|
|
// // let params = [this.account, JSON.stringify(signObj)];
|
|
// // this.connector.signTypedData(params).then((signature)=> {
|
|
// // this.log("signature: " + signature);
|
|
// // return { signature, tips: this.loginTips };
|
|
// // })
|
|
// let signObj = [this.account, '0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79']
|
|
// this.connector.signMessage(signObj).then((signature)=> {
|
|
// this.log("signature: " + signature);
|
|
// return { signature, tips: this.loginTips };
|
|
// })
|
|
var nonce = (Math.random() * 10000 | 0)+'';
|
|
var signData = {
|
|
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: {
|
|
tips: 'sign request',
|
|
nonce: nonce,
|
|
},
|
|
};
|
|
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "signApp", "(Ljava/lang/String;)V", `${JSON.stringify(signData)}`);
|
|
},
|
|
|
|
testSign() {
|
|
const nonce = (Math.random() * 100000) | 0;
|
|
this.signForLogin(nonce);
|
|
},
|
|
});
|