Skip to content

Commit

Permalink
fix: allow 20 and 32 bytes addresses from events (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
maharifu authored Aug 19, 2024
1 parent b5164f3 commit e7d7e31
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
16 changes: 14 additions & 2 deletions chain/evm/compass.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,20 @@ func compassBytesToPalomaAddress(b any) (sdk.AccAddress, error) {
return addr, fmt.Errorf("invalid paloma address bytes")
}

// Keep only the last 20 bytes, removing the first 12 zeroes
addrBytes := rawBytes[12:]
var addrBytes []byte
for i := range rawBytes {
if rawBytes[i] != 0 {
// Allow addresses starting with 0 and align to either 20 or 32
// bytes
if i < 12 {
addrBytes = rawBytes[:]
} else {
addrBytes = rawBytes[12:]
}

break
}
}

// The Unmarshal function below does not check for errors, so we need to do
// it beforehand
Expand Down
46 changes: 46 additions & 0 deletions chain/evm/compass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2063,3 +2063,49 @@ func TestIfTheConsensusHasBeenReached(t *testing.T) {
})
}
}

func TestCompassBytesToPaloma(t *testing.T) {
for _, tt := range []struct {
name string
src [32]byte
res sdk.AccAddress
err bool
}{
{
name: "20 byte address",
src: [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
res: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
{
name: "20 byte address starting with 0",
src: [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
res: []byte{0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
{
name: "32 byte address",
src: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
res: []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
{
name: "32 byte address starting with 0",
src: [32]byte{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
res: []byte{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
{
name: "invalid address",
src: [32]byte{},
err: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
res, err := compassBytesToPalomaAddress(tt.src)
if tt.err {
require.Error(t, err)
} else {
require.NoError(t, err)
}

require.Equal(t, tt.res, res)
})
}
}

0 comments on commit e7d7e31

Please sign in to comment.