|
| 1 | +package bech32 |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | +) |
| 6 | + |
| 7 | +// MaxLengthBIP173 is the maximum length of bech32-encoded address defined by |
| 8 | +// BIP-173. |
| 9 | +const MaxLengthBIP173 = 90 |
| 10 | + |
| 11 | +// VerifyChecksum verifies whether the bech32 string specified by the |
| 12 | +// provided hrp and payload data (encoded as 5 bits per element byte slice) has |
| 13 | +// the correct checksum suffix. The version of bech32 used (bech32 OG, or |
| 14 | +// bech32m) is also returned to allow the caller to perform proper address |
| 15 | +// validation (segwitv0 should use bech32, v1+ should use bech32m). |
| 16 | +// |
| 17 | +// For more details on the checksum verification, please refer to BIP 173. |
| 18 | +func VerifyChecksum(hrp string, data []byte, checksum []byte) (Version, bool) { |
| 19 | + polymod := bech32Polymod(hrp, data, checksum) |
| 20 | + |
| 21 | + // Before BIP-350, we'd always check this against a static constant of |
| 22 | + // 1 to know if the checksum was computed properly. As we want to |
| 23 | + // generically support decoding for bech32m as well as bech32, we'll |
| 24 | + // look up the returned value and compare it to the set of defined |
| 25 | + // constants. |
| 26 | + bech32Version, ok := ConstsToVersion[ChecksumConst(polymod)] |
| 27 | + if ok { |
| 28 | + return bech32Version, true |
| 29 | + } |
| 30 | + |
| 31 | + return VersionUnknown, false |
| 32 | +} |
| 33 | + |
| 34 | +// Normalize converts the uppercase letters to lowercase in string, because |
| 35 | +// Bech32 standard uses only the lowercase for of string for checksum calculation. |
| 36 | +// If conversion occurs during function call, `true` will be returned. |
| 37 | +// |
| 38 | +// Mixed case is NOT allowed. |
| 39 | +func Normalize(bech *string) (bool, error) { |
| 40 | + // OnlyASCII characters between 33 and 126 are allowed. |
| 41 | + var hasLower, hasUpper bool |
| 42 | + for i := 0; i < len(*bech); i++ { |
| 43 | + if (*bech)[i] < 33 || (*bech)[i] > 126 { |
| 44 | + return false, ErrInvalidCharacter((*bech)[i]) |
| 45 | + } |
| 46 | + |
| 47 | + // The characters must be either all lowercase or all uppercase. Testing |
| 48 | + // directly with ascii codes is safe here, given the previous test. |
| 49 | + hasLower = hasLower || ((*bech)[i] >= 97 && (*bech)[i] <= 122) |
| 50 | + hasUpper = hasUpper || ((*bech)[i] >= 65 && (*bech)[i] <= 90) |
| 51 | + if hasLower && hasUpper { |
| 52 | + return false, ErrMixedCase{} |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Bech32 standard uses only the lowercase for of strings for checksum |
| 57 | + // calculation. |
| 58 | + if hasUpper { |
| 59 | + *bech = strings.ToLower(*bech) |
| 60 | + return true, nil |
| 61 | + } |
| 62 | + |
| 63 | + return false, nil |
| 64 | +} |
| 65 | + |
| 66 | +// DecodeUnsafe decodes a bech32 encoded string, returning the human-readable |
| 67 | +// part, the data part (excluding the checksum) and the checksum. This function |
| 68 | +// does NOT validate against the BIP-173 maximum length allowed for bech32 strings |
| 69 | +// and is meant for use in custom applications (such as lightning network payment |
| 70 | +// requests), NOT on-chain addresses. This function assumes the given string |
| 71 | +// includes lowercase letters only, so if not, you should call Normalize first. |
| 72 | +// |
| 73 | +// Note that the returned data is 5-bit (base32) encoded and the human-readable |
| 74 | +// part will be lowercase. |
| 75 | +func DecodeUnsafe(bech string) (string, []byte, []byte, error) { |
| 76 | + // The string is invalid if the last '1' is non-existent, it is the |
| 77 | + // first character of the string (no human-readable part) or one of the |
| 78 | + // last 6 characters of the string (since checksum cannot contain '1'). |
| 79 | + one := strings.LastIndexByte(bech, '1') |
| 80 | + if one < 1 || one+7 > len(bech) { |
| 81 | + return "", nil, nil, ErrInvalidSeparatorIndex(one) |
| 82 | + } |
| 83 | + |
| 84 | + // The human-readable part is everything before the last '1'. |
| 85 | + hrp := bech[:one] |
| 86 | + data := bech[one+1:] |
| 87 | + |
| 88 | + // Each character corresponds to the byte with value of the index in |
| 89 | + // 'charset'. |
| 90 | + decoded, err := toBytes(data) |
| 91 | + if err != nil { |
| 92 | + return "", nil, nil, err |
| 93 | + } |
| 94 | + |
| 95 | + return hrp, decoded[:len(decoded)-6], decoded[len(decoded)-6:], nil |
| 96 | +} |
| 97 | + |
| 98 | +// decodeWithLimit is a bech32 checksum version aware bounded string length |
| 99 | +// decoder. This function will return the version of the decoded checksum |
| 100 | +// constant so higher level validation can be performed to ensure the correct |
| 101 | +// version of bech32 was used when encoding. |
| 102 | +func decodeWithLimit(bech string, limit int) (string, []byte, Version, error) { |
| 103 | + // The length of the string should not exceed the given limit. |
| 104 | + if len(bech) < 8 || len(bech) > limit { |
| 105 | + return "", nil, VersionUnknown, ErrInvalidLength(len(bech)) |
| 106 | + } |
| 107 | + |
| 108 | + _, err := Normalize(&bech) |
| 109 | + if err != nil { |
| 110 | + return "", nil, VersionUnknown, err |
| 111 | + } |
| 112 | + |
| 113 | + hrp, data, checksum, err := DecodeUnsafe(bech) |
| 114 | + if err != nil { |
| 115 | + return "", nil, VersionUnknown, err |
| 116 | + } |
| 117 | + |
| 118 | + // Verify if the checksum (stored inside decoded[:]) is valid, given the |
| 119 | + // previously decoded hrp. |
| 120 | + bech32Version, ok := VerifyChecksum(hrp, data, checksum) |
| 121 | + if !ok { |
| 122 | + // Invalid checksum. Calculate what it should have been, so that the |
| 123 | + // error contains this information. |
| 124 | + |
| 125 | + // Extract the payload bytes and actual checksum in the string. |
| 126 | + actual := bech[len(bech)-6:] |
| 127 | + |
| 128 | + // Calculate the expected checksum, given the hrp and payload |
| 129 | + // data. We'll actually compute _both_ possibly valid checksum |
| 130 | + // to further aide in debugging. |
| 131 | + var expectedBldr strings.Builder |
| 132 | + expectedBldr.Grow(6) |
| 133 | + writeBech32Checksum(hrp, data, &expectedBldr, Version0) |
| 134 | + expectedVersion0 := expectedBldr.String() |
| 135 | + |
| 136 | + var b strings.Builder |
| 137 | + b.Grow(6) |
| 138 | + writeBech32Checksum(hrp, data, &expectedBldr, VersionM) |
| 139 | + expectedVersionM := expectedBldr.String() |
| 140 | + |
| 141 | + err = ErrInvalidChecksum{ |
| 142 | + Expected: expectedVersion0, |
| 143 | + ExpectedM: expectedVersionM, |
| 144 | + Actual: actual, |
| 145 | + } |
| 146 | + return "", nil, VersionUnknown, err |
| 147 | + } |
| 148 | + |
| 149 | + // We exclude the last 6 bytes, which is the checksum. |
| 150 | + return hrp, data, bech32Version, nil |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | +// DecodeWithLimit decodes a bech32 encoded string, returning the human-readable part and |
| 155 | +// the data part excluding the checksum. |
| 156 | +// |
| 157 | +// Note that the returned data is 5-bit (base32) encoded and the human-readable |
| 158 | +// part will be lowercase. |
| 159 | +func DecodeWithLimit(bech string, limit int) (string, []byte, error) { |
| 160 | + hrp, data, _, err := decodeWithLimit(bech, limit) |
| 161 | + return hrp, data, err |
| 162 | +} |
0 commit comments