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: keep address format the same with ethereum #119

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 11 additions & 7 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/tendermint/crypto/sha3"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -166,11 +167,8 @@ func AccAddressFromHexUnsafe(address string) (AccAddress, error) {
return AccAddress{}, fmt.Errorf("invalid address hex length: %v != %v", length, 2*EthAddressLength)
}

bz, err := hex.DecodeString(addr)
if err != nil {
return AccAddress{}, err
}
return AccAddress(bz), nil
// Convert to ethereum compatible address format.
return common.HexToAddress(addr).Bytes(), nil
}

// VerifyAddressFormat verifies that the provided bytes form a valid address
Expand Down Expand Up @@ -354,7 +352,10 @@ func (va ValAddress) Equals(va2 Address) bool {

// Returns boolean for whether an AccAddress is empty
func (va ValAddress) Empty() bool {
return len(va) == 0
addrValue := big.NewInt(0)
addrValue.SetBytes(va[:])

return addrValue.Cmp(big.NewInt(0)) == 0
}

// Marshal returns the raw address bytes. It is needed for protobuf
Expand Down Expand Up @@ -493,7 +494,10 @@ func (ca ConsAddress) Equals(ca2 Address) bool {

// Returns boolean for whether an ConsAddress is empty
func (ca ConsAddress) Empty() bool {
return len(ca) == 0
addrValue := big.NewInt(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we change to this, then 0x0000000000000000000000000000000000000000 is empty, is this expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

addrValue.SetBytes(ca[:])

return addrValue.Cmp(big.NewInt(0)) == 0
}

// Marshal returns the raw address bytes. It is needed for protobuf
Expand Down
10 changes: 10 additions & 0 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,13 @@ func (s *addressTestSuite) TestGetFromBech32() {
s.Require().Error(err)
s.Require().Equal("invalid Bech32 prefix; expected x, got cosmos", err.Error())
}

func (s *addressTestSuite) TestAddressCases() {
addrStr1 := "0x17d749d3e2ac204a07e19d8096d9a05c423ea3af"
addr1, _ := types.AccAddressFromHexUnsafe(addrStr1)
s.Require().False(addr1.Empty())

addrStr2 := "0x17D749D3E2ac204a07e19D8096d9a05c423ea3af"
addr2, _ := types.AccAddressFromHexUnsafe(addrStr2)
s.Require().True(addr1.Equals(addr2))
}