File tree Expand file tree Collapse file tree 2 files changed +8
-2
lines changed Expand file tree Collapse file tree 2 files changed +8
-2
lines changed Original file line number Diff line number Diff line change 4
4
5
5
#include < bech32.h>
6
6
7
+ #include < assert.h>
8
+
7
9
namespace
8
10
{
9
11
@@ -58,7 +60,7 @@ uint32_t PolyMod(const data& v)
58
60
59
61
// During the course of the loop below, `c` contains the bitpacked coefficients of the
60
62
// polynomial constructed from just the values of v that were processed so far, mod g(x). In
61
- // the above example, `c` initially corresponds to 1 mod (x), and after processing 2 inputs of
63
+ // the above example, `c` initially corresponds to 1 mod g (x), and after processing 2 inputs of
62
64
// v, it corresponds to x^2 + v0*x + v1 mod g(x). As 1 mod g(x) = 1, that is the starting value
63
65
// for `c`.
64
66
uint32_t c = 1 ;
@@ -145,6 +147,10 @@ namespace bech32
145
147
146
148
/* * Encode a Bech32 string. */
147
149
std::string Encode (const std::string& hrp, const data& values) {
150
+ // First ensure that the HRP is all lowercase. BIP-173 requires an encoder
151
+ // to return a lowercase Bech32 string, but if given an uppercase HRP, the
152
+ // result will always be invalid.
153
+ for (const char & c : hrp) assert (c < ' A' || c > ' Z' );
148
154
data checksum = CreateChecksum (hrp, values);
149
155
data combined = Cat (values, checksum);
150
156
std::string ret = hrp + ' 1' ;
Original file line number Diff line number Diff line change 19
19
namespace bech32
20
20
{
21
21
22
- /* * Encode a Bech32 string. Returns the empty string in case of failure . */
22
+ /* * Encode a Bech32 string. If hrp contains uppercase characters, this will cause an assertion error . */
23
23
std::string Encode (const std::string& hrp, const std::vector<uint8_t >& values);
24
24
25
25
/* * Decode a Bech32 string. Returns (hrp, data). Empty hrp means failure. */
You can’t perform that action at this time.
0 commit comments