diff --git a/CHANGELOG.md b/CHANGELOG.md index 43b0ea6..e695fd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.4 + +- Fix issues with convert to legacy seed for other languages other than english + ## 0.0.3 - Add Wownero support diff --git a/lib/src/mnemonics/legacy/legacy_seed_lang.dart b/lib/src/mnemonics/legacy/legacy_seed_lang.dart index da0e8d3..9e9acc1 100644 --- a/lib/src/mnemonics/legacy/legacy_seed_lang.dart +++ b/lib/src/mnemonics/legacy/legacy_seed_lang.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:hashlib/hashlib.dart'; import 'package:polyseed/src/mnemonics/legacy/de_lang.dart'; import 'package:polyseed/src/mnemonics/legacy/en_lang.dart'; @@ -121,10 +123,12 @@ class LegacySeedLang { int _getChecksumIndex(List words, int prefixLen) { var trimmedWords = ''; for (var i = 0; i < words.length; i++) { - trimmedWords += words[i].substring(0, prefixLen); + final actualPrefixLength = + words[i].length > prefixLen ? prefixLen : words[i].length; + trimmedWords += words[i].substring(0, actualPrefixLength); } - var checksum = trimmedWords.crc32code(); + var checksum = trimmedWords.crc32code(Encoding.getByName('utf-8')); var index = (checksum % words.length); return index; } diff --git a/pubspec.yaml b/pubspec.yaml index 3851a1a..4f3c17a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: polyseed description: A pure dart implementation of the 16-word seed scheme for monero -version: 0.0.3 +version: 0.0.4 homepage: https://cakelabs.com repository: https://github.com/cake-tech/polyseed_dart issue_tracker: https://github.com/cake-tech/polyseed_dart/issues diff --git a/test/polyseed_test.dart b/test/polyseed_test.dart index 1ffdacb..23b094d 100644 --- a/test/polyseed_test.dart +++ b/test/polyseed_test.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:polyseed/polyseed.dart'; +import 'package:polyseed/src/mnemonics/es_lang.dart'; import 'package:test/test.dart'; void main() { @@ -16,6 +17,8 @@ void main() { final expectedBirthday = 1693622412; final expectedLegacySeed = "avidly chlorine gave yeti ramped certain hybrid comb governing amply hinder pamphlet offend geometry narrate unopened robot epoxy annoyed glide ingested ascend were austere unopened"; + final expectedLegacySeedEs = + "apoyo bestia desnudo revés meter beso este bonsái duelo algodón engaño llaga lámina diamante imperio pobre mortal cochino altar diva fábrica ángulo recurso aplicar algodón"; test('Decode and test for correct birthday', () { final seed = Polyseed.decode(expectedSeedString, enLang, coin); @@ -71,12 +74,36 @@ void main() { expect(Polyseed.load(serializedSeed).birthday, seed.birthday); }); - test('Generate a 25 Word LegacySeed from a Seed', () { - final seed = Polyseed.decode(expectedSeedString, enLang, coin); - final keyBytes = seed.generateKey(coin, 32); - final legacySeed = LegacySeedLang.getByName("English") - .encodePhrase(keyBytes.toHexString()); - expect(legacySeed, expectedLegacySeed); + group('Convert to Legacy Seed', () { + test('Generate a 25 Word english LegacySeed from a Seed', () { + final seed = Polyseed.decode(expectedSeedString, enLang, coin); + final keyBytes = seed.generateKey(coin, 32); + final legacySeed = LegacySeedLang.getByName("English") + .encodePhrase(keyBytes.toHexString()); + expect(legacySeed, expectedLegacySeed); + }); + + test('Generate a 25 Word spanish LegacySeed from a Seed', () { + final seed = Polyseed.decode(expectedSeedString, enLang, coin); + final keyBytes = seed.generateKey(coin, 32); + final legacySeed = LegacySeedLang.getByEnglishName("Spanish") + .encodePhrase(keyBytes.toHexString()); + expect(legacySeed, expectedLegacySeedEs); + }); + + test( + 'EdgeCase: Generate a 25 Word Spanish LegacySeed from a Seed with words smaller than the prefix length of the word list', + () { + final seed = Polyseed.decode( + "remedio foca sujeto veneno bello humilde surco crear típico chacal célula empate moreno varón verde masa", + esLang, + coin); + final keyBytes = seed.generateKey(coin, 32); + final legacySeed = LegacySeedLang.getByEnglishName("Spanish") + .encodePhrase(keyBytes.toHexString()); + expect(legacySeed, + "remedio haz ébano lobo orden celda pezuña regreso ardilla estar acelga fallo punto nación hada quitar ancla obeso piedra pausa helio fuente joroba pista quitar"); + }); }); }); }