forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnemonic.go
208 lines (167 loc) · 4.67 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package hedera
import (
"crypto/sha512"
"fmt"
"github.com/tyler-smith/go-bip39"
"golang.org/x/crypto/pbkdf2"
"math/big"
"strings"
)
type Mnemonic struct {
words string
}
func (m Mnemonic) ToPrivateKey(passPhrase string) (PrivateKey, error) {
return PrivateKeyFromMnemonic(m, passPhrase)
}
// GenerateMnemonic generates a random 24-word mnemonic
func GenerateMnemonic24() (Mnemonic, error) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
// It is only possible for there to be an error if the operating
// system's rng is unreadable
return Mnemonic{}, fmt.Errorf("could not retrieve random bytes from the operating system")
}
mnemonic, err := bip39.NewMnemonic(entropy)
// Note that this should never actually fail since it is being provided by library generated mnemonic
if err != nil {
return Mnemonic{}, err
}
return Mnemonic{mnemonic}, nil
}
func GenerateMnemonic12() (Mnemonic, error) {
entropy, err := bip39.NewEntropy(128)
if err != nil {
// It is only possible for there to be an error if the operating
// system's rng is unreadable
return Mnemonic{}, fmt.Errorf("could not retrieve random bytes from the operating system")
}
mnemonic, err := bip39.NewMnemonic(entropy)
// Note that this should never actually fail since it is being provided by library generated mnemonic
if err != nil {
return Mnemonic{}, err
}
return Mnemonic{mnemonic}, nil
}
// MnemonicFromString creates a mnemonic from a string of 24 words separated by spaces
//
// Keys are lazily generated
func MnemonicFromString(s string) (Mnemonic, error) {
return NewMnemonic(strings.Split(s, " "))
}
func (m Mnemonic) String() string {
return m.words
}
func (m Mnemonic) Words() []string {
return strings.Split(m.words, " ")
}
// NewMnemonic Creates a mnemonic from a slice of 24 strings
//
// Keys are lazily generated
func NewMnemonic(words []string) (Mnemonic, error) {
joinedString := strings.Join(words, " ")
if len(words) == 24 || len(words) == 12 || len(words) == 22 {
if len(words) == 22 {
return Mnemonic{
words: joinedString,
}.legacyValidate()
} else if bip39.IsMnemonicValid(joinedString) {
return Mnemonic{
words: joinedString,
}, nil
} else {
return Mnemonic{}, fmt.Errorf("invalid mnemonic composition")
}
} else {
return Mnemonic{}, fmt.Errorf("invalid mnemonic string")
}
}
func (m Mnemonic) legacyValidate() (Mnemonic, error) {
if len(strings.Split(m.words, " ")) != 22 {
return Mnemonic{}, fmt.Errorf("not a legacy mnemonic")
}
indices, err := m.indices()
if err != nil {
return Mnemonic{}, err
}
entropy, checksum := m.toLegacyEntropy(indices)
newChecksum := crc8(entropy)
if checksum != newChecksum {
return Mnemonic{}, fmt.Errorf("legacy mnemonic checksum mismatch")
}
return m, nil
}
func (m Mnemonic) indices() ([]int, error) {
var indices []int
var check bool
for _, mnemonicString := range strings.Split(m.words, " ") {
check = false
for i, stringCheck := range legacy {
if mnemonicString == stringCheck {
check = true
indices = append(indices, int(i))
}
}
if !check {
return make([]int, 0), fmt.Errorf("word is not in the legacy word list")
}
}
return indices, nil
}
func (m Mnemonic) ToLegacyPrivateKey() (PrivateKey, error) {
indices, err := m.indices()
if err != nil {
return PrivateKey{}, err
}
entropy, _ := m.toLegacyEntropy(indices)
password := make([]uint8, len(entropy)+8)
for i, number := range entropy {
password[i] = number
}
for i := len(entropy); i < len(password); i++ {
password[i] = 0xFF
}
salt := []byte{0xFF}
keyData := pbkdf2.Key(password, salt, 2048, 32, sha512.New)
return PrivateKeyFromBytes(keyData)
}
func (m Mnemonic) toLegacyEntropy(indices []int) ([]byte, uint8) {
data := convertRadix(indices, len(legacy), 256, 33)
checksum := data[len(data)-1]
result := make([]uint8, len(data)-1)
for i := 0; i < len(data)-1; i++ {
result[i] = data[i] ^ checksum
}
return result, checksum
}
func convertRadix(nums []int, fromRadix int, toRadix int, toLength int) []uint8 {
num := big.NewInt(0)
for _, element := range nums {
num = num.Mul(num, big.NewInt(int64(fromRadix)))
num = num.Add(num, big.NewInt(int64(element)))
}
result := make([]uint8, toLength)
for i := toLength - 1; i >= 0; i-- {
tem := new(big.Int).Div(num, big.NewInt(int64(toRadix)))
rem := new(big.Int).Mod(num, big.NewInt(int64(toRadix)))
num = num.Set(tem)
result[i] = uint8(rem.Uint64())
}
return result
}
func crc8(data []uint8) uint8 {
var crc uint8
crc = 0xff
for i := 0; i < len(data)-1; i++ {
crc ^= data[i]
for j := 0; j < 8; j++ {
var temp uint8
if crc&1 == 0 {
temp = 0
} else {
temp = 0xb2
}
crc = crc>>1 ^ temp
}
}
return crc ^ 0xff
}