From ccf694c02c2ea044bccad32b028238c89e660c72 Mon Sep 17 00:00:00 2001 From: marcoezekiel Date: Fri, 10 May 2024 09:35:55 -0600 Subject: [PATCH] Rename decodeNoLimit to DecodeNoLimitWithVersion This exposes publicly the ability to decode arbitrary-length bech32 strings and return the bech32 version that was used in the encoding. It provides the underlying functionality for both DecodeNoLimit and DecodeGeneric. --- btcutil/bech32/bech32.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/btcutil/bech32/bech32.go b/btcutil/bech32/bech32.go index 250b6042e8..92994b2881 100644 --- a/btcutil/bech32/bech32.go +++ b/btcutil/bech32/bech32.go @@ -165,11 +165,14 @@ func bech32VerifyChecksum(hrp string, data []byte) (Version, bool) { return VersionUnknown, false } -// DecodeNoLimit is a bech32 checksum version aware arbitrary string length -// decoder. This function will return the version of the decoded checksum -// constant so higher level validation can be performed to ensure the correct -// version of bech32 was used when encoding. -func decodeNoLimit(bech string) (string, []byte, Version, error) { +// DecodeNoLimitWithVersion is a bech32 checksum version aware arbitrary string +// length decoder. This function will return the version of the decoded +// checksum constant so higher level validation can be performed to ensure the +// correct version of bech32 was used when encoding. +// +// Note that the returned data is 5-bit (base32) encoded and the human-readable +// part will be lowercase. +func DecodeNoLimitWithVersion(bech string) (string, []byte, Version, error) { // The minimum allowed size of a bech32 string is 8 characters, since it // needs a non-empty HRP, a separator, and a 6 character checksum. if len(bech) < 8 { @@ -262,19 +265,10 @@ func decodeNoLimit(bech string) (string, []byte, Version, error) { // Note that the returned data is 5-bit (base32) encoded and the human-readable // part will be lowercase. func DecodeNoLimit(bech string) (string, []byte, error) { - hrp, data, _, err := decodeNoLimit(bech) + hrp, data, _, err := DecodeNoLimitWithVersion(bech) return hrp, data, err } -// DecodeGeneric is identical to the existing DecodeNoLimit method, but will also -// return the bech32 version that matches the decoded checksum. -// -// Note that the returned data is 5-bit (base32) encoded and the human-readable -// part will be lowercase. -func DecodeNoLimitGeneric(bech string) (string, []byte, Version, error) { - return decodeNoLimit(bech) -} - // Decode decodes a bech32 encoded string, returning the human-readable part and // the data part excluding the checksum. // @@ -286,7 +280,7 @@ func Decode(bech string) (string, []byte, error) { return "", nil, ErrInvalidLength(len(bech)) } - hrp, data, _, err := decodeNoLimit(bech) + hrp, data, _, err := DecodeNoLimitWithVersion(bech) return hrp, data, err } @@ -300,7 +294,7 @@ func DecodeGeneric(bech string) (string, []byte, Version, error) { return "", nil, VersionUnknown, ErrInvalidLength(len(bech)) } - return decodeNoLimit(bech) + return DecodeNoLimitWithVersion(bech) } // encodeGeneric is the base bech32 encoding function that is aware of the