From 5f24a93ba54ca2e470ba1349ca08aeaf50304cb8 Mon Sep 17 00:00:00 2001 From: rachid Date: Tue, 10 May 2022 00:18:12 +0200 Subject: [PATCH] remove local ethAddress type and use cosmos-sdk one --- x/qgb/keeper/msg_server.go | 5 +- x/qgb/keeper/query_data_commitment_test.go | 17 +++--- x/qgb/keeper/query_valset_confirm_test.go | 9 +-- x/qgb/orchestrator/test_utils.go | 5 +- x/qgb/types/datacommitmentconfirm.go | 5 +- x/qgb/types/datacommitmentconfirm_test.go | 3 +- x/qgb/types/ethereum.go | 69 ---------------------- x/qgb/types/ethereum_signer.go | 7 ++- x/qgb/types/ethereum_signer_test.go | 3 +- x/qgb/types/keys.go | 3 +- x/qgb/types/validator.go | 11 +++- x/qgb/types/valsetconfirm.go | 5 +- 12 files changed, 44 insertions(+), 98 deletions(-) delete mode 100644 x/qgb/types/ethereum.go diff --git a/x/qgb/keeper/msg_server.go b/x/qgb/keeper/msg_server.go index e314e137b7..464bf6406b 100644 --- a/x/qgb/keeper/msg_server.go +++ b/x/qgb/keeper/msg_server.go @@ -4,6 +4,7 @@ import ( "context" "encoding/hex" "fmt" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "math/big" "github.com/celestiaorg/celestia-app/x/qgb/types" @@ -50,7 +51,7 @@ func (k msgServer) ValsetConfirm( } // Verify ethereum address match - submittedEthAddress, err := types.NewEthAddress(msg.EthAddress) + submittedEthAddress, err := stakingtypes.NewEthAddress(msg.EthAddress) if err != nil { return nil, sdkerrors.Wrap(types.ErrInvalid, "invalid eth address") } @@ -124,7 +125,7 @@ func (k msgServer) DataCommitmentConfirm( } // Verify ethereum address - ethAddress, err := types.NewEthAddress(msg.EthAddress) + ethAddress, err := stakingtypes.NewEthAddress(msg.EthAddress) if err != nil { return nil, sdkerrors.Wrap(types.ErrInvalid, "invalid eth address") } diff --git a/x/qgb/keeper/query_data_commitment_test.go b/x/qgb/keeper/query_data_commitment_test.go index df99624737..f5a0873ba8 100644 --- a/x/qgb/keeper/query_data_commitment_test.go +++ b/x/qgb/keeper/query_data_commitment_test.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/celestiaorg/celestia-app/x/qgb/types" sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -15,7 +16,7 @@ func TestQueryDataCommitment(t *testing.T) { var ( addrStr = "cosmos1v4s3yfg8rujaz56yt5a3xznqjqgyeff4552l40" myValidatorCosmosAddr, err1 = sdk.AccAddressFromBech32(addrStr) - myValidatorEthereumAddr, err2 = types.NewEthAddress("0x3232323232323232323232323232323232323232") + myValidatorEthereumAddr, err2 = stakingtypes.NewEthAddress("0x3232323232323232323232323232323232323232") ) require.NoError(t, err1) require.NoError(t, err2) @@ -107,7 +108,7 @@ func TestAllDataCommitmentsByValidator(t *testing.T) { } var ( myValidatorCosmosAddr1, _ = sdk.AccAddressFromBech32(addr) - myValidatorEthereumAddr1, _ = types.NewEthAddress("0x0101010101010101010101010101010101010101") + myValidatorEthereumAddr1, _ = stakingtypes.NewEthAddress("0x0101010101010101010101010101010101010101") ) input := CreateTestEnv(t) @@ -204,9 +205,9 @@ func TestAllDataCommitmentsByRange(t *testing.T) { myValidatorCosmosAddr1, _ = sdk.AccAddressFromBech32(addrs[0]) myValidatorCosmosAddr2, _ = sdk.AccAddressFromBech32(addrs[1]) myValidatorCosmosAddr3, _ = sdk.AccAddressFromBech32(addrs[2]) - myValidatorEthereumAddr1, _ = types.NewEthAddress("0x0101010101010101010101010101010101010101") - myValidatorEthereumAddr2, _ = types.NewEthAddress("0x0202020202020202020202020202020202020202") - myValidatorEthereumAddr3, _ = types.NewEthAddress("0x0303030303030303030303030303030303030303") + myValidatorEthereumAddr1, _ = stakingtypes.NewEthAddress("0x0101010101010101010101010101010101010101") + myValidatorEthereumAddr2, _ = stakingtypes.NewEthAddress("0x0202020202020202020202020202020202020202") + myValidatorEthereumAddr3, _ = stakingtypes.NewEthAddress("0x0303030303030303030303030303030303030303") ) input := CreateTestEnv(t) @@ -337,9 +338,9 @@ func TestAllDataCommitmentsByCommitment(t *testing.T) { myValidatorCosmosAddr1, _ = sdk.AccAddressFromBech32(addrs[0]) myValidatorCosmosAddr2, _ = sdk.AccAddressFromBech32(addrs[1]) myValidatorCosmosAddr3, _ = sdk.AccAddressFromBech32(addrs[2]) - myValidatorEthereumAddr1, _ = types.NewEthAddress("0x0101010101010101010101010101010101010101") - myValidatorEthereumAddr2, _ = types.NewEthAddress("0x0202020202020202020202020202020202020202") - myValidatorEthereumAddr3, _ = types.NewEthAddress("0x0303030303030303030303030303030303030303") + myValidatorEthereumAddr1, _ = stakingtypes.NewEthAddress("0x0101010101010101010101010101010101010101") + myValidatorEthereumAddr2, _ = stakingtypes.NewEthAddress("0x0202020202020202020202020202020202020202") + myValidatorEthereumAddr3, _ = stakingtypes.NewEthAddress("0x0303030303030303030303030303030303030303") ) input := CreateTestEnv(t) diff --git a/x/qgb/keeper/query_valset_confirm_test.go b/x/qgb/keeper/query_valset_confirm_test.go index 08deeaa2dd..6880d164d6 100644 --- a/x/qgb/keeper/query_valset_confirm_test.go +++ b/x/qgb/keeper/query_valset_confirm_test.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/celestiaorg/celestia-app/x/qgb/types" sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -16,7 +17,7 @@ func TestQueryValsetConfirm(t *testing.T) { addrStr = "cosmos1v4s3yfg8rujaz56yt5a3xznqjqgyeff4552l40" nonce = uint64(1) myValidatorCosmosAddr, err1 = sdk.AccAddressFromBech32(addrStr) - myValidatorEthereumAddr, err2 = types.NewEthAddress("0x3232323232323232323232323232323232323232") + myValidatorEthereumAddr, err2 = stakingtypes.NewEthAddress("0x3232323232323232323232323232323232323232") ) require.NoError(t, err1) require.NoError(t, err2) @@ -80,9 +81,9 @@ func TestAllValsetConfirmsByNonce(t *testing.T) { myValidatorCosmosAddr1, _ = sdk.AccAddressFromBech32(addrs[0]) myValidatorCosmosAddr2, _ = sdk.AccAddressFromBech32(addrs[1]) myValidatorCosmosAddr3, _ = sdk.AccAddressFromBech32(addrs[2]) - myValidatorEthereumAddr1, _ = types.NewEthAddress("0x0101010101010101010101010101010101010101") - myValidatorEthereumAddr2, _ = types.NewEthAddress("0x0202020202020202020202020202020202020202") - myValidatorEthereumAddr3, _ = types.NewEthAddress("0x0303030303030303030303030303030303030303") + myValidatorEthereumAddr1, _ = stakingtypes.NewEthAddress("0x0101010101010101010101010101010101010101") + myValidatorEthereumAddr2, _ = stakingtypes.NewEthAddress("0x0202020202020202020202020202020202020202") + myValidatorEthereumAddr3, _ = stakingtypes.NewEthAddress("0x0303030303030303030303030303030303030303") ) input := CreateTestEnv(t) diff --git a/x/qgb/orchestrator/test_utils.go b/x/qgb/orchestrator/test_utils.go index a04bcfa299..2fcdeb5127 100644 --- a/x/qgb/orchestrator/test_utils.go +++ b/x/qgb/orchestrator/test_utils.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/celestiaorg/celestia-app/x/qgb/types" sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/ethereum/go-ethereum/common" "math/big" ) @@ -18,7 +19,7 @@ func verifyOrchestratorValsetSignatures(broadCasted []sdk.Msg, valsets []*types. if err != nil { return err } - ethAddress, err := types.NewEthAddress(msg.EthAddress) + ethAddress, err := stakingtypes.NewEthAddress(msg.EthAddress) if err != nil { return err } @@ -121,7 +122,7 @@ func verifyOrchestratorDcSignatures(broadCasted []sdk.Msg, dcs []ExtendedDataCom big.NewInt(int64(dcs[i].Nonce)), dcs[i].Commitment, ) - ethAddress, err := types.NewEthAddress(msg.EthAddress) + ethAddress, err := stakingtypes.NewEthAddress(msg.EthAddress) if err != nil { return err } diff --git a/x/qgb/types/datacommitmentconfirm.go b/x/qgb/types/datacommitmentconfirm.go index fdac0ce5d5..845b1da058 100644 --- a/x/qgb/types/datacommitmentconfirm.go +++ b/x/qgb/types/datacommitmentconfirm.go @@ -3,6 +3,7 @@ package types import ( "errors" "fmt" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "math/big" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,7 +17,7 @@ func NewMsgDataCommitmentConfirm( commitment string, signature string, validatorAddress sdk.AccAddress, - ethAddress EthAddress, + ethAddress stakingtypes.EthAddress, beginBlock uint64, endBlock uint64, ) *MsgDataCommitmentConfirm { @@ -47,7 +48,7 @@ func (msg *MsgDataCommitmentConfirm) ValidateBasic() (err error) { if msg.BeginBlock > msg.EndBlock { return errors.New("begin block should be less than end block") } - if err := ValidateEthAddress(msg.EthAddress); err != nil { + if err := stakingtypes.ValidateEthAddress(msg.EthAddress); err != nil { return sdkerrors.Wrap(err, "ethereum address") } return nil diff --git a/x/qgb/types/datacommitmentconfirm_test.go b/x/qgb/types/datacommitmentconfirm_test.go index 9046a63cd7..550678cfa9 100644 --- a/x/qgb/types/datacommitmentconfirm_test.go +++ b/x/qgb/types/datacommitmentconfirm_test.go @@ -4,13 +4,14 @@ import ( "bytes" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/assert" "testing" ) func TestValidateMsgDataCommitmentConfirm(t *testing.T) { var ( - ethAddress, _ = NewEthAddress("0xb462864E395d88d6bc7C5dd5F3F5eb4cc2599255") + ethAddress, _ = stakingtypes.NewEthAddress("0xb462864E395d88d6bc7C5dd5F3F5eb4cc2599255") cosmosAddress sdk.AccAddress = bytes.Repeat([]byte{0x1}, 20) ) specs := map[string]struct { diff --git a/x/qgb/types/ethereum.go b/x/qgb/types/ethereum.go deleted file mode 100644 index 05f66fdc76..0000000000 --- a/x/qgb/types/ethereum.go +++ /dev/null @@ -1,69 +0,0 @@ -package types - -import ( - "bytes" - "fmt" - "regexp" - - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" -) - -// TODO use either this or the one from the cosmos-sdk -const ( - // ETHContractAddressLen is the length of contract address strings - ETHContractAddressLen = 42 -) - -// EthAddress Regular EthAddress -type EthAddress struct { - address string -} - -// GetAddress Returns the contained address as a string -func (ea EthAddress) GetAddress() string { - return ea.address -} - -// SetAddress Sets the contained address, performing validation before updating the value -func (ea *EthAddress) SetAddress(address string) error { - if err := ValidateEthAddress(address); err != nil { - return err - } - ea.address = address - return nil -} - -// NewEthAddress Creates a new EthAddress from a string, performing validation and returning any validation errors -func NewEthAddress(address string) (*EthAddress, error) { - if err := ValidateEthAddress(address); err != nil { - return nil, sdkerrors.Wrap(err, "invalid input address") - } - addr := EthAddress{address} - return &addr, nil -} - -// ValidateEthAddress Validates the input string as an Ethereum Address. -// Addresses must not be empty, have 42 character length, start with 0x and have 40 remaining characters in [0-9a-fA-F] -func ValidateEthAddress(address string) error { - if address == "" { - return fmt.Errorf("empty") - } - if len(address) != ETHContractAddressLen { - return fmt.Errorf("address(%s) of the wrong length exp(%d) actual(%d)", address, ETHContractAddressLen, len(address)) - } - if !regexp.MustCompile("^0x[0-9a-fA-F]{40}$").MatchString(address) { - return fmt.Errorf("address(%s) doesn't pass regex", address) - } - - return nil -} - -// Performs validation on the wrapped string -func (ea EthAddress) ValidateBasic() error { - return ValidateEthAddress(ea.address) -} - -// EthAddrLessThan migrates the Ethereum address less than function -func EthAddrLessThan(e EthAddress, o EthAddress) bool { - return bytes.Compare([]byte(e.GetAddress())[:], []byte(o.GetAddress())[:]) == -1 -} diff --git a/x/qgb/types/ethereum_signer.go b/x/qgb/types/ethereum_signer.go index 0012e39a9c..27a6b5b4ee 100644 --- a/x/qgb/types/ethereum_signer.go +++ b/x/qgb/types/ethereum_signer.go @@ -2,6 +2,7 @@ package types import ( "crypto/ecdsa" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/ethereum/go-ethereum/crypto" @@ -20,7 +21,7 @@ func NewEthereumSignature(hash []byte, privateKey *ecdsa.PrivateKey) ([]byte, er return crypto.Sign(protectedHash.Bytes(), privateKey) } -func EthAddressFromSignature(hash []byte, signature []byte) (*EthAddress, error) { +func EthAddressFromSignature(hash []byte, signature []byte) (*stakingtypes.EthAddress, error) { if len(signature) < 65 { return nil, sdkerrors.Wrap(ErrInvalid, "signature too short") } @@ -49,7 +50,7 @@ func EthAddressFromSignature(hash []byte, signature []byte) (*EthAddress, error) return nil, sdkerrors.Wrap(err, "signature to public key") } - addr, err := NewEthAddress(crypto.PubkeyToAddress(*pubkey).Hex()) + addr, err := stakingtypes.NewEthAddress(crypto.PubkeyToAddress(*pubkey).Hex()) if err != nil { return nil, sdkerrors.Wrap(err, "invalid address from public key") } @@ -59,7 +60,7 @@ func EthAddressFromSignature(hash []byte, signature []byte) (*EthAddress, error) // ValidateEthereumSignature takes a message, an associated signature and public key and // returns an error if the signature isn't valid -func ValidateEthereumSignature(hash []byte, signature []byte, ethAddress EthAddress) error { +func ValidateEthereumSignature(hash []byte, signature []byte, ethAddress stakingtypes.EthAddress) error { addr, err := EthAddressFromSignature(hash, signature) if err != nil { diff --git a/x/qgb/types/ethereum_signer_test.go b/x/qgb/types/ethereum_signer_test.go index 2d32feaa81..b904a39663 100644 --- a/x/qgb/types/ethereum_signer_test.go +++ b/x/qgb/types/ethereum_signer_test.go @@ -2,6 +2,7 @@ package types import ( "encoding/hex" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "testing" "github.com/stretchr/testify/assert" @@ -74,7 +75,7 @@ func TestValsetConfirmSig(t *testing.T) { require.NoError(t, err) // when - ethAddr, err := NewEthAddress(spec.srcETHAddr) + ethAddr, err := stakingtypes.NewEthAddress(spec.srcETHAddr) assert.NoError(t, err) err = ValidateEthereumSignature(hashBytes, sigBytes, *ethAddr) if spec.expErr { diff --git a/x/qgb/types/keys.go b/x/qgb/types/keys.go index 96c55e92d6..31167f56a7 100644 --- a/x/qgb/types/keys.go +++ b/x/qgb/types/keys.go @@ -1,6 +1,7 @@ package types import ( + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "strconv" "strings" @@ -98,7 +99,7 @@ func GetEthAddressByValidatorKey(validator sdk.ValAddress) string { // GetValidatorByEthAddressKey returns the following key format // prefix cosmos-validator // [0xf9][0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B] -func GetValidatorByEthAddressKey(ethAddress EthAddress) string { +func GetValidatorByEthAddressKey(ethAddress stakingtypes.EthAddress) string { return ValidatorByEthAddressKey + string([]byte(ethAddress.GetAddress())) } diff --git a/x/qgb/types/validator.go b/x/qgb/types/validator.go index 317591fea1..3e4ab95bcf 100644 --- a/x/qgb/types/validator.go +++ b/x/qgb/types/validator.go @@ -2,6 +2,7 @@ package types import ( "fmt" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" math "math" "math/big" "sort" @@ -35,13 +36,17 @@ func (b BridgeValidators) ToInternal() (*InternalBridgeValidators, error) { // Bridge Validator but with validated EthereumAddress type InternalBridgeValidator struct { Power uint64 - EthereumAddress EthAddress + EthereumAddress stakingtypes.EthAddress } func NewInternalBridgeValidator(bridgeValidator BridgeValidator) (*InternalBridgeValidator, error) { + validatorEthAddr, err := stakingtypes.NewEthAddress(bridgeValidator.EthereumAddress) + if err != nil { + return nil, err + } i := &InternalBridgeValidator{ Power: bridgeValidator.Power, - EthereumAddress: EthAddress{bridgeValidator.EthereumAddress}, + EthereumAddress: *validatorEthAddr, } if err := i.ValidateBasic(); err != nil { return nil, sdkerrors.Wrap(err, "invalid bridge validator") @@ -83,7 +88,7 @@ func (b InternalBridgeValidators) Sort() { sort.Slice(b, func(i, j int) bool { if b[i].Power == b[j].Power { // Secondary sort on eth address in case powers are equal - return EthAddrLessThan(b[i].EthereumAddress, b[j].EthereumAddress) + return stakingtypes.EthAddrLessThan(b[i].EthereumAddress, b[j].EthereumAddress) } return b[i].Power > b[j].Power }) diff --git a/x/qgb/types/valsetconfirm.go b/x/qgb/types/valsetconfirm.go index 2745e37253..f56da8dd49 100644 --- a/x/qgb/types/valsetconfirm.go +++ b/x/qgb/types/valsetconfirm.go @@ -3,6 +3,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) var _ sdk.Msg = &MsgValsetConfirm{} @@ -10,7 +11,7 @@ var _ sdk.Msg = &MsgValsetConfirm{} // NewMsgValsetConfirm returns a new msgValSetConfirm func NewMsgValsetConfirm( nonce uint64, - ethAddress EthAddress, + ethAddress stakingtypes.EthAddress, validator sdk.AccAddress, signature string, ) *MsgValsetConfirm { @@ -38,7 +39,7 @@ func (msg *MsgValsetConfirm) ValidateBasic() (err error) { if _, err = sdk.AccAddressFromBech32(msg.Orchestrator); err != nil { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Orchestrator) } - if err := ValidateEthAddress(msg.EthAddress); err != nil { + if err := stakingtypes.ValidateEthAddress(msg.EthAddress); err != nil { return sdkerrors.Wrap(err, "ethereum address") } return nil