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;
20
21/// Simple brainwallet.
22pub struct Brain(String);
23
24impl Brain {
25 pub fn new(s: String) -> Self { Brain(s) }
26
27 pub fn validate_phrase(
28 phrase: &str, expected_words: usize,
29 ) -> Result<(), crate::WordlistError> {
30 parity_wordlist::validate_phrase(phrase, expected_words)
31 }
32}
33
34impl KeyPairGenerator for Brain {
35 type Error = crate::Void;
36
37 fn generate(&mut self) -> Result<KeyPair, Self::Error> {
38 let seed = self.0.clone();
39 let mut secret = seed.into_bytes().keccak256();
40
41 let mut i = 0;
42 loop {
43 secret = secret.keccak256();
44
45 match i > 16384 {
46 false => i += 1,
47 true => {
48 if let Ok(pair) = Secret::from_unsafe_slice(&secret)
49 .and_then(KeyPair::from_secret)
50 {
51 if pair.address()[0] == 0x10 {
52 trace!(
53 "Testing: {}, got: {:?}",
54 self.0,
55 pair.address()
56 );
57 return Ok(pair);
58 }
59 }
60 }
61 }
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use crate::{Brain, KeyPairGenerator};
69 use std::str::FromStr;
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
79 // Brain-wallet compatibility guard for the parity-wordlist swap
80 // (paritytech/wordlist -> Conflux-Chain/conflux-parity-deps fork at
81 // rand 0.9). If the 7530-word dictionary ever drifts, or if the brain
82 // key-derivation (keccak chain -> secret) changes, this test fails and
83 // anyone's previously-generated brain wallets would become unrecoverable.
84 #[test]
85 fn brain_wallet_compat_fixed_phrase() {
86 // All twelve words are taken from the original parity-wordlist
87 // dictionary, so validate_phrase must accept the phrase.
88 let phrase = "abacus abdomen ability able abnormal absence \
89 absolute abstract accent accurate accustom acorn"
90 .to_owned();
91 Brain::validate_phrase(&phrase, 12)
92 .expect("phrase is all dictionary words");
93
94 // Derivation is deterministic (double-keccak chain until the address
95 // has the 0x10 type nibble), so a hardcoded secret pins both the
96 // hash path and the dictionary-word inputs.
97 let kp = Brain::new(phrase).generate().unwrap();
98 let expected = crate::Secret::from_str(
99 "0ae3b9521d5bc321284646b6b7ed286223d6630b5092ec795a9ca31884a81442",
100 )
101 .unwrap();
102 assert_eq!(kp.secret(), &expected);
103 }
104}