diff --git a/lib/Cargo.toml b/lib/Cargo.toml index fa78c8b..e028166 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -11,4 +11,4 @@ crate-type = ["staticlib"] [dependencies] libc = "0.2.58" lazy_static = "1.4.0" -zecwalletlitelib = { path = "../../lightwallet/lightwalletclient/lib/" } \ No newline at end of file +zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "6dc22e4d74e9cec458d279d793477dea3bfe27e5" } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 1bf53ba..d9cbf5a 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -72,6 +72,53 @@ pub extern fn litelib_initialize_new(dangerous: bool, server: *const c_char) -> return s_str.into_raw(); } +/// Restore a wallet from the seed phrase +#[no_mangle] +pub extern fn litelib_initialize_new_from_phrase(dangerous: bool, server: *const c_char, + seed: *const c_char, birthday: u64) -> *mut c_char { + let server_str = unsafe { + assert!(!server.is_null()); + + CStr::from_ptr(server).to_string_lossy().into_owned() + }; + + let seed_str = unsafe { + assert!(!seed.is_null()); + + CStr::from_ptr(seed).to_string_lossy().into_owned() + }; + + let server = LightClientConfig::get_server_or_default(Some(server_str)); + let (config, _latest_block_height) = match LightClientConfig::create(server, dangerous) { + Ok((c, h)) => (c, h), + Err(e) => { + let e_str = CString::new(format!("Error: {}", e)).unwrap(); + return e_str.into_raw(); + } + }; + + let lightclient = match LightClient::new_from_phrase(seed_str, &config, birthday) { + Ok(l) => l, + Err(e) => { + let e_str = CString::new(format!("Error: {}", e)).unwrap(); + return e_str.into_raw(); + } + }; + + let seed = match lightclient.do_seed_phrase() { + Ok(s) => s.dump(), + Err(e) => { + let e_str = CString::new(format!("Error: {}", e)).unwrap(); + return e_str.into_raw(); + } + }; + + LIGHTCLIENT.lock().unwrap().replace(Some(lightclient)); + + let c_str = CString::new("OK").unwrap(); + return c_str.into_raw(); +} + // Initialize a new lightclient and store its value #[no_mangle] pub extern fn litelib_initialize_existing(dangerous: bool, server: *const c_char) -> *mut c_char { diff --git a/lib/zecwalletlitelib.h b/lib/zecwalletlitelib.h index c5bb4ed..b985fa7 100644 --- a/lib/zecwalletlitelib.h +++ b/lib/zecwalletlitelib.h @@ -6,7 +6,10 @@ extern "C" { #endif extern bool litelib_wallet_exists (const char* chain_name); -extern char * litelib_initialize_new (bool dangerous, const char* server); +extern char * litelib_initialize_new (bool dangerous, const char* server); +extern char * litelib_initialize_new_from_phrase + (bool dangerous, const char* server, const char* seed, + unsigned long long birthday); extern char * litelib_initialize_existing (bool dangerous, const char* server); extern char * litelib_execute (const char* s, const char* args); extern void litelib_rust_free_string (char* s); diff --git a/src/firsttimewizard.cpp b/src/firsttimewizard.cpp index 3ca417b..4d2c8ad 100644 --- a/src/firsttimewizard.cpp +++ b/src/firsttimewizard.cpp @@ -67,7 +67,7 @@ NewOrRestorePage::NewOrRestorePage(FirstTimeWizard *parent) : QWizardPage(parent NewSeedPage::NewSeedPage(FirstTimeWizard *parent) : QWizardPage(parent) { this->parent = parent; - + setTitle("Your new wallet"); QWidget* pageWidget = new QWidget(); @@ -102,7 +102,7 @@ bool NewSeedPage::validatePage() { auto parsed = json::parse(reply.toStdString().c_str(), nullptr, false); if (parsed.is_discarded() || parsed.is_null() || parsed.find("result") == parsed.end()) { QMessageBox::warning(this, tr("Failed to save wallet"), - tr("Couldn't save the wallet. Error") + "\n" + reply, + tr("Couldn't save the wallet") + "\n" + reply, QMessageBox::Ok); return false; } else { @@ -112,6 +112,8 @@ bool NewSeedPage::validatePage() { RestoreSeedPage::RestoreSeedPage(FirstTimeWizard *parent) : QWizardPage(parent) { + this->parent = parent; + setTitle("Restore wallet from seed"); QWidget* pageWidget = new QWidget(); @@ -125,11 +127,25 @@ RestoreSeedPage::RestoreSeedPage(FirstTimeWizard *parent) : QWizardPage(parent) bool RestoreSeedPage::validatePage() { // 1. Validate that we do have 24 words - QString seed = form.txtSeed->toPlainText(); - if (seed.trimmed().split(QRegExp("[ \n\r]+")).length() != 24) { + QString seed = form.txtSeed->toPlainText().replace(QRegExp("[ \n\r]+"), " "); + if (seed.trimmed().split(" ").length() != 24) { QMessageBox::warning(this, tr("Failed to restore wallet"), tr("Zecwallet needs 24 words to restore wallet"), QMessageBox::Ok); return false; } + + // 2. Attempt to restore wallet with the seed phrase + char* resp = litelib_initialize_new_from_phrase(parent->dangerous, parent->server.toStdString().c_str(), + seed.toStdString().c_str(), 0); + QString reply = litelib_process_response(resp); + + if (reply.toUpper().trimmed() != "OK") { + QMessageBox::warning(this, tr("Failed to restore wallet"), + tr("Couldn't restore the wallet") + "\n" + reply, + QMessageBox::Ok); + return false; + } else { + return true; + } } \ No newline at end of file diff --git a/src/firsttimewizard.h b/src/firsttimewizard.h index 8f894d7..276c0cd 100644 --- a/src/firsttimewizard.h +++ b/src/firsttimewizard.h @@ -52,9 +52,12 @@ private: class RestoreSeedPage: public QWizardPage { public: RestoreSeedPage(FirstTimeWizard* parent); + protected: bool validatePage(); + private: + FirstTimeWizard* parent; Ui_RestoreSeedForm form; };