cfxkey/
brain.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17use super::{KeyPair, KeyPairGenerator, Secret};
18use cfx_crypto::crypto::keccak::Keccak256;
19use log::trace;
20use parity_wordlist;
21
22/// Simple brainwallet.
23pub struct Brain(String);
24
25impl Brain {
26    pub fn new(s: String) -> Self { Brain(s) }
27
28    pub fn validate_phrase(
29        phrase: &str, expected_words: usize,
30    ) -> Result<(), crate::WordlistError> {
31        parity_wordlist::validate_phrase(phrase, expected_words)
32    }
33}
34
35impl KeyPairGenerator for Brain {
36    type Error = crate::Void;
37
38    fn generate(&mut self) -> Result<KeyPair, Self::Error> {
39        let seed = self.0.clone();
40        let mut secret = seed.into_bytes().keccak256();
41
42        let mut i = 0;
43        loop {
44            secret = secret.keccak256();
45
46            match i > 16384 {
47                false => i += 1,
48                true => {
49                    if let Ok(pair) = Secret::from_unsafe_slice(&secret)
50                        .and_then(KeyPair::from_secret)
51                    {
52                        if pair.address()[0] == 0x10 {
53                            trace!(
54                                "Testing: {}, got: {:?}",
55                                self.0,
56                                pair.address()
57                            );
58                            return Ok(pair);
59                        }
60                    }
61                }
62            }
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::{Brain, KeyPairGenerator};
70
71    #[test]
72    fn test_brain() {
73        let words = "this is sparta!".to_owned();
74        let first_keypair = Brain::new(words.clone()).generate().unwrap();
75        let second_keypair = Brain::new(words).generate().unwrap();
76        assert_eq!(first_keypair.secret(), second_keypair.secret());
77    }
78}