Browse Source

lib/bin structure

taddr
Aditya Kulkarni 5 years ago
parent
commit
7fe4833549
  1. 3
      cli/Cargo.toml
  2. 17
      cli/src/main.rs
  3. 1
      lib/.gitignore
  4. 3
      lib/Cargo.toml
  5. 4
      lib/src/lib.rs
  6. 47
      lib/src/paper.rs

3
cli/Cargo.toml

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

17
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));
}

1
lib/.gitignore

@ -0,0 +1 @@
target/

3
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" }

4
lib/src/lib.rs

@ -1,3 +1 @@
fn sayhello() {
println!("Hello, world!");
}
pub mod paper;

47
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<u5> = 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<u5> = 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());
}
Loading…
Cancel
Save