Skip to content

Commit f540007

Browse files
authored
Merge pull request #108 from input-output-hk/move_stats_and_qr
Move stats and qr mods from iapyx to catalyst-toolbox
2 parents e1c6b42 + 9bb77d5 commit f540007

File tree

35 files changed

+1345
-70
lines changed

35 files changed

+1345
-70
lines changed

Cargo.lock

Lines changed: 111 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

catalyst-toolbox/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ symmetric-cipher = { git = "https://github.com/input-output-hk/chain-wallet-libs
5959
graphql_client = "0.10"
6060
gag = "1"
6161
vit-servicing-station-lib = { git = "https://github.com/input-output-hk/vit-servicing-station.git", branch = "master" }
62+
stopwatch = "0.0.7"
6263

6364
[dev-dependencies]
6465
rand_chacha = "0.3"

catalyst-toolbox/src/bin/cli/advisor_reviews/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Export {
8383
let reviews = read_vca_reviews_aggregated_file(&from)?;
8484
match format {
8585
OutputFormat::Csv => {
86-
utils::csv::dump_data_to_csv(&reviews, &to)?;
86+
utils::csv::dump_data_to_csv(reviews.iter(), &to)?;
8787
}
8888
OutputFormat::Json => {
8989
serde_json::to_writer(open_file_write(&Some(to))?, &reviews)?;

catalyst-toolbox/src/bin/cli/kedqr/decode/img.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ use catalyst_toolbox::kedqr::KeyQrCode;
33
use catalyst_toolbox::kedqr::QrPin;
44
use chain_crypto::AsymmetricKey;
55
use chain_crypto::Ed25519Extended;
6+
use chain_crypto::SecretKey;
67
use std::error::Error;
78
use std::fs::File;
89
use std::io::Write;
10+
use std::path::Path;
911
use std::path::PathBuf;
1012

11-
pub fn secret_from_qr(
13+
pub fn save_secret_from_qr(
1214
qr: PathBuf,
1315
output: Option<PathBuf>,
1416
pin: QrPin,
1517
) -> Result<(), Box<dyn Error>> {
16-
let img = image::open(qr)?;
17-
let secret = KeyQrCode::decode(img, &pin.password)?;
18-
let sk = secret.first().unwrap().clone();
18+
let sk = secret_from_qr(&qr, pin)?;
1919
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
2020
let secret_key = bech32::encode(hrp, sk.leak_secret().to_base32(), Variant::Bech32)?;
2121

@@ -32,3 +32,12 @@ pub fn secret_from_qr(
3232
};
3333
Ok(())
3434
}
35+
36+
pub fn secret_from_qr(
37+
qr: impl AsRef<Path>,
38+
pin: QrPin,
39+
) -> Result<SecretKey<Ed25519Extended>, catalyst_toolbox::kedqr::KeyQrCodeError> {
40+
let img = image::open(qr)?;
41+
let secret = KeyQrCode::decode(img, &pin.password)?;
42+
Ok(secret.first().unwrap().clone())
43+
}

catalyst-toolbox/src/bin/cli/kedqr/decode/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
mod hash;
21
mod img;
2+
mod payload;
33

4-
use crate::cli::kedqr::encode::QrCodeOpts;
4+
use crate::cli::kedqr::QrCodeOpts;
55
use catalyst_toolbox::kedqr::QrPin;
6-
pub use hash::decode_hash;
7-
pub use img::secret_from_qr;
6+
pub use img::{save_secret_from_qr, secret_from_qr};
7+
pub use payload::{decode_payload, secret_from_payload};
88
use std::error::Error;
99
use std::path::PathBuf;
1010
use structopt::StructOpt;
@@ -13,7 +13,7 @@ use structopt::StructOpt;
1313
#[derive(Debug, PartialEq, StructOpt)]
1414
#[structopt(rename_all = "kebab-case")]
1515
pub struct DecodeQrCodeCmd {
16-
/// Path to file containing img or hash.
16+
/// Path to file containing img or payload.
1717
#[structopt(short, long, parse(from_os_str))]
1818
input: PathBuf,
1919
/// Path to file to save secret output, if not provided console output will be attempted.
@@ -30,8 +30,8 @@ pub struct DecodeQrCodeCmd {
3030
impl DecodeQrCodeCmd {
3131
pub fn exec(self) -> Result<(), Box<dyn Error>> {
3232
match self.opts {
33-
QrCodeOpts::Hash => decode_hash(self.input, self.output, self.pin),
34-
QrCodeOpts::Img => secret_from_qr(self.input, self.output, self.pin),
33+
QrCodeOpts::Payload => decode_payload(self.input, self.output, self.pin),
34+
QrCodeOpts::Img => save_secret_from_qr(self.input, self.output, self.pin),
3535
}
3636
}
3737
}
Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
use bech32::{ToBase32, Variant};
2-
use catalyst_toolbox::kedqr::decode;
3-
use catalyst_toolbox::kedqr::QrPin;
4-
use chain_crypto::AsymmetricKey;
5-
use chain_crypto::Ed25519Extended;
6-
use std::fs::File;
7-
use std::io::BufRead;
8-
use std::io::Write;
9-
use std::{error::Error, fs::OpenOptions, io::BufReader, path::PathBuf};
2+
use catalyst_toolbox::kedqr::{decode, QrPin};
3+
use chain_crypto::{AsymmetricKey, Ed25519Extended, SecretKey};
4+
use std::{
5+
error::Error,
6+
fs::{File, OpenOptions},
7+
io::{BufRead, BufReader, Write},
8+
path::{Path, PathBuf},
9+
};
1010

11-
pub fn decode_hash(
11+
pub fn decode_payload(
1212
input: PathBuf,
1313
output: Option<PathBuf>,
1414
pin: QrPin,
1515
) -> Result<(), Box<dyn Error>> {
16-
// open input key and parse it
17-
let input = OpenOptions::new()
18-
.create(false)
19-
.read(true)
20-
.write(false)
21-
.append(false)
22-
.open(&input)
23-
.expect("Could not open input file.");
24-
25-
let mut reader = BufReader::new(input);
26-
let mut hash_str = String::new();
27-
let _len = reader
28-
.read_line(&mut hash_str)
29-
.expect("Could not read input file.");
30-
hash_str = hash_str.trim_end().to_string();
31-
32-
// use parsed pin from args
33-
let pwd = pin.password;
3416
// generate qrcode with key and parsed pin
35-
let secret = decode(hash_str, &pwd)?;
17+
let secret = secret_from_payload(input, pin)?;
3618
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
3719
let secret_key = bech32::encode(hrp, secret.leak_secret().to_base32(), Variant::Bech32)?;
3820
// process output
@@ -49,3 +31,28 @@ pub fn decode_hash(
4931
}
5032
Ok(())
5133
}
34+
35+
pub fn secret_from_payload(
36+
input: impl AsRef<Path>,
37+
pin: QrPin,
38+
) -> Result<SecretKey<Ed25519Extended>, catalyst_toolbox::kedqr::KeyQrCodePayloadError> {
39+
let input = OpenOptions::new()
40+
.create(false)
41+
.read(true)
42+
.write(false)
43+
.append(false)
44+
.open(&input)
45+
.expect("Could not open input file.");
46+
47+
let mut reader = BufReader::new(input);
48+
let mut payload_str = String::new();
49+
let _len = reader
50+
.read_line(&mut payload_str)
51+
.expect("Could not read input file.");
52+
payload_str = payload_str.trim_end().to_string();
53+
54+
// use parsed pin from args
55+
let pwd = pin.password;
56+
// generate qrcode with key and parsed pin
57+
decode(payload_str, &pwd)
58+
}

0 commit comments

Comments
 (0)