54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
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
|
|
}
|