diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 1d73d59..1b179aa 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -6,4 +6,5 @@ edition = "2018" [dependencies] clap = "~2.33" -zecpaperlib = { path = "../lib" } \ No newline at end of file +zecpaperlib = { path = "../lib" } +json = "0.11.14" \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index 1a09ee5..fd56ca7 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,5 +1,9 @@ extern crate clap; -use clap::{Arg, App, SubCommand}; +extern crate zecpaperlib; + +use clap::{Arg, App}; +use zecpaperlib::paper::get_address; +use json::object; fn main() { App::new("zecpaperwaller") @@ -20,7 +24,14 @@ fn main() { .short("o") .long("output") .index(1) - .required(true) .help("Name of output file.")) - .get_matches(); + .get_matches(); + + let (addr, pk) = get_address(); + let ans = object!{ + "address" => addr, + "private_key" => pk + }; + + println!("{}", json::stringify_pretty(ans, 2)); } \ No newline at end of file diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/lib/Cargo.toml b/lib/Cargo.toml index eb86bc8..92a48a9 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -4,9 +4,6 @@ version = "0.1.0" authors = ["ZecWallet"] edition = "2018" -[lib] -crate-type = ["cdylib"] - [dependencies] bech32 = "0.6" bellman = { git = "https://github.com/zcash/librustzcash" } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 41afeaf..617b234 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,3 +1 @@ -fn sayhello() { - println!("Hello, world!"); -} +pub mod paper; diff --git a/lib/src/paper.rs b/lib/src/paper.rs new file mode 100644 index 0000000..34adb15 --- /dev/null +++ b/lib/src/paper.rs @@ -0,0 +1,47 @@ + +use zip32::{ChildIndex, ExtendedSpendingKey}; +use bech32::{Bech32, u5, ToBase32}; +use rand::{OsRng, Rng}; + +pub fn get_address() -> (String, String) { + + let mut rng = OsRng::new().expect("Error opening random number generator"); + let mut seed:[u8; 32] = [0; 32]; + rng.fill_bytes(&mut seed); + + let spk: ExtendedSpendingKey = ExtendedSpendingKey::from_path( + &ExtendedSpendingKey::master(&seed), + &[ + ChildIndex::Hardened(32), + ChildIndex::Hardened(44), + ChildIndex::Hardened(0), + ], + ); + + let (_d, addr) = spk.default_address().expect("Cannot get result"); + //println!("{:?}", d.0); + //println!("{:?}", addr.diversifier.0); + + // Address + let mut v = vec![0; 43]; + v.get_mut(..11).unwrap().copy_from_slice(&addr.diversifier.0); + addr.pk_d.write(v.get_mut(11..).unwrap()).expect("Cannot write!"); + + + let checked_data: Vec = v.to_base32(); + let encoded = Bech32::new("zs".into(), checked_data).expect("bech32 failed").to_string(); + //println!("{}", encoded); + + // Private Key + let mut vp = Vec::new(); + spk.write(&mut vp).expect("Can't write private key"); + //println!("Len {:?}", vp.len()); + + let c_d: Vec = vp.to_base32(); + let encoded_pk = Bech32::new("secret-extended-key-main".into(), c_d).expect("bech32 failed").to_string(); + //println!("{}", encoded_pk); + + + //println!("Hello, {}, {}!", encoded, encoded_pk); + return (encoded.to_string(), encoded_pk.to_string()); +} \ No newline at end of file