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
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2018-04-25 21:27:40 -07:00
|
|
|
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-05 03:31:33 -07:00
|
|
|
// generate a private key, with recovery phrase
|
2020-04-14 12:13:15 -07:00
|
|
|
info, secret, err := keyring.NewInMemory().NewMnemonic("name", keyring.English, sdk.FullFundraiserPath, hd.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
|
|
|
}
|
2020-04-08 02:38:28 -07:00
|
|
|
return sdk.AccAddress(info.GetPubKey().Address()), 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.
|
2020-04-08 02:38:28 -07:00
|
|
|
func GenerateSaveCoinKey(keybase keyring.Keyring, keyName, keyPass string, overwrite bool) (sdk.AccAddress, string, error) {
|
|
|
|
exists := false
|
|
|
|
_, err := keybase.Key(keyName)
|
|
|
|
if err == nil {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
|
2018-04-25 21:27:40 -07:00
|
|
|
// ensure no overwrite
|
2020-04-08 02:38:28 -07:00
|
|
|
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 {
|
2018-10-19 11:00:27 -07:00
|
|
|
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
|
2020-04-08 02:38:28 -07:00
|
|
|
"failed to overwrite key")
|
2018-04-25 21:27:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 12:13:15 -07:00
|
|
|
info, secret, err := keybase.NewMnemonic(keyName, keyring.English, sdk.FullFundraiserPath, hd.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
|
|
|
}
|