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 use std::str::FromStr;
71
72 #[test]
73 fn test_brain() {
74 let words = "this is sparta!".to_owned();
75 let first_keypair = Brain::new(words.clone()).generate().unwrap();
76 let second_keypair = Brain::new(words).generate().unwrap();
77 assert_eq!(first_keypair.secret(), second_keypair.secret());
78 }
79
80 // Brain-wallet compatibility guard for the parity-wordlist swap
81 // (paritytech/wordlist -> Conflux-Chain/conflux-parity-deps fork at
82 // rand 0.9). If the 7530-word dictionary ever drifts, or if the brain
83 // key-derivation (keccak chain -> secret) changes, this test fails and
84 // anyone's previously-generated brain wallets would become unrecoverable.
85 #[test]
86 fn brain_wallet_compat_fixed_phrase() {
87 // All twelve words are taken from the original parity-wordlist
88 // dictionary, so validate_phrase must accept the phrase.
89 let phrase = "abacus abdomen ability able abnormal absence \
90 absolute abstract accent accurate accustom acorn"
91 .to_owned();
92 Brain::validate_phrase(&phrase, 12)
93 .expect("phrase is all dictionary words");
94
95 // Derivation is deterministic (double-keccak chain until the address
96 // has the 0x10 type nibble), so a hardcoded secret pins both the
97 // hash path and the dictionary-word inputs.
98 let kp = Brain::new(phrase).generate().unwrap();
99 let expected = crate::Secret::from_str(
100 "0ae3b9521d5bc321284646b6b7ed286223d6630b5092ec795a9ca31884a81442",
101 )
102 .unwrap();
103 assert_eq!(kp.secret(), &expected);
104 }
105}