-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathinit.go
52 lines (43 loc) · 1.58 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
package server
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
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(algo keyring.SignatureAlgo) (sdk.AccAddress, string, error) {
// generate a private key, with recovery phrase
info, secret, err := keyring.NewInMemory().NewMnemonic("name", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, algo)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
}
// GenerateSaveCoinKey returns the address of a public key, along with the secret
// phrase to recover the private key.
func GenerateSaveCoinKey(keybase keyring.Keyring, keyName string, overwrite bool, algo keyring.SignatureAlgo) (sdk.AccAddress, string, error) {
exists := false
_, err := keybase.Key(keyName)
if err == nil {
exists = true
}
// ensure no overwrite
if !overwrite && exists {
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
"key already exists, overwrite is disabled")
}
// generate a private key, with recovery phrase
if exists {
err = keybase.Delete(keyName)
if err != nil {
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
"failed to overwrite key")
}
}
info, secret, err := keybase.NewMnemonic(keyName, keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, algo)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
}