/ecc/javascript/segwit_addr_ecc.js

https://github.com/sipa/bech32 · JavaScript · 117 lines · 94 code · 4 blank · 19 comment · 54 complexity · 7f0ea99dc2fc9b342cef4e1298ddad1d MD5 · raw file

  1. // Copyright (c) 2017 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_ecc = require('./bech32_ecc');
  21. module.exports = {
  22. check: check
  23. };
  24. function convertbits (data, frombits, tobits, pad) {
  25. var acc = 0;
  26. var bits = 0;
  27. var ret = [];
  28. var maxv = (1 << tobits) - 1;
  29. for (var p = 0; p < data.length; ++p) {
  30. var value = data[p];
  31. if (value < 0 || (value >> frombits) !== 0) {
  32. return null;
  33. }
  34. acc = (acc << frombits) | value;
  35. bits += frombits;
  36. while (bits >= tobits) {
  37. bits -= tobits;
  38. ret.push((acc >> bits) & maxv);
  39. }
  40. }
  41. if (pad) {
  42. if (bits > 0) {
  43. ret.push((acc << (tobits - bits)) & maxv);
  44. }
  45. } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
  46. return null;
  47. }
  48. return ret;
  49. }
  50. function check (addr, validHrp) {
  51. if (addr.length < 14) {
  52. return {error:"Too short", pos:null};
  53. }
  54. if (addr.length > 74) {
  55. return {error:"Too long", pos:null};
  56. }
  57. eposs = []
  58. for (var encname in bech32_ecc.encodings) {
  59. const encoding = bech32_ecc.encodings[encname];
  60. var dec = bech32_ecc.check(addr, validHrp, encoding);
  61. if ("data_pattern" in dec) {
  62. if (dec.data_pattern !== null) {
  63. const numbytes = ((dec.data_pattern.length - 6 - 1) * 5) >> 3;
  64. if (dec.data_pattern.length - 6 - 1 != ((numbytes * 8 + 4) / 5|0)) continue;
  65. if (dec.data_pattern[0] == 0 && encoding != bech32_ecc.encodings.BECH32) continue;
  66. if (dec.data_pattern[0] > 0 && dec.data_pattern[0] <= 16 && encoding != bech32_ecc.encodings.BECH32M) continue;
  67. if (dec.data_pattern[0] > 16) continue;
  68. if (dec.data_pattern[0] == 0 && numbytes != 20 && numbytes != 32) continue;
  69. var epos = [];
  70. for (var i = 0; i < dec.data_pattern.length; ++i) {
  71. if (dec.data_pattern[i] == -1) {
  72. epos.push(addr.length - dec.data_pattern.length + i);
  73. }
  74. }
  75. eposs.push(epos);
  76. }
  77. continue;
  78. }
  79. if (dec.error !== null) {
  80. return {error:dec.error, pos:dec.pos};
  81. }
  82. var res = convertbits(dec.data.slice(1), 5, 8, false);
  83. if (res === null) {
  84. return {error:"Padding error", pos:[addr.length - 6]};
  85. }
  86. if (res.length < 2 || res.length > 40) {
  87. return {error:"Invalid witness program length", pos:null};
  88. }
  89. if (dec.data[0] > 16) {
  90. return {error:"Invalid witness version", pos:[dec.hrp.length + 1]};
  91. }
  92. if (dec.data[0] == 0 && encoding != bech32_ecc.encodings.BECH32) {
  93. return {error:"Bech32 must be used for witness v0 programs", pos:null};
  94. }
  95. if (dec.data[0] != 0 && encoding != bech32_ecc.encodings.BECH32M) {
  96. return {error:"Bech32m must be used for witness v1+ programs", pos:null};
  97. }
  98. if (dec.data[0] === 0 && res.length !== 20 && res.length !== 32) {
  99. return {error:"Invalid witness program length for v0", pos:null};
  100. }
  101. return {error:null, version:dec.data[0], program:res};
  102. }
  103. if (eposs.length == 1) {
  104. return {error:"Checksum error", pos:eposs[0]};
  105. }
  106. if (eposs.length > 1) {
  107. eposs.sort(function (a,b) { return a.length - b.length; });
  108. if (eposs[0].length < eposs[1].length) {
  109. return {error:"Checksum error", pos:eposs[0]};
  110. }
  111. }
  112. return {error:"Checksum error", pos:null};
  113. }