/ref/c++/segwit_addr.cpp

https://github.com/sipa/bech32 · C++ · 86 lines · 53 code · 10 blank · 23 comment · 27 complexity · 4ea8e5c1fc5804c12e3882c3fa23ca9c MD5 · raw file

  1. /* Copyright (c) 2017, 2021 Pieter Wuille
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #include "segwit_addr.h"
  22. #include "bech32.h"
  23. #include <tuple>
  24. namespace
  25. {
  26. typedef std::vector<uint8_t> data;
  27. /** Convert from one power-of-2 number base to another. */
  28. template<int frombits, int tobits, bool pad>
  29. bool convertbits(data& out, const data& in) {
  30. int acc = 0;
  31. int bits = 0;
  32. const int maxv = (1 << tobits) - 1;
  33. const int max_acc = (1 << (frombits + tobits - 1)) - 1;
  34. for (size_t i = 0; i < in.size(); ++i) {
  35. int value = in[i];
  36. acc = ((acc << frombits) | value) & max_acc;
  37. bits += frombits;
  38. while (bits >= tobits) {
  39. bits -= tobits;
  40. out.push_back((acc >> bits) & maxv);
  41. }
  42. }
  43. if (pad) {
  44. if (bits) out.push_back((acc << (tobits - bits)) & maxv);
  45. } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. }
  51. namespace segwit_addr
  52. {
  53. /** Decode a SegWit address. */
  54. std::pair<int, data> decode(const std::string& hrp, const std::string& addr) {
  55. const auto dec = bech32::decode(addr);
  56. if (dec.hrp != hrp || dec.data.size() < 1) return std::make_pair(-1, data());
  57. data conv;
  58. uint8_t witver = dec.data[0];
  59. if (!convertbits<5, 8, false>(conv, data(dec.data.begin() + 1, dec.data.end())) ||
  60. conv.size() < 2 || conv.size() > 40 || witver > 16 || (witver == 0 &&
  61. conv.size() != 20 && conv.size() != 32) || (witver == 0 && dec.encoding !=
  62. bech32::Encoding::BECH32) || (witver != 0 && dec.encoding != bech32::Encoding::BECH32M)) {
  63. return std::make_pair(-1, data());
  64. }
  65. return std::make_pair(dec.data[0], conv);
  66. }
  67. /** Encode a SegWit address. */
  68. std::string encode(const std::string& hrp, int witver, const data& witprog) {
  69. data enc;
  70. enc.push_back(witver);
  71. convertbits<8, 5, true>(enc, witprog);
  72. std::string ret = bech32::encode(hrp, enc, witver > 0 ? bech32::Encoding::BECH32M : bech32::Encoding::BECH32);
  73. if (decode(hrp, ret).first == -1) return "";
  74. return ret;
  75. }
  76. }