|
| 1 | +use super::QrCodeOpts; |
| 2 | +use crate::cli::kedqr::decode::{secret_from_payload, secret_from_qr}; |
| 3 | +use catalyst_toolbox::kedqr::QrPin; |
| 4 | +use chain_addr::{AddressReadable, Discrimination, Kind}; |
| 5 | +use chain_core::{ |
| 6 | + mempack::{ReadBuf, Readable}, |
| 7 | + property::Deserialize, |
| 8 | +}; |
| 9 | +use chain_crypto::{Ed25519Extended, SecretKey}; |
| 10 | +use chain_impl_mockchain::block::Block; |
| 11 | +use jormungandr_lib::interfaces::{Block0Configuration, Initial}; |
| 12 | +use std::io::BufReader; |
| 13 | +use std::path::{Path, PathBuf}; |
| 14 | +use structopt::StructOpt; |
| 15 | +use url::Url; |
| 16 | +#[derive(StructOpt, Debug)] |
| 17 | +pub struct InfoForQrCodeCmd { |
| 18 | + /// Path to file containing img or payload. |
| 19 | + #[structopt(short, long, parse(from_os_str))] |
| 20 | + input: PathBuf, |
| 21 | + |
| 22 | + /// Pin code. 4-digit number is used on Catalyst. |
| 23 | + #[structopt(short, long, parse(try_from_str))] |
| 24 | + pin: QrPin, |
| 25 | + |
| 26 | + /// Blockchain block0. Can be either url of local file path |
| 27 | + #[structopt(long = "block0")] |
| 28 | + pub block0: Option<String>, |
| 29 | + |
| 30 | + /// Set the discrimination type to testing (default is production). |
| 31 | + #[structopt(short, long)] |
| 32 | + pub testing: bool, |
| 33 | + |
| 34 | + #[structopt(flatten)] |
| 35 | + opts: QrCodeOpts, |
| 36 | +} |
| 37 | + |
| 38 | +impl InfoForQrCodeCmd { |
| 39 | + pub fn exec(self) -> Result<(), Box<dyn std::error::Error>> { |
| 40 | + let secret_key: SecretKey<Ed25519Extended> = { |
| 41 | + match self.opts { |
| 42 | + QrCodeOpts::Payload => secret_from_payload(&self.input, self.pin)?, |
| 43 | + QrCodeOpts::Img => secret_from_qr(&self.input, self.pin)?, |
| 44 | + } |
| 45 | + }; |
| 46 | + let kind = Kind::Account(secret_key.to_public()); |
| 47 | + let address = chain_addr::Address(test_discrimination(self.testing), kind); |
| 48 | + |
| 49 | + if let Some(block0_path) = &self.block0 { |
| 50 | + let block = { |
| 51 | + if Path::new(block0_path).exists() { |
| 52 | + let reader = std::fs::OpenOptions::new() |
| 53 | + .create(false) |
| 54 | + .write(false) |
| 55 | + .read(true) |
| 56 | + .append(false) |
| 57 | + .open(block0_path)?; |
| 58 | + let reader = BufReader::new(reader); |
| 59 | + Block::deserialize(reader)? |
| 60 | + } else if Url::parse(block0_path).is_ok() { |
| 61 | + let response = reqwest::blocking::get(block0_path)?; |
| 62 | + let block0_bytes = response.bytes()?.to_vec(); |
| 63 | + Block::read(&mut ReadBuf::from(&block0_bytes))? |
| 64 | + } else { |
| 65 | + panic!("invalid block0: should be either path to filesystem or url "); |
| 66 | + } |
| 67 | + }; |
| 68 | + let genesis = Block0Configuration::from_block(&block)?; |
| 69 | + |
| 70 | + for initial in genesis.initial.iter() { |
| 71 | + if let Initial::Fund(initial_utxos) = initial { |
| 72 | + if let Some(entry) = initial_utxos |
| 73 | + .iter() |
| 74 | + .find(|x| x.address == address.clone().into()) |
| 75 | + { |
| 76 | + println!( |
| 77 | + "Address corresponding to input qr found in block0: '{}' with value: '{}'", |
| 78 | + AddressReadable::from_address(&test_prefix(self.testing),&address), entry.value); |
| 79 | + return Ok(()); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + eprintln!("Address corresponding to input qr not found in block0"); |
| 84 | + } else { |
| 85 | + println!( |
| 86 | + "Address: {}", |
| 87 | + AddressReadable::from_address(&test_prefix(self.testing), &address) |
| 88 | + ); |
| 89 | + } |
| 90 | + Ok(()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +pub fn test_discrimination(testing: bool) -> Discrimination { |
| 95 | + match testing { |
| 96 | + false => Discrimination::Production, |
| 97 | + true => Discrimination::Test, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +pub fn test_prefix(testing: bool) -> String { |
| 102 | + match test_discrimination(testing) { |
| 103 | + Discrimination::Production => "ca".to_string(), |
| 104 | + Discrimination::Test => "ta".to_string(), |
| 105 | + } |
| 106 | +} |
0 commit comments