crypto: add keyring supported algorithms (#439)

* add keyring supported algorithms

* lint

* minor updates

* use eth_secp256k1 as the default signing algo

* add flag

* derivation func

* refactor

* rename keys amino registration

* fix keys

* address comments from review
This commit is contained in:
Federico Kunze
2020-08-11 11:01:15 -04:00
committed by GitHub
parent defcad2bcd
commit a243f43fe2
17 changed files with 239 additions and 52 deletions
+14 -12
View File
@@ -16,7 +16,7 @@ import (
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/ethermint/app"
emintcrypto "github.com/cosmos/ethermint/crypto"
"github.com/cosmos/ethermint/crypto"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra"
@@ -45,7 +45,7 @@ func registerRoutes(rs *lcd.RestServer) {
accountName := viper.GetString(flagUnlockKey)
accountNames := strings.Split(accountName, ",")
var emintKeys []emintcrypto.PrivKeySecp256k1
var keys []crypto.PrivKeySecp256k1
if len(accountName) > 0 {
var err error
inBuf := bufio.NewReader(os.Stdin)
@@ -64,13 +64,13 @@ func registerRoutes(rs *lcd.RestServer) {
}
}
emintKeys, err = unlockKeyFromNameAndPassphrase(accountNames, passphrase)
keys, err = unlockKeyFromNameAndPassphrase(accountNames, passphrase)
if err != nil {
panic(err)
}
}
apis := GetRPCAPIs(rs.CliCtx, emintKeys)
apis := GetRPCAPIs(rs.CliCtx, keys)
// TODO: Allow cli to configure modules https://github.com/ChainSafe/ethermint/issues/74
whitelist := make(map[string]bool)
@@ -98,33 +98,35 @@ func registerRoutes(rs *lcd.RestServer) {
ws.start()
}
func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) (emintKeys []emintcrypto.PrivKeySecp256k1, err error) {
func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]crypto.PrivKeySecp256k1, error) {
keybase, err := keyring.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
os.Stdin,
crypto.EthSecp256k1Options()...,
)
if err != nil {
return
return []crypto.PrivKeySecp256k1{}, err
}
// try the for loop with array []string accountNames
// run through the bottom code inside the for loop
for _, acc := range accountNames {
keys := make([]crypto.PrivKeySecp256k1, len(accountNames))
for i, acc := range accountNames {
// With keyring keybase, password is not required as it is pulled from the OS prompt
privKey, err := keybase.ExportPrivateKeyObject(acc, passphrase)
if err != nil {
return nil, err
return []crypto.PrivKeySecp256k1{}, err
}
var ok bool
emintKey, ok := privKey.(emintcrypto.PrivKeySecp256k1)
keys[i], ok = privKey.(crypto.PrivKeySecp256k1)
if !ok {
panic(fmt.Sprintf("invalid private key type: %T", privKey))
panic(fmt.Sprintf("invalid private key type %T at index %d", privKey, i))
}
emintKeys = append(emintKeys, emintKey)
}
return
return keys, nil
}
+5 -4
View File
@@ -14,7 +14,7 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/ethermint/codec"
emintcrypto "github.com/cosmos/ethermint/crypto"
"github.com/cosmos/ethermint/crypto"
params "github.com/cosmos/ethermint/rpc/args"
emint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/utils"
@@ -46,14 +46,14 @@ import (
type PublicEthAPI struct {
cliCtx context.CLIContext
backend Backend
keys []emintcrypto.PrivKeySecp256k1
keys []crypto.PrivKeySecp256k1
nonceLock *AddrLocker
keybaseLock sync.Mutex
}
// NewPublicEthAPI creates an instance of the public ETH Web3 API.
func NewPublicEthAPI(cliCtx context.CLIContext, backend Backend, nonceLock *AddrLocker,
key []emintcrypto.PrivKeySecp256k1) *PublicEthAPI {
key []crypto.PrivKeySecp256k1) *PublicEthAPI {
return &PublicEthAPI{
cliCtx: cliCtx,
@@ -142,6 +142,7 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
e.cliCtx.Input,
crypto.EthSecp256k1Options()...,
)
if err != nil {
return addresses, err
@@ -294,7 +295,7 @@ func (e *PublicEthAPI) ExportAccount(address common.Address, blockNumber BlockNu
return string(res), nil
}
func checkKeyInKeyring(keys []emintcrypto.PrivKeySecp256k1, address common.Address) (key emintcrypto.PrivKeySecp256k1, exist bool) {
func checkKeyInKeyring(keys []crypto.PrivKeySecp256k1, address common.Address) (key crypto.PrivKeySecp256k1, exist bool) {
if len(keys) > 0 {
for _, key := range keys {
if bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {