xpi-ts 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/dist/cjs/lib/bitcore/address.js +75 -74
  2. package/dist/cjs/lib/bitcore/block/block.js +124 -126
  3. package/dist/cjs/lib/bitcore/block/blockheader.js +142 -208
  4. package/dist/cjs/lib/bitcore/block/index.js +3 -1
  5. package/dist/cjs/lib/bitcore/block/merkleblock.js +301 -0
  6. package/dist/cjs/lib/bitcore/crypto/bn.js +18 -22
  7. package/dist/cjs/lib/bitcore/crypto/ecdsa.js +50 -49
  8. package/dist/cjs/lib/bitcore/crypto/hash.js +25 -22
  9. package/dist/cjs/lib/bitcore/crypto/musig2.js +86 -92
  10. package/dist/cjs/lib/bitcore/crypto/point.js +42 -35
  11. package/dist/cjs/lib/bitcore/crypto/random.js +3 -2
  12. package/dist/cjs/lib/bitcore/crypto/schnorr.js +46 -55
  13. package/dist/cjs/lib/bitcore/crypto/signature.js +67 -55
  14. package/dist/cjs/lib/bitcore/crypto/sigtype.js +1 -1
  15. package/dist/cjs/lib/bitcore/encoding/base58.js +5 -4
  16. package/dist/cjs/lib/bitcore/encoding/base58check.js +14 -57
  17. package/dist/cjs/lib/bitcore/encoding/bufferreader.js +17 -17
  18. package/dist/cjs/lib/bitcore/encoding/bufferwriter.js +22 -26
  19. package/dist/cjs/lib/bitcore/encoding/varint.js +12 -11
  20. package/dist/cjs/lib/bitcore/errors.js +1 -0
  21. package/dist/cjs/lib/bitcore/hdprivatekey.js +74 -66
  22. package/dist/cjs/lib/bitcore/hdpublickey.js +96 -116
  23. package/dist/cjs/lib/bitcore/index.js +165 -172
  24. package/dist/cjs/lib/bitcore/message.js +36 -35
  25. package/dist/cjs/lib/bitcore/mnemonic/mnemonic.js +4 -3
  26. package/dist/cjs/lib/bitcore/mnemonic/pbkdf2.js +6 -5
  27. package/dist/cjs/lib/bitcore/mnemonic/words/french.js +2053 -0
  28. package/dist/cjs/lib/bitcore/mnemonic/words/index.js +2 -0
  29. package/dist/cjs/lib/bitcore/musig2/session.js +26 -25
  30. package/dist/cjs/lib/bitcore/musig2/signer.js +17 -16
  31. package/dist/cjs/lib/bitcore/networks.js +4 -3
  32. package/dist/cjs/lib/bitcore/opcode.js +11 -7
  33. package/dist/cjs/lib/bitcore/privatekey.js +42 -42
  34. package/dist/cjs/lib/bitcore/publickey.js +61 -57
  35. package/dist/cjs/lib/bitcore/script/interpreter/condition-stack.js +41 -0
  36. package/dist/cjs/lib/bitcore/script/interpreter/index.js +25 -0
  37. package/dist/cjs/lib/bitcore/script/interpreter/interpreter.js +1362 -0
  38. package/dist/cjs/lib/bitcore/script/interpreter/script-num.js +200 -0
  39. package/dist/cjs/lib/bitcore/script/interpreter/types.js +96 -0
  40. package/dist/cjs/lib/bitcore/{taproot.js → script/taproot.js} +107 -95
  41. package/dist/cjs/lib/bitcore/script.js +163 -104
  42. package/dist/cjs/lib/bitcore/taproot/musig2.js +51 -42
  43. package/dist/cjs/lib/bitcore/transaction/index.js +21 -21
  44. package/dist/cjs/lib/bitcore/transaction/input/multisig.js +10 -0
  45. package/dist/cjs/lib/bitcore/transaction/input/multisigscripthash.js +10 -0
  46. package/dist/cjs/lib/bitcore/transaction/input/publickey.js +10 -0
  47. package/dist/cjs/lib/bitcore/transaction/input/publickeyhash.js +10 -0
  48. package/dist/cjs/lib/bitcore/transaction/input/taproot.js +20 -0
  49. package/dist/cjs/lib/bitcore/transaction/input.js +145 -166
  50. package/dist/cjs/lib/bitcore/transaction/outpoint.js +89 -0
  51. package/dist/cjs/lib/bitcore/transaction/output.js +13 -8
  52. package/dist/cjs/lib/bitcore/transaction/sighash.js +96 -96
  53. package/dist/cjs/lib/bitcore/transaction/signature.js +39 -40
  54. package/dist/cjs/lib/bitcore/transaction/transaction.js +130 -118
  55. package/dist/cjs/lib/bitcore/transaction/unspentoutput.js +27 -23
  56. package/dist/cjs/lib/bitcore/util/bits.js +33 -0
  57. package/dist/cjs/lib/bitcore/util/buffer.js +29 -18
  58. package/dist/cjs/lib/bitcore/util/js.js +0 -3
  59. package/dist/cjs/lib/bitcore/util/merkle.js +53 -0
  60. package/dist/cjs/lib/bitcore/util/preconditions.js +8 -7
  61. package/dist/cjs/lib/bitcore/xaddress.js +27 -23
  62. package/dist/cjs/lib/{bitcore/taproot/nft.js → nft.js} +98 -29
  63. package/dist/cjs/lib/rank/index.js +28 -27
  64. package/dist/cjs/lib/rank/script.js +2 -1
  65. package/dist/cjs/lib/rank/transaction.js +2 -1
  66. package/dist/cjs/utils/string.js +6 -5
  67. package/dist/esm/lib/bitcore/address.js +42 -41
  68. package/dist/esm/lib/bitcore/block/block.js +126 -128
  69. package/dist/esm/lib/bitcore/block/blockheader.js +141 -207
  70. package/dist/esm/lib/bitcore/block/index.js +1 -0
  71. package/dist/esm/lib/bitcore/block/merkleblock.js +295 -0
  72. package/dist/esm/lib/bitcore/crypto/bn.js +18 -22
  73. package/dist/esm/lib/bitcore/crypto/ecdsa.js +25 -24
  74. package/dist/esm/lib/bitcore/crypto/hash.js +25 -22
  75. package/dist/esm/lib/bitcore/crypto/musig2.js +60 -66
  76. package/dist/esm/lib/bitcore/crypto/point.js +22 -15
  77. package/dist/esm/lib/bitcore/crypto/random.js +3 -2
  78. package/dist/esm/lib/bitcore/crypto/schnorr.js +35 -44
  79. package/dist/esm/lib/bitcore/crypto/signature.js +61 -49
  80. package/dist/esm/lib/bitcore/crypto/sigtype.js +1 -1
  81. package/dist/esm/lib/bitcore/encoding/base58.js +5 -4
  82. package/dist/esm/lib/bitcore/encoding/base58check.js +11 -57
  83. package/dist/esm/lib/bitcore/encoding/bufferreader.js +7 -7
  84. package/dist/esm/lib/bitcore/encoding/bufferwriter.js +18 -21
  85. package/dist/esm/lib/bitcore/encoding/varint.js +7 -6
  86. package/dist/esm/lib/bitcore/errors.js +1 -0
  87. package/dist/esm/lib/bitcore/hdprivatekey.js +47 -39
  88. package/dist/esm/lib/bitcore/hdpublickey.js +77 -97
  89. package/dist/esm/lib/bitcore/index.js +42 -45
  90. package/dist/esm/lib/bitcore/message.js +16 -15
  91. package/dist/esm/lib/bitcore/mnemonic/mnemonic.js +4 -3
  92. package/dist/esm/lib/bitcore/mnemonic/pbkdf2.js +5 -4
  93. package/dist/esm/lib/bitcore/mnemonic/words/french.js +2050 -0
  94. package/dist/esm/lib/bitcore/mnemonic/words/index.js +2 -0
  95. package/dist/esm/lib/bitcore/musig2/session.js +17 -16
  96. package/dist/esm/lib/bitcore/musig2/signer.js +19 -18
  97. package/dist/esm/lib/bitcore/networks.js +4 -3
  98. package/dist/esm/lib/bitcore/opcode.js +9 -5
  99. package/dist/esm/lib/bitcore/privatekey.js +25 -25
  100. package/dist/esm/lib/bitcore/publickey.js +46 -42
  101. package/dist/esm/lib/bitcore/script/interpreter/condition-stack.js +37 -0
  102. package/dist/esm/lib/bitcore/script/interpreter/index.js +4 -0
  103. package/dist/esm/lib/bitcore/script/interpreter/interpreter.js +1356 -0
  104. package/dist/esm/lib/bitcore/script/interpreter/script-num.js +195 -0
  105. package/dist/esm/lib/bitcore/script/interpreter/types.js +93 -0
  106. package/dist/esm/lib/bitcore/{taproot.js → script/taproot.js} +92 -81
  107. package/dist/esm/lib/bitcore/script.js +163 -102
  108. package/dist/esm/lib/bitcore/taproot/musig2.js +36 -27
  109. package/dist/esm/lib/bitcore/transaction/index.js +6 -6
  110. package/dist/esm/lib/bitcore/transaction/input/multisig.js +6 -0
  111. package/dist/esm/lib/bitcore/transaction/input/multisigscripthash.js +6 -0
  112. package/dist/esm/lib/bitcore/transaction/input/publickey.js +6 -0
  113. package/dist/esm/lib/bitcore/transaction/input/publickeyhash.js +6 -0
  114. package/dist/esm/lib/bitcore/transaction/input/taproot.js +16 -0
  115. package/dist/esm/lib/bitcore/transaction/input.js +55 -76
  116. package/dist/esm/lib/bitcore/transaction/outpoint.js +85 -0
  117. package/dist/esm/lib/bitcore/transaction/output.js +13 -8
  118. package/dist/esm/lib/bitcore/transaction/sighash.js +22 -22
  119. package/dist/esm/lib/bitcore/transaction/signature.js +16 -17
  120. package/dist/esm/lib/bitcore/transaction/transaction.js +58 -46
  121. package/dist/esm/lib/bitcore/transaction/unspentoutput.js +15 -11
  122. package/dist/esm/lib/bitcore/util/bits.js +27 -0
  123. package/dist/esm/lib/bitcore/util/buffer.js +20 -9
  124. package/dist/esm/lib/bitcore/util/js.js +0 -3
  125. package/dist/esm/lib/bitcore/util/merkle.js +47 -0
  126. package/dist/esm/lib/bitcore/util/preconditions.js +3 -2
  127. package/dist/esm/lib/bitcore/xaddress.js +16 -12
  128. package/dist/esm/lib/{bitcore/taproot/nft.js → nft.js} +97 -28
  129. package/dist/esm/lib/rank/index.js +15 -14
  130. package/dist/esm/lib/rank/script.js +1 -0
  131. package/dist/esm/lib/rank/transaction.js +1 -0
  132. package/dist/esm/utils/string.js +1 -0
  133. package/dist/types/lib/bitcore/address.d.ts +4 -3
  134. package/dist/types/lib/bitcore/address.d.ts.map +1 -1
  135. package/dist/types/lib/bitcore/block/block.d.ts +11 -19
  136. package/dist/types/lib/bitcore/block/block.d.ts.map +1 -1
  137. package/dist/types/lib/bitcore/block/blockheader.d.ts +30 -32
  138. package/dist/types/lib/bitcore/block/blockheader.d.ts.map +1 -1
  139. package/dist/types/lib/bitcore/block/index.d.ts +2 -0
  140. package/dist/types/lib/bitcore/block/index.d.ts.map +1 -1
  141. package/dist/types/lib/bitcore/block/merkleblock.d.ts +53 -0
  142. package/dist/types/lib/bitcore/block/merkleblock.d.ts.map +1 -0
  143. package/dist/types/lib/bitcore/crypto/bn.d.ts +2 -2
  144. package/dist/types/lib/bitcore/crypto/bn.d.ts.map +1 -1
  145. package/dist/types/lib/bitcore/crypto/ecdsa.d.ts +5 -4
  146. package/dist/types/lib/bitcore/crypto/ecdsa.d.ts.map +1 -1
  147. package/dist/types/lib/bitcore/crypto/hash.d.ts +3 -1
  148. package/dist/types/lib/bitcore/crypto/hash.d.ts.map +1 -1
  149. package/dist/types/lib/bitcore/crypto/musig2.d.ts +22 -30
  150. package/dist/types/lib/bitcore/crypto/musig2.d.ts.map +1 -1
  151. package/dist/types/lib/bitcore/crypto/point.d.ts +8 -3
  152. package/dist/types/lib/bitcore/crypto/point.d.ts.map +1 -1
  153. package/dist/types/lib/bitcore/crypto/random.d.ts +1 -0
  154. package/dist/types/lib/bitcore/crypto/random.d.ts.map +1 -1
  155. package/dist/types/lib/bitcore/crypto/schnorr.d.ts +5 -4
  156. package/dist/types/lib/bitcore/crypto/schnorr.d.ts.map +1 -1
  157. package/dist/types/lib/bitcore/crypto/signature.d.ts +4 -1
  158. package/dist/types/lib/bitcore/crypto/signature.d.ts.map +1 -1
  159. package/dist/types/lib/bitcore/crypto/sigtype.d.ts +1 -0
  160. package/dist/types/lib/bitcore/crypto/sigtype.d.ts.map +1 -1
  161. package/dist/types/lib/bitcore/encoding/base58.d.ts +1 -0
  162. package/dist/types/lib/bitcore/encoding/base58.d.ts.map +1 -1
  163. package/dist/types/lib/bitcore/encoding/base58check.d.ts +1 -2
  164. package/dist/types/lib/bitcore/encoding/base58check.d.ts.map +1 -1
  165. package/dist/types/lib/bitcore/encoding/bufferreader.d.ts +2 -1
  166. package/dist/types/lib/bitcore/encoding/bufferreader.d.ts.map +1 -1
  167. package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts +2 -2
  168. package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -1
  169. package/dist/types/lib/bitcore/encoding/varint.d.ts +3 -2
  170. package/dist/types/lib/bitcore/encoding/varint.d.ts.map +1 -1
  171. package/dist/types/lib/bitcore/errors.d.ts +3 -0
  172. package/dist/types/lib/bitcore/errors.d.ts.map +1 -1
  173. package/dist/types/lib/bitcore/hdprivatekey.d.ts +5 -4
  174. package/dist/types/lib/bitcore/hdprivatekey.d.ts.map +1 -1
  175. package/dist/types/lib/bitcore/hdpublickey.d.ts +4 -3
  176. package/dist/types/lib/bitcore/hdpublickey.d.ts.map +1 -1
  177. package/dist/types/lib/bitcore/index.d.ts +55 -60
  178. package/dist/types/lib/bitcore/index.d.ts.map +1 -1
  179. package/dist/types/lib/bitcore/message.d.ts +7 -4
  180. package/dist/types/lib/bitcore/message.d.ts.map +1 -1
  181. package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts +2 -0
  182. package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -1
  183. package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts +1 -0
  184. package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -1
  185. package/dist/types/lib/bitcore/mnemonic/words/french.d.ts +2 -0
  186. package/dist/types/lib/bitcore/mnemonic/words/french.d.ts.map +1 -0
  187. package/dist/types/lib/bitcore/mnemonic/words/index.d.ts +1 -0
  188. package/dist/types/lib/bitcore/mnemonic/words/index.d.ts.map +1 -1
  189. package/dist/types/lib/bitcore/musig2/session.d.ts +8 -7
  190. package/dist/types/lib/bitcore/musig2/session.d.ts.map +1 -1
  191. package/dist/types/lib/bitcore/musig2/signer.d.ts +10 -9
  192. package/dist/types/lib/bitcore/musig2/signer.d.ts.map +1 -1
  193. package/dist/types/lib/bitcore/networks.d.ts +1 -0
  194. package/dist/types/lib/bitcore/networks.d.ts.map +1 -1
  195. package/dist/types/lib/bitcore/opcode.d.ts +5 -0
  196. package/dist/types/lib/bitcore/opcode.d.ts.map +1 -1
  197. package/dist/types/lib/bitcore/privatekey.d.ts +5 -4
  198. package/dist/types/lib/bitcore/privatekey.d.ts.map +1 -1
  199. package/dist/types/lib/bitcore/publickey.d.ts +13 -6
  200. package/dist/types/lib/bitcore/publickey.d.ts.map +1 -1
  201. package/dist/types/lib/bitcore/{chunk.d.ts → script/chunk.d.ts} +1 -0
  202. package/dist/types/lib/bitcore/script/chunk.d.ts.map +1 -0
  203. package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts +11 -0
  204. package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts.map +1 -0
  205. package/dist/types/lib/bitcore/script/interpreter/index.d.ts +5 -0
  206. package/dist/types/lib/bitcore/script/interpreter/index.d.ts.map +1 -0
  207. package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts +21 -0
  208. package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts.map +1 -0
  209. package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts +35 -0
  210. package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts.map +1 -0
  211. package/dist/types/lib/bitcore/script/interpreter/types.d.ts +101 -0
  212. package/dist/types/lib/bitcore/script/interpreter/types.d.ts.map +1 -0
  213. package/dist/types/lib/bitcore/{taproot.d.ts → script/taproot.d.ts} +20 -7
  214. package/dist/types/lib/bitcore/script/taproot.d.ts.map +1 -0
  215. package/dist/types/lib/bitcore/script.d.ts +12 -11
  216. package/dist/types/lib/bitcore/script.d.ts.map +1 -1
  217. package/dist/types/lib/bitcore/taproot/musig2.d.ts +17 -16
  218. package/dist/types/lib/bitcore/taproot/musig2.d.ts.map +1 -1
  219. package/dist/types/lib/bitcore/transaction/index.d.ts +11 -11
  220. package/dist/types/lib/bitcore/transaction/index.d.ts.map +1 -1
  221. package/dist/types/lib/bitcore/transaction/input/multisig.d.ts +5 -0
  222. package/dist/types/lib/bitcore/transaction/input/multisig.d.ts.map +1 -0
  223. package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts +5 -0
  224. package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts.map +1 -0
  225. package/dist/types/lib/bitcore/transaction/input/publickey.d.ts +5 -0
  226. package/dist/types/lib/bitcore/transaction/input/publickey.d.ts.map +1 -0
  227. package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts +6 -0
  228. package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts.map +1 -0
  229. package/dist/types/lib/bitcore/transaction/input/taproot.d.ts +18 -0
  230. package/dist/types/lib/bitcore/transaction/input/taproot.d.ts.map +1 -0
  231. package/dist/types/lib/bitcore/transaction/input.d.ts +27 -35
  232. package/dist/types/lib/bitcore/transaction/input.d.ts.map +1 -1
  233. package/dist/types/lib/bitcore/transaction/outpoint.d.ts +32 -0
  234. package/dist/types/lib/bitcore/transaction/outpoint.d.ts.map +1 -0
  235. package/dist/types/lib/bitcore/transaction/output.d.ts +3 -1
  236. package/dist/types/lib/bitcore/transaction/output.d.ts.map +1 -1
  237. package/dist/types/lib/bitcore/transaction/sighash.d.ts +8 -7
  238. package/dist/types/lib/bitcore/transaction/sighash.d.ts.map +1 -1
  239. package/dist/types/lib/bitcore/transaction/signature.d.ts +3 -2
  240. package/dist/types/lib/bitcore/transaction/signature.d.ts.map +1 -1
  241. package/dist/types/lib/bitcore/transaction/transaction.d.ts +18 -15
  242. package/dist/types/lib/bitcore/transaction/transaction.d.ts.map +1 -1
  243. package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts +9 -7
  244. package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -1
  245. package/dist/types/lib/bitcore/unit.d.ts.map +1 -1
  246. package/dist/types/lib/bitcore/util/base32.d.ts.map +1 -1
  247. package/dist/types/lib/bitcore/util/bits.d.ts +5 -0
  248. package/dist/types/lib/bitcore/util/bits.d.ts.map +1 -0
  249. package/dist/types/lib/bitcore/util/buffer.d.ts +10 -2
  250. package/dist/types/lib/bitcore/util/buffer.d.ts.map +1 -1
  251. package/dist/types/lib/bitcore/util/js.d.ts +0 -1
  252. package/dist/types/lib/bitcore/util/js.d.ts.map +1 -1
  253. package/dist/types/lib/bitcore/util/merkle.d.ts +6 -0
  254. package/dist/types/lib/bitcore/util/merkle.d.ts.map +1 -0
  255. package/dist/types/lib/bitcore/util/preconditions.d.ts.map +1 -1
  256. package/dist/types/lib/bitcore/xaddress.d.ts +1 -0
  257. package/dist/types/lib/bitcore/xaddress.d.ts.map +1 -1
  258. package/dist/types/lib/{bitcore/taproot/nft.d.ts → nft.d.ts} +26 -12
  259. package/dist/types/lib/nft.d.ts.map +1 -0
  260. package/dist/types/lib/rank/index.d.ts +1 -0
  261. package/dist/types/lib/rank/index.d.ts.map +1 -1
  262. package/dist/types/lib/rank/script.d.ts +1 -0
  263. package/dist/types/lib/rank/script.d.ts.map +1 -1
  264. package/dist/types/lib/rank/transaction.d.ts +1 -0
  265. package/dist/types/lib/rank/transaction.d.ts.map +1 -1
  266. package/dist/types/utils/string.d.ts +1 -0
  267. package/dist/types/utils/string.d.ts.map +1 -1
  268. package/package.json +3 -2
  269. package/dist/cjs/lib/bitcore/script/interpreter.js +0 -1757
  270. package/dist/esm/lib/bitcore/script/interpreter.js +0 -1753
  271. package/dist/types/lib/bitcore/chunk.d.ts.map +0 -1
  272. package/dist/types/lib/bitcore/script/interpreter.d.ts +0 -101
  273. package/dist/types/lib/bitcore/script/interpreter.d.ts.map +0 -1
  274. package/dist/types/lib/bitcore/taproot/nft.d.ts.map +0 -1
  275. package/dist/types/lib/bitcore/taproot.d.ts.map +0 -1
  276. /package/dist/cjs/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
  277. /package/dist/esm/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
@@ -1,1757 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Interpreter = void 0;
4
- const script_js_1 = require("../script.js");
5
- const opcode_js_1 = require("../opcode.js");
6
- const bn_js_1 = require("../crypto/bn.js");
7
- const hash_js_1 = require("../crypto/hash.js");
8
- const signature_js_1 = require("../crypto/signature.js");
9
- const publickey_js_1 = require("../publickey.js");
10
- const ecdsa_js_1 = require("../crypto/ecdsa.js");
11
- const schnorr_js_1 = require("../crypto/schnorr.js");
12
- const preconditions_js_1 = require("../util/preconditions.js");
13
- const taproot_js_1 = require("../taproot.js");
14
- class Interpreter {
15
- static SCRIPT_VERIFY_NONE = 0;
16
- static SCRIPT_VERIFY_P2SH = 1 << 0;
17
- static SCRIPT_VERIFY_STRICTENC = 1 << 1;
18
- static SCRIPT_VERIFY_DERSIG = 1 << 2;
19
- static SCRIPT_VERIFY_LOW_S = 1 << 3;
20
- static SCRIPT_VERIFY_NULLDUMMY = 1 << 4;
21
- static SCRIPT_VERIFY_SIGPUSHONLY = 1 << 5;
22
- static SCRIPT_VERIFY_MINIMALDATA = 1 << 6;
23
- static SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = 1 << 7;
24
- static SCRIPT_VERIFY_CLEANSTACK = 1 << 8;
25
- static SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = 1 << 9;
26
- static SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = 1 << 10;
27
- static SCRIPT_VERIFY_MINIMALIF = 1 << 13;
28
- static SCRIPT_VERIFY_NULLFAIL = 1 << 14;
29
- static SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE = 1 << 15;
30
- static SCRIPT_ENABLE_SIGHASH_FORKID = 1 << 16;
31
- static SCRIPT_ENABLE_REPLAY_PROTECTION = 1 << 17;
32
- static SCRIPT_ENABLE_CHECKDATASIG = 1 << 18;
33
- static SCRIPT_DISALLOW_SEGWIT_RECOVERY = 1 << 20;
34
- static SCRIPT_ENABLE_SCHNORR_MULTISIG = 1 << 21;
35
- static SCRIPT_VERIFY_INPUT_SIGCHECKS = 1 << 22;
36
- static SCRIPT_TAPROOT_KEY_SPEND_PATH = 1 << 23;
37
- static SCRIPT_DISABLE_TAPROOT_SIGHASH_LOTUS = 1 << 24;
38
- static MAX_SCRIPT_ELEMENT_SIZE = 520;
39
- static MAX_SCRIPT_SIZE = 10000;
40
- static MAX_STACK_SIZE = 1000;
41
- static MAX_OPCODE_COUNT = 201;
42
- static false = Buffer.from([0]);
43
- static true = Buffer.from([1]);
44
- static MAXIMUM_ELEMENT_SIZE = 4;
45
- static LOCKTIME_THRESHOLD = 500000000;
46
- static LOCKTIME_THRESHOLD_BN = new bn_js_1.BN(500000000);
47
- static SEQUENCE_LOCKTIME_DISABLE_FLAG = 1 << 31;
48
- static SEQUENCE_LOCKTIME_TYPE_FLAG = 1 << 22;
49
- static SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
50
- script;
51
- tx;
52
- nin;
53
- flags;
54
- satoshisBN;
55
- outputScript;
56
- stack = [];
57
- altstack = [];
58
- pc = 0;
59
- pbegincodehash = 0;
60
- nOpCount = 0;
61
- vfExec = [];
62
- errstr = '';
63
- constructor(obj) {
64
- this.initialize();
65
- if (obj) {
66
- this.set(obj);
67
- }
68
- }
69
- static create(obj) {
70
- return new Interpreter(obj);
71
- }
72
- initialize() {
73
- this.stack = [];
74
- this.altstack = [];
75
- this.pc = 0;
76
- this.pbegincodehash = 0;
77
- this.nOpCount = 0;
78
- this.vfExec = [];
79
- this.errstr = '';
80
- this.flags = Interpreter.SCRIPT_VERIFY_NONE;
81
- }
82
- set(obj) {
83
- this.script = obj.script || this.script;
84
- this.tx = obj.tx || this.tx;
85
- this.nin = obj.nin !== undefined ? obj.nin : this.nin;
86
- this.flags = obj.flags !== undefined ? obj.flags : this.flags;
87
- this.satoshisBN = obj.satoshisBN || this.satoshisBN;
88
- this.outputScript = obj.outputScript || this.outputScript;
89
- this.stack = obj.stack || this.stack;
90
- return this;
91
- }
92
- verify(scriptSig, scriptPubkey, tx, nin, flags, satoshisBN) {
93
- preconditions_js_1.Preconditions.checkArgument(scriptSig instanceof script_js_1.Script, 'scriptSig', 'Must be a Script');
94
- preconditions_js_1.Preconditions.checkArgument(scriptPubkey instanceof script_js_1.Script, 'scriptPubkey', 'Must be a Script');
95
- if (flags && (flags & Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID) !== 0) {
96
- flags |= Interpreter.SCRIPT_VERIFY_STRICTENC;
97
- if (!satoshisBN) {
98
- throw new Error('internal error - need satoshisBN to verify FORKID transactions');
99
- }
100
- }
101
- this.set({
102
- script: scriptSig,
103
- tx: tx,
104
- nin: nin,
105
- flags: flags,
106
- satoshisBN: satoshisBN,
107
- });
108
- let stackCopy = [];
109
- if ((this.flags & Interpreter.SCRIPT_VERIFY_SIGPUSHONLY) !== 0) {
110
- if (!scriptSig.isPushOnly()) {
111
- this.errstr = 'SCRIPT_ERR_SIG_PUSHONLY';
112
- return false;
113
- }
114
- }
115
- if (!this.evaluate()) {
116
- return false;
117
- }
118
- if (this.flags & Interpreter.SCRIPT_VERIFY_P2SH) {
119
- stackCopy = this.stack.slice();
120
- }
121
- const stack = this.stack;
122
- this.initialize();
123
- this.script = scriptPubkey;
124
- this.stack = stack;
125
- this.tx = tx;
126
- this.nin = nin;
127
- this.flags = flags || Interpreter.SCRIPT_VERIFY_NONE;
128
- this.satoshisBN = satoshisBN;
129
- const scriptPubkeyBuf = scriptPubkey.toBuffer();
130
- if (scriptPubkeyBuf.length > 0 &&
131
- scriptPubkeyBuf[0] === opcode_js_1.Opcode.OP_SCRIPTTYPE) {
132
- if (!this._verifyScriptType(scriptPubkey)) {
133
- return false;
134
- }
135
- return true;
136
- }
137
- if (!this.evaluate()) {
138
- return false;
139
- }
140
- if (this.stack.length === 0) {
141
- this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_RESULT';
142
- return false;
143
- }
144
- const buf = this.stack[this.stack.length - 1];
145
- if (!Interpreter.castToBool(buf)) {
146
- this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_STACK';
147
- return false;
148
- }
149
- if (this.flags & Interpreter.SCRIPT_VERIFY_P2SH &&
150
- scriptPubkey.isScriptHashOut()) {
151
- if (!scriptSig.isPushOnly()) {
152
- this.errstr = 'SCRIPT_ERR_SIG_PUSHONLY';
153
- return false;
154
- }
155
- if (stackCopy.length === 0) {
156
- throw new Error('internal error - stack copy empty');
157
- }
158
- const redeemScriptSerialized = stackCopy[stackCopy.length - 1];
159
- const redeemScript = script_js_1.Script.fromBuffer(redeemScriptSerialized);
160
- stackCopy.pop();
161
- this.initialize();
162
- this.script = redeemScript;
163
- this.stack = stackCopy;
164
- this.tx = tx;
165
- this.nin = nin;
166
- this.flags = flags || Interpreter.SCRIPT_VERIFY_NONE;
167
- this.satoshisBN = satoshisBN;
168
- if (!this.evaluate()) {
169
- return false;
170
- }
171
- if (stackCopy.length === 0) {
172
- this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_P2SH_STACK';
173
- return false;
174
- }
175
- if (!Interpreter.castToBool(stackCopy[stackCopy.length - 1])) {
176
- this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_P2SH_STACK';
177
- return false;
178
- }
179
- }
180
- if ((this.flags & Interpreter.SCRIPT_VERIFY_CLEANSTACK) != 0) {
181
- if ((this.flags & Interpreter.SCRIPT_VERIFY_P2SH) == 0) {
182
- throw new Error('internal error - CLEANSTACK without P2SH');
183
- }
184
- if (stackCopy.length != 1) {
185
- this.errstr = 'SCRIPT_ERR_CLEANSTACK';
186
- return false;
187
- }
188
- }
189
- return true;
190
- }
191
- checkRawSignatureEncoding(buf) {
192
- if (buf.length === 0) {
193
- return true;
194
- }
195
- if (Interpreter.isSchnorrSig(buf)) {
196
- return this.checkRawSchnorrSignatureEncoding(buf);
197
- }
198
- if ((this.flags &
199
- (Interpreter.SCRIPT_VERIFY_DERSIG |
200
- Interpreter.SCRIPT_VERIFY_LOW_S |
201
- Interpreter.SCRIPT_VERIFY_STRICTENC)) !==
202
- 0 &&
203
- !signature_js_1.Signature.isDER(buf)) {
204
- this.errstr = 'SCRIPT_ERR_SIG_DER_INVALID_FORMAT';
205
- return false;
206
- }
207
- else if ((this.flags & Interpreter.SCRIPT_VERIFY_LOW_S) !== 0) {
208
- const sig = signature_js_1.Signature.fromDER(buf, false);
209
- if (!sig.hasLowS()) {
210
- this.errstr = 'SCRIPT_ERR_SIG_DER_HIGH_S';
211
- return false;
212
- }
213
- }
214
- return true;
215
- }
216
- checkRawSchnorrSignatureEncoding(buf) {
217
- if (buf.length !== 64) {
218
- this.errstr = 'SCRIPT_ERR_SIG_NONSCHNORR';
219
- return false;
220
- }
221
- return true;
222
- }
223
- checkTransactionSchnorrSignatureEncoding(buf) {
224
- if (buf.length !== 65) {
225
- this.errstr = 'SCRIPT_ERR_SIG_NONSCHNORR';
226
- return false;
227
- }
228
- if (!this.checkRawSchnorrSignatureEncoding(buf.subarray(0, 64))) {
229
- return false;
230
- }
231
- if (!this.checkSighashEncoding(buf)) {
232
- return false;
233
- }
234
- return true;
235
- }
236
- checkSighashEncoding(buf) {
237
- if (buf.length === 0) {
238
- return true;
239
- }
240
- const hashType = buf[buf.length - 1];
241
- if (hashType === 0x00) {
242
- this.errstr = 'SCRIPT_ERR_SIG_HASHTYPE';
243
- return false;
244
- }
245
- if (this.flags & Interpreter.SCRIPT_TAPROOT_KEY_SPEND_PATH &&
246
- (hashType & 0x60) !== 0x60) {
247
- this.errstr = 'SCRIPT_ERR_TAPROOT_KEY_SPEND_MUST_USE_LOTUS_SIGHASH';
248
- return false;
249
- }
250
- const usesForkId = (hashType & 0x40) !== 0;
251
- const forkIdEnabled = (this.flags & Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID) !== 0;
252
- if (!forkIdEnabled && usesForkId) {
253
- this.errstr = 'SCRIPT_ERR_ILLEGAL_FORKID';
254
- return false;
255
- }
256
- if (forkIdEnabled && !usesForkId) {
257
- this.errstr = 'SCRIPT_ERR_MUST_USE_FORKID';
258
- return false;
259
- }
260
- return true;
261
- }
262
- checkSignatureEncoding(buf) {
263
- if (buf.length === 0) {
264
- return true;
265
- }
266
- try {
267
- const sig = signature_js_1.Signature.fromDER(buf);
268
- if ((this.flags & Interpreter.SCRIPT_VERIFY_LOW_S) !== 0) {
269
- if (sig.s >
270
- new bn_js_1.BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 16)) {
271
- this.errstr = 'SCRIPT_ERR_SIG_DER_HIGH_S';
272
- return false;
273
- }
274
- }
275
- const hashType = buf[buf.length - 1];
276
- if (hashType < 0x80 || hashType > 0x84) {
277
- this.errstr = 'SCRIPT_ERR_SIG_HASHTYPE';
278
- return false;
279
- }
280
- if ((this.flags & Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID) !== 0) {
281
- if ((hashType & 0x40) === 0) {
282
- this.errstr = 'SCRIPT_ERR_ILLEGAL_FORKID';
283
- return false;
284
- }
285
- }
286
- else {
287
- if ((hashType & 0x40) !== 0) {
288
- this.errstr = 'SCRIPT_ERR_MUST_USE_FORKID';
289
- return false;
290
- }
291
- }
292
- return true;
293
- }
294
- catch (e) {
295
- this.errstr = 'SCRIPT_ERR_SIG_DER_INVALID_FORMAT';
296
- return false;
297
- }
298
- }
299
- checkTxSignatureEncoding(buf) {
300
- if (buf.length === 0) {
301
- return true;
302
- }
303
- if (Interpreter.isSchnorrSig(buf)) {
304
- return this.checkTransactionSchnorrSignatureEncoding(buf);
305
- }
306
- if (!this.checkRawSignatureEncoding(buf.subarray(0, buf.length - 1))) {
307
- return false;
308
- }
309
- if ((this.flags & Interpreter.SCRIPT_VERIFY_STRICTENC) !== 0) {
310
- const sig = signature_js_1.Signature.fromTxFormat(buf);
311
- if (!sig.hasDefinedHashtype()) {
312
- this.errstr = 'SCRIPT_ERR_SIG_HASHTYPE';
313
- return false;
314
- }
315
- const isTaprootKeyPath = (this.flags & Interpreter.SCRIPT_TAPROOT_KEY_SPEND_PATH) !== 0;
316
- if (!isTaprootKeyPath) {
317
- if (!(this.flags & Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID) &&
318
- sig.nhashtype & signature_js_1.Signature.SIGHASH_FORKID) {
319
- this.errstr = 'SCRIPT_ERR_ILLEGAL_FORKID';
320
- return false;
321
- }
322
- if (this.flags & Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID &&
323
- !(sig.nhashtype & signature_js_1.Signature.SIGHASH_FORKID)) {
324
- this.errstr = 'SCRIPT_ERR_MUST_USE_FORKID';
325
- return false;
326
- }
327
- }
328
- }
329
- return true;
330
- }
331
- checkDataSignatureEncoding(buf) {
332
- if (buf.length === 0) {
333
- return true;
334
- }
335
- return this.checkRawSignatureEncoding(buf);
336
- }
337
- checkPubkeyEncoding(buf) {
338
- if ((this.flags & Interpreter.SCRIPT_VERIFY_STRICTENC) !== 0) {
339
- if (!publickey_js_1.PublicKey.isValid(buf) &&
340
- !this.isCompressedOrUncompressedPubkey(buf)) {
341
- this.errstr = 'SCRIPT_ERR_PUBKEYTYPE';
342
- return false;
343
- }
344
- }
345
- return true;
346
- }
347
- isCompressedOrUncompressedPubkey(buf) {
348
- if (buf.length === 33) {
349
- return buf[0] === 0x02 || buf[0] === 0x03;
350
- }
351
- if (buf.length === 65) {
352
- return buf[0] === 0x04;
353
- }
354
- return false;
355
- }
356
- evaluate() {
357
- if (this.script.toBuffer().length > Interpreter.MAX_SCRIPT_SIZE) {
358
- this.errstr = 'SCRIPT_ERR_SCRIPT_SIZE';
359
- return false;
360
- }
361
- try {
362
- while (this.pc < this.script.chunks.length) {
363
- if (this.stack.length > Interpreter.MAX_STACK_SIZE) {
364
- this.errstr = 'SCRIPT_ERR_STACK_SIZE';
365
- return false;
366
- }
367
- if (!this.step()) {
368
- return false;
369
- }
370
- }
371
- if (this.stack.length + this.altstack.length >
372
- Interpreter.MAX_STACK_SIZE) {
373
- this.errstr = 'SCRIPT_ERR_STACK_SIZE';
374
- return false;
375
- }
376
- if (this.vfExec.length > 0) {
377
- this.errstr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL';
378
- return false;
379
- }
380
- return true;
381
- }
382
- catch (e) {
383
- this.errstr = 'SCRIPT_ERR_UNKNOWN_ERROR: ' + e.message;
384
- return false;
385
- }
386
- }
387
- toScriptNumBuffer(value) {
388
- const num = typeof value === 'bigint' ? value : BigInt(value);
389
- if (num === 0n) {
390
- return Buffer.alloc(0);
391
- }
392
- const isNegative = num < 0n;
393
- const absNum = isNegative ? -num : num;
394
- const bytes = [];
395
- let temp = absNum;
396
- while (temp > 0n) {
397
- bytes.push(Number(temp & 0xffn));
398
- temp >>= 8n;
399
- }
400
- if (isNegative) {
401
- if (bytes.length > 0 && (bytes[bytes.length - 1] & 0x80) !== 0) {
402
- bytes.push(0x80);
403
- }
404
- else if (bytes.length > 0) {
405
- bytes[bytes.length - 1] |= 0x80;
406
- }
407
- else {
408
- bytes.push(0x80);
409
- }
410
- }
411
- return Buffer.from(bytes);
412
- }
413
- fromScriptNumBuffer(buf) {
414
- if (buf.length === 0) {
415
- return 0n;
416
- }
417
- let result = 0n;
418
- for (let i = 0; i < buf.length; i++) {
419
- result |= BigInt(buf[i]) << BigInt(i * 8);
420
- }
421
- if (buf.length > 0 && (buf[buf.length - 1] & 0x80) !== 0) {
422
- const lastByte = buf[buf.length - 1] & 0x7f;
423
- result =
424
- (result & ~(0xffn << BigInt((buf.length - 1) * 8))) |
425
- (BigInt(lastByte) << BigInt((buf.length - 1) * 8));
426
- result = -result;
427
- }
428
- return result;
429
- }
430
- castToBool(buf) {
431
- for (let i = 0; i < buf.length; i++) {
432
- if (buf[i] !== 0) {
433
- if (i === buf.length - 1 && buf[i] === 0x80) {
434
- return false;
435
- }
436
- return true;
437
- }
438
- }
439
- return false;
440
- }
441
- step() {
442
- if (this.pc >= this.script.chunks.length) {
443
- return true;
444
- }
445
- const chunk = this.script.chunks[this.pc];
446
- this.pc++;
447
- const opcodenum = chunk.opcodenum;
448
- if (opcodenum === undefined) {
449
- this.errstr = 'SCRIPT_ERR_UNDEFINED_OPCODE';
450
- return false;
451
- }
452
- if (chunk.buf && chunk.buf.length > Interpreter.MAX_SCRIPT_ELEMENT_SIZE) {
453
- this.errstr = 'SCRIPT_ERR_PUSH_SIZE';
454
- return false;
455
- }
456
- if (opcodenum > opcode_js_1.Opcode.OP_16 &&
457
- ++this.nOpCount > Interpreter.MAX_OPCODE_COUNT) {
458
- this.errstr = 'SCRIPT_ERR_OP_COUNT';
459
- return false;
460
- }
461
- if (this.isOpcodeDisabled(opcodenum)) {
462
- this.errstr = 'SCRIPT_ERR_DISABLED_OPCODE';
463
- return false;
464
- }
465
- const fRequireMinimal = (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALDATA) !== 0;
466
- const fExec = this.vfExec.indexOf(false) === -1;
467
- if (fExec && opcodenum >= 0 && opcodenum <= opcode_js_1.Opcode.OP_PUSHDATA4) {
468
- if (fRequireMinimal && !this.script.checkMinimalPush(this.pc - 1)) {
469
- this.errstr = 'SCRIPT_ERR_MINIMALDATA';
470
- return false;
471
- }
472
- if (!chunk.buf) {
473
- this.stack.push(Interpreter.false);
474
- }
475
- else if (chunk.len !== chunk.buf.length) {
476
- throw new Error('Length of push value not equal to length of data');
477
- }
478
- else {
479
- this.stack.push(chunk.buf);
480
- }
481
- }
482
- else if (fExec ||
483
- (opcode_js_1.Opcode.OP_IF <= opcodenum && opcodenum <= opcode_js_1.Opcode.OP_ENDIF)) {
484
- switch (opcodenum) {
485
- case opcode_js_1.Opcode.OP_1NEGATE: {
486
- this.stack.push(this.toScriptNumBuffer(-1));
487
- break;
488
- }
489
- case opcode_js_1.Opcode.OP_1:
490
- case opcode_js_1.Opcode.OP_2:
491
- case opcode_js_1.Opcode.OP_3:
492
- case opcode_js_1.Opcode.OP_4:
493
- case opcode_js_1.Opcode.OP_5:
494
- case opcode_js_1.Opcode.OP_6:
495
- case opcode_js_1.Opcode.OP_7:
496
- case opcode_js_1.Opcode.OP_8:
497
- case opcode_js_1.Opcode.OP_9:
498
- case opcode_js_1.Opcode.OP_10:
499
- case opcode_js_1.Opcode.OP_11:
500
- case opcode_js_1.Opcode.OP_12:
501
- case opcode_js_1.Opcode.OP_13:
502
- case opcode_js_1.Opcode.OP_14:
503
- case opcode_js_1.Opcode.OP_15:
504
- case opcode_js_1.Opcode.OP_16: {
505
- const value = opcodenum - opcode_js_1.Opcode.OP_1 + 1;
506
- this.stack.push(this.toScriptNumBuffer(value));
507
- break;
508
- }
509
- case opcode_js_1.Opcode.OP_NOP:
510
- case opcode_js_1.Opcode.OP_NOP1:
511
- case opcode_js_1.Opcode.OP_NOP4:
512
- case opcode_js_1.Opcode.OP_NOP5:
513
- case opcode_js_1.Opcode.OP_NOP6:
514
- case opcode_js_1.Opcode.OP_NOP7:
515
- case opcode_js_1.Opcode.OP_NOP8:
516
- case opcode_js_1.Opcode.OP_NOP9:
517
- case opcode_js_1.Opcode.OP_NOP10: {
518
- if (this.flags & Interpreter.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
519
- this.errstr = 'SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS';
520
- return false;
521
- }
522
- break;
523
- }
524
- case opcode_js_1.Opcode.OP_NOP2:
525
- case opcode_js_1.Opcode.OP_CHECKLOCKTIMEVERIFY: {
526
- if (!(this.flags & Interpreter.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) {
527
- if (this.flags & Interpreter.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
528
- this.errstr = 'SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS';
529
- return false;
530
- }
531
- break;
532
- }
533
- if (this.stack.length < 1) {
534
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
535
- return false;
536
- }
537
- const fRequireMinimal = (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALDATA) !== 0;
538
- const nLockTime = bn_js_1.BN.fromScriptNumBuffer(this.stack[this.stack.length - 1], fRequireMinimal, 5);
539
- if (nLockTime.lt(new bn_js_1.BN(0))) {
540
- this.errstr = 'SCRIPT_ERR_NEGATIVE_LOCKTIME';
541
- return false;
542
- }
543
- if (!this.checkLockTime(nLockTime)) {
544
- this.errstr = 'SCRIPT_ERR_UNSATISFIED_LOCKTIME';
545
- return false;
546
- }
547
- break;
548
- }
549
- case opcode_js_1.Opcode.OP_NOP3:
550
- case opcode_js_1.Opcode.OP_CHECKSEQUENCEVERIFY: {
551
- if (!(this.flags & Interpreter.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) {
552
- if (this.flags & Interpreter.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
553
- this.errstr = 'SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS';
554
- return false;
555
- }
556
- break;
557
- }
558
- if (this.stack.length < 1) {
559
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
560
- return false;
561
- }
562
- const fRequireMinimal = (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALDATA) !== 0;
563
- const nSequence = bn_js_1.BN.fromScriptNumBuffer(this.stacktop(-1), fRequireMinimal, 5);
564
- if (nSequence.lt(new bn_js_1.BN(0))) {
565
- this.errstr = 'SCRIPT_ERR_NEGATIVE_LOCKTIME';
566
- return false;
567
- }
568
- if (!nSequence.and(Interpreter.SEQUENCE_LOCKTIME_DISABLE_FLAG).isZero()) {
569
- break;
570
- }
571
- if (!this.checkSequence(nSequence)) {
572
- this.errstr = 'SCRIPT_ERR_UNSATISFIED_LOCKTIME';
573
- return false;
574
- }
575
- break;
576
- }
577
- case opcode_js_1.Opcode.OP_IF:
578
- case opcode_js_1.Opcode.OP_NOTIF: {
579
- let fValue = false;
580
- if (fExec) {
581
- if (this.stack.length < 1) {
582
- this.errstr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL';
583
- return false;
584
- }
585
- const buf = this.stacktop(-1);
586
- if (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALIF) {
587
- if (buf.length > 1) {
588
- this.errstr = 'SCRIPT_ERR_MINIMALIF';
589
- return false;
590
- }
591
- if (buf.length == 1 && buf[0] != 1) {
592
- this.errstr = 'SCRIPT_ERR_MINIMALIF';
593
- return false;
594
- }
595
- }
596
- fValue = Interpreter.castToBool(buf);
597
- if (opcodenum === opcode_js_1.Opcode.OP_NOTIF) {
598
- fValue = !fValue;
599
- }
600
- this.stack.pop();
601
- }
602
- this.vfExec.push(fValue);
603
- break;
604
- }
605
- case opcode_js_1.Opcode.OP_ELSE: {
606
- if (this.vfExec.length === 0) {
607
- this.errstr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL';
608
- return false;
609
- }
610
- this.vfExec[this.vfExec.length - 1] =
611
- !this.vfExec[this.vfExec.length - 1];
612
- break;
613
- }
614
- case opcode_js_1.Opcode.OP_ENDIF: {
615
- if (this.vfExec.length === 0) {
616
- this.errstr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL';
617
- return false;
618
- }
619
- this.vfExec.pop();
620
- break;
621
- }
622
- case opcode_js_1.Opcode.OP_VERIFY: {
623
- if (this.stack.length < 1) {
624
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
625
- return false;
626
- }
627
- const buf = this.stacktop(-1);
628
- const fValue = Interpreter.castToBool(buf);
629
- if (fValue) {
630
- this.stack.pop();
631
- }
632
- else {
633
- this.errstr = 'SCRIPT_ERR_VERIFY';
634
- return false;
635
- }
636
- break;
637
- }
638
- case opcode_js_1.Opcode.OP_RETURN:
639
- this.errstr = 'SCRIPT_ERR_OP_RETURN';
640
- return false;
641
- case opcode_js_1.Opcode.OP_TOALTSTACK: {
642
- if (this.stack.length < 1) {
643
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
644
- return false;
645
- }
646
- this.altstack.push(this.stack.pop());
647
- break;
648
- }
649
- case opcode_js_1.Opcode.OP_FROMALTSTACK: {
650
- if (this.altstack.length < 1) {
651
- this.errstr = 'SCRIPT_ERR_INVALID_ALTSTACK_OPERATION';
652
- return false;
653
- }
654
- this.stack.push(this.altstack.pop());
655
- break;
656
- }
657
- case opcode_js_1.Opcode.OP_2DROP: {
658
- if (this.stack.length < 2) {
659
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
660
- return false;
661
- }
662
- this.stack.pop();
663
- this.stack.pop();
664
- break;
665
- }
666
- case opcode_js_1.Opcode.OP_2DUP: {
667
- if (this.stack.length < 2) {
668
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
669
- return false;
670
- }
671
- const x1 = this.stack[this.stack.length - 2];
672
- const x2 = this.stack[this.stack.length - 1];
673
- this.stack.push(x1);
674
- this.stack.push(x2);
675
- break;
676
- }
677
- case opcode_js_1.Opcode.OP_3DUP: {
678
- if (this.stack.length < 3) {
679
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
680
- return false;
681
- }
682
- const x1 = this.stack[this.stack.length - 3];
683
- const x2 = this.stack[this.stack.length - 2];
684
- const x3 = this.stack[this.stack.length - 1];
685
- this.stack.push(x1);
686
- this.stack.push(x2);
687
- this.stack.push(x3);
688
- break;
689
- }
690
- case opcode_js_1.Opcode.OP_2OVER: {
691
- if (this.stack.length < 4) {
692
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
693
- return false;
694
- }
695
- const x1 = this.stack[this.stack.length - 4];
696
- const x2 = this.stack[this.stack.length - 3];
697
- this.stack.push(x1);
698
- this.stack.push(x2);
699
- break;
700
- }
701
- case opcode_js_1.Opcode.OP_2ROT: {
702
- if (this.stack.length < 6) {
703
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
704
- return false;
705
- }
706
- const x1 = this.stack.splice(this.stack.length - 6, 1)[0];
707
- const x2 = this.stack.splice(this.stack.length - 5, 1)[0];
708
- this.stack.push(x1);
709
- this.stack.push(x2);
710
- break;
711
- }
712
- case opcode_js_1.Opcode.OP_2SWAP: {
713
- if (this.stack.length < 4) {
714
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
715
- return false;
716
- }
717
- const x1 = this.stack[this.stack.length - 4];
718
- const x2 = this.stack[this.stack.length - 3];
719
- const x3 = this.stack[this.stack.length - 2];
720
- const x4 = this.stack[this.stack.length - 1];
721
- this.stack[this.stack.length - 4] = x3;
722
- this.stack[this.stack.length - 3] = x4;
723
- this.stack[this.stack.length - 2] = x1;
724
- this.stack[this.stack.length - 1] = x2;
725
- break;
726
- }
727
- case opcode_js_1.Opcode.OP_DEPTH: {
728
- this.stack.push(this.toScriptNumBuffer(this.stack.length));
729
- break;
730
- }
731
- case opcode_js_1.Opcode.OP_DROP: {
732
- if (this.stack.length < 1) {
733
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
734
- return false;
735
- }
736
- this.stack.pop();
737
- break;
738
- }
739
- case opcode_js_1.Opcode.OP_DUP: {
740
- if (this.stack.length < 1) {
741
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
742
- return false;
743
- }
744
- this.stack.push(this.stack[this.stack.length - 1]);
745
- break;
746
- }
747
- case opcode_js_1.Opcode.OP_NIP: {
748
- if (this.stack.length < 2) {
749
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
750
- return false;
751
- }
752
- this.stack.splice(this.stack.length - 2, 1);
753
- break;
754
- }
755
- case opcode_js_1.Opcode.OP_OVER: {
756
- if (this.stack.length < 2) {
757
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
758
- return false;
759
- }
760
- this.stack.push(this.stack[this.stack.length - 2]);
761
- break;
762
- }
763
- case opcode_js_1.Opcode.OP_PICK:
764
- case opcode_js_1.Opcode.OP_ROLL: {
765
- if (this.stack.length < 1) {
766
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
767
- return false;
768
- }
769
- const n = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
770
- this.stack.pop();
771
- if (n < 0n || n >= BigInt(this.stack.length)) {
772
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
773
- return false;
774
- }
775
- const val = this.stack[this.stack.length - 1 - Number(n)];
776
- if (opcodenum === opcode_js_1.Opcode.OP_ROLL) {
777
- this.stack.splice(this.stack.length - 1 - Number(n), 1);
778
- }
779
- this.stack.push(val);
780
- break;
781
- }
782
- case opcode_js_1.Opcode.OP_ROT: {
783
- if (this.stack.length < 3) {
784
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
785
- return false;
786
- }
787
- const x1 = this.stack[this.stack.length - 3];
788
- const x2 = this.stack[this.stack.length - 2];
789
- const x3 = this.stack[this.stack.length - 1];
790
- this.stack[this.stack.length - 3] = x2;
791
- this.stack[this.stack.length - 2] = x3;
792
- this.stack[this.stack.length - 1] = x1;
793
- break;
794
- }
795
- case opcode_js_1.Opcode.OP_SWAP: {
796
- if (this.stack.length < 2) {
797
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
798
- return false;
799
- }
800
- const x1 = this.stack[this.stack.length - 2];
801
- const x2 = this.stack[this.stack.length - 1];
802
- this.stack[this.stack.length - 2] = x2;
803
- this.stack[this.stack.length - 1] = x1;
804
- break;
805
- }
806
- case opcode_js_1.Opcode.OP_TUCK: {
807
- if (this.stack.length < 2) {
808
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
809
- return false;
810
- }
811
- const x1 = this.stack[this.stack.length - 2];
812
- const x2 = this.stack[this.stack.length - 1];
813
- this.stack.splice(this.stack.length - 2, 0, x2);
814
- break;
815
- }
816
- case opcode_js_1.Opcode.OP_SIZE: {
817
- if (this.stack.length < 1) {
818
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
819
- return false;
820
- }
821
- const size = this.stack[this.stack.length - 1].length;
822
- this.stack.push(this.toScriptNumBuffer(size));
823
- break;
824
- }
825
- case opcode_js_1.Opcode.OP_AND:
826
- case opcode_js_1.Opcode.OP_OR:
827
- case opcode_js_1.Opcode.OP_XOR: {
828
- if (this.stack.length < 2) {
829
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
830
- return false;
831
- }
832
- const buf1 = this.stack.pop();
833
- const buf2 = this.stack.pop();
834
- if (buf1.length !== buf2.length) {
835
- this.errstr = 'SCRIPT_ERR_INVALID_OPERAND_SIZE';
836
- return false;
837
- }
838
- const result = Buffer.alloc(buf1.length);
839
- for (let i = 0; i < buf1.length; i++) {
840
- switch (opcodenum) {
841
- case opcode_js_1.Opcode.OP_AND:
842
- result[i] = buf1[i] & buf2[i];
843
- break;
844
- case opcode_js_1.Opcode.OP_OR:
845
- result[i] = buf1[i] | buf2[i];
846
- break;
847
- case opcode_js_1.Opcode.OP_XOR:
848
- result[i] = buf1[i] ^ buf2[i];
849
- break;
850
- }
851
- }
852
- this.stack.push(result);
853
- break;
854
- }
855
- case opcode_js_1.Opcode.OP_EQUAL:
856
- case opcode_js_1.Opcode.OP_EQUALVERIFY: {
857
- if (this.stack.length < 2) {
858
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
859
- return false;
860
- }
861
- const buf1 = this.stacktop(-2);
862
- const buf2 = this.stacktop(-1);
863
- const fEqual = buf1.toString('hex') === buf2.toString('hex');
864
- this.stack.pop();
865
- this.stack.pop();
866
- this.stack.push(fEqual ? Interpreter.true : Interpreter.false);
867
- if (opcodenum === opcode_js_1.Opcode.OP_EQUALVERIFY) {
868
- if (fEqual) {
869
- this.stack.pop();
870
- }
871
- else {
872
- this.errstr = 'SCRIPT_ERR_EQUALVERIFY';
873
- return false;
874
- }
875
- }
876
- break;
877
- }
878
- case opcode_js_1.Opcode.OP_1ADD: {
879
- if (this.stack.length < 1) {
880
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
881
- return false;
882
- }
883
- const bn = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
884
- this.stack[this.stack.length - 1] = this.toScriptNumBuffer(bn + 1n);
885
- break;
886
- }
887
- case opcode_js_1.Opcode.OP_1SUB: {
888
- if (this.stack.length < 1) {
889
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
890
- return false;
891
- }
892
- const bn = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
893
- this.stack[this.stack.length - 1] = this.toScriptNumBuffer(bn - 1n);
894
- break;
895
- }
896
- case opcode_js_1.Opcode.OP_NEGATE: {
897
- if (this.stack.length < 1) {
898
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
899
- return false;
900
- }
901
- const bn = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
902
- this.stack[this.stack.length - 1] = this.toScriptNumBuffer(-bn);
903
- break;
904
- }
905
- case opcode_js_1.Opcode.OP_ABS: {
906
- if (this.stack.length < 1) {
907
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
908
- return false;
909
- }
910
- const bn = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
911
- this.stack[this.stack.length - 1] = this.toScriptNumBuffer(bn < 0n ? -bn : bn);
912
- break;
913
- }
914
- case opcode_js_1.Opcode.OP_NOT: {
915
- if (this.stack.length < 1) {
916
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
917
- return false;
918
- }
919
- const bn = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
920
- this.stack[this.stack.length - 1] = this.toScriptNumBuffer(bn === 0n ? 1n : 0n);
921
- break;
922
- }
923
- case opcode_js_1.Opcode.OP_0NOTEQUAL: {
924
- if (this.stack.length < 1) {
925
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
926
- return false;
927
- }
928
- const bn = this.fromScriptNumBuffer(this.stack[this.stack.length - 1]);
929
- this.stack[this.stack.length - 1] = this.toScriptNumBuffer(bn !== 0n ? 1n : 0n);
930
- break;
931
- }
932
- case opcode_js_1.Opcode.OP_ADD: {
933
- if (this.stack.length < 2) {
934
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
935
- return false;
936
- }
937
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
938
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
939
- this.stack.push(this.toScriptNumBuffer(bn1 + bn2));
940
- break;
941
- }
942
- case opcode_js_1.Opcode.OP_SUB: {
943
- if (this.stack.length < 2) {
944
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
945
- return false;
946
- }
947
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
948
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
949
- this.stack.push(this.toScriptNumBuffer(bn2 - bn1));
950
- break;
951
- }
952
- case opcode_js_1.Opcode.OP_BOOLAND: {
953
- if (this.stack.length < 2) {
954
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
955
- return false;
956
- }
957
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
958
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
959
- const result = bn1 !== 0n && bn2 !== 0n ? 1n : 0n;
960
- this.stack.push(this.toScriptNumBuffer(result));
961
- break;
962
- }
963
- case opcode_js_1.Opcode.OP_BOOLOR: {
964
- if (this.stack.length < 2) {
965
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
966
- return false;
967
- }
968
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
969
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
970
- const result = bn1 !== 0n || bn2 !== 0n ? 1n : 0n;
971
- this.stack.push(this.toScriptNumBuffer(result));
972
- break;
973
- }
974
- case opcode_js_1.Opcode.OP_LESSTHAN: {
975
- if (this.stack.length < 2) {
976
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
977
- return false;
978
- }
979
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
980
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
981
- const result = bn2 < bn1 ? 1n : 0n;
982
- this.stack.push(this.toScriptNumBuffer(result));
983
- break;
984
- }
985
- case opcode_js_1.Opcode.OP_GREATERTHAN: {
986
- if (this.stack.length < 2) {
987
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
988
- return false;
989
- }
990
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
991
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
992
- const result = bn2 > bn1 ? 1n : 0n;
993
- this.stack.push(this.toScriptNumBuffer(result));
994
- break;
995
- }
996
- case opcode_js_1.Opcode.OP_LESSTHANOREQUAL: {
997
- if (this.stack.length < 2) {
998
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
999
- return false;
1000
- }
1001
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
1002
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
1003
- const result = bn2 <= bn1 ? 1n : 0n;
1004
- this.stack.push(this.toScriptNumBuffer(result));
1005
- break;
1006
- }
1007
- case opcode_js_1.Opcode.OP_GREATERTHANOREQUAL: {
1008
- if (this.stack.length < 2) {
1009
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1010
- return false;
1011
- }
1012
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
1013
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
1014
- const result = bn2 >= bn1 ? 1n : 0n;
1015
- this.stack.push(this.toScriptNumBuffer(result));
1016
- break;
1017
- }
1018
- case opcode_js_1.Opcode.OP_MIN: {
1019
- if (this.stack.length < 2) {
1020
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1021
- return false;
1022
- }
1023
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
1024
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
1025
- const result = bn1 < bn2 ? bn1 : bn2;
1026
- this.stack.push(this.toScriptNumBuffer(result));
1027
- break;
1028
- }
1029
- case opcode_js_1.Opcode.OP_MAX: {
1030
- if (this.stack.length < 2) {
1031
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1032
- return false;
1033
- }
1034
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
1035
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
1036
- const result = bn1 > bn2 ? bn1 : bn2;
1037
- this.stack.push(this.toScriptNumBuffer(result));
1038
- break;
1039
- }
1040
- case opcode_js_1.Opcode.OP_WITHIN: {
1041
- if (this.stack.length < 3) {
1042
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1043
- return false;
1044
- }
1045
- const bn1 = this.fromScriptNumBuffer(this.stack.pop());
1046
- const bn2 = this.fromScriptNumBuffer(this.stack.pop());
1047
- const bn3 = this.fromScriptNumBuffer(this.stack.pop());
1048
- const result = bn3 >= bn2 && bn3 < bn1 ? 1n : 0n;
1049
- this.stack.push(this.toScriptNumBuffer(result));
1050
- break;
1051
- }
1052
- case opcode_js_1.Opcode.OP_RIPEMD160: {
1053
- if (this.stack.length < 1) {
1054
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1055
- return false;
1056
- }
1057
- const buf = this.stack.pop();
1058
- this.stack.push(hash_js_1.Hash.ripemd160(buf));
1059
- break;
1060
- }
1061
- case opcode_js_1.Opcode.OP_SHA256: {
1062
- if (this.stack.length < 1) {
1063
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1064
- return false;
1065
- }
1066
- const buf = this.stack.pop();
1067
- this.stack.push(hash_js_1.Hash.sha256(buf));
1068
- break;
1069
- }
1070
- case opcode_js_1.Opcode.OP_HASH160: {
1071
- if (this.stack.length < 1) {
1072
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1073
- return false;
1074
- }
1075
- const buf = this.stack.pop();
1076
- this.stack.push(hash_js_1.Hash.sha256ripemd160(buf));
1077
- break;
1078
- }
1079
- case opcode_js_1.Opcode.OP_HASH256: {
1080
- if (this.stack.length < 1) {
1081
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1082
- return false;
1083
- }
1084
- const buf = this.stack.pop();
1085
- this.stack.push(hash_js_1.Hash.sha256sha256(buf));
1086
- break;
1087
- }
1088
- case opcode_js_1.Opcode.OP_CODESEPARATOR: {
1089
- this.pbegincodehash = this.pc;
1090
- break;
1091
- }
1092
- case opcode_js_1.Opcode.OP_CHECKDATASIG:
1093
- case opcode_js_1.Opcode.OP_CHECKDATASIGVERIFY: {
1094
- if (this.stack.length < 3) {
1095
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1096
- return false;
1097
- }
1098
- const bufSig = this.stacktop(-3);
1099
- const bufMessage = this.stacktop(-2);
1100
- const bufPubkey = this.stacktop(-1);
1101
- if (!this.checkDataSignatureEncoding(bufSig) ||
1102
- !this.checkPubkeyEncoding(bufPubkey)) {
1103
- return false;
1104
- }
1105
- let fSuccess = false;
1106
- try {
1107
- const sig = signature_js_1.Signature.fromDataFormat(bufSig);
1108
- const pubkey = new publickey_js_1.PublicKey(bufPubkey);
1109
- const bufHash = hash_js_1.Hash.sha256(bufMessage);
1110
- if (!sig.isSchnorr) {
1111
- fSuccess = ecdsa_js_1.ECDSA.verify(bufHash, sig, pubkey, 'big');
1112
- }
1113
- else {
1114
- fSuccess = schnorr_js_1.Schnorr.verify(bufHash, sig, pubkey, 'big');
1115
- }
1116
- }
1117
- catch (e) {
1118
- fSuccess = false;
1119
- }
1120
- if (!fSuccess &&
1121
- this.flags & Interpreter.SCRIPT_VERIFY_NULLFAIL &&
1122
- bufSig.length) {
1123
- this.errstr = 'SCRIPT_ERR_NULLFAIL';
1124
- return false;
1125
- }
1126
- this.stack.pop();
1127
- this.stack.pop();
1128
- this.stack.pop();
1129
- this.stack.push(fSuccess ? Interpreter.true : Interpreter.false);
1130
- if (opcodenum === opcode_js_1.Opcode.OP_CHECKDATASIGVERIFY) {
1131
- if (fSuccess) {
1132
- this.stack.pop();
1133
- }
1134
- else {
1135
- this.errstr = 'SCRIPT_ERR_CHECKDATASIGVERIFY';
1136
- return false;
1137
- }
1138
- }
1139
- break;
1140
- }
1141
- case opcode_js_1.Opcode.OP_REVERSEBYTES: {
1142
- if (this.stack.length < 1) {
1143
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1144
- return false;
1145
- }
1146
- const buf = this.stacktop(-1);
1147
- const reversedBuf = Buffer.from(buf).reverse();
1148
- this.stack.pop();
1149
- this.stack.push(reversedBuf);
1150
- break;
1151
- }
1152
- case opcode_js_1.Opcode.OP_CHECKSIG: {
1153
- if (this.stack.length < 2) {
1154
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1155
- return false;
1156
- }
1157
- const sigBuf = this.stack[this.stack.length - 2];
1158
- const pubkeyBuf = this.stack[this.stack.length - 1];
1159
- if (!this.checkTxSignatureEncoding(sigBuf) ||
1160
- !this.checkPubkeyEncoding(pubkeyBuf)) {
1161
- return false;
1162
- }
1163
- const subscript = new script_js_1.Script();
1164
- subscript.chunks = this.script.chunks.slice(this.pbegincodehash);
1165
- const tmpScript = new script_js_1.Script().add(sigBuf);
1166
- subscript.findAndDelete(tmpScript);
1167
- let fSuccess = false;
1168
- try {
1169
- const signature = signature_js_1.Signature.fromTxFormat(sigBuf);
1170
- const pubkey = new publickey_js_1.PublicKey(pubkeyBuf);
1171
- if (this.tx &&
1172
- this.nin !== undefined &&
1173
- this.satoshisBN !== undefined) {
1174
- if (!signature.isSchnorr) {
1175
- fSuccess = this.tx.verifySignature(signature, pubkey, this.nin, subscript, new bn_js_1.BN(this.satoshisBN.toString()), this.flags);
1176
- }
1177
- else {
1178
- fSuccess = this.tx.verifySignature(signature, pubkey, this.nin, subscript, new bn_js_1.BN(this.satoshisBN.toString()), this.flags, 'schnorr');
1179
- }
1180
- }
1181
- }
1182
- catch (e) {
1183
- fSuccess = false;
1184
- }
1185
- if (!fSuccess &&
1186
- this.flags & Interpreter.SCRIPT_VERIFY_NULLFAIL &&
1187
- sigBuf.length) {
1188
- this.errstr = 'SCRIPT_ERR_NULLFAIL';
1189
- return false;
1190
- }
1191
- this.stack.pop();
1192
- this.stack.pop();
1193
- this.stack.push(fSuccess ? Interpreter.true : Interpreter.false);
1194
- break;
1195
- }
1196
- case opcode_js_1.Opcode.OP_CHECKSIGVERIFY: {
1197
- if (this.stack.length < 2) {
1198
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1199
- return false;
1200
- }
1201
- const sigBuf2 = this.stack[this.stack.length - 2];
1202
- const pubkeyBuf2 = this.stack[this.stack.length - 1];
1203
- if (!this.checkTxSignatureEncoding(sigBuf2) ||
1204
- !this.checkPubkeyEncoding(pubkeyBuf2)) {
1205
- return false;
1206
- }
1207
- const subscript2 = new script_js_1.Script();
1208
- subscript2.chunks = this.script.chunks.slice(this.pbegincodehash);
1209
- const tmpScript2 = new script_js_1.Script().add(sigBuf2);
1210
- subscript2.findAndDelete(tmpScript2);
1211
- let fSuccess2 = false;
1212
- try {
1213
- const signature = signature_js_1.Signature.fromTxFormat(sigBuf2);
1214
- const pubkey = new publickey_js_1.PublicKey(pubkeyBuf2);
1215
- if (this.tx &&
1216
- this.nin !== undefined &&
1217
- this.satoshisBN !== undefined) {
1218
- if (!signature.isSchnorr) {
1219
- fSuccess2 = this.tx.verifySignature(signature, pubkey, this.nin, subscript2, new bn_js_1.BN(this.satoshisBN.toString()), this.flags);
1220
- }
1221
- else {
1222
- fSuccess2 = this.tx.verifySignature(signature, pubkey, this.nin, subscript2, new bn_js_1.BN(this.satoshisBN.toString()), this.flags, 'schnorr');
1223
- }
1224
- }
1225
- }
1226
- catch (e) {
1227
- fSuccess2 = false;
1228
- }
1229
- if (!fSuccess2 &&
1230
- this.flags & Interpreter.SCRIPT_VERIFY_NULLFAIL &&
1231
- sigBuf2.length) {
1232
- this.errstr = 'SCRIPT_ERR_NULLFAIL';
1233
- return false;
1234
- }
1235
- this.stack.pop();
1236
- this.stack.pop();
1237
- if (fSuccess2) {
1238
- }
1239
- else {
1240
- this.errstr = 'SCRIPT_ERR_CHECKSIGVERIFY';
1241
- return false;
1242
- }
1243
- break;
1244
- }
1245
- case opcode_js_1.Opcode.OP_CHECKMULTISIG:
1246
- case opcode_js_1.Opcode.OP_CHECKMULTISIGVERIFY: {
1247
- const fRequireMinimal = (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALDATA) !== 0;
1248
- let i = 1;
1249
- const idxTopKey = i + 1;
1250
- if (this.stack.length < i) {
1251
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1252
- return false;
1253
- }
1254
- const nKeysCountBN = bn_js_1.BN.fromScriptNumBuffer(this.stacktop(-i), fRequireMinimal);
1255
- let nKeysCount = nKeysCountBN.toNumber();
1256
- const idxSigCount = idxTopKey + nKeysCount;
1257
- if (nKeysCount < 0 || nKeysCount > 20) {
1258
- this.errstr = 'SCRIPT_ERR_PUBKEY_COUNT';
1259
- return false;
1260
- }
1261
- this.nOpCount += nKeysCount;
1262
- if (this.nOpCount > 201) {
1263
- this.errstr = 'SCRIPT_ERR_OP_COUNT';
1264
- return false;
1265
- }
1266
- let ikey = ++i;
1267
- const idxTopSig = idxSigCount + 1;
1268
- i += nKeysCount;
1269
- let ikey2 = nKeysCount + 2;
1270
- if (this.stack.length < i) {
1271
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1272
- return false;
1273
- }
1274
- const nSigsCountBN = bn_js_1.BN.fromScriptNumBuffer(this.stacktop(-idxSigCount), fRequireMinimal);
1275
- let nSigsCount = nSigsCountBN.toNumber();
1276
- const idxDummy = idxTopSig + nSigsCount;
1277
- if (nSigsCount < 0 || nSigsCount > nKeysCount) {
1278
- this.errstr = 'SCRIPT_ERR_SIG_COUNT';
1279
- return false;
1280
- }
1281
- let isig = ++i;
1282
- i += nSigsCount;
1283
- if (this.stack.length < idxDummy) {
1284
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1285
- return false;
1286
- }
1287
- const subscript = new script_js_1.Script();
1288
- subscript.chunks = this.script.chunks.slice(this.pbegincodehash);
1289
- let fSuccess = true;
1290
- if (this.flags & Interpreter.SCRIPT_ENABLE_SCHNORR_MULTISIG &&
1291
- this.stacktop(-idxDummy).length !== 0) {
1292
- const dummy = this.stacktop(-idxDummy);
1293
- const bitfieldObj = this.decodeBitfield(dummy, nKeysCount);
1294
- if (!bitfieldObj.result) {
1295
- fSuccess = false;
1296
- }
1297
- const nSigs8bit = new Uint8Array([nSigsCount]);
1298
- const nSigs32 = Uint32Array.from(nSigs8bit);
1299
- if (this.countBits(bitfieldObj.bitfield) !== nSigs32[0]) {
1300
- this.errstr = 'INVALID_BIT_COUNT';
1301
- fSuccess = false;
1302
- }
1303
- const bottomKey = idxTopKey + nKeysCount - 1;
1304
- const bottomSig = idxTopSig + nSigsCount - 1;
1305
- let iKey = 0;
1306
- for (let iSig = 0; iSig < nSigsCount; iSig++, iKey++) {
1307
- if (bitfieldObj.bitfield >> iKey === 0) {
1308
- this.errstr = 'INVALID_BIT_RANGE';
1309
- fSuccess = false;
1310
- }
1311
- while (((bitfieldObj.bitfield >> iKey) & 0x01) == 0) {
1312
- if (iKey >= nKeysCount) {
1313
- this.errstr = 'wrong';
1314
- fSuccess = false;
1315
- break;
1316
- }
1317
- iKey++;
1318
- }
1319
- if (iKey >= nKeysCount) {
1320
- this.errstr = 'PUBKEY_COUNT';
1321
- fSuccess = false;
1322
- }
1323
- const bufsig = this.stacktop(-bottomSig + iSig);
1324
- const bufPubkey = this.stacktop(-bottomKey + iKey);
1325
- if (!this.checkRawSignatureEncoding(bufsig) ||
1326
- !this.checkPubkeyEncoding(bufPubkey)) {
1327
- fSuccess = false;
1328
- }
1329
- try {
1330
- const sig = signature_js_1.Signature.fromTxFormat(bufsig);
1331
- const pubkey = new publickey_js_1.PublicKey(bufPubkey);
1332
- const fOk = this.tx?.verifySignature(sig, pubkey, this.nin, subscript, new bn_js_1.BN(this.satoshisBN.toString()), this.flags, 'schnorr') || false;
1333
- if (!fOk) {
1334
- this.errstr = 'SIG_NULLFAIL';
1335
- fSuccess = false;
1336
- }
1337
- }
1338
- catch (e) {
1339
- fSuccess = false;
1340
- }
1341
- }
1342
- if (bitfieldObj.bitfield >> iKey != 0) {
1343
- this.errstr = 'INVALID_BIT_COUNT';
1344
- fSuccess = false;
1345
- }
1346
- }
1347
- else {
1348
- for (let k = 0; k < nSigsCount; k++) {
1349
- const bufSig = this.stacktop(-isig - k);
1350
- subscript.findAndDelete(new script_js_1.Script().add(bufSig));
1351
- }
1352
- while (fSuccess && nSigsCount > 0) {
1353
- const bufSig = this.stacktop(-isig);
1354
- const bufPubkey = this.stacktop(-ikey);
1355
- if (!this.checkTxSignatureEncoding(bufSig) ||
1356
- !this.checkPubkeyEncoding(bufPubkey)) {
1357
- return false;
1358
- }
1359
- let fOk = false;
1360
- try {
1361
- const sig = signature_js_1.Signature.fromTxFormat(bufSig);
1362
- const pubkey = new publickey_js_1.PublicKey(bufPubkey);
1363
- fOk =
1364
- this.tx?.verifySignature(sig, pubkey, this.nin, subscript, new bn_js_1.BN(this.satoshisBN.toString()), this.flags) || false;
1365
- }
1366
- catch (e) {
1367
- fOk = false;
1368
- }
1369
- if (fOk) {
1370
- isig++;
1371
- nSigsCount--;
1372
- }
1373
- ikey++;
1374
- nKeysCount--;
1375
- if (nSigsCount > nKeysCount) {
1376
- fSuccess = false;
1377
- }
1378
- }
1379
- }
1380
- while (i-- > 1) {
1381
- if (!fSuccess &&
1382
- this.flags & Interpreter.SCRIPT_VERIFY_NULLFAIL &&
1383
- !ikey2 &&
1384
- this.stacktop(-1).length) {
1385
- this.errstr = 'SCRIPT_ERR_NULLFAIL';
1386
- return false;
1387
- }
1388
- if (ikey2 > 0) {
1389
- ikey2--;
1390
- }
1391
- this.stack.pop();
1392
- }
1393
- if (this.stack.length < 1) {
1394
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1395
- return false;
1396
- }
1397
- if (this.flags & Interpreter.SCRIPT_VERIFY_NULLDUMMY &&
1398
- this.stacktop(-1).length) {
1399
- this.errstr = 'SCRIPT_ERR_SIG_NULLDUMMY';
1400
- return false;
1401
- }
1402
- this.stack.pop();
1403
- this.stack.push(fSuccess ? Interpreter.true : Interpreter.false);
1404
- if (opcodenum === opcode_js_1.Opcode.OP_CHECKMULTISIGVERIFY) {
1405
- if (fSuccess) {
1406
- this.stack.pop();
1407
- }
1408
- else {
1409
- this.errstr = 'SCRIPT_ERR_CHECKMULTISIGVERIFY';
1410
- return false;
1411
- }
1412
- }
1413
- break;
1414
- }
1415
- case opcode_js_1.Opcode.OP_CAT: {
1416
- if (this.stack.length < 2) {
1417
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1418
- return false;
1419
- }
1420
- const buf1 = this.stacktop(-2);
1421
- const buf2 = this.stacktop(-1);
1422
- if (buf1.length + buf2.length > Interpreter.MAX_SCRIPT_ELEMENT_SIZE) {
1423
- this.errstr = 'SCRIPT_ERR_PUSH_SIZE';
1424
- return false;
1425
- }
1426
- this.stack[this.stack.length - 2] = Buffer.concat([buf1, buf2]);
1427
- this.stack.pop();
1428
- break;
1429
- }
1430
- case opcode_js_1.Opcode.OP_SPLIT: {
1431
- if (this.stack.length < 2) {
1432
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1433
- return false;
1434
- }
1435
- const fRequireMinimal = (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALDATA) !== 0;
1436
- const buf1 = this.stacktop(-2);
1437
- const position = bn_js_1.BN.fromScriptNumBuffer(this.stacktop(-1), fRequireMinimal).toNumber();
1438
- if (position < 0 || position > buf1.length) {
1439
- this.errstr = 'SCRIPT_ERR_INVALID_SPLIT_RANGE';
1440
- return false;
1441
- }
1442
- const n1 = Buffer.from(buf1);
1443
- this.stack[this.stack.length - 2] = n1.subarray(0, position);
1444
- this.stack[this.stack.length - 1] = n1.subarray(position);
1445
- break;
1446
- }
1447
- case opcode_js_1.Opcode.OP_NUM2BIN: {
1448
- if (this.stack.length < 2) {
1449
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1450
- return false;
1451
- }
1452
- const fRequireMinimal = (this.flags & Interpreter.SCRIPT_VERIFY_MINIMALDATA) !== 0;
1453
- const size = bn_js_1.BN.fromScriptNumBuffer(this.stacktop(-1), fRequireMinimal).toNumber();
1454
- if (size > Interpreter.MAX_SCRIPT_ELEMENT_SIZE) {
1455
- this.errstr = 'SCRIPT_ERR_PUSH_SIZE';
1456
- return false;
1457
- }
1458
- this.stack.pop();
1459
- let rawnum = this.stacktop(-1);
1460
- rawnum = Interpreter._minimallyEncode(rawnum);
1461
- if (rawnum.length > size) {
1462
- this.errstr = 'SCRIPT_ERR_IMPOSSIBLE_ENCODING';
1463
- return false;
1464
- }
1465
- if (rawnum.length == size) {
1466
- this.stack[this.stack.length - 1] = rawnum;
1467
- break;
1468
- }
1469
- let signbit = 0x00;
1470
- if (rawnum.length > 0) {
1471
- signbit = rawnum[rawnum.length - 1] & 0x80;
1472
- rawnum[rawnum.length - 1] &= 0x7f;
1473
- }
1474
- const num = Buffer.alloc(size);
1475
- rawnum.copy(num, 0);
1476
- let l = rawnum.length - 1;
1477
- while (l++ < size - 2) {
1478
- num[l] = 0x00;
1479
- }
1480
- num[l] = signbit;
1481
- this.stack[this.stack.length - 1] = num;
1482
- break;
1483
- }
1484
- case opcode_js_1.Opcode.OP_BIN2NUM: {
1485
- if (this.stack.length < 1) {
1486
- this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
1487
- return false;
1488
- }
1489
- const buf1 = this.stacktop(-1);
1490
- const buf2 = Interpreter._minimallyEncode(buf1);
1491
- this.stack[this.stack.length - 1] = buf2;
1492
- if (!Interpreter._isMinimallyEncoded(buf2)) {
1493
- this.errstr = 'SCRIPT_ERR_INVALID_NUMBER_RANGE';
1494
- return false;
1495
- }
1496
- break;
1497
- }
1498
- default:
1499
- this.errstr = 'SCRIPT_ERR_BAD_OPCODE';
1500
- return false;
1501
- }
1502
- }
1503
- return true;
1504
- }
1505
- decodeBitfield(dummy, size) {
1506
- if (size > 32) {
1507
- this.errstr = 'INVALID_BITFIELD_SIZE';
1508
- return { result: false };
1509
- }
1510
- const bitfieldSize = Math.floor((size + 7) / 8);
1511
- const dummyBitlength = dummy.length;
1512
- if (dummyBitlength !== bitfieldSize) {
1513
- this.errstr = 'INVALID_BITFIELD_SIZE';
1514
- return { result: false };
1515
- }
1516
- let bitfield = 0;
1517
- const dummyAs32Bit = Uint32Array.from(dummy);
1518
- for (let i = 0; i < bitfieldSize; i++) {
1519
- bitfield = bitfield | (dummyAs32Bit[i] << (8 * i));
1520
- }
1521
- const mask = (0x01 << size) - 1;
1522
- if ((bitfield & mask) != bitfield) {
1523
- this.errstr = 'INVALID_BIT_RANGE';
1524
- return { result: false };
1525
- }
1526
- return { result: true, bitfield: bitfield };
1527
- }
1528
- countBits(v) {
1529
- v = v - ((v >> 1) & 0x55555555);
1530
- v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
1531
- return (((v + (v >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
1532
- }
1533
- stacktop(i) {
1534
- return this.stack[this.stack.length + i];
1535
- }
1536
- isOpcodeDisabled(opcode) {
1537
- switch (opcode) {
1538
- case 80:
1539
- case 101:
1540
- case 102:
1541
- case 115:
1542
- case opcode_js_1.Opcode.OP_INVERT:
1543
- case 137:
1544
- case 138:
1545
- case opcode_js_1.Opcode.OP_2MUL:
1546
- case opcode_js_1.Opcode.OP_2DIV:
1547
- case opcode_js_1.Opcode.OP_MUL:
1548
- case 156:
1549
- case 157:
1550
- case 158:
1551
- case 167:
1552
- case opcode_js_1.Opcode.OP_LSHIFT:
1553
- case opcode_js_1.Opcode.OP_RSHIFT:
1554
- return true;
1555
- case opcode_js_1.Opcode.OP_DIV:
1556
- case opcode_js_1.Opcode.OP_MOD:
1557
- case opcode_js_1.Opcode.OP_SPLIT:
1558
- case opcode_js_1.Opcode.OP_CAT:
1559
- case opcode_js_1.Opcode.OP_AND:
1560
- case opcode_js_1.Opcode.OP_OR:
1561
- case opcode_js_1.Opcode.OP_XOR:
1562
- case opcode_js_1.Opcode.OP_BIN2NUM:
1563
- case opcode_js_1.Opcode.OP_NUM2BIN:
1564
- return false;
1565
- default:
1566
- if (opcode >= 189) {
1567
- return true;
1568
- }
1569
- return false;
1570
- }
1571
- }
1572
- checkLockTime(nLockTime) {
1573
- if (!this.tx || this.nin === undefined) {
1574
- return false;
1575
- }
1576
- if (!((this.tx.nLockTime < Interpreter.LOCKTIME_THRESHOLD &&
1577
- nLockTime.lt(Interpreter.LOCKTIME_THRESHOLD_BN)) ||
1578
- (this.tx.nLockTime >= Interpreter.LOCKTIME_THRESHOLD &&
1579
- nLockTime.gte(Interpreter.LOCKTIME_THRESHOLD_BN)))) {
1580
- return false;
1581
- }
1582
- if (nLockTime.gt(new bn_js_1.BN(this.tx.nLockTime))) {
1583
- return false;
1584
- }
1585
- if (!this.tx.inputs[this.nin].isFinal()) {
1586
- return false;
1587
- }
1588
- return true;
1589
- }
1590
- checkSequence(nSequence) {
1591
- if (!this.tx || this.nin === undefined) {
1592
- return false;
1593
- }
1594
- const txToSequence = this.tx.inputs[this.nin].sequenceNumber;
1595
- if (this.tx.version < 2) {
1596
- return false;
1597
- }
1598
- if (txToSequence & Interpreter.SEQUENCE_LOCKTIME_DISABLE_FLAG) {
1599
- return false;
1600
- }
1601
- const nLockTimeMask = Interpreter.SEQUENCE_LOCKTIME_TYPE_FLAG |
1602
- Interpreter.SEQUENCE_LOCKTIME_MASK;
1603
- const txToSequenceMasked = new bn_js_1.BN(txToSequence & nLockTimeMask);
1604
- const nSequenceMasked = nSequence.and(nLockTimeMask);
1605
- const SEQUENCE_LOCKTIME_TYPE_FLAG_BN = new bn_js_1.BN(Interpreter.SEQUENCE_LOCKTIME_TYPE_FLAG);
1606
- if (!((txToSequenceMasked.lt(SEQUENCE_LOCKTIME_TYPE_FLAG_BN) &&
1607
- nSequenceMasked.lt(SEQUENCE_LOCKTIME_TYPE_FLAG_BN)) ||
1608
- (txToSequenceMasked.gte(SEQUENCE_LOCKTIME_TYPE_FLAG_BN) &&
1609
- nSequenceMasked.gte(SEQUENCE_LOCKTIME_TYPE_FLAG_BN)))) {
1610
- return false;
1611
- }
1612
- if (nSequenceMasked.gt(txToSequenceMasked)) {
1613
- return false;
1614
- }
1615
- return true;
1616
- }
1617
- static castToBool(buf) {
1618
- for (let i = 0; i < buf.length; i++) {
1619
- if (buf[i] !== 0) {
1620
- if (i === buf.length - 1 && buf[i] === 0x80) {
1621
- return false;
1622
- }
1623
- return true;
1624
- }
1625
- }
1626
- return false;
1627
- }
1628
- static isSchnorrSig(buf) {
1629
- return (buf.length === 64 || buf.length === 65) && buf[0] !== 0x30;
1630
- }
1631
- _verifyScriptType(scriptPubkey) {
1632
- const buf = scriptPubkey.toBuffer();
1633
- if (buf.length < 2) {
1634
- this.errstr = 'SCRIPT_ERR_SCRIPTTYPE_MALFORMED_SCRIPT';
1635
- return false;
1636
- }
1637
- const scriptType = buf[1];
1638
- switch (scriptType) {
1639
- case taproot_js_1.TAPROOT_SCRIPTTYPE: {
1640
- const result = (0, taproot_js_1.verifyTaprootSpend)(scriptPubkey, this.stack, this.flags);
1641
- if (!result.success) {
1642
- this.errstr = result.error || 'SCRIPT_ERR_UNKNOWN';
1643
- return false;
1644
- }
1645
- if (result.stack) {
1646
- this.stack = result.stack;
1647
- }
1648
- if (result.scriptToExecute) {
1649
- const prevScript = this.script;
1650
- const prevPc = this.pc;
1651
- const prevPbegincodehash = this.pbegincodehash;
1652
- this.script = result.scriptToExecute;
1653
- this.pc = 0;
1654
- this.pbegincodehash = 0;
1655
- const evalResult = this.evaluate();
1656
- this.script = prevScript;
1657
- this.pc = prevPc;
1658
- this.pbegincodehash = prevPbegincodehash;
1659
- if (!evalResult) {
1660
- return false;
1661
- }
1662
- if (this.stack.length === 0) {
1663
- this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_RESULT';
1664
- return false;
1665
- }
1666
- const finalBuf = this.stack[this.stack.length - 1];
1667
- if (!Interpreter.castToBool(finalBuf)) {
1668
- this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_STACK';
1669
- return false;
1670
- }
1671
- }
1672
- else {
1673
- const scriptBuf = scriptPubkey.toBuffer();
1674
- const vchPubkey = scriptBuf.subarray(taproot_js_1.TAPROOT_INTRO_SIZE, taproot_js_1.TAPROOT_SIZE_WITHOUT_STATE);
1675
- const vchSig = this.stack[this.stack.length - 1];
1676
- const sigFlags = this.flags | Interpreter.SCRIPT_TAPROOT_KEY_SPEND_PATH;
1677
- if (!this.checkTxSignatureEncoding(vchSig) ||
1678
- !this.checkPubkeyEncoding(vchPubkey)) {
1679
- return false;
1680
- }
1681
- if (vchSig.length === 0) {
1682
- this.errstr = 'SCRIPT_ERR_TAPROOT_VERIFY_SIGNATURE_FAILED';
1683
- return false;
1684
- }
1685
- const sig = signature_js_1.Signature.fromTxFormat(vchSig);
1686
- const pubkey = new publickey_js_1.PublicKey(vchPubkey);
1687
- if (!sig.isSchnorr) {
1688
- this.errstr = 'SCRIPT_ERR_TAPROOT_KEY_SPEND_SIGNATURE_NOT_SCHNORR';
1689
- return false;
1690
- }
1691
- try {
1692
- const isValid = this.tx?.verifySignature(sig, pubkey, this.nin, scriptPubkey, new bn_js_1.BN(this.satoshisBN.toString()), sigFlags, 'schnorr');
1693
- if (!isValid) {
1694
- this.errstr = 'SCRIPT_ERR_TAPROOT_VERIFY_SIGNATURE_FAILED';
1695
- return false;
1696
- }
1697
- }
1698
- catch (e) {
1699
- this.errstr = 'SCRIPT_ERR_TAPROOT_VERIFY_SIGNATURE_FAILED';
1700
- return false;
1701
- }
1702
- }
1703
- return true;
1704
- }
1705
- default:
1706
- this.errstr = 'SCRIPT_ERR_SCRIPTTYPE_INVALID_TYPE';
1707
- return false;
1708
- }
1709
- }
1710
- static _isMinimallyEncoded(buf, nMaxNumSize) {
1711
- nMaxNumSize = nMaxNumSize || Interpreter.MAXIMUM_ELEMENT_SIZE;
1712
- if (buf.length > nMaxNumSize) {
1713
- return false;
1714
- }
1715
- if (buf.length > 0) {
1716
- if ((buf[buf.length - 1] & 0x7f) === 0) {
1717
- if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
1718
- return false;
1719
- }
1720
- }
1721
- }
1722
- return true;
1723
- }
1724
- static _minimallyEncode(buf) {
1725
- if (buf.length === 0) {
1726
- return buf;
1727
- }
1728
- const last = buf[buf.length - 1];
1729
- if (last & 0x7f) {
1730
- return buf;
1731
- }
1732
- if (buf.length === 1) {
1733
- return Buffer.from('');
1734
- }
1735
- if (buf[buf.length - 2] & 0x80) {
1736
- return buf;
1737
- }
1738
- for (let i = buf.length - 1; i > 0; i--) {
1739
- if (buf[i - 1] !== 0) {
1740
- if (buf[i - 1] & 0x80) {
1741
- const result = Buffer.alloc(i + 1);
1742
- buf.copy(result, 0, 0, i);
1743
- result[i] = last;
1744
- return result;
1745
- }
1746
- else {
1747
- const result = Buffer.alloc(i);
1748
- buf.copy(result, 0, 0, i);
1749
- result[i - 1] |= last;
1750
- return result;
1751
- }
1752
- }
1753
- }
1754
- return Buffer.from('');
1755
- }
1756
- }
1757
- exports.Interpreter = Interpreter;