Skip to content

Commit f19b661

Browse files
authored
Merge pull request #6 from cake-tech/improve-jp
feat: Improve compatibility for japanese strings
2 parents cc3092e + 493124b commit f19b661

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 0.0.5
1+
## 0.0.7
2+
3+
- Improve compatibility with japanese strings using U+0020 separators
4+
5+
## 0.0.6
26

37
- Make getters less strict
48
- Support up-to-date dependencies

lib/src/mnemonics/polyseed_lang.dart

+9-3
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ class PolyseedLang {
6060

6161
/// Get the [PolyseedLang] by it's english name eg. "Chinese (Simplified)"
6262
static PolyseedLang getByEnglishName(String englishName) =>
63-
languages.firstWhere((e) => e.nameEnglish.toLowerCase() == englishName.toLowerCase());
63+
languages.firstWhere(
64+
(e) => e.nameEnglish.toLowerCase() == englishName.toLowerCase());
6465

6566
/// Get the [PolyseedLang] using the words of [phrase]
6667
static PolyseedLang getByPhrase(String phrase) {
6768
for (var language in languages) {
6869
phrase = language.hasAccents ? unorm.nfkd(phrase) : phrase;
69-
final phraseWords = phrase.split(language.separator);
70+
final phraseWords =
71+
language.normalizeSeparator(phrase).split(language.separator);
7072
if (language.words.containsAll(phraseWords)) {
7173
return language;
7274
}
@@ -85,12 +87,16 @@ class PolyseedLang {
8587
}
8688

8789
/// Decode a valid seed [phrase] into it's coefficients
88-
List<int> decodePhrase(String phrase) => phrase
90+
List<int> decodePhrase(String phrase) => normalizeSeparator(phrase)
8991
.split(separator)
9092
.map((e) => words.indexOf(hasAccents ? unorm.nfkd(e) : e))
9193
.toList();
9294

9395
/// Encode a seed [coefficients] into a valid seed phrase
9496
String encodePhrase(List<int> coefficients) =>
9597
coefficients.map((e) => words[e]).join(separator);
98+
99+
/// Replace space with the expected [separator] to improve compatibility
100+
String normalizeSeparator(String phrase) =>
101+
separator != '\u0020' ? phrase.replaceAll("\u0020", separator) : phrase;
96102
}

lib/src/polyseed.dart

+11-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ class Polyseed {
2121
late PolyseedData _data;
2222

2323
/// Check if a seed is a valid Polyseed
24-
static bool isValidSeed(String phrase) {
25-
if (!PolyseedLang.isValidPhrase(phrase)) return false;
26-
final lang = PolyseedLang.getByPhrase(phrase);
27-
28-
return phrase.split(lang.separator).length == numberOfWords;
24+
static bool isValidSeed(String phrase, [PolyseedLang? lang]) {
25+
if (lang != null && !PolyseedLang.isValidPhrase(phrase)) return false;
26+
final polyseedLang = lang ?? PolyseedLang.getByPhrase(phrase);
27+
28+
return polyseedLang
29+
.normalizeSeparator(phrase)
30+
.split(polyseedLang.separator)
31+
.length ==
32+
numberOfWords;
2933
}
3034

3135
/// Create a random [Polyseed]
@@ -66,9 +70,10 @@ class Polyseed {
6670
Polyseed.decode(String str, PolyseedLang lang, PolyseedCoin coin) {
6771
assert(coin.index < GFPoly.size);
6872

69-
final words = str.split(lang.separator);
73+
final words = lang.normalizeSeparator(str).split(lang.separator);
7074
final poly = GFPoly();
7175

76+
7277
// split into words
7378
if (words.length != numberOfWords) {
7479
throw WordNumberException();

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: polyseed
22
description: A pure dart implementation of the 16-word seed scheme for monero
3-
version: 0.0.6
3+
version: 0.0.7
44
homepage: https://cakelabs.com
55
repository: https://github.com/cake-tech/polyseed_dart
66
issue_tracker: https://github.com/cake-tech/polyseed_dart/issues

test/polyseed_test.dart

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:polyseed/polyseed.dart';
44
import 'package:polyseed/src/mnemonics/es_lang.dart';
5+
import 'package:polyseed/src/mnemonics/jp_lang.dart';
56
import 'package:test/test.dart';
67

78
void main() {
@@ -118,6 +119,24 @@ void main() {
118119
expect(legacySeed,
119120
"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");
120121
});
122+
123+
test('Edge Case: Japanese seed with normal space separator', () {
124+
final seed =
125+
"せつぶん いせい てはい けんか たてる ねんきん くたびれる いよく やたい あいこくしん ちきゅう きたえる せたけ あひる かのう げつれい";
126+
127+
final detectedLang = PolyseedLang.getByPhrase(seed);
128+
expect(detectedLang, jpLang);
129+
130+
final isValidSeed = Polyseed.isValidSeed(seed);
131+
expect(isValidSeed, true);
132+
133+
final key = Polyseed.decode(seed, jpLang, coin)
134+
.generateKey(coin, 32)
135+
.toHexString();
136+
137+
expect(key,
138+
"340593b3fd0b2670b4727b6a9b64f3f817e2b97fcb26ea3ae8467234eb857f95");
139+
});
121140
});
122141
});
123142
}

0 commit comments

Comments
 (0)