Skip to content

Commit

Permalink
fix: Make the length limitation configurable on bech32
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d committed Sep 10, 2021
1 parent 03062cf commit 4f289b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 8 additions & 4 deletions types/bech32/bech32.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"strings"
)

// LengthLimitBIP173 is the maximum length of the bech32-encoded address
// defined by BIP-173.
const LengthLimitBIP173 = 90

// charset is the set of characters used in the data section of bech32 strings.
// Note that this is ordered, such that for a given charset[i], i is the binary
// value of the character.
Expand Down Expand Up @@ -239,9 +243,9 @@ func DecodeNoLimit(bech string) (string, []byte, error) {
//
// Note that the returned data is 5-bit (base32) encoded and the human-readable
// part will be lowercase.
func Decode(bech string) (string, []byte, error) {
// The maximum allowed length for a bech32 string is 90.
if len(bech) > 90 {
func Decode(bech string, limit int) (string, []byte, error) {
// The length of the given string should not exceed the limit.
if len(bech) > limit {
return "", nil, ErrInvalidLength(len(bech))
}

Expand Down Expand Up @@ -368,7 +372,7 @@ func EncodeFromBase256(hrp string, data []byte) (string, error) {
// human-readable part (HRP) and base32-encoded data, converts that data to a
// base256-encoded byte slice and returns it along with the lowercase HRP.
func DecodeToBase256(bech string) (string, []byte, error) {
hrp, data, err := Decode(bech)
hrp, data, err := Decode(bech, LengthLimitBIP173)
if err != nil {
return "", nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions types/bech32/bech32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestBech32(t *testing.T) {

for i, test := range tests {
str := test.str
hrp, decoded, err := Decode(str)
hrp, decoded, err := Decode(str, LengthLimitBIP173)
if test.expectedError != err {
t.Errorf("%d: expected decoding error %v "+
"instead got %v", i, test.expectedError, err)
Expand All @@ -81,7 +81,7 @@ func TestBech32(t *testing.T) {
// Flip a bit in the string an make sure it is caught.
pos := strings.LastIndexAny(str, "1")
flipped := str[:pos+1] + string((str[pos+1] ^ 1)) + str[pos+2:]
_, _, err = Decode(flipped)
_, _, err = Decode(flipped, LengthLimitBIP173)
if err == nil {
t.Error("expected decoding to fail")
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestMixedCaseEncode(t *testing.T) {

// Ensure the decoding the expected lowercase encoding converted to all
// uppercase produces the lowercase HRP and original data.
gotHRP, gotData, err := Decode(strings.ToUpper(test.encoded))
gotHRP, gotData, err := Decode(strings.ToUpper(test.encoded), LengthLimitBIP173)
if err != nil {
t.Errorf("%q: unexpected decode error: %v", test.name, err)
continue
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestCanDecodeUnlimtedBech32(t *testing.T) {
input := "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq5kx0yd"

// Sanity check that an input of this length errors on regular Decode()
_, _, err := Decode(input)
_, _, err := Decode(input, LengthLimitBIP173)
if err == nil {
t.Fatalf("Test vector not appropriate")
}
Expand Down Expand Up @@ -403,7 +403,7 @@ func BenchmarkEncodeDecodeCycle(b *testing.B) {
b.Fatalf("failed to encode input: %v", err)
}

_, _, err = Decode(str)
_, _, err = Decode(str, LengthLimitBIP173)
if err != nil {
b.Fatalf("failed to decode string: %v", err)
}
Expand Down

0 comments on commit 4f289b2

Please sign in to comment.