xpi-ts 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/dist/cjs/lib/bitcore/address.js +75 -74
  2. package/dist/cjs/lib/bitcore/block/block.js +124 -126
  3. package/dist/cjs/lib/bitcore/block/blockheader.js +142 -208
  4. package/dist/cjs/lib/bitcore/block/index.js +3 -1
  5. package/dist/cjs/lib/bitcore/block/merkleblock.js +301 -0
  6. package/dist/cjs/lib/bitcore/crypto/bn.js +18 -22
  7. package/dist/cjs/lib/bitcore/crypto/ecdsa.js +50 -49
  8. package/dist/cjs/lib/bitcore/crypto/hash.js +25 -22
  9. package/dist/cjs/lib/bitcore/crypto/musig2.js +86 -92
  10. package/dist/cjs/lib/bitcore/crypto/point.js +42 -35
  11. package/dist/cjs/lib/bitcore/crypto/random.js +3 -2
  12. package/dist/cjs/lib/bitcore/crypto/schnorr.js +46 -55
  13. package/dist/cjs/lib/bitcore/crypto/signature.js +67 -55
  14. package/dist/cjs/lib/bitcore/crypto/sigtype.js +1 -1
  15. package/dist/cjs/lib/bitcore/encoding/base58.js +5 -4
  16. package/dist/cjs/lib/bitcore/encoding/base58check.js +14 -57
  17. package/dist/cjs/lib/bitcore/encoding/bufferreader.js +17 -17
  18. package/dist/cjs/lib/bitcore/encoding/bufferwriter.js +22 -26
  19. package/dist/cjs/lib/bitcore/encoding/varint.js +12 -11
  20. package/dist/cjs/lib/bitcore/errors.js +1 -0
  21. package/dist/cjs/lib/bitcore/hdprivatekey.js +74 -66
  22. package/dist/cjs/lib/bitcore/hdpublickey.js +96 -116
  23. package/dist/cjs/lib/bitcore/index.js +165 -172
  24. package/dist/cjs/lib/bitcore/message.js +36 -35
  25. package/dist/cjs/lib/bitcore/mnemonic/mnemonic.js +4 -3
  26. package/dist/cjs/lib/bitcore/mnemonic/pbkdf2.js +6 -5
  27. package/dist/cjs/lib/bitcore/mnemonic/words/french.js +2053 -0
  28. package/dist/cjs/lib/bitcore/mnemonic/words/index.js +2 -0
  29. package/dist/cjs/lib/bitcore/musig2/session.js +26 -25
  30. package/dist/cjs/lib/bitcore/musig2/signer.js +17 -16
  31. package/dist/cjs/lib/bitcore/networks.js +4 -3
  32. package/dist/cjs/lib/bitcore/opcode.js +11 -7
  33. package/dist/cjs/lib/bitcore/privatekey.js +42 -42
  34. package/dist/cjs/lib/bitcore/publickey.js +61 -57
  35. package/dist/cjs/lib/bitcore/script/interpreter/condition-stack.js +41 -0
  36. package/dist/cjs/lib/bitcore/script/interpreter/index.js +25 -0
  37. package/dist/cjs/lib/bitcore/script/interpreter/interpreter.js +1362 -0
  38. package/dist/cjs/lib/bitcore/script/interpreter/script-num.js +200 -0
  39. package/dist/cjs/lib/bitcore/script/interpreter/types.js +96 -0
  40. package/dist/cjs/lib/bitcore/{taproot.js → script/taproot.js} +107 -95
  41. package/dist/cjs/lib/bitcore/script.js +163 -104
  42. package/dist/cjs/lib/bitcore/taproot/musig2.js +51 -42
  43. package/dist/cjs/lib/bitcore/transaction/index.js +21 -21
  44. package/dist/cjs/lib/bitcore/transaction/input/multisig.js +10 -0
  45. package/dist/cjs/lib/bitcore/transaction/input/multisigscripthash.js +10 -0
  46. package/dist/cjs/lib/bitcore/transaction/input/publickey.js +10 -0
  47. package/dist/cjs/lib/bitcore/transaction/input/publickeyhash.js +10 -0
  48. package/dist/cjs/lib/bitcore/transaction/input/taproot.js +20 -0
  49. package/dist/cjs/lib/bitcore/transaction/input.js +145 -166
  50. package/dist/cjs/lib/bitcore/transaction/outpoint.js +89 -0
  51. package/dist/cjs/lib/bitcore/transaction/output.js +13 -8
  52. package/dist/cjs/lib/bitcore/transaction/sighash.js +96 -96
  53. package/dist/cjs/lib/bitcore/transaction/signature.js +39 -40
  54. package/dist/cjs/lib/bitcore/transaction/transaction.js +130 -118
  55. package/dist/cjs/lib/bitcore/transaction/unspentoutput.js +27 -23
  56. package/dist/cjs/lib/bitcore/util/bits.js +33 -0
  57. package/dist/cjs/lib/bitcore/util/buffer.js +29 -18
  58. package/dist/cjs/lib/bitcore/util/js.js +0 -3
  59. package/dist/cjs/lib/bitcore/util/merkle.js +53 -0
  60. package/dist/cjs/lib/bitcore/util/preconditions.js +8 -7
  61. package/dist/cjs/lib/bitcore/xaddress.js +27 -23
  62. package/dist/cjs/lib/{bitcore/taproot/nft.js → nft.js} +98 -29
  63. package/dist/cjs/lib/rank/index.js +28 -27
  64. package/dist/cjs/lib/rank/script.js +2 -1
  65. package/dist/cjs/lib/rank/transaction.js +2 -1
  66. package/dist/cjs/utils/string.js +6 -5
  67. package/dist/esm/lib/bitcore/address.js +42 -41
  68. package/dist/esm/lib/bitcore/block/block.js +126 -128
  69. package/dist/esm/lib/bitcore/block/blockheader.js +141 -207
  70. package/dist/esm/lib/bitcore/block/index.js +1 -0
  71. package/dist/esm/lib/bitcore/block/merkleblock.js +295 -0
  72. package/dist/esm/lib/bitcore/crypto/bn.js +18 -22
  73. package/dist/esm/lib/bitcore/crypto/ecdsa.js +25 -24
  74. package/dist/esm/lib/bitcore/crypto/hash.js +25 -22
  75. package/dist/esm/lib/bitcore/crypto/musig2.js +60 -66
  76. package/dist/esm/lib/bitcore/crypto/point.js +22 -15
  77. package/dist/esm/lib/bitcore/crypto/random.js +3 -2
  78. package/dist/esm/lib/bitcore/crypto/schnorr.js +35 -44
  79. package/dist/esm/lib/bitcore/crypto/signature.js +61 -49
  80. package/dist/esm/lib/bitcore/crypto/sigtype.js +1 -1
  81. package/dist/esm/lib/bitcore/encoding/base58.js +5 -4
  82. package/dist/esm/lib/bitcore/encoding/base58check.js +11 -57
  83. package/dist/esm/lib/bitcore/encoding/bufferreader.js +7 -7
  84. package/dist/esm/lib/bitcore/encoding/bufferwriter.js +18 -21
  85. package/dist/esm/lib/bitcore/encoding/varint.js +7 -6
  86. package/dist/esm/lib/bitcore/errors.js +1 -0
  87. package/dist/esm/lib/bitcore/hdprivatekey.js +47 -39
  88. package/dist/esm/lib/bitcore/hdpublickey.js +77 -97
  89. package/dist/esm/lib/bitcore/index.js +42 -45
  90. package/dist/esm/lib/bitcore/message.js +16 -15
  91. package/dist/esm/lib/bitcore/mnemonic/mnemonic.js +4 -3
  92. package/dist/esm/lib/bitcore/mnemonic/pbkdf2.js +5 -4
  93. package/dist/esm/lib/bitcore/mnemonic/words/french.js +2050 -0
  94. package/dist/esm/lib/bitcore/mnemonic/words/index.js +2 -0
  95. package/dist/esm/lib/bitcore/musig2/session.js +17 -16
  96. package/dist/esm/lib/bitcore/musig2/signer.js +19 -18
  97. package/dist/esm/lib/bitcore/networks.js +4 -3
  98. package/dist/esm/lib/bitcore/opcode.js +9 -5
  99. package/dist/esm/lib/bitcore/privatekey.js +25 -25
  100. package/dist/esm/lib/bitcore/publickey.js +46 -42
  101. package/dist/esm/lib/bitcore/script/interpreter/condition-stack.js +37 -0
  102. package/dist/esm/lib/bitcore/script/interpreter/index.js +4 -0
  103. package/dist/esm/lib/bitcore/script/interpreter/interpreter.js +1356 -0
  104. package/dist/esm/lib/bitcore/script/interpreter/script-num.js +195 -0
  105. package/dist/esm/lib/bitcore/script/interpreter/types.js +93 -0
  106. package/dist/esm/lib/bitcore/{taproot.js → script/taproot.js} +92 -81
  107. package/dist/esm/lib/bitcore/script.js +163 -102
  108. package/dist/esm/lib/bitcore/taproot/musig2.js +36 -27
  109. package/dist/esm/lib/bitcore/transaction/index.js +6 -6
  110. package/dist/esm/lib/bitcore/transaction/input/multisig.js +6 -0
  111. package/dist/esm/lib/bitcore/transaction/input/multisigscripthash.js +6 -0
  112. package/dist/esm/lib/bitcore/transaction/input/publickey.js +6 -0
  113. package/dist/esm/lib/bitcore/transaction/input/publickeyhash.js +6 -0
  114. package/dist/esm/lib/bitcore/transaction/input/taproot.js +16 -0
  115. package/dist/esm/lib/bitcore/transaction/input.js +55 -76
  116. package/dist/esm/lib/bitcore/transaction/outpoint.js +85 -0
  117. package/dist/esm/lib/bitcore/transaction/output.js +13 -8
  118. package/dist/esm/lib/bitcore/transaction/sighash.js +22 -22
  119. package/dist/esm/lib/bitcore/transaction/signature.js +16 -17
  120. package/dist/esm/lib/bitcore/transaction/transaction.js +58 -46
  121. package/dist/esm/lib/bitcore/transaction/unspentoutput.js +15 -11
  122. package/dist/esm/lib/bitcore/util/bits.js +27 -0
  123. package/dist/esm/lib/bitcore/util/buffer.js +20 -9
  124. package/dist/esm/lib/bitcore/util/js.js +0 -3
  125. package/dist/esm/lib/bitcore/util/merkle.js +47 -0
  126. package/dist/esm/lib/bitcore/util/preconditions.js +3 -2
  127. package/dist/esm/lib/bitcore/xaddress.js +16 -12
  128. package/dist/esm/lib/{bitcore/taproot/nft.js → nft.js} +97 -28
  129. package/dist/esm/lib/rank/index.js +15 -14
  130. package/dist/esm/lib/rank/script.js +1 -0
  131. package/dist/esm/lib/rank/transaction.js +1 -0
  132. package/dist/esm/utils/string.js +1 -0
  133. package/dist/types/lib/bitcore/address.d.ts +4 -3
  134. package/dist/types/lib/bitcore/address.d.ts.map +1 -1
  135. package/dist/types/lib/bitcore/block/block.d.ts +11 -19
  136. package/dist/types/lib/bitcore/block/block.d.ts.map +1 -1
  137. package/dist/types/lib/bitcore/block/blockheader.d.ts +30 -32
  138. package/dist/types/lib/bitcore/block/blockheader.d.ts.map +1 -1
  139. package/dist/types/lib/bitcore/block/index.d.ts +2 -0
  140. package/dist/types/lib/bitcore/block/index.d.ts.map +1 -1
  141. package/dist/types/lib/bitcore/block/merkleblock.d.ts +53 -0
  142. package/dist/types/lib/bitcore/block/merkleblock.d.ts.map +1 -0
  143. package/dist/types/lib/bitcore/crypto/bn.d.ts +2 -2
  144. package/dist/types/lib/bitcore/crypto/bn.d.ts.map +1 -1
  145. package/dist/types/lib/bitcore/crypto/ecdsa.d.ts +5 -4
  146. package/dist/types/lib/bitcore/crypto/ecdsa.d.ts.map +1 -1
  147. package/dist/types/lib/bitcore/crypto/hash.d.ts +3 -1
  148. package/dist/types/lib/bitcore/crypto/hash.d.ts.map +1 -1
  149. package/dist/types/lib/bitcore/crypto/musig2.d.ts +22 -30
  150. package/dist/types/lib/bitcore/crypto/musig2.d.ts.map +1 -1
  151. package/dist/types/lib/bitcore/crypto/point.d.ts +8 -3
  152. package/dist/types/lib/bitcore/crypto/point.d.ts.map +1 -1
  153. package/dist/types/lib/bitcore/crypto/random.d.ts +1 -0
  154. package/dist/types/lib/bitcore/crypto/random.d.ts.map +1 -1
  155. package/dist/types/lib/bitcore/crypto/schnorr.d.ts +5 -4
  156. package/dist/types/lib/bitcore/crypto/schnorr.d.ts.map +1 -1
  157. package/dist/types/lib/bitcore/crypto/signature.d.ts +4 -1
  158. package/dist/types/lib/bitcore/crypto/signature.d.ts.map +1 -1
  159. package/dist/types/lib/bitcore/crypto/sigtype.d.ts +1 -0
  160. package/dist/types/lib/bitcore/crypto/sigtype.d.ts.map +1 -1
  161. package/dist/types/lib/bitcore/encoding/base58.d.ts +1 -0
  162. package/dist/types/lib/bitcore/encoding/base58.d.ts.map +1 -1
  163. package/dist/types/lib/bitcore/encoding/base58check.d.ts +1 -2
  164. package/dist/types/lib/bitcore/encoding/base58check.d.ts.map +1 -1
  165. package/dist/types/lib/bitcore/encoding/bufferreader.d.ts +2 -1
  166. package/dist/types/lib/bitcore/encoding/bufferreader.d.ts.map +1 -1
  167. package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts +2 -2
  168. package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -1
  169. package/dist/types/lib/bitcore/encoding/varint.d.ts +3 -2
  170. package/dist/types/lib/bitcore/encoding/varint.d.ts.map +1 -1
  171. package/dist/types/lib/bitcore/errors.d.ts +3 -0
  172. package/dist/types/lib/bitcore/errors.d.ts.map +1 -1
  173. package/dist/types/lib/bitcore/hdprivatekey.d.ts +5 -4
  174. package/dist/types/lib/bitcore/hdprivatekey.d.ts.map +1 -1
  175. package/dist/types/lib/bitcore/hdpublickey.d.ts +4 -3
  176. package/dist/types/lib/bitcore/hdpublickey.d.ts.map +1 -1
  177. package/dist/types/lib/bitcore/index.d.ts +55 -60
  178. package/dist/types/lib/bitcore/index.d.ts.map +1 -1
  179. package/dist/types/lib/bitcore/message.d.ts +7 -4
  180. package/dist/types/lib/bitcore/message.d.ts.map +1 -1
  181. package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts +2 -0
  182. package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -1
  183. package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts +1 -0
  184. package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -1
  185. package/dist/types/lib/bitcore/mnemonic/words/french.d.ts +2 -0
  186. package/dist/types/lib/bitcore/mnemonic/words/french.d.ts.map +1 -0
  187. package/dist/types/lib/bitcore/mnemonic/words/index.d.ts +1 -0
  188. package/dist/types/lib/bitcore/mnemonic/words/index.d.ts.map +1 -1
  189. package/dist/types/lib/bitcore/musig2/session.d.ts +8 -7
  190. package/dist/types/lib/bitcore/musig2/session.d.ts.map +1 -1
  191. package/dist/types/lib/bitcore/musig2/signer.d.ts +10 -9
  192. package/dist/types/lib/bitcore/musig2/signer.d.ts.map +1 -1
  193. package/dist/types/lib/bitcore/networks.d.ts +1 -0
  194. package/dist/types/lib/bitcore/networks.d.ts.map +1 -1
  195. package/dist/types/lib/bitcore/opcode.d.ts +5 -0
  196. package/dist/types/lib/bitcore/opcode.d.ts.map +1 -1
  197. package/dist/types/lib/bitcore/privatekey.d.ts +5 -4
  198. package/dist/types/lib/bitcore/privatekey.d.ts.map +1 -1
  199. package/dist/types/lib/bitcore/publickey.d.ts +13 -6
  200. package/dist/types/lib/bitcore/publickey.d.ts.map +1 -1
  201. package/dist/types/lib/bitcore/{chunk.d.ts → script/chunk.d.ts} +1 -0
  202. package/dist/types/lib/bitcore/script/chunk.d.ts.map +1 -0
  203. package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts +11 -0
  204. package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts.map +1 -0
  205. package/dist/types/lib/bitcore/script/interpreter/index.d.ts +5 -0
  206. package/dist/types/lib/bitcore/script/interpreter/index.d.ts.map +1 -0
  207. package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts +21 -0
  208. package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts.map +1 -0
  209. package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts +35 -0
  210. package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts.map +1 -0
  211. package/dist/types/lib/bitcore/script/interpreter/types.d.ts +101 -0
  212. package/dist/types/lib/bitcore/script/interpreter/types.d.ts.map +1 -0
  213. package/dist/types/lib/bitcore/{taproot.d.ts → script/taproot.d.ts} +20 -7
  214. package/dist/types/lib/bitcore/script/taproot.d.ts.map +1 -0
  215. package/dist/types/lib/bitcore/script.d.ts +12 -11
  216. package/dist/types/lib/bitcore/script.d.ts.map +1 -1
  217. package/dist/types/lib/bitcore/taproot/musig2.d.ts +17 -16
  218. package/dist/types/lib/bitcore/taproot/musig2.d.ts.map +1 -1
  219. package/dist/types/lib/bitcore/transaction/index.d.ts +11 -11
  220. package/dist/types/lib/bitcore/transaction/index.d.ts.map +1 -1
  221. package/dist/types/lib/bitcore/transaction/input/multisig.d.ts +5 -0
  222. package/dist/types/lib/bitcore/transaction/input/multisig.d.ts.map +1 -0
  223. package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts +5 -0
  224. package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts.map +1 -0
  225. package/dist/types/lib/bitcore/transaction/input/publickey.d.ts +5 -0
  226. package/dist/types/lib/bitcore/transaction/input/publickey.d.ts.map +1 -0
  227. package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts +6 -0
  228. package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts.map +1 -0
  229. package/dist/types/lib/bitcore/transaction/input/taproot.d.ts +18 -0
  230. package/dist/types/lib/bitcore/transaction/input/taproot.d.ts.map +1 -0
  231. package/dist/types/lib/bitcore/transaction/input.d.ts +27 -35
  232. package/dist/types/lib/bitcore/transaction/input.d.ts.map +1 -1
  233. package/dist/types/lib/bitcore/transaction/outpoint.d.ts +32 -0
  234. package/dist/types/lib/bitcore/transaction/outpoint.d.ts.map +1 -0
  235. package/dist/types/lib/bitcore/transaction/output.d.ts +3 -1
  236. package/dist/types/lib/bitcore/transaction/output.d.ts.map +1 -1
  237. package/dist/types/lib/bitcore/transaction/sighash.d.ts +8 -7
  238. package/dist/types/lib/bitcore/transaction/sighash.d.ts.map +1 -1
  239. package/dist/types/lib/bitcore/transaction/signature.d.ts +3 -2
  240. package/dist/types/lib/bitcore/transaction/signature.d.ts.map +1 -1
  241. package/dist/types/lib/bitcore/transaction/transaction.d.ts +18 -15
  242. package/dist/types/lib/bitcore/transaction/transaction.d.ts.map +1 -1
  243. package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts +9 -7
  244. package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -1
  245. package/dist/types/lib/bitcore/unit.d.ts.map +1 -1
  246. package/dist/types/lib/bitcore/util/base32.d.ts.map +1 -1
  247. package/dist/types/lib/bitcore/util/bits.d.ts +5 -0
  248. package/dist/types/lib/bitcore/util/bits.d.ts.map +1 -0
  249. package/dist/types/lib/bitcore/util/buffer.d.ts +10 -2
  250. package/dist/types/lib/bitcore/util/buffer.d.ts.map +1 -1
  251. package/dist/types/lib/bitcore/util/js.d.ts +0 -1
  252. package/dist/types/lib/bitcore/util/js.d.ts.map +1 -1
  253. package/dist/types/lib/bitcore/util/merkle.d.ts +6 -0
  254. package/dist/types/lib/bitcore/util/merkle.d.ts.map +1 -0
  255. package/dist/types/lib/bitcore/util/preconditions.d.ts.map +1 -1
  256. package/dist/types/lib/bitcore/xaddress.d.ts +1 -0
  257. package/dist/types/lib/bitcore/xaddress.d.ts.map +1 -1
  258. package/dist/types/lib/{bitcore/taproot/nft.d.ts → nft.d.ts} +26 -12
  259. package/dist/types/lib/nft.d.ts.map +1 -0
  260. package/dist/types/lib/rank/index.d.ts +1 -0
  261. package/dist/types/lib/rank/index.d.ts.map +1 -1
  262. package/dist/types/lib/rank/script.d.ts +1 -0
  263. package/dist/types/lib/rank/script.d.ts.map +1 -1
  264. package/dist/types/lib/rank/transaction.d.ts +1 -0
  265. package/dist/types/lib/rank/transaction.d.ts.map +1 -1
  266. package/dist/types/utils/string.d.ts +1 -0
  267. package/dist/types/utils/string.d.ts.map +1 -1
  268. package/package.json +3 -2
  269. package/dist/cjs/lib/bitcore/script/interpreter.js +0 -1757
  270. package/dist/esm/lib/bitcore/script/interpreter.js +0 -1753
  271. package/dist/types/lib/bitcore/chunk.d.ts.map +0 -1
  272. package/dist/types/lib/bitcore/script/interpreter.d.ts +0 -101
  273. package/dist/types/lib/bitcore/script/interpreter.d.ts.map +0 -1
  274. package/dist/types/lib/bitcore/taproot/nft.d.ts.map +0 -1
  275. package/dist/types/lib/bitcore/taproot.d.ts.map +0 -1
  276. /package/dist/cjs/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
  277. /package/dist/esm/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Schnorr = void 0;
4
- const bn_js_1 = require("./bn.js");
5
- const point_js_1 = require("./point.js");
6
- const signature_js_1 = require("./signature.js");
7
- const hash_js_1 = require("./hash.js");
4
+ const bn_1 = require("./bn");
5
+ const point_1 = require("./point");
6
+ const signature_1 = require("./signature");
7
+ const hash_1 = require("./hash");
8
+ const util_1 = require("../util");
8
9
  class Schnorr {
9
10
  hashbuf;
10
11
  endian;
@@ -41,26 +42,26 @@ class Schnorr {
41
42
  if (!hashbuf || !privkey || !d) {
42
43
  throw new Error('invalid parameters');
43
44
  }
44
- if (!Buffer.isBuffer(hashbuf) || hashbuf.length !== 32) {
45
+ if (!util_1.BufferUtil.isBuffer(hashbuf) || hashbuf.length !== 32) {
45
46
  throw new Error('hashbuf must be a 32 byte buffer');
46
47
  }
47
- const e = new bn_js_1.BN(hashbuf, 'be');
48
+ const e = bn_1.BN.fromBuffer(hashbuf);
48
49
  const obj = this._findSignature(d, e);
49
50
  obj.compressed = this.pubkey.compressed;
50
51
  obj.isSchnorr = true;
51
- this.sig = new signature_js_1.Signature(obj);
52
+ this.sig = new signature_1.Signature(obj);
52
53
  return this;
53
54
  }
54
55
  _findSignature(d, e) {
55
- const n = point_js_1.Point.getN();
56
- const G = point_js_1.Point.getG();
57
- if (d.lte(new bn_js_1.BN(0))) {
56
+ const n = point_1.Point.getN();
57
+ const G = point_1.Point.getG();
58
+ if (d.lte(bn_1.BN.Zero)) {
58
59
  throw new Error('privkey out of field of curve');
59
60
  }
60
61
  if (d.gte(n)) {
61
62
  throw new Error('privkey out of field of curve');
62
63
  }
63
- let k = this.nonceFunctionRFC6979(d.toArrayLike(Buffer, 'be', 32), e.toArrayLike(Buffer, 'be', 32));
64
+ let k = this.nonceFunctionRFC6979(d.toBuffer({ size: 32 }), e.toBuffer({ size: 32 }));
64
65
  const P = G.mul(d);
65
66
  const R = G.mul(k);
66
67
  if (R.hasSquare()) {
@@ -68,94 +69,84 @@ class Schnorr {
68
69
  else {
69
70
  k = n.sub(k);
70
71
  }
71
- const r = R.getX();
72
+ const r = R.x;
72
73
  const rBuffer = this.getrBuffer(r);
73
- const e0 = new bn_js_1.BN(hash_js_1.Hash.sha256(Buffer.concat([
74
+ const e0 = bn_1.BN.fromBuffer(hash_1.Hash.sha256(util_1.BufferUtil.concat([
74
75
  rBuffer,
75
- point_js_1.Point.pointToCompressed(P),
76
- e.toArrayLike(Buffer, 'be', 32),
77
- ])), 'be');
76
+ point_1.Point.pointToCompressed(P),
77
+ e.toBuffer({ size: 32 }),
78
+ ])));
78
79
  const s = e0.mul(d).add(k).mod(n);
79
80
  return { r, s, compressed: this.pubkey.compressed, isSchnorr: true };
80
81
  }
81
82
  getrBuffer(r) {
82
- const rNaturalLength = r.toArrayLike(Buffer, 'be').length;
83
- if (rNaturalLength < 32) {
84
- return r.toArrayLike(Buffer, 'be', 32);
85
- }
86
- return r.toArrayLike(Buffer, 'be');
83
+ const buf = r.toBuffer();
84
+ return buf.length < 32 ? r.toBuffer({ size: 32 }) : buf;
87
85
  }
88
86
  getsBuffer(s) {
89
- const sNaturalLength = s.toArrayLike(Buffer, 'be').length;
90
- if (sNaturalLength < 32) {
91
- return s.toArrayLike(Buffer, 'be', 32);
92
- }
93
- return s.toArrayLike(Buffer, 'be');
87
+ const buf = s.toBuffer();
88
+ return buf.length < 32 ? s.toBuffer({ size: 32 }) : buf;
94
89
  }
95
90
  sigError() {
96
- if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
97
- return true;
98
- }
99
- const sigLength = this.getrBuffer(this.sig.r).length + this.getsBuffer(this.sig.s).length;
100
- if (!(sigLength === 64 || sigLength === 65)) {
91
+ if (!util_1.BufferUtil.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
101
92
  return true;
102
93
  }
103
94
  const hashbuf = this.endian === 'little' ? this.reverseBuffer(this.hashbuf) : this.hashbuf;
104
95
  const P = this.pubkey.point;
105
- const G = point_js_1.Point.getG();
96
+ const G = point_1.Point.getG();
106
97
  if (P.isInfinity()) {
107
98
  return true;
108
99
  }
109
100
  const r = this.sig.r;
110
101
  const s = this.sig.s;
111
- const p = new bn_js_1.BN('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16);
112
- const n = point_js_1.Point.getN();
102
+ const p = bn_1.BN.fromString('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex');
103
+ const n = point_1.Point.getN();
113
104
  if (r.gte(p) || s.gte(n)) {
114
105
  return true;
115
106
  }
116
107
  const Br = this.getrBuffer(this.sig.r);
117
- const Bp = point_js_1.Point.pointToCompressed(P);
118
- const hash = hash_js_1.Hash.sha256(Buffer.concat([Br, Bp, hashbuf]));
119
- const e = new bn_js_1.BN(hash, 'be').mod(n);
108
+ const Bp = point_1.Point.pointToCompressed(P);
109
+ const hash = hash_1.Hash.sha256(util_1.BufferUtil.concat([Br, Bp, hashbuf]));
110
+ const e = bn_1.BN.fromBuffer(hash).mod(n);
120
111
  const sG = G.mul(s);
121
112
  const eP = P.mul(n.sub(e));
122
113
  const R = sG.add(eP);
123
- if (R.isInfinity() || !R.hasSquare() || !R.getX().eq(r)) {
114
+ if (R.isInfinity() || !R.hasSquare() || !R.x.eq(r)) {
124
115
  return true;
125
116
  }
126
117
  return false;
127
118
  }
128
119
  verify() {
129
- this.verified = !!!this.sigError();
120
+ this.verified = this.sigError() === false;
130
121
  return this;
131
122
  }
132
123
  nonceFunctionRFC6979(privkey, msgbuf) {
133
- let V = Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex');
134
- let K = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
135
- const blob = Buffer.concat([
124
+ let V = util_1.BufferUtil.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex');
125
+ let K = util_1.BufferUtil.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
126
+ const blob = util_1.BufferUtil.concat([
136
127
  privkey,
137
128
  msgbuf,
138
- Buffer.from('', 'ascii'),
139
- Buffer.from('Schnorr+SHA256 ', 'ascii'),
129
+ util_1.BufferUtil.from('', 'ascii'),
130
+ util_1.BufferUtil.from('Schnorr+SHA256 ', 'ascii'),
140
131
  ]);
141
- K = hash_js_1.Hash.sha256hmac(Buffer.concat([V, Buffer.from('00', 'hex'), blob]), K);
142
- V = hash_js_1.Hash.sha256hmac(V, K);
143
- K = hash_js_1.Hash.sha256hmac(Buffer.concat([V, Buffer.from('01', 'hex'), blob]), K);
144
- V = hash_js_1.Hash.sha256hmac(V, K);
145
- let k = new bn_js_1.BN(0);
132
+ K = hash_1.Hash.sha256hmac(util_1.BufferUtil.concat([V, util_1.BufferUtil.from('00', 'hex'), blob]), K);
133
+ V = hash_1.Hash.sha256hmac(V, K);
134
+ K = hash_1.Hash.sha256hmac(util_1.BufferUtil.concat([V, util_1.BufferUtil.from('01', 'hex'), blob]), K);
135
+ V = hash_1.Hash.sha256hmac(V, K);
136
+ let k = bn_1.BN.Zero;
146
137
  let T;
147
138
  while (true) {
148
- V = hash_js_1.Hash.sha256hmac(V, K);
149
- T = new bn_js_1.BN(V, 'be');
139
+ V = hash_1.Hash.sha256hmac(V, K);
140
+ T = bn_1.BN.fromBuffer(V);
150
141
  k = T;
151
142
  if (V.length < 32) {
152
143
  throw new Error('V length should be >= 32');
153
144
  }
154
- if (k.gt(new bn_js_1.BN(0)) && k.lt(point_js_1.Point.getN())) {
145
+ if (k.gt(bn_1.BN.Zero) && k.lt(point_1.Point.getN())) {
155
146
  break;
156
147
  }
157
- K = hash_js_1.Hash.sha256hmac(Buffer.concat([V, Buffer.from('00', 'hex')]), K);
158
- V = hash_js_1.Hash.sha256hmac(V, K);
148
+ K = hash_1.Hash.sha256hmac(util_1.BufferUtil.concat([V, util_1.BufferUtil.from('00', 'hex')]), K);
149
+ V = hash_1.Hash.sha256hmac(V, K);
159
150
  }
160
151
  return k;
161
152
  }
@@ -179,7 +170,7 @@ class Schnorr {
179
170
  .verify().verified;
180
171
  }
181
172
  reverseBuffer(buf) {
182
- const buf2 = Buffer.alloc(buf.length);
173
+ const buf2 = util_1.BufferUtil.alloc(buf.length);
183
174
  for (let i = 0; i < buf.length; i++) {
184
175
  buf2[i] = buf[buf.length - 1 - i];
185
176
  }
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Signature = void 0;
4
- const bn_js_1 = require("./bn.js");
4
+ const bn_1 = require("./bn");
5
+ const util_1 = require("../util");
6
+ const sigtype_1 = require("./sigtype");
5
7
  class Signature {
6
8
  r;
7
9
  s;
@@ -15,8 +17,10 @@ class Signature {
15
17
  static SIGHASH_FORKID = 0x40;
16
18
  static SIGHASH_LOTUS = 0x60;
17
19
  static SIGHASH_ANYONECANPAY = 0x80;
20
+ static SIGHASH_RESERVED = 0x20;
21
+ static SIGHASH_ALGORITHM_MASK = 0x60;
18
22
  constructor(r, s, isSchnorr) {
19
- if (r instanceof bn_js_1.BN) {
23
+ if (r instanceof bn_1.BN) {
20
24
  this.set({
21
25
  r: r,
22
26
  s: s,
@@ -41,18 +45,18 @@ class Signature {
41
45
  return this;
42
46
  }
43
47
  static fromCompact(buf) {
44
- if (!Buffer.isBuffer(buf)) {
48
+ if (!util_1.BufferUtil.isBuffer(buf)) {
45
49
  throw new Error('Argument is expected to be a Buffer');
46
50
  }
47
- const sig = new Signature(new bn_js_1.BN(0), new bn_js_1.BN(0));
51
+ const sig = new Signature(new bn_1.BN(0), new bn_1.BN(0));
48
52
  let compressed = true;
49
- let i = buf.subarray(0, 1)[0] - 27 - 4;
53
+ let i = buf.slice(0, 1)[0] - 27 - 4;
50
54
  if (i < 0) {
51
55
  compressed = false;
52
56
  i = i + 4;
53
57
  }
54
- const b2 = buf.subarray(1, 33);
55
- const b3 = buf.subarray(33, 65);
58
+ const b2 = buf.slice(1, 33);
59
+ const b3 = buf.slice(33, 65);
56
60
  if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
57
61
  throw new Error('i must be 0, 1, 2, or 3');
58
62
  }
@@ -64,24 +68,25 @@ class Signature {
64
68
  }
65
69
  sig.compressed = compressed;
66
70
  sig.i = i;
67
- sig.r = new bn_js_1.BN(b2, 'le');
68
- sig.s = new bn_js_1.BN(b3, 'le');
71
+ sig.r = bn_1.BN.fromBuffer(b2);
72
+ sig.s = bn_1.BN.fromBuffer(b3);
69
73
  return sig;
70
74
  }
71
75
  static fromDER(buf, strict = true) {
72
- if ((buf.length === 64 || buf.length === 65) && buf[0] !== 0x30) {
76
+ const rawSig = buf.length === 65 && buf[0] !== 0x30 ? buf.slice(0, 64) : buf;
77
+ if ((0, sigtype_1.isSchnorrSignature)(rawSig) && buf[0] !== 0x30) {
73
78
  const obj = Signature.parseSchnorrEncodedSig(buf);
74
- const sig = new Signature(new bn_js_1.BN(0), new bn_js_1.BN(0));
79
+ const sig = new Signature(new bn_1.BN(0), new bn_1.BN(0));
75
80
  sig.r = obj.r;
76
81
  sig.s = obj.s;
77
82
  sig.isSchnorr = true;
78
83
  return sig;
79
84
  }
80
- if (buf.length === 64 && buf[0] === 0x30) {
85
+ if ((0, sigtype_1.isSchnorrSignature)(buf) && buf[0] === 0x30) {
81
86
  throw new Error('64 DER (ecdsa) signatures not allowed');
82
87
  }
83
88
  const obj = Signature.parseDER(buf, strict);
84
- const sig = new Signature(new bn_js_1.BN(0), new bn_js_1.BN(0));
89
+ const sig = new Signature(new bn_1.BN(0), new bn_1.BN(0));
85
90
  sig.r = obj.r;
86
91
  sig.s = obj.s;
87
92
  return sig;
@@ -91,17 +96,17 @@ class Signature {
91
96
  }
92
97
  static fromTxFormat(buf) {
93
98
  const nhashtype = buf.readUInt8(buf.length - 1);
94
- const derbuf = buf.subarray(0, buf.length - 1);
99
+ const derbuf = buf.slice(0, buf.length - 1);
95
100
  const sig = Signature.fromDER(derbuf, false);
96
101
  sig.nhashtype = nhashtype;
97
102
  return sig;
98
103
  }
99
104
  static fromDataFormat(buf) {
100
- const derbuf = buf.subarray(0, buf.length);
105
+ const derbuf = buf.slice(0, buf.length);
101
106
  return Signature.fromDER(derbuf, false);
102
107
  }
103
108
  static fromString(str) {
104
- const buf = Buffer.from(str, 'hex');
109
+ const buf = util_1.BufferUtil.from(str, 'hex');
105
110
  return Signature.fromDER(buf);
106
111
  }
107
112
  static fromSchnorr(buf) {
@@ -117,20 +122,20 @@ class Signature {
117
122
  if (buf.length !== 64 && buf.length !== 65) {
118
123
  throw new Error(`Invalid Schnorr signature length: ${buf.length} bytes (expected 64 or 65)`);
119
124
  }
120
- const r = buf.subarray(0, 32);
121
- const s = buf.subarray(32, 64);
125
+ const r = buf.slice(0, 32);
126
+ const s = buf.slice(32, 64);
122
127
  let hashtype;
123
128
  if (buf.length === 65) {
124
- hashtype = buf.subarray(64, 65);
129
+ hashtype = buf.slice(64, 65);
125
130
  }
126
131
  return {
127
- r: new bn_js_1.BN(r, 'be'),
128
- s: new bn_js_1.BN(s, 'be'),
132
+ r: bn_1.BN.fromBuffer(r),
133
+ s: bn_1.BN.fromBuffer(s),
129
134
  nhashtype: hashtype,
130
135
  };
131
136
  }
132
137
  static parseDER(buf, strict = true) {
133
- if (!Buffer.isBuffer(buf)) {
138
+ if (!util_1.BufferUtil.isBuffer(buf)) {
134
139
  throw new Error('DER formatted signature should be a buffer');
135
140
  }
136
141
  const header = buf[0];
@@ -138,7 +143,7 @@ class Signature {
138
143
  throw new Error('Header byte should be 0x30');
139
144
  }
140
145
  let length = buf[1];
141
- const buflength = buf.subarray(2).length;
146
+ const buflength = buf.slice(2).length;
142
147
  if (strict && length !== buflength) {
143
148
  throw new Error('Length byte should length of what follows');
144
149
  }
@@ -148,8 +153,8 @@ class Signature {
148
153
  throw new Error('Integer byte for r should be 0x02');
149
154
  }
150
155
  const rlength = buf[2 + 1];
151
- const rbuf = buf.subarray(2 + 2, 2 + 2 + rlength);
152
- const r = new bn_js_1.BN(rbuf, 'be');
156
+ const rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
157
+ const r = new bn_1.BN(rbuf, 'be');
153
158
  if (rlength !== rbuf.length) {
154
159
  throw new Error('Length of r incorrect');
155
160
  }
@@ -158,8 +163,8 @@ class Signature {
158
163
  throw new Error('Integer byte for s should be 0x02');
159
164
  }
160
165
  const slength = buf[2 + 2 + rlength + 1];
161
- const sbuf = buf.subarray(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
162
- const s = new bn_js_1.BN(sbuf, 'be');
166
+ const sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
167
+ const s = new bn_1.BN(sbuf, 'be');
163
168
  if (slength !== sbuf.length) {
164
169
  throw new Error('Length of s incorrect');
165
170
  }
@@ -182,34 +187,38 @@ class Signature {
182
187
  if (isCompressed === false) {
183
188
  val = val - 4;
184
189
  }
185
- const b1 = Buffer.from([val]);
186
- const b2 = this.r.toArrayLike(Buffer, 'le', 32);
187
- const b3 = this.s.toArrayLike(Buffer, 'le', 32);
188
- return Buffer.concat([b1, b2, b3]);
190
+ const b1 = util_1.BufferUtil.from([val]);
191
+ const b2 = this.r.toBuffer({ size: 32 });
192
+ const b3 = this.s.toBuffer({ size: 32 });
193
+ return util_1.BufferUtil.concat([b1, b2, b3]);
189
194
  }
190
195
  toDER(signingMethod = 'ecdsa') {
191
196
  if (signingMethod === 'schnorr') {
192
- return Buffer.concat([
193
- this.r.toArrayLike(Buffer, 'be', 32),
194
- this.s.toArrayLike(Buffer, 'be', 32),
197
+ return util_1.BufferUtil.concat([
198
+ this.r.toBuffer({ size: 32 }),
199
+ this.s.toBuffer({ size: 32 }),
195
200
  ]);
196
201
  }
197
- const rnbuf = this.r.toArrayLike(Buffer, 'be');
198
- const snbuf = this.s.toArrayLike(Buffer, 'be');
202
+ const rnbuf = this.r.toBuffer();
203
+ const snbuf = this.s.toBuffer();
199
204
  const rneg = (rnbuf[0] & 0x80) !== 0;
200
205
  const sneg = (snbuf[0] & 0x80) !== 0;
201
- const rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf;
202
- const sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf;
206
+ const rbuf = rneg
207
+ ? util_1.BufferUtil.concat([util_1.BufferUtil.from([0x00]), rnbuf])
208
+ : rnbuf;
209
+ const sbuf = sneg
210
+ ? util_1.BufferUtil.concat([util_1.BufferUtil.from([0x00]), snbuf])
211
+ : snbuf;
203
212
  const rlength = rbuf.length;
204
213
  const slength = sbuf.length;
205
214
  const length = 2 + rlength + 2 + slength;
206
215
  const rheader = 0x02;
207
216
  const sheader = 0x02;
208
217
  const header = 0x30;
209
- return Buffer.concat([
210
- Buffer.from([header, length, rheader, rlength]),
218
+ return util_1.BufferUtil.concat([
219
+ util_1.BufferUtil.from([header, length, rheader, rlength]),
211
220
  rbuf,
212
- Buffer.from([sheader, slength]),
221
+ util_1.BufferUtil.from([sheader, slength]),
213
222
  sbuf,
214
223
  ]);
215
224
  }
@@ -224,9 +233,12 @@ class Signature {
224
233
  }
225
234
  toTxFormat(signingMethod) {
226
235
  const derbuf = this.toDER(signingMethod);
227
- const buf = Buffer.alloc(1);
228
- buf.writeUInt8(this.nhashtype || 0, 0);
229
- return Buffer.concat([derbuf, buf]);
236
+ const buf = util_1.BufferUtil.alloc(1);
237
+ const defaultSighash = this.isSchnorr
238
+ ? Signature.SIGHASH_ALL | Signature.SIGHASH_LOTUS
239
+ : 0;
240
+ buf.writeUInt8(this.nhashtype ?? defaultSighash, 0);
241
+ return util_1.BufferUtil.concat([derbuf, buf]);
230
242
  }
231
243
  static isDER(buf) {
232
244
  if (buf.length < 8 || buf.length > 72) {
@@ -274,8 +286,8 @@ class Signature {
274
286
  return true;
275
287
  }
276
288
  hasLowS() {
277
- const lowSThreshold = new bn_js_1.BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 16);
278
- if (this.s.lt(new bn_js_1.BN(1)) || this.s.gt(lowSThreshold)) {
289
+ const lowSThreshold = bn_1.BN.fromString('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex');
290
+ if (this.s.lt(bn_1.BN.One) || this.s.gt(lowSThreshold)) {
279
291
  return false;
280
292
  }
281
293
  return true;
@@ -284,21 +296,21 @@ class Signature {
284
296
  if (typeof this.nhashtype !== 'number') {
285
297
  return false;
286
298
  }
287
- if (this.nhashtype & Signature.SIGHASH_LOTUS) {
288
- const baseMask = ~(Signature.SIGHASH_LOTUS | Signature.SIGHASH_ANYONECANPAY) >>> 0;
289
- const baseType = this.nhashtype & baseMask;
290
- return (baseType >= Signature.SIGHASH_ALL &&
291
- baseType <= Signature.SIGHASH_SINGLE);
299
+ const baseType = this.nhashtype & 0x1f;
300
+ if (baseType < Signature.SIGHASH_ALL ||
301
+ baseType > Signature.SIGHASH_SINGLE) {
302
+ return false;
292
303
  }
293
- const mask = ~(Signature.SIGHASH_FORKID | Signature.SIGHASH_ANYONECANPAY) >>> 0;
294
- const temp = this.nhashtype & mask;
295
- if (temp < Signature.SIGHASH_ALL || temp > Signature.SIGHASH_SINGLE) {
304
+ const algorithm = this.nhashtype & Signature.SIGHASH_ALGORITHM_MASK;
305
+ if (algorithm !== 0x00 &&
306
+ algorithm !== Signature.SIGHASH_FORKID &&
307
+ algorithm !== Signature.SIGHASH_LOTUS) {
296
308
  return false;
297
309
  }
298
310
  return true;
299
311
  }
300
312
  static isTxDER(buf) {
301
- return Signature.isDER(buf.subarray(0, buf.length - 1));
313
+ return Signature.isDER(buf.slice(0, buf.length - 1));
302
314
  }
303
315
  }
304
316
  exports.Signature = Signature;
@@ -17,7 +17,7 @@ function isValidSignatureLength(sig) {
17
17
  if (sig.length === 64) {
18
18
  return true;
19
19
  }
20
- if (sig.length >= 9 && sig.length <= 73) {
20
+ if (sig.length >= 8 && sig.length <= 72) {
21
21
  return true;
22
22
  }
23
23
  return false;
@@ -5,11 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Base58 = void 0;
7
7
  const bs58_1 = __importDefault(require("bs58"));
8
+ const util_1 = require("../util");
8
9
  const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
9
10
  class Base58 {
10
11
  buf;
11
12
  constructor(obj) {
12
- if (Buffer.isBuffer(obj)) {
13
+ if (util_1.BufferUtil.isBuffer(obj)) {
13
14
  const buf = obj;
14
15
  this.fromBuffer(buf);
15
16
  }
@@ -22,7 +23,7 @@ class Base58 {
22
23
  }
23
24
  }
24
25
  static validCharacters(chars) {
25
- if (Buffer.isBuffer(chars)) {
26
+ if (util_1.BufferUtil.isBuffer(chars)) {
26
27
  chars = chars.toString();
27
28
  }
28
29
  return Array.from(chars).every(char => ALPHABET.includes(char));
@@ -32,7 +33,7 @@ class Base58 {
32
33
  return this;
33
34
  }
34
35
  static encode(buf) {
35
- if (!Buffer.isBuffer(buf)) {
36
+ if (!util_1.BufferUtil.isBuffer(buf)) {
36
37
  throw new Error('Input should be a buffer');
37
38
  }
38
39
  return bs58_1.default.encode(buf);
@@ -41,7 +42,7 @@ class Base58 {
41
42
  if (typeof str !== 'string') {
42
43
  throw new Error('Input should be a string');
43
44
  }
44
- return Buffer.from(bs58_1.default.decode(str));
45
+ return util_1.BufferUtil.from(bs58_1.default.decode(str));
45
46
  }
46
47
  fromBuffer(buf) {
47
48
  this.buf = buf;
@@ -1,86 +1,43 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Base58Check = void 0;
4
- const sha256_1 = require("@noble/hashes/sha256");
5
- const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
6
- const ALPHABET_MAP = {};
7
- for (let i = 0; i < ALPHABET.length; i++) {
8
- ALPHABET_MAP[ALPHABET[i]] = i;
9
- }
7
+ const sha2_1 = require("@noble/hashes/sha2");
8
+ const bs58_1 = __importDefault(require("bs58"));
9
+ const util_1 = require("../util");
10
10
  class Base58Check {
11
11
  static encode(buf) {
12
- if (!Buffer.isBuffer(buf)) {
12
+ if (!util_1.BufferUtil.isBuffer(buf)) {
13
13
  throw new Error('Input must be a Buffer');
14
14
  }
15
15
  const checksum = this.checksum(buf);
16
- const payload = Buffer.concat([buf, checksum]);
17
- return this.base58Encode(payload);
16
+ const payload = util_1.BufferUtil.concat([buf, checksum]);
17
+ return bs58_1.default.encode(payload);
18
18
  }
19
19
  static decode(str) {
20
20
  if (typeof str !== 'string') {
21
21
  throw new Error('Input must be a string');
22
22
  }
23
- const buf = this.base58Decode(str);
23
+ const buf = util_1.BufferUtil.from(bs58_1.default.decode(str));
24
24
  if (buf.length < 4) {
25
25
  throw new Error('Input string too short');
26
26
  }
27
- const data = buf.subarray(0, -4);
28
- const checksum = buf.subarray(-4);
27
+ const data = buf.slice(0, -4);
28
+ const checksum = buf.slice(-4);
29
29
  if (!this.validChecksum(data, checksum)) {
30
30
  throw new Error('Invalid checksum');
31
31
  }
32
32
  return data;
33
33
  }
34
34
  static checksum(data) {
35
- const hash1 = (0, sha256_1.sha256)(data);
36
- const hash2 = (0, sha256_1.sha256)(hash1);
37
- return Buffer.from(hash2).subarray(0, 4);
35
+ const hash = (0, sha2_1.sha256)((0, sha2_1.sha256)(data));
36
+ return util_1.BufferUtil.from(hash).slice(0, 4);
38
37
  }
39
38
  static validChecksum(data, checksum) {
40
39
  const calculatedChecksum = this.checksum(data);
41
40
  return calculatedChecksum.equals(checksum);
42
41
  }
43
- static base58Encode(buf) {
44
- if (buf.length === 0)
45
- return '';
46
- let num = 0n;
47
- for (let i = 0; i < buf.length; i++) {
48
- num = num * 256n + BigInt(buf[i]);
49
- }
50
- let result = '';
51
- while (num > 0n) {
52
- result = ALPHABET[Number(num % 58n)] + result;
53
- num = num / 58n;
54
- }
55
- for (let i = 0; i < buf.length && buf[i] === 0; i++) {
56
- result = '1' + result;
57
- }
58
- return result;
59
- }
60
- static base58Decode(str) {
61
- if (str.length === 0)
62
- return Buffer.alloc(0);
63
- let num = 0n;
64
- let leadingZeros = 0;
65
- for (let i = 0; i < str.length; i++) {
66
- const char = str[i];
67
- if (ALPHABET_MAP[char] === undefined) {
68
- throw new Error(`Invalid character: ${char}`);
69
- }
70
- num = num * 58n + BigInt(ALPHABET_MAP[char]);
71
- }
72
- for (let i = 0; i < str.length && str[i] === '1'; i++) {
73
- leadingZeros++;
74
- }
75
- const bytes = [];
76
- while (num > 0n) {
77
- bytes.unshift(Number(num % 256n));
78
- num = num / 256n;
79
- }
80
- for (let i = 0; i < leadingZeros; i++) {
81
- bytes.unshift(0);
82
- }
83
- return Buffer.from(bytes);
84
- }
85
42
  }
86
43
  exports.Base58Check = Base58Check;