@@ -26,10 +26,10 @@ type Recoder interface {
2626}
2727
2828// NewDictionary creates a new Recoder instance using the provided slice of words.
29- // Returns an error if any problems with words.
29+ // Returns an error if there are any problems with the words.
3030func NewDictionary (words []string ) (Recoder , error ) {
3131 if len (words ) < 2 {
32- return nil , errors .New ("more then 2 words required" )
32+ return nil , errors .New ("more than 2 words are required" )
3333 }
3434
3535 bitsToWord := make (map [string ]string , len (words ))
@@ -39,7 +39,7 @@ func NewDictionary(words []string) (Recoder, error) {
3939
4040 for i , word := range words {
4141 if word != strings .TrimSpace (word ) {
42- return nil , errors .New ("all words should be trimed " )
42+ return nil , errors .New ("all words should be trimmed " )
4343 }
4444
4545 if word == "" {
@@ -82,10 +82,9 @@ func (d *dictionary) Encode(data []byte) ([]string, error) {
8282 bits := bitsBuilder .String () + cs
8383
8484 right , left := 0 , d .maxBitsLen
85- for right < left {
85+ for right < len ( bits ) {
8686 if left > len (bits ) {
8787 left = len (bits )
88- continue
8988 }
9089
9190 lb := bits [right :left ]
@@ -108,13 +107,15 @@ func (d *dictionary) Decode(mnemonic []string) ([]byte, error) {
108107 for _ , m := range mnemonic {
109108 idx , ok := d .wordToBits [m ]
110109 if ! ok {
111- return nil , errors .New ("invalid mnemonic" )
110+ return nil , errors .New ("invalid mnemonic word " )
112111 }
113112 bitsBuilder .WriteString (fmt .Sprintf ("%b" , idx ))
114113 }
115114
116- // thanks to https://medium.com/@yaievgeniy/converting-bit-binary-string-to-bytes-in-go-and-not-only-7888156e9576
117115 bitString := bitsBuilder .String ()
116+ if len (bitString ) < checksumLen {
117+ return nil , errors .New ("mnemonic too short for checksum" )
118+ }
118119 checksum := bitString [len (bitString )- checksumLen :]
119120 bitString = bitString [:len (bitString )- checksumLen ]
120121
0 commit comments