crypto: refactor for stargate (#559)

* changelog

* update changelog

* crypto: refactor for stargate

* fixes

* fix keys

* changelog
This commit is contained in:
Federico Kunze
2020-10-06 20:57:55 +02:00
committed by GitHub
parent e7a19a1c0a
commit 4e01da905a
32 changed files with 165 additions and 146 deletions
+5 -3
View File
@@ -3,9 +3,11 @@
package rpc
import (
"github.com/cosmos/cosmos-sdk/client/context"
emintcrypto "github.com/cosmos/ethermint/crypto"
"github.com/ethereum/go-ethereum/rpc"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
)
// RPC namespaces and API version
@@ -19,7 +21,7 @@ const (
)
// GetRPCAPIs returns the list of all APIs
func GetRPCAPIs(cliCtx context.CLIContext, keys []emintcrypto.PrivKeySecp256k1) []rpc.API {
func GetRPCAPIs(cliCtx context.CLIContext, keys []ethsecp256k1.PrivKey) []rpc.API {
nonceLock := new(AddrLocker)
backend := NewEthermintBackend(cliCtx)
ethAPI := NewPublicEthAPI(cliCtx, backend, nonceLock, keys)
+9 -8
View File
@@ -19,7 +19,8 @@ import (
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/ethermint/app"
"github.com/cosmos/ethermint/crypto"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/crypto/hd"
"github.com/ethereum/go-ethereum/rpc"
)
@@ -45,7 +46,7 @@ func registerRoutes(rs *lcd.RestServer) {
accountName := viper.GetString(flagUnlockKey)
accountNames := strings.Split(accountName, ",")
var privkeys []crypto.PrivKeySecp256k1
var privkeys []ethsecp256k1.PrivKey
if len(accountName) > 0 {
var err error
inBuf := bufio.NewReader(os.Stdin)
@@ -102,31 +103,31 @@ func registerRoutes(rs *lcd.RestServer) {
ws.start()
}
func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]crypto.PrivKeySecp256k1, error) {
func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]ethsecp256k1.PrivKey, error) {
keybase, err := keys.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
os.Stdin,
crypto.EthSecp256k1Options()...,
hd.EthSecp256k1Options()...,
)
if err != nil {
return []crypto.PrivKeySecp256k1{}, err
return []ethsecp256k1.PrivKey{}, err
}
// try the for loop with array []string accountNames
// run through the bottom code inside the for loop
keys := make([]crypto.PrivKeySecp256k1, len(accountNames))
keys := make([]ethsecp256k1.PrivKey, 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 []crypto.PrivKeySecp256k1{}, err
return []ethsecp256k1.PrivKey{}, err
}
var ok bool
keys[i], ok = privKey.(crypto.PrivKeySecp256k1)
keys[i], ok = privKey.(ethsecp256k1.PrivKey)
if !ok {
panic(fmt.Sprintf("invalid private key type %T at index %d", privKey, i))
}
+7 -6
View File
@@ -10,7 +10,8 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/ethermint/crypto"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/crypto/hd"
params "github.com/cosmos/ethermint/rpc/args"
ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/utils"
@@ -46,14 +47,14 @@ type PublicEthAPI struct {
chainIDEpoch *big.Int
logger log.Logger
backend Backend
keys []crypto.PrivKeySecp256k1 // unlocked keys
keys []ethsecp256k1.PrivKey // unlocked keys
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 []crypto.PrivKeySecp256k1) *PublicEthAPI {
key []ethsecp256k1.PrivKey) *PublicEthAPI {
epoch, err := ethermint.ParseChainID(cliCtx.ChainID)
if err != nil {
@@ -86,7 +87,7 @@ func (e *PublicEthAPI) getKeybaseInfo() error {
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
e.cliCtx.Input,
crypto.EthSecp256k1Options()...,
hd.EthSecp256k1Options()...,
)
if err != nil {
return err
@@ -181,7 +182,7 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
e.cliCtx.Input,
crypto.EthSecp256k1Options()...,
hd.EthSecp256k1Options()...,
)
if err != nil {
return addresses, err
@@ -342,7 +343,7 @@ func (e *PublicEthAPI) ExportAccount(address common.Address, blockNumber BlockNu
return string(res), nil
}
func checkKeyInKeyring(keys []crypto.PrivKeySecp256k1, address common.Address) (key crypto.PrivKeySecp256k1, exist bool) {
func checkKeyInKeyring(keys []ethsecp256k1.PrivKey, address common.Address) (key ethsecp256k1.PrivKey, exist bool) {
if len(keys) > 0 {
for _, key := range keys {
if bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {
+8 -7
View File
@@ -19,7 +19,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
emintcrypto "github.com/cosmos/ethermint/crypto"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/crypto/hd"
params "github.com/cosmos/ethermint/rpc/args"
)
@@ -54,7 +55,7 @@ func (e *PersonalEthAPI) getKeybaseInfo() ([]keys.Info, error) {
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
e.ethAPI.cliCtx.Input,
emintcrypto.EthSecp256k1Options()...,
hd.EthSecp256k1Options()...,
)
if err != nil {
return nil, err
@@ -77,9 +78,9 @@ func (e *PersonalEthAPI) ImportRawKey(privkey, password string) (common.Address,
return common.Address{}, err
}
privKey := emintcrypto.PrivKeySecp256k1(crypto.FromECDSA(priv))
privKey := ethsecp256k1.PrivKey(crypto.FromECDSA(priv))
armor := mintkey.EncryptArmorPrivKey(privKey, password, emintcrypto.EthSecp256k1Type)
armor := mintkey.EncryptArmorPrivKey(privKey, password, ethsecp256k1.KeyType)
// ignore error as we only care about the length of the list
list, _ := e.ethAPI.cliCtx.Keybase.List()
@@ -126,7 +127,7 @@ func (e *PersonalEthAPI) LockAccount(address common.Address) bool {
continue
}
tmp := make([]emintcrypto.PrivKeySecp256k1, len(e.ethAPI.keys)-1)
tmp := make([]ethsecp256k1.PrivKey, len(e.ethAPI.keys)-1)
copy(tmp[:i], e.ethAPI.keys[:i])
copy(tmp[i:], e.ethAPI.keys[i+1:])
e.ethAPI.keys = tmp
@@ -147,7 +148,7 @@ func (e *PersonalEthAPI) NewAccount(password string) (common.Address, error) {
}
name := "key_" + time.Now().UTC().Format(time.RFC3339)
info, _, err := e.ethAPI.cliCtx.Keybase.CreateMnemonic(name, keys.English, password, emintcrypto.EthSecp256k1)
info, _, err := e.ethAPI.cliCtx.Keybase.CreateMnemonic(name, keys.English, password, hd.EthSecp256k1)
if err != nil {
return common.Address{}, err
}
@@ -193,7 +194,7 @@ func (e *PersonalEthAPI) UnlockAccount(_ context.Context, addr common.Address, p
return false, err
}
emintKey, ok := privKey.(emintcrypto.PrivKeySecp256k1)
emintKey, ok := privKey.(ethsecp256k1.PrivKey)
if !ok {
return false, fmt.Errorf("invalid private key type: %T", privKey)
}