Browse Source

PDF support for taddrs

taddr
Aditya Kulkarni 5 years ago
parent
commit
2d5d4c7a7c
  1. 1
      lib/Cargo.toml
  2. 9
      lib/src/paper.rs
  3. 35
      lib/src/pdf.rs

1
lib/Cargo.toml

@ -12,7 +12,6 @@ zip32 = { git = "https://github.com/zcash/librustzcash", rev="3b6f5e3d5ede6469f
json = "0.11.14"
qrcode = { version = "0.8", default-features = false }
printpdf = "0.2.8"
blake2-rfc = { git = "https://github.com/gtank/blake2-rfc", rev="7a5b5fc99ae483a0043db7547fb79a6fa44b88a9" }
secp256k1 = { version = "0.13.0", features = ["rand", "serde"] }
ripemd160 = "0.8.0"
sha2 = "0.8.0"

9
lib/src/paper.rs

@ -7,7 +7,6 @@ use zip32::{ChildIndex, ExtendedSpendingKey};
use bech32::{Bech32, u5, ToBase32};
use rand::{Rng, ChaChaRng, FromEntropy, SeedableRng};
use json::{array, object};
use blake2_rfc::blake2b::Blake2b;
use sha2;
@ -53,12 +52,12 @@ pub fn generate_wallet(testnet: bool, nohd: bool, zcount: u32, tcount: u32, user
}
// Add in user entropy to the system entropy, and produce a 32 byte hash...
let mut state = Blake2b::new(32);
state.update(&system_entropy);
state.update(&user_entropy);
let mut state = sha2::Sha256::new();
state.input(&system_entropy);
state.input(&user_entropy);
let mut final_entropy: [u8; 32] = [0; 32];
final_entropy.clone_from_slice(&state.finalize().as_bytes()[0..32]);
final_entropy.clone_from_slice(&double_sha256(&state.result()[..]));
// ...which will we use to seed the RNG
let mut rng = ChaChaRng::from_seed(final_entropy);

35
lib/src/pdf.rs

@ -39,9 +39,18 @@ pub fn save_to_pdf(addresses: &str, filename: &str) {
current_layer = doc.get_page(page2).add_layer("Layer 3");
}
let address = kv["address"].as_str().unwrap();
let pk = kv["private_key"].as_str().unwrap();
let (seed, hdpath, is_taddr) = if kv["type"].as_str().unwrap() == "zaddr" {
(kv["seed"]["HDSeed"].as_str().unwrap(), kv["seed"]["path"].as_str().unwrap(), false)
} else {
("", "", true)
};
// Add address + private key
add_address_to_page(&current_layer, &font, &font_bold, kv["address"].as_str().unwrap(), pos);
add_pk_to_page(&current_layer, &font, &font_bold, kv["private_key"].as_str().unwrap(), kv["seed"]["HDSeed"].as_str().unwrap(), kv["seed"]["path"].as_str().unwrap(), pos);
add_address_to_page(&current_layer, &font, &font_bold, address, is_taddr, pos);
add_pk_to_page(&current_layer, &font, &font_bold, pk, is_taddr, seed, hdpath, pos);
// Is the shape stroked? Is the shape closed? Is the shape filled?
let line1 = Line {
@ -112,14 +121,15 @@ fn add_footer_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef,
/**
* Add the address section to the PDF at `pos`. Note that each page can fit only 2 wallets, so pos has to effectively be either 0 or 1.
*/
fn add_address_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef, font_bold: &IndirectFontRef, address: &str, pos: u32) {
let (scaledimg, finalsize) = qrcode_scaled(address, 10);
fn add_address_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef, font_bold: &IndirectFontRef, address: &str, is_taddr: bool, pos: u32) {
let (scaledimg, finalsize) = qrcode_scaled(address, if is_taddr {15} else {10});
// page_height top_margin vertical_padding position
// page_height top_margin vertical_padding position
let ypos = 297.0 - 5.0 - 50.0 - (140.0 * pos as f64);
add_qrcode_image_to_page(current_layer, scaledimg, finalsize, Mm(10.0), Mm(ypos));
current_layer.use_text("ZEC Address (Sapling)", 14, Mm(55.0), Mm(ypos+27.5), &font_bold);
let title = if is_taddr {"T Address"} else {"ZEC Address (Sapling)"};
current_layer.use_text(title, 14, Mm(55.0), Mm(ypos+27.5), &font_bold);
let strs = split_to_max(&address, 39, 39); // No spaces, so user can copy the address
for i in 0..strs.len() {
@ -130,11 +140,11 @@ fn add_address_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef
/**
* Add the private key section to the PDF at `pos`, which can effectively be only 0 or 1.
*/
fn add_pk_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef, font_bold: &IndirectFontRef, pk: &str, seed: &str, path: &str, pos: u32) {
let (scaledimg, finalsize) = qrcode_scaled(pk, 10);
fn add_pk_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef, font_bold: &IndirectFontRef, pk: &str, is_taddr: bool, seed: &str, path: &str, pos: u32) {
let (scaledimg, finalsize) = qrcode_scaled(pk, if is_taddr {20} else {10});
// page_height top_margin vertical_padding position
let ypos = 297.0 - 5.0 - 100.0 - (140.0 * pos as f64);
// page_height top_margin vertical_padding position
let ypos = 297.0 - 5.0 - 100.0 - (140.0 * pos as f64);
add_qrcode_image_to_page(current_layer, scaledimg, finalsize, Mm(145.0), Mm(ypos-17.5));
current_layer.use_text("Private Key", 14, Mm(10.0), Mm(ypos+32.5), &font_bold);
@ -144,8 +154,9 @@ fn add_pk_to_page(current_layer: &PdfLayerReference, font: &IndirectFontRef, fon
}
// And add the seed too.
current_layer.use_text(format!("HDSeed: {}, Path: {}", seed, path).as_str(), 8, Mm(10.0), Mm(ypos-25.0), &font);
if !is_taddr {
current_layer.use_text(format!("HDSeed: {}, Path: {}", seed, path).as_str(), 8, Mm(10.0), Mm(ypos-25.0), &font);
}
}
/**

Loading…
Cancel
Save