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
+84
View File
@@ -0,0 +1,84 @@
package crypto
import (
"fmt"
"github.com/pkg/errors"
"crypto/hmac"
"crypto/sha512"
"github.com/tyler-smith/go-bip39"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)
const (
// EthSecp256k1 defines the ECDSA secp256k1 used on Ethereum
EthSecp256k1 = keyring.SigningAlgo("eth_secp256k1")
)
// SupportedAlgorithms defines the list of signing algorithms used on Ethermint:
// - eth_secp256k1 (Ethereum)
// - secp256k1 (Tendermint)
var SupportedAlgorithms = []keyring.SigningAlgo{EthSecp256k1, keyring.Secp256k1}
// EthSecp256k1Options defines a keyring options for the ethereum Secp256k1 curve.
func EthSecp256k1Options() []keyring.KeybaseOption {
return []keyring.KeybaseOption{
keyring.WithKeygenFunc(EthermintKeygenFunc),
keyring.WithDeriveFunc(DeriveKey),
keyring.WithSupportedAlgos(SupportedAlgorithms),
keyring.WithSupportedAlgosLedger(SupportedAlgorithms),
}
}
func DeriveKey(mnemonic, bip39Passphrase, hdPath string, algo keyring.SigningAlgo) ([]byte, error) {
switch algo {
case keyring.Secp256k1:
return keyring.StdDeriveKey(mnemonic, bip39Passphrase, hdPath, algo)
case EthSecp256k1:
return DeriveSecp256k1(mnemonic, bip39Passphrase, hdPath)
default:
return nil, errors.Wrap(keyring.ErrUnsupportedSigningAlgo, string(algo))
}
}
// EthermintKeygenFunc is the key generation function to generate secp256k1 ToECDSA
// from ethereum.
func EthermintKeygenFunc(bz []byte, algo keyring.SigningAlgo) (tmcrypto.PrivKey, error) {
if algo != EthSecp256k1 {
return nil, fmt.Errorf("signing algorithm must be %s, got %s", EthSecp256k1, algo)
}
return PrivKeySecp256k1(bz), nil
}
func DeriveSecp256k1(mnemonic, bip39Passphrase, _ string) ([]byte, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, bip39Passphrase)
if err != nil {
return nil, err
}
// HMAC the seed to produce the private key and chain code
mac := hmac.New(sha512.New, []byte("Bitcoin seed"))
_, err = mac.Write(seed)
if err != nil {
return nil, err
}
seed = mac.Sum(nil)
priv, err := ethcrypto.ToECDSA(seed[:32])
if err != nil {
return nil, err
}
derivedKey := PrivKeySecp256k1(ethcrypto.FromECDSA(priv))
return derivedKey, nil
}
+99
View File
@@ -0,0 +1,99 @@
package crypto
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestEthermintKeygenFunc(t *testing.T) {
privkey, err := GenerateKey()
require.NoError(t, err)
testCases := []struct {
name string
privKey []byte
algo keyring.SigningAlgo
expPass bool
}{
{
"valid ECDSA privKey",
ethcrypto.FromECDSA(privkey.ToECDSA()),
EthSecp256k1,
true,
},
{
"nil bytes, valid algo",
nil,
EthSecp256k1,
true,
},
{
"empty bytes, valid algo",
[]byte{},
EthSecp256k1,
true,
},
{
"invalid algo",
nil,
keyring.MultiAlgo,
false,
},
}
for _, tc := range testCases {
privkey, err := EthermintKeygenFunc(tc.privKey, tc.algo)
if tc.expPass {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
require.Nil(t, privkey, tc.name)
}
}
}
func TestKeyring(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
mockIn := strings.NewReader("")
t.Cleanup(cleanup)
kr, err := keyring.NewKeyring("ethermint", keyring.BackendTest, dir, mockIn, EthSecp256k1Options()...)
require.NoError(t, err)
// fail in retrieving key
info, err := kr.Get("foo")
require.Error(t, err)
require.Nil(t, info)
mockIn.Reset("password\npassword\n")
info, mnemonic, err := kr.CreateMnemonic("foo", keyring.English, sdk.FullFundraiserPath, EthSecp256k1)
require.NoError(t, err)
require.NotEmpty(t, mnemonic)
require.Equal(t, "foo", info.GetName())
require.Equal(t, "local", info.GetType().String())
require.Equal(t, EthSecp256k1, info.GetAlgo())
params := *hd.NewFundraiserParams(0, sdk.CoinType, 0)
hdPath := params.String()
bz, err := DeriveKey(mnemonic, keyring.DefaultBIP39Passphrase, hdPath, EthSecp256k1)
require.NoError(t, err)
require.NotEmpty(t, bz)
bz, err = DeriveKey(mnemonic, keyring.DefaultBIP39Passphrase, hdPath, keyring.Secp256k1)
require.NoError(t, err)
require.NotEmpty(t, bz)
bz, err = DeriveKey(mnemonic, keyring.DefaultBIP39Passphrase, hdPath, keyring.SigningAlgo(""))
require.Error(t, err)
require.Empty(t, bz)
}
+14 -5
View File
@@ -1,19 +1,28 @@
package crypto
import (
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)
var cryptoCodec = codec.New()
// CryptoCodec is the default amino codec used by ethermint
var CryptoCodec = codec.New()
// Amino encoding names
const (
// Amino encoding names
PrivKeyAminoName = "crypto/PrivKeySecp256k1"
PubKeyAminoName = "crypto/PubKeySecp256k1"
PrivKeyAminoName = "ethermint/PrivKeySecp256k1"
PubKeyAminoName = "ethermint/PubKeySecp256k1"
)
func init() {
RegisterCodec(cryptoCodec)
// replace the keyring codec with the ethermint crypto codec to prevent
// amino panics because of unregistered Priv/PubKey
keyring.CryptoCdc = CryptoCodec
keyring.RegisterCodec(CryptoCodec)
cryptoamino.RegisterAmino(CryptoCodec)
RegisterCodec(CryptoCodec)
}
// RegisterCodec registers all the necessary types with amino for the given
-12
View File
@@ -1,12 +0,0 @@
package crypto
import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
tmcrypto "github.com/tendermint/tendermint/crypto"
)
// EthermintKeygenFunc is the key generation function to generate secp256k1 ToECDSA
// from ethereum.
func EthermintKeygenFunc(bz []byte, algo keyring.SigningAlgo) (tmcrypto.PrivKey, error) {
return PrivKeySecp256k1(bz), nil
}
+2 -2
View File
@@ -45,7 +45,7 @@ func (privkey PrivKeySecp256k1) PubKey() tmcrypto.PubKey {
// Bytes returns the raw ECDSA private key bytes.
func (privkey PrivKeySecp256k1) Bytes() []byte {
return cryptoCodec.MustMarshalBinaryBare(privkey)
return CryptoCodec.MustMarshalBinaryBare(privkey)
}
// Sign creates a recoverable ECDSA signature on the secp256k1 curve over the
@@ -87,7 +87,7 @@ func (key PubKeySecp256k1) Address() tmcrypto.Address {
// Bytes returns the raw bytes of the ECDSA public key.
func (key PubKeySecp256k1) Bytes() []byte {
bz, err := cryptoCodec.MarshalBinaryBare(key)
bz, err := CryptoCodec.MarshalBinaryBare(key)
if err != nil {
panic(err)
}