147 lines
4.7 KiB
C++
147 lines
4.7 KiB
C++
#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"
|
|
#endif
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
cocos2d::Application *cocos_android_app_init(int width, int height);
|
|
#endif
|
|
using namespace cocos2d;
|
|
NS_CC_BEGIN
|
|
|
|
cocos2d::Application *g_app = nullptr;
|
|
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;
|
|
}
|
|
|
|
void JcWallet::initEnv() {
|
|
if (!_isStarted) {
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
g_app = new AppDelegate(1, 1);
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
g_app = cocos_android_app_init(1, 1);
|
|
#endif
|
|
EventDispatcher::init();
|
|
g_app->start();
|
|
_isStarted = true;
|
|
}
|
|
}
|
|
|
|
JcWallet::~JcWallet() {
|
|
EventDispatcher::destroy();
|
|
se::ScriptEngine::destroyInstance();
|
|
JcWallet::_instance = nullptr;
|
|
}
|
|
|
|
char* JcWallet::initWallet(const char *passwordChar) {
|
|
static std::string result;
|
|
se::ValueArray args;
|
|
addToArgArray(&args, passwordChar);
|
|
se::Value value;
|
|
bool ok = runGlobalMethod("initWallet", args, &value);
|
|
if (ok && !value.isNullOrUndefined()) {
|
|
result = value.toString();
|
|
WalletEvent::Emit("wallet_inited", "{1}");
|
|
}else {
|
|
result = "";
|
|
}
|
|
return const_cast<char*>(result.c_str());
|
|
}
|
|
|
|
char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) {
|
|
se::ValueArray args;
|
|
addToArgArray(&args, nonceChar);
|
|
addToArgArray(&args, tipChar);
|
|
|
|
se::Value value;
|
|
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() {
|
|
// return JcWallet::getInstance()->runJsMethod("createAccount", "");
|
|
}
|
|
|
|
char* JcWallet::importAccount(const char *privateKey){
|
|
// return JcWallet::getInstance()->runJsMethod("importAccount", privateKey);
|
|
}
|
|
|
|
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;
|
|
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(const char *passwordChar) {
|
|
JcWallet::getInstance()->initEnv();
|
|
return JcWallet::getInstance()->initWallet(passwordChar);
|
|
}
|
|
char* signLogin(const char *nonceChar, const char *tipChar) {
|
|
return JcWallet::getInstance()->signLogin(nonceChar, tipChar);
|
|
}
|
|
char* createAccount() {
|
|
return JcWallet::getInstance()->createAccount();
|
|
}
|
|
char* importAccount(const char *privateKey) {
|
|
return JcWallet::getInstance()->importAccount(privateKey);
|
|
}
|
|
char* runWalletMethod(const char *methodName, int paramCount, char **paramList) {
|
|
JcWallet::getInstance()->initEnv();
|
|
return JcWallet::getInstance()->runJsMethod(methodName, paramCount, paramList);
|
|
}
|
|
}
|
|
|
|
NS_CC_END
|