update wallet lib

This commit is contained in:
cebgcontract 2022-11-10 15:32:06 +08:00
parent 920494120c
commit ba9a266247
12 changed files with 196 additions and 151 deletions

View File

@ -115,19 +115,19 @@ public:
inline char* getKeySecond() {return key_second;}
inline char* getKeyBackup() {return key_backup;}
inline char* getKeySeed() {return key_seed;}
void setKeyMaster(char *val) {
void setKeyMaster(const char *val) {
key_master = (char*)malloc(strlen(val) + 1);
strcpy(key_master, val);
}
void setKeySecond(char *val) {
void setKeySecond(const char *val) {
key_second = (char*)malloc(strlen(val) + 1);
strcpy(key_second, val);
}
void setKeyBackup(char *val) {
void setKeyBackup(const char *val) {
key_backup = (char*)malloc(strlen(val) + 1);
strcpy(key_backup, val);
}
void setKeySeed(char *val) {
void setKeySeed(const char *val) {
key_seed = (char*)malloc(strlen(val) + 1);
strcpy(key_seed, val);
}

View File

@ -653,9 +653,9 @@ 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)
void showQRCodeJNI(const std::string& funid, const std::string& content, const std::string& title, const std::string& oid)
{
JniHelper::callStaticVoidMethod(JCSDK, "showQRCode", funid, content, title);
JniHelper::callStaticVoidMethod(JCSDK, "showQRCode", funid, content, title, oid);
}
void scanQRCodeJNI(const std::string& funid, const std::string& title)
@ -663,6 +663,11 @@ void scanQRCodeJNI(const std::string& funid, const std::string& title)
JniHelper::callStaticVoidMethod(JCSDK, "scanQRCode", funid, title);
}
void loadRestoreKeyJNI(const std::string& funid, const std::string& oid)
{
JniHelper::callStaticVoidMethod(JCSDK, "loadRestoreKey", funid, oid);
}
void signWithGoogleJNI(const std::string& funid)
{
JniHelper::callStaticVoidMethod(JCSDK, "signWithGoogle", funid);

View File

@ -35,8 +35,9 @@ 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 showQRCodeJNI(const std::string& funid, const std::string& content, const std::string& title, const std::string& id);
void scanQRCodeJNI(const std::string& funid, const std::string& title);
void loadRestoreKeyJNI(const std::string& funid, const std::string& oid);
void signWithGoogleJNI(const std::string& funid);
void signOutGoogleJNI(const std::string& funid);
void toWalletJNI(const std::string& url);

View File

@ -809,7 +809,7 @@ 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) {
if (argc > 4) {
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -819,11 +819,14 @@ static bool JSB_showQRCode(se::State& s)
std::string title;
ok = seval_to_std_string(args[2], &title);
SE_PRECONDITION2(ok, false, "title is invalid!");
showQRCodeJNI(funid, text, title);
std::string id;
ok = seval_to_std_string(args[3], &id);
SE_PRECONDITION2(ok, false, "data id is invalid!");
showQRCodeJNI(funid, text, title, id);
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2);
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 4);
return false;
}
SE_BIND_FUNC(JSB_showQRCode)
@ -849,6 +852,27 @@ static bool JSB_scanQRCode(se::State& s)
}
SE_BIND_FUNC(JSB_scanQRCode)
static bool JSB_restoreKey(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 oid;
ok = seval_to_std_string(args[1], &oid);
SE_PRECONDITION2(ok, false, "oid is invalid!");
loadRestoreKeyJNI(funid, oid);
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2);
return false;
}
SE_BIND_FUNC(JSB_restoreKey)
static bool JSB_signWithGoogle(se::State& s)
{
const auto& args = s.args();
@ -909,7 +933,9 @@ static bool JSB_generateWallet(se::State& s)
{
const auto& args = s.args();
size_t argc = args.size();
CCLOG("JSB_generateWallet\n");
CC_UNUSED bool ok = true;
if (argc > 0) {
std::string id;
ok = seval_to_std_string(args[0], &id);
@ -917,15 +943,29 @@ static bool JSB_generateWallet(se::State& s)
std::string seed;
ok = seval_to_std_string(args[1], &seed);
SE_PRECONDITION2(ok, false, "Error processing seed");
CCLOG("JSB_generateWallet 1 %s\n", seed.c_str());
CWallet wallet = new_wallet(seed.c_str());
std::string address = get_address(&wallet);
std::string storageKey =StringUtils::format("cebg_wallet_%s", id.c_str());
localStorageSetItem(storageKey, wallet.second_key);
CCLOG("JSB_generateWallet 2 %s\n", wallet.master_key);
std::string address = get_address(
seed.c_str(),
wallet.master_key,
wallet.second_key,
""
);
CCLOG("JSB_generateWallet 3 %s\n", address.c_str());
std::string localSKey = StringUtils::format("cebg_wallet_s_%s", id.c_str());
localStorageSetItem(localSKey, wallet.second_key);
CCLOG("JSB_generateWallet 4 \n");
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");
CCLOG("JSB_generateWallet 5 \n");
size_t msgLen = strlen(wallet.backup_key);
char* msg_cpy = (char*)malloc(msgLen + 1);
strcpy(msg_cpy, wallet.backup_key);
showQRCodeJNI("", msg_cpy, "save this qrcode plz", id);
std::string result = StringUtils::format("{\"master\": \"%s\",\"backup\": \"%s\", \"address\": \"%s\"}\n",
wallet.master_key,
wallet.backup_key,
@ -956,33 +996,24 @@ static bool JSB_prepareWallet(se::State& s)
std::string keyMaster;
ok = seval_to_std_string(args[2], &keyMaster);
SE_PRECONDITION2(ok, false, "Error processing master key");
std::string storageKey = StringUtils::format("cebg_wallet_%s", id.c_str());
std::string localKey;
localStorageGetItem(storageKey, &localKey);
std::string storageSKey = StringUtils::format("cebg_wallet_s_%s", id.c_str());
std::string storageBKey = StringUtils::format("cebg_wallet_b_%s", id.c_str());
std::string localSKey;
localStorageGetItem(storageSKey, &localSKey);
std::string localBKey;
localStorageGetItem(storageBKey, &localBKey);
Application::getInstance()->setKeyMaster(const_cast<char *>(keyMaster.c_str()));
Application::getInstance()->setKeySecond(const_cast<char *>(localKey.c_str()));
Application::getInstance()->setKeySeed(const_cast<char *>(seed.c_str()));
Application::getInstance()->setKeyMaster(keyMaster.c_str());
Application::getInstance()->setKeySecond(localSKey.c_str());
Application::getInstance()->setKeyBackup(localBKey.c_str());
Application::getInstance()->setKeySeed(seed.c_str());
size_t masterLen = strlen(Application::getInstance()->getKeyMaster());
char* master_cpy = (char*)malloc(masterLen + 1);
strcpy(master_cpy, Application::getInstance()->getKeyMaster());
size_t msgLen = strlen(Application::getInstance()->getKeySeed());
char* msg_cpy = (char*)malloc(msgLen + 1);
strcpy(msg_cpy, Application::getInstance()->getKeySeed());
size_t secondLen = strlen(Application::getInstance()->getKeySecond());
char* second_cpy = (char*)malloc(secondLen + 1);
strcpy(second_cpy, Application::getInstance()->getKeySecond());
CWallet wallet = CWallet{
.msg_key = msg_cpy,
.master_key = master_cpy,
.second_key = second_cpy,
.backup_key = ""
};
std::string address = get_address(&wallet);
std::string address = get_address(
Application::getInstance()->getKeySeed(),
Application::getInstance()->getKeyMaster(),
Application::getInstance()->getKeySecond(),
Application::getInstance()->getKeyBackup()
);
std::string result = StringUtils::format("{\"address\": \"%s\"}\n",
address.c_str()
);
@ -1011,41 +1042,24 @@ static bool JSB_restoreWallet(se::State& s)
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());
Application::getInstance()->setKeySeed(seed.c_str());
Application::getInstance()->setKeyMaster(keyMaster.c_str());
Application::getInstance()->setKeySecond("");
Application::getInstance()->setKeyBackup(keyBackup.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,
std::string address = get_address(
seed.c_str(),
keyMaster.c_str(),
"",
keyBackup.c_str()
);
std::string storageKey =StringUtils::format("cebg_wallet_b_%s", id.c_str());
localStorageSetItem(storageKey, Application::getInstance()->getKeyBackup());
std::string result = StringUtils::format("{\"address\": \"%s\"}\n",
address.c_str()
);
s.rval().setString(result);
@ -1068,24 +1082,12 @@ static bool JSB_walletSign(se::State& s)
std::string msg;
ok = seval_to_std_string(args[0], &msg);
SE_PRECONDITION2(ok, false, "Error processing seed");
size_t masterLen = strlen(Application::getInstance()->getKeyMaster());
char* master_cpy = (char*)malloc(masterLen + 1);
strcpy(master_cpy, Application::getInstance()->getKeyMaster());
size_t msgLen = strlen(Application::getInstance()->getKeySeed());
char* msg_cpy = (char*)malloc(msgLen + 1);
strcpy(msg_cpy, Application::getInstance()->getKeySeed());
size_t secondLen = strlen(Application::getInstance()->getKeySecond());
char* second_cpy = (char*)malloc(secondLen + 1);
strcpy(second_cpy, Application::getInstance()->getKeySecond());
CWallet wallet = CWallet{
.msg_key = msg_cpy,
.master_key = master_cpy,
.second_key = second_cpy,
.backup_key = ""
};
std::string sign_str = sign(&wallet, msg.c_str());
std::string sign_str = sign(
Application::getInstance()->getKeySeed(),
Application::getInstance()->getKeyMaster(),
Application::getInstance()->getKeySecond(),
Application::getInstance()->getKeyBackup(),
msg.c_str());
s.rval().setString(sign_str);
return true;
}
@ -1104,24 +1106,12 @@ static bool JSB_walletSecKey(se::State& s)
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
size_t masterLen = strlen(Application::getInstance()->getKeyMaster());
char* master_cpy = (char*)malloc(masterLen + 1);
strcpy(master_cpy, Application::getInstance()->getKeyMaster());
size_t msgLen = strlen(Application::getInstance()->getKeySeed());
char* msg_cpy = (char*)malloc(msgLen + 1);
strcpy(msg_cpy, Application::getInstance()->getKeySeed());
size_t secondLen = strlen(Application::getInstance()->getKeySecond());
char* second_cpy = (char*)malloc(secondLen + 1);
strcpy(second_cpy, Application::getInstance()->getKeySecond());
CWallet wallet = CWallet{
.msg_key = msg_cpy,
.master_key = master_cpy,
.second_key = second_cpy,
.backup_key = ""
};
std::string key_str = generate_sec_key(&wallet);
std::string key_str = generate_sec_key(
Application::getInstance()->getKeySeed(),
Application::getInstance()->getKeyMaster(),
Application::getInstance()->getKeySecond(),
Application::getInstance()->getKeyBackup()
);
s.rval().setString(key_str);
return true;
}
@ -1140,24 +1130,12 @@ static bool JSB_walletSignTran(se::State& s)
std::string msg;
ok = seval_to_std_string(args[0], &msg);
SE_PRECONDITION2(ok, false, "Error processing seed");
size_t masterLen = strlen(Application::getInstance()->getKeyMaster());
char* master_cpy = (char*)malloc(masterLen + 1);
strcpy(master_cpy, Application::getInstance()->getKeyMaster());
size_t msgLen = strlen(Application::getInstance()->getKeySeed());
char* msg_cpy = (char*)malloc(msgLen + 1);
strcpy(msg_cpy, Application::getInstance()->getKeySeed());
size_t secondLen = strlen(Application::getInstance()->getKeySecond());
char* second_cpy = (char*)malloc(secondLen + 1);
strcpy(second_cpy, Application::getInstance()->getKeySecond());
CWallet wallet = CWallet{
.msg_key = msg_cpy,
.master_key = master_cpy,
.second_key = second_cpy,
.backup_key = ""
};
std::string sign_str = sign_for_tran(&wallet, msg.c_str());
std::string sign_str = sign_for_tran(
Application::getInstance()->getKeySeed(),
Application::getInstance()->getKeyMaster(),
Application::getInstance()->getKeySecond(),
Application::getInstance()->getKeyBackup(),
msg.c_str());
s.rval().setString(sign_str);
// free_cwallet(wallet);
return true;
@ -1191,6 +1169,7 @@ 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("restoreKey", _SE(JSB_restoreKey));
__jsbObj->defineFunction("showQRCode", _SE(JSB_showQRCode));
__jsbObj->defineFunction("signWithGoogle", _SE(JSB_signWithGoogle));
__jsbObj->defineFunction("signOutGoogle", _SE(JSB_signOutGoogle));

View File

@ -12,16 +12,31 @@ typedef struct CWallet {
struct CWallet new_wallet(const char *msg);
struct CWallet restore_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
void free_cwallet(struct CWallet cw);
char *get_address(const struct CWallet *cw);
char *get_address(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *generate_sec_key(const struct CWallet *cw);
char *generate_sec_key(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *sign(const struct CWallet *cw, const char *msg);
char *sign(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);
char *sign_for_tran(const struct CWallet *cw, const char *msg);
char *sign_for_tran(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);

Binary file not shown.

View File

@ -12,16 +12,31 @@ typedef struct CWallet {
struct CWallet new_wallet(const char *msg);
struct CWallet restore_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
void free_cwallet(struct CWallet cw);
char *get_address(const struct CWallet *cw);
char *get_address(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *generate_sec_key(const struct CWallet *cw);
char *generate_sec_key(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *sign(const struct CWallet *cw, const char *msg);
char *sign(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);
char *sign_for_tran(const struct CWallet *cw, const char *msg);
char *sign_for_tran(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);

Binary file not shown.

View File

@ -12,16 +12,31 @@ typedef struct CWallet {
struct CWallet new_wallet(const char *msg);
struct CWallet restore_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
void free_cwallet(struct CWallet cw);
char *get_address(const struct CWallet *cw);
char *get_address(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *generate_sec_key(const struct CWallet *cw);
char *generate_sec_key(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *sign(const struct CWallet *cw, const char *msg);
char *sign(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);
char *sign_for_tran(const struct CWallet *cw, const char *msg);
char *sign_for_tran(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);

Binary file not shown.

View File

@ -12,16 +12,31 @@ typedef struct CWallet {
struct CWallet new_wallet(const char *msg);
struct CWallet restore_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const struct CWallet *cw);
struct CWallet reset_wallet(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
void free_cwallet(struct CWallet cw);
char *get_address(const struct CWallet *cw);
char *get_address(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *generate_sec_key(const struct CWallet *cw);
char *generate_sec_key(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key);
char *sign(const struct CWallet *cw, const char *msg);
char *sign(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);
char *sign_for_tran(const struct CWallet *cw, const char *msg);
char *sign_for_tran(const char *msg_key,
const char *master_key,
const char *second_key,
const char *backup_key,
const char *msg);

Binary file not shown.