Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix seid keys list with evm-addr #495

Merged
merged 11 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crypto/keyring/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keyring

import (
"encoding/hex"
"github.com/cosmos/cosmos-sdk/crypto/hd"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
Expand Down Expand Up @@ -77,9 +78,13 @@ func MkAccKeysOutput(infos []Info) ([]KeyOutput, error) {
if err != nil {
return nil, err
}
kos[i], err = PopulateEvmAddrIfApplicable(info, kos[i])
if err != nil {
return nil, err
if info.GetAlgo() == hd.Secp256k1Type {
// We only support getting evm-addr if the algo type is secp256k1 (which it should be, though there
// may be some legacy keys with sr25519)
kos[i], err = PopulateEvmAddrIfApplicable(info, kos[i])
if err != nil {
return nil, err
}
}
}

Expand All @@ -89,6 +94,7 @@ func MkAccKeysOutput(infos []Info) ([]KeyOutput, error) {
func PopulateEvmAddrIfApplicable(info Info, o KeyOutput) (KeyOutput, error) {
localInfo, ok := info.(LocalInfo)
if ok {
// Only works with secp256k1 algo
priv, err := legacy.PrivKeyFromBytes([]byte(localInfo.PrivKeyArmor))
if err != nil {
return o, err
Expand All @@ -99,6 +105,7 @@ func PopulateEvmAddrIfApplicable(info Info, o KeyOutput) (KeyOutput, error) {
return o, err
}
o.EvmAddress = crypto.PubkeyToAddress(privKey.PublicKey).Hex()
} else {
}
return o, nil
}
17 changes: 17 additions & 0 deletions crypto/keyring/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keyring

import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys/sr25519"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -28,3 +29,19 @@ func TestBech32KeysOutput(t *testing.T) {
require.Equal(t, expectedOutput, out)
require.Equal(t, `{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw EvmAddress: PubKey:{"@type":"/cosmos.crypto.multisig.LegacyAminoPubKey","threshold":1,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}]} Mnemonic:}`, fmt.Sprintf("%+v", out))
}

func TestMkAccKeyOutputForSr25519(t *testing.T) {
sk := sr25519.GenPrivKey()
tmpKey := sk.PubKey()
multisigPk := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey})

info, err := NewMultiInfo("multisig", multisigPk)
require.NoError(t, err)
accAddr := sdk.AccAddress(info.GetPubKey().Address())
expectedOutput, err := NewKeyOutput(info.GetName(), info.GetType(), accAddr, multisigPk)
require.NoError(t, err)

out, err := MkAccKeyOutput(info)
require.NoError(t, err)
require.Equal(t, expectedOutput, out)
}
Loading