MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / Decode

Function Decode

src/bech32.cpp:159–190  ·  view source on GitHub ↗

Decode a Bech32 string. */

Source from the content-addressed store, hash-verified

157
158/** Decode a Bech32 string. */
159std::pair<std::string, data> Decode(const std::string& str) {
160 bool lower = false, upper = false;
161 for (size_t i = 0; i < str.size(); ++i) {
162 unsigned char c = str[i];
163 if (c >= 'a' && c <= 'z') lower = true;
164 else if (c >= 'A' && c <= 'Z') upper = true;
165 else if (c < 33 || c > 126) return {};
166 }
167 if (lower && upper) return {};
168 size_t pos = str.rfind('1');
169 if (str.size() > 90 || pos == str.npos || pos == 0 || pos + 7 > str.size()) {
170 return {};
171 }
172 data values(str.size() - 1 - pos);
173 for (size_t i = 0; i < str.size() - 1 - pos; ++i) {
174 unsigned char c = str[i + pos + 1];
175 int8_t rev = CHARSET_REV[c];
176
177 if (rev == -1) {
178 return {};
179 }
180 values[i] = rev;
181 }
182 std::string hrp;
183 for (size_t i = 0; i < pos; ++i) {
184 hrp += LowerCase(str[i]);
185 }
186 if (!VerifyChecksum(hrp, values)) {
187 return {};
188 }
189 return {hrp, data(values.begin(), values.end() - 6)};
190}
191
192} // namespace bech32

Callers 5

UnserializeMethod · 0.85
DecodeDestinationFunction · 0.85
UnserializeMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
Bech32DecodeFunction · 0.85

Calls 5

LowerCaseFunction · 0.85
VerifyChecksumFunction · 0.85
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68