Skip to content

Commit

Permalink
If seed checksum fails, keep trying more languages
Browse files Browse the repository at this point in the history
Fixes #478
  • Loading branch information
j-berman committed Dec 16, 2023
1 parent 74a68c6 commit 470747f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
22 changes: 5 additions & 17 deletions coins/monero/src/tests/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ fn test_classic_seed() {
},
Vector {
language: classic::Language::Spanish,
seed: "minero ocupar mirar evadir octubre cal logro miope \
opaco disco ancla litio clase cuello nasal clase \
fiar avance deseo mente grumo negro cordón croqueta clase"
seed: "pluma laico atraer pintor peor cerca balde buscar \
lancha batir nulo reloj resto gemelo nevera poder columna gol \
oveja latir amplio bolero feliz fuerza nevera"
.into(),
spend: "ae2c9bebdddac067d73ec0180147fc92bdf9ac7337f1bcafbbe57dd13558eb02".into(),
view: "18deafb34d55b7a43cae2c1c1c206a3c80c12cc9d1f84640b484b95b7fec3e05".into(),
spend: "30303983fc8d215dd020cc6b8223793318d55c466a86e4390954f373fdc7200a".into(),
view: "97c649143f3c147ba59aa5506cc09c7992c5c219bb26964442142bf97980800e".into(),
},
Vector {
language: classic::Language::German,
Expand Down Expand Up @@ -152,12 +152,6 @@ fn test_classic_seed() {
{
let seed = Seed::from_string(Zeroizing::new(vector.seed.clone())).unwrap();
let trim = trim_seed(&vector.seed);
println!(
"{}. seed: {}, entropy: {:?}, trim: {trim}",
line!(),
*seed.to_string(),
*seed.entropy()
);
assert_eq!(seed, Seed::from_string(Zeroizing::new(trim)).unwrap());

let spend: [u8; 32] = hex::decode(vector.spend).unwrap().try_into().unwrap();
Expand Down Expand Up @@ -185,12 +179,6 @@ fn test_classic_seed() {
{
let seed = Seed::new(&mut OsRng, SeedType::Classic(vector.language));
let trim = trim_seed(&seed.to_string());
println!(
"{}. seed: {}, entropy: {:?}, trim: {trim}",
line!(),
*seed.to_string(),
*seed.entropy()
);
assert_eq!(seed, Seed::from_string(Zeroizing::new(trim)).unwrap());
assert_eq!(
seed,
Expand Down
10 changes: 9 additions & 1 deletion coins/monero/src/wallet/seed/classic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub(crate) fn seed_to_bytes(words: &str) -> Result<(Language, Zeroizing<[u8; 32]
}

// find the language
let mut invalid_checksum = false;
let (matched_indices, lang_name, lang) = (|| {
let has_checksum = words.len() == CLASSIC_SEED_LENGTH_WITH_CHECKSUM;
let mut matched_indices = Zeroizing::new(vec![]);
Expand Down Expand Up @@ -232,13 +233,20 @@ pub(crate) fn seed_to_bytes(words: &str) -> Result<(Language, Zeroizing<[u8; 32]
// check the trimmed checksum and trimmed last word line up
if trim(&checksum, lang.unique_prefix_length) != trim(&last_word, lang.unique_prefix_length)
{
Err(SeedError::InvalidChecksum)?;
// Valid checksum can appear invalid because multiple languages have the same prefixes,
// so keep trying all languages
invalid_checksum = true;
continue 'language;
}
}

return Ok((matched_indices, lang_name, lang));
}

if invalid_checksum {
Err(SeedError::InvalidChecksum)?
}

Err(SeedError::UnknownLanguage)?
})()?;

Expand Down

0 comments on commit 470747f

Please sign in to comment.