diff --git a/cocos/platform/android/jni/JniImp.cpp b/cocos/platform/android/jni/JniImp.cpp index f4c8b16..8b0aab2 100644 --- a/cocos/platform/android/jni/JniImp.cpp +++ b/cocos/platform/android/jni/JniImp.cpp @@ -653,6 +653,11 @@ void copyTextToClipboardJNI(const std::string& text) JniHelper::callStaticVoidMethod(JCLS_HELPER, "copyTextToClipboard", text); } +void showQRCodeJNI(const std::string& funid, const std::string& content, const std::string& title) +{ + JniHelper::callStaticVoidMethod(JCSDK, "showQRCode", funid, content, title); +} + void scanQRCodeJNI(const std::string& funid, const std::string& title) { JniHelper::callStaticVoidMethod(JCSDK, "scanQRCode", funid, title); diff --git a/cocos/platform/android/jni/JniImp.h b/cocos/platform/android/jni/JniImp.h index 45259d2..63f794b 100644 --- a/cocos/platform/android/jni/JniImp.h +++ b/cocos/platform/android/jni/JniImp.h @@ -35,6 +35,7 @@ extern void exitApplication(); extern std::string getApkPathJNI(); extern std::string getPackageNameJNI(); +void showQRCodeJNI(const std::string& funid, const std::string& content, const std::string& title); void scanQRCodeJNI(const std::string& funid, const std::string& title); void signWithGoogleJNI(const std::string& funid); void signOutGoogleJNI(const std::string& funid); diff --git a/cocos/scripting/js-bindings/manual/jsb_global.cpp b/cocos/scripting/js-bindings/manual/jsb_global.cpp index 3b0e750..896cfb6 100644 --- a/cocos/scripting/js-bindings/manual/jsb_global.cpp +++ b/cocos/scripting/js-bindings/manual/jsb_global.cpp @@ -804,6 +804,29 @@ static bool JSB_cryptoScrypt(se::State& s) SE_BIND_FUNC(JSB_cryptoScrypt) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +static bool JSB_showQRCode(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc > 1) { + std::string funid; + ok = seval_to_std_string(args[0], &funid); + SE_PRECONDITION2(ok, false, "funid is invalid!"); + std::string text; + ok = seval_to_std_string(args[1], &text); + SE_PRECONDITION2(ok, false, "content is invalid!"); + std::string title; + ok = seval_to_std_string(args[2], &title); + SE_PRECONDITION2(ok, false, "title is invalid!"); + showQRCodeJNI(funid, text, title); + return true; + } + + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2); + return false; +} +SE_BIND_FUNC(JSB_showQRCode) static bool JSB_scanQRCode(se::State& s) { const auto& args = s.args(); @@ -816,7 +839,8 @@ static bool JSB_scanQRCode(se::State& s) std::string text; ok = seval_to_std_string(args[1], &text); SE_PRECONDITION2(ok, false, "title is invalid!"); - Application::getInstance()->scanQRCode(funid, text); +// Application::getInstance()->scanQRCode(funid, text); + scanQRCodeJNI(funid, text); return true; } @@ -834,7 +858,8 @@ static bool JSB_signWithGoogle(se::State& s) std::string funid; ok = seval_to_std_string(args[0], &funid); SE_PRECONDITION2(ok, false, "funid is invalid!"); - Application::getInstance()->signWithGoogle(funid); +// Application::getInstance()->signWithGoogle(funid); + signWithGoogleJNI(funid); return true; } SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); @@ -851,7 +876,8 @@ static bool JSB_signOutGoogle(se::State& s) std::string funid; ok = seval_to_std_string(args[0], &funid); SE_PRECONDITION2(ok, false, "funid is invalid!"); - Application::getInstance()->signOutGoogle(funid); +// Application::getInstance()->signOutGoogle(funid); + signOutGoogleJNI(funid); return true; } SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); @@ -869,7 +895,8 @@ static bool JSB_toWallet(se::State& s) std::string text; ok = seval_to_std_string(args[0], &text); SE_PRECONDITION2(ok, false, "url is invalid!"); - Application::getInstance()->toWallet(text); +// Application::getInstance()->toWallet(text); + toWalletJNI(text); return true; } @@ -898,6 +925,7 @@ static bool JSB_generateWallet(se::State& s) Application::getInstance()->setKeyMaster(wallet.master_key); Application::getInstance()->setKeySecond(wallet.second_key); Application::getInstance()->setKeyBackup(wallet.backup_key); + showQRCodeJNI("", wallet.backup_key, "save this qrcode plz"); std::string result = StringUtils::format("{\"master\": \"%s\",\"backup\": \"%s\", \"address\": \"%s\"}\n", wallet.master_key, wallet.backup_key, @@ -968,6 +996,69 @@ static bool JSB_prepareWallet(se::State& s) } SE_BIND_FUNC(JSB_prepareWallet) +static bool JSB_restoreWallet(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc > 0) { + std::string id; + ok = seval_to_std_string(args[0], &id); + SE_PRECONDITION2(ok, false, "Error processing oid"); + std::string seed; + ok = seval_to_std_string(args[1], &seed); + SE_PRECONDITION2(ok, false, "Error processing seed"); + std::string keyMaster; + ok = seval_to_std_string(args[2], &keyMaster); + SE_PRECONDITION2(ok, false, "Error processing master key"); + + std::string keyBackup; + ok = seval_to_std_string(args[3], &keyBackup); + SE_PRECONDITION2(ok, false, "Error processing backup key"); + + size_t masterLen = strlen(keyMaster.c_str()); + char* master_cpy = (char*)malloc(masterLen + 1); + strcpy(master_cpy, keyMaster.c_str()); + + size_t msgLen = strlen(seed.c_str()); + char* msg_cpy = (char*)malloc(msgLen + 1); + strcpy(msg_cpy, seed.c_str()); + + size_t backLen = strlen(keyBackup.c_str()); + char* back_cpy = (char*)malloc(backLen + 1); + strcpy(back_cpy, keyBackup.c_str()); + + CWallet wallet_tmp = CWallet{ + .msg_key = msg_cpy, + .master_key = master_cpy, + .second_key = "", + .backup_key = back_cpy + }; + CWallet wallet = reset_wallet(&wallet_tmp); + std::string address = get_address(&wallet); + std::string storageKey =StringUtils::format("cebg_wallet_%s", id.c_str()); + localStorageSetItem(storageKey, wallet.second_key); + Application::getInstance()->setKeySeed(wallet.msg_key); + Application::getInstance()->setKeyMaster(wallet.master_key); + Application::getInstance()->setKeySecond(wallet.second_key); + Application::getInstance()->setKeyBackup(wallet.backup_key); + showQRCodeJNI("", wallet.backup_key, "save this qrcode plz"); + std::string result = StringUtils::format("{\"master\": \"%s\",\"backup\": \"%s\", \"address\": \"%s\"}\n", + wallet.master_key, + wallet.backup_key, + address.c_str() + ); + s.rval().setString(result); +// free_cwallet(wallet); + return true; + } + + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 3); + return false; +} + +SE_BIND_FUNC(JSB_restoreWallet) + static bool JSB_walletSign(se::State& s) { const auto& args = s.args(); @@ -1100,10 +1191,12 @@ bool jsb_register_global_variables(se::Object* global) __jsbObj->defineFunction("copyTextToClipboard", _SE(JSB_copyTextToClipboard)); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) __jsbObj->defineFunction("scanQRCode", _SE(JSB_scanQRCode)); + __jsbObj->defineFunction("showQRCode", _SE(JSB_showQRCode)); __jsbObj->defineFunction("signWithGoogle", _SE(JSB_signWithGoogle)); __jsbObj->defineFunction("signOutGoogle", _SE(JSB_signOutGoogle)); __jsbObj->defineFunction("generateWallet", _SE(JSB_generateWallet)); __jsbObj->defineFunction("prepareWallet", _SE(JSB_prepareWallet)); + __jsbObj->defineFunction("restoreWallet", _SE(JSB_restoreWallet)); __jsbObj->defineFunction("walletSign", _SE(JSB_walletSign)); __jsbObj->defineFunction("walletSignTran", _SE(JSB_walletSignTran)); __jsbObj->defineFunction("walletSecKey", _SE(JSB_walletSecKey)); diff --git a/external/ios/include/rustwallet/rustwallet.h b/external/ios/include/rustwallet/rustwallet.h new file mode 100644 index 0000000..593e636 --- /dev/null +++ b/external/ios/include/rustwallet/rustwallet.h @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +typedef struct CWallet { + char *msg_key; + char *master_key; + char *second_key; + char *backup_key; +} CWallet; + +struct CWallet new_wallet(const char *msg); + +struct CWallet restore_wallet(const struct CWallet *cw); + +struct CWallet reset_wallet(const struct CWallet *cw); + +void free_cwallet(struct CWallet cw); + +char *get_address(const struct CWallet *cw); + +char *generate_sec_key(const struct CWallet *cw); + +char *sign(const struct CWallet *cw, const char *msg); + +char *sign_for_tran(const struct CWallet *cw, const char *msg); diff --git a/external/ios/libs/librustwallet.a b/external/ios/libs/librustwallet.a new file mode 100644 index 0000000..d36a02b Binary files /dev/null and b/external/ios/libs/librustwallet.a differ