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
@@ -0,0 +1,1362 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Interpreter = void 0;
4
+ exports.evalScript = evalScript;
5
+ exports.verifyScript = verifyScript;
6
+ const crypto_1 = require("../../crypto");
7
+ const publickey_1 = require("../../publickey");
8
+ const sighash_1 = require("../../transaction/sighash");
9
+ const buffer_1 = require("../../util/buffer");
10
+ const opcode_1 = require("../../opcode");
11
+ const hash_1 = require("../../crypto/hash");
12
+ const script_1 = require("../../script");
13
+ const condition_stack_1 = require("./condition-stack");
14
+ const script_num_1 = require("./script-num");
15
+ const types_1 = require("./types");
16
+ const taproot_1 = require("../taproot");
17
+ function stacktop(stack, offset) {
18
+ return stack[stack.length + offset];
19
+ }
20
+ function stackset(stack, offset, value) {
21
+ stack[stack.length + offset] = value;
22
+ }
23
+ function popstack(stack) {
24
+ if (stack.length === 0) {
25
+ throw new Error('popstack: stack empty');
26
+ }
27
+ stack.pop();
28
+ }
29
+ function stackswap(stack, offsetA, offsetB) {
30
+ const idxA = stack.length + offsetA;
31
+ const idxB = stack.length + offsetB;
32
+ const tmp = stack[idxA];
33
+ stack[idxA] = stack[idxB];
34
+ stack[idxB] = tmp;
35
+ }
36
+ const EMPTY_BUFFER = buffer_1.BufferUtil.alloc(0);
37
+ const FALSE_BUFFER = buffer_1.BufferUtil.alloc(0);
38
+ const TRUE_BUFFER = buffer_1.BufferUtil.from([0x01]);
39
+ function castToBool(buf) {
40
+ for (let i = 0; i < buf.length; i++) {
41
+ if (buf[i] !== 0) {
42
+ if (i === buf.length - 1 && buf[i] === 0x80) {
43
+ return false;
44
+ }
45
+ return true;
46
+ }
47
+ }
48
+ return false;
49
+ }
50
+ const FIRST_UNDEFINED_OP_VALUE = opcode_1.Opcode.OP_RAWLEFTBITSHIFT + 1;
51
+ function isOpcodeDisabled(opcode) {
52
+ switch (opcode) {
53
+ case opcode_1.Opcode.OP_RESERVED:
54
+ case opcode_1.Opcode.OP_VERIF:
55
+ case opcode_1.Opcode.OP_VERNOTIF:
56
+ case opcode_1.Opcode.OP_IFDUP:
57
+ case opcode_1.Opcode.OP_INVERT:
58
+ case opcode_1.Opcode.OP_RESERVED1:
59
+ case opcode_1.Opcode.OP_RESERVED2:
60
+ case opcode_1.Opcode.OP_2MUL:
61
+ case opcode_1.Opcode.OP_2DIV:
62
+ case opcode_1.Opcode.OP_MUL:
63
+ case opcode_1.Opcode.OP_NUMEQUAL:
64
+ case opcode_1.Opcode.OP_NUMEQUALVERIFY:
65
+ case opcode_1.Opcode.OP_NUMNOTEQUAL:
66
+ case opcode_1.Opcode.OP_SHA1:
67
+ return true;
68
+ default:
69
+ if (opcode >= FIRST_UNDEFINED_OP_VALUE) {
70
+ return true;
71
+ }
72
+ return false;
73
+ }
74
+ }
75
+ function checkMinimalPush(data, opcode) {
76
+ if (data.length === 0) {
77
+ return opcode === opcode_1.Opcode.OP_0;
78
+ }
79
+ if (data.length === 1) {
80
+ if (data[0] >= 1 && data[0] <= 16) {
81
+ return opcode === opcode_1.Opcode.OP_1 + (data[0] - 1);
82
+ }
83
+ if (data[0] === 0x81) {
84
+ return opcode === opcode_1.Opcode.OP_1NEGATE;
85
+ }
86
+ }
87
+ if (data.length <= 75) {
88
+ return opcode === data.length;
89
+ }
90
+ if (data.length <= 255) {
91
+ return opcode === opcode_1.Opcode.OP_PUSHDATA1;
92
+ }
93
+ if (data.length <= 65535) {
94
+ return opcode === opcode_1.Opcode.OP_PUSHDATA2;
95
+ }
96
+ return true;
97
+ }
98
+ function readInstruction(script, offset) {
99
+ if (offset >= script.length) {
100
+ return null;
101
+ }
102
+ const opcode = script[offset];
103
+ let data = EMPTY_BUFFER;
104
+ let next = offset + 1;
105
+ if (opcode >= 0 && opcode < opcode_1.Opcode.OP_PUSHDATA1) {
106
+ const len = opcode;
107
+ if (next + len > script.length) {
108
+ return null;
109
+ }
110
+ data = script.slice(next, next + len);
111
+ next += len;
112
+ }
113
+ else if (opcode === opcode_1.Opcode.OP_PUSHDATA1) {
114
+ if (next >= script.length) {
115
+ return null;
116
+ }
117
+ const len = script[next];
118
+ next++;
119
+ if (next + len > script.length) {
120
+ return null;
121
+ }
122
+ data = script.slice(next, next + len);
123
+ next += len;
124
+ }
125
+ else if (opcode === opcode_1.Opcode.OP_PUSHDATA2) {
126
+ if (next + 2 > script.length) {
127
+ return null;
128
+ }
129
+ const len = script[next] | (script[next + 1] << 8);
130
+ next += 2;
131
+ if (next + len > script.length) {
132
+ return null;
133
+ }
134
+ data = script.slice(next, next + len);
135
+ next += len;
136
+ }
137
+ else if (opcode === opcode_1.Opcode.OP_PUSHDATA4) {
138
+ if (next + 4 > script.length) {
139
+ return null;
140
+ }
141
+ const len = script[next] |
142
+ (script[next + 1] << 8) |
143
+ (script[next + 2] << 16) |
144
+ ((script[next + 3] << 24) >>> 0);
145
+ next += 4;
146
+ if (next + len > script.length) {
147
+ return null;
148
+ }
149
+ data = script.slice(next, next + len);
150
+ next += len;
151
+ }
152
+ return { opcode, data, nextOffset: next };
153
+ }
154
+ function success() {
155
+ return { success: true, error: types_1.ScriptError.OK };
156
+ }
157
+ function failure(error) {
158
+ return { success: false, error };
159
+ }
160
+ function evalScript(stack, scriptBuf, flags, checker, metrics, execdata) {
161
+ if (scriptBuf.length > types_1.MAX_SCRIPT_SIZE) {
162
+ return failure(types_1.ScriptError.SCRIPT_SIZE);
163
+ }
164
+ const altstack = [];
165
+ const vfExec = new condition_stack_1.ConditionStack();
166
+ let nOpCount = 0;
167
+ let opcodePos = 0;
168
+ let offset = 0;
169
+ let codeHashBegin = 0;
170
+ try {
171
+ while (offset < scriptBuf.length) {
172
+ const fExec = vfExec.allTrue();
173
+ const instr = readInstruction(scriptBuf, offset);
174
+ if (!instr) {
175
+ return failure(types_1.ScriptError.BAD_OPCODE);
176
+ }
177
+ const { opcode, data: pushData } = instr;
178
+ offset = instr.nextOffset;
179
+ if (pushData.length > types_1.MAX_SCRIPT_ELEMENT_SIZE) {
180
+ return failure(types_1.ScriptError.PUSH_SIZE);
181
+ }
182
+ if (opcode > opcode_1.Opcode.OP_16 && ++nOpCount > types_1.MAX_OPS_PER_SCRIPT) {
183
+ return failure(types_1.ScriptError.OP_COUNT);
184
+ }
185
+ if (isOpcodeDisabled(opcode)) {
186
+ return failure(types_1.ScriptError.DISABLED_OPCODE);
187
+ }
188
+ if (opcode === opcode_1.Opcode.OP_SCRIPTTYPE) {
189
+ return failure(types_1.ScriptError.INVALID_OP_SCRIPTTYPE);
190
+ }
191
+ if (fExec && opcode >= 0 && opcode <= opcode_1.Opcode.OP_PUSHDATA4) {
192
+ if (!checkMinimalPush(pushData, opcode)) {
193
+ return failure(types_1.ScriptError.MINIMALDATA);
194
+ }
195
+ stack.push(pushData);
196
+ }
197
+ else if (fExec ||
198
+ (opcode >= opcode_1.Opcode.OP_IF && opcode <= opcode_1.Opcode.OP_ENDIF)) {
199
+ const result = executeOpcode(opcode, stack, altstack, vfExec, fExec, flags, checker, metrics, execdata, scriptBuf, codeHashBegin, offset, opcodePos, nOpCount);
200
+ if (!result.success) {
201
+ return result;
202
+ }
203
+ if (result.codeHashBegin !== undefined) {
204
+ codeHashBegin = result.codeHashBegin;
205
+ }
206
+ if (result.nOpCount !== undefined) {
207
+ nOpCount = result.nOpCount;
208
+ }
209
+ }
210
+ if (stack.length + altstack.length > types_1.MAX_STACK_SIZE) {
211
+ return failure(types_1.ScriptError.STACK_SIZE);
212
+ }
213
+ opcodePos++;
214
+ }
215
+ }
216
+ catch {
217
+ return failure(types_1.ScriptError.UNKNOWN);
218
+ }
219
+ if (!vfExec.isEmpty()) {
220
+ return failure(types_1.ScriptError.UNBALANCED_CONDITIONAL);
221
+ }
222
+ return success();
223
+ }
224
+ function opcodeSuccess(overrides) {
225
+ return { success: true, error: types_1.ScriptError.OK, ...overrides };
226
+ }
227
+ function opcodeFailure(error) {
228
+ return { success: false, error };
229
+ }
230
+ function executeOpcode(opcode, stack, altstack, vfExec, fExec, flags, checker, metrics, execdata, scriptBuf, codeHashBegin, currentOffset, opcodePos, nOpCount) {
231
+ switch (opcode) {
232
+ case opcode_1.Opcode.OP_1NEGATE:
233
+ case opcode_1.Opcode.OP_1:
234
+ case opcode_1.Opcode.OP_2:
235
+ case opcode_1.Opcode.OP_3:
236
+ case opcode_1.Opcode.OP_4:
237
+ case opcode_1.Opcode.OP_5:
238
+ case opcode_1.Opcode.OP_6:
239
+ case opcode_1.Opcode.OP_7:
240
+ case opcode_1.Opcode.OP_8:
241
+ case opcode_1.Opcode.OP_9:
242
+ case opcode_1.Opcode.OP_10:
243
+ case opcode_1.Opcode.OP_11:
244
+ case opcode_1.Opcode.OP_12:
245
+ case opcode_1.Opcode.OP_13:
246
+ case opcode_1.Opcode.OP_14:
247
+ case opcode_1.Opcode.OP_15:
248
+ case opcode_1.Opcode.OP_16: {
249
+ const n = opcode - (opcode_1.Opcode.OP_1 - 1);
250
+ const bn = new script_num_1.ScriptNum(BigInt(n));
251
+ stack.push(bn.toBuffer());
252
+ return opcodeSuccess();
253
+ }
254
+ case opcode_1.Opcode.OP_NOP:
255
+ return opcodeSuccess();
256
+ case opcode_1.Opcode.OP_CHECKLOCKTIMEVERIFY: {
257
+ if (stack.length < 1) {
258
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
259
+ }
260
+ const nLockTime = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true, 5);
261
+ if (nLockTime.isNegative()) {
262
+ return opcodeFailure(types_1.ScriptError.NEGATIVE_LOCKTIME);
263
+ }
264
+ if (!checker.checkLockTime(nLockTime.value)) {
265
+ return opcodeFailure(types_1.ScriptError.UNSATISFIED_LOCKTIME);
266
+ }
267
+ return opcodeSuccess();
268
+ }
269
+ case opcode_1.Opcode.OP_CHECKSEQUENCEVERIFY: {
270
+ if (stack.length < 1) {
271
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
272
+ }
273
+ const nSequence = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true, 5);
274
+ if (nSequence.isNegative()) {
275
+ return opcodeFailure(types_1.ScriptError.NEGATIVE_LOCKTIME);
276
+ }
277
+ const SEQUENCE_LOCKTIME_DISABLE_FLAG = 1 << 31;
278
+ if ((Number(nSequence.value) & SEQUENCE_LOCKTIME_DISABLE_FLAG) !== 0) {
279
+ return opcodeSuccess();
280
+ }
281
+ if (!checker.checkSequence(nSequence.value)) {
282
+ return opcodeFailure(types_1.ScriptError.UNSATISFIED_LOCKTIME);
283
+ }
284
+ return opcodeSuccess();
285
+ }
286
+ case opcode_1.Opcode.OP_NOP1:
287
+ case opcode_1.Opcode.OP_NOP4:
288
+ case opcode_1.Opcode.OP_NOP5:
289
+ case opcode_1.Opcode.OP_NOP6:
290
+ case opcode_1.Opcode.OP_NOP7:
291
+ case opcode_1.Opcode.OP_NOP8:
292
+ case opcode_1.Opcode.OP_NOP9:
293
+ case opcode_1.Opcode.OP_NOP10: {
294
+ if (flags & types_1.ScriptFlags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
295
+ return opcodeFailure(types_1.ScriptError.DISCOURAGE_UPGRADABLE_NOPS);
296
+ }
297
+ return opcodeSuccess();
298
+ }
299
+ case opcode_1.Opcode.OP_IF:
300
+ case opcode_1.Opcode.OP_NOTIF: {
301
+ let fValue = false;
302
+ if (fExec) {
303
+ if (stack.length < 1) {
304
+ return opcodeFailure(types_1.ScriptError.UNBALANCED_CONDITIONAL);
305
+ }
306
+ const vch = stacktop(stack, -1);
307
+ if (flags & types_1.ScriptFlags.VERIFY_MINIMALIF) {
308
+ if (vch.length > 1) {
309
+ return opcodeFailure(types_1.ScriptError.MINIMALIF);
310
+ }
311
+ if (vch.length === 1 && vch[0] !== 1) {
312
+ return opcodeFailure(types_1.ScriptError.MINIMALIF);
313
+ }
314
+ }
315
+ fValue = castToBool(vch);
316
+ if (opcode === opcode_1.Opcode.OP_NOTIF) {
317
+ fValue = !fValue;
318
+ }
319
+ popstack(stack);
320
+ }
321
+ vfExec.pushBack(fValue);
322
+ return opcodeSuccess();
323
+ }
324
+ case opcode_1.Opcode.OP_ELSE: {
325
+ if (vfExec.isEmpty()) {
326
+ return opcodeFailure(types_1.ScriptError.UNBALANCED_CONDITIONAL);
327
+ }
328
+ vfExec.toggleTop();
329
+ return opcodeSuccess();
330
+ }
331
+ case opcode_1.Opcode.OP_ENDIF: {
332
+ if (vfExec.isEmpty()) {
333
+ return opcodeFailure(types_1.ScriptError.UNBALANCED_CONDITIONAL);
334
+ }
335
+ vfExec.popBack();
336
+ return opcodeSuccess();
337
+ }
338
+ case opcode_1.Opcode.OP_VERIFY: {
339
+ if (stack.length < 1) {
340
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
341
+ }
342
+ if (castToBool(stacktop(stack, -1))) {
343
+ popstack(stack);
344
+ }
345
+ else {
346
+ return opcodeFailure(types_1.ScriptError.VERIFY);
347
+ }
348
+ return opcodeSuccess();
349
+ }
350
+ case opcode_1.Opcode.OP_RETURN:
351
+ return opcodeFailure(types_1.ScriptError.OP_RETURN);
352
+ case opcode_1.Opcode.OP_TOALTSTACK: {
353
+ if (stack.length < 1) {
354
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
355
+ }
356
+ altstack.push(stacktop(stack, -1));
357
+ popstack(stack);
358
+ return opcodeSuccess();
359
+ }
360
+ case opcode_1.Opcode.OP_FROMALTSTACK: {
361
+ if (altstack.length < 1) {
362
+ return opcodeFailure(types_1.ScriptError.INVALID_ALTSTACK_OPERATION);
363
+ }
364
+ stack.push(altstack[altstack.length - 1]);
365
+ altstack.pop();
366
+ return opcodeSuccess();
367
+ }
368
+ case opcode_1.Opcode.OP_2DROP: {
369
+ if (stack.length < 2) {
370
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
371
+ }
372
+ popstack(stack);
373
+ popstack(stack);
374
+ return opcodeSuccess();
375
+ }
376
+ case opcode_1.Opcode.OP_2DUP: {
377
+ if (stack.length < 2) {
378
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
379
+ }
380
+ stack.push(stacktop(stack, -2));
381
+ stack.push(stacktop(stack, -2));
382
+ return opcodeSuccess();
383
+ }
384
+ case opcode_1.Opcode.OP_3DUP: {
385
+ if (stack.length < 3) {
386
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
387
+ }
388
+ stack.push(stacktop(stack, -3));
389
+ stack.push(stacktop(stack, -3));
390
+ stack.push(stacktop(stack, -3));
391
+ return opcodeSuccess();
392
+ }
393
+ case opcode_1.Opcode.OP_2OVER: {
394
+ if (stack.length < 4) {
395
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
396
+ }
397
+ stack.push(stacktop(stack, -4));
398
+ stack.push(stacktop(stack, -4));
399
+ return opcodeSuccess();
400
+ }
401
+ case opcode_1.Opcode.OP_2ROT: {
402
+ if (stack.length < 6) {
403
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
404
+ }
405
+ const v1 = stacktop(stack, -6);
406
+ const v2 = stacktop(stack, -5);
407
+ stack.splice(stack.length - 6, 2);
408
+ stack.push(v1, v2);
409
+ return opcodeSuccess();
410
+ }
411
+ case opcode_1.Opcode.OP_2SWAP: {
412
+ if (stack.length < 4) {
413
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
414
+ }
415
+ stackswap(stack, -4, -2);
416
+ stackswap(stack, -3, -1);
417
+ return opcodeSuccess();
418
+ }
419
+ case opcode_1.Opcode.OP_DEPTH: {
420
+ const bn = new script_num_1.ScriptNum(BigInt(stack.length));
421
+ stack.push(bn.toBuffer());
422
+ return opcodeSuccess();
423
+ }
424
+ case opcode_1.Opcode.OP_DROP: {
425
+ if (stack.length < 1) {
426
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
427
+ }
428
+ popstack(stack);
429
+ return opcodeSuccess();
430
+ }
431
+ case opcode_1.Opcode.OP_DUP: {
432
+ if (stack.length < 1) {
433
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
434
+ }
435
+ stack.push(stacktop(stack, -1));
436
+ return opcodeSuccess();
437
+ }
438
+ case opcode_1.Opcode.OP_NIP: {
439
+ if (stack.length < 2) {
440
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
441
+ }
442
+ stack.splice(stack.length - 2, 1);
443
+ return opcodeSuccess();
444
+ }
445
+ case opcode_1.Opcode.OP_OVER: {
446
+ if (stack.length < 2) {
447
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
448
+ }
449
+ stack.push(stacktop(stack, -2));
450
+ return opcodeSuccess();
451
+ }
452
+ case opcode_1.Opcode.OP_PICK:
453
+ case opcode_1.Opcode.OP_ROLL: {
454
+ if (stack.length < 2) {
455
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
456
+ }
457
+ const n = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true).toNumber();
458
+ popstack(stack);
459
+ if (n < 0 || n >= stack.length) {
460
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
461
+ }
462
+ const val = stacktop(stack, -n - 1);
463
+ if (opcode === opcode_1.Opcode.OP_ROLL) {
464
+ stack.splice(stack.length - n - 1, 1);
465
+ }
466
+ stack.push(val);
467
+ return opcodeSuccess();
468
+ }
469
+ case opcode_1.Opcode.OP_ROT: {
470
+ if (stack.length < 3) {
471
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
472
+ }
473
+ stackswap(stack, -3, -2);
474
+ stackswap(stack, -2, -1);
475
+ return opcodeSuccess();
476
+ }
477
+ case opcode_1.Opcode.OP_SWAP: {
478
+ if (stack.length < 2) {
479
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
480
+ }
481
+ stackswap(stack, -2, -1);
482
+ return opcodeSuccess();
483
+ }
484
+ case opcode_1.Opcode.OP_TUCK: {
485
+ if (stack.length < 2) {
486
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
487
+ }
488
+ const vch = stacktop(stack, -1);
489
+ stack.splice(stack.length - 2, 0, vch);
490
+ return opcodeSuccess();
491
+ }
492
+ case opcode_1.Opcode.OP_SIZE: {
493
+ if (stack.length < 1) {
494
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
495
+ }
496
+ const bn = new script_num_1.ScriptNum(BigInt(stacktop(stack, -1).length));
497
+ stack.push(bn.toBuffer());
498
+ return opcodeSuccess();
499
+ }
500
+ case opcode_1.Opcode.OP_AND:
501
+ case opcode_1.Opcode.OP_OR:
502
+ case opcode_1.Opcode.OP_XOR: {
503
+ if (stack.length < 2) {
504
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
505
+ }
506
+ const a = stacktop(stack, -2);
507
+ const b = stacktop(stack, -1);
508
+ const longer = a.length >= b.length ? a : b;
509
+ const shorter = a.length >= b.length ? b : a;
510
+ const result = longer;
511
+ if (opcode === opcode_1.Opcode.OP_AND) {
512
+ for (let i = 0; i < result.length; i++) {
513
+ result[i] = i < shorter.length ? result[i] & shorter[i] : 0;
514
+ }
515
+ }
516
+ else if (opcode === opcode_1.Opcode.OP_OR) {
517
+ for (let i = 0; i < shorter.length; i++) {
518
+ result[i] |= shorter[i];
519
+ }
520
+ }
521
+ else {
522
+ for (let i = 0; i < shorter.length; i++) {
523
+ result[i] ^= shorter[i];
524
+ }
525
+ }
526
+ const depthShorter = a.length >= b.length ? -1 : -2;
527
+ stack.splice(stack.length + depthShorter, 1);
528
+ stackset(stack, -1, result);
529
+ return opcodeSuccess();
530
+ }
531
+ case opcode_1.Opcode.OP_EQUAL:
532
+ case opcode_1.Opcode.OP_EQUALVERIFY: {
533
+ if (stack.length < 2) {
534
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
535
+ }
536
+ const a2 = stacktop(stack, -2);
537
+ const b2 = stacktop(stack, -1);
538
+ const fEqual = a2.equals(b2);
539
+ popstack(stack);
540
+ popstack(stack);
541
+ stack.push(fEqual ? TRUE_BUFFER : FALSE_BUFFER);
542
+ if (opcode === opcode_1.Opcode.OP_EQUALVERIFY) {
543
+ if (fEqual) {
544
+ popstack(stack);
545
+ }
546
+ else {
547
+ return opcodeFailure(types_1.ScriptError.EQUALVERIFY);
548
+ }
549
+ }
550
+ return opcodeSuccess();
551
+ }
552
+ case opcode_1.Opcode.OP_1ADD:
553
+ case opcode_1.Opcode.OP_1SUB:
554
+ case opcode_1.Opcode.OP_NEGATE:
555
+ case opcode_1.Opcode.OP_ABS:
556
+ case opcode_1.Opcode.OP_NOT:
557
+ case opcode_1.Opcode.OP_0NOTEQUAL: {
558
+ if (stack.length < 1) {
559
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
560
+ }
561
+ let bn = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true);
562
+ const ONE = new script_num_1.ScriptNum(1n);
563
+ switch (opcode) {
564
+ case opcode_1.Opcode.OP_1ADD:
565
+ bn = bn.add(ONE);
566
+ break;
567
+ case opcode_1.Opcode.OP_1SUB:
568
+ bn = bn.sub(ONE);
569
+ break;
570
+ case opcode_1.Opcode.OP_NEGATE:
571
+ bn = bn.negate();
572
+ break;
573
+ case opcode_1.Opcode.OP_ABS:
574
+ bn = bn.abs();
575
+ break;
576
+ case opcode_1.Opcode.OP_NOT:
577
+ bn = new script_num_1.ScriptNum(bn.isZero() ? 1n : 0n);
578
+ break;
579
+ case opcode_1.Opcode.OP_0NOTEQUAL:
580
+ bn = new script_num_1.ScriptNum(bn.isZero() ? 0n : 1n);
581
+ break;
582
+ }
583
+ popstack(stack);
584
+ stack.push(bn.toBuffer());
585
+ return opcodeSuccess();
586
+ }
587
+ case opcode_1.Opcode.OP_ADD:
588
+ case opcode_1.Opcode.OP_SUB:
589
+ case opcode_1.Opcode.OP_DIV:
590
+ case opcode_1.Opcode.OP_MOD:
591
+ case opcode_1.Opcode.OP_MULPOW2:
592
+ case opcode_1.Opcode.OP_BOOLAND:
593
+ case opcode_1.Opcode.OP_BOOLOR:
594
+ case opcode_1.Opcode.OP_LESSTHAN:
595
+ case opcode_1.Opcode.OP_GREATERTHAN:
596
+ case opcode_1.Opcode.OP_LESSTHANOREQUAL:
597
+ case opcode_1.Opcode.OP_GREATERTHANOREQUAL:
598
+ case opcode_1.Opcode.OP_MIN:
599
+ case opcode_1.Opcode.OP_MAX: {
600
+ if (stack.length < 2) {
601
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
602
+ }
603
+ const bn1 = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -2), true);
604
+ const bn2 = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true);
605
+ let bnResult;
606
+ switch (opcode) {
607
+ case opcode_1.Opcode.OP_ADD:
608
+ bnResult = bn1.add(bn2);
609
+ break;
610
+ case opcode_1.Opcode.OP_SUB:
611
+ bnResult = bn1.sub(bn2);
612
+ break;
613
+ case opcode_1.Opcode.OP_DIV:
614
+ if (bn2.isZero()) {
615
+ return opcodeFailure(types_1.ScriptError.DIV_BY_ZERO);
616
+ }
617
+ bnResult = bn1.div(bn2);
618
+ break;
619
+ case opcode_1.Opcode.OP_MOD:
620
+ if (bn2.isZero()) {
621
+ return opcodeFailure(types_1.ScriptError.MOD_BY_ZERO);
622
+ }
623
+ bnResult = bn1.mod(bn2);
624
+ break;
625
+ case opcode_1.Opcode.OP_MULPOW2:
626
+ bnResult = bn1.mulpow2(bn2);
627
+ break;
628
+ case opcode_1.Opcode.OP_BOOLAND:
629
+ bnResult = new script_num_1.ScriptNum(!bn1.isZero() && !bn2.isZero() ? 1n : 0n);
630
+ break;
631
+ case opcode_1.Opcode.OP_BOOLOR:
632
+ bnResult = new script_num_1.ScriptNum(!bn1.isZero() || !bn2.isZero() ? 1n : 0n);
633
+ break;
634
+ case opcode_1.Opcode.OP_LESSTHAN:
635
+ bnResult = new script_num_1.ScriptNum(bn1.lt(bn2) ? 1n : 0n);
636
+ break;
637
+ case opcode_1.Opcode.OP_GREATERTHAN:
638
+ bnResult = new script_num_1.ScriptNum(bn1.gt(bn2) ? 1n : 0n);
639
+ break;
640
+ case opcode_1.Opcode.OP_LESSTHANOREQUAL:
641
+ bnResult = new script_num_1.ScriptNum(bn1.lte(bn2) ? 1n : 0n);
642
+ break;
643
+ case opcode_1.Opcode.OP_GREATERTHANOREQUAL:
644
+ bnResult = new script_num_1.ScriptNum(bn1.gte(bn2) ? 1n : 0n);
645
+ break;
646
+ case opcode_1.Opcode.OP_MIN:
647
+ bnResult = bn1.lt(bn2) ? bn1 : bn2;
648
+ break;
649
+ case opcode_1.Opcode.OP_MAX:
650
+ bnResult = bn1.gt(bn2) ? bn1 : bn2;
651
+ break;
652
+ default:
653
+ return opcodeFailure(types_1.ScriptError.BAD_OPCODE);
654
+ }
655
+ popstack(stack);
656
+ popstack(stack);
657
+ stack.push(bnResult.toBuffer());
658
+ return opcodeSuccess();
659
+ }
660
+ case opcode_1.Opcode.OP_WITHIN: {
661
+ if (stack.length < 3) {
662
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
663
+ }
664
+ const x = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -3), true);
665
+ const min = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -2), true);
666
+ const max = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true);
667
+ const fValue = min.lte(x) && x.lt(max);
668
+ popstack(stack);
669
+ popstack(stack);
670
+ popstack(stack);
671
+ stack.push(fValue ? TRUE_BUFFER : FALSE_BUFFER);
672
+ return opcodeSuccess();
673
+ }
674
+ case opcode_1.Opcode.OP_RIPEMD160:
675
+ case opcode_1.Opcode.OP_SHA256:
676
+ case opcode_1.Opcode.OP_HASH160:
677
+ case opcode_1.Opcode.OP_HASH256: {
678
+ if (stack.length < 1) {
679
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
680
+ }
681
+ const data = stacktop(stack, -1);
682
+ let hashResult;
683
+ switch (opcode) {
684
+ case opcode_1.Opcode.OP_RIPEMD160:
685
+ hashResult = hash_1.Hash.ripemd160(data);
686
+ break;
687
+ case opcode_1.Opcode.OP_SHA256:
688
+ hashResult = hash_1.Hash.sha256(data);
689
+ break;
690
+ case opcode_1.Opcode.OP_HASH160:
691
+ hashResult = hash_1.Hash.sha256ripemd160(data);
692
+ break;
693
+ case opcode_1.Opcode.OP_HASH256:
694
+ hashResult = hash_1.Hash.sha256sha256(data);
695
+ break;
696
+ default:
697
+ return opcodeFailure(types_1.ScriptError.BAD_OPCODE);
698
+ }
699
+ popstack(stack);
700
+ stack.push(hashResult);
701
+ return opcodeSuccess();
702
+ }
703
+ case opcode_1.Opcode.OP_CODESEPARATOR: {
704
+ execdata.codeseparatorPos = opcodePos;
705
+ return opcodeSuccess({ codeHashBegin: currentOffset });
706
+ }
707
+ case opcode_1.Opcode.OP_CHECKSIG:
708
+ case opcode_1.Opcode.OP_CHECKSIGVERIFY: {
709
+ if (stack.length < 2) {
710
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
711
+ }
712
+ const vchSig = stacktop(stack, -2);
713
+ const vchPubKey = stacktop(stack, -1);
714
+ let fSuccess = false;
715
+ if (vchSig.length > 0) {
716
+ const scriptCode = scriptBuf.slice(codeHashBegin);
717
+ fSuccess = checker.checkSignature(vchSig, vchPubKey, execdata, scriptCode, flags);
718
+ metrics.sigChecks += 1;
719
+ if (!fSuccess) {
720
+ return opcodeFailure(types_1.ScriptError.SIG_NULLFAIL);
721
+ }
722
+ }
723
+ popstack(stack);
724
+ popstack(stack);
725
+ stack.push(fSuccess ? TRUE_BUFFER : FALSE_BUFFER);
726
+ if (opcode === opcode_1.Opcode.OP_CHECKSIGVERIFY) {
727
+ if (fSuccess) {
728
+ popstack(stack);
729
+ }
730
+ else {
731
+ return opcodeFailure(types_1.ScriptError.CHECKSIGVERIFY);
732
+ }
733
+ }
734
+ return opcodeSuccess();
735
+ }
736
+ case opcode_1.Opcode.OP_CHECKDATASIG:
737
+ case opcode_1.Opcode.OP_CHECKDATASIGVERIFY: {
738
+ if (stack.length < 3) {
739
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
740
+ }
741
+ const cdsSig = stacktop(stack, -3);
742
+ const cdsMsg = stacktop(stack, -2);
743
+ const cdsPubKey = stacktop(stack, -1);
744
+ let cdSuccess = false;
745
+ if (cdsSig.length > 0) {
746
+ const msgHash = hash_1.Hash.sha256(cdsMsg);
747
+ cdSuccess = checker.verifySignature(cdsSig, cdsPubKey, msgHash);
748
+ metrics.sigChecks += 1;
749
+ if (!cdSuccess) {
750
+ return opcodeFailure(types_1.ScriptError.SIG_NULLFAIL);
751
+ }
752
+ }
753
+ popstack(stack);
754
+ popstack(stack);
755
+ popstack(stack);
756
+ stack.push(cdSuccess ? TRUE_BUFFER : FALSE_BUFFER);
757
+ if (opcode === opcode_1.Opcode.OP_CHECKDATASIGVERIFY) {
758
+ if (cdSuccess) {
759
+ popstack(stack);
760
+ }
761
+ else {
762
+ return opcodeFailure(types_1.ScriptError.CHECKDATASIGVERIFY);
763
+ }
764
+ }
765
+ return opcodeSuccess();
766
+ }
767
+ case opcode_1.Opcode.OP_CHECKMULTISIG:
768
+ case opcode_1.Opcode.OP_CHECKMULTISIGVERIFY: {
769
+ return executeCheckmultisig(opcode, stack, flags, checker, metrics, execdata, scriptBuf, codeHashBegin, currentOffset, nOpCount);
770
+ }
771
+ case opcode_1.Opcode.OP_CAT: {
772
+ if (stack.length < 2) {
773
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
774
+ }
775
+ const catA = stacktop(stack, -2);
776
+ const catB = stacktop(stack, -1);
777
+ if (catA.length + catB.length > types_1.MAX_SCRIPT_ELEMENT_SIZE) {
778
+ return opcodeFailure(types_1.ScriptError.PUSH_SIZE);
779
+ }
780
+ const catResult = buffer_1.BufferUtil.concat([catA, catB]);
781
+ popstack(stack);
782
+ stackset(stack, -1, catResult);
783
+ return opcodeSuccess();
784
+ }
785
+ case opcode_1.Opcode.OP_SPLIT: {
786
+ if (stack.length < 2) {
787
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
788
+ }
789
+ const splitData = stacktop(stack, -2);
790
+ const pos = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true).toNumber();
791
+ if (pos < 0 || pos > splitData.length) {
792
+ return opcodeFailure(types_1.ScriptError.INVALID_SPLIT_RANGE);
793
+ }
794
+ const n1 = splitData.slice(0, pos);
795
+ const n2 = splitData.slice(pos);
796
+ stackset(stack, -2, n1);
797
+ stackset(stack, -1, n2);
798
+ return opcodeSuccess();
799
+ }
800
+ case opcode_1.Opcode.OP_REVERSEBYTES: {
801
+ if (stack.length < 1) {
802
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
803
+ }
804
+ const revData = stacktop(stack, -1);
805
+ revData.reverse();
806
+ stackset(stack, -1, revData);
807
+ return opcodeSuccess();
808
+ }
809
+ case opcode_1.Opcode.OP_NUM2BIN: {
810
+ if (stack.length < 2) {
811
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
812
+ }
813
+ const targetSize = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true).toNumber();
814
+ if (targetSize > types_1.MAX_SCRIPT_ELEMENT_SIZE) {
815
+ return opcodeFailure(types_1.ScriptError.PUSH_SIZE);
816
+ }
817
+ if (targetSize > types_1.MAX_NUM2BIN_SIZE) {
818
+ return opcodeFailure(types_1.ScriptError.INVALID_NUM2BIN_SIZE);
819
+ }
820
+ popstack(stack);
821
+ let rawnum = stacktop(stack, -1);
822
+ rawnum = script_num_1.ScriptNum.minimallyEncode(rawnum);
823
+ if (rawnum.length > targetSize) {
824
+ return opcodeFailure(types_1.ScriptError.IMPOSSIBLE_ENCODING);
825
+ }
826
+ if (rawnum.length === targetSize) {
827
+ stackset(stack, -1, rawnum);
828
+ return opcodeSuccess();
829
+ }
830
+ let signbit = 0x00;
831
+ if (rawnum.length > 0) {
832
+ signbit = rawnum[rawnum.length - 1] & 0x80;
833
+ rawnum[rawnum.length - 1] &= 0x7f;
834
+ }
835
+ const padded = buffer_1.BufferUtil.alloc(targetSize, 0);
836
+ rawnum.copy(padded);
837
+ padded[targetSize - 1] = signbit;
838
+ stackset(stack, -1, padded);
839
+ return opcodeSuccess();
840
+ }
841
+ case opcode_1.Opcode.OP_BIN2NUM: {
842
+ if (stack.length < 1) {
843
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
844
+ }
845
+ const encoded = script_num_1.ScriptNum.minimallyEncode(stacktop(stack, -1));
846
+ if (!script_num_1.ScriptNum.isMinimallyEncoded(encoded)) {
847
+ return opcodeFailure(types_1.ScriptError.INVALID_NUMBER_RANGE);
848
+ }
849
+ stackset(stack, -1, encoded);
850
+ return opcodeSuccess();
851
+ }
852
+ case opcode_1.Opcode.OP_RAWLEFTBITSHIFT: {
853
+ if (stack.length < 2) {
854
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
855
+ }
856
+ const shiftData = stacktop(stack, -2);
857
+ const signedBitshift = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true).toNumber();
858
+ popstack(stack);
859
+ if (shiftData.length === 0 || signedBitshift === 0) {
860
+ stackset(stack, -1, shiftData);
861
+ return opcodeSuccess();
862
+ }
863
+ let bitshift = Math.abs(signedBitshift);
864
+ if (bitshift > shiftData.length * 8) {
865
+ bitshift = shiftData.length * 8;
866
+ }
867
+ const innerBitshift = bitshift % 8;
868
+ const byteshift = Math.floor(bitshift / 8);
869
+ if (signedBitshift > 0) {
870
+ const mask = 0xff >> innerBitshift;
871
+ for (let idx = byteshift; idx < shiftData.length; idx++) {
872
+ const bits = shiftData[idx];
873
+ const off = idx - byteshift;
874
+ const removeMask = mask << innerBitshift;
875
+ shiftData[off] &= ~removeMask & 0xff;
876
+ shiftData[off] |= (bits & mask) << innerBitshift;
877
+ if (off > 0) {
878
+ const removeMaskCarry = (~mask & 0xff) >> (8 - innerBitshift);
879
+ shiftData[off - 1] &= ~removeMaskCarry & 0xff;
880
+ shiftData[off - 1] |= (bits & ~mask & 0xff) >> (8 - innerBitshift);
881
+ }
882
+ }
883
+ if (byteshift < shiftData.length) {
884
+ shiftData[shiftData.length - byteshift - 1] &=
885
+ (0xff << innerBitshift) & 0xff;
886
+ }
887
+ for (let i = shiftData.length - byteshift; i < shiftData.length; i++) {
888
+ shiftData[i] = 0;
889
+ }
890
+ }
891
+ else {
892
+ const mask = (0xff << innerBitshift) & 0xff;
893
+ for (let idx = shiftData.length - byteshift - 1; idx >= 0; idx--) {
894
+ const bits = shiftData[idx];
895
+ const off = idx + byteshift;
896
+ const removeMask = (mask >> innerBitshift) & 0xff;
897
+ shiftData[off] &= ~removeMask & 0xff;
898
+ shiftData[off] |= (bits & mask) >> innerBitshift;
899
+ if (off + 1 < shiftData.length) {
900
+ const removeMaskCarry = ((~mask & 0xff) << (8 - innerBitshift)) & 0xff;
901
+ shiftData[off + 1] &= ~removeMaskCarry & 0xff;
902
+ shiftData[off + 1] |=
903
+ ((bits & ~mask & 0xff) << (8 - innerBitshift)) & 0xff;
904
+ }
905
+ }
906
+ for (let i = 0; i < byteshift; i++) {
907
+ shiftData[i] = 0;
908
+ }
909
+ if (byteshift < shiftData.length) {
910
+ shiftData[byteshift] &= (0xff >> innerBitshift) & 0xff;
911
+ }
912
+ }
913
+ stackset(stack, -1, shiftData);
914
+ return opcodeSuccess();
915
+ }
916
+ default:
917
+ return opcodeFailure(types_1.ScriptError.BAD_OPCODE);
918
+ }
919
+ }
920
+ function executeCheckmultisig(opcode, stack, flags, checker, metrics, execdata, scriptBuf, codeHashBegin, _currentOffset, nOpCount) {
921
+ if (stack.length < 1) {
922
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
923
+ }
924
+ const nKeysCount = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -1), true).toNumber();
925
+ if (nKeysCount < 0 || nKeysCount > types_1.MAX_PUBKEYS_PER_MULTISIG) {
926
+ return opcodeFailure(types_1.ScriptError.PUBKEY_COUNT);
927
+ }
928
+ let updatedOpCount = nOpCount + nKeysCount;
929
+ if (updatedOpCount > types_1.MAX_OPS_PER_SCRIPT) {
930
+ return opcodeFailure(types_1.ScriptError.OP_COUNT);
931
+ }
932
+ const idxTopKey = 2;
933
+ const idxSigCount = idxTopKey + nKeysCount;
934
+ if (stack.length < idxSigCount) {
935
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
936
+ }
937
+ const nSigsCount = script_num_1.ScriptNum.fromBuffer(stacktop(stack, -idxSigCount), true).toNumber();
938
+ if (nSigsCount < 0 || nSigsCount > nKeysCount) {
939
+ return opcodeFailure(types_1.ScriptError.SIG_COUNT);
940
+ }
941
+ const idxTopSig = idxSigCount + 1;
942
+ const idxDummy = idxTopSig + nSigsCount;
943
+ if (stack.length < idxDummy) {
944
+ return opcodeFailure(types_1.ScriptError.INVALID_STACK_OPERATION);
945
+ }
946
+ const scriptCode = scriptBuf.slice(codeHashBegin);
947
+ let fSuccess = true;
948
+ const dummyElement = stacktop(stack, -idxDummy);
949
+ if (dummyElement.length !== 0) {
950
+ let checkBits = 0;
951
+ for (let i = 0; i < dummyElement.length; i++) {
952
+ checkBits |= dummyElement[i] << (8 * i);
953
+ }
954
+ let bitCount = 0;
955
+ let tmp = checkBits;
956
+ while (tmp) {
957
+ bitCount += tmp & 1;
958
+ tmp >>>= 1;
959
+ }
960
+ if (bitCount !== nSigsCount) {
961
+ return opcodeFailure(types_1.ScriptError.INVALID_BIT_COUNT);
962
+ }
963
+ const idxBottomKey = idxTopKey + nKeysCount - 1;
964
+ const idxBottomSig = idxTopSig + nSigsCount - 1;
965
+ let iKey = 0;
966
+ for (let iSig = 0; iSig < nSigsCount; iSig++, iKey++) {
967
+ if (checkBits >>> iKey === 0) {
968
+ return opcodeFailure(types_1.ScriptError.INVALID_BIT_RANGE);
969
+ }
970
+ while (((checkBits >>> iKey) & 0x01) === 0) {
971
+ iKey++;
972
+ }
973
+ if (iKey >= nKeysCount) {
974
+ return opcodeFailure(types_1.ScriptError.PUBKEY_COUNT);
975
+ }
976
+ const vchSig = stacktop(stack, -idxBottomSig + iSig);
977
+ const vchPubKey = stacktop(stack, -idxBottomKey + iKey);
978
+ if (!checker.checkSignature(vchSig, vchPubKey, execdata, scriptCode, flags)) {
979
+ return opcodeFailure(types_1.ScriptError.SIG_NULLFAIL);
980
+ }
981
+ metrics.sigChecks += 1;
982
+ }
983
+ if (checkBits >>> iKey !== 0) {
984
+ return opcodeFailure(types_1.ScriptError.INVALID_BIT_COUNT);
985
+ }
986
+ }
987
+ else {
988
+ let nSigsRemaining = nSigsCount;
989
+ let nKeysRemaining = nKeysCount;
990
+ while (fSuccess && nSigsRemaining > 0) {
991
+ const vchSig = stacktop(stack, -idxTopSig - (nSigsCount - nSigsRemaining));
992
+ const vchPubKey = stacktop(stack, -idxTopKey - (nKeysCount - nKeysRemaining));
993
+ const fOk = checker.checkSignature(vchSig, vchPubKey, execdata, scriptCode, flags);
994
+ if (fOk) {
995
+ nSigsRemaining--;
996
+ }
997
+ nKeysRemaining--;
998
+ if (nSigsRemaining > nKeysRemaining) {
999
+ fSuccess = false;
1000
+ }
1001
+ }
1002
+ let allSigsNull = true;
1003
+ for (let i = 0; i < nSigsCount; i++) {
1004
+ if (stacktop(stack, -idxTopSig - i).length > 0) {
1005
+ allSigsNull = false;
1006
+ break;
1007
+ }
1008
+ }
1009
+ if (!fSuccess && !allSigsNull) {
1010
+ return opcodeFailure(types_1.ScriptError.SIG_NULLFAIL);
1011
+ }
1012
+ if (!allSigsNull) {
1013
+ metrics.sigChecks += nKeysCount;
1014
+ }
1015
+ }
1016
+ for (let i = 0; i < idxDummy; i++) {
1017
+ popstack(stack);
1018
+ }
1019
+ stack.push(fSuccess ? TRUE_BUFFER : FALSE_BUFFER);
1020
+ if (opcode === opcode_1.Opcode.OP_CHECKMULTISIGVERIFY) {
1021
+ if (fSuccess) {
1022
+ popstack(stack);
1023
+ }
1024
+ else {
1025
+ return opcodeFailure(types_1.ScriptError.CHECKMULTISIGVERIFY);
1026
+ }
1027
+ }
1028
+ return opcodeSuccess({ nOpCount: updatedOpCount });
1029
+ }
1030
+ function isPayToTaproot(scriptPubKey) {
1031
+ if (scriptPubKey.length < taproot_1.TAPROOT_SIZE_WITHOUT_STATE) {
1032
+ return false;
1033
+ }
1034
+ if (scriptPubKey[0] !== opcode_1.Opcode.OP_SCRIPTTYPE ||
1035
+ scriptPubKey[1] !== taproot_1.TAPROOT_SCRIPTTYPE) {
1036
+ return false;
1037
+ }
1038
+ if (scriptPubKey[2] !== 33) {
1039
+ return false;
1040
+ }
1041
+ if (scriptPubKey.length === taproot_1.TAPROOT_SIZE_WITHOUT_STATE) {
1042
+ return true;
1043
+ }
1044
+ return (scriptPubKey.length === taproot_1.TAPROOT_SIZE_WITH_STATE &&
1045
+ scriptPubKey[taproot_1.TAPROOT_SIZE_WITHOUT_STATE] === 32);
1046
+ }
1047
+ function checkTaprootKeySpendSigEncoding(sig) {
1048
+ if (sig.length === 0) {
1049
+ return null;
1050
+ }
1051
+ if (sig.length !== 65) {
1052
+ return types_1.ScriptError.SIG_BADLENGTH;
1053
+ }
1054
+ const hashtype = sig[sig.length - 1];
1055
+ if ((hashtype & crypto_1.Signature.SIGHASH_ALGORITHM_MASK) !==
1056
+ crypto_1.Signature.SIGHASH_LOTUS) {
1057
+ return types_1.ScriptError.TAPROOT_KEY_SPEND_MUST_USE_LOTUS_SIGHASH;
1058
+ }
1059
+ return null;
1060
+ }
1061
+ function checkPubKeyEncoding(pubkey) {
1062
+ if (pubkey.length === 33) {
1063
+ if (pubkey[0] === 0x02 || pubkey[0] === 0x03) {
1064
+ return null;
1065
+ }
1066
+ }
1067
+ else if (pubkey.length === 65) {
1068
+ if (pubkey[0] === 0x04) {
1069
+ return null;
1070
+ }
1071
+ }
1072
+ return types_1.ScriptError.PUBKEYTYPE;
1073
+ }
1074
+ function verifyTaprootSpend(stack, scriptSig, scriptPubKey, flags, checker, metrics) {
1075
+ if (flags & types_1.ScriptFlags.DISABLE_TAPROOT_SIGHASH_LOTUS) {
1076
+ return failure(types_1.ScriptError.TAPROOT_PHASEOUT);
1077
+ }
1078
+ if (!isPayToTaproot(scriptPubKey)) {
1079
+ return failure(types_1.ScriptError.SCRIPTTYPE_MALFORMED_SCRIPT);
1080
+ }
1081
+ const vchPubkey = scriptPubKey.slice(taproot_1.TAPROOT_INTRO_SIZE, taproot_1.TAPROOT_SIZE_WITHOUT_STATE);
1082
+ if (stack.length === 0) {
1083
+ return failure(types_1.ScriptError.INVALID_STACK_OPERATION);
1084
+ }
1085
+ if (stack.length >= 2 &&
1086
+ stack[stack.length - 1].length > 0 &&
1087
+ stack[stack.length - 1][0] === taproot_1.TAPROOT_ANNEX_TAG) {
1088
+ return failure(types_1.ScriptError.TAPROOT_ANNEX_NOT_SUPPORTED);
1089
+ }
1090
+ if (stack.length === 1) {
1091
+ const vchSig = stack[stack.length - 1];
1092
+ const sigFlags = flags | types_1.ScriptFlags.TAPROOT_KEY_SPEND_PATH;
1093
+ const sigErr = checkTaprootKeySpendSigEncoding(vchSig);
1094
+ if (sigErr !== null) {
1095
+ return failure(sigErr);
1096
+ }
1097
+ const pkErr = checkPubKeyEncoding(vchPubkey);
1098
+ if (pkErr !== null) {
1099
+ return failure(pkErr);
1100
+ }
1101
+ if (vchSig.length === 0 ||
1102
+ !checker.checkSignature(vchSig, vchPubkey, undefined, buffer_1.BufferUtil.alloc(0), sigFlags)) {
1103
+ return failure(types_1.ScriptError.TAPROOT_VERIFY_SIGNATURE_FAILED);
1104
+ }
1105
+ metrics.sigChecks += 1;
1106
+ return success();
1107
+ }
1108
+ const controlBlock = stack[stack.length - 1];
1109
+ const scriptBytes = stack[stack.length - 2];
1110
+ const execScript = scriptBytes;
1111
+ stack.pop();
1112
+ stack.pop();
1113
+ const sizeRemainder = (controlBlock.length - taproot_1.TAPROOT_CONTROL_BASE_SIZE) %
1114
+ taproot_1.TAPROOT_CONTROL_NODE_SIZE;
1115
+ if (controlBlock.length < taproot_1.TAPROOT_CONTROL_BASE_SIZE ||
1116
+ controlBlock.length > taproot_1.TAPROOT_CONTROL_MAX_SIZE ||
1117
+ sizeRemainder !== 0) {
1118
+ return failure(types_1.ScriptError.TAPROOT_WRONG_CONTROL_SIZE);
1119
+ }
1120
+ if ((controlBlock[0] & taproot_1.TAPROOT_LEAF_MASK) !== taproot_1.TAPROOT_LEAF_TAPSCRIPT) {
1121
+ return failure(types_1.ScriptError.TAPROOT_LEAF_VERSION_NOT_SUPPORTED);
1122
+ }
1123
+ const commitResult = (0, taproot_1.verifyTaprootCommitment)(controlBlock, vchPubkey, script_1.Script.fromBuffer(execScript));
1124
+ if (!commitResult.success) {
1125
+ return failure(types_1.ScriptError.TAPROOT_VERIFY_COMMITMENT_FAILED);
1126
+ }
1127
+ if (scriptPubKey.length === taproot_1.TAPROOT_SIZE_WITH_STATE) {
1128
+ const stateStart = taproot_1.TAPROOT_SIZE_WITHOUT_STATE + 1;
1129
+ const stateEnd = taproot_1.TAPROOT_SIZE_WITH_STATE;
1130
+ stack.push(scriptPubKey.slice(stateStart, stateEnd));
1131
+ }
1132
+ const taprootExecdata = {
1133
+ codeseparatorPos: 0xffffffff,
1134
+ executedScriptHash: commitResult.tapleafHash,
1135
+ };
1136
+ const evalResult = evalScript(stack, execScript, flags, checker, metrics, taprootExecdata);
1137
+ if (!evalResult.success) {
1138
+ return evalResult;
1139
+ }
1140
+ if (stack.length === 0 || !castToBool(stack[stack.length - 1])) {
1141
+ return failure(types_1.ScriptError.EVAL_FALSE);
1142
+ }
1143
+ return verifyPostConditions(stack, scriptSig, flags, metrics);
1144
+ }
1145
+ function verifyScriptType(stack, scriptSig, scriptPubKey, flags, checker, metrics) {
1146
+ if (scriptPubKey.length === 1) {
1147
+ return failure(types_1.ScriptError.SCRIPTTYPE_MALFORMED_SCRIPT);
1148
+ }
1149
+ if (scriptPubKey[1] === taproot_1.TAPROOT_SCRIPTTYPE) {
1150
+ return verifyTaprootSpend(stack, scriptSig, scriptPubKey, flags, checker, metrics);
1151
+ }
1152
+ return failure(types_1.ScriptError.SCRIPTTYPE_INVALID_TYPE);
1153
+ }
1154
+ function verifyPostConditions(stack, scriptSig, flags, metrics) {
1155
+ if (flags & types_1.ScriptFlags.VERIFY_CLEANSTACK) {
1156
+ if (stack.length !== 1) {
1157
+ return failure(types_1.ScriptError.CLEANSTACK);
1158
+ }
1159
+ }
1160
+ if (flags & types_1.ScriptFlags.VERIFY_INPUT_SIGCHECKS) {
1161
+ if (scriptSig.length < metrics.sigChecks * 43 - 60) {
1162
+ return failure(types_1.ScriptError.INPUT_SIGCHECKS);
1163
+ }
1164
+ }
1165
+ return success();
1166
+ }
1167
+ function verifyScript(scriptSig, scriptPubKey, flags, checker) {
1168
+ if (!isPushOnly(scriptSig)) {
1169
+ return failure(types_1.ScriptError.SIG_PUSHONLY);
1170
+ }
1171
+ const metrics = { sigChecks: 0 };
1172
+ const stack = [];
1173
+ const sigExecdata = makeExecdata(buffer_1.BufferUtil.alloc(0));
1174
+ const sigResult = evalScript(stack, scriptSig, flags, checker, metrics, sigExecdata);
1175
+ if (!sigResult.success) {
1176
+ return sigResult;
1177
+ }
1178
+ if (scriptPubKey.length > 0 && scriptPubKey[0] === opcode_1.Opcode.OP_SCRIPTTYPE) {
1179
+ const stResult = verifyScriptType(stack, scriptSig, scriptPubKey, flags, checker, metrics);
1180
+ return stResult;
1181
+ }
1182
+ const stackCopy = stack.map(el => el.slice());
1183
+ const pubkeyExecdata = makeExecdata(scriptPubKey);
1184
+ const pubkeyResult = evalScript(stack, scriptPubKey, flags, checker, metrics, pubkeyExecdata);
1185
+ if (!pubkeyResult.success) {
1186
+ return pubkeyResult;
1187
+ }
1188
+ if (stack.length === 0) {
1189
+ return failure(types_1.ScriptError.EVAL_FALSE);
1190
+ }
1191
+ if (!castToBool(stack[stack.length - 1])) {
1192
+ return failure(types_1.ScriptError.EVAL_FALSE);
1193
+ }
1194
+ if (isPayToScriptHash(scriptPubKey)) {
1195
+ if (!isPushOnly(scriptSig)) {
1196
+ return failure(types_1.ScriptError.SIG_PUSHONLY);
1197
+ }
1198
+ const p2shStack = stackCopy;
1199
+ if (p2shStack.length === 0) {
1200
+ return failure(types_1.ScriptError.EVAL_FALSE);
1201
+ }
1202
+ const serializedScript = p2shStack[p2shStack.length - 1];
1203
+ p2shStack.pop();
1204
+ const p2shExecdata = makeExecdata(serializedScript);
1205
+ const p2shResult = evalScript(p2shStack, serializedScript, flags, checker, metrics, p2shExecdata);
1206
+ if (!p2shResult.success) {
1207
+ return p2shResult;
1208
+ }
1209
+ if (p2shStack.length === 0) {
1210
+ return failure(types_1.ScriptError.EVAL_FALSE);
1211
+ }
1212
+ if (!castToBool(p2shStack[p2shStack.length - 1])) {
1213
+ return failure(types_1.ScriptError.EVAL_FALSE);
1214
+ }
1215
+ stack.length = 0;
1216
+ stack.push(...p2shStack);
1217
+ }
1218
+ return verifyPostConditions(stack, scriptSig, flags, metrics);
1219
+ }
1220
+ function isPushOnly(scriptBuf) {
1221
+ let offset = 0;
1222
+ while (offset < scriptBuf.length) {
1223
+ const instr = readInstruction(scriptBuf, offset);
1224
+ if (!instr) {
1225
+ return false;
1226
+ }
1227
+ if (instr.opcode > opcode_1.Opcode.OP_16) {
1228
+ return false;
1229
+ }
1230
+ offset = instr.nextOffset;
1231
+ }
1232
+ return true;
1233
+ }
1234
+ function isPayToScriptHash(scriptBuf) {
1235
+ return (scriptBuf.length === 23 &&
1236
+ scriptBuf[0] === opcode_1.Opcode.OP_HASH160 &&
1237
+ scriptBuf[1] === 0x14 &&
1238
+ scriptBuf[22] === opcode_1.Opcode.OP_EQUAL);
1239
+ }
1240
+ function makeExecdata(script) {
1241
+ return {
1242
+ codeseparatorPos: 0xffffffff,
1243
+ executedScriptHash: hash_1.Hash.sha256sha256(script.length > 0 ? script : buffer_1.BufferUtil.alloc(0)),
1244
+ };
1245
+ }
1246
+ class Interpreter {
1247
+ errstr = '';
1248
+ static SCRIPT_VERIFY_NONE = types_1.ScriptFlags.VERIFY_NONE;
1249
+ static SCRIPT_TAPROOT_KEY_SPEND_PATH = types_1.ScriptFlags.TAPROOT_KEY_SPEND_PATH;
1250
+ static SCRIPT_DISABLE_TAPROOT_SIGHASH_LOTUS = types_1.ScriptFlags.DISABLE_TAPROOT_SIGHASH_LOTUS;
1251
+ static SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = types_1.ScriptFlags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS;
1252
+ static SCRIPT_VERIFY_CLEANSTACK = types_1.ScriptFlags.VERIFY_CLEANSTACK;
1253
+ static SCRIPT_VERIFY_MINIMALIF = types_1.ScriptFlags.VERIFY_MINIMALIF;
1254
+ static SCRIPT_ENABLE_SIGHASH_FORKID = types_1.ScriptFlags.ENABLE_SIGHASH_FORKID;
1255
+ static SCRIPT_ENABLE_REPLAY_PROTECTION = types_1.ScriptFlags.ENABLE_REPLAY_PROTECTION;
1256
+ static SCRIPT_VERIFY_INPUT_SIGCHECKS = types_1.ScriptFlags.VERIFY_INPUT_SIGCHECKS;
1257
+ verify(scriptSig, scriptPubKey, tx, nin, flags, satoshis) {
1258
+ this.errstr = '';
1259
+ const sigBuf = scriptSig.toBuffer();
1260
+ const pubkeyBuf = scriptPubKey.toBuffer();
1261
+ const checker = new TransactionSignatureChecker(tx, nin, satoshis);
1262
+ const result = verifyScript(sigBuf, pubkeyBuf, flags, checker);
1263
+ if (!result.success) {
1264
+ this.errstr = result.error;
1265
+ }
1266
+ return result.success;
1267
+ }
1268
+ }
1269
+ exports.Interpreter = Interpreter;
1270
+ class TransactionSignatureChecker {
1271
+ tx;
1272
+ nin;
1273
+ satoshis;
1274
+ constructor(tx, nin, satoshis) {
1275
+ this.tx = tx;
1276
+ this.nin = nin;
1277
+ this.satoshis = satoshis;
1278
+ }
1279
+ verifySignature(signature, publicKey, sighash) {
1280
+ try {
1281
+ const pubkey = publickey_1.PublicKey.fromBuffer(publicKey);
1282
+ if ((0, crypto_1.isSchnorrSignature)(signature)) {
1283
+ const sig = crypto_1.Signature.fromSchnorr(signature);
1284
+ return crypto_1.Schnorr.verify(sighash, sig, pubkey);
1285
+ }
1286
+ else {
1287
+ const sig = crypto_1.Signature.fromDER(signature);
1288
+ const ecdsa = new crypto_1.ECDSA();
1289
+ ecdsa.hashbuf = sighash;
1290
+ ecdsa.sig = sig;
1291
+ ecdsa.pubkey = pubkey;
1292
+ ecdsa.verify();
1293
+ return ecdsa.verified;
1294
+ }
1295
+ }
1296
+ catch {
1297
+ return false;
1298
+ }
1299
+ }
1300
+ checkSignature(signature, publicKey, execdata, scriptCode, flags) {
1301
+ if (signature.length === 0) {
1302
+ return false;
1303
+ }
1304
+ try {
1305
+ const hashtype = signature[signature.length - 1];
1306
+ const rawSig = signature.slice(0, signature.length - 1);
1307
+ const subscript = script_1.Script.fromBuffer(scriptCode);
1308
+ const sighashBuf = (0, sighash_1.sighash)(this.tx, hashtype, this.nin, subscript, this.satoshis !== undefined
1309
+ ? crypto_1.BN.fromNumber(Number(this.satoshis))
1310
+ : undefined, flags);
1311
+ return this.verifySignature(rawSig, publicKey, sighashBuf);
1312
+ }
1313
+ catch {
1314
+ return false;
1315
+ }
1316
+ }
1317
+ checkLockTime(lockTime) {
1318
+ const LOCKTIME_THRESHOLD = 500000000n;
1319
+ const txLockTime = BigInt(this.tx.nLockTime || 0);
1320
+ if ((txLockTime < LOCKTIME_THRESHOLD && lockTime >= LOCKTIME_THRESHOLD) ||
1321
+ (txLockTime >= LOCKTIME_THRESHOLD && lockTime < LOCKTIME_THRESHOLD)) {
1322
+ return false;
1323
+ }
1324
+ if (lockTime > txLockTime) {
1325
+ return false;
1326
+ }
1327
+ const input = this.tx.inputs?.[this.nin];
1328
+ if (input && input.sequenceNumber === 0xffffffff) {
1329
+ return false;
1330
+ }
1331
+ return true;
1332
+ }
1333
+ checkSequence(sequence) {
1334
+ const SEQUENCE_LOCKTIME_DISABLE_FLAG = 1n << 31n;
1335
+ const SEQUENCE_LOCKTIME_TYPE_FLAG = 1n << 22n;
1336
+ const SEQUENCE_LOCKTIME_MASK = 0x0000ffffn;
1337
+ if (sequence & SEQUENCE_LOCKTIME_DISABLE_FLAG) {
1338
+ return true;
1339
+ }
1340
+ const txVersion = this.tx.version || 1;
1341
+ if (txVersion < 2) {
1342
+ return false;
1343
+ }
1344
+ const input = this.tx.inputs?.[this.nin];
1345
+ if (!input) {
1346
+ return false;
1347
+ }
1348
+ const txSequence = BigInt(input.sequenceNumber || 0);
1349
+ if (txSequence & SEQUENCE_LOCKTIME_DISABLE_FLAG) {
1350
+ return false;
1351
+ }
1352
+ if ((sequence & SEQUENCE_LOCKTIME_TYPE_FLAG) !==
1353
+ (txSequence & SEQUENCE_LOCKTIME_TYPE_FLAG)) {
1354
+ return false;
1355
+ }
1356
+ if ((sequence & SEQUENCE_LOCKTIME_MASK) >
1357
+ (txSequence & SEQUENCE_LOCKTIME_MASK)) {
1358
+ return false;
1359
+ }
1360
+ return true;
1361
+ }
1362
+ }