/
libs.go
190 lines (166 loc) · 5.45 KB
/
libs.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package mnemonic
import (
"crypto/rand"
"errors"
"strings"
"fmt"
"crypto/sha512"
"golang.org/x/crypto/pbkdf2"
"crypto/sha256"
"strconv"
"github.com/straysh/go_mnemonic/utils"
)
// CS = ENT / 32
// MS = (ENT + CS) / 11
// the initial entropy length (ENT)
// mnemonic sentence (MS)
// checksum bits length (CS)
//| ENT | CS | ENT+CS | MS |
//+-------+----+--------+------+
//| 128 | 4 | 132 | 12 |
//| 160 | 5 | 165 | 15 |
//| 192 | 6 | 198 | 18 |
//| 224 | 7 | 231 | 21 |
//| 256 | 8 | 264 | 24 |
func isValidEntropyBitsLength(entLength int) bool {
return entLength % 32 == 0 && entLength >= 128 && entLength <= 256
}
func (m *Mnemonic) generateRandomEntropy(ent uint) ([]byte, error) {
if !m.isEntropyValid(ent) {
return nil, errors.New("values must be ENT > 128 and ENT % 32 == 0")
}
entropy := make([]byte, ent/8)
_, err := rand.Read( entropy )
if err != nil {
return nil, err
}
return entropy, nil
}
func entropy2Checksum(ent []byte) string {
hash := sha256.Sum256(ent)
entropyBitsLength := len(ent) * bitsInByte
checkhsumBitsLength := entropyBitsLength / multiple
bin := make([]byte, 0)
for i:=0;i<len(hash);i++ {
bufi := utils.IntTo8BitsArray( int(hash[i]) )
bin = append(bin, bufi[:]...)
}
checksum := bin[:checkhsumBitsLength]
return string(checksum)
}
func (m *Mnemonic) isMnemonicValid(mnemonic string) bool {
words := strings.Fields(mnemonic)
mnemonicBitsLength := len(words) * wordBits
checksumBitsLength := mnemonicBitsLength % multiple
entropyBitsLength := mnemonicBitsLength - checksumBitsLength
if isValidEntropyBitsLength(entropyBitsLength) == false {
return false
}
binWithChecksum := make([]byte, 0)
for _,word := range words {
wordIndex, ok := m.WordList.SeekWord(word)
if !ok {
return false
}
buf := utils.IntTo11BitsArray(wordIndex)
binWithChecksum = append(binWithChecksum, buf[:]...)
}
if len(binWithChecksum) != mnemonicBitsLength {
return false
}
bin := binWithChecksum[:entropyBitsLength]
if len(bin) != entropyBitsLength {
return false
}
return true
}
func (m *Mnemonic) pickMnemonic(entropy []byte) (string, error) {
entropyBitsLength := len(entropy)*bitsInByte
if isValidEntropyBitsLength(entropyBitsLength) == false {
return "", fmt.Errorf("values must be 128<= ENT <= 256 and ENT %% 32 == 0, current is %v", entropyBitsLength)
}
checksumBitsLength := entropyBitsLength / 32
pharseLength := (entropyBitsLength + checksumBitsLength) / 11
bin := make([]byte, 0)
for _, b:= range entropy {
buf := utils.IntTo8BitsArray(int(b))
bin = append(bin, buf[:]...)
}
checksum := entropy2Checksum(entropy)
bin = append(bin, checksum...)
if len(bin)%11 !=0 {
return "", fmt.Errorf("invalid entropy checksum length %v %% 11 !==0", len(bin))
}
pharse := make([]string, pharseLength)
var byteAsBinaryString string
for i:=0;i<pharseLength;i++{
startIndex := i * wordBits
endIndex := startIndex + wordBits
byteAsBinaryString = string(bin[startIndex:endIndex])
asInt64, err := strconv.ParseInt(byteAsBinaryString, 2, 64)
if err != nil {
return "", err
}
pharse[i] = m.WordList.PickIndex(asInt64)
}
// \x20:ASCII标准空格/\xa0:nbsp(non-breaking space)不间断空白符/\u3000:全角空白符
// 日语 Join(pharse, "\u3000")
if m.Language == Japanese {
return strings.Join(pharse, "\u3000"), nil
} else {
return strings.Join(pharse, " "), nil
}
}
func (m *Mnemonic) mnemonic2Entropy(mnemonic string) ([]byte, error){
words := strings.Fields(mnemonic)
mnemonicBitsLength := len(words) * wordBits
checksumBitsLength := mnemonicBitsLength % multiple
entropyBitsLength := mnemonicBitsLength - checksumBitsLength
if isValidEntropyBitsLength(entropyBitsLength) == false {
return nil, fmt.Errorf("values must be 128<= ENT <= 256 and ENT %% 32 == 0, current is %v", entropyBitsLength)
}
binWithChecksum := make([]byte, 0)
for _,word := range words {
wordIndex, ok := m.WordList.SeekWord(word)
if !ok {
return nil, fmt.Errorf("invalid word {%v} in wordlist", word)
}
buf := utils.IntTo11BitsArray(wordIndex)
binWithChecksum = append(binWithChecksum, buf[:]...)
}
if len(binWithChecksum) != mnemonicBitsLength {
return nil, fmt.Errorf("mnemonicBitsLength should be %v, current %v, mnemonic: [%v]", mnemonicBitsLength, len(binWithChecksum), mnemonic)
}
bin := binWithChecksum[:entropyBitsLength]
if len(bin) != entropyBitsLength {
return nil, fmt.Errorf("entropyBitsLength should be %v, current %v, mnemonic: [%v]", entropyBitsLength, len(bin), mnemonic)
}
ent := make([]byte, entropyBitsLength / bitsInByte)
var byteAsBinaryString string
for i:=0;i<len(ent);i++ {
startIndex := i * bitsInByte
endIndex := startIndex + bitsInByte
if endIndex >= len(bin)-1 {
byteAsBinaryString = string(bin[startIndex:])
} else {
byteAsBinaryString = string(bin[startIndex:endIndex])
}
asInt64, err := strconv.ParseInt(byteAsBinaryString, 2, 64)
if err != nil {
return nil, err
}
ent[i] = byte(asInt64)
}
checksum := binWithChecksum[entropyBitsLength:]
if len(checksum) != checksumBitsLength{
return nil, fmt.Errorf("checksum expected %d character, but got %d", entropyBitsLength, len(checksum))
}
if string(checksum)!=entropy2Checksum(ent) {
return nil, fmt.Errorf("invalid mnemonic: invalid checksum")
}
return ent, nil
}
//NewSeed Based on a code (word list) returns the seed (hex bytes)
func NewSeed(mnecmonic, passphrase string) []byte {
return pbkdf2.Key([]byte(mnecmonic), []byte("mnemonic"+passphrase), 2048, 64, sha512.New)
}