Import from ZWL file

This commit is contained in:
Hanh 2022-07-18 10:55:57 +08:00
parent 33d34cbc9e
commit 4fabf69589
6 changed files with 44 additions and 0 deletions

View File

@ -115,3 +115,5 @@ char *merge_data(char *drop);
char *get_tx_summary(char *tx);
char *get_best_server(char **servers, uint32_t count);
void import_from_zwl(uint8_t coin, char *name, char *path);

View File

@ -144,3 +144,15 @@ pub fn delete_account(coin: u8, account: u32) -> anyhow::Result<()> {
db.delete_account(account)?;
Ok(())
}
pub fn import_from_zwl(coin: u8, name: &str, data: &str) -> anyhow::Result<()> {
let c = CoinConfig::get(coin);
let sks = crate::read_zwl(data)?;
let db = c.db()?;
for (i, key) in sks.iter().enumerate() {
let name = format!("{}-{}", name, i + 1);
let (seed, sk, ivk, pa) = decode_key(coin, key, 0)?;
db.store_account(&name, seed.as_deref(), 0, sk.as_deref(), &ivk, &pa)?;
}
Ok(())
}

View File

@ -584,3 +584,11 @@ pub async unsafe extern "C" fn get_best_server(
.unwrap_or(String::new());
to_c_str(best_server)
}
#[no_mangle]
pub unsafe extern "C" fn import_from_zwl(coin: u8, name: *mut c_char, data: *mut c_char) {
from_c_str!(name);
from_c_str!(data);
let res = crate::api::account::import_from_zwl(coin, &name, &data);
log_result(res)
}

View File

@ -30,6 +30,7 @@ mod hash;
mod key;
mod key2;
mod mempool;
mod misc;
mod pay;
mod prices;
mod print;
@ -76,6 +77,7 @@ pub use crate::key::{generate_random_enc_key, KeyHelpers};
pub use crate::lw_rpc::compact_tx_streamer_client::CompactTxStreamerClient;
pub use crate::lw_rpc::*;
pub use crate::mempool::MemPool;
pub use crate::misc::read_zwl;
pub use crate::pay::{broadcast_tx, get_tx_summary, Tx, TxIn, TxOut};
pub use crate::print::*;
pub use crate::scan::{latest_height, scan_all, sync_async};

3
src/misc.rs Normal file
View File

@ -0,0 +1,3 @@
mod zwl;
pub use zwl::read_zwl;

17
src/misc/zwl.rs Normal file
View File

@ -0,0 +1,17 @@
use serde::Deserialize;
#[derive(Deserialize)]
struct ZWLKey {
private_key: String,
}
pub fn read_zwl(data: &str) -> anyhow::Result<Vec<String>> {
let keys: Vec<ZWLKey> = serde_json::from_str(data)?;
let mut secret_keys = vec![];
for k in keys.iter() {
if k.private_key.starts_with("secret-") {
secret_keys.push(k.private_key.clone());
}
}
Ok(secret_keys)
}