Skip to content

Fix: ethsecp256k1.PubKey implements cosmos-sdk PubKey interface incorrectly. #601

@cloudgray

Description

@cloudgray

Problem

ethsecp256k1.PubKey implements cosmos-sdk PubKey interface incorrectly.

Problem Details

Error Reproduction

When running systemtests without replacing cosmos/evm dependency (replace github.com/cosmos/evm => ../..), the build error occurs.
This is combination of 2 different issue.

  1. PubKey.Address() method is defined with invalid return type.
  2. cosmossdk.io/systemtests used in cosmos/evm is dependent on different cosmos-sdk and cometbft version from cosmos/evm.
../../../../../go/pkg/mod/github.com/cosmos/[email protected]/crypto/ethsecp256k1/ethsecp256k1.go:76:9: cannot use &PubKey{…} (value of type *PubKey) as "github.com/cosmos/cosmos-sdk/crypto/types".PubKey value in return statement: *PubKey does not implement "github.com/cosmos/cosmos-sdk/crypto/types".PubKey (wrong type for method Address)
                have Address() "github.com/cometbft/cometbft/crypto".Address
                want Address() "github.com/cosmos/cosmos-sdk/crypto/types".Address
../../../../../go/pkg/mod/github.com/cosmos/[email protected]/crypto/ethsecp256k1/ethsecp256k1.go:144:27: cannot use &PubKey{} (value of type *PubKey) as "github.com/cosmos/cosmos-sdk/crypto/types".PubKey value in variable declaration: *PubKey does not implement "github.com/cosmos/cosmos-sdk/crypto/types".PubKey (wrong type for method Address)
                have Address() "github.com/cometbft/cometbft/crypto".Address
                want Address() "github.com/cosmos/cosmos-sdk/crypto/types".Address
FAIL    github.com/cosmos/evm/tests/systemtests [build failed]
FAIL    github.com/cosmos/evm/tests/systemtests/clients [build failed]
FAIL    github.com/cosmos/evm/tests/systemtests/mempool [build failed]
FAIL    github.com/cosmos/evm/tests/systemtests/suite [build failed]

Solution

If we implement PubKey interface correctly like below, even though cosmossdk.io/systemtests is dependent on different versions of cosmos-sdk and cometbft from cosmos/evm, it can be compatible.

As-Is

// Address returns the address of the ECDSA public key.
// The function will return an empty address if the public key is invalid.
func (pubKey PubKey) Address() tmcrypto.Address {
	pubk, err := crypto.DecompressPubkey(pubKey.Key)
	if err != nil {
		return nil
	}

	return tmcrypto.Address(crypto.PubkeyToAddress(*pubk).Bytes())
}

To-Be

// Address returns the address of the ECDSA public key.
// The function will return an empty address if the public key is invalid.
func (pubKey PubKey) Address() cryptotypes.Address {
	pubk, err := crypto.DecompressPubkey(pubKey.Key)
	if err != nil {
		return nil
	}

	return cryptotypes.Address(crypto.PubkeyToAddress(*pubk).Bytes())
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions