forked from alphaqiu/mnemonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnemonic.go
116 lines (102 loc) · 2.76 KB
/
mnemonic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package mnemonic
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
"github.com/alphaqiu/mnemonic/entropy"
)
// Mnemonic represents a collection of human readable words
// used for HD wallet seed generation
type Mnemonic struct {
Words []string
Language Language
}
// New returns a new Mnemonic for the given entropy and language
func New(ent []byte, lang Language) (*Mnemonic, error) {
const chunkSize = 11
bits := entropy.CheckSummed(ent)
length := len(bits)
words := make([]string, length/11)
for i := 0; i < length; i += chunkSize {
stringVal := string(bits[i : chunkSize+i])
intVal, err := strconv.ParseInt(stringVal, 2, 64)
if err != nil {
return nil, fmt.Errorf("Could not convert %s to word index", stringVal)
}
word, err := GetWord(lang, intVal)
if err != nil {
return nil, err
}
words[(chunkSize+i)/11-1] = word
}
m := Mnemonic{words, lang}
return &m, nil
}
// NewRandom returns a new Mnemonic with random entropy of the given length
// in bits
func NewRandom(length int, lang Language) (*Mnemonic, error) {
ent, err := entropy.Random(length)
if err != nil {
return nil, fmt.Errorf("Error generating random entropy: %s", err)
}
return New(ent, lang)
}
func IsMnemonicValid(lang Language, sentence string) (bool, error) {
words := strings.Split(sentence, " ")
buff := bytes.NewBuffer(nil)
for _, word := range words {
idx, err := GetIndex(lang, word)
if err != nil {
return false, err
}
buff.Write(paddingLeft([]byte(strconv.FormatInt(idx, 2))))
}
ent, chksum := entropy.BitsToBytes(buff.Bytes())
if ent == nil && chksum == nil {
return false, entropy.ErrBitsChecksumLength
}
return bytes.Equal(entropy.CheckSum(ent), chksum), nil
}
func paddingLeft(data []byte) []byte {
const chunkSize = 11
var ret [chunkSize]byte
length := len(data)
if length < chunkSize {
copy(ret[:chunkSize-length], zero)
copy(ret[chunkSize-length:], data)
return ret[:]
} else {
return data[:chunkSize]
}
}
// Sentence returns a Mnemonic's word collection as a space separated
// sentence
func (m *Mnemonic) Sentence() string {
if m.Language == Japanese {
return strings.Join(m.Words, ` `)
}
return strings.Join(m.Words, " ")
}
func RecoverFromMnemonic(lang Language, sentence string) (m *Mnemonic, err error) {
var check bool
if check, err = IsMnemonicValid(lang, sentence); err != nil {
return nil, err
}
if !check {
return nil, errors.New("invalid mnemonic")
}
words := strings.Split(sentence, " ")
m = &Mnemonic{
Words: words,
Language: lang,
}
return m, nil
}
// GenerateSeed returns a seed used for wallet generation per
// BIP-0032 or similar method. The internal Words set
// of the Mnemonic will be used
func (m *Mnemonic) GenerateSeed(passphrase string) *Seed {
return NewSeed(m.Sentence(), passphrase)
}