/ref/javascript/segwit_addr.js

https://github.com/sipa/bech32 · JavaScript · 91 lines · 67 code · 5 blank · 19 comment · 26 complexity · e09c7fe19dbdcad2ab3848fd045291d5 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. var bech32 = require('./bech32');
  21. module.exports = {
  22. encode: encode,
  23. decode: decode
  24. };
  25. function convertbits (data, frombits, tobits, pad) {
  26. var acc = 0;
  27. var bits = 0;
  28. var ret = [];
  29. var maxv = (1 << tobits) - 1;
  30. for (var p = 0; p < data.length; ++p) {
  31. var value = data[p];
  32. if (value < 0 || (value >> frombits) !== 0) {
  33. return null;
  34. }
  35. acc = (acc << frombits) | value;
  36. bits += frombits;
  37. while (bits >= tobits) {
  38. bits -= tobits;
  39. ret.push((acc >> bits) & maxv);
  40. }
  41. }
  42. if (pad) {
  43. if (bits > 0) {
  44. ret.push((acc << (tobits - bits)) & maxv);
  45. }
  46. } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
  47. return null;
  48. }
  49. return ret;
  50. }
  51. function decode (hrp, addr) {
  52. var bech32m = false;
  53. var dec = bech32.decode(addr, bech32.encodings.BECH32);
  54. if (dec === null) {
  55. dec = bech32.decode(addr, bech32.encodings.BECH32M);
  56. bech32m = true;
  57. }
  58. if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) {
  59. return null;
  60. }
  61. var res = convertbits(dec.data.slice(1), 5, 8, false);
  62. if (res === null || res.length < 2 || res.length > 40) {
  63. return null;
  64. }
  65. if (dec.data[0] === 0 && res.length !== 20 && res.length !== 32) {
  66. return null;
  67. }
  68. if (dec.data[0] === 0 && bech32m) {
  69. return null;
  70. }
  71. if (dec.data[0] !== 0 && !bech32m) {
  72. return null;
  73. }
  74. return {version: dec.data[0], program: res};
  75. }
  76. function encode (hrp, version, program) {
  77. var enc = bech32.encodings.BECH32;
  78. if (version > 0) {
  79. enc = bech32.encodings.BECH32M;
  80. }
  81. var ret = bech32.encode(hrp, [version].concat(convertbits(program, 8, 5, true)), enc);
  82. if (decode(hrp, ret, enc) === null) {
  83. return null;
  84. }
  85. return ret;
  86. }