MCPcopy Create free account
hub / github.com/ElementsProject/elements / Decode

Function Decode

src/bech32.cpp:368–392  ·  view source on GitHub ↗

Decode a Bech32 or Bech32m string. */

Source from the content-addressed store, hash-verified

366
367/** Decode a Bech32 or Bech32m string. */
368DecodeResult Decode(const std::string& str) {
369 std::vector<int> errors;
370 if (!CheckCharacters(str, errors)) return {};
371 size_t pos = str.rfind('1');
372 if (str.size() > 90 || pos == str.npos || pos == 0 || pos + 7 > str.size()) {
373 return {};
374 }
375 data values(str.size() - 1 - pos);
376 for (size_t i = 0; i < str.size() - 1 - pos; ++i) {
377 unsigned char c = str[i + pos + 1];
378 int8_t rev = CHARSET_REV[c];
379
380 if (rev == -1) {
381 return {};
382 }
383 values[i] = rev;
384 }
385 std::string hrp;
386 for (size_t i = 0; i < pos; ++i) {
387 hrp += LowerCase(str[i]);
388 }
389 Encoding result = VerifyChecksum(hrp, values);
390 if (result == Encoding::INVALID) return {};
391 return {result, std::move(hrp), data(values.begin(), values.end() - 6)};
392}
393
394/** Find index of an incorrect character in a Bech32 string. */
395std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str) {

Callers 7

DecodeWithVersionMethod · 0.70
DecodeDestinationFunction · 0.70
BOOST_AUTO_TEST_CASEFunction · 0.50
BOOST_AUTO_TEST_CASEFunction · 0.50
FUZZ_TARGETFunction · 0.50
DecodeFPMethod · 0.50
Bech32DecodeFunction · 0.50

Calls 7

CheckCharactersFunction · 0.85
LowerCaseFunction · 0.70
VerifyChecksumFunction · 0.70
dataFunction · 0.70
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by 3

BOOST_AUTO_TEST_CASEFunction · 0.40
BOOST_AUTO_TEST_CASEFunction · 0.40
FUZZ_TARGETFunction · 0.40