97 lines
2.9 KiB
JavaScript
97 lines
2.9 KiB
JavaScript
var Web3 = require('web3.min')
|
|
cc.Class({
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
webview: cc.WebView,
|
|
debugText: cc.Label,
|
|
noLog: false
|
|
},
|
|
|
|
async start() {
|
|
console.log('main')
|
|
let web3 = new Web3('https://rpc-testnet.kcc.network');
|
|
// let web2 = new Web3('https://rpc-testnet.kcc.network');
|
|
// let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
|
|
// web3.eth.accounts.wallet.load('111111');
|
|
// web3.eth.accounts.wallet.add(key);
|
|
// web3.eth.accounts.wallet.save('111111');
|
|
// let account = web3.eth.accounts.privateKeyToAccount(key);
|
|
// console.log('new account: ')
|
|
// console.log(JSON.stringify(account));
|
|
// let encryptData = web3.eth.accounts.encrypt(account.privateKey, '111111');
|
|
// console.log('encrypt data: ' );
|
|
// console.log(JSON.stringify(encryptData));
|
|
// let privateKey = web3.eth.accounts.decrypt(encryptData, '111111');
|
|
// console.log('decrypt data: ');
|
|
// console.log(JSON.stringify(privateKey));
|
|
// let balance = await web3.eth.getBalance(privateKey.address);
|
|
// console.log(`balance: ${balance}`);
|
|
|
|
// console.log(typeof exports, typeof module, typeof define);
|
|
// this.webview.node.on("loaded", this.callback, this);
|
|
// this.bindWebToMe();
|
|
// this.onClickLogin();
|
|
// var pemUrl = cc.url.raw('resources/walletconnect.cer')
|
|
|
|
},
|
|
|
|
connectWallet() {
|
|
jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "connectWallet");
|
|
},
|
|
|
|
testSign() {
|
|
|
|
},
|
|
|
|
disconnect() {
|
|
|
|
},
|
|
|
|
// update (dt) {},
|
|
onClickLogin() {
|
|
let url = "https://www.cebg.games/nativelogin?a=login";
|
|
this.setDebugText(url);
|
|
this.webview.url = url;
|
|
},
|
|
cocosToWeb() {
|
|
this.webview.node.active = true;
|
|
},
|
|
|
|
bindWebToMe() {
|
|
var scheme = "nativechain";
|
|
//这里是移动端, 接收web传过来的消息
|
|
function jsCallback(target, url) {
|
|
// 这里的返回值是内部页面的 URL 数值,需要自行解析自己需要的数据。
|
|
var str = url.replace(scheme + "://", "");
|
|
str = decodeURIComponent(str);
|
|
let data = JSON.parse(str);
|
|
// webview target
|
|
console.log(JSON.stringify(data));
|
|
}
|
|
this.webview.setJavascriptInterfaceScheme(scheme);
|
|
this.webview.setOnJSCallback(jsCallback);
|
|
},
|
|
callback(event) {
|
|
console.log("webview on loaded");
|
|
console.log(event);
|
|
this.cocosToWeb();
|
|
},
|
|
|
|
onWebFinishLoad: function (sender, event, customEventData) {
|
|
if (event === cc.WebView.EventType.LOADED) {
|
|
this.setDebugText("----webView---loaded---finish!!----");
|
|
this.cocosToWeb();
|
|
} else if (event === cc.WebView.EventType.LOADING) {
|
|
this.setDebugText("----webView---loading----");
|
|
} else if (event === cc.WebView.EventType.ERROR) {
|
|
this.setDebugText("----webView---load---error----");
|
|
}
|
|
},
|
|
|
|
setDebugText(str) {
|
|
console.log("DEBUG:", str);
|
|
this.debugText.string = str;
|
|
},
|
|
});
|