finaly add random sietch addr generation
This commit is contained in:
@@ -1 +0,0 @@
|
||||
|
||||
@@ -722,6 +722,36 @@ impl Command for NewAddressCommand {
|
||||
}
|
||||
}
|
||||
|
||||
struct NewSietchAddressCommand {}
|
||||
impl Command for NewSietchAddressCommand {
|
||||
fn help(&self) -> String {
|
||||
let mut h = vec![];
|
||||
h.push("New Sietch Address");
|
||||
h.push("Usage:");
|
||||
h.push("sietch [zs]");
|
||||
h.push("");
|
||||
h.push("Example:");
|
||||
h.push("To create a new zdust:");
|
||||
h.push("sietch zs");
|
||||
h.join("\n")
|
||||
}
|
||||
|
||||
fn short_help(&self) -> String {
|
||||
"Create a sietch Address".to_string()
|
||||
}
|
||||
|
||||
fn exec(&self, args: &[&str], lightclient: &LightClient) -> String {
|
||||
if args.len() != 1 {
|
||||
return format!("No address type specified\n{}", self.help());
|
||||
}
|
||||
|
||||
match lightclient.do_new_sietchaddress(args[0]) {
|
||||
Ok(j) => j,
|
||||
Err(e) => object!{ "error" => e }
|
||||
}.pretty(2)
|
||||
}
|
||||
}
|
||||
|
||||
struct NotesCommand {}
|
||||
impl Command for NotesCommand {
|
||||
fn help(&self) -> String {
|
||||
@@ -832,6 +862,7 @@ pub fn get_commands() -> Box<HashMap<String, Box<dyn Command>>> {
|
||||
map.insert("list".to_string(), Box::new(TransactionsCommand{}));
|
||||
map.insert("notes".to_string(), Box::new(NotesCommand{}));
|
||||
map.insert("new".to_string(), Box::new(NewAddressCommand{}));
|
||||
map.insert("sietch".to_string(), Box::new(NewSietchAddressCommand{}));
|
||||
map.insert("seed".to_string(), Box::new(SeedCommand{}));
|
||||
map.insert("encrypt".to_string(), Box::new(EncryptCommand{}));
|
||||
map.insert("decrypt".to_string(), Box::new(DecryptCommand{}));
|
||||
|
||||
@@ -868,6 +868,31 @@ impl LightClient {
|
||||
Ok(array![new_address])
|
||||
}
|
||||
|
||||
pub fn do_new_sietchaddress(&self, addr_type: &str) -> Result<JsonValue, String> {
|
||||
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
|
||||
error!("Wallet is locked");
|
||||
return Err("Wallet is locked".to_string());
|
||||
}
|
||||
|
||||
let new_address = {
|
||||
let wallet = self.wallet.write().unwrap();
|
||||
|
||||
match addr_type {
|
||||
"zs" => wallet.add_zaddrdust(),
|
||||
|
||||
_ => {
|
||||
let e = format!("Unrecognized address type: {}", addr_type);
|
||||
error!("{}", e);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.do_save()?;
|
||||
|
||||
Ok(array!["sietch",new_address])
|
||||
}
|
||||
|
||||
pub fn clear_state(&self) {
|
||||
// First, clear the state from the wallet
|
||||
self.wallet.read().unwrap().clear_blocks();
|
||||
|
||||
@@ -460,6 +460,39 @@ impl LightWallet {
|
||||
|
||||
zaddr
|
||||
}
|
||||
// Add a new Sietch Addr. This will derive a new zdust address from the seed
|
||||
pub fn add_zaddrdust(&self) -> String {
|
||||
if !self.unlocked {
|
||||
return "".to_string();
|
||||
}
|
||||
|
||||
let pos = self.extsks.read().unwrap().len() as u32;
|
||||
|
||||
// Use random generator to create a new Sietch seed every time when call.
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let letter: String = rng.gen_range(b'A', b'Z').to_string();
|
||||
let number: String = rng.gen_range(0, 999999).to_string();
|
||||
// let combi: String = letter.to_string() + number.to_string();
|
||||
let s = format!("{}{:06}", letter, number);
|
||||
//println!("{}", s);
|
||||
|
||||
let my_string = String::from(s);
|
||||
// let my_immutable_string = &my_string; //This is a &String type
|
||||
let dust: &str = &my_string; //This is an &str type
|
||||
|
||||
|
||||
|
||||
let bip39_seed = bip39::Seed::new(&Mnemonic::from_entropy(&self.seed, Language::English).unwrap(), dust);
|
||||
|
||||
let (_extsk, _extfvk, address) =
|
||||
LightWallet::get_zaddr_from_bip39seed(&self.config, &bip39_seed.as_bytes(), pos);
|
||||
|
||||
let zaddr = encode_payment_address(self.config.hrp_sapling_address(), &address);
|
||||
|
||||
|
||||
zaddr
|
||||
}
|
||||
|
||||
/// Add a new t address to the wallet. This will derive a new address from the seed
|
||||
/// at the next position.
|
||||
|
||||
Reference in New Issue
Block a user