From cd1aba7703763a823101b81b9c999419eea2746c Mon Sep 17 00:00:00 2001 From: Ales Verbic Date: Mon, 14 Oct 2024 13:26:30 -0400 Subject: [PATCH] fix(address): normalize mixed-case Bech32 address before decoding Signed-off-by: Ales Verbic --- ledger/common/address.go | 2 ++ ledger/common/address_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/ledger/common/address.go b/ledger/common/address.go index a6d9439a..e85470b0 100644 --- a/ledger/common/address.go +++ b/ledger/common/address.go @@ -16,6 +16,7 @@ package common import ( "fmt" + "strings" "github.com/blinklabs-io/gouroboros/base58" "github.com/blinklabs-io/gouroboros/bech32" @@ -55,6 +56,7 @@ type Address struct { // NewAddress returns an Address based on the provided bech32 address string func NewAddress(addr string) (Address, error) { + addr = strings.ToLower(addr) _, data, err := bech32.DecodeNoLimit(addr) if err != nil { return Address{}, err diff --git a/ledger/common/address_test.go b/ledger/common/address_test.go index 99649038..7c921482 100644 --- a/ledger/common/address_test.go +++ b/ledger/common/address_test.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/blinklabs-io/gouroboros/internal/test" + "github.com/stretchr/testify/assert" ) func TestAddressFromBytes(t *testing.T) { @@ -225,3 +226,13 @@ func TestAddressStakeAddress(t *testing.T) { } } } + +func TestAddressPaymentAddress_MixedCase(t *testing.T) { + + // address: "addr_test1qqawz5hm2tchtmarkfn2tamzvd2spatl89gtutgra6zwc3ktqj7p944ckc9lq7u36jrq99znwhzlq6jfv2j4ql92m4rq07hp8t", + // address with mixed case + mixedCaseAddress := "addr_test1QQawz5hm2tchtmarkfn2tamzvd2spatl89gtutgra6zwc3ktqj7p944ckc9lq7u36jrq99znwhzlq6jfv2j4ql92m4rq07hp8t" + addr, err := NewAddress(mixedCaseAddress) + assert.Nil(t, err, "Expected no error when decoding a mixed-case address") + assert.NotNil(t, addr, "Expected a valid address object after decoding") +}