Skip to content

Commit

Permalink
fix(cosmos): return an error if version is unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 11, 2024
1 parent 65e665c commit 88eb9bc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
14 changes: 11 additions & 3 deletions golang/cosmos/types/address_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (

// AddressHookMagic is a magic byte prefix that identifies a hooked address.
// Chosen to make bech32 address hooks that look like "agoric10rch..."
var AddressHookMagic = []byte{0x78, 0xf1, 0x70 | AddressHookVersion}
var AddressHookMagic = []byte{0x78, 0xf1, 0x70 /* | AddressHookVersion */}

func init() {
if AddressHookVersion&0x0f != AddressHookVersion {
Expand All @@ -51,12 +51,19 @@ func SplitHookedAddress(addr string) (string, []byte, error) {
return "", []byte{}, err
}

bz := bytes.TrimPrefix(payload, AddressHookMagic)
if len(bz) == len(payload) {
lastMagicHighNibble := AddressHookMagic[len(AddressHookMagic)-1]
bz := bytes.TrimPrefix(payload, AddressHookMagic[:len(AddressHookMagic)-1])
if len(bz) == len(payload) || len(bz) == 0 || bz[0]&0xf0 != lastMagicHighNibble {
// Return an unhooked address.
return addr, []byte{}, nil
}

version := bz[0] & 0x0f
bz = bz[1:]
if version != AddressHookVersion {
return "", []byte{}, fmt.Errorf("unsupported address hook version %d", version)
}

if len(bz) < BaseAddressLengthBytes {
return "", []byte{}, fmt.Errorf("hooked address must have at least %d bytes", BaseAddressLengthBytes)
}
Expand Down Expand Up @@ -99,6 +106,7 @@ func JoinHookedAddress(baseAddr string, hookData []byte) (string, error) {

payload := make([]byte, 0, len(AddressHookMagic)+b+len(hookData)+BaseAddressLengthBytes)
payload = append(payload, AddressHookMagic...)
payload[len(payload)-1] |= byte(AddressHookVersion)
payload = append(payload, bz...)
payload = append(payload, hookData...)
baLen := make([]byte, BaseAddressLengthBytes)
Expand Down
47 changes: 47 additions & 0 deletions golang/cosmos/types/address_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,53 @@ import (
"github.com/Agoric/agoric-sdk/golang/cosmos/types"
)

func TestSplitHookedAddress(t *testing.T) {
cases := []struct {
name string
hook string
baseAddr string
hookData []byte
err string
}{
{"empty", "", "", []byte{}, "decoding bech32 failed: invalid bech32 string length 0"},
{"no hook", "agoric1qqp0e5ys", "agoric1qqp0e5ys", []byte{}, ""},
{"Fast USDC", "agoric10rchp4vc53apxn32q42c3zryml8xq3xshyzuhjk6405wtxy7tl3d7e0f8az423padaek6me38qekget2vdhx66mtvy6kg7nrw5uhsaekd4uhwufswqex6dtsv44hxv3cd4jkuqpqvduyhf",
"agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek",
[]byte("?EUD=osmo183dejcnmkka5dzcu9xw6mywq0p2m5peks28men"),
""},
{"version 0",
"agoric10rchqqqpqgpsgpgxquyqjzstpsxsurcszyfpxpqrqgqsq9qx0p9wp",
"agoric1qqqsyqcyq5rqwzqfpg9scrgwpugpzysn3tn9p0",
[]byte{4, 3, 2, 1},
""},
{"version 1 reject",
"agoric10rchzqqpqgpsgpgxquyqjzstpsxsurcszyfpxpqrqgqsq9q04n2fg",
"",
[]byte{},
"unsupported address hook version 1"},
{"version 15 reject",
"agoric10rch7qqpqgpsgpgxquyqjzstpsxsurcszyfpxpqrqgqsq9q25ez2d",
"",
[]byte{},
"unsupported address hook version 15"},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
baseAddr, hookData, err := types.SplitHookedAddress(tc.hook)
if len(tc.err) > 0 {
require.Error(t, err)
require.Equal(t, tc.err, err.Error())
} else {
require.NoError(t, err)
require.Equal(t, tc.baseAddr, baseAddr)
require.Equal(t, string(tc.hookData), string(hookData))
}
})
}
}

func TestExtractBaseAddress(t *testing.T) {
bases := []struct {
name string
Expand Down

0 comments on commit 88eb9bc

Please sign in to comment.