Skip to content

Commit

Permalink
remove local ethAddress type and use cosmos-sdk one
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed May 9, 2022
1 parent fb271ad commit db67044
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 98 deletions.
5 changes: 3 additions & 2 deletions x/qgb/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
17 changes: 9 additions & 8 deletions x/qgb/keeper/query_data_commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions x/qgb/keeper/query_valset_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions x/qgb/orchestrator/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions x/qgb/types/datacommitmentconfirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,7 +17,7 @@ func NewMsgDataCommitmentConfirm(
commitment string,
signature string,
validatorAddress sdk.AccAddress,
ethAddress EthAddress,
ethAddress stakingtypes.EthAddress,
beginBlock uint64,
endBlock uint64,
) *MsgDataCommitmentConfirm {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion x/qgb/types/datacommitmentconfirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
69 changes: 0 additions & 69 deletions x/qgb/types/ethereum.go

This file was deleted.

7 changes: 4 additions & 3 deletions x/qgb/types/ethereum_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion x/qgb/types/ethereum_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/hex"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion x/qgb/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"strconv"
"strings"

Expand Down Expand Up @@ -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()))
}

Expand Down
11 changes: 8 additions & 3 deletions x/qgb/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
math "math"
"math/big"
"sort"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
})
Expand Down
5 changes: 3 additions & 2 deletions x/qgb/types/valsetconfirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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{}

// NewMsgValsetConfirm returns a new msgValSetConfirm
func NewMsgValsetConfirm(
nonce uint64,
ethAddress EthAddress,
ethAddress stakingtypes.EthAddress,
validator sdk.AccAddress,
signature string,
) *MsgValsetConfirm {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit db67044

Please sign in to comment.