增加sth

This commit is contained in:
cebgcontract 2022-10-28 19:57:20 +08:00
parent 8f1001d240
commit cbac329433
11 changed files with 172 additions and 101 deletions

View File

@ -69,6 +69,7 @@ bool AppDelegate::applicationDidFinishLaunching()
jsb_run_script("js/jcwallet.js");
jsb_run_script("js/platform.js");
jsb_run_script("js/main.js");
jsb_run_script("js/wallet.js");
Application::getInstance()->setKeyMaster("1111");
se->addAfterCleanupHook([]() {
JSBClassType::destroy();

View File

@ -11,13 +11,23 @@
#include "platform/CCApplication.h"
#include "base/CCScheduler.h"
#include "scrypt/native-crypto.h"
#include <jni.h>
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#include "AppDelegate.h"
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#ifndef COM_JC_JCFW_CLASS_NAME
#define COM_JC_JCFW_CLASS_NAME com_jc_jcfw_JcSDK
#endif
#define JNI_JCFW(FUNC) JNI_METHOD1(COM_JC_JCFW_CLASS_NAME,FUNC)
#define JNI_IMP_LOG_TAG "JcWallet"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,JNI_IMP_LOG_TAG,__VA_ARGS__)
cocos2d::Application *cocos_android_app_init(int width, int height);
#endif
@ -140,7 +150,7 @@ NS_CC_BEGIN
}
char *JcWallet::runJsMethod(std::shared_ptr<JSMethodParam> data) {
cocos2d::log("thread: %ld call method %s", uv_thread_self(), data->methodName.c_str());
cocos2d::log("thread: %ld call method %s params: %d", uv_thread_self(), data->methodName.c_str(), data->args.size());
se::Value value;
bool ok = cocos2d::runGlobalMethod(data->methodName.c_str(), data->args, &value);
static std::string result;
@ -217,6 +227,25 @@ NS_CC_BEGIN
});
return result == 0 ? 1 : 0;
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JNIEXPORT jint JNICALL JNI_JCFW(runJS)(JNIEnv *env, jclass clazz, jstring jfunId, jstring jmethodName, jstring jparams) {
std::string funId = JniHelper::jstring2string(jfunId);
std::string methodName = JniHelper::jstring2string(jmethodName);
LOGD("jni call %s | %s", methodName.c_str(), funId.c_str());
JSMethodParam *data = new JSMethodParam();
data->methodName = methodName;
data->funId = funId;
data->paramCount = 1;
addToArgArray(&data->args, funId.c_str());
std::string sstr = JniHelper::jstring2string(jparams);
addToArgArray(&data->args, sstr.c_str());
std::shared_ptr<JSMethodParam> params(data);
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
JcWallet::getInstance()->runJsMethod(params);
});
return result == 0 ? 1 : 0;
}
#endif
}
NS_CC_END
@ -263,3 +292,5 @@ bool jsb_register_walletevent_modules(se::Object* global) {
__jsbObj->defineFunction("jcCallback", _SE(jsb_wallet_callback));
return true;
}

View File

@ -5,6 +5,7 @@
<application
android:allowBackup="true"
android:name=".MainApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"

View File

@ -136,4 +136,6 @@ dependencies {
implementation project(path: ':libcocos2dx')
implementation 'net.openid:appauth:0.11.1'
implementation "com.squareup.okio:okio:2.10.0"
implementation 'com.android.volley:volley:1.2.1'
implementation 'org.greenrobot:eventbus:3.0.0'
}

View File

@ -0,0 +1,5 @@
package com.fitchgc.jcwallet;
public class Constants {
public static final String PREF_NAME = "jcwallet";
}

View File

@ -416,6 +416,7 @@ public class MainActivity extends Activity
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null) {
Log.w(TAG, "already login: " + account.getIdToken());
runOnUiThread(() -> verifyGoogleIDToken(account.getIdToken()));
} else {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
@ -434,6 +435,7 @@ public class MainActivity extends Activity
} else {
Log.w(TAG, "already login, accessToken not expired");
Log.w(TAG, "id token : " + state.getIdToken());
runOnUiThread(() -> verifyGoogleIDToken(state.getIdToken()));
}
} else {
mExecutor.submit(this::doAuth);
@ -452,7 +454,8 @@ public class MainActivity extends Activity
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
Log.w(TAG, "signIn success: ");
Log.w(TAG, "app auth idToken: " + account.getIdToken());
Log.w(TAG, "gsa idToken: " + account.getIdToken());
runOnUiThread(() -> verifyGoogleIDToken(account.getIdToken()));
// Signed in successfully, show authenticated UI.
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
@ -708,7 +711,12 @@ public class MainActivity extends Activity
Log.d(TAG, "login success, auth state: " + state.isAuthorized());
Log.d(TAG, "app auth idToken: " + state.getIdToken());
mAuthStateManager.replace(state);
runOnUiThread(() -> verifyGoogleIDToken(state.getIdToken()));
}
}
@MainThread
private void verifyGoogleIDToken(String idToken) {
JcSDK.googleOauthCb(this.funId, null, idToken);
}
}

View File

@ -0,0 +1,15 @@
package com.fitchgc.jcwallet;
import android.app.Application;
import android.content.SharedPreferences;
public class MainApplication extends Application {
public static MainApplication application;
public static SharedPreferences _pref;
public void onCreate() {
super.onCreate();
application = this;
_pref = getSharedPreferences(Constants.PREF_NAME, 0);
}
}

View File

@ -8,10 +8,14 @@ import android.util.Log;
import com.fitchgc.jcwallet.MainActivity;
import org.cocos2dx.lib.CocosJSHelper;
import org.json.JSONException;
import org.json.JSONObject;
public class JcSDK {
private static final String TAG = JcSDK.class.getSimpleName();
private static UnityCallback commonCB;
private static native int runJS(final String funId, final String methodName, final String params);
public static void initCommonCB(UnityCallback callBack) {
Log.i(TAG, "call init common callback from unity");
commonCB = callBack;
@ -34,7 +38,7 @@ public class JcSDK {
* @param msg
*/
public static void csCallback(String funId, String msg) {
if (funId.indexOf("js_") == 0) {
if (funId != "" && funId.indexOf("js_") == 0) {
commonCB.nativeCallback(funId, msg, 1);
} else {
commonCB.nativeCallback(funId, msg, 0);
@ -74,6 +78,24 @@ public class JcSDK {
public static void signOutGoogle(String funid) {
MainActivity.app.signOutGoogle(funid);
}
public static void googleOauthCb(String funId, String error, String idToken) {
JSONObject result = new JSONObject();
try {
if (error != "" && null != error) {
result.put("errocde", 1);
result.put("errmessage", error);
} else {
result.put("errocde", 0);
result.put("data", idToken);
}
} catch (JSONException e) {
e.printStackTrace();
}
String methodName = "jniCallback";
JcSDK.runJS(funId, methodName, result.toString());
}
}

File diff suppressed because one or more lines are too long

View File

@ -1,19 +1,18 @@
console.log(">>begin load wallet main file");
/**
* 初始化钱包, 所有操作进行前, 必须调用此方法
* @param {string} type: 钱包类型, 0: 内置钱包, 1: 第三方钱包
* @param {string} password: 用于加密钱包数据的密码
* init wallet, must call this before all other method
* @param {string} type: wallet type, 0: internal wallet, 1: third party wallet
*/
function initWallet(funId, type, password) {
type = 1;
function initWallet(funId, type) {
type = 0;
try {
var wallet;
if (!window.jc || !jc.wallet) {
wallet = new jcwallet.default({
chain: 322,
type,
password,
type
});
} else {
wallet = jc.wallet;
@ -26,47 +25,32 @@ function initWallet(funId, type, password) {
.then(() => {
console.log("walletconnect connect success");
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) => {
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 {
let address = jc.wallet.currentAccount().address;
jsb.jcCallback(
funId,
JSON.stringify({
errcode: 0,
data: address,
wallet.initInternalWallet()
.then(() => {
console.log("internal init success");
var address = jc.wallet.nativeAccount;
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: address}));
})
);
.catch((err) => {
console.log("internal wallet error: " + JSON.stringify(err));
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
});
}
} catch (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}));
}
}
/**
* 钱包当前激活的帐号的详细信息
* current account for internal wallet
*/
function currentAccount(funId) {
try {
@ -83,7 +67,7 @@ function currentAccount(funId) {
}
}
/**
* 获取当前链所有帐号列表
* all account for internal wallet
*/
function accountList(funId) {
try {
@ -100,7 +84,7 @@ function accountList(funId) {
}
}
/**
* 获取所有支持的链的列表
* all chain list we supported
*/
function chainList(funId) {
try {
@ -117,7 +101,7 @@ function chainList(funId) {
}
}
/**
* 当前链的信息
* chain active
*/
function currentChain(funId) {
try {
@ -134,8 +118,7 @@ function currentChain(funId) {
}
}
/**
* [BOTH]切换当前链
* 切换链需要调用currentAccount方法, 以刷新界面显示
* [BOTH]change chain
*/
function changeChain(funId, chainId) {
// chainId = parseInt(chainId);
@ -162,9 +145,9 @@ function changeChain(funId, chainId) {
});
}
/**
* [BOTH]获取当前帐户的登录签名
* @param {string} nonce: 从服务端获取的nonce
* @param {string} tips: 签名时的提示
* [BOTH] get sign for login
* @param {string} nonce: nonce from server
* @param {string} tips: tips message when sign
*/
function loginSign(funId, nonce, tips) {
jc.wallet
@ -189,8 +172,8 @@ function loginSign(funId, nonce, tips) {
});
}
/**
* 创建一个新帐号, 并将新建帐号设为当前激活帐号
* @return {string} 当前激活帐户的地址
* add one new account, then active this account
* @return {string} account actived
*/
function createAccount(funId) {
try {
@ -207,8 +190,8 @@ function createAccount(funId) {
}
}
/**
* 用导入一个密钥, 并将导入的帐号设为当前激活帐号
* @return {string} 当前激活帐户的地址
* import account with private key
* @return {string} account actived
*/
function importAccount(funId, privateKey) {
try {
@ -225,7 +208,7 @@ function importAccount(funId, privateKey) {
}
}
/**
* 将一个帐号地址设为当前激活帐号
* active one account
*/
function selectAccount(funId, address) {
try {
@ -242,9 +225,9 @@ function selectAccount(funId, address) {
}
}
/**
* 获取当前链上基础代币的余额
* @param {string} address: 待查询的帐户地址
* 不传的话, 则获取当前帐户的余额
* get balance of ETH
* @param {string} address: account
* if account is null, we`ll query for current account of wallet
*/
function getEthBalance(funId, address) {
jc.wallet
@ -269,9 +252,9 @@ function getEthBalance(funId, address) {
});
}
/**
* 将当前帐户里的基础代币转账给别人
* @param {string} to: 转账目标地址
* @param {string} amount: 转账数量
* send ETH from current account
* @param {string} to: target account
* @param {string} amount:
*/
function sendEth(funId, to, amount) {
jc.wallet
@ -297,9 +280,9 @@ function sendEth(funId, to, amount) {
}
/**
* [BOTH]生成hash图片
* @param {string} msg: 要生成图片的内容
* @param {string} diameter: 图片尺寸
* [BOTH] generate ICON with hashed message
* @param {string} msg:
* @param {string} diameter: size of icon
*/
function generateIcon(funId, msg, diameter) {
try {
@ -318,9 +301,8 @@ function generateIcon(funId, msg, diameter) {
}
/**
* 获取ERC20代币的基本信息, 包括symbol和decimal
* 这些信息一般都不会变化, 客户端需缓存这些信息
* @param {string} address: 代币的地址initThirdPartyWallet
* get symbol and decimal of ERC20, symbol and decimal
* @param {string} address: address of ERC20
*/
function erc20Info(funId, address) {
jc.wallet
@ -345,9 +327,9 @@ function erc20Info(funId, address) {
});
}
/**
* 获取erc20代币的余额
* @param {string} address: 代币地址
* @param {string} account: 所属帐户的地址, 不传该参数的话, 获取当前钱包激活帐户的余额
* get balance of ERC20
* @param {string} address:
* @param {string} account:
*/
function erc20Balance(funId, address, account) {
jc.wallet
@ -422,29 +404,29 @@ function scanQRCode(funId, title) {
}
function signWithGoogle(funId) {
try {
jsb.signWithGoogle(funId);
} catch (err) {
return JSON.stringify({
errcode: 1,
errmsg: err,
});
}
jc.wallet.nativeSvr.signWithGoogle(funId)
.then(res => {
console.log(`google sign result: ${typeof res}`)
console.log(res)
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: res}));
})
.catch(err => {
console.log('google sign error: ' + err);
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
})
}
function signOutGoogle(funId) {
try {
jsb.signOutGoogle(funId);
return JSON.stringify({
errcode: 0,
data: "success",
});
} catch (err) {
return JSON.stringify({
errcode: 1,
errmsg: err,
});
}
jc.wallet.nativeSvr.signOutGoogle(funId)
then(res => {
console.log(`google sign out result: ${typeof res}`)
console.log(res)
jsb.jcCallback(funId,JSON.stringify({errcode: 0,data: res}));
})
.catch(err => {
console.log('google sign out error: ' + err);
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
})
}
//function toWalletJNI(funId, url) {

4
js/wallet.js Normal file
View File

@ -0,0 +1,4 @@
function jniCallback(...args) {
console.log(`jniCallback: ${args[0]}`)
jc.wallet.nativeSvr.handleNativeCallback(...args);
}