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,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ScriptTypes = exports.Script = void 0;
4
- exports.toAddress = toAddress;
5
- exports.empty = empty;
6
4
  const preconditions_js_1 = require("./util/preconditions.js");
7
5
  const bufferreader_js_1 = require("./encoding/bufferreader.js");
8
6
  const bufferwriter_js_1 = require("./encoding/bufferwriter.js");
@@ -13,13 +11,17 @@ const address_js_1 = require("./address.js");
13
11
  const errors_js_1 = require("./errors.js");
14
12
  const buffer_js_1 = require("./util/buffer.js");
15
13
  const signature_js_1 = require("./crypto/signature.js");
16
- const chunk_js_1 = require("./chunk.js");
17
- const taproot_js_1 = require("./taproot.js");
14
+ const chunk_js_1 = require("./script/chunk.js");
15
+ const taproot_js_1 = require("./script/taproot.js");
18
16
  class Script {
19
17
  chunks;
20
18
  _network;
21
19
  constructor(from) {
22
- if (Buffer.isBuffer(from)) {
20
+ if (!from) {
21
+ this.chunks = [];
22
+ return;
23
+ }
24
+ if (buffer_js_1.BufferUtil.isBuffer(from)) {
23
25
  return Script.fromBuffer(from);
24
26
  }
25
27
  else if (from instanceof address_js_1.Address) {
@@ -31,14 +33,9 @@ class Script {
31
33
  else if (typeof from === 'string') {
32
34
  return Script.fromString(from);
33
35
  }
34
- else if (typeof from === 'object' &&
35
- from !== null &&
36
- Array.isArray(from.chunks)) {
36
+ else if (typeof from === 'object' && Array.isArray(from.chunks)) {
37
37
  this.set(from);
38
38
  }
39
- else {
40
- this.chunks = [];
41
- }
42
39
  }
43
40
  set(obj) {
44
41
  preconditions_js_1.Preconditions.checkArgument(typeof obj === 'object', 'obj', 'Must be an object');
@@ -47,18 +44,25 @@ class Script {
47
44
  this._network = obj._network;
48
45
  return this;
49
46
  }
47
+ static empty() {
48
+ return new Script();
49
+ }
50
50
  static fromBuffer(buffer) {
51
- const script = new Script();
51
+ const script = Script.empty();
52
52
  script.chunks = [];
53
53
  const br = new bufferreader_js_1.BufferReader(buffer);
54
54
  while (!br.finished()) {
55
55
  try {
56
56
  const opcodenum = br.readUInt8();
57
57
  let len, buf;
58
- if (opcodenum > 0 && opcodenum < opcode_js_1.Opcode.OP_PUSHDATA1) {
58
+ if (opcodenum >= 0 && opcodenum < opcode_js_1.Opcode.OP_PUSHDATA1) {
59
59
  len = opcodenum;
60
+ buf = br.read(len);
61
+ if (buf.length !== len) {
62
+ throw new errors_js_1.BitcoreError.Script.InvalidBuffer(`Push data length mismatch: expected ${len}, got ${buf.length}`);
63
+ }
60
64
  script.chunks.push(new chunk_js_1.Chunk({
61
- buf: br.read(len),
65
+ buf: buf,
62
66
  len: len,
63
67
  opcodenum: opcodenum,
64
68
  }));
@@ -66,6 +70,9 @@ class Script {
66
70
  else if (opcodenum === opcode_js_1.Opcode.OP_PUSHDATA1) {
67
71
  len = br.readUInt8();
68
72
  buf = br.read(len);
73
+ if (buf.length !== len) {
74
+ throw new errors_js_1.BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA1 length mismatch: expected ${len}, got ${buf.length}`);
75
+ }
69
76
  script.chunks.push(new chunk_js_1.Chunk({
70
77
  buf: buf,
71
78
  len: len,
@@ -75,6 +82,9 @@ class Script {
75
82
  else if (opcodenum === opcode_js_1.Opcode.OP_PUSHDATA2) {
76
83
  len = br.readUInt16LE();
77
84
  buf = br.read(len);
85
+ if (buf.length !== len) {
86
+ throw new errors_js_1.BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA2 length mismatch: expected ${len}, got ${buf.length}`);
87
+ }
78
88
  script.chunks.push(new chunk_js_1.Chunk({
79
89
  buf: buf,
80
90
  len: len,
@@ -84,6 +94,9 @@ class Script {
84
94
  else if (opcodenum === opcode_js_1.Opcode.OP_PUSHDATA4) {
85
95
  len = br.readUInt32LE();
86
96
  buf = br.read(len);
97
+ if (buf.length !== len) {
98
+ throw new errors_js_1.BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA4 length mismatch: expected ${len}, got ${buf.length}`);
99
+ }
87
100
  script.chunks.push(new chunk_js_1.Chunk({
88
101
  buf: buf,
89
102
  len: len,
@@ -111,7 +124,7 @@ class Script {
111
124
  if (!/^[0-9a-f]*$/.test(cleanStr)) {
112
125
  throw new errors_js_1.BitcoreError.Script.InvalidScriptString(str);
113
126
  }
114
- const buffer = Buffer.from(cleanStr, 'hex');
127
+ const buffer = buffer_js_1.BufferUtil.from(cleanStr, 'hex');
115
128
  return Script.fromBuffer(buffer);
116
129
  }
117
130
  static fromASM(str) {
@@ -124,7 +137,7 @@ class Script {
124
137
  const opcode = new opcode_js_1.Opcode(token);
125
138
  const opcodenum = opcode.num;
126
139
  if (opcodenum === undefined) {
127
- const buf = Buffer.from(tokens[i], 'hex');
140
+ const buf = buffer_js_1.BufferUtil.from(tokens[i], 'hex');
128
141
  let opcodenum;
129
142
  const len = buf.length;
130
143
  if (len >= 0 && len < opcode_js_1.Opcode.OP_PUSHDATA1) {
@@ -159,14 +172,14 @@ class Script {
159
172
  return script;
160
173
  }
161
174
  static fromHex(str) {
162
- return new Script(Buffer.from(str, 'hex'));
175
+ return new Script(buffer_js_1.BufferUtil.from(str, 'hex'));
163
176
  }
164
177
  static fromAddress(address) {
165
178
  if (typeof address === 'string') {
166
179
  address = address_js_1.Address.fromString(address);
167
180
  }
168
181
  if (address.isPayToTaproot()) {
169
- return Script.buildPayToTaproot(address.hashBuffer);
182
+ return Script.buildTaprootOut(address.hashBuffer);
170
183
  }
171
184
  else if (address.isPayToScriptHash()) {
172
185
  return Script.buildScriptHashOut(address);
@@ -176,6 +189,55 @@ class Script {
176
189
  }
177
190
  throw new errors_js_1.BitcoreError.Script.UnrecognizedAddress(address);
178
191
  }
192
+ static fromPayload(scriptType, payload) {
193
+ const buf = typeof payload === 'string' ? buffer_js_1.BufferUtil.from(payload, 'hex') : payload;
194
+ switch (scriptType) {
195
+ case 'p2pk': {
196
+ if (buf.length !== 33 && buf.length !== 65) {
197
+ throw new Error(`Invalid p2pk payload length: expected 33 (compressed) or 65 (uncompressed), got ${buf.length}`);
198
+ }
199
+ return Script.buildPublicKeyOut(new publickey_js_1.PublicKey(buf));
200
+ }
201
+ case 'p2pkh': {
202
+ if (buf.length !== 20) {
203
+ throw new Error(`Invalid p2pkh payload length: expected 20, got ${buf.length}`);
204
+ }
205
+ const p2pkhScript = new Script();
206
+ p2pkhScript.chunks = [];
207
+ p2pkhScript
208
+ .add(opcode_js_1.Opcode.OP_DUP)
209
+ .add(opcode_js_1.Opcode.OP_HASH160)
210
+ .add(buf)
211
+ .add(opcode_js_1.Opcode.OP_EQUALVERIFY)
212
+ .add(opcode_js_1.Opcode.OP_CHECKSIG);
213
+ return p2pkhScript;
214
+ }
215
+ case 'p2sh': {
216
+ if (buf.length !== 20) {
217
+ throw new Error(`Invalid p2sh payload length: expected 20, got ${buf.length}`);
218
+ }
219
+ const p2shScript = new Script();
220
+ p2shScript.add(opcode_js_1.Opcode.OP_HASH160).add(buf).add(opcode_js_1.Opcode.OP_EQUAL);
221
+ return p2shScript;
222
+ }
223
+ case 'p2tr-commitment': {
224
+ if (buf.length !== 33) {
225
+ throw new Error(`Invalid p2tr-commitment payload length: expected 33, got ${buf.length}`);
226
+ }
227
+ return Script.buildTaprootOut(buf);
228
+ }
229
+ case 'p2tr-state': {
230
+ throw new Error('Cannot reconstruct full script from p2tr-state payload alone; ' +
231
+ 'the commitment is required. Use p2tr-commitment payload instead.');
232
+ }
233
+ case 'other': {
234
+ return Script.fromBuffer(buf);
235
+ }
236
+ default: {
237
+ throw new Error(`Unknown script type: ${scriptType}`);
238
+ }
239
+ }
240
+ }
179
241
  static buildMultisigOut(publicKeys, threshold, opts = {}) {
180
242
  preconditions_js_1.Preconditions.checkArgument(threshold <= publicKeys.length, 'threshold', 'Number of required signatures must be less than or equal to the number of public keys');
181
243
  const script = new Script();
@@ -318,13 +380,6 @@ class Script {
318
380
  script.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
319
381
  return script;
320
382
  }
321
- static buildWitnessMultisigOutFromScript(script) {
322
- const scriptHash = hash_js_1.Hash.sha256(script.toBuffer());
323
- const witnessScript = new Script();
324
- witnessScript.add(opcode_js_1.Opcode.OP_0);
325
- witnessScript.add(scriptHash);
326
- return witnessScript;
327
- }
328
383
  static buildPublicKeyOut(pubkey) {
329
384
  const script = new Script();
330
385
  script.add(pubkey.toBuffer());
@@ -335,10 +390,10 @@ class Script {
335
390
  let buffer;
336
391
  if (typeof data === 'string') {
337
392
  if (encoding === 'hex') {
338
- buffer = Buffer.from(data, 'hex');
393
+ buffer = buffer_js_1.BufferUtil.from(data, 'hex');
339
394
  }
340
395
  else {
341
- buffer = Buffer.from(data, 'utf8');
396
+ buffer = buffer_js_1.BufferUtil.from(data, 'utf8');
342
397
  }
343
398
  }
344
399
  else {
@@ -394,10 +449,10 @@ class Script {
394
449
  script.add(publicKey.toBuffer());
395
450
  return script;
396
451
  }
397
- static buildPayToTaproot(commitment, state) {
452
+ static buildTaprootOut(commitment, state) {
398
453
  preconditions_js_1.Preconditions.checkArgument(commitment !== undefined, 'commitment', 'Must be defined');
399
454
  const commitmentBuf = commitment instanceof publickey_js_1.PublicKey ? commitment.toBuffer() : commitment;
400
- if (commitmentBuf.length !== 33) {
455
+ if (commitmentBuf.length !== taproot_js_1.PUBKEY_COMPRESSED_SIZE) {
401
456
  throw new Error('Taproot commitment must be 33-byte compressed public key');
402
457
  }
403
458
  if (state && state.length !== 32) {
@@ -418,7 +473,7 @@ class Script {
418
473
  opcodenum: chunk.num,
419
474
  }));
420
475
  }
421
- else if (Buffer.isBuffer(chunk)) {
476
+ else if (buffer_js_1.BufferUtil.isBuffer(chunk)) {
422
477
  const chunkObj = {
423
478
  buf: chunk,
424
479
  len: chunk.length,
@@ -448,6 +503,17 @@ class Script {
448
503
  }
449
504
  return this;
450
505
  }
506
+ addTaprootState(state) {
507
+ if (state.length !== 32) {
508
+ throw new Error('Taproot state must be exactly 32 bytes');
509
+ }
510
+ if (this.chunks[3]) {
511
+ this.chunks[3].buf = state;
512
+ return this;
513
+ }
514
+ this.add(state);
515
+ return this;
516
+ }
451
517
  toBuffer() {
452
518
  const bw = new bufferwriter_js_1.BufferWriter();
453
519
  for (const chunk of this.chunks) {
@@ -470,18 +536,6 @@ class Script {
470
536
  toString() {
471
537
  return this.toBuffer().toString('hex');
472
538
  }
473
- toP2PKH() {
474
- if (!this.isPayToPublicKeyHash()) {
475
- throw new Error('Script is not a P2PKH address');
476
- }
477
- return this.chunks[2].buf.toString('hex');
478
- }
479
- toP2SH() {
480
- if (!this.isPayToScriptHash()) {
481
- throw new Error('Script is not a P2SH address');
482
- }
483
- return this.chunks[1].buf.toString('hex');
484
- }
485
539
  toASM() {
486
540
  let str = '';
487
541
  for (let i = 0; i < this.chunks.length; i++) {
@@ -560,53 +614,37 @@ class Script {
560
614
  }
561
615
  return str;
562
616
  }
563
- isPayToPublicKeyHash() {
617
+ isPublicKeyHashOut() {
564
618
  return (this.chunks.length === 5 &&
565
619
  this.chunks[0].opcodenum === opcode_js_1.Opcode.OP_DUP &&
566
620
  this.chunks[1].opcodenum === opcode_js_1.Opcode.OP_HASH160 &&
567
621
  this.chunks[2].opcodenum === 20 &&
568
- this.chunks[2].buf.length === 20 &&
569
- this.chunks[3].opcodenum === opcode_js_1.Opcode.OP_EQUALVERIFY &&
570
- this.chunks[4].opcodenum === opcode_js_1.Opcode.OP_CHECKSIG);
571
- }
572
- isPublicKeyHashOut() {
573
- return !!(this.chunks.length === 5 &&
574
- this.chunks[0].opcodenum === opcode_js_1.Opcode.OP_DUP &&
575
- this.chunks[1].opcodenum === opcode_js_1.Opcode.OP_HASH160 &&
576
- this.chunks[2].buf &&
577
- this.chunks[2].buf.length === 20 &&
622
+ this.chunks[2].buf?.length === 20 &&
578
623
  this.chunks[3].opcodenum === opcode_js_1.Opcode.OP_EQUALVERIFY &&
579
624
  this.chunks[4].opcodenum === opcode_js_1.Opcode.OP_CHECKSIG);
580
625
  }
581
- isPayToScriptHash() {
626
+ isScriptHashOut() {
582
627
  return (this.chunks.length === 3 &&
583
628
  this.chunks[0].opcodenum === opcode_js_1.Opcode.OP_HASH160 &&
584
629
  this.chunks[1].opcodenum === 20 &&
585
- this.chunks[1].buf.length === 20 &&
630
+ this.chunks[1].buf?.length === 20 &&
586
631
  this.chunks[2].opcodenum === opcode_js_1.Opcode.OP_EQUAL);
587
632
  }
588
- isScriptHashOut() {
589
- const buf = this.toBuffer();
590
- return (buf.length === 23 &&
591
- buf[0] === opcode_js_1.Opcode.OP_HASH160 &&
592
- buf[1] === 0x14 &&
593
- buf[buf.length - 1] === opcode_js_1.Opcode.OP_EQUAL);
594
- }
595
- isPayToTaproot() {
596
- const buf = this.toBuffer();
597
- if (buf.length < taproot_js_1.TAPROOT_SIZE_WITHOUT_STATE ||
598
- buf[0] !== opcode_js_1.Opcode.OP_SCRIPTTYPE ||
599
- buf[1] !== opcode_js_1.Opcode.OP_1 ||
600
- buf[2] !== 33) {
633
+ isTaprootOut() {
634
+ if (this.chunks[0].opcodenum !== opcode_js_1.Opcode.OP_SCRIPTTYPE ||
635
+ this.chunks[1].opcodenum !== opcode_js_1.Opcode.OP_1) {
601
636
  return false;
602
637
  }
603
- if (buf.length === taproot_js_1.TAPROOT_SIZE_WITHOUT_STATE) {
604
- return true;
638
+ if (this.chunks.length === 3) {
639
+ return (this.chunks[2].opcodenum === 33 && this.chunks[2].buf?.length === 33);
605
640
  }
606
- return buf.length === taproot_js_1.TAPROOT_SIZE_WITH_STATE && buf[36] === 32;
607
- }
608
- isTaprootOut() {
609
- return this.isPayToTaproot();
641
+ if (this.chunks.length === 4) {
642
+ return (this.chunks[2].opcodenum === 33 &&
643
+ this.chunks[2].buf?.length === 33 &&
644
+ this.chunks[3].opcodenum === 32 &&
645
+ this.chunks[3].buf?.length === 32);
646
+ }
647
+ return false;
610
648
  }
611
649
  isTaprootIn() {
612
650
  if (this.chunks.length === 0) {
@@ -637,20 +675,45 @@ class Script {
637
675
  getData() {
638
676
  if (this.isScriptHashOut()) {
639
677
  if (this.chunks[1] === undefined) {
640
- return Buffer.alloc(0);
678
+ return buffer_js_1.BufferUtil.alloc(0);
641
679
  }
642
680
  else {
643
- return Buffer.from(this.chunks[1].buf);
681
+ return buffer_js_1.BufferUtil.from(this.chunks[1].buf);
644
682
  }
645
683
  }
646
684
  if (this.isPublicKeyHashOut()) {
647
- return Buffer.from(this.chunks[2].buf);
685
+ return buffer_js_1.BufferUtil.from(this.chunks[2].buf);
648
686
  }
649
- if (this.isPayToTaproot()) {
650
- return Buffer.from(this.chunks[2].buf);
687
+ if (this.isTaprootOut()) {
688
+ return buffer_js_1.BufferUtil.from(this.chunks[2].buf);
651
689
  }
652
690
  throw new Error('Unrecognized script type to get data from');
653
691
  }
692
+ getPayload() {
693
+ let type;
694
+ let payload;
695
+ if (this.isPublicKeyOut()) {
696
+ type = 'p2pk';
697
+ payload = buffer_js_1.BufferUtil.from(this.chunks[0].buf);
698
+ }
699
+ else if (this.isPublicKeyHashOut()) {
700
+ type = 'p2pkh';
701
+ payload = buffer_js_1.BufferUtil.from(this.chunks[2].buf);
702
+ }
703
+ else if (this.isScriptHashOut()) {
704
+ type = 'p2sh';
705
+ payload = buffer_js_1.BufferUtil.from(this.chunks[1].buf);
706
+ }
707
+ else if (this.isTaprootOut()) {
708
+ type = this.chunks.length === 4 ? 'p2tr-state' : 'p2tr-commitment';
709
+ payload = buffer_js_1.BufferUtil.from(this.chunks[2].buf);
710
+ }
711
+ else {
712
+ type = 'other';
713
+ payload = this.toBuffer();
714
+ }
715
+ return { type, payload, payloadHex: payload.toString('hex') };
716
+ }
654
717
  getAddressInfo() {
655
718
  if (this._isInput) {
656
719
  return this._getInputAddressInfo();
@@ -668,7 +731,7 @@ class Script {
668
731
  }
669
732
  _getOutputAddressInfo() {
670
733
  const info = {};
671
- if (this.isPayToTaproot()) {
734
+ if (this.isTaprootOut()) {
672
735
  const buf = this.toBuffer();
673
736
  info.hashBuffer = buf.slice(3, 36);
674
737
  info.type = address_js_1.Address.PayToTaproot;
@@ -708,7 +771,7 @@ class Script {
708
771
  }
709
772
  if (info instanceof address_js_1.Address) {
710
773
  if (network) {
711
- if (this.isPayToTaproot()) {
774
+ if (this.isTaprootOut()) {
712
775
  const buf = this.toBuffer();
713
776
  const commitment = buf.slice(3, 36);
714
777
  return address_js_1.Address.fromTaprootCommitment(commitment, network);
@@ -763,7 +826,16 @@ class Script {
763
826
  }
764
827
  isValid() {
765
828
  try {
766
- return this.chunks.length > 0;
829
+ for (const chunk of this.chunks) {
830
+ if (chunk.opcodenum === undefined) {
831
+ return false;
832
+ }
833
+ if (chunk.buf &&
834
+ (chunk.len === undefined || chunk.len !== chunk.buf.length)) {
835
+ return false;
836
+ }
837
+ }
838
+ return true;
767
839
  }
768
840
  catch (e) {
769
841
  return false;
@@ -773,7 +845,7 @@ class Script {
773
845
  const cloned = new Script();
774
846
  cloned.chunks = this.chunks.map(chunk => new chunk_js_1.Chunk({
775
847
  opcodenum: chunk.opcodenum,
776
- buf: chunk.buf ? Buffer.from(chunk.buf) : undefined,
848
+ buf: chunk.buf ? buffer_js_1.BufferUtil.from(chunk.buf) : undefined,
777
849
  len: chunk.len,
778
850
  }));
779
851
  return cloned;
@@ -864,7 +936,7 @@ class Script {
864
936
  return (this.chunks.length > 3 &&
865
937
  this._isSmallIntOp(this.chunks[0].opcodenum) &&
866
938
  this.chunks.slice(1, this.chunks.length - 2).every(function (obj) {
867
- return obj.buf && Buffer.isBuffer(obj.buf);
939
+ return obj.buf && buffer_js_1.BufferUtil.isBuffer(obj.buf);
868
940
  }) &&
869
941
  this._isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
870
942
  this.chunks[this.chunks.length - 1].opcodenum === opcode_js_1.Opcode.OP_CHECKMULTISIG);
@@ -876,7 +948,7 @@ class Script {
876
948
  return (this.chunks.length >= 2 &&
877
949
  this.chunks[0].opcodenum === 0 &&
878
950
  this.chunks.slice(1, this.chunks.length).every(function (obj) {
879
- return obj.buf && Buffer.isBuffer(obj.buf) && signature_js_1.Signature.isTxDER(obj.buf);
951
+ return (obj.buf && buffer_js_1.BufferUtil.isBuffer(obj.buf) && signature_js_1.Signature.isTxDER(obj.buf));
880
952
  }));
881
953
  }
882
954
  isDataOut() {
@@ -946,11 +1018,8 @@ class Script {
946
1018
  return this.classify() !== exports.ScriptTypes.UNKNOWN;
947
1019
  }
948
1020
  getType() {
949
- if (this.isPayToTaproot()) {
950
- const buf = this.toBuffer();
951
- return buf.length === taproot_js_1.TAPROOT_SIZE_WITH_STATE
952
- ? 'p2tr-state'
953
- : 'p2tr-commitment';
1021
+ if (this.isTaprootOut()) {
1022
+ return this.chunks.length === 4 ? 'p2tr-state' : 'p2tr-commitment';
954
1023
  }
955
1024
  else if (this.isPublicKeyOut()) {
956
1025
  return 'p2pk';
@@ -973,11 +1042,11 @@ class Script {
973
1042
  return false;
974
1043
  }
975
1044
  for (let i = 0; i < this.chunks.length; i++) {
976
- if (Buffer.isBuffer(this.chunks[i].buf) &&
977
- !Buffer.isBuffer(script.chunks[i].buf)) {
1045
+ if (buffer_js_1.BufferUtil.isBuffer(this.chunks[i].buf) &&
1046
+ !buffer_js_1.BufferUtil.isBuffer(script.chunks[i].buf)) {
978
1047
  return false;
979
1048
  }
980
- if (Buffer.isBuffer(this.chunks[i].buf) &&
1049
+ if (buffer_js_1.BufferUtil.isBuffer(this.chunks[i].buf) &&
981
1050
  !buffer_js_1.BufferUtil.equals(this.chunks[i].buf, script.chunks[i].buf)) {
982
1051
  return false;
983
1052
  }
@@ -997,7 +1066,7 @@ class Script {
997
1066
  else if (obj instanceof opcode_js_1.Opcode) {
998
1067
  this._addOpcode(obj, prepend);
999
1068
  }
1000
- else if (Buffer.isBuffer(obj)) {
1069
+ else if (buffer_js_1.BufferUtil.isBuffer(obj)) {
1001
1070
  this._addBuffer(obj, prepend);
1002
1071
  }
1003
1072
  else if (obj instanceof Script) {
@@ -1139,13 +1208,3 @@ exports.ScriptTypes = {
1139
1208
  MULTISIG_IN: 'Spend from multisig',
1140
1209
  DATA_OUT: 'Data push',
1141
1210
  };
1142
- function toAddress(script, network) {
1143
- const addr = script.toAddress(network);
1144
- if (!addr || typeof addr === 'boolean') {
1145
- throw new Error('Cannot convert script to address');
1146
- }
1147
- return addr;
1148
- }
1149
- function empty() {
1150
- return new Script();
1151
- }
@@ -1,22 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildMuSigTaprootKey = buildMuSigTaprootKey;
4
- exports.buildMuSigTaprootKeyWithScripts = buildMuSigTaprootKeyWithScripts;
3
+ exports.buildMuSig2TaprootKey = buildMuSig2TaprootKey;
4
+ exports.buildMuSig2TaprootKeyWithScripts = buildMuSig2TaprootKeyWithScripts;
5
5
  exports.signTaprootKeyPathWithMuSig2 = signTaprootKeyPathWithMuSig2;
6
6
  exports.verifyTaprootKeyPathMuSigPartial = verifyTaprootKeyPathMuSigPartial;
7
7
  exports.isMuSigTaprootOutput = isMuSigTaprootOutput;
8
8
  exports.createMuSigTaprootAddress = createMuSigTaprootAddress;
9
- const address_js_1 = require("../address.js");
10
- const index_js_1 = require("../crypto/index.js");
11
- const musig2_js_1 = require("../crypto/musig2.js");
12
- const taproot_js_1 = require("../taproot.js");
13
- function buildMuSigTaprootKey(signerPubKeys, state) {
14
- const keyAggContext = (0, musig2_js_1.musigKeyAgg)(signerPubKeys);
9
+ const address_1 = require("../address");
10
+ const buffer_1 = require("../util/buffer");
11
+ const index_1 = require("../crypto/index");
12
+ const musig2_1 = require("../crypto/musig2");
13
+ const taproot_1 = require("../script/taproot");
14
+ function buildMuSig2TaprootKey(signerPubKeys) {
15
+ const keyAggContext = (0, musig2_1.muSig2KeyAgg)(signerPubKeys);
15
16
  const aggregatedPubKey = keyAggContext.aggregatedPubKey;
16
- const merkleRoot = Buffer.alloc(32);
17
- const tweak = (0, taproot_js_1.calculateTapTweak)(aggregatedPubKey, merkleRoot);
18
- const commitment = (0, taproot_js_1.tweakPublicKey)(aggregatedPubKey, merkleRoot);
19
- const script = (0, taproot_js_1.buildKeyPathTaproot)(aggregatedPubKey, state);
17
+ const merkleRoot = buffer_1.BufferUtil.alloc(32);
18
+ const tweak = (0, taproot_1.calculateTapTweak)(aggregatedPubKey, merkleRoot);
19
+ const commitment = (0, taproot_1.tweakPublicKey)(aggregatedPubKey, merkleRoot);
20
+ const script = (0, taproot_1.buildKeyPathTaproot)(aggregatedPubKey);
20
21
  return {
21
22
  aggregatedPubKey,
22
23
  commitment,
@@ -26,11 +27,11 @@ function buildMuSigTaprootKey(signerPubKeys, state) {
26
27
  tweak,
27
28
  };
28
29
  }
29
- function buildMuSigTaprootKeyWithScripts(signerPubKeys, scriptTree, state) {
30
- const keyAggContext = (0, musig2_js_1.musigKeyAgg)(signerPubKeys);
30
+ function buildMuSig2TaprootKeyWithScripts(signerPubKeys, scriptTree, state) {
31
+ const keyAggContext = (0, musig2_1.muSig2KeyAgg)(signerPubKeys);
31
32
  const aggregatedPubKey = keyAggContext.aggregatedPubKey;
32
- const { script, commitment, merkleRoot, leaves } = (0, taproot_js_1.buildScriptPathTaproot)(aggregatedPubKey, scriptTree, state);
33
- const tweak = (0, taproot_js_1.calculateTapTweak)(aggregatedPubKey, merkleRoot);
33
+ const { script, commitment, merkleRoot, leaves } = (0, taproot_1.buildScriptPathTaproot)(aggregatedPubKey, scriptTree, state);
34
+ const tweak = (0, taproot_1.calculateTapTweak)(aggregatedPubKey, merkleRoot);
34
35
  return {
35
36
  aggregatedPubKey,
36
37
  commitment,
@@ -42,35 +43,39 @@ function buildMuSigTaprootKeyWithScripts(signerPubKeys, scriptTree, state) {
42
43
  };
43
44
  }
44
45
  function signTaprootKeyPathWithMuSig2(secretNonce, privateKey, keyAggContext, signerIndex, aggregatedNonce, message, tweak) {
45
- const n = index_js_1.Point.getN();
46
+ const n = index_1.Point.getN();
46
47
  const commitment = keyAggContext.aggregatedPubKey.addScalar(tweak);
47
48
  const modifiedKeyAggContext = {
48
49
  ...keyAggContext,
49
50
  aggregatedPubKey: commitment,
50
51
  };
51
- const partialSig = (0, musig2_js_1.musigPartialSign)(secretNonce, privateKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
52
+ const partialSig = (0, musig2_1.muSig2PartialSign)(secretNonce, privateKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
52
53
  if (signerIndex === 0) {
53
54
  const { R1, R2 } = aggregatedNonce;
54
- const nonceCoefData = Buffer.concat([
55
+ const nonceCoefData = buffer_1.BufferUtil.concat([
55
56
  commitment.toBuffer(),
56
- index_js_1.Point.pointToCompressed(R1),
57
- index_js_1.Point.pointToCompressed(R2),
57
+ index_1.Point.pointToCompressed(R1),
58
+ index_1.Point.pointToCompressed(R2),
58
59
  message,
59
60
  ]);
60
- const b = new index_js_1.BN((0, musig2_js_1.musigTaggedHash)(musig2_js_1.MUSIG_TAG_NONCE_COEFF, nonceCoefData), 'be');
61
+ const b = index_1.BN.fromBuffer((0, musig2_1.muSig2TaggedHash)(musig2_1.MUSIG2_TAG_NONCE_COEFF, nonceCoefData));
61
62
  const R = R1.add(R2.mul(b));
62
- const R_x = R.getX().toArrayLike(Buffer, 'be', 32);
63
- const commitment_compressed = index_js_1.Point.pointToCompressed(commitment.point);
64
- const challengeData = Buffer.concat([R_x, commitment_compressed, message]);
65
- const e = new index_js_1.BN(index_js_1.Hash.sha256(challengeData), 'be').umod(n);
66
- const tweakBN = new index_js_1.BN(tweak, 'be').umod(n);
63
+ const R_x = R.x.toBuffer({ size: 32 });
64
+ const commitment_compressed = index_1.Point.pointToCompressed(commitment.point);
65
+ const challengeData = buffer_1.BufferUtil.concat([
66
+ R_x,
67
+ commitment_compressed,
68
+ message,
69
+ ]);
70
+ const e = index_1.BN.fromBuffer(index_1.Hash.sha256(challengeData)).umod(n);
71
+ const tweakBN = index_1.BN.fromBuffer(tweak).umod(n);
67
72
  const tweakTerm = e.mul(tweakBN).umod(n);
68
73
  return partialSig.add(tweakTerm).umod(n);
69
74
  }
70
75
  return partialSig;
71
76
  }
72
77
  function verifyTaprootKeyPathMuSigPartial(partialSig, publicNonce, publicKey, keyAggContext, signerIndex, aggregatedNonce, message, tweak) {
73
- const n = index_js_1.Point.getN();
78
+ const n = index_1.Point.getN();
74
79
  const commitment = keyAggContext.aggregatedPubKey.addScalar(tweak);
75
80
  const modifiedKeyAggContext = {
76
81
  ...keyAggContext,
@@ -79,28 +84,32 @@ function verifyTaprootKeyPathMuSigPartial(partialSig, publicNonce, publicKey, ke
79
84
  let adjustedPartialSig = partialSig;
80
85
  if (signerIndex === 0) {
81
86
  const { R1, R2 } = aggregatedNonce;
82
- const nonceCoefData = Buffer.concat([
87
+ const nonceCoefData = buffer_1.BufferUtil.concat([
83
88
  commitment.toBuffer(),
84
- index_js_1.Point.pointToCompressed(R1),
85
- index_js_1.Point.pointToCompressed(R2),
89
+ index_1.Point.pointToCompressed(R1),
90
+ index_1.Point.pointToCompressed(R2),
86
91
  message,
87
92
  ]);
88
- const b = new index_js_1.BN((0, musig2_js_1.musigTaggedHash)(musig2_js_1.MUSIG_TAG_NONCE_COEFF, nonceCoefData), 'be');
93
+ const b = index_1.BN.fromBuffer((0, musig2_1.muSig2TaggedHash)(musig2_1.MUSIG2_TAG_NONCE_COEFF, nonceCoefData));
89
94
  const R = R1.add(R2.mul(b));
90
- const R_x = R.getX().toArrayLike(Buffer, 'be', 32);
91
- const commitment_compressed = index_js_1.Point.pointToCompressed(commitment.point);
92
- const challengeData = Buffer.concat([R_x, commitment_compressed, message]);
93
- const e = new index_js_1.BN(index_js_1.Hash.sha256(challengeData), 'be').umod(n);
94
- const tweakBN = new index_js_1.BN(tweak, 'be').umod(n);
95
+ const R_x = R.x.toBuffer({ size: 32 });
96
+ const commitment_compressed = index_1.Point.pointToCompressed(commitment.point);
97
+ const challengeData = buffer_1.BufferUtil.concat([
98
+ R_x,
99
+ commitment_compressed,
100
+ message,
101
+ ]);
102
+ const e = index_1.BN.fromBuffer(index_1.Hash.sha256(challengeData)).umod(n);
103
+ const tweakBN = index_1.BN.fromBuffer(tweak).umod(n);
95
104
  const tweakTerm = e.mul(tweakBN).umod(n);
96
105
  adjustedPartialSig = partialSig.sub(tweakTerm).umod(n);
97
106
  }
98
- return (0, musig2_js_1.musigPartialSigVerify)(adjustedPartialSig, publicNonce, publicKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
107
+ return (0, musig2_1.muSig2PartialSigVerify)(adjustedPartialSig, publicNonce, publicKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
99
108
  }
100
109
  function isMuSigTaprootOutput(script) {
101
- return script.isPayToTaproot();
110
+ return script.isTaprootOut();
102
111
  }
103
- function createMuSigTaprootAddress(signerPubKeys, network, state) {
112
+ function createMuSigTaprootAddress(signerPubKeys, network) {
104
113
  if (signerPubKeys.length === 0) {
105
114
  throw new Error('At least one signer public key is required');
106
115
  }
@@ -109,8 +118,8 @@ function createMuSigTaprootAddress(signerPubKeys, network, state) {
109
118
  if (mismatchedKeyIndex !== -1) {
110
119
  throw new Error(`Public key network mismatch at index ${mismatchedKeyIndex}: expected '${inferredNetwork}', got '${signerPubKeys[mismatchedKeyIndex].network.name}'`);
111
120
  }
112
- const result = buildMuSigTaprootKey(signerPubKeys, state);
113
- const address = address_js_1.Address.fromTaprootCommitment(result.commitment, inferredNetwork);
121
+ const result = buildMuSig2TaprootKey(signerPubKeys);
122
+ const address = address_1.Address.fromTaprootCommitment(result.commitment, inferredNetwork);
114
123
  return {
115
124
  address,
116
125
  script: result.script,