2020-08-11 15:01:15 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcutil/hdkeychain"
|
2020-08-11 15:01:15 +00:00
|
|
|
"github.com/tyler-smith/go-bip39"
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
ethaccounts "github.com/ethereum/go-ethereum/accounts"
|
2020-08-11 15:01:15 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
|
|
|
tmcrypto "github.com/tendermint/tendermint/crypto"
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
2020-08-11 15:01:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-09-30 00:34:01 +00:00
|
|
|
// EthSecp256k1Type string constant for the EthSecp256k1 algorithm
|
|
|
|
EthSecp256k1Type = "eth_secp256k1"
|
2020-08-11 15:01:15 +00:00
|
|
|
// EthSecp256k1 defines the ECDSA secp256k1 used on Ethereum
|
2020-09-30 00:34:01 +00:00
|
|
|
EthSecp256k1 = keys.SigningAlgo(EthSecp256k1Type)
|
2020-08-11 15:01:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SupportedAlgorithms defines the list of signing algorithms used on Ethermint:
|
|
|
|
// - eth_secp256k1 (Ethereum)
|
|
|
|
// - secp256k1 (Tendermint)
|
2020-08-23 21:41:54 +00:00
|
|
|
var SupportedAlgorithms = []keys.SigningAlgo{EthSecp256k1, keys.Secp256k1}
|
|
|
|
|
|
|
|
// EthSecp256k1Options defines a keys options for the ethereum Secp256k1 curve.
|
|
|
|
func EthSecp256k1Options() []keys.KeybaseOption {
|
|
|
|
return []keys.KeybaseOption{
|
|
|
|
keys.WithKeygenFunc(EthermintKeygenFunc),
|
|
|
|
keys.WithDeriveFunc(DeriveKey),
|
|
|
|
keys.WithSupportedAlgos(SupportedAlgorithms),
|
|
|
|
keys.WithSupportedAlgosLedger(SupportedAlgorithms),
|
2020-08-11 15:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
func DeriveKey(mnemonic, bip39Passphrase, hdPath string, algo keys.SigningAlgo) ([]byte, error) {
|
2020-08-11 15:01:15 +00:00
|
|
|
switch algo {
|
2020-08-23 21:41:54 +00:00
|
|
|
case keys.Secp256k1:
|
|
|
|
return keys.StdDeriveKey(mnemonic, bip39Passphrase, hdPath, algo)
|
2020-08-11 15:01:15 +00:00
|
|
|
case EthSecp256k1:
|
|
|
|
return DeriveSecp256k1(mnemonic, bip39Passphrase, hdPath)
|
|
|
|
default:
|
2020-08-23 21:41:54 +00:00
|
|
|
return nil, errors.Wrap(keys.ErrUnsupportedSigningAlgo, string(algo))
|
2020-08-11 15:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthermintKeygenFunc is the key generation function to generate secp256k1 ToECDSA
|
|
|
|
// from ethereum.
|
2020-08-23 21:41:54 +00:00
|
|
|
func EthermintKeygenFunc(bz []byte, algo keys.SigningAlgo) (tmcrypto.PrivKey, error) {
|
2020-08-11 15:01:15 +00:00
|
|
|
if algo != EthSecp256k1 {
|
|
|
|
return nil, fmt.Errorf("signing algorithm must be %s, got %s", EthSecp256k1, algo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrivKeySecp256k1(bz), nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
// DeriveSecp256k1 derives and returns the eth_secp256k1 private key for the given mnemonic and HD path.
|
|
|
|
func DeriveSecp256k1(mnemonic, bip39Passphrase, path string) ([]byte, error) {
|
|
|
|
hdpath, err := ethaccounts.ParseDerivationPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:01:15 +00:00
|
|
|
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, bip39Passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
|
2020-08-11 15:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
key := masterKey
|
|
|
|
for _, n := range hdpath {
|
|
|
|
key, err = key.Child(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 15:01:15 +00:00
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
privateKey, err := key.ECPrivKey()
|
2020-08-11 15:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
privateKeyECDSA := privateKey.ToECDSA()
|
|
|
|
derivedKey := PrivKeySecp256k1(ethcrypto.FromECDSA(privateKeyECDSA))
|
2020-08-11 15:01:15 +00:00
|
|
|
return derivedKey, nil
|
|
|
|
}
|