更新一些js方法的调用
This commit is contained in:
parent
9d9b2cbf37
commit
0d68a31aee
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@
|
|||||||
.externalNativeBuild
|
.externalNativeBuild
|
||||||
.cxx
|
.cxx
|
||||||
local.properties
|
local.properties
|
||||||
|
/app/release/
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#include "JcWallet.h"
|
#include "JcWallet.h"
|
||||||
#include "WalletEvent.h"
|
#include "WalletEvent.h"
|
||||||
|
#include "stdarg.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
|
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
|
||||||
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
|
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
|
||||||
#include "scripting/js-bindings/event/EventDispatcher.h"
|
#include "scripting/js-bindings/event/EventDispatcher.h"
|
||||||
|
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
|
||||||
#include "platform/CCApplication.h"
|
#include "platform/CCApplication.h"
|
||||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
||||||
#include "AppDelegate.h"
|
#include "AppDelegate.h"
|
||||||
@ -19,6 +21,25 @@ NS_CC_BEGIN
|
|||||||
JcWallet *JcWallet::_instance = nullptr;
|
JcWallet *JcWallet::_instance = nullptr;
|
||||||
bool _isStarted = false;
|
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::JcWallet() {
|
||||||
JcWallet::_instance = this;
|
JcWallet::_instance = this;
|
||||||
}
|
}
|
||||||
@ -42,57 +63,70 @@ NS_CC_BEGIN
|
|||||||
JcWallet::_instance = nullptr;
|
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;
|
se::Value value;
|
||||||
jsb_run_code("jc.wallet.currentAccount().address", &value);
|
bool ok = runGlobalMethod("initWallet", args, &value);
|
||||||
static std::string result = value.toString();
|
if (ok && !value.isNullOrUndefined()) {
|
||||||
WalletEvent::Emit("wallet_inited", "{1}");
|
result = value.toString();
|
||||||
WalletEvent::Emit("wallet_inited", "{2}");
|
WalletEvent::Emit("wallet_inited", "{1}");
|
||||||
WalletEvent::Emit("wallet_inited", "{3}");
|
}else {
|
||||||
|
result = "";
|
||||||
|
}
|
||||||
return const_cast<char*>(result.c_str());
|
return const_cast<char*>(result.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) {
|
char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) {
|
||||||
std::string nonce(nonceChar);
|
se::ValueArray args;
|
||||||
std::string tips(tipChar);
|
addToArgArray(&args, nonceChar);
|
||||||
std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')";
|
addToArgArray(&args, tipChar);
|
||||||
|
|
||||||
se::Value value;
|
se::Value value;
|
||||||
jsb_run_code(jsCode, &value);
|
bool ok = runGlobalMethod("loginSign", args, &value);
|
||||||
static std::string result = value.toString();
|
static std::string result;
|
||||||
|
if (ok && !value.isNullOrUndefined()) {
|
||||||
|
result = value.toString();
|
||||||
|
}else {
|
||||||
|
result = "";
|
||||||
|
}
|
||||||
return const_cast<char*>(result.c_str());
|
return const_cast<char*>(result.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
char* JcWallet::createAccount() {
|
char* JcWallet::createAccount() {
|
||||||
se::Value value;
|
// return JcWallet::getInstance()->runJsMethod("createAccount", "");
|
||||||
jsb_run_code("jc.wallet.createAccount()", &value);
|
|
||||||
static std::string result = value.toString();
|
|
||||||
return const_cast<char*>(result.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* JcWallet::importAccount(const char *privateKey){
|
char* JcWallet::importAccount(const char *privateKey){
|
||||||
std::string key(privateKey);
|
// return JcWallet::getInstance()->runJsMethod("importAccount", 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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* JcWallet::runJsMethod(const char *methodName, const char *paramObj) {
|
char* JcWallet::runJsMethod(const char *methodName, int paramCount, char **paramList) {
|
||||||
std::string methodStr(methodName);
|
const char *arg;
|
||||||
std::string paramsStr(paramObj);
|
se::ValueArray args;
|
||||||
std::string jsCode = "jc.wallet."+methodStr+"('" + paramsStr + "')";
|
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;
|
se::Value value;
|
||||||
jsb_run_code(jsCode, &value);
|
bool ok = runGlobalMethod(methodName, args, &value);
|
||||||
static std::string result = value.toString();
|
static std::string result;
|
||||||
|
if (ok && !value.isNullOrUndefined()) {
|
||||||
|
result = value.toString();
|
||||||
|
}else {
|
||||||
|
result = "";
|
||||||
|
}
|
||||||
return const_cast<char*>(result.c_str());
|
return const_cast<char*>(result.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
char* initWallet() {
|
char* initWallet(const char *passwordChar) {
|
||||||
JcWallet::getInstance()->initEnv();
|
JcWallet::getInstance()->initEnv();
|
||||||
return JcWallet::getInstance()->initWallet();
|
return JcWallet::getInstance()->initWallet(passwordChar);
|
||||||
}
|
}
|
||||||
char* signLogin(const char *nonceChar, const char *tipChar) {
|
char* signLogin(const char *nonceChar, const char *tipChar) {
|
||||||
return JcWallet::getInstance()->signLogin(nonceChar, tipChar);
|
return JcWallet::getInstance()->signLogin(nonceChar, tipChar);
|
||||||
@ -103,8 +137,9 @@ NS_CC_BEGIN
|
|||||||
char* importAccount(const char *privateKey) {
|
char* importAccount(const char *privateKey) {
|
||||||
return JcWallet::getInstance()->importAccount(privateKey);
|
return JcWallet::getInstance()->importAccount(privateKey);
|
||||||
}
|
}
|
||||||
char* runWalletMethod(const char *methodName, const char *paramObj) {
|
char* runWalletMethod(const char *methodName, int paramCount, char **paramList) {
|
||||||
return JcWallet::getInstance()->runJsMethod(methodName, paramObj);
|
JcWallet::getInstance()->initEnv();
|
||||||
|
return JcWallet::getInstance()->runJsMethod(methodName, paramCount, paramList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
#include "cocos2d.h"
|
#include "cocos2d.h"
|
||||||
|
#include "stdarg.h"
|
||||||
|
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
|
||||||
#include "base/ccMacros.h"
|
#include "base/ccMacros.h"
|
||||||
NS_CC_BEGIN
|
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 {
|
class CC_DLL JcWallet {
|
||||||
public:
|
public:
|
||||||
void initEnv();
|
void initEnv();
|
||||||
JcWallet();
|
JcWallet();
|
||||||
virtual ~JcWallet();
|
virtual ~JcWallet();
|
||||||
static JcWallet* getInstance() { return _instance; }
|
static JcWallet* getInstance() { return _instance; }
|
||||||
char* initWallet();
|
char* initWallet(const char *passwordChar);
|
||||||
char* signLogin(const char *nonceChar, const char *tipChar);
|
char* signLogin(const char *nonceChar, const char *tipChar);
|
||||||
char* createAccount();
|
char* createAccount();
|
||||||
char* importAccount(const char *privateKey);
|
char* importAccount(const char *privateKey);
|
||||||
char* runJsMethod(const char *methodName, const char *paramObj);
|
char* runJsMethod(const char *methodName, int paramCount, char **paramList);
|
||||||
private:
|
private:
|
||||||
static JcWallet* _instance;
|
static JcWallet* _instance;
|
||||||
};
|
};
|
||||||
|
File diff suppressed because one or more lines are too long
46
js/main.js
46
js/main.js
@ -1,12 +1,34 @@
|
|||||||
console.log('hi tiny cocos')
|
console.log('>>hi tiny wallet3')
|
||||||
var wallet = new jcwallet.default();
|
|
||||||
var account = jc.wallet.currentAccount();
|
function initWallet(password) {
|
||||||
console.log('[WALLET]' + account.address);
|
var wallet = new jcwallet.default(password);
|
||||||
// var web3 = new Web3('https://rpc-testnet.kcc.network')
|
return jc.wallet.currentAccount().address;
|
||||||
// let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
|
}
|
||||||
// web3.eth.accounts.wallet.add(key);
|
|
||||||
// web3.eth.accounts.wallet.save('111111')
|
function currentAccount() {
|
||||||
// window.wallet = web3.eth.accounts.wallet.load('111111')
|
return jc.wallet.currentAccount().address;
|
||||||
// console.log(web3.eth.accounts.wallet[0].address);
|
}
|
||||||
// console.log(web3.eth.accounts.wallet[0].privateKey);
|
|
||||||
// console.log('end of main.js')
|
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
BIN
keys/release
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user