更新一些js方法的调用

This commit is contained in:
cebgcontract 2022-07-19 19:14:02 +08:00
parent 9d9b2cbf37
commit 0d68a31aee
6 changed files with 117 additions and 55 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
/app/release/

View File

@ -1,9 +1,11 @@
#include "JcWallet.h"
#include "WalletEvent.h"
#include "stdarg.h"
#include <string>
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
#include "scripting/js-bindings/event/EventDispatcher.h"
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
#include "platform/CCApplication.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#include "AppDelegate.h"
@ -19,6 +21,25 @@ NS_CC_BEGIN
JcWallet *JcWallet::_instance = nullptr;
bool _isStarted = false;
bool runGlobalMethod(const char *name, se::ValueArray args, se::Value *value) {
se::AutoHandleScope scope;
bool ok = false;
auto global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Value func;
if (global->getProperty(name, &func) && func.isObject()) {
ok = func.toObject()->call(args, global, value);
}
return ok;
}
bool addToArgArray(se::ValueArray *args, const char *valChar) {
std::string strVal(valChar);
se::Value tmpVal;
bool ok = true;
ok &= std_string_to_seval(strVal, &tmpVal);
args->push_back(tmpVal);
return ok;
}
JcWallet::JcWallet() {
JcWallet::_instance = this;
}
@ -42,57 +63,70 @@ NS_CC_BEGIN
JcWallet::_instance = nullptr;
}
char* JcWallet::initWallet() {
char* JcWallet::initWallet(const char *passwordChar) {
static std::string result;
se::ValueArray args;
addToArgArray(&args, passwordChar);
se::Value value;
jsb_run_code("jc.wallet.currentAccount().address", &value);
static std::string result = value.toString();
bool ok = runGlobalMethod("initWallet", args, &value);
if (ok && !value.isNullOrUndefined()) {
result = value.toString();
WalletEvent::Emit("wallet_inited", "{1}");
WalletEvent::Emit("wallet_inited", "{2}");
WalletEvent::Emit("wallet_inited", "{3}");
}else {
result = "";
}
return const_cast<char*>(result.c_str());
}
char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) {
std::string nonce(nonceChar);
std::string tips(tipChar);
std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')";
se::ValueArray args;
addToArgArray(&args, nonceChar);
addToArgArray(&args, tipChar);
se::Value value;
jsb_run_code(jsCode, &value);
static std::string result = value.toString();
bool ok = runGlobalMethod("loginSign", args, &value);
static std::string result;
if (ok && !value.isNullOrUndefined()) {
result = value.toString();
}else {
result = "";
}
return const_cast<char*>(result.c_str());
}
char* JcWallet::createAccount() {
se::Value value;
jsb_run_code("jc.wallet.createAccount()", &value);
static std::string result = value.toString();
return const_cast<char*>(result.c_str());
// return JcWallet::getInstance()->runJsMethod("createAccount", "");
}
char* JcWallet::importAccount(const char *privateKey){
std::string key(privateKey);
std::string jsCode = "jc.wallet.importAccount('" + key + "')";
se::Value value;
jsb_run_code(jsCode, &value);
static std::string result = value.toString();
return const_cast<char*>(result.c_str());
// return JcWallet::getInstance()->runJsMethod("importAccount", privateKey);
}
char* JcWallet::runJsMethod(const char *methodName, const char *paramObj) {
std::string methodStr(methodName);
std::string paramsStr(paramObj);
std::string jsCode = "jc.wallet."+methodStr+"('" + paramsStr + "')";
char* JcWallet::runJsMethod(const char *methodName, int paramCount, char **paramList) {
const char *arg;
se::ValueArray args;
cocos2d::log("call method %s", methodName);
for (int i = 0; i < paramCount; i++) {
arg = *(paramList + i);
cocos2d::log("call method param: %d, val: %s", i, arg);
addToArgArray(&args, arg);
}
se::Value value;
jsb_run_code(jsCode, &value);
static std::string result = value.toString();
bool ok = runGlobalMethod(methodName, args, &value);
static std::string result;
if (ok && !value.isNullOrUndefined()) {
result = value.toString();
}else {
result = "";
}
return const_cast<char*>(result.c_str());
}
extern "C"
{
char* initWallet() {
char* initWallet(const char *passwordChar) {
JcWallet::getInstance()->initEnv();
return JcWallet::getInstance()->initWallet();
return JcWallet::getInstance()->initWallet(passwordChar);
}
char* signLogin(const char *nonceChar, const char *tipChar) {
return JcWallet::getInstance()->signLogin(nonceChar, tipChar);
@ -103,8 +137,9 @@ NS_CC_BEGIN
char* importAccount(const char *privateKey) {
return JcWallet::getInstance()->importAccount(privateKey);
}
char* runWalletMethod(const char *methodName, const char *paramObj) {
return JcWallet::getInstance()->runJsMethod(methodName, paramObj);
char* runWalletMethod(const char *methodName, int paramCount, char **paramList) {
JcWallet::getInstance()->initEnv();
return JcWallet::getInstance()->runJsMethod(methodName, paramCount, paramList);
}
}

View File

@ -1,17 +1,21 @@
#include "cocos2d.h"
#include "stdarg.h"
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
#include "base/ccMacros.h"
NS_CC_BEGIN
bool addToArgArray(se::ValueArray *args, const char *val);
bool runGlobalMethod(const char *name, se::ValueArray args, se::Value *value);
class CC_DLL JcWallet {
public:
void initEnv();
JcWallet();
virtual ~JcWallet();
static JcWallet* getInstance() { return _instance; }
char* initWallet();
char* initWallet(const char *passwordChar);
char* signLogin(const char *nonceChar, const char *tipChar);
char* createAccount();
char* importAccount(const char *privateKey);
char* runJsMethod(const char *methodName, const char *paramObj);
char* runJsMethod(const char *methodName, int paramCount, char **paramList);
private:
static JcWallet* _instance;
};

File diff suppressed because one or more lines are too long

View File

@ -1,12 +1,34 @@
console.log('hi tiny cocos')
var wallet = new jcwallet.default();
var account = jc.wallet.currentAccount();
console.log('[WALLET]' + account.address);
// var web3 = new Web3('https://rpc-testnet.kcc.network')
// let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
// web3.eth.accounts.wallet.add(key);
// web3.eth.accounts.wallet.save('111111')
// window.wallet = web3.eth.accounts.wallet.load('111111')
// console.log(web3.eth.accounts.wallet[0].address);
// console.log(web3.eth.accounts.wallet[0].privateKey);
// console.log('end of main.js')
console.log('>>hi tiny wallet3')
function initWallet(password) {
var wallet = new jcwallet.default(password);
return jc.wallet.currentAccount().address;
}
function currentAccount() {
return jc.wallet.currentAccount().address;
}
function loginSign(nonce, tips) {
return jc.wallet.loginSign(nonce, tips);
}
function createAccount() {
return jc.wallet.createAccount();
}
function importAccount(privateKey) {
return jc.wallet.importAccount(privateKey);
}
function selectAccount(address) {
return jc.wallet.selectAccount(address);
}
function getBalance(address) {
return jc.wallet.getBalance(address);
}
function sendEth(to, amount) {
jc.wallet.sendEth(to, amount);
}

BIN
keys/release Normal file

Binary file not shown.