/
local.go
320 lines (279 loc) · 7.84 KB
/
local.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package sdksource
import (
"encoding/json"
"fmt"
"github.com/tyler-smith/go-bip39/wordlists"
"regexp"
"github.com/cosmos/cosmos-sdk/client/keys"
crkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
bip39 "github.com/cosmos/go-bip39"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
Bip32 "github.com/tyler-smith/go-bip32"
Bip39 "github.com/tyler-smith/go-bip39"
)
// keybase is used to make GetKeyBase a singleton
//var keybase crkeys.Keybase
const (
DenomName = "uatom"
defaultBIP39pass = ""
)
type KeyOutput struct {
Name string `json:"name"`
Type string `json:"type"`
Address string `json:"address"`
PubKey string `json:"pub_key"`
Seed string `json:"seed,omitempty"`
Denom string `json:"denom"`
}
type SeedOutput struct {
Seed string `json:"seed"`
}
// To be depreacted! SetKeyBase initialized the LCD keybase. It also requires rootDir as input for the directory for key storing.
//func SetKeyBase(rootDir string) crkeys.Keybase {
// var err error
// keybase = nil
// keybase, err = keys.NewKeyBaseFromDir(rootDir)
// if err != nil {
// fmt.Println(err)
// }
// return keybase
//}
//Deprecated!
//func CreateSeed(rootDir string) string {
// //get the Keybase
// viper.Set(cli.HomeFlag, rootDir)
// kb, err1 := keys.NewKeyBaseFromHomeFlag()
// if err1 != nil {
// fmt.Println(err1)
// }
// // algo type defaults to secp256k1
// algo := crkeys.SigningAlgo("secp256k1")
// pass := defaultBIP39pass
// name := "inmemorykey"
// _, seed, _ := kb.CreateMnemonic(name, crkeys.English, pass, algo)
// //json output the seed
// var So SeedOutput
// So.Seed = seed
// respbyte, _ := json.Marshal(So)
// return string(respbyte)
//
//}
//create mnemonics with bip39 to output 12-word list
func CreateSeed() string {
// default number of words (12):
// this generates a mnemonic directly from the number of words by reading system entropy.
defaultEntropySize := 128
entropy, err := bip39.NewEntropy(defaultEntropySize)
if err != nil {
return err.Error()
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return err.Error()
}
//json output the seed
var So SeedOutput
So.Seed = mnemonic
respbyte, _ := json.Marshal(So)
return string(respbyte)
}
//errors on account creation
func errKeyNameConflict(name string) error {
return fmt.Errorf("acount with name %s already exists", name)
}
func errMissingName() error {
return fmt.Errorf("you have to specify a name for the locally stored account")
}
func errMissingPassword() error {
return fmt.Errorf("you have to specify a password for the locally stored account")
}
func errMissingSeed() error {
return fmt.Errorf("you have to specify seed for key recover")
}
func CreateAccount(rootDir, name, password, seed string) string {
var (
err error
info crkeys.Info
)
//initialize keybase
//SetKeyBase(rootDir)
viper.Set(cli.HomeFlag, rootDir)
kb, errz := keys.NewKeyBaseFromHomeFlag()
if errz != nil {
fmt.Println(errz)
}
//check out the input
if name == "" {
err = errMissingName()
return err.Error()
}
if password == "" {
err = errMissingPassword()
return err.Error()
}
// check if already exists
infos, err := kb.List()
for _, info := range infos {
if info.GetName() == name {
err = errKeyNameConflict(name)
return err.Error()
}
}
//create account
if seed == "" {
algo := crkeys.SigningAlgo("secp256k1")
pass := defaultBIP39pass
name := "inmemorykey"
_, Seed, _ := kb.CreateMnemonic(name, crkeys.English, pass, algo)
seed = Seed
}
info, err1 := kb.CreateAccount(name, seed, defaultBIP39pass, password, 0, 0)
if err1 != nil {
return err1.Error()
}
keyOutput, err2 := crkeys.Bech32KeyOutput(info)
if err2 != nil {
return err2.Error()
}
keyOutput.Mnemonic = seed
//add new field denom for the coin name
var Ko KeyOutput
Ko = KeyOutput{keyOutput.Name, keyOutput.Type, keyOutput.Address, keyOutput.PubKey, keyOutput.Mnemonic, DenomName}
respbyte, _ := json.Marshal(Ko)
return string(respbyte)
}
//for recover key with name, password and seed input
func RecoverKey(rootDir, name, password, seed string) string {
var (
err error
info crkeys.Info
)
//initialize keybase
//SetKeyBase(rootDir)
viper.Set(cli.HomeFlag, rootDir)
kb, errz := keys.NewKeyBaseFromHomeFlag()
if errz != nil {
fmt.Println(errz)
}
if name == "" {
err = errMissingName()
return err.Error()
}
if password == "" {
err = errMissingPassword()
return err.Error()
}
if seed == "" {
err = errMissingSeed()
return err.Error()
}
if err != nil {
return err.Error()
}
info, err1 := kb.CreateAccount(name, seed, defaultBIP39pass, password, 0, 0)
if err1 != nil {
return err1.Error()
}
keyOutput, err2 := crkeys.Bech32KeyOutput(info)
if err2 != nil {
return err2.Error()
}
keyOutput.Mnemonic = seed
//add new field denom for the coin name
var Ko KeyOutput
Ko = KeyOutput{keyOutput.Name, keyOutput.Type, keyOutput.Address, keyOutput.PubKey, keyOutput.Mnemonic, DenomName}
respbyte, _ := json.Marshal(Ko)
return string(respbyte)
}
type UpdateKeyOutput struct {
PasswordUpdate string `json:"pass_update"`
}
//for update the password of the name key stored in level db
func UpdateKey(rootDir, name, oldpass, newpass string) string {
//SetKeyBase(rootDir)
viper.Set(cli.HomeFlag, rootDir)
kb, errz := keys.NewKeyBaseFromHomeFlag()
if errz != nil {
fmt.Println(errz)
}
getNewpass := func() (string, error) {
return newpass, nil
}
err2 := kb.Update(name, oldpass, getNewpass)
if err2 != nil {
return err2.Error()
}
res := fmt.Sprintf("Password is successfully updated!")
//json output the result
var Po UpdateKeyOutput
Po.PasswordUpdate = res
respbyte, _ := json.Marshal(Po)
return string(respbyte)
}
//To differentiate the addresses from various wallets, e.g. cosmos,ETH,qos, .etc
func WalletAddressCheck(addr string) string {
//split the address with prefix, e.g. "0x", "cosmos", "address" for ETH, cosmos, qos respectively
//regexp for ETH address
ethre := regexp.MustCompile("^0x[0-9a-fA-F]{40}$")
//regexp for cosmos address
cosre := regexp.MustCompile("^cosmos1[0-9a-z]{38}$")
//regexp for qos address
qosre := regexp.MustCompile("address1[0-9a-z]{38}$")
switch {
case ethre.MatchString(addr):
return "ETH"
case cosre.MatchString(addr):
return "COSMOS"
case qosre.MatchString(addr):
return "QOS"
default:
return fmt.Sprintf("None")
}
}
func CreateSeedWithMulLangs(lang string) string {
//SetWordlist interactively
switch lang {
case "English" : Bip39.SetWordList(wordlists.English)
case "French" : Bip39.SetWordList(wordlists.French)
case "ChineseSimplified" : Bip39.SetWordList(wordlists.ChineseSimplified)
case "ChineseTraditional" : Bip39.SetWordList(wordlists.ChineseTraditional)
case "Korean" : Bip39.SetWordList(wordlists.Korean)
case "Japanese" : Bip39.SetWordList(wordlists.Japanese)
case "Italian" : Bip39.SetWordList(wordlists.Italian)
case "Spanish" : Bip39.SetWordList(wordlists.Spanish)
default:
fmt.Print("no match language list in wordlist, use English as default")
Bip39.SetWordList(wordlists.English)
}
// Generate a mnemonic for memorization or user-friendly seeds
entropy, _ := Bip39.NewEntropy(256)
mnemonic, _ := Bip39.NewMnemonic(entropy)
// Generate a Bip32 HD wallet for the mnemonic and a user supplied password
seed := Bip39.NewSeed(mnemonic, "Secret Passphrase")
masterKey, _ := Bip32.NewMasterKey(seed)
publicKey := masterKey.PublicKey()
// Display mnemonic and keys
fmt.Println("Mnemonic: ", mnemonic)
fmt.Println("Master private key: ", masterKey)
fmt.Println("Master public key: ", publicKey)
return mnemonic
}
func changeMnemonicLang(lang string) string {
_, ok := Bip39.GetWordIndex(lang)
if ok {
return CreateSeedWithMulLangs(lang)
}
fmt.Errorf("No lang matched")
return ""
}
func compateKDF(functionName string) string{
var algo string
switch functionName {
case "secpk1": algo = "secp256k1"
case "ed25519": algo = "ed25519"
default:
algo = "secp256k1"
}
return algo
}