forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
53 lines (43 loc) · 1.51 KB
/
init.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
package server
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys"
clkeys "github.com/cosmos/cosmos-sdk/client/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GenerateCoinKey returns the address of a public key, along with the secret
// phrase to recover the private key.
func GenerateCoinKey() (sdk.AccAddress, string, error) {
// generate a private key, with recovery phrase
info, secret, err := clkeys.NewInMemoryKeyBase().CreateMnemonic(
"name", keys.English, "pass", keys.Secp256k1)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
addr := info.GetPubKey().Address()
return sdk.AccAddress(addr), secret, nil
}
// GenerateSaveCoinKey returns the address of a public key, along with the secret
// phrase to recover the private key.
func GenerateSaveCoinKey(clientRoot, keyName, keyPass string,
overwrite bool) (sdk.AccAddress, string, error) {
// get the keystore from the client
keybase, err := clkeys.NewKeyBaseFromDir(clientRoot)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
// ensure no overwrite
if !overwrite {
_, err := keybase.Get(keyName)
if err == nil {
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
"key already exists, overwrite is disabled (clientRoot: %s)", clientRoot)
}
}
// generate a private key, with recovery phrase
info, secret, err := keybase.CreateMnemonic(keyName, keys.English, keyPass, keys.Secp256k1)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
}