增加wallet connect
This commit is contained in:
parent
edc04c7b3b
commit
15409bf5e6
@ -84,6 +84,10 @@ android.applicationVariants.all { variant ->
|
||||
include "js/**"
|
||||
into outputDir
|
||||
}
|
||||
copy {
|
||||
from "${sourceDir}/cert/cacert.pem"
|
||||
into outputDir
|
||||
}
|
||||
def sourceDirUnity = rootProject.ext.cfgs.unityAndroidProject + "/src/main/assets/"
|
||||
|
||||
copy {
|
||||
|
@ -1,7 +1,12 @@
|
||||
package com.jc.jcfw;
|
||||
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fitchgc.headlesscocos.MainActivity;
|
||||
|
||||
import org.cocos2dx.lib.CocosJSHelper;
|
||||
|
||||
public class JcSDK {
|
||||
@ -17,4 +22,24 @@ public class JcSDK {
|
||||
CocosJSHelper.initWallet(password);
|
||||
commonCB.stringCallback("wallet init success");
|
||||
}
|
||||
|
||||
public static void connectwallet(String url){
|
||||
// String url = "https://metamask.app.link/wc?uri="+ExampleApplication.config.toWCUri();
|
||||
Uri uri = Uri.parse(url);
|
||||
Log.i(TAG, url);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
|
||||
try {
|
||||
MainActivity.app.startActivity(intent);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://metamask.io/download/"));
|
||||
MainActivity.app.startActivity(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static void toWallet(String url) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(Uri.parse(url));
|
||||
MainActivity.app.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
3347
cert/cacert.pem
Normal file
3347
cert/cacert.pem
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
132
js/main.js
132
js/main.js
@ -1,13 +1,17 @@
|
||||
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: 用于加密钱包数据的密码
|
||||
* @return {string} 当前激活帐户的地址
|
||||
*/
|
||||
function initWallet(funId,password) {
|
||||
try {
|
||||
if ( !window.jc || !jc.wwallet ) {
|
||||
if ( !window.jc || !jc.wallet ) {
|
||||
var wallet = new jcwallet.default(password);
|
||||
}
|
||||
let address = jc.wallet.currentAccount().address
|
||||
@ -200,7 +204,7 @@ function sendErc20(funId, address, to, amount) {
|
||||
})
|
||||
}
|
||||
|
||||
function restoreFromMnemonic(mnemonic, password) {
|
||||
function restoreFromMnemonic(funId, mnemonic, password) {
|
||||
try {
|
||||
diameter = parseFloat(diameter);
|
||||
let result = jc.wallet.restoreFromMnemonic(mnemonic, password);
|
||||
@ -210,3 +214,123 @@ function restoreFromMnemonic(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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user