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
@@ -8,13 +8,17 @@ import { Address } from './address.js';
8
8
  import { BitcoreError } from './errors.js';
9
9
  import { BufferUtil } from './util/buffer.js';
10
10
  import { Signature } from './crypto/signature.js';
11
- import { Chunk } from './chunk.js';
12
- import { TAPROOT_SIZE_WITH_STATE, TAPROOT_SIZE_WITHOUT_STATE, } from './taproot.js';
11
+ import { Chunk } from './script/chunk.js';
12
+ import { PUBKEY_COMPRESSED_SIZE } from './script/taproot.js';
13
13
  export class Script {
14
14
  chunks;
15
15
  _network;
16
16
  constructor(from) {
17
- if (Buffer.isBuffer(from)) {
17
+ if (!from) {
18
+ this.chunks = [];
19
+ return;
20
+ }
21
+ if (BufferUtil.isBuffer(from)) {
18
22
  return Script.fromBuffer(from);
19
23
  }
20
24
  else if (from instanceof Address) {
@@ -26,14 +30,9 @@ export class Script {
26
30
  else if (typeof from === 'string') {
27
31
  return Script.fromString(from);
28
32
  }
29
- else if (typeof from === 'object' &&
30
- from !== null &&
31
- Array.isArray(from.chunks)) {
33
+ else if (typeof from === 'object' && Array.isArray(from.chunks)) {
32
34
  this.set(from);
33
35
  }
34
- else {
35
- this.chunks = [];
36
- }
37
36
  }
38
37
  set(obj) {
39
38
  Preconditions.checkArgument(typeof obj === 'object', 'obj', 'Must be an object');
@@ -42,18 +41,25 @@ export class Script {
42
41
  this._network = obj._network;
43
42
  return this;
44
43
  }
44
+ static empty() {
45
+ return new Script();
46
+ }
45
47
  static fromBuffer(buffer) {
46
- const script = new Script();
48
+ const script = Script.empty();
47
49
  script.chunks = [];
48
50
  const br = new BufferReader(buffer);
49
51
  while (!br.finished()) {
50
52
  try {
51
53
  const opcodenum = br.readUInt8();
52
54
  let len, buf;
53
- if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
55
+ if (opcodenum >= 0 && opcodenum < Opcode.OP_PUSHDATA1) {
54
56
  len = opcodenum;
57
+ buf = br.read(len);
58
+ if (buf.length !== len) {
59
+ throw new BitcoreError.Script.InvalidBuffer(`Push data length mismatch: expected ${len}, got ${buf.length}`);
60
+ }
55
61
  script.chunks.push(new Chunk({
56
- buf: br.read(len),
62
+ buf: buf,
57
63
  len: len,
58
64
  opcodenum: opcodenum,
59
65
  }));
@@ -61,6 +67,9 @@ export class Script {
61
67
  else if (opcodenum === Opcode.OP_PUSHDATA1) {
62
68
  len = br.readUInt8();
63
69
  buf = br.read(len);
70
+ if (buf.length !== len) {
71
+ throw new BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA1 length mismatch: expected ${len}, got ${buf.length}`);
72
+ }
64
73
  script.chunks.push(new Chunk({
65
74
  buf: buf,
66
75
  len: len,
@@ -70,6 +79,9 @@ export class Script {
70
79
  else if (opcodenum === Opcode.OP_PUSHDATA2) {
71
80
  len = br.readUInt16LE();
72
81
  buf = br.read(len);
82
+ if (buf.length !== len) {
83
+ throw new BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA2 length mismatch: expected ${len}, got ${buf.length}`);
84
+ }
73
85
  script.chunks.push(new Chunk({
74
86
  buf: buf,
75
87
  len: len,
@@ -79,6 +91,9 @@ export class Script {
79
91
  else if (opcodenum === Opcode.OP_PUSHDATA4) {
80
92
  len = br.readUInt32LE();
81
93
  buf = br.read(len);
94
+ if (buf.length !== len) {
95
+ throw new BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA4 length mismatch: expected ${len}, got ${buf.length}`);
96
+ }
82
97
  script.chunks.push(new Chunk({
83
98
  buf: buf,
84
99
  len: len,
@@ -106,7 +121,7 @@ export class Script {
106
121
  if (!/^[0-9a-f]*$/.test(cleanStr)) {
107
122
  throw new BitcoreError.Script.InvalidScriptString(str);
108
123
  }
109
- const buffer = Buffer.from(cleanStr, 'hex');
124
+ const buffer = BufferUtil.from(cleanStr, 'hex');
110
125
  return Script.fromBuffer(buffer);
111
126
  }
112
127
  static fromASM(str) {
@@ -119,7 +134,7 @@ export class Script {
119
134
  const opcode = new Opcode(token);
120
135
  const opcodenum = opcode.num;
121
136
  if (opcodenum === undefined) {
122
- const buf = Buffer.from(tokens[i], 'hex');
137
+ const buf = BufferUtil.from(tokens[i], 'hex');
123
138
  let opcodenum;
124
139
  const len = buf.length;
125
140
  if (len >= 0 && len < Opcode.OP_PUSHDATA1) {
@@ -154,14 +169,14 @@ export class Script {
154
169
  return script;
155
170
  }
156
171
  static fromHex(str) {
157
- return new Script(Buffer.from(str, 'hex'));
172
+ return new Script(BufferUtil.from(str, 'hex'));
158
173
  }
159
174
  static fromAddress(address) {
160
175
  if (typeof address === 'string') {
161
176
  address = Address.fromString(address);
162
177
  }
163
178
  if (address.isPayToTaproot()) {
164
- return Script.buildPayToTaproot(address.hashBuffer);
179
+ return Script.buildTaprootOut(address.hashBuffer);
165
180
  }
166
181
  else if (address.isPayToScriptHash()) {
167
182
  return Script.buildScriptHashOut(address);
@@ -171,6 +186,55 @@ export class Script {
171
186
  }
172
187
  throw new BitcoreError.Script.UnrecognizedAddress(address);
173
188
  }
189
+ static fromPayload(scriptType, payload) {
190
+ const buf = typeof payload === 'string' ? BufferUtil.from(payload, 'hex') : payload;
191
+ switch (scriptType) {
192
+ case 'p2pk': {
193
+ if (buf.length !== 33 && buf.length !== 65) {
194
+ throw new Error(`Invalid p2pk payload length: expected 33 (compressed) or 65 (uncompressed), got ${buf.length}`);
195
+ }
196
+ return Script.buildPublicKeyOut(new PublicKey(buf));
197
+ }
198
+ case 'p2pkh': {
199
+ if (buf.length !== 20) {
200
+ throw new Error(`Invalid p2pkh payload length: expected 20, got ${buf.length}`);
201
+ }
202
+ const p2pkhScript = new Script();
203
+ p2pkhScript.chunks = [];
204
+ p2pkhScript
205
+ .add(Opcode.OP_DUP)
206
+ .add(Opcode.OP_HASH160)
207
+ .add(buf)
208
+ .add(Opcode.OP_EQUALVERIFY)
209
+ .add(Opcode.OP_CHECKSIG);
210
+ return p2pkhScript;
211
+ }
212
+ case 'p2sh': {
213
+ if (buf.length !== 20) {
214
+ throw new Error(`Invalid p2sh payload length: expected 20, got ${buf.length}`);
215
+ }
216
+ const p2shScript = new Script();
217
+ p2shScript.add(Opcode.OP_HASH160).add(buf).add(Opcode.OP_EQUAL);
218
+ return p2shScript;
219
+ }
220
+ case 'p2tr-commitment': {
221
+ if (buf.length !== 33) {
222
+ throw new Error(`Invalid p2tr-commitment payload length: expected 33, got ${buf.length}`);
223
+ }
224
+ return Script.buildTaprootOut(buf);
225
+ }
226
+ case 'p2tr-state': {
227
+ throw new Error('Cannot reconstruct full script from p2tr-state payload alone; ' +
228
+ 'the commitment is required. Use p2tr-commitment payload instead.');
229
+ }
230
+ case 'other': {
231
+ return Script.fromBuffer(buf);
232
+ }
233
+ default: {
234
+ throw new Error(`Unknown script type: ${scriptType}`);
235
+ }
236
+ }
237
+ }
174
238
  static buildMultisigOut(publicKeys, threshold, opts = {}) {
175
239
  Preconditions.checkArgument(threshold <= publicKeys.length, 'threshold', 'Number of required signatures must be less than or equal to the number of public keys');
176
240
  const script = new Script();
@@ -313,13 +377,6 @@ export class Script {
313
377
  script.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
314
378
  return script;
315
379
  }
316
- static buildWitnessMultisigOutFromScript(script) {
317
- const scriptHash = Hash.sha256(script.toBuffer());
318
- const witnessScript = new Script();
319
- witnessScript.add(Opcode.OP_0);
320
- witnessScript.add(scriptHash);
321
- return witnessScript;
322
- }
323
380
  static buildPublicKeyOut(pubkey) {
324
381
  const script = new Script();
325
382
  script.add(pubkey.toBuffer());
@@ -330,10 +387,10 @@ export class Script {
330
387
  let buffer;
331
388
  if (typeof data === 'string') {
332
389
  if (encoding === 'hex') {
333
- buffer = Buffer.from(data, 'hex');
390
+ buffer = BufferUtil.from(data, 'hex');
334
391
  }
335
392
  else {
336
- buffer = Buffer.from(data, 'utf8');
393
+ buffer = BufferUtil.from(data, 'utf8');
337
394
  }
338
395
  }
339
396
  else {
@@ -389,10 +446,10 @@ export class Script {
389
446
  script.add(publicKey.toBuffer());
390
447
  return script;
391
448
  }
392
- static buildPayToTaproot(commitment, state) {
449
+ static buildTaprootOut(commitment, state) {
393
450
  Preconditions.checkArgument(commitment !== undefined, 'commitment', 'Must be defined');
394
451
  const commitmentBuf = commitment instanceof PublicKey ? commitment.toBuffer() : commitment;
395
- if (commitmentBuf.length !== 33) {
452
+ if (commitmentBuf.length !== PUBKEY_COMPRESSED_SIZE) {
396
453
  throw new Error('Taproot commitment must be 33-byte compressed public key');
397
454
  }
398
455
  if (state && state.length !== 32) {
@@ -413,7 +470,7 @@ export class Script {
413
470
  opcodenum: chunk.num,
414
471
  }));
415
472
  }
416
- else if (Buffer.isBuffer(chunk)) {
473
+ else if (BufferUtil.isBuffer(chunk)) {
417
474
  const chunkObj = {
418
475
  buf: chunk,
419
476
  len: chunk.length,
@@ -443,6 +500,17 @@ export class Script {
443
500
  }
444
501
  return this;
445
502
  }
503
+ addTaprootState(state) {
504
+ if (state.length !== 32) {
505
+ throw new Error('Taproot state must be exactly 32 bytes');
506
+ }
507
+ if (this.chunks[3]) {
508
+ this.chunks[3].buf = state;
509
+ return this;
510
+ }
511
+ this.add(state);
512
+ return this;
513
+ }
446
514
  toBuffer() {
447
515
  const bw = new BufferWriter();
448
516
  for (const chunk of this.chunks) {
@@ -465,18 +533,6 @@ export class Script {
465
533
  toString() {
466
534
  return this.toBuffer().toString('hex');
467
535
  }
468
- toP2PKH() {
469
- if (!this.isPayToPublicKeyHash()) {
470
- throw new Error('Script is not a P2PKH address');
471
- }
472
- return this.chunks[2].buf.toString('hex');
473
- }
474
- toP2SH() {
475
- if (!this.isPayToScriptHash()) {
476
- throw new Error('Script is not a P2SH address');
477
- }
478
- return this.chunks[1].buf.toString('hex');
479
- }
480
536
  toASM() {
481
537
  let str = '';
482
538
  for (let i = 0; i < this.chunks.length; i++) {
@@ -555,53 +611,37 @@ export class Script {
555
611
  }
556
612
  return str;
557
613
  }
558
- isPayToPublicKeyHash() {
614
+ isPublicKeyHashOut() {
559
615
  return (this.chunks.length === 5 &&
560
616
  this.chunks[0].opcodenum === Opcode.OP_DUP &&
561
617
  this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
562
618
  this.chunks[2].opcodenum === 20 &&
563
- this.chunks[2].buf.length === 20 &&
564
- this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
565
- this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
566
- }
567
- isPublicKeyHashOut() {
568
- return !!(this.chunks.length === 5 &&
569
- this.chunks[0].opcodenum === Opcode.OP_DUP &&
570
- this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
571
- this.chunks[2].buf &&
572
- this.chunks[2].buf.length === 20 &&
619
+ this.chunks[2].buf?.length === 20 &&
573
620
  this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
574
621
  this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
575
622
  }
576
- isPayToScriptHash() {
623
+ isScriptHashOut() {
577
624
  return (this.chunks.length === 3 &&
578
625
  this.chunks[0].opcodenum === Opcode.OP_HASH160 &&
579
626
  this.chunks[1].opcodenum === 20 &&
580
- this.chunks[1].buf.length === 20 &&
627
+ this.chunks[1].buf?.length === 20 &&
581
628
  this.chunks[2].opcodenum === Opcode.OP_EQUAL);
582
629
  }
583
- isScriptHashOut() {
584
- const buf = this.toBuffer();
585
- return (buf.length === 23 &&
586
- buf[0] === Opcode.OP_HASH160 &&
587
- buf[1] === 0x14 &&
588
- buf[buf.length - 1] === Opcode.OP_EQUAL);
589
- }
590
- isPayToTaproot() {
591
- const buf = this.toBuffer();
592
- if (buf.length < TAPROOT_SIZE_WITHOUT_STATE ||
593
- buf[0] !== Opcode.OP_SCRIPTTYPE ||
594
- buf[1] !== Opcode.OP_1 ||
595
- buf[2] !== 33) {
630
+ isTaprootOut() {
631
+ if (this.chunks[0].opcodenum !== Opcode.OP_SCRIPTTYPE ||
632
+ this.chunks[1].opcodenum !== Opcode.OP_1) {
596
633
  return false;
597
634
  }
598
- if (buf.length === TAPROOT_SIZE_WITHOUT_STATE) {
599
- return true;
635
+ if (this.chunks.length === 3) {
636
+ return (this.chunks[2].opcodenum === 33 && this.chunks[2].buf?.length === 33);
600
637
  }
601
- return buf.length === TAPROOT_SIZE_WITH_STATE && buf[36] === 32;
602
- }
603
- isTaprootOut() {
604
- return this.isPayToTaproot();
638
+ if (this.chunks.length === 4) {
639
+ return (this.chunks[2].opcodenum === 33 &&
640
+ this.chunks[2].buf?.length === 33 &&
641
+ this.chunks[3].opcodenum === 32 &&
642
+ this.chunks[3].buf?.length === 32);
643
+ }
644
+ return false;
605
645
  }
606
646
  isTaprootIn() {
607
647
  if (this.chunks.length === 0) {
@@ -632,20 +672,45 @@ export class Script {
632
672
  getData() {
633
673
  if (this.isScriptHashOut()) {
634
674
  if (this.chunks[1] === undefined) {
635
- return Buffer.alloc(0);
675
+ return BufferUtil.alloc(0);
636
676
  }
637
677
  else {
638
- return Buffer.from(this.chunks[1].buf);
678
+ return BufferUtil.from(this.chunks[1].buf);
639
679
  }
640
680
  }
641
681
  if (this.isPublicKeyHashOut()) {
642
- return Buffer.from(this.chunks[2].buf);
682
+ return BufferUtil.from(this.chunks[2].buf);
643
683
  }
644
- if (this.isPayToTaproot()) {
645
- return Buffer.from(this.chunks[2].buf);
684
+ if (this.isTaprootOut()) {
685
+ return BufferUtil.from(this.chunks[2].buf);
646
686
  }
647
687
  throw new Error('Unrecognized script type to get data from');
648
688
  }
689
+ getPayload() {
690
+ let type;
691
+ let payload;
692
+ if (this.isPublicKeyOut()) {
693
+ type = 'p2pk';
694
+ payload = BufferUtil.from(this.chunks[0].buf);
695
+ }
696
+ else if (this.isPublicKeyHashOut()) {
697
+ type = 'p2pkh';
698
+ payload = BufferUtil.from(this.chunks[2].buf);
699
+ }
700
+ else if (this.isScriptHashOut()) {
701
+ type = 'p2sh';
702
+ payload = BufferUtil.from(this.chunks[1].buf);
703
+ }
704
+ else if (this.isTaprootOut()) {
705
+ type = this.chunks.length === 4 ? 'p2tr-state' : 'p2tr-commitment';
706
+ payload = BufferUtil.from(this.chunks[2].buf);
707
+ }
708
+ else {
709
+ type = 'other';
710
+ payload = this.toBuffer();
711
+ }
712
+ return { type, payload, payloadHex: payload.toString('hex') };
713
+ }
649
714
  getAddressInfo() {
650
715
  if (this._isInput) {
651
716
  return this._getInputAddressInfo();
@@ -663,7 +728,7 @@ export class Script {
663
728
  }
664
729
  _getOutputAddressInfo() {
665
730
  const info = {};
666
- if (this.isPayToTaproot()) {
731
+ if (this.isTaprootOut()) {
667
732
  const buf = this.toBuffer();
668
733
  info.hashBuffer = buf.slice(3, 36);
669
734
  info.type = Address.PayToTaproot;
@@ -703,7 +768,7 @@ export class Script {
703
768
  }
704
769
  if (info instanceof Address) {
705
770
  if (network) {
706
- if (this.isPayToTaproot()) {
771
+ if (this.isTaprootOut()) {
707
772
  const buf = this.toBuffer();
708
773
  const commitment = buf.slice(3, 36);
709
774
  return Address.fromTaprootCommitment(commitment, network);
@@ -758,7 +823,16 @@ export class Script {
758
823
  }
759
824
  isValid() {
760
825
  try {
761
- return this.chunks.length > 0;
826
+ for (const chunk of this.chunks) {
827
+ if (chunk.opcodenum === undefined) {
828
+ return false;
829
+ }
830
+ if (chunk.buf &&
831
+ (chunk.len === undefined || chunk.len !== chunk.buf.length)) {
832
+ return false;
833
+ }
834
+ }
835
+ return true;
762
836
  }
763
837
  catch (e) {
764
838
  return false;
@@ -768,7 +842,7 @@ export class Script {
768
842
  const cloned = new Script();
769
843
  cloned.chunks = this.chunks.map(chunk => new Chunk({
770
844
  opcodenum: chunk.opcodenum,
771
- buf: chunk.buf ? Buffer.from(chunk.buf) : undefined,
845
+ buf: chunk.buf ? BufferUtil.from(chunk.buf) : undefined,
772
846
  len: chunk.len,
773
847
  }));
774
848
  return cloned;
@@ -859,7 +933,7 @@ export class Script {
859
933
  return (this.chunks.length > 3 &&
860
934
  this._isSmallIntOp(this.chunks[0].opcodenum) &&
861
935
  this.chunks.slice(1, this.chunks.length - 2).every(function (obj) {
862
- return obj.buf && Buffer.isBuffer(obj.buf);
936
+ return obj.buf && BufferUtil.isBuffer(obj.buf);
863
937
  }) &&
864
938
  this._isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
865
939
  this.chunks[this.chunks.length - 1].opcodenum === Opcode.OP_CHECKMULTISIG);
@@ -871,7 +945,7 @@ export class Script {
871
945
  return (this.chunks.length >= 2 &&
872
946
  this.chunks[0].opcodenum === 0 &&
873
947
  this.chunks.slice(1, this.chunks.length).every(function (obj) {
874
- return obj.buf && Buffer.isBuffer(obj.buf) && Signature.isTxDER(obj.buf);
948
+ return (obj.buf && BufferUtil.isBuffer(obj.buf) && Signature.isTxDER(obj.buf));
875
949
  }));
876
950
  }
877
951
  isDataOut() {
@@ -941,11 +1015,8 @@ export class Script {
941
1015
  return this.classify() !== ScriptTypes.UNKNOWN;
942
1016
  }
943
1017
  getType() {
944
- if (this.isPayToTaproot()) {
945
- const buf = this.toBuffer();
946
- return buf.length === TAPROOT_SIZE_WITH_STATE
947
- ? 'p2tr-state'
948
- : 'p2tr-commitment';
1018
+ if (this.isTaprootOut()) {
1019
+ return this.chunks.length === 4 ? 'p2tr-state' : 'p2tr-commitment';
949
1020
  }
950
1021
  else if (this.isPublicKeyOut()) {
951
1022
  return 'p2pk';
@@ -968,11 +1039,11 @@ export class Script {
968
1039
  return false;
969
1040
  }
970
1041
  for (let i = 0; i < this.chunks.length; i++) {
971
- if (Buffer.isBuffer(this.chunks[i].buf) &&
972
- !Buffer.isBuffer(script.chunks[i].buf)) {
1042
+ if (BufferUtil.isBuffer(this.chunks[i].buf) &&
1043
+ !BufferUtil.isBuffer(script.chunks[i].buf)) {
973
1044
  return false;
974
1045
  }
975
- if (Buffer.isBuffer(this.chunks[i].buf) &&
1046
+ if (BufferUtil.isBuffer(this.chunks[i].buf) &&
976
1047
  !BufferUtil.equals(this.chunks[i].buf, script.chunks[i].buf)) {
977
1048
  return false;
978
1049
  }
@@ -992,7 +1063,7 @@ export class Script {
992
1063
  else if (obj instanceof Opcode) {
993
1064
  this._addOpcode(obj, prepend);
994
1065
  }
995
- else if (Buffer.isBuffer(obj)) {
1066
+ else if (BufferUtil.isBuffer(obj)) {
996
1067
  this._addBuffer(obj, prepend);
997
1068
  }
998
1069
  else if (obj instanceof Script) {
@@ -1133,13 +1204,3 @@ export const ScriptTypes = {
1133
1204
  MULTISIG_IN: 'Spend from multisig',
1134
1205
  DATA_OUT: 'Data push',
1135
1206
  };
1136
- export function toAddress(script, network) {
1137
- const addr = script.toAddress(network);
1138
- if (!addr || typeof addr === 'boolean') {
1139
- throw new Error('Cannot convert script to address');
1140
- }
1141
- return addr;
1142
- }
1143
- export function empty() {
1144
- return new Script();
1145
- }
@@ -1,14 +1,15 @@
1
- import { Address } from '../address.js';
2
- import { BN, Point, Hash } from '../crypto/index.js';
3
- import { musigKeyAgg, musigPartialSign, musigPartialSigVerify, musigTaggedHash, MUSIG_TAG_NONCE_COEFF, } from '../crypto/musig2.js';
4
- import { buildKeyPathTaproot, buildScriptPathTaproot, calculateTapTweak, tweakPublicKey, } from '../taproot.js';
5
- export function buildMuSigTaprootKey(signerPubKeys, state) {
6
- const keyAggContext = musigKeyAgg(signerPubKeys);
1
+ import { Address } from '../address';
2
+ import { BufferUtil } from '../util/buffer';
3
+ import { BN, Point, Hash } from '../crypto/index';
4
+ import { MUSIG2_TAG_NONCE_COEFF, muSig2KeyAgg, muSig2PartialSign, muSig2PartialSigVerify, muSig2TaggedHash, } from '../crypto/musig2';
5
+ import { buildKeyPathTaproot, buildScriptPathTaproot, calculateTapTweak, tweakPublicKey, } from '../script/taproot';
6
+ export function buildMuSig2TaprootKey(signerPubKeys) {
7
+ const keyAggContext = muSig2KeyAgg(signerPubKeys);
7
8
  const aggregatedPubKey = keyAggContext.aggregatedPubKey;
8
- const merkleRoot = Buffer.alloc(32);
9
+ const merkleRoot = BufferUtil.alloc(32);
9
10
  const tweak = calculateTapTweak(aggregatedPubKey, merkleRoot);
10
11
  const commitment = tweakPublicKey(aggregatedPubKey, merkleRoot);
11
- const script = buildKeyPathTaproot(aggregatedPubKey, state);
12
+ const script = buildKeyPathTaproot(aggregatedPubKey);
12
13
  return {
13
14
  aggregatedPubKey,
14
15
  commitment,
@@ -18,8 +19,8 @@ export function buildMuSigTaprootKey(signerPubKeys, state) {
18
19
  tweak,
19
20
  };
20
21
  }
21
- export function buildMuSigTaprootKeyWithScripts(signerPubKeys, scriptTree, state) {
22
- const keyAggContext = musigKeyAgg(signerPubKeys);
22
+ export function buildMuSig2TaprootKeyWithScripts(signerPubKeys, scriptTree, state) {
23
+ const keyAggContext = muSig2KeyAgg(signerPubKeys);
23
24
  const aggregatedPubKey = keyAggContext.aggregatedPubKey;
24
25
  const { script, commitment, merkleRoot, leaves } = buildScriptPathTaproot(aggregatedPubKey, scriptTree, state);
25
26
  const tweak = calculateTapTweak(aggregatedPubKey, merkleRoot);
@@ -40,22 +41,26 @@ export function signTaprootKeyPathWithMuSig2(secretNonce, privateKey, keyAggCont
40
41
  ...keyAggContext,
41
42
  aggregatedPubKey: commitment,
42
43
  };
43
- const partialSig = musigPartialSign(secretNonce, privateKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
44
+ const partialSig = muSig2PartialSign(secretNonce, privateKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
44
45
  if (signerIndex === 0) {
45
46
  const { R1, R2 } = aggregatedNonce;
46
- const nonceCoefData = Buffer.concat([
47
+ const nonceCoefData = BufferUtil.concat([
47
48
  commitment.toBuffer(),
48
49
  Point.pointToCompressed(R1),
49
50
  Point.pointToCompressed(R2),
50
51
  message,
51
52
  ]);
52
- const b = new BN(musigTaggedHash(MUSIG_TAG_NONCE_COEFF, nonceCoefData), 'be');
53
+ const b = BN.fromBuffer(muSig2TaggedHash(MUSIG2_TAG_NONCE_COEFF, nonceCoefData));
53
54
  const R = R1.add(R2.mul(b));
54
- const R_x = R.getX().toArrayLike(Buffer, 'be', 32);
55
+ const R_x = R.x.toBuffer({ size: 32 });
55
56
  const commitment_compressed = Point.pointToCompressed(commitment.point);
56
- const challengeData = Buffer.concat([R_x, commitment_compressed, message]);
57
- const e = new BN(Hash.sha256(challengeData), 'be').umod(n);
58
- const tweakBN = new BN(tweak, 'be').umod(n);
57
+ const challengeData = BufferUtil.concat([
58
+ R_x,
59
+ commitment_compressed,
60
+ message,
61
+ ]);
62
+ const e = BN.fromBuffer(Hash.sha256(challengeData)).umod(n);
63
+ const tweakBN = BN.fromBuffer(tweak).umod(n);
59
64
  const tweakTerm = e.mul(tweakBN).umod(n);
60
65
  return partialSig.add(tweakTerm).umod(n);
61
66
  }
@@ -71,28 +76,32 @@ export function verifyTaprootKeyPathMuSigPartial(partialSig, publicNonce, public
71
76
  let adjustedPartialSig = partialSig;
72
77
  if (signerIndex === 0) {
73
78
  const { R1, R2 } = aggregatedNonce;
74
- const nonceCoefData = Buffer.concat([
79
+ const nonceCoefData = BufferUtil.concat([
75
80
  commitment.toBuffer(),
76
81
  Point.pointToCompressed(R1),
77
82
  Point.pointToCompressed(R2),
78
83
  message,
79
84
  ]);
80
- const b = new BN(musigTaggedHash(MUSIG_TAG_NONCE_COEFF, nonceCoefData), 'be');
85
+ const b = BN.fromBuffer(muSig2TaggedHash(MUSIG2_TAG_NONCE_COEFF, nonceCoefData));
81
86
  const R = R1.add(R2.mul(b));
82
- const R_x = R.getX().toArrayLike(Buffer, 'be', 32);
87
+ const R_x = R.x.toBuffer({ size: 32 });
83
88
  const commitment_compressed = Point.pointToCompressed(commitment.point);
84
- const challengeData = Buffer.concat([R_x, commitment_compressed, message]);
85
- const e = new BN(Hash.sha256(challengeData), 'be').umod(n);
86
- const tweakBN = new BN(tweak, 'be').umod(n);
89
+ const challengeData = BufferUtil.concat([
90
+ R_x,
91
+ commitment_compressed,
92
+ message,
93
+ ]);
94
+ const e = BN.fromBuffer(Hash.sha256(challengeData)).umod(n);
95
+ const tweakBN = BN.fromBuffer(tweak).umod(n);
87
96
  const tweakTerm = e.mul(tweakBN).umod(n);
88
97
  adjustedPartialSig = partialSig.sub(tweakTerm).umod(n);
89
98
  }
90
- return musigPartialSigVerify(adjustedPartialSig, publicNonce, publicKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
99
+ return muSig2PartialSigVerify(adjustedPartialSig, publicNonce, publicKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
91
100
  }
92
101
  export function isMuSigTaprootOutput(script) {
93
- return script.isPayToTaproot();
102
+ return script.isTaprootOut();
94
103
  }
95
- export function createMuSigTaprootAddress(signerPubKeys, network, state) {
104
+ export function createMuSigTaprootAddress(signerPubKeys, network) {
96
105
  if (signerPubKeys.length === 0) {
97
106
  throw new Error('At least one signer public key is required');
98
107
  }
@@ -101,7 +110,7 @@ export function createMuSigTaprootAddress(signerPubKeys, network, state) {
101
110
  if (mismatchedKeyIndex !== -1) {
102
111
  throw new Error(`Public key network mismatch at index ${mismatchedKeyIndex}: expected '${inferredNetwork}', got '${signerPubKeys[mismatchedKeyIndex].network.name}'`);
103
112
  }
104
- const result = buildMuSigTaprootKey(signerPubKeys, state);
113
+ const result = buildMuSig2TaprootKey(signerPubKeys);
105
114
  const address = Address.fromTaprootCommitment(result.commitment, inferredNetwork);
106
115
  return {
107
116
  address,
@@ -1,6 +1,6 @@
1
- export { Input, MultisigInput, MultisigScriptHashInput, PublicKeyInput, PublicKeyHashInput, TaprootInput, MuSigTaprootInput, } from './input.js';
2
- export { Output } from './output.js';
3
- export { UnspentOutput } from './unspentoutput.js';
4
- export { sighash, sign, verify } from './sighash.js';
5
- export { TransactionSignature } from './signature.js';
6
- export { Transaction } from './transaction.js';
1
+ export { Input, MultisigInput, MultisigScriptHashInput, PublicKeyInput, PublicKeyHashInput, TaprootInput, MuSig2TaprootInput, } from './input';
2
+ export { Output } from './output';
3
+ export { UnspentOutput } from './unspentoutput';
4
+ export { sighash, sign, verify } from './sighash';
5
+ export { TransactionSignature } from './signature';
6
+ export { Transaction } from './transaction';
@@ -0,0 +1,6 @@
1
+ import { Input } from '../input';
2
+ export class MultisigInput extends Input {
3
+ constructor(data) {
4
+ super(data);
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ import { Input } from '../input';
2
+ export class MultisigScriptHashInput extends Input {
3
+ constructor(data) {
4
+ super(data);
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ import { Input } from '../input';
2
+ export class PublicKeyInput extends Input {
3
+ constructor(data) {
4
+ super(data);
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ import { Input } from '../input';
2
+ export class PublicKeyHashInput extends Input {
3
+ constructor(data) {
4
+ super(data);
5
+ }
6
+ }