Skip to content

Commit 9597e65

Browse files
committed
fix(address): normalize mixed-case Bech32 address before decoding
Signed-off-by: Ales Verbic <[email protected]>
1 parent c442124 commit 9597e65

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

ledger/common/address.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package common
1616

1717
import (
1818
"fmt"
19+
"strings"
1920

2021
"github.com/blinklabs-io/gouroboros/base58"
2122
"github.com/blinklabs-io/gouroboros/bech32"
@@ -55,6 +56,7 @@ type Address struct {
5556

5657
// NewAddress returns an Address based on the provided bech32 address string
5758
func NewAddress(addr string) (Address, error) {
59+
addr = strings.ToLower(addr)
5860
_, data, err := bech32.DecodeNoLimit(addr)
5961
if err != nil {
6062
return Address{}, err

ledger/common/address_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/blinklabs-io/gouroboros/internal/test"
21+
"github.com/stretchr/testify/assert"
2122
)
2223

2324
func TestAddressFromBytes(t *testing.T) {
@@ -225,3 +226,16 @@ func TestAddressStakeAddress(t *testing.T) {
225226
}
226227
}
227228
}
229+
230+
func TestAddressPaymentAddress_MixedCase(t *testing.T) {
231+
232+
lowerCaseAddress := "addr_test1qqawz5hm2tchtmarkfn2tamzvd2spatl89gtutgra6zwc3ktqj7p944ckc9lq7u36jrq99znwhzlq6jfv2j4ql92m4rq07hp8t"
233+
laddr, err := NewAddress(lowerCaseAddress)
234+
assert.Nil(t, err, "Expected no error when decoding a lowercase address")
235+
// address with mixed case
236+
mixedCaseAddress := "addr_test1QQawz5hm2tchtmarkfn2tamzvd2spatl89gtutgra6zwc3ktqj7p944ckc9lq7u36jrq99znwhzlq6jfv2j4ql92m4rq07hp8t"
237+
addr, err := NewAddress(mixedCaseAddress)
238+
assert.Nil(t, err, "Expected no error when decoding a mixed-case address")
239+
assert.NotNil(t, addr, "Expected a valid address object after decoding")
240+
assert.Equal(t, laddr.String(), addr.String(), "Expected the mixed-case address to be equal to lowercase")
241+
}

0 commit comments

Comments
 (0)