1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
mod img;
mod payload;

pub use img::{KeyQrCode, KeyQrCodeError};
pub use payload::{decode, generate, Error as KeyQrCodePayloadError};
use std::path::PathBuf;
use std::str::FromStr;
use thiserror::Error;

pub const PIN_LENGTH: usize = 4;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QrPin {
    pub password: [u8; 4],
}

#[derive(Error, Debug)]
pub enum Error {
    #[error(transparent)]
    BadPin(#[from] BadPinError),
    #[error(transparent)]
    KeyQrCodeHash(#[from] KeyQrCodePayloadError),
    #[error(transparent)]
    KeyQrCode(#[from] KeyQrCodeError),
}

#[derive(Error, Debug)]
pub enum BadPinError {
    #[error("The PIN must consist of {PIN_LENGTH} digits, found {0}")]
    InvalidLength(usize),
    #[error("Invalid digit {0}")]
    InvalidDigit(char),
    #[error("cannot detect file name from path {0:?} in order to read qr pin from it")]
    UnableToDetectFileName(PathBuf),
}

impl FromStr for QrPin {
    type Err = BadPinError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.chars().count() != PIN_LENGTH {
            return Err(BadPinError::InvalidLength(s.len()));
        }

        let mut pwd = [0u8; 4];
        for (i, digit) in s.chars().enumerate() {
            pwd[i] = digit.to_digit(10).ok_or(BadPinError::InvalidDigit(digit))? as u8;
        }
        Ok(QrPin { password: pwd })
    }
}

#[derive(Clone, Debug)]
pub enum PinReadMode {
    Global(String),
    FromFileName(PathBuf),
}

/// supported format is *1234.png
impl PinReadMode {
    pub fn into_qr_pin(&self) -> Result<QrPin, BadPinError> {
        match self {
            PinReadMode::Global(ref global) => QrPin::from_str(global),
            PinReadMode::FromFileName(qr) => {
                let file_name = qr
                    .file_stem()
                    .ok_or_else(|| BadPinError::UnableToDetectFileName(qr.to_path_buf()))?;
                QrPin::from_str(
                    &file_name
                        .to_str()
                        .unwrap()
                        .chars()
                        .rev()
                        .take(4)
                        .collect::<Vec<char>>()
                        .iter()
                        .rev()
                        .collect::<String>(),
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chain_crypto::SecretKey;
    use image::DynamicImage;

    #[test]
    fn parse_pin_successfully() {
        for (pin, pwd) in &[
            ("0000", [0, 0, 0, 0]),
            ("1123", [1, 1, 2, 3]),
            ("0002", [0, 0, 0, 2]),
        ] {
            let qr_pin = QrPin::from_str(pin).unwrap();
            assert_eq!(qr_pin, QrPin { password: *pwd })
        }
    }
    #[test]
    fn pins_that_do_not_satisfy_length_reqs_return_error() {
        for bad_pin in &["", "1", "11", "111", "11111"] {
            let qr_pin = QrPin::from_str(bad_pin);
            assert!(qr_pin.is_err(),)
        }
    }

    #[test]
    fn pins_that_do_not_satisfy_content_reqs_return_error() {
        for bad_pin in &["    ", " 111", "llll", "000u"] {
            let qr_pin = QrPin::from_str(bad_pin);
            assert!(qr_pin.is_err(),)
        }
    }

    // TODO: Improve into an integration test using a temporary directory.
    // Leaving here as an example.
    #[test]
    fn generate_svg() {
        const PASSWORD: &[u8] = &[1, 2, 3, 4];
        let sk = SecretKey::generate(rand::thread_rng());
        let qr = KeyQrCode::generate(sk, PASSWORD);
        qr.write_svg("qr-code.svg").unwrap();
    }

    #[test]
    fn encode_decode() {
        const PASSWORD: &[u8] = &[1, 2, 3, 4];
        let sk = SecretKey::generate(rand::thread_rng());
        let qr = KeyQrCode::generate(sk.clone(), PASSWORD);
        let img = qr.to_img();
        // img.save("qr.png").unwrap();
        assert_eq!(
            sk.leak_secret().as_ref(),
            KeyQrCode::decode(DynamicImage::ImageLuma8(img), PASSWORD).unwrap()[0]
                .clone()
                .leak_secret()
                .as_ref()
        );
    }
}