2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2017-12-01 09:40:07 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2017-12-01 09:40:07 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2017-12-01 09:40:07 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-12-01 09:40:07 +01:00
|
|
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
use edit_distance::edit_distance;
|
2021-03-12 10:12:42 +01:00
|
|
|
use parity_crypto::publickey::Address;
|
2017-12-01 09:40:07 +01:00
|
|
|
use parity_wordlist;
|
|
|
|
|
2021-03-12 10:12:42 +01:00
|
|
|
use super::Brain;
|
2017-12-01 09:40:07 +01:00
|
|
|
|
|
|
|
/// Tries to find a phrase for address, given the number
|
|
|
|
/// of expected words and a partial phrase.
|
|
|
|
///
|
|
|
|
/// Returns `None` if phrase couldn't be found.
|
|
|
|
pub fn brain_recover(
|
2020-08-05 06:08:03 +02:00
|
|
|
address: &Address,
|
|
|
|
known_phrase: &str,
|
|
|
|
expected_words: usize,
|
2017-12-01 09:40:07 +01:00
|
|
|
) -> Option<String> {
|
2020-08-05 06:08:03 +02:00
|
|
|
let it = PhrasesIterator::from_known_phrase(known_phrase, expected_words);
|
|
|
|
for phrase in it {
|
2021-03-12 10:12:42 +01:00
|
|
|
let keypair = Brain::new(phrase.clone()).generate();
|
2020-08-05 06:08:03 +02:00
|
|
|
trace!("Testing: {}, got: {:?}", phrase, keypair.address());
|
|
|
|
if &keypair.address() == address {
|
|
|
|
return Some(phrase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2017-12-01 09:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_substitutions(word: &str) -> Vec<&'static str> {
|
2020-08-05 06:08:03 +02:00
|
|
|
let mut words = parity_wordlist::WORDS
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.map(|w| (edit_distance(w, word), w))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
words.sort_by(|a, b| a.0.cmp(&b.0));
|
|
|
|
|
|
|
|
words.into_iter().map(|pair| pair.1).collect()
|
2017-12-01 09:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator over possible
|
|
|
|
pub struct PhrasesIterator {
|
2020-08-05 06:08:03 +02:00
|
|
|
words: Vec<Vec<&'static str>>,
|
|
|
|
combinations: u64,
|
|
|
|
indexes: Vec<usize>,
|
|
|
|
has_next: bool,
|
2017-12-01 09:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PhrasesIterator {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn from_known_phrase(known_phrase: &str, expected_words: usize) -> Self {
|
|
|
|
let known_words = parity_wordlist::WORDS
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
let mut words = known_phrase
|
|
|
|
.split(' ')
|
|
|
|
.map(|word| match known_words.get(word) {
|
|
|
|
None => {
|
|
|
|
info!(
|
|
|
|
"Invalid word '{}', looking for potential substitutions.",
|
|
|
|
word
|
|
|
|
);
|
|
|
|
let substitutions = generate_substitutions(word);
|
|
|
|
info!("Closest words: {:?}", &substitutions[..10]);
|
|
|
|
substitutions
|
|
|
|
}
|
|
|
|
Some(word) => vec![*word],
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// add missing words
|
|
|
|
if words.len() < expected_words {
|
|
|
|
let to_add = expected_words - words.len();
|
|
|
|
info!("Number of words is insuficcient adding {} more.", to_add);
|
|
|
|
for _ in 0..to_add {
|
|
|
|
words.push(parity_wordlist::WORDS.iter().cloned().collect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start searching
|
|
|
|
PhrasesIterator::new(words)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(words: Vec<Vec<&'static str>>) -> Self {
|
|
|
|
let combinations = words.iter().fold(1u64, |acc, x| acc * x.len() as u64);
|
|
|
|
let indexes = words.iter().map(|_| 0).collect();
|
|
|
|
info!("Starting to test {} possible combinations.", combinations);
|
|
|
|
|
|
|
|
PhrasesIterator {
|
|
|
|
words,
|
|
|
|
combinations,
|
|
|
|
indexes,
|
|
|
|
has_next: combinations > 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn combinations(&self) -> u64 {
|
|
|
|
self.combinations
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current(&self) -> String {
|
|
|
|
let mut s = self.words[0][self.indexes[0]].to_owned();
|
|
|
|
for i in 1..self.indexes.len() {
|
|
|
|
s.push(' ');
|
|
|
|
s.push_str(self.words[i][self.indexes[i]]);
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_index(&mut self) -> bool {
|
|
|
|
let mut pos = self.indexes.len();
|
|
|
|
while pos > 0 {
|
|
|
|
pos -= 1;
|
|
|
|
self.indexes[pos] += 1;
|
|
|
|
if self.indexes[pos] >= self.words[pos].len() {
|
|
|
|
self.indexes[pos] = 0;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2017-12-01 09:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for PhrasesIterator {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Item = String;
|
2017-12-01 09:40:07 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn next(&mut self) -> Option<String> {
|
|
|
|
if !self.has_next {
|
|
|
|
return None;
|
|
|
|
}
|
2017-12-01 09:40:07 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
let phrase = self.current();
|
|
|
|
self.has_next = self.next_index();
|
|
|
|
Some(phrase)
|
|
|
|
}
|
2017-12-01 09:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-05 06:08:03 +02:00
|
|
|
use super::PhrasesIterator;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_generate_possible_combinations() {
|
|
|
|
let mut it =
|
|
|
|
PhrasesIterator::new(vec![vec!["1", "2", "3"], vec!["test"], vec!["a", "b", "c"]]);
|
|
|
|
|
|
|
|
assert_eq!(it.combinations(), 9);
|
|
|
|
assert_eq!(it.next(), Some("1 test a".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("1 test b".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("1 test c".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("2 test a".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("2 test b".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("2 test c".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("3 test a".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("3 test b".to_owned()));
|
|
|
|
assert_eq!(it.next(), Some("3 test c".to_owned()));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
2017-12-01 09:40:07 +01:00
|
|
|
}
|