Browse Source

Refactor to lib

taddr
Aditya Kulkarni 5 years ago
parent
commit
735ba5661f
  1. 2
      cli/Cargo.toml
  2. 29
      cli/src/main.rs
  3. 3
      lib/Cargo.toml
  4. 31
      lib/src/paper.rs

2
cli/Cargo.toml

@ -7,4 +7,4 @@ edition = "2018"
[dependencies]
clap = "~2.33"
zecpaperlib = { path = "../lib" }
json = "0.11.14"
json = "0.11.14"

29
cli/src/main.rs

@ -2,8 +2,7 @@ extern crate clap;
extern crate zecpaperlib;
use clap::{Arg, App};
use zecpaperlib::paper::get_address;
use json::{array, object};
use zecpaperlib::paper::*;
fn main() {
let matches = App::new("zecpaperwaller")
@ -12,14 +11,15 @@ fn main() {
.arg(Arg::with_name("testnet")
.short("t")
.long("testnet")
.help("Generate Testnet addresses."))
.help("Generate Testnet addresses"))
.arg(Arg::with_name("format")
.short("f")
.long("format")
.help("What format to generate the output in.")
.help("What format to generate the output in")
.takes_value(true)
.value_name("FORMAT")
.possible_values(&["png", "pdf", "txt"]))
.possible_values(&["png", "pdf", "json"])
.default_value("json"))
.arg(Arg::with_name("output")
.short("o")
.long("output")
@ -37,18 +37,13 @@ fn main() {
}))
.get_matches();
let num_addresses = matches.value_of("num_addresses").unwrap().parse::<i32>().unwrap();
let mut ans = array![];
for count in 0..num_addresses {
let (addr, pk) = get_address(true);
ans.push(object!{
"num" => count,
"address" => addr,
"private_key" => pk
}).unwrap();
}
let testnet: bool = matches.is_present("testnet");
if !testnet {
eprint!("Mainnet addresses are not supported yet. Please re-run with --testnet");
return;
}
let num_addresses = matches.value_of("num_addresses").unwrap().parse::<i32>().unwrap();
println!("{}", json::stringify_pretty(ans, 2));
println!("{}", gen_addresses_as_json(testnet, num_addresses));
}

3
lib/Cargo.toml

@ -17,4 +17,5 @@ sapling-crypto = { git = "https://github.com/zcash/librustzcash" }
time = { version = "0.1", optional = true }
zcash_primitives = { git = "https://github.com/zcash/librustzcash" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash" }
zip32 = { git = "https://github.com/zcash/librustzcash" }
zip32 = { git = "https://github.com/zcash/librustzcash" }
json = "0.11.14"

31
lib/src/paper.rs

@ -2,18 +2,37 @@
use zip32::{ChildIndex, ExtendedSpendingKey};
use bech32::{Bech32, u5, ToBase32};
use rand::{OsRng, Rng};
use json::{array, object};
pub fn get_address(testnet: bool) -> (String, String) {
let addr_prefix = if testnet {"ztestsapling"} else {"zs"};
let pk_prefix = if testnet {"secret-extended-key-test"} else {"secret-extended-key-main"};
pub fn gen_addresses_as_json(testnet: bool, count: i32) -> String {
let mut rng = OsRng::new().expect("Error opening random number generator");
let mut seed:[u8; 32] = [0; 32];
rng.fill_bytes(&mut seed);
return gen_addresses_with_seed_as_json(testnet, count, &seed);
}
pub fn gen_addresses_with_seed_as_json(testnet: bool, count: i32, seed: &[u8; 32]) -> String {
let mut ans = array![];
for i in 0..count {
let (addr, pk) = get_address(testnet, &seed);
ans.push(object!{
"num" => i,
"address" => addr,
"private_key" => pk
}).unwrap();
}
return json::stringify_pretty(ans, 2);
}
fn get_address(testnet: bool, seed: &[u8; 32]) -> (String, String) {
let addr_prefix = if testnet {"ztestsapling"} else {"zs"};
let pk_prefix = if testnet {"secret-extended-key-test"} else {"secret-extended-key-main"};
let spk: ExtendedSpendingKey = ExtendedSpendingKey::from_path(
&ExtendedSpendingKey::master(&seed),
&ExtendedSpendingKey::master(seed),
&[
ChildIndex::Hardened(32),
ChildIndex::Hardened(44),

Loading…
Cancel
Save