@@ -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.
3434func 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
0 commit comments