99 lines
3.0 KiB
C++
99 lines
3.0 KiB
C++
#include "JcWallet.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 "platform/CCApplication.h"
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
#import "AppDelegate.h"
|
|
#import "WalletEvent.hpp"
|
|
#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;
|
|
static std::string result;
|
|
bool _isStarted = false;
|
|
|
|
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() {
|
|
se::Value value;
|
|
jsb_run_code("jc.wallet.currentAccount().address", &value);
|
|
result = value.toString();
|
|
WalletEvent::Emit("wallet_inited", "{}");
|
|
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::Value value;
|
|
jsb_run_code(jsCode, &value);
|
|
result = value.toString();
|
|
return const_cast<char*>(result.c_str());
|
|
}
|
|
|
|
char* JcWallet::createAccount() {
|
|
se::Value value;
|
|
jsb_run_code("jc.wallet.createAccount()", &value);
|
|
result = value.toString();
|
|
return const_cast<char*>(result.c_str());
|
|
}
|
|
|
|
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);
|
|
result = value.toString();
|
|
return const_cast<char*>(result.c_str());
|
|
}
|
|
|
|
|
|
extern "C"
|
|
{
|
|
char* initWallet() {
|
|
JcWallet::getInstance()->initEnv();
|
|
return JcWallet::getInstance()->initWallet();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
NS_CC_END
|