bsv2 2.1.0 → 2.1.2

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 (241) hide show
  1. package/dist/bsv2.mjs +1 -1
  2. package/dist/bsv2.umd.js +1 -1
  3. package/package.json +7 -3
  4. package/.babel.test +0 -10
  5. package/.editorconfig +0 -4
  6. package/.env.defaults +0 -1
  7. package/.env.schema +0 -1
  8. package/.husky/pre-commit +0 -1
  9. package/.mocharc.yml +0 -4
  10. package/.nvmrc +0 -1
  11. package/.travis.yml +0 -7
  12. package/babel.config.cjs +0 -44
  13. package/benchmark.js +0 -42
  14. package/bin/bsv.js +0 -11
  15. package/bin/network.js +0 -93
  16. package/build/index.html +0 -20
  17. package/doc/assets/anchor.js +0 -350
  18. package/doc/assets/bass-addons.css +0 -12
  19. package/doc/assets/bass.css +0 -544
  20. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  21. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  22. package/doc/assets/fonts/LICENSE.txt +0 -93
  23. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  24. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  25. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  26. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  27. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  28. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  29. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  30. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  31. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  32. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  33. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  34. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  35. package/doc/assets/fonts/source-code-pro.css +0 -23
  36. package/doc/assets/github.css +0 -123
  37. package/doc/assets/site.js +0 -168
  38. package/doc/assets/split.css +0 -15
  39. package/doc/assets/split.js +0 -782
  40. package/doc/assets/style.css +0 -147
  41. package/doc/index.html +0 -9461
  42. package/entry.js +0 -81
  43. package/eslint.config.mjs +0 -8
  44. package/index.js +0 -98
  45. package/karma.conf.ts +0 -77
  46. package/lib/ach.js +0 -59
  47. package/lib/address.js +0 -266
  48. package/lib/aes.js +0 -57
  49. package/lib/aescbc.js +0 -38
  50. package/lib/base-58-check.js +0 -83
  51. package/lib/base-58.js +0 -59
  52. package/lib/bip-32.js +0 -472
  53. package/lib/bip-39-en-wordlist.js +0 -2054
  54. package/lib/bip-39-en.js +0 -10
  55. package/lib/bip-39-es-wordlist.js +0 -2055
  56. package/lib/bip-39-es.js +0 -10
  57. package/lib/bip-39-jp-wordlist.js +0 -2053
  58. package/lib/bip-39-jp.js +0 -10
  59. package/lib/bip-39-words.js +0 -5
  60. package/lib/bip-39.js +0 -286
  61. package/lib/block-header.js +0 -79
  62. package/lib/block.js +0 -132
  63. package/lib/bn.js +0 -462
  64. package/lib/br.js +0 -172
  65. package/lib/bsm.js +0 -127
  66. package/lib/bw.js +0 -188
  67. package/lib/cbc.js +0 -140
  68. package/lib/cmp.js +0 -37
  69. package/lib/config.js +0 -50
  70. package/lib/constants.js +0 -150
  71. package/lib/ecdsa.js +0 -496
  72. package/lib/ecies.js +0 -151
  73. package/lib/hash-cache.js +0 -41
  74. package/lib/hash.js +0 -191
  75. package/lib/interp.js +0 -2017
  76. package/lib/inv.js +0 -49
  77. package/lib/key-pair.js +0 -133
  78. package/lib/mainnet-address.js +0 -5
  79. package/lib/mainnet-bip-32.js +0 -5
  80. package/lib/mainnet-key-pair.js +0 -5
  81. package/lib/mainnet-priv-key.js +0 -5
  82. package/lib/merkle.js +0 -92
  83. package/lib/msg.js +0 -184
  84. package/lib/op-code.js +0 -400
  85. package/lib/point.js +0 -134
  86. package/lib/priv-key.js +0 -157
  87. package/lib/pub-key.js +0 -230
  88. package/lib/random.js +0 -28
  89. package/lib/script.js +0 -828
  90. package/lib/sig-operations.js +0 -120
  91. package/lib/sig.js +0 -399
  92. package/lib/struct.js +0 -392
  93. package/lib/testnet-address.js +0 -5
  94. package/lib/testnet-bip-32.js +0 -5
  95. package/lib/testnet-key-pair.js +0 -5
  96. package/lib/testnet-priv-key.js +0 -5
  97. package/lib/tx-builder.js +0 -593
  98. package/lib/tx-in.js +0 -172
  99. package/lib/tx-out-map.js +0 -57
  100. package/lib/tx-out.js +0 -73
  101. package/lib/tx-verifier.js +0 -209
  102. package/lib/tx.js +0 -430
  103. package/lib/var-int.js +0 -73
  104. package/lib/version.js +0 -92
  105. package/lib/workers-result.js +0 -65
  106. package/lib/workers.js +0 -77
  107. package/load-babel.js +0 -5
  108. package/mtest/test.html +0 -17
  109. package/mtest/test.js +0 -2
  110. package/mtest/test.mjs +0 -2
  111. package/mtest/webpack.config.js +0 -115
  112. package/out/Sig.html +0 -541
  113. package/out/bip-32.js.html +0 -523
  114. package/out/bip-39.js.html +0 -336
  115. package/out/block.js.html +0 -183
  116. package/out/bn.js.html +0 -512
  117. package/out/ecdsa.js.html +0 -546
  118. package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
  119. package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
  120. package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
  121. package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  122. package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
  123. package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  124. package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
  125. package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
  126. package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
  127. package/out/fonts/OpenSans-Light-webfont.eot +0 -0
  128. package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
  129. package/out/fonts/OpenSans-Light-webfont.woff +0 -0
  130. package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  131. package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
  132. package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  133. package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
  134. package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
  135. package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
  136. package/out/global.html +0 -220
  137. package/out/index.html +0 -128
  138. package/out/interp.js.html +0 -2064
  139. package/out/merkle.js.html +0 -143
  140. package/out/msg.js.html +0 -235
  141. package/out/priv-key.js.html +0 -208
  142. package/out/pub-key.js.html +0 -281
  143. package/out/script.js.html +0 -876
  144. package/out/scripts/linenumber.js +0 -25
  145. package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
  146. package/out/scripts/prettify/lang-css.js +0 -2
  147. package/out/scripts/prettify/prettify.js +0 -28
  148. package/out/sig-operations.js.html +0 -171
  149. package/out/sig.js.html +0 -450
  150. package/out/struct.js.html +0 -440
  151. package/out/styles/jsdoc-default.css +0 -358
  152. package/out/styles/prettify-jsdoc.css +0 -111
  153. package/out/styles/prettify-tomorrow.css +0 -132
  154. package/out/tx-builder.js.html +0 -644
  155. package/out/tx-in.js.html +0 -223
  156. package/out/tx-verifier.js.html +0 -260
  157. package/out/tx.js.html +0 -481
  158. package/test/ach.js +0 -172
  159. package/test/address.js +0 -464
  160. package/test/aes.js +0 -96
  161. package/test/aescbc.js +0 -65
  162. package/test/base-58-check.js +0 -128
  163. package/test/base-58.js +0 -113
  164. package/test/bip-32.js +0 -721
  165. package/test/bip-39.js +0 -282
  166. package/test/block-header.js +0 -152
  167. package/test/block.js +0 -243
  168. package/test/bn.js +0 -643
  169. package/test/br.js +0 -353
  170. package/test/bsm.js +0 -141
  171. package/test/bw.js +0 -292
  172. package/test/cbc.js +0 -335
  173. package/test/cmp.js +0 -56
  174. package/test/data/bip69.json +0 -215
  175. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  176. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  177. package/test/data/bitcoind/blocks.json +0 -27
  178. package/test/data/bitcoind/script_tests.json +0 -2244
  179. package/test/data/bitcoind/sig_canonical.json +0 -7
  180. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  181. package/test/data/bitcoind/tx_invalid.json +0 -177
  182. package/test/data/bitcoind/tx_valid.json +0 -224
  183. package/test/data/blk86756-testnet.dat +0 -0
  184. package/test/data/blk86756-testnet.js +0 -12
  185. package/test/data/blk86756-testnet.json +0 -684
  186. package/test/data/ecdsa.json +0 -230
  187. package/test/data/messages.json +0 -22
  188. package/test/data/sighash.json +0 -1004
  189. package/test/data/tx_creation.json +0 -85
  190. package/test/ecdsa.js +0 -748
  191. package/test/ecies.js +0 -238
  192. package/test/global.js +0 -13
  193. package/test/hash-cache.js +0 -52
  194. package/test/hash.js +0 -354
  195. package/test/index.js +0 -10
  196. package/test/interp.js +0 -512
  197. package/test/inv.js +0 -65
  198. package/test/key-pair.js +0 -243
  199. package/test/merkle.js +0 -179
  200. package/test/msg.js +0 -295
  201. package/test/op-code.js +0 -79
  202. package/test/point.js +0 -375
  203. package/test/priv-key.js +0 -264
  204. package/test/pub-key.js +0 -549
  205. package/test/random.js +0 -31
  206. package/test/script.js +0 -1340
  207. package/test/sig-operations.js +0 -149
  208. package/test/sig.js +0 -662
  209. package/test/struct.js +0 -552
  210. package/test/tx-builder.js +0 -2104
  211. package/test/tx-in.js +0 -198
  212. package/test/tx-out-map.js +0 -104
  213. package/test/tx-out.js +0 -162
  214. package/test/tx-verifier.js +0 -246
  215. package/test/tx.js +0 -539
  216. package/test/var-int.js +0 -132
  217. package/test/vectors/aes.json +0 -10392
  218. package/test/vectors/aescbc.json +0 -620
  219. package/test/vectors/bip39.json +0 -502
  220. package/test/vectors/bip69.json +0 -215
  221. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  222. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  223. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  224. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  225. package/test/vectors/bitcoind/script_valid.json +0 -1462
  226. package/test/vectors/bitcoind/sighash.json +0 -3503
  227. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  228. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  229. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  230. package/test/vectors/ecdsa.json +0 -300
  231. package/test/vectors/hash.json +0 -6133
  232. package/test/vectors/kdf.json +0 -47
  233. package/test/vectors/largesttx.json +0 -5
  234. package/test/vectors/largesttxblock.json +0 -6
  235. package/test/vectors/sig.json +0 -79
  236. package/test/vectors/sighash-single-bug.json +0 -8
  237. package/test/version.js +0 -81
  238. package/test/workers-result.js +0 -89
  239. package/test/workers.js +0 -91
  240. package/test-index.js +0 -3
  241. package/vite.config.js +0 -36
package/lib/interp.js DELETED
@@ -1,2017 +0,0 @@
1
- /**
2
- * Script Interpreter
3
- * ==================
4
- *
5
- * Bitcoin transactions contain scripts. Each input has a script called the
6
- * scriptSig, and each output has a script called the scriptPubKey. To validate
7
- * an input, the ScriptSig is executed, then with the same stack, the
8
- * scriptPubKey from the output corresponding to that input is run. The primary
9
- * way to use this class is via the verify function:
10
- *
11
- * new Interp().verify( ... )
12
- *
13
- * In some ways, the script interpreter is one of the most poorly architected
14
- * components of Yours Bitcoin because of the giant switch statement in step(). But
15
- * that is deliberately so to make it similar to bitcoin core, and thus easier
16
- * to audit.
17
- */
18
- 'use strict'
19
-
20
- import { Br } from './br'
21
- import { Bn } from './bn'
22
- import { Bw } from './bw'
23
- import { cmp } from './cmp'
24
- import { Hash } from './hash'
25
- import { OpCode } from './op-code'
26
- import { PubKey } from './pub-key'
27
- import { Script } from './script'
28
- import { Sig } from './sig'
29
- import { Struct } from './struct'
30
- import { Tx } from './tx'
31
- import { TxIn } from './tx-in'
32
- import { Ecdsa } from './ecdsa'
33
-
34
-
35
- const minimallyEncode = (buf) => {
36
- if (buf.length === 0) {
37
- return buf
38
- }
39
-
40
- // If the last byte is not 0x00 or 0x80, we are minimally encoded.
41
- const last = buf[buf.length - 1]
42
- if ((last & 0x7f) !== 0) {
43
- return buf
44
- }
45
-
46
- // If the script is one byte long, then we have a zero, which encodes as an
47
- // empty array.
48
- if (buf.length === 1) {
49
- return []
50
- }
51
-
52
- // If the next byte has it sign bit set, then we are minimaly encoded.
53
- if ((buf[buf.length - 2] & 0x80) !== 0) {
54
- return buf
55
- }
56
-
57
- // We are not minimally encoded, we need to figure out how much to trim.
58
- for (let i = buf.length - 1; i > 0; i--) {
59
- // We found a non zero byte, time to encode.
60
- if (buf[i - 1] !== 0) {
61
- if ((buf[i - 1] & 0x80) !== 0) {
62
- // We found a byte with it sign bit set so we need one more
63
- // byte.
64
- buf[i] = last
65
- return buf.slice(0, i + 1)
66
- } else {
67
- // the sign bit is clear, we can use it.
68
- buf[i - 1] |= last
69
- return buf.slice(0, i)
70
- }
71
- }
72
- }
73
-
74
- // If we found the whole thing is zeros, then we have a zero.
75
- return []
76
- }
77
-
78
- class Interp extends Struct {
79
- constructor (
80
- script,
81
- tx,
82
- nIn,
83
- stack = [],
84
- altStack = [],
85
- pc = 0,
86
- pBeginCodeHash = 0,
87
- nOpCount = 0,
88
- ifStack = [],
89
- errStr = '',
90
- flags = Interp.defaultFlags,
91
- valueBn = new Bn(0)
92
- ) {
93
- super({
94
- script,
95
- tx,
96
- nIn,
97
- stack,
98
- altStack,
99
- pc,
100
- pBeginCodeHash,
101
- nOpCount,
102
- ifStack,
103
- errStr,
104
- flags,
105
- valueBn
106
- })
107
- }
108
-
109
- initialize () {
110
- this.script = new Script()
111
- this.stack = []
112
- this.altStack = []
113
- this.pc = 0
114
- this.pBeginCodeHash = 0
115
- this.nOpCount = 0
116
- this.ifStack = []
117
- this.errStr = ''
118
- this.flags = Interp.defaultFlags
119
- this.scriptLimit = 10000000
120
- this.stackLimit = 10000000
121
- return this
122
- }
123
-
124
- fromJSON (json) {
125
- this.fromJSONNoTx(json)
126
- this.tx = json.tx ? new Tx().fromJSON(json.tx) : undefined
127
- return this
128
- }
129
- /**
130
- * Convert JSON containing everything but the tx to an interp object.
131
- */
132
- fromJSONNoTx (json) {
133
- this.fromObject({
134
- script:
135
- json.script !== undefined
136
- ? new Script().fromJSON(json.script)
137
- : undefined,
138
- nIn: json.nIn
139
- })
140
- this.stack = []
141
- json.stack.forEach(
142
- function (hex) {
143
- this.stack.push(Buffer.from(hex, 'hex'))
144
- }.bind(this)
145
- )
146
- this.altStack = []
147
- json.altStack.forEach(
148
- function (hex) {
149
- this.altStack.push(Buffer.from(hex, 'hex'))
150
- }.bind(this)
151
- )
152
- this.fromObject({
153
- pc: json.pc,
154
- pBeginCodeHash: json.pBeginCodeHash,
155
- nOpCount: json.nOpCount,
156
- ifStack: json.ifStack,
157
- errStr: json.errStr,
158
- flags: json.flags
159
- })
160
- return this
161
- }
162
-
163
- fromBr (br) {
164
- let jsonNoTxBufLEn = br.readVarIntNum()
165
- let jsonNoTxBuf = br.read(jsonNoTxBufLEn)
166
- this.fromJSONNoTx(JSON.parse(jsonNoTxBuf.toString()))
167
- let txbuflen = br.readVarIntNum()
168
- if (txbuflen > 0) {
169
- let txbuf = br.read(txbuflen)
170
- this.tx = new Tx().fromFastBuffer(txbuf)
171
- }
172
- return this
173
- }
174
-
175
- toJSON () {
176
- let json = this.toJSONNoTx()
177
- json.tx = this.tx ? this.tx.toJSON() : undefined
178
- return json
179
- }
180
-
181
- /**
182
- * Convert everything but the tx to JSON.
183
- */
184
- toJSONNoTx () {
185
- let stack = []
186
- this.stack.forEach(function (buf) {
187
- stack.push(buf.toString('hex'))
188
- })
189
- let altStack = []
190
- this.altStack.forEach(function (buf) {
191
- altStack.push(buf.toString('hex'))
192
- })
193
- return {
194
- script: this.script ? this.script.toJSON() : undefined,
195
- nIn: this.nIn,
196
- stack: stack,
197
- altStack: altStack,
198
- pc: this.pc,
199
- pBeginCodeHash: this.pBeginCodeHash,
200
- nOpCount: this.nOpCount,
201
- ifStack: this.ifStack,
202
- errStr: this.errStr,
203
- flags: this.flags,
204
- preImage: this?.preImage ? this.preImage.toString("hex"):"",
205
- hashBuf: this?.hashBuf ? this.hashBuf.toString("hex"):""
206
- }
207
- }
208
-
209
- toBw (bw) {
210
- if (!bw) {
211
- bw = new Bw()
212
- }
213
- let jsonNoTxBuf = Buffer.from(JSON.stringify(this.toJSONNoTx()))
214
- bw.writeVarIntNum(jsonNoTxBuf.length)
215
- bw.write(jsonNoTxBuf)
216
- if (this.tx) {
217
- let txbuf = this.tx.toFastBuffer()
218
- bw.writeVarIntNum(txbuf.length)
219
- bw.write(txbuf)
220
- } else {
221
- bw.writeVarIntNum(0)
222
- }
223
- return bw
224
- }
225
-
226
- /**
227
- * In order to make auduting the script interpreter easier, we use the same
228
- * constants as bitcoin core, including the flags, which customize the
229
- * operation of the interpreter.
230
- */
231
- static getFlags (flagstr) {
232
- let flags = 0
233
- if (flagstr.indexOf('NONE') !== -1) {
234
- flags = flags | Interp.SCRIPT_VERIFY_NONE
235
- }
236
- if (flagstr.indexOf('P2SH') !== -1) {
237
- flags = flags | Interp.SCRIPT_VERIFY_P2SH
238
- }
239
- if (flagstr.indexOf('STRICTENC') !== -1) {
240
- flags = flags | Interp.SCRIPT_VERIFY_STRICTENC
241
- }
242
- if (flagstr.indexOf('DERSIG') !== -1) {
243
- flags = flags | Interp.SCRIPT_VERIFY_DERSIG
244
- }
245
- if (flagstr.indexOf('LOW_S') !== -1) {
246
- flags = flags | Interp.SCRIPT_VERIFY_LOW_S
247
- }
248
- if (flagstr.indexOf('NULLDUMMY') !== -1) {
249
- flags = flags | Interp.SCRIPT_VERIFY_NULLDUMMY
250
- }
251
- if (flagstr.indexOf('SIGPUSHONLY') !== -1) {
252
- flags = flags | Interp.SCRIPT_VERIFY_SIGPUSHONLY
253
- }
254
- if (flagstr.indexOf('MINIMALDATA') !== -1) {
255
- flags = flags | Interp.SCRIPT_VERIFY_MINIMALDATA
256
- }
257
- if (flagstr.indexOf('DISCOURAGE_UPGRADABLE_NOPS') !== -1) {
258
- flags = flags | Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
259
- }
260
- if (flagstr.indexOf('CLEANSTACK') !== -1) {
261
- flags = flags | Interp.SCRIPT_VERIFY_CLEANSTACK
262
- }
263
- if (flagstr.indexOf('CHECKLOCKTIMEVERIFY') !== -1) {
264
- flags = flags | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
265
- }
266
- if (flagstr.indexOf('CHECKSEQUENCEVERIFY') !== -1) {
267
- flags = flags | Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
268
- }
269
- if (flagstr.indexOf('SIGHASH_FORKID') !== -1) {
270
- flags = flags | Interp.SCRIPT_ENABLE_SIGHASH_FORKID
271
- }
272
- return flags
273
- }
274
-
275
- static castToBool (buf) {
276
- for (let i = 0; i < buf.length; i++) {
277
- if (buf[i] !== 0) {
278
- // can be negative zero
279
- if (i === buf.length - 1 && buf[i] === 0x80) {
280
- return false
281
- }
282
- return true
283
- }
284
- }
285
- return false
286
- }
287
-
288
- /**
289
- * Translated from bitcoin core's CheckSigEncoding
290
- */
291
- checkSigEncoding (buf) {
292
- // Empty signature. Not strictly DER encoded, but allowed to provide a
293
- // compact way to provide an invalid signature for use with CHECK(MULTI)SIG
294
- if (buf.length === 0) {
295
- return true
296
- }
297
- if (
298
- (this.flags &
299
- (Interp.SCRIPT_VERIFY_DERSIG |
300
- Interp.SCRIPT_VERIFY_LOW_S |
301
- Interp.SCRIPT_VERIFY_STRICTENC)) !==
302
- 0 &&
303
- !Sig.IsTxDer(buf)
304
- ) {
305
- this.errStr = 'SCRIPT_ERR_SIG_DER'
306
- return false
307
- } else if ((this.flags & Interp.SCRIPT_VERIFY_LOW_S) !== 0) {
308
- let sig = new Sig().fromTxFormat(buf)
309
- if (!sig.hasLowS()) {
310
- this.errStr = 'SCRIPT_ERR_SIG_DER'
311
- return false
312
- }
313
- } else if ((this.flags & Interp.SCRIPT_VERIFY_STRICTENC) !== 0) {
314
- let sig = new Sig().fromTxFormat(buf)
315
- if (!sig.hasDefinedHashType()) {
316
- this.errStr = 'SCRIPT_ERR_SIG_HASHTYPE'
317
- return false
318
- }
319
- }
320
- return true
321
- }
322
-
323
- /**
324
- * Translated from bitcoin core's CheckPubKeyEncoding
325
- */
326
- checkPubKeyEncoding (buf) {
327
- if (
328
- (this.flags & Interp.SCRIPT_VERIFY_STRICTENC) !== 0 &&
329
- !PubKey.isCompressedOrUncompressed(buf)
330
- ) {
331
- this.errStr = 'SCRIPT_ERR_PUBKEYTYPE'
332
- return false
333
- }
334
- return true
335
- }
336
-
337
- /**
338
- * Translated from bitcoin core's CheckLockTime
339
- */
340
- checkLockTime (nLockTime) {
341
- // There are two kinds of nLockTime: lock-by-blockheight
342
- // and lock-by-blocktime, distinguished by whether
343
- // nLockTime < LOCKTIME_THRESHOLD.
344
- //
345
- // We want to compare apples to apples, so fail the script
346
- // unless the type of nLockTime being tested is the same as
347
- // the nLockTime in the transaction.
348
- if (
349
- !(
350
- (this.tx.nLockTime < Interp.LOCKTIME_THRESHOLD &&
351
- nLockTime < Interp.LOCKTIME_THRESHOLD) ||
352
- (this.tx.nLockTime >= Interp.LOCKTIME_THRESHOLD &&
353
- nLockTime >= Interp.LOCKTIME_THRESHOLD)
354
- )
355
- ) {
356
- return false
357
- }
358
-
359
- // Now that we know we're comparing apples-to-apples, the
360
- // comparison is a simple numeric one.
361
- if (nLockTime > this.tx.nLockTime) {
362
- return false
363
- }
364
-
365
- // Finally the nLockTime feature can be disabled and thus
366
- // CHECKLOCKTIMEVERIFY bypassed if every txIn has been
367
- // finalized by setting nSequence to maxint. The
368
- // transaction would be allowed into the blockchain, making
369
- // the opCode ineffective.
370
- //
371
- // Testing if this vin is not final is sufficient to
372
- // prevent this condition. Alternatively we could test all
373
- // inputs, but testing just this input minimizes the data
374
- // required to prove correct CHECKLOCKTIMEVERIFY execution.
375
- if (TxIn.SEQUENCE_FINAL === this.tx.txIns[this.nIn].nSequence) {
376
- return false
377
- }
378
-
379
- return true
380
- }
381
-
382
- /**
383
- * Translated from bitcoin core's CheckSequence.
384
- */
385
- checkSequence (nSequence) {
386
- // Relative lock times are supported by comparing the passed
387
- // in operand to the sequence number of the input.
388
- let txToSequence = this.tx.txIns[this.nIn].nSequence
389
-
390
- // Fail if the transaction's version number is not set high
391
- // enough to trigger Bip 68 rules.
392
- if (this.tx.versionBytesNum < 2) {
393
- return false
394
- }
395
-
396
- // Sequence numbers with their most significant bit set are not
397
- // consensus constrained. Testing that the transaction's sequence
398
- // number do not have this bit set prevents using this property
399
- // to get around a CHECKSEQUENCEVERIFY check.
400
- if (txToSequence & TxIn.SEQUENCE_LOCKTIME_DISABLE_FLAG) {
401
- return false
402
- }
403
-
404
- // Mask off any bits that do not have consensus-enforced meaning
405
- // before doing the integer comparisons
406
- let nLockTimeMask =
407
- TxIn.SEQUENCE_LOCKTIME_TYPE_FLAG | TxIn.SEQUENCE_LOCKTIME_MASK
408
- let txToSequenceMasked = txToSequence & nLockTimeMask
409
- let nSequenceMasked = nSequence & nLockTimeMask
410
-
411
- // There are two kinds of nSequence: lock-by-blockheight
412
- // and lock-by-blocktime, distinguished by whether
413
- // nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG.
414
- //
415
- // We want to compare apples to apples, so fail the script
416
- // unless the type of nSequenceMasked being tested is the same as
417
- // the nSequenceMasked in the transaction.
418
- if (
419
- !(
420
- (txToSequenceMasked < TxIn.SEQUENCE_LOCKTIME_TYPE_FLAG &&
421
- nSequenceMasked < TxIn.SEQUENCE_LOCKTIME_TYPE_FLAG) ||
422
- (txToSequenceMasked >= TxIn.SEQUENCE_LOCKTIME_TYPE_FLAG &&
423
- nSequenceMasked >= TxIn.SEQUENCE_LOCKTIME_TYPE_FLAG)
424
- )
425
- ) {
426
- return false
427
- }
428
-
429
- // Now that we know we're comparing apples-to-apples, the
430
- // comparison is a simple numeric one.
431
- if (nSequenceMasked > txToSequenceMasked) {
432
- return false
433
- }
434
-
435
- return true
436
- }
437
-
438
- /**
439
- * Based on bitcoin core's EvalScript function, with the inner loop moved to
440
- * Interp.prototype.step()
441
- * bitcoin core commit: b5d1b1092998bc95313856d535c632ea5a8f9104
442
- */
443
- * eval () {
444
- if (this.script.toBuffer().length > this.scriptLimit) {
445
- this.errStr = 'SCRIPT_ERR_SCRIPT_SIZE'
446
- yield false
447
- }
448
-
449
- try {
450
- while (this.pc < this.script.chunks.length) {
451
- let fSuccess = this.step()
452
- if (!fSuccess) {
453
- yield false
454
- } else {
455
- yield fSuccess
456
- }
457
- }
458
-
459
- // Size limits
460
- if (this.stack.length + this.altStack.length > this.stackLimit) {
461
- this.errStr = 'SCRIPT_ERR_STACK_SIZE'
462
- yield false
463
- }
464
- } catch (e) {
465
- this.errStr = 'SCRIPT_ERR_UNKNOWN_ERROR: ' + e
466
- yield false
467
- }
468
-
469
- if (this.ifStack.length > 0) {
470
- this.errStr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL'
471
- yield false
472
- }
473
-
474
- yield true
475
- }
476
-
477
- /**
478
- * Based on the inner loop of bitcoin core's EvalScript function
479
- */
480
- step () {
481
- let fRequireMinimal =
482
- (this.flags & Interp.SCRIPT_VERIFY_MINIMALDATA) !== 0
483
-
484
- // bool fExec = !count(ifStack.begin(), ifStack.end(), false)
485
- let fExec = !(this.ifStack.indexOf(false) + 1)
486
-
487
- //
488
- // Read instruction
489
- //
490
- let chunk = this.script.chunks[this.pc]
491
- this.pc++
492
- let opCodeNum = chunk.opCodeNum
493
- if (opCodeNum === undefined) {
494
- this.errStr = 'SCRIPT_ERR_BAD_OPCODE'
495
- return false
496
- }
497
- if (chunk.buf && chunk.buf.length > Interp.MAX_SCRIPT_ELEMENT_SIZE) {
498
- this.errStr = 'SCRIPT_ERR_PUSH_SIZE'
499
- return false
500
- }
501
-
502
- // Note how OpCode.OP_RESERVED does not count towards the opCode limit.
503
- if (opCodeNum > OpCode.OP_16 && ++this.nOpCount > 201) {
504
- this.errStr = 'SCRIPT_ERR_OP_COUNT'
505
- return false
506
- }
507
-
508
- if (
509
- opCodeNum === OpCode.OP_2MUL ||
510
- opCodeNum === OpCode.OP_2DIV
511
- ) {
512
- this.errStr = 'SCRIPT_ERR_DISABLED_OPCODE'
513
- return false
514
- }
515
-
516
- if (fExec && opCodeNum >= 0 && opCodeNum <= OpCode.OP_PUSHDATA4) {
517
- if (fRequireMinimal && !this.script.checkMinimalPush(this.pc - 1)) {
518
- this.errStr = 'SCRIPT_ERR_MINIMALDATA'
519
- return false
520
- }
521
- if (!chunk.buf) {
522
- this.stack.push(Interp.false)
523
- } else if (chunk.len !== chunk.buf.length) {
524
- throw new Error('LEngth of push value not equal to length of data')
525
- } else {
526
- this.stack.push(chunk.buf)
527
- }
528
- } else if (
529
- fExec ||
530
- (OpCode.OP_IF <= opCodeNum && opCodeNum <= OpCode.OP_ENDIF)
531
- ) {
532
- switch (opCodeNum) {
533
- //
534
- // Push value
535
- //
536
- case OpCode.OP_1NEGATE:
537
- case OpCode.OP_1:
538
- case OpCode.OP_2:
539
- case OpCode.OP_3:
540
- case OpCode.OP_4:
541
- case OpCode.OP_5:
542
- case OpCode.OP_6:
543
- case OpCode.OP_7:
544
- case OpCode.OP_8:
545
- case OpCode.OP_9:
546
- case OpCode.OP_10:
547
- case OpCode.OP_11:
548
- case OpCode.OP_12:
549
- case OpCode.OP_13:
550
- case OpCode.OP_14:
551
- case OpCode.OP_15:
552
- case OpCode.OP_16:
553
- {
554
- // ( -- value)
555
- // ScriptNum bn((int)opCode - (int)(OpCode.OP_1 - 1))
556
- let n = opCodeNum - (OpCode.OP_1 - 1)
557
- let buf = new Bn(n).toScriptNumBuffer()
558
- this.stack.push(buf)
559
- // The result of these opCodes should always be the minimal way to push the data
560
- // they push, so no need for a CheckMinimalPush here.
561
- }
562
- break
563
-
564
- //
565
- // Control
566
- //
567
- case OpCode.OP_NOP:
568
- break
569
-
570
- case OpCode.OP_CHECKLOCKTIMEVERIFY:
571
- {
572
- if (!(this.flags & Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) {
573
- // not enabled; treat as a NOP2
574
- if (
575
- this.flags & Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
576
- ) {
577
- this.errStr = 'SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS'
578
- return false
579
- }
580
- break
581
- }
582
-
583
- if (this.stack.length < 1) {
584
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
585
- return false
586
- }
587
-
588
- // Note that elsewhere numeric opCodes are limited to
589
- // operands in the range -2**31+1 to 2**31-1, however it is
590
- // legal for opCodes to produce results exceeding that
591
- // range. This limitation is implemented by CScriptNum's
592
- // default 4-byte limit.
593
- //
594
- // If we kept to that limit we'd have a year 2038 problem,
595
- // even though the nLockTime field in transactions
596
- // themselves is uint32 which only becomes meaningless
597
- // after the year 2106.
598
- //
599
- // Thus as a special case we tell CScriptNum to accept up
600
- // to 5-byte bignums, which are good until 2**39-1, well
601
- // beyond the 2**32-1 limit of the nLockTime field itself.
602
- let nLockTimebuf = this.stack[this.stack.length - 1]
603
- let nLockTimebn = new Bn().fromScriptNumBuffer(
604
- nLockTimebuf,
605
- fRequireMinimal,
606
- 5
607
- )
608
- let nLockTime = nLockTimebn.toNumber()
609
-
610
- // In the rare event that the argument may be < 0 due to
611
- // some arithmetic being done first, you can always use
612
- // 0 MAX CHECKLOCKTIMEVERIFY.
613
- if (nLockTime < 0) {
614
- this.errStr = 'SCRIPT_ERR_NEGATIVE_LOCKTIME'
615
- return false
616
- }
617
-
618
- // Actually compare the specified lock time with the transaction.
619
- if (!this.checkLockTime(nLockTime)) {
620
- this.errStr = 'SCRIPT_ERR_UNSATISFIED_LOCKTIME'
621
- return false
622
- }
623
- }
624
- break
625
-
626
- case OpCode.OP_CHECKSEQUENCEVERIFY:
627
- {
628
- if (!(this.flags & Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) {
629
- // not enabled; treat as a NOP3
630
- if (
631
- this.flags & Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
632
- ) {
633
- this.errStr = 'SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS'
634
- return false
635
- }
636
- break
637
- }
638
-
639
- if (this.stack.length < 1) {
640
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
641
- return false
642
- }
643
-
644
- // nSequence, like nLockTime, is a 32-bit unsigned integer
645
- // field. See the comment in CHECKLOCKTIMEVERIFY regarding
646
- // 5-byte numeric operands.
647
- let nSequencebuf = this.stack[this.stack.length - 1]
648
- let nSequencebn = new Bn().fromScriptNumBuffer(
649
- nSequencebuf,
650
- fRequireMinimal,
651
- 5
652
- )
653
- let nSequence = nSequencebn.toNumber()
654
-
655
- // In the rare event that the argument may be < 0 due to
656
- // some arithmetic being done first, you can always use
657
- // 0 MAX CHECKSEQUENCEVERIFY.
658
- if (nSequence < 0) {
659
- this.errStr = 'SCRIPT_ERR_NEGATIVE_LOCKTIME'
660
- return false
661
- }
662
-
663
- // To provide for future soft-fork extensibility, if the
664
- // operand has the disabled lock-time flag set,
665
- // CHECKSEQUENCEVERIFY behaves as a NOP.
666
- if ((nSequence & TxIn.SEQUENCE_LOCKTIME_DISABLE_FLAG) !== 0) {
667
- break
668
- }
669
-
670
- // Compare the specified sequence number with the input.
671
- if (!this.checkSequence(nSequence)) {
672
- this.errStr = 'SCRIPT_ERR_UNSATISFIED_LOCKTIME'
673
- return false
674
- }
675
- }
676
- break
677
-
678
- case OpCode.OP_NOP1:
679
- case OpCode.OP_NOP3:
680
- case OpCode.OP_NOP4:
681
- case OpCode.OP_NOP5:
682
- case OpCode.OP_NOP6:
683
- case OpCode.OP_NOP7:
684
- case OpCode.OP_NOP8:
685
- case OpCode.OP_NOP9:
686
- case OpCode.OP_NOP10:
687
- if (this.flags & Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
688
- this.errStr = 'SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS'
689
- return false
690
- }
691
- break
692
-
693
- case OpCode.OP_IF:
694
- case OpCode.OP_NOTIF:
695
- {
696
- // <expression> if [statements] [else [statements]] endif
697
- // bool fValue = false
698
- let fValue = false
699
- if (fExec) {
700
- if (this.stack.length < 1) {
701
- this.errStr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL'
702
- return false
703
- }
704
- let buf = this.stack.pop()
705
- fValue = Interp.castToBool(buf)
706
- if (opCodeNum === OpCode.OP_NOTIF) {
707
- fValue = !fValue
708
- }
709
- }
710
- this.ifStack.push(fValue)
711
- }
712
- break
713
-
714
- case OpCode.OP_ELSE:
715
- if (this.ifStack.length === 0) {
716
- this.errStr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL'
717
- return false
718
- }
719
- this.ifStack[this.ifStack.length - 1] = !this.ifStack[
720
- this.ifStack.length - 1
721
- ]
722
- break
723
-
724
- case OpCode.OP_ENDIF:
725
- if (this.ifStack.length === 0) {
726
- this.errStr = 'SCRIPT_ERR_UNBALANCED_CONDITIONAL'
727
- return false
728
- }
729
- this.ifStack.pop()
730
- break
731
-
732
- case OpCode.OP_VERIFY:
733
- {
734
- // (true -- ) or
735
- // (false -- false) and return
736
- if (this.stack.length < 1) {
737
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
738
- return false
739
- }
740
- let buf = this.stack[this.stack.length - 1]
741
- let fValue = Interp.castToBool(buf)
742
- if (fValue) {
743
- this.stack.pop()
744
- } else {
745
- this.errStr = 'SCRIPT_ERR_VERIFY'
746
- return false
747
- }
748
- }
749
- break
750
-
751
- case OpCode.OP_RETURN: {
752
- this.errStr = 'SCRIPT_ERR_OP_RETURN'
753
- return false
754
- }
755
- // unreachable code: break
756
-
757
- //
758
- // Stack ops
759
- //
760
- case OpCode.OP_TOALTSTACK:
761
- if (this.stack.length < 1) {
762
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
763
- return false
764
- }
765
- this.altStack.push(this.stack.pop())
766
- break
767
-
768
- case OpCode.OP_FROMALTSTACK:
769
- if (this.altStack.length < 1) {
770
- this.errStr = 'SCRIPT_ERR_INVALID_ALTSTACK_OPERATION'
771
- return false
772
- }
773
- this.stack.push(this.altStack.pop())
774
- break
775
-
776
- case OpCode.OP_2DROP:
777
- // (x1 x2 -- )
778
- if (this.stack.length < 2) {
779
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
780
- return false
781
- }
782
- this.stack.pop()
783
- this.stack.pop()
784
- break
785
-
786
- case OpCode.OP_2DUP:
787
- {
788
- // (x1 x2 -- x1 x2 x1 x2)
789
- if (this.stack.length < 2) {
790
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
791
- return false
792
- }
793
- let buf1 = this.stack[this.stack.length - 2]
794
- let buf2 = this.stack[this.stack.length - 1]
795
- this.stack.push(buf1)
796
- this.stack.push(buf2)
797
- }
798
- break
799
-
800
- case OpCode.OP_3DUP:
801
- {
802
- // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
803
- if (this.stack.length < 3) {
804
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
805
- return false
806
- }
807
- let buf1 = this.stack[this.stack.length - 3]
808
- let buf2 = this.stack[this.stack.length - 2]
809
- let buf3 = this.stack[this.stack.length - 1]
810
- this.stack.push(buf1)
811
- this.stack.push(buf2)
812
- this.stack.push(buf3)
813
- }
814
- break
815
-
816
- case OpCode.OP_2OVER:
817
- {
818
- // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
819
- if (this.stack.length < 4) {
820
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
821
- return false
822
- }
823
- let buf1 = this.stack[this.stack.length - 4]
824
- let buf2 = this.stack[this.stack.length - 3]
825
- this.stack.push(buf1)
826
- this.stack.push(buf2)
827
- }
828
- break
829
-
830
- case OpCode.OP_2ROT:
831
- {
832
- // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
833
- if (this.stack.length < 6) {
834
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
835
- return false
836
- }
837
- let spliced = this.stack.splice(this.stack.length - 6, 2)
838
- this.stack.push(spliced[0])
839
- this.stack.push(spliced[1])
840
- }
841
- break
842
-
843
- case OpCode.OP_2SWAP:
844
- {
845
- // (x1 x2 x3 x4 -- x3 x4 x1 x2)
846
- if (this.stack.length < 4) {
847
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
848
- return false
849
- }
850
- let spliced = this.stack.splice(this.stack.length - 4, 2)
851
- this.stack.push(spliced[0])
852
- this.stack.push(spliced[1])
853
- }
854
- break
855
-
856
- case OpCode.OP_IFDUP:
857
- {
858
- // (x - 0 | x x)
859
- if (this.stack.length < 1) {
860
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
861
- return false
862
- }
863
- let buf = this.stack[this.stack.length - 1]
864
- let fValue = Interp.castToBool(buf)
865
- if (fValue) {
866
- this.stack.push(buf)
867
- }
868
- }
869
- break
870
-
871
- case OpCode.OP_DEPTH:
872
- {
873
- // -- stacksize
874
- let buf = new Bn(this.stack.length).toScriptNumBuffer()
875
- this.stack.push(buf)
876
- }
877
- break
878
-
879
- case OpCode.OP_DROP:
880
- // (x -- )
881
- if (this.stack.length < 1) {
882
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
883
- return false
884
- }
885
- this.stack.pop()
886
- break
887
-
888
- case OpCode.OP_DUP:
889
- // (x -- x x)
890
- if (this.stack.length < 1) {
891
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
892
- return false
893
- }
894
- this.stack.push(this.stack[this.stack.length - 1])
895
- break
896
-
897
- case OpCode.OP_NIP:
898
- // (x1 x2 -- x2)
899
- if (this.stack.length < 2) {
900
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
901
- return false
902
- }
903
- this.stack.splice(this.stack.length - 2, 1)
904
- break
905
-
906
- case OpCode.OP_OVER:
907
- // (x1 x2 -- x1 x2 x1)
908
- if (this.stack.length < 2) {
909
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
910
- return false
911
- }
912
- this.stack.push(this.stack[this.stack.length - 2])
913
- break
914
-
915
- case OpCode.OP_PICK:
916
- case OpCode.OP_ROLL:
917
- {
918
- // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
919
- // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
920
- if (this.stack.length < 2) {
921
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
922
- return false
923
- }
924
- let buf = this.stack[this.stack.length - 1]
925
- let bn = new Bn().fromScriptNumBuffer(buf, fRequireMinimal, Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS)
926
- let n = bn.toNumber()
927
- this.stack.pop()
928
- if (n < 0 || n >= this.stack.length) {
929
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
930
- return false
931
- }
932
- buf = this.stack[this.stack.length - n - 1]
933
- if (opCodeNum === OpCode.OP_ROLL) {
934
- this.stack.splice(this.stack.length - n - 1, 1)
935
- }
936
- this.stack.push(buf)
937
- }
938
- break
939
-
940
- case OpCode.OP_ROT:
941
- {
942
- // (x1 x2 x3 -- x2 x3 x1)
943
- // x2 x1 x3 after first swap
944
- // x2 x3 x1 after second swap
945
- if (this.stack.length < 3) {
946
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
947
- return false
948
- }
949
- let x1 = this.stack[this.stack.length - 3]
950
- let x2 = this.stack[this.stack.length - 2]
951
- let x3 = this.stack[this.stack.length - 1]
952
- this.stack[this.stack.length - 3] = x2
953
- this.stack[this.stack.length - 2] = x3
954
- this.stack[this.stack.length - 1] = x1
955
- }
956
- break
957
-
958
- case OpCode.OP_SWAP:
959
- {
960
- // (x1 x2 -- x2 x1)
961
- if (this.stack.length < 2) {
962
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
963
- return false
964
- }
965
- let x1 = this.stack[this.stack.length - 2]
966
- let x2 = this.stack[this.stack.length - 1]
967
- this.stack[this.stack.length - 2] = x2
968
- this.stack[this.stack.length - 1] = x1
969
- }
970
- break
971
-
972
- case OpCode.OP_TUCK:
973
- // (x1 x2 -- x2 x1 x2)
974
- if (this.stack.length < 2) {
975
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
976
- return false
977
- }
978
- this.stack.splice(
979
- this.stack.length - 2,
980
- 0,
981
- this.stack[this.stack.length - 1]
982
- )
983
- break
984
-
985
- case OpCode.OP_SIZE:
986
- {
987
- // (in -- in size)
988
- if (this.stack.length < 1) {
989
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
990
- return false
991
- }
992
- let bn = new Bn(this.stack[this.stack.length - 1].length)
993
- this.stack.push(bn.toScriptNumBuffer())
994
- }
995
- break
996
-
997
- //
998
- // Bitwise logic
999
- //
1000
- case OpCode.OP_OR:
1001
- case OpCode.OP_AND:
1002
- case OpCode.OP_XOR:
1003
- // (x1 x2 -- out)
1004
- if (this.stack.length < 2) {
1005
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1006
- return false
1007
- }
1008
- var buf1 = this.stack[this.stack.length - 2]
1009
- var buf2 = this.stack[this.stack.length - 1]
1010
-
1011
- if (buf1.length != buf2.length) {
1012
- this.errStr = 'SCRIPT_ERR_INVALID_OPERAND_SIZE'
1013
- return false
1014
- }
1015
-
1016
- switch (opCodeNum) {
1017
- case OpCode.OP_AND:
1018
- for(let i = 0; i < buf1.length; i++) {
1019
- buf1[i] &= buf2[i]
1020
- }
1021
- break
1022
- case OpCode.OP_OR:
1023
- for(let i = 0; i < buf1.length; i++) {
1024
- buf1[i] |= buf2[i]
1025
- }
1026
- break
1027
- case OpCode.OP_XOR:
1028
- for(let i = 0; i < buf1.length; i++) {
1029
- buf1[i] ^= buf2[i]
1030
- }
1031
- break
1032
- }
1033
-
1034
- // pop out buf2
1035
- this.stack.pop();
1036
- break
1037
- case OpCode.OP_INVERT:
1038
- // (x -- out)
1039
- if (this.stack.length < 1) {
1040
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1041
- return false
1042
- }
1043
- var buf = this.stack[this.stack.length - 1]
1044
- for(let i = 0; i < buf.length; i++) {
1045
- buf[i] = ~buf[i]
1046
- }
1047
- break
1048
- case OpCode.OP_LSHIFT:
1049
- case OpCode.OP_RSHIFT:
1050
- // (x n -- out)
1051
- {
1052
- if (this.stack.length < 2) {
1053
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1054
- return false
1055
- }
1056
-
1057
- let buf1 = this.stack[this.stack.length - 2]
1058
- let value = new Bn(buf1)
1059
- let n = new Bn().fromScriptNumBuffer(
1060
- this.stack[this.stack.length - 1],
1061
- fRequireMinimal,
1062
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1063
- ).toNumber()
1064
- if(n < 0) {
1065
- this.errStr = 'SCRIPT_ERR_INVALID_NUMBER_RANGE'
1066
- return false
1067
- }
1068
-
1069
- this.stack.pop()
1070
- this.stack.pop()
1071
-
1072
- switch(opCodeNum) {
1073
- case OpCode.OP_LSHIFT:
1074
- value = value.ushln(n)
1075
- break
1076
- case OpCode.OP_RSHIFT:
1077
- value = value.ushrn(n)
1078
- break
1079
- }
1080
-
1081
- let buf2 = value.toBuffer().slice(-buf1.length)
1082
- if(buf2.length < buf1.length) {
1083
- buf2 = Buffer.concat([Buffer.alloc(buf1.length - buf2.length), buf2])
1084
- }
1085
-
1086
- this.stack.push(buf2)
1087
- break
1088
- }
1089
- case OpCode.OP_EQUAL:
1090
- case OpCode.OP_EQUALVERIFY:
1091
- // case OpCode.OP_NOTEQUAL: // use OpCode.OP_NUMNOTEQUAL
1092
- {
1093
- // (x1 x2 - bool)
1094
- if (this.stack.length < 2) {
1095
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1096
- return false
1097
- }
1098
- let buf1 = this.stack[this.stack.length - 2]
1099
- let buf2 = this.stack[this.stack.length - 1]
1100
- let fEqual = cmp(buf1, buf2)
1101
- // OpCode.OP_NOTEQUAL is disabled because it would be too easy to say
1102
- // something like n != 1 and have some wiseguy pass in 1 with extra
1103
- // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)
1104
- // if (opCode == OpCode.OP_NOTEQUAL)
1105
- // fEqual = !fEqual
1106
- this.stack.pop()
1107
- this.stack.pop()
1108
- this.stack.push(fEqual ? Interp.true : Interp.false)
1109
- if (opCodeNum === OpCode.OP_EQUALVERIFY) {
1110
- if (fEqual) {
1111
- this.stack.pop()
1112
- } else {
1113
- this.errStr = 'SCRIPT_ERR_EQUALVERIFY'
1114
- return false
1115
- }
1116
- }
1117
- }
1118
- break
1119
-
1120
- //
1121
- // Numeric
1122
- //
1123
- case OpCode.OP_1ADD:
1124
- case OpCode.OP_1SUB:
1125
- case OpCode.OP_NEGATE:
1126
- case OpCode.OP_ABS:
1127
- case OpCode.OP_NOT:
1128
- case OpCode.OP_0NOTEQUAL:
1129
- {
1130
- // (in -- out)
1131
- if (this.stack.length < 1) {
1132
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1133
- return false
1134
- }
1135
- let buf = this.stack[this.stack.length - 1]
1136
- let bn = new Bn().fromScriptNumBuffer(buf, fRequireMinimal, Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS)
1137
- switch (opCodeNum) {
1138
- case OpCode.OP_1ADD:
1139
- bn = bn.add(1)
1140
- break
1141
- case OpCode.OP_1SUB:
1142
- bn = bn.sub(1)
1143
- break
1144
- case OpCode.OP_NEGATE:
1145
- bn = bn.neg()
1146
- break
1147
- case OpCode.OP_ABS:
1148
- if (bn.lt(0)) bn = bn.neg()
1149
- break
1150
- case OpCode.OP_NOT:
1151
- bn = new Bn(bn.eq(0) + 0)
1152
- break
1153
- case OpCode.OP_0NOTEQUAL:
1154
- bn = new Bn(bn.neq(0) + 0)
1155
- break
1156
- // default: assert(!"invalid opCode"); break; // TODO: does this ever occur?
1157
- }
1158
- this.stack.pop()
1159
- this.stack.push(bn.toScriptNumBuffer())
1160
- }
1161
- break
1162
-
1163
- case OpCode.OP_ADD:
1164
- case OpCode.OP_SUB:
1165
- case OpCode.OP_MUL:
1166
- case OpCode.OP_DIV:
1167
- case OpCode.OP_MOD:
1168
- case OpCode.OP_BOOLAND:
1169
- case OpCode.OP_BOOLOR:
1170
- case OpCode.OP_NUMEQUAL:
1171
- case OpCode.OP_NUMEQUALVERIFY:
1172
- case OpCode.OP_NUMNOTEQUAL:
1173
- case OpCode.OP_LESSTHAN:
1174
- case OpCode.OP_GREATERTHAN:
1175
- case OpCode.OP_LESSTHANOREQUAL:
1176
- case OpCode.OP_GREATERTHANOREQUAL:
1177
- case OpCode.OP_MIN:
1178
- case OpCode.OP_MAX:
1179
- {
1180
- // (x1 x2 -- out)
1181
- if (this.stack.length < 2) {
1182
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1183
- return false
1184
- }
1185
- let bn1 = new Bn().fromScriptNumBuffer(
1186
- this.stack[this.stack.length - 2],
1187
- fRequireMinimal,
1188
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1189
- )
1190
- let bn2 = new Bn().fromScriptNumBuffer(
1191
- this.stack[this.stack.length - 1],
1192
- fRequireMinimal,
1193
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1194
- )
1195
- let bn = new Bn(0)
1196
-
1197
- switch (opCodeNum) {
1198
- case OpCode.OP_ADD:
1199
- bn = bn1.add(bn2)
1200
- break
1201
-
1202
- case OpCode.OP_SUB:
1203
- bn = bn1.sub(bn2)
1204
- break
1205
-
1206
- case OpCode.OP_MUL:
1207
- bn = bn1.mul(bn2)
1208
- break
1209
-
1210
- case OpCode.OP_DIV:
1211
- if (bn2 == 0) {
1212
- this.errStr = "SCRIPT_ERR_DIV_BY_ZERO"
1213
- return false
1214
- }
1215
- bn = bn1.div(bn2)
1216
- break
1217
-
1218
- case OpCode.OP_MOD:
1219
- if (bn2 == 0) {
1220
- this.errStr = "SCRIPT_ERR_DIV_BY_ZERO"
1221
- return false
1222
- }
1223
- bn = bn1.mod(bn2)
1224
- break
1225
-
1226
- // case OpCode.OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break
1227
- case OpCode.OP_BOOLAND:
1228
- bn = new Bn((bn1.neq(0) && bn2.neq(0)) + 0)
1229
- break
1230
- // case OpCode.OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break
1231
- case OpCode.OP_BOOLOR:
1232
- bn = new Bn((bn1.neq(0) || bn2.neq(0)) + 0)
1233
- break
1234
- // case OpCode.OP_NUMEQUAL: bn = (bn1 == bn2); break
1235
- case OpCode.OP_NUMEQUAL:
1236
- bn = new Bn(bn1.eq(bn2) + 0)
1237
- break
1238
- // case OpCode.OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break
1239
- case OpCode.OP_NUMEQUALVERIFY:
1240
- bn = new Bn(bn1.eq(bn2) + 0)
1241
- break
1242
- // case OpCode.OP_NUMNOTEQUAL: bn = (bn1 != bn2); break
1243
- case OpCode.OP_NUMNOTEQUAL:
1244
- bn = new Bn(bn1.neq(bn2) + 0)
1245
- break
1246
- // case OpCode.OP_LESSTHAN: bn = (bn1 < bn2); break
1247
- case OpCode.OP_LESSTHAN:
1248
- bn = new Bn(bn1.lt(bn2) + 0)
1249
- break
1250
- // case OpCode.OP_GREATERTHAN: bn = (bn1 > bn2); break
1251
- case OpCode.OP_GREATERTHAN:
1252
- bn = new Bn(bn1.gt(bn2) + 0)
1253
- break
1254
- // case OpCode.OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break
1255
- case OpCode.OP_LESSTHANOREQUAL:
1256
- bn = new Bn(bn1.leq(bn2) + 0)
1257
- break
1258
- // case OpCode.OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break
1259
- case OpCode.OP_GREATERTHANOREQUAL:
1260
- bn = new Bn(bn1.geq(bn2) + 0)
1261
- break
1262
- case OpCode.OP_MIN:
1263
- bn = bn1.lt(bn2) ? bn1 : bn2
1264
- break
1265
- case OpCode.OP_MAX:
1266
- bn = bn1.gt(bn2) ? bn1 : bn2
1267
- break
1268
- // default: assert(!"invalid opCode"); break; //TODO: does this ever occur?
1269
- }
1270
- this.stack.pop()
1271
- this.stack.pop()
1272
- this.stack.push(bn.toScriptNumBuffer())
1273
-
1274
- if (opCodeNum === OpCode.OP_NUMEQUALVERIFY) {
1275
- // if (CastToBool(stacktop(-1)))
1276
- if (Interp.castToBool(this.stack[this.stack.length - 1])) {
1277
- this.stack.pop()
1278
- } else {
1279
- this.errStr = 'SCRIPT_ERR_NUMEQUALVERIFY'
1280
- return false
1281
- }
1282
- }
1283
- }
1284
- break
1285
-
1286
- case OpCode.OP_WITHIN:
1287
- {
1288
- // (x min max -- out)
1289
- if (this.stack.length < 3) {
1290
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1291
- return false
1292
- }
1293
- let bn1 = new Bn().fromScriptNumBuffer(
1294
- this.stack[this.stack.length - 3],
1295
- fRequireMinimal,
1296
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1297
- )
1298
- let bn2 = new Bn().fromScriptNumBuffer(
1299
- this.stack[this.stack.length - 2],
1300
- fRequireMinimal,
1301
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1302
-
1303
- )
1304
- let bn3 = new Bn().fromScriptNumBuffer(
1305
- this.stack[this.stack.length - 1],
1306
- fRequireMinimal,
1307
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1308
- )
1309
- // bool fValue = (bn2 <= bn1 && bn1 < bn3)
1310
- let fValue = bn2.leq(bn1) && bn1.lt(bn3)
1311
- this.stack.pop()
1312
- this.stack.pop()
1313
- this.stack.pop()
1314
- this.stack.push(fValue ? Interp.true : Interp.false)
1315
- }
1316
- break
1317
-
1318
- //
1319
- // Crypto
1320
- //
1321
- case OpCode.OP_RIPEMD160:
1322
- case OpCode.OP_SHA1:
1323
- case OpCode.OP_SHA256:
1324
- case OpCode.OP_HASH160:
1325
- case OpCode.OP_HASH256:
1326
- {
1327
- // (in -- hash)
1328
- if (this.stack.length < 1) {
1329
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1330
- return false
1331
- }
1332
- let buf = this.stack[this.stack.length - 1]
1333
- // valtype vchnew Hash((opCode == OpCode.OP_RIPEMD160 || opCode == OpCode.OP_SHA1 || opCode == OpCode.OP_HASH160) ? 20 : 32)
1334
- let bufHash
1335
- if (opCodeNum === OpCode.OP_RIPEMD160) {
1336
- bufHash = Hash.ripemd160(buf)
1337
- } else if (opCodeNum === OpCode.OP_SHA1) {
1338
- bufHash = Hash.sha1(buf)
1339
- } else if (opCodeNum === OpCode.OP_SHA256) {
1340
- bufHash = Hash.sha256(buf)
1341
- } else if (opCodeNum === OpCode.OP_HASH160) {
1342
- bufHash = Hash.sha256Ripemd160(buf)
1343
- } else if (opCodeNum === OpCode.OP_HASH256) {
1344
- bufHash = Hash.sha256Sha256(buf)
1345
- }
1346
- this.stack.pop()
1347
- this.stack.push(bufHash)
1348
- }
1349
- break
1350
-
1351
- case OpCode.OP_CODESEPARATOR:
1352
- // Hash starts after the code separator
1353
- this.pBeginCodeHash = this.pc
1354
- break
1355
-
1356
- case OpCode.OP_CHECKSIG:
1357
- case OpCode.OP_CHECKSIGVERIFY:
1358
- {
1359
- // (sig pubKey -- bool)
1360
- if (this.stack.length < 2) {
1361
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1362
- return false
1363
- }
1364
-
1365
- let bufSig = this.stack[this.stack.length - 2]
1366
- let bufPubKey = this.stack[this.stack.length - 1]
1367
- if (
1368
- !this.checkSigEncoding(bufSig) ||
1369
- !this.checkPubKeyEncoding(bufPubKey)
1370
- ) {
1371
- // serror is set
1372
- return false
1373
- }
1374
-
1375
- // Subset of script starting at the most recent codeseparator
1376
- // CScript scriptCode(pBeginCodeHash, pend)
1377
- let subScript = new Script().fromObject({
1378
- chunks: this.script.chunks.slice(this.pBeginCodeHash)
1379
- })
1380
- // https://github.com/Bitcoin-UAHF/spec/blob/master/replay-protected-sighash.md
1381
- let nHashType =
1382
- bufSig.length > 0 ? bufSig.readUInt8(bufSig.length - 1) : 0
1383
- if (nHashType & Sig.SIGHASH_FORKID) {
1384
- if (!(this.flags & Interp.SCRIPT_ENABLE_SIGHASH_FORKID)) {
1385
- this.errStr = 'SCRIPT_ERR_ILLEGAL_FORKID'
1386
- return false
1387
- }
1388
- } else {
1389
- subScript.findAndDelete(new Script().writeBuffer(bufSig))
1390
- }
1391
-
1392
- let fSuccess
1393
- try {
1394
- let sig = new Sig().fromTxFormat(bufSig)
1395
- let pubKey = new PubKey().fromBuffer(bufPubKey, false)
1396
-
1397
-
1398
- this.nHashType = sig.nHashType
1399
-
1400
- if (typeof this.nIn === "undefined"){
1401
- console.log("Missing tx input nIn")
1402
- }
1403
- if (typeof this.tx === "undefined"){
1404
- this.tx = new Tx()
1405
- }
1406
-
1407
- //subScript is usually the redeemScript
1408
-
1409
- try{
1410
-
1411
-
1412
- this.preImage = this.tx.sighashPreimage (sig.nHashType, this.nIn, subScript, this.valueBn, Tx.SCRIPT_ENABLE_SIGHASH_FORKID )
1413
- //this.hashBuf = this.tx.sighash(sig.nHashType, this.nIn, subScript, this.valueBn, Tx.SCRIPT_ENABLE_SIGHASH_FORKID)
1414
-
1415
- if (this.preImage.compare(Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')) === 0) {
1416
- this.hashBuf = this.preImage
1417
- }else{
1418
- this.hashBuf = new Br(Hash.sha256Sha256(this.preImage)).readReverse()
1419
- }
1420
- this.verify = Ecdsa.verify( this.hashBuf, sig, pubKey, 'little', true)
1421
-
1422
- }catch(e){
1423
- console.log("ERROR: ",e.message)
1424
- }
1425
- fSuccess = this.tx.verify(
1426
- sig,
1427
- pubKey,
1428
- this.nIn,
1429
- subScript,
1430
- Boolean(this.flags & Interp.SCRIPT_VERIFY_LOW_S),
1431
- this.valueBn,
1432
- Tx.SCRIPT_ENABLE_SIGHASH_FORKID
1433
- )
1434
-
1435
- } catch (e) {
1436
- // invalid sig or pubKey
1437
- fSuccess = false
1438
- }
1439
-
1440
- this.stack.pop()
1441
- this.stack.pop()
1442
- // stack.push_back(fSuccess ? vchTrue : vchFalse)
1443
- this.stack.push(fSuccess ? Interp.true : Interp.false)
1444
- if (opCodeNum === OpCode.OP_CHECKSIGVERIFY) {
1445
- if (fSuccess) {
1446
- this.stack.pop()
1447
- } else {
1448
- this.errStr = 'SCRIPT_ERR_CHECKSIGVERIFY'
1449
- return false
1450
- }
1451
- }
1452
- }
1453
- break
1454
-
1455
- case OpCode.OP_CHECKMULTISIG:
1456
- case OpCode.OP_CHECKMULTISIGVERIFY:
1457
- {
1458
- // ([sig ...] num_of_signatures [pubKey ...] num_of_pubKeys -- bool)
1459
-
1460
- let i = 1
1461
- if (this.stack.length < i) {
1462
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1463
- return false
1464
- }
1465
-
1466
- let nKeysCount = new Bn()
1467
- .fromScriptNumBuffer(
1468
- this.stack[this.stack.length - i],
1469
- fRequireMinimal
1470
- )
1471
- .toNumber()
1472
- if (nKeysCount < 0 || nKeysCount > 20) {
1473
- this.errStr = 'SCRIPT_ERR_PUBKEY_COUNT'
1474
- return false
1475
- }
1476
- this.nOpCount += nKeysCount
1477
- if (this.nOpCount > 201) {
1478
- this.errStr = 'SCRIPT_ERR_OP_COUNT'
1479
- return false
1480
- }
1481
- // int ikey = ++i
1482
- let ikey = ++i
1483
- i += nKeysCount
1484
- if (this.stack.length < i) {
1485
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1486
- return false
1487
- }
1488
-
1489
- let nSigsCount = new Bn()
1490
- .fromScriptNumBuffer(
1491
- this.stack[this.stack.length - i],
1492
- fRequireMinimal
1493
- )
1494
- .toNumber()
1495
- if (nSigsCount < 0 || nSigsCount > nKeysCount) {
1496
- this.errStr = 'SCRIPT_ERR_SIG_COUNT'
1497
- return false
1498
- }
1499
- // int isig = ++i
1500
- let isig = ++i
1501
- i += nSigsCount
1502
- if (this.stack.length < i) {
1503
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1504
- return false
1505
- }
1506
-
1507
- // Subset of script starting at the most recent codeseparator
1508
- let subScript = new Script().fromObject({
1509
- chunks: this.script.chunks.slice(this.pBeginCodeHash)
1510
- })
1511
-
1512
- for (let k = 0; k < nSigsCount; k++) {
1513
- let bufSig = this.stack[this.stack.length - isig - k]
1514
-
1515
- // https://github.com/Bitcoin-UAHF/spec/blob/master/replay-protected-sighash.md
1516
- let nHashType =
1517
- bufSig.length > 0 ? bufSig.readUInt8(bufSig.length - 1) : 0
1518
- if (nHashType & Sig.SIGHASH_FORKID) {
1519
- if (!(this.flags & Interp.SCRIPT_ENABLE_SIGHASH_FORKID)) {
1520
- this.errStr = 'SCRIPT_ERR_ILLEGAL_FORKID'
1521
- return false
1522
- }
1523
- } else {
1524
- // Drop the signature, since there's no way for a signature to sign itself
1525
- subScript.findAndDelete(new Script().writeBuffer(bufSig))
1526
- }
1527
- }
1528
-
1529
- let fSuccess = true
1530
- while (fSuccess && nSigsCount > 0) {
1531
- // valtype& vchSig = stacktop(-isig)
1532
- let bufSig = this.stack[this.stack.length - isig]
1533
- // valtype& vchPubKey = stacktop(-ikey)
1534
- let bufPubKey = this.stack[this.stack.length - ikey]
1535
-
1536
- if (
1537
- !this.checkSigEncoding(bufSig) ||
1538
- !this.checkPubKeyEncoding(bufPubKey)
1539
- ) {
1540
- // serror is set
1541
- return false
1542
- }
1543
-
1544
- let fOk
1545
- try {
1546
- let sig = new Sig().fromTxFormat(bufSig)
1547
- let pubKey = new PubKey().fromBuffer(bufPubKey, false)
1548
- fOk = this.tx.verify(
1549
- sig,
1550
- pubKey,
1551
- this.nIn,
1552
- subScript,
1553
- Boolean(this.flags & Interp.SCRIPT_VERIFY_LOW_S),
1554
- this.valueBn,
1555
- this.flags
1556
- )
1557
-
1558
- } catch (e) {
1559
- // invalid sig or pubKey
1560
- fOk = false
1561
- }
1562
-
1563
- if (fOk) {
1564
- isig++
1565
- nSigsCount--
1566
- }
1567
- ikey++
1568
- nKeysCount--
1569
-
1570
- // If there are more signatures left than keys left,
1571
- // then too many signatures have failed
1572
- if (nSigsCount > nKeysCount) {
1573
- fSuccess = false
1574
- }
1575
- }
1576
-
1577
- // Clean up stack of actual arguments
1578
- while (i-- > 1) {
1579
- this.stack.pop()
1580
- }
1581
-
1582
- // A bug causes CHECKMULTISIG to consume one extra argument
1583
- // whose contents were not checked in any way.
1584
- //
1585
- // Unfortunately this is a potential source of mutability,
1586
- // so optionally verify it is exactly equal to zero prior
1587
- // to removing it from the stack.
1588
- if (this.stack.length < 1) {
1589
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1590
- return false
1591
- }
1592
- if (
1593
- this.flags & Interp.SCRIPT_VERIFY_NULLDUMMY &&
1594
- this.stack[this.stack.length - 1].length
1595
- ) {
1596
- this.errStr = 'SCRIPT_ERR_SIG_NULLDUMMY'
1597
- return false
1598
- }
1599
- this.stack.pop()
1600
-
1601
- // stack.push_back(fSuccess ? vchTrue : vchFalse)
1602
- this.stack.push(fSuccess ? Interp.true : Interp.false)
1603
-
1604
- if (opCodeNum === OpCode.OP_CHECKMULTISIGVERIFY) {
1605
- if (fSuccess) {
1606
- this.stack.pop()
1607
- } else {
1608
- this.errStr = 'SCRIPT_ERR_CHECKMULTISIGVERIFY'
1609
- return false
1610
- }
1611
- }
1612
- }
1613
- break
1614
-
1615
- //
1616
- // Byte string operations
1617
- //
1618
- case OpCode.OP_CAT:
1619
- if (this.stack.length < 2) {
1620
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1621
- return false
1622
- }
1623
-
1624
- let vch1 = this.stack[this.stack.length - 2]
1625
-
1626
- let vch2 = this.stack[this.stack.length - 1]
1627
-
1628
- this.stack[this.stack.length - 2] = Buffer.concat([vch1, vch2])
1629
- this.stack.pop()
1630
- break
1631
-
1632
- case OpCode.OP_SPLIT:
1633
- if (this.stack.length < 2) {
1634
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1635
- return false
1636
- }
1637
-
1638
- let data = this.stack[this.stack.length - 2]
1639
-
1640
- let position = new Bn().fromScriptNumBuffer(
1641
- this.stack[this.stack.length - 1],
1642
- fRequireMinimal
1643
- )
1644
-
1645
- if(position.lt(0) || position.gt(data.length)) {
1646
- this.errStr = 'SCRIPT_ERR_INVALID_SPLIT_RANGE'
1647
- return false
1648
- }
1649
-
1650
- let n1 = data.slice(0, position)
1651
-
1652
- let n2 = data.slice(position)
1653
-
1654
- this.stack.pop()
1655
- this.stack.pop()
1656
-
1657
- this.stack.push(n1)
1658
- this.stack.push(n2)
1659
-
1660
- break
1661
- // input: a b
1662
- // output: out
1663
- // Converts numeric value a into byte sequence of length b.
1664
- case OpCode.OP_NUM2BIN:
1665
- if (this.stack.length < 2) {
1666
- this.errStr = 'OP_NUM2BIN requires at least two items to be on the stack.'
1667
- }
1668
-
1669
- let size = new Bn().fromScriptNumBuffer(
1670
- this.stack[this.stack.length - 1],
1671
- fRequireMinimal,
1672
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1673
- ).toNumber()
1674
- if (size > Interp.MAX_SCRIPT_ELEMENT_SIZE) {
1675
- this.errStr=`It's not currently possible to push data larger than ${Interp.MAX_SCRIPT_ELEMENT_SIZE} bytes.`
1676
- }
1677
-
1678
- this.stack.pop()
1679
-
1680
- let rawnum = this.stack[this.stack.length - 1]
1681
- // Try to see if we can fit that number in the number of
1682
- // byte requested.
1683
- rawnum = minimallyEncode(rawnum)
1684
-
1685
- if (rawnum.length > size) {
1686
- this.errStr='OP_NUM2BIN requires that the size expressed in the top stack item is large enough to hold the value expressed in the second-from-top stack item.'
1687
- }
1688
-
1689
- // We already have an element of the right size, we
1690
- // don't need to do anything.
1691
- if (rawnum.length === size) {
1692
- this.stack[this.stack.length - 1] = rawnum
1693
- break
1694
- }
1695
-
1696
- var signbit = 0x00
1697
- if (rawnum.length > 0) {
1698
- signbit = rawnum[rawnum.length - 1] & 0x80
1699
- rawnum[rawnum.length - 1] &= 0x7f
1700
- }
1701
-
1702
- var num = new Array(size)
1703
- num.fill(0)
1704
- for (var n = 0; n < size; n++) {
1705
- num[n] = rawnum[n]
1706
- }
1707
- n = rawnum.length - 1
1708
- while (n++ < size - 2) {
1709
- num[n] = 0x00
1710
- }
1711
-
1712
- num[n] = signbit
1713
-
1714
- this.stack[this.stack.length - 1] = Buffer.from(new Uint8Array(num).buffer, "binary")
1715
- break
1716
-
1717
-
1718
- // input: x
1719
- // output: out
1720
- // Converts byte sequence x into a numeric value.
1721
- case OpCode.OP_BIN2NUM:
1722
- if (this.stack.length < 1) {
1723
- this.errStr='OP_BIN2NUM requires at least one item to be on the stack.'
1724
- }
1725
-
1726
- var tmp1 = this.stack[this.stack.length - 1]
1727
- var tmp2 = minimallyEncode(tmp1)
1728
- // The resulting number must be a valid number.
1729
- this.stack[this.stack.length - 1] = new Bn().fromScriptNumBuffer(tmp2, fRequireMinimal, Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS)
1730
-
1731
- break
1732
- default:
1733
- this.errStr = 'SCRIPT_ERR_BAD_OPCODE'
1734
- return false
1735
- }
1736
- }
1737
-
1738
- return true
1739
- }
1740
-
1741
- /**
1742
- * This function has the same interface as bitcoin core's VerifyScript and is
1743
- * the function you want to use to know if a particular input in a
1744
- * transaction is valid or not. It simply iterates over the results generated
1745
- * by the results method.
1746
- */
1747
- verify (scriptSig, scriptPubKey, tx, nIn, flags, valueBn) {
1748
- let results = this.results(
1749
- scriptSig,
1750
- scriptPubKey,
1751
- tx,
1752
- nIn,
1753
- flags,
1754
- valueBn
1755
- )
1756
- for (let success of results) {
1757
- if (!success) {
1758
- return false
1759
- }
1760
- }
1761
- return true
1762
- }
1763
-
1764
- /**
1765
- * Gives you the results of the execution each operation of the scripSig and
1766
- * scriptPubKey corresponding to a particular input (nIn) for the concerned
1767
- * transaction (tx). Each result can be either true or false. If true, then
1768
- * the operation did not invalidate the transaction. If false, then the
1769
- * operation has invalidated the script, and the transaction is not valid.
1770
- * flags is a number that can pass in some special flags, such as whether or
1771
- * not to execute the redeemScript in a p2sh transaction.
1772
- *
1773
- * This method is translated from bitcoin core's VerifyScript. This function
1774
- * is a generator, thus you can and need to iterate through it. To
1775
- * automatically return true or false, use the verify method.
1776
- */
1777
- * results (scriptSig, scriptPubKey, tx, nIn, flags, valueBn) {
1778
- let stackCopy
1779
-
1780
- this.fromObject({
1781
- script: scriptSig,
1782
- tx: tx,
1783
- nIn: nIn,
1784
- flags: flags,
1785
- valueBn: valueBn
1786
- })
1787
-
1788
- if (
1789
- (flags & Interp.SCRIPT_VERIFY_SIGPUSHONLY) !== 0 &&
1790
- !scriptSig.isPushOnly()
1791
- ) {
1792
- this.errStr = this.errStr || 'SCRIPT_ERR_SIG_PUSHONLY'
1793
- yield false
1794
- }
1795
-
1796
- yield * this.eval()
1797
-
1798
- if (flags & Interp.SCRIPT_VERIFY_P2SH) {
1799
- stackCopy = this.stack.slice()
1800
- }
1801
-
1802
- let stack = this.stack
1803
- this.initialize()
1804
- this.fromObject({
1805
- script: scriptPubKey,
1806
- stack: stack,
1807
- tx: tx,
1808
- nIn: nIn,
1809
- flags: flags,
1810
- valueBn: valueBn
1811
- })
1812
-
1813
- yield * this.eval()
1814
-
1815
- if (this.stack.length === 0) {
1816
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1817
- yield false
1818
- }
1819
-
1820
- let buf = this.stack[this.stack.length - 1]
1821
- if (!Interp.castToBool(buf)) {
1822
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1823
- yield false
1824
- }
1825
-
1826
- // Additional validation for spend-to-script-hash transactions:
1827
- if (flags & Interp.SCRIPT_VERIFY_P2SH && scriptPubKey.isScriptHashOut()) {
1828
- // scriptSig must be literals-only or validation fails
1829
- if (!scriptSig.isPushOnly()) {
1830
- this.errStr = this.errStr || 'SCRIPT_ERR_SIG_PUSHONLY'
1831
- yield false
1832
- }
1833
-
1834
- // Restore stack.
1835
- let tmp = stack
1836
- stack = stackCopy
1837
- stackCopy = tmp
1838
-
1839
- // stack cannot be empty here, because if it was the
1840
- // P2SH HASH <> EQUAL scriptPubKey would be evaluated with
1841
- // an empty stack and the EvalScript above would yield false.
1842
- if (stack.length === 0) {
1843
- throw new Error('internal error - stack copy empty')
1844
- }
1845
-
1846
- let pubKeySerialized = stack[stack.length - 1]
1847
- let scriptPubKey2 = new Script().fromBuffer(pubKeySerialized)
1848
- stack.pop()
1849
-
1850
- this.initialize()
1851
- this.fromObject({
1852
- script: scriptPubKey2,
1853
- stack: stack,
1854
- tx: tx,
1855
- nIn: nIn,
1856
- flags: flags,
1857
- valueBn: valueBn
1858
- })
1859
-
1860
- yield * this.eval()
1861
-
1862
- if (stack.length === 0) {
1863
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1864
- yield false
1865
- }
1866
-
1867
- if (!Interp.castToBool(stack[stack.length - 1])) {
1868
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1869
- yield false
1870
- } else {
1871
- yield true
1872
- }
1873
- }
1874
-
1875
- // The CLEANSTACK check is only performed after potential P2SH evaluation,
1876
- // as the non-P2SH evaluation of a P2SH script will obviously not result in
1877
- // a clean stack (the P2SH inputs remain).
1878
- if ((flags & Interp.SCRIPT_VERIFY_CLEANSTACK) !== 0) {
1879
- // Disallow CLEANSTACK without P2SH, as otherwise a switch
1880
- // CLEANSTACK->P2SH+CLEANSTACK would be possible, which is not a softfork
1881
- // (and P2SH should be one).
1882
- if (!(flags & Interp.SCRIPT_VERIFY_P2SH)) {
1883
- throw new Error('cannot use CLEANSTACK without P2SH')
1884
- }
1885
- if (stack.length !== 1) {
1886
- this.errStr = this.errStr || 'SCRIPT_ERR_CLEANSTACK'
1887
- yield false
1888
- }
1889
- }
1890
-
1891
- yield true
1892
- }
1893
-
1894
- /**
1895
- * If the script has failed, this methods returns valuable debug
1896
- * information about exactly where the script failed. It is a
1897
- * JSON-compatible object so it can be easily stringified. pc refers to the
1898
- * currently executing opcode.
1899
- */
1900
- getDebugObject () {
1901
- let pc = this.pc - 1 // pc is incremented immediately after getting
1902
- return {
1903
- errStr: this.errStr,
1904
- scriptStr: this.script ? this.script.toString() : 'no script found',
1905
- pc: pc,
1906
- stack: this.stack.map(buf => buf.toString('hex')),
1907
- altStack: this.altStack.map(buf => buf.toString('hex')),
1908
- opCodeStr: this.script
1909
- ? OpCode.fromNumber(this.script.chunks[pc].opCodeNum).toString()
1910
- : 'no script found'
1911
- }
1912
- }
1913
-
1914
- getDebugString () {
1915
- return JSON.stringify(this.getDebugObject(), null, 2)
1916
- }
1917
- }
1918
-
1919
- Interp.true = Buffer.from([1])
1920
- Interp.false = Buffer.from([])
1921
- // TODO: https://github.com/bitcoin-sv/bitcoin-sv/blob/b489c32ef55d428c5c3825d5526de018031a20af/src/policy/policy.h
1922
- /** Maximum stack memory usage (used instead of MAX_SCRIPT_ELEMENT_SIZE and MAX_STACK_ELEMENTS) after Genesis. **/
1923
-
1924
- Interp.MAX_SCRIPT_ELEMENT_SIZE = 500*1000
1925
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS = 10*1000 // 10KB
1926
-
1927
- Interp.LOCKTIME_THRESHOLD = 500000000 // Tue Nov 5 00:53:20 1985 UTC
1928
-
1929
- // flags taken from bitcoin core
1930
- // bitcoin core commit: b5d1b1092998bc95313856d535c632ea5a8f9104
1931
- Interp.SCRIPT_VERIFY_NONE = 0
1932
-
1933
- // Evaluate P2SH subScripts (softfork safe, Bip16).
1934
- Interp.SCRIPT_VERIFY_P2SH = 1 << 0
1935
-
1936
- // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
1937
- // Passing a pubKey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) to checksig causes that pubKey to be
1938
- // skipped (not softfork safe: this flag can widen the validity of OP_CHECKSIG OP_NOT).
1939
- Interp.SCRIPT_VERIFY_STRICTENC = 1 << 1
1940
-
1941
- // Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, Bip62 rule 1)
1942
- Interp.SCRIPT_VERIFY_DERSIG = 1 << 2
1943
-
1944
- // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
1945
- // (softfork safe, Bip62 rule 5).
1946
- Interp.SCRIPT_VERIFY_LOW_S = 1 << 3
1947
-
1948
- // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, Bip62 rule 7).
1949
- Interp.SCRIPT_VERIFY_NULLDUMMY = 1 << 4
1950
-
1951
- // Using a non-push operator in the scriptSig causes script failure (softfork safe, Bip62 rule 2).
1952
- Interp.SCRIPT_VERIFY_SIGPUSHONLY = 1 << 5
1953
-
1954
- // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
1955
- // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
1956
- // any other push causes the script to fail (Bip62 rule 3).
1957
- // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (Bip62 rule 4).
1958
- // (softfork safe)
1959
- Interp.SCRIPT_VERIFY_MINIMALDATA = 1 << 6
1960
-
1961
- // Discourage use of NOPs reserved for upgrades (NOP1-10)
1962
- //
1963
- // Provided so that nodes can avoid accepting or mining transactions
1964
- // containing executed NOP's whose meaning may change after a soft-fork,
1965
- // thus rendering the script invalid; with this flag set executing
1966
- // discouraged NOPs fails the script. This verification flag will never be
1967
- // a mandatory flag applied to scripts in a block. NOPs that are not
1968
- // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
1969
- Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = 1 << 7
1970
-
1971
- // Require that only a single stack element remains after evaluation. This
1972
- // changes the success criterion from "At least one stack element must
1973
- // remain, and when interpreted as a boolean, it must be true" to "Exactly
1974
- // one stack element must remain, and when interpreted as a boolean, it must
1975
- // be true". (softfork safe, Bip62 rule 6)
1976
- // Note: CLEANSTACK should never be used without P2SH.
1977
- Interp.SCRIPT_VERIFY_CLEANSTACK = 1 << 8
1978
-
1979
- // Verify CHECKLOCKTIMEVERIFY
1980
- //
1981
- // See Bip65 for details.
1982
- Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = 1 << 9
1983
-
1984
- // support CHECKSEQUENCEVERIFY opCode
1985
- //
1986
- // See Bip112 for details
1987
- Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = 1 << 10
1988
-
1989
- // used for UAHF
1990
- // https://github.com/Bitcoin-UAHF/spec/blob/master/replay-protected-sighash.md
1991
- Interp.SCRIPT_ENABLE_SIGHASH_FORKID = 1 << 16
1992
-
1993
- // These are the things we wish to verify by default. At the time of writing,
1994
- // P2SH and CHECKLOCKTIMEVERIFY are both active, but CHECKSEQUENCEVERIFY is
1995
- // not.
1996
-
1997
-
1998
- // https://github.com/bitcoin-sv/bitcoin-sv/blob/86eb5e8bdf5573c3cd844a1d81bd4fb151b909e0/src/policy/policy.h#L176
1999
- // https://github.com/bitcoin-sv/bitcoin-sv/blob/86eb5e8bdf5573c3cd844a1d81bd4fb151b909e0/src/script/standard.h#L41
2000
- Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH |
2001
- Interp.SCRIPT_VERIFY_STRICTENC |
2002
- Interp.SCRIPT_ENABLE_SIGHASH_FORKID |
2003
- Interp.SCRIPT_VERIFY_LOW_S |
2004
- Interp.SCRIPT_VERIFY_NULLDUMMY |
2005
- Interp.SCRIPT_VERIFY_DERSIG |
2006
- Interp.SCRIPT_VERIFY_MINIMALDATA |
2007
- Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
2008
- Interp.SCRIPT_VERIFY_CLEANSTACK |
2009
- Interp.SCRIPT_VERIFY_SIGPUSHONLY |
2010
- Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY |
2011
- Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
2012
-
2013
-
2014
- // Interp.SCRIPT_VERIFY_P2SH | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
2015
- // Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH |
2016
-
2017
- export { Interp }