2018-02-14 12:53:06 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-03-28 08:03:17 -07:00
|
|
|
"fmt"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
|
|
|
|
2018-04-25 21:27:40 -07:00
|
|
|
clkeys "github.com/cosmos/cosmos-sdk/client/keys"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-02-14 12:53:06 -08:00
|
|
|
)
|
|
|
|
|
2018-04-20 14:42:56 -07:00
|
|
|
// GenerateCoinKey returns the address of a public key, along with the secret
|
|
|
|
// phrase to recover the private key.
|
2018-07-06 00:06:53 -07:00
|
|
|
func GenerateCoinKey() (sdk.AccAddress, string, error) {
|
2018-04-20 16:55:22 -07:00
|
|
|
|
2018-04-05 03:31:33 -07:00
|
|
|
// generate a private key, with recovery phrase
|
2019-02-07 14:38:45 -08:00
|
|
|
info, secret, err := clkeys.NewInMemoryKeyBase().CreateMnemonic(
|
|
|
|
"name", keys.English, "pass", keys.Secp256k1)
|
2018-04-05 03:31:33 -07:00
|
|
|
if err != nil {
|
2018-07-09 01:47:38 -07:00
|
|
|
return sdk.AccAddress([]byte{}), "", err
|
2018-04-05 03:31:33 -07:00
|
|
|
}
|
2018-06-28 17:54:47 -07:00
|
|
|
addr := info.GetPubKey().Address()
|
2018-07-06 00:06:53 -07:00
|
|
|
return sdk.AccAddress(addr), secret, nil
|
2018-04-05 03:31:33 -07:00
|
|
|
}
|
2018-04-25 21:27:40 -07:00
|
|
|
|
|
|
|
// GenerateSaveCoinKey returns the address of a public key, along with the secret
|
|
|
|
// phrase to recover the private key.
|
2019-11-14 06:17:21 -08:00
|
|
|
func GenerateSaveCoinKey(keybase keys.Keybase, keyName, keyPass string, overwrite bool) (sdk.AccAddress, string, error) {
|
2018-04-25 21:27:40 -07:00
|
|
|
// ensure no overwrite
|
|
|
|
if !overwrite {
|
|
|
|
_, err := keybase.Get(keyName)
|
|
|
|
if err == nil {
|
2018-10-19 11:00:27 -07:00
|
|
|
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
|
2019-11-14 06:17:21 -08:00
|
|
|
"key already exists, overwrite is disabled")
|
2018-04-25 21:27:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a private key, with recovery phrase
|
2018-06-28 17:54:47 -07:00
|
|
|
info, secret, err := keybase.CreateMnemonic(keyName, keys.English, keyPass, keys.Secp256k1)
|
2018-04-25 21:27:40 -07:00
|
|
|
if err != nil {
|
2018-07-09 01:47:38 -07:00
|
|
|
return sdk.AccAddress([]byte{}), "", err
|
2018-04-25 21:27:40 -07:00
|
|
|
}
|
2019-02-07 14:38:45 -08:00
|
|
|
|
|
|
|
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
|
2018-04-25 21:27:40 -07:00
|
|
|
}
|