Skip to content

Commit 4cc403c

Browse files
committed
Refactor error handling
1 parent 241594f commit 4cc403c

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

dictionary.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,18 @@ type Recoder interface {
2525
// Encode converts the input byte slice into a mnemonic.
2626
Encode(data []byte) ([]string, error)
2727

28-
// Decode takes a mnemonic and returns the original byte slice.
28+
// Decode takes a mnemonic and returns the original byte slice.
2929
Decode(mnemonic []string) ([]byte, error)
3030
}
3131

3232
// NewDictionary creates a new Recoder instance using the provided slice of words.
3333
// Returns an error if there are any problems with the words.
3434
func NewDictionary(words []string) (Recoder, error) {
35-
if len(words) < 2 {
36-
return nil, errors.New("more than 2 words are required")
35+
if len(words) < 2 || (len(words)&(len(words)-1)) != 0 {
36+
return nil, errors.New("dictionary should be complete and len(words) == 2^N")
3737
}
3838

39-
bitLenRaw := math.Log2(float64(len(words)))
40-
if bitLenRaw != float64(int(bitLenRaw)) {
41-
return nil, errors.New("dictionary should be complete, len(words) == 2^N")
42-
}
43-
bitsBatchSize := int(bitLenRaw)
39+
bitsBatchSize := int(math.Log2(float64(len(words))))
4440

4541
bitsToWord := make(map[string]string, len(words))
4642
wordToBits := make(map[string]string, len(words))
@@ -49,16 +45,13 @@ func NewDictionary(words []string) (Recoder, error) {
4945
h := sha256.New()
5046

5147
for i, word := range words {
52-
if word != strings.TrimSpace(word) {
53-
return nil, errors.New("all words should be trimmed")
54-
}
55-
48+
word = strings.TrimSpace(word)
5649
if word == "" {
5750
return nil, errors.New("words should not be empty")
5851
}
5952

6053
if dups[word] {
61-
return nil, errors.New("words should be unique")
54+
return nil, fmt.Errorf("dictionary has duplicate: %s", word)
6255
}
6356
dups[word] = true
6457

@@ -142,7 +135,7 @@ func (d *dictionary) Encode(data []byte) ([]string, error) {
142135
lb := bits[i : i+d.bitsBatchSize]
143136
word, ok := d.bitsToWord[lb]
144137
if !ok {
145-
return mnemonic, errors.New("this should not exists")
138+
return mnemonic, fmt.Errorf("bits-to-word mapping not found for bits: %s", lb)
146139
}
147140

148141
mnemonic = append(mnemonic, word)
@@ -153,7 +146,7 @@ func (d *dictionary) Encode(data []byte) ([]string, error) {
153146
tailBits += strings.Repeat("1", d.bitsBatchSize-tailLen)
154147
tailWord, ok := d.bitsToWord[tailBits]
155148
if !ok {
156-
return mnemonic, errors.New("this should not exists")
149+
return mnemonic, fmt.Errorf("bits-to-word mapping not found for tail bits: %s", tailBits)
157150
}
158151
mnemonic = append(mnemonic, tailWord)
159152
}
@@ -209,12 +202,12 @@ func (d *dictionary) Decode(mnemonic []string) ([]byte, error) {
209202
}
210203
}
211204

212-
deccs, err := d.checksum(dst)
205+
decodedChecksum, err := d.checksum(dst)
213206
if err != nil {
214207
return nil, err
215208
}
216209

217-
if checksum != deccs {
210+
if checksum != decodedChecksum {
218211
return nil, errors.New("invalid checksum")
219212
}
220213

dictionary_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestNewError(t *testing.T) {
4545
{
4646
"not trimmed words",
4747
[]string{"foo", "bar", "fizz", "buzz "},
48-
true,
48+
false,
4949
},
5050
{
5151
"not unique words",
@@ -57,6 +57,11 @@ func TestNewError(t *testing.T) {
5757
[]string{"foo", "bar", "fizz", ""},
5858
true,
5959
},
60+
{
61+
"space word",
62+
[]string{"foo", "bar", "fizz", " \t"},
63+
true,
64+
},
6065
{
6166
"emoji",
6267
[]string{"foo", "bar", "buzz", "👍"},
@@ -90,6 +95,13 @@ func TestDic_Encode(t *testing.T) {
9095
[]string{"0", "0", "0", "1", "1", "0", "1", "0", "0", "0", "0", "1", "1", "0", "0", "1", "0"},
9196
false,
9297
},
98+
{
99+
"word trim",
100+
[]string{"0 ", " 1 \t"},
101+
[]byte{42},
102+
[]string{"1", "0", "0", "1", "0", "1", "0", "1", "0"},
103+
false,
104+
},
93105
{
94106
"base",
95107
[]string{"foo", "bar", "fizz", "buzz"},

0 commit comments

Comments
 (0)