Skip to content

Commit

Permalink
Merge pull request #2 from cake-tech/fix-legacy-seed-for-non-english
Browse files Browse the repository at this point in the history
Fix issues with converting to legacy seed
  • Loading branch information
OmarHatem28 authored Apr 15, 2024
2 parents df1c963 + 1b1977d commit 3b5f877
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/src/mnemonics/legacy/legacy_seed_lang.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -121,10 +123,12 @@ class LegacySeedLang {
int _getChecksumIndex(List<String> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 33 additions & 6 deletions test/polyseed_test.dart
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -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");
});
});
});
}
Expand Down

0 comments on commit 3b5f877

Please sign in to comment.