bsv2 2.0.24 → 2.1.1

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 (170) hide show
  1. package/dist/bsv2.mjs +36061 -4
  2. package/package.json +35 -39
  3. package/.babelrc.test +0 -10
  4. package/.editorconfig +0 -4
  5. package/.env.defaults +0 -1
  6. package/.env.schema +0 -1
  7. package/.husky/pre-commit +0 -1
  8. package/.mocharc.yml +0 -4
  9. package/.nvmrc +0 -1
  10. package/.travis.yml +0 -7
  11. package/benchmark.js +0 -50
  12. package/bin/bsv.js +0 -11
  13. package/bin/network.js +0 -92
  14. package/build/index.html +0 -20
  15. package/dist/bsv2.bundle.js +0 -8
  16. package/dist/bsv2.cjs.js +0 -2
  17. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  18. package/dist/bsv2.module.js +0 -2
  19. package/entry.js +0 -82
  20. package/index.js +0 -102
  21. package/karma.conf.js +0 -78
  22. package/lib/ach.js +0 -59
  23. package/lib/address.js +0 -264
  24. package/lib/aes.js +0 -57
  25. package/lib/aescbc.js +0 -38
  26. package/lib/base-58-check.js +0 -83
  27. package/lib/base-58.js +0 -59
  28. package/lib/bip-32.js +0 -472
  29. package/lib/bip-39-en-wordlist.js +0 -2054
  30. package/lib/bip-39-en.js +0 -10
  31. package/lib/bip-39-es-wordlist.js +0 -2055
  32. package/lib/bip-39-es.js +0 -10
  33. package/lib/bip-39-jp-wordlist.js +0 -2053
  34. package/lib/bip-39-jp.js +0 -10
  35. package/lib/bip-39-words.js +0 -5
  36. package/lib/bip-39.js +0 -285
  37. package/lib/block-header.js +0 -79
  38. package/lib/block.js +0 -132
  39. package/lib/bn.js +0 -467
  40. package/lib/br.js +0 -172
  41. package/lib/bsm.js +0 -127
  42. package/lib/bw.js +0 -188
  43. package/lib/cbc.js +0 -140
  44. package/lib/cmp.js +0 -37
  45. package/lib/config.js +0 -50
  46. package/lib/constants.js +0 -150
  47. package/lib/ecdsa.js +0 -495
  48. package/lib/ecies.js +0 -151
  49. package/lib/hash-cache.js +0 -41
  50. package/lib/hash.js +0 -191
  51. package/lib/interp.js +0 -2013
  52. package/lib/inv.js +0 -49
  53. package/lib/key-pair.js +0 -133
  54. package/lib/mainnet-address.js +0 -5
  55. package/lib/mainnet-bip-32.js +0 -5
  56. package/lib/mainnet-key-pair.js +0 -5
  57. package/lib/mainnet-priv-key.js +0 -5
  58. package/lib/merkle.js +0 -92
  59. package/lib/msg.js +0 -184
  60. package/lib/op-code.js +0 -398
  61. package/lib/point.js +0 -134
  62. package/lib/priv-key.js +0 -157
  63. package/lib/pub-key.js +0 -230
  64. package/lib/random.js +0 -28
  65. package/lib/script.js +0 -826
  66. package/lib/sig-operations.js +0 -120
  67. package/lib/sig.js +0 -399
  68. package/lib/struct.js +0 -389
  69. package/lib/testnet-address.js +0 -5
  70. package/lib/testnet-bip-32.js +0 -5
  71. package/lib/testnet-key-pair.js +0 -5
  72. package/lib/testnet-priv-key.js +0 -5
  73. package/lib/tx-builder.js +0 -593
  74. package/lib/tx-in.js +0 -172
  75. package/lib/tx-out-map.js +0 -57
  76. package/lib/tx-out.js +0 -73
  77. package/lib/tx-verifier.js +0 -209
  78. package/lib/tx.js +0 -430
  79. package/lib/var-int.js +0 -73
  80. package/lib/version.js +0 -92
  81. package/lib/workers-result.js +0 -65
  82. package/lib/workers.js +0 -77
  83. package/load-babel.js +0 -3
  84. package/mtest/test.html +0 -15
  85. package/mtest/test.js +0 -2
  86. package/mtest/test.mjs +0 -2
  87. package/test/ach.js +0 -172
  88. package/test/address.js +0 -464
  89. package/test/aes.js +0 -96
  90. package/test/aescbc.js +0 -65
  91. package/test/base-58-check.js +0 -128
  92. package/test/base-58.js +0 -113
  93. package/test/bip-32.js +0 -721
  94. package/test/bip-39.js +0 -282
  95. package/test/block-header.js +0 -152
  96. package/test/block.js +0 -243
  97. package/test/bn.js +0 -643
  98. package/test/br.js +0 -353
  99. package/test/bsm.js +0 -141
  100. package/test/bw.js +0 -292
  101. package/test/cbc.js +0 -335
  102. package/test/cmp.js +0 -56
  103. package/test/data/bip69.json +0 -215
  104. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  105. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  106. package/test/data/bitcoind/blocks.json +0 -27
  107. package/test/data/bitcoind/script_tests.json +0 -2244
  108. package/test/data/bitcoind/sig_canonical.json +0 -7
  109. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  110. package/test/data/bitcoind/tx_invalid.json +0 -177
  111. package/test/data/bitcoind/tx_valid.json +0 -224
  112. package/test/data/blk86756-testnet.dat +0 -0
  113. package/test/data/blk86756-testnet.js +0 -12
  114. package/test/data/blk86756-testnet.json +0 -684
  115. package/test/data/ecdsa.json +0 -230
  116. package/test/data/messages.json +0 -22
  117. package/test/data/sighash.json +0 -1004
  118. package/test/data/tx_creation.json +0 -85
  119. package/test/ecdsa.js +0 -748
  120. package/test/ecies.js +0 -238
  121. package/test/global.js +0 -13
  122. package/test/hash-cache.js +0 -52
  123. package/test/hash.js +0 -354
  124. package/test/index.js +0 -10
  125. package/test/interp.js +0 -512
  126. package/test/inv.js +0 -65
  127. package/test/key-pair.js +0 -243
  128. package/test/merkle.js +0 -179
  129. package/test/msg.js +0 -295
  130. package/test/op-code.js +0 -79
  131. package/test/point.js +0 -375
  132. package/test/priv-key.js +0 -264
  133. package/test/pub-key.js +0 -549
  134. package/test/random.js +0 -31
  135. package/test/script.js +0 -1340
  136. package/test/sig-operations.js +0 -149
  137. package/test/sig.js +0 -662
  138. package/test/struct.js +0 -552
  139. package/test/tx-builder.js +0 -2104
  140. package/test/tx-in.js +0 -198
  141. package/test/tx-out-map.js +0 -104
  142. package/test/tx-out.js +0 -162
  143. package/test/tx-verifier.js +0 -246
  144. package/test/tx.js +0 -539
  145. package/test/var-int.js +0 -132
  146. package/test/vectors/aes.json +0 -10392
  147. package/test/vectors/aescbc.json +0 -620
  148. package/test/vectors/bip39.json +0 -502
  149. package/test/vectors/bip69.json +0 -215
  150. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  151. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  152. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  153. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  154. package/test/vectors/bitcoind/script_valid.json +0 -1462
  155. package/test/vectors/bitcoind/sighash.json +0 -3503
  156. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  157. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  158. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  159. package/test/vectors/ecdsa.json +0 -300
  160. package/test/vectors/hash.json +0 -6133
  161. package/test/vectors/kdf.json +0 -47
  162. package/test/vectors/largesttx.json +0 -5
  163. package/test/vectors/largesttxblock.json +0 -6
  164. package/test/vectors/sig.json +0 -79
  165. package/test/vectors/sighash-single-bug.json +0 -8
  166. package/test/version.js +0 -81
  167. package/test/workers-result.js +0 -89
  168. package/test/workers.js +0 -91
  169. package/test-index.js +0 -3
  170. package/webpack.config.js +0 -115
package/lib/interp.js DELETED
@@ -1,2013 +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
- let buf1 = this.stack[this.stack.length - 2]
1009
- let 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
- let 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
- } catch (e) {
1558
- // invalid sig or pubKey
1559
- fOk = false
1560
- }
1561
-
1562
- if (fOk) {
1563
- isig++
1564
- nSigsCount--
1565
- }
1566
- ikey++
1567
- nKeysCount--
1568
-
1569
- // If there are more signatures left than keys left,
1570
- // then too many signatures have failed
1571
- if (nSigsCount > nKeysCount) {
1572
- fSuccess = false
1573
- }
1574
- }
1575
-
1576
- // Clean up stack of actual arguments
1577
- while (i-- > 1) {
1578
- this.stack.pop()
1579
- }
1580
-
1581
- // A bug causes CHECKMULTISIG to consume one extra argument
1582
- // whose contents were not checked in any way.
1583
- //
1584
- // Unfortunately this is a potential source of mutability,
1585
- // so optionally verify it is exactly equal to zero prior
1586
- // to removing it from the stack.
1587
- if (this.stack.length < 1) {
1588
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1589
- return false
1590
- }
1591
- if (
1592
- this.flags & Interp.SCRIPT_VERIFY_NULLDUMMY &&
1593
- this.stack[this.stack.length - 1].length
1594
- ) {
1595
- this.errStr = 'SCRIPT_ERR_SIG_NULLDUMMY'
1596
- return false
1597
- }
1598
- this.stack.pop()
1599
-
1600
- // stack.push_back(fSuccess ? vchTrue : vchFalse)
1601
- this.stack.push(fSuccess ? Interp.true : Interp.false)
1602
-
1603
- if (opCodeNum === OpCode.OP_CHECKMULTISIGVERIFY) {
1604
- if (fSuccess) {
1605
- this.stack.pop()
1606
- } else {
1607
- this.errStr = 'SCRIPT_ERR_CHECKMULTISIGVERIFY'
1608
- return false
1609
- }
1610
- }
1611
- }
1612
- break
1613
-
1614
- //
1615
- // Byte string operations
1616
- //
1617
- case OpCode.OP_CAT:
1618
- if (this.stack.length < 2) {
1619
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1620
- return false
1621
- }
1622
-
1623
- let vch1 = this.stack[this.stack.length - 2]
1624
- let vch2 = this.stack[this.stack.length - 1]
1625
-
1626
- this.stack[this.stack.length - 2] = Buffer.concat([vch1, vch2])
1627
- this.stack.pop()
1628
- break
1629
-
1630
- case OpCode.OP_SPLIT:
1631
- if (this.stack.length < 2) {
1632
- this.errStr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'
1633
- return false
1634
- }
1635
-
1636
- let data = this.stack[this.stack.length - 2]
1637
- let position = new Bn().fromScriptNumBuffer(
1638
- this.stack[this.stack.length - 1],
1639
- fRequireMinimal
1640
- )
1641
-
1642
- if(position.lt(0) || position.gt(data.length)) {
1643
- this.errStr = 'SCRIPT_ERR_INVALID_SPLIT_RANGE'
1644
- return false
1645
- }
1646
-
1647
- let n1 = data.slice(0, position)
1648
- let n2 = data.slice(position)
1649
-
1650
- this.stack.pop()
1651
- this.stack.pop()
1652
-
1653
- this.stack.push(n1)
1654
- this.stack.push(n2)
1655
-
1656
- break
1657
- // input: a b
1658
- // output: out
1659
- // Converts numeric value a into byte sequence of length b.
1660
- case OpCode.OP_NUM2BIN:
1661
- if (this.stack.length < 2) {
1662
- this.errStr = 'OP_NUM2BIN requires at least two items to be on the stack.'
1663
- }
1664
-
1665
- let size = new Bn().fromScriptNumBuffer(
1666
- this.stack[this.stack.length - 1],
1667
- fRequireMinimal,
1668
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS
1669
- ).toNumber()
1670
- if (size > Interp.MAX_SCRIPT_ELEMENT_SIZE) {
1671
- this.errStr=`It's not currently possible to push data larger than ${Interp.MAX_SCRIPT_ELEMENT_SIZE} bytes.`
1672
- }
1673
-
1674
- this.stack.pop()
1675
- let rawnum = this.stack[this.stack.length - 1]
1676
- // Try to see if we can fit that number in the number of
1677
- // byte requested.
1678
- rawnum = minimallyEncode(rawnum)
1679
-
1680
- if (rawnum.length > size) {
1681
- 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.'
1682
- }
1683
-
1684
- // We already have an element of the right size, we
1685
- // don't need to do anything.
1686
- if (rawnum.length === size) {
1687
- this.stack[this.stack.length - 1] = rawnum
1688
- break
1689
- }
1690
-
1691
- let signbit = 0x00
1692
- if (rawnum.length > 0) {
1693
- signbit = rawnum[rawnum.length - 1] & 0x80
1694
- rawnum[rawnum.length - 1] &= 0x7f
1695
- }
1696
-
1697
- let num = new Array(size)
1698
- num.fill(0)
1699
- let n
1700
- for (n = 0; n < size; n++) {
1701
- num[n] = rawnum[n]
1702
- }
1703
- n = rawnum.length - 1
1704
- while (n++ < size - 2) {
1705
- num[n] = 0x00
1706
- }
1707
-
1708
- num[n] = signbit
1709
-
1710
- this.stack[this.stack.length - 1] = Buffer.from(new Uint8Array(num).buffer, "binary")
1711
- break
1712
-
1713
-
1714
- // input: x
1715
- // output: out
1716
- // Converts byte sequence x into a numeric value.
1717
- case OpCode.OP_BIN2NUM:
1718
- if (this.stack.length < 1) {
1719
- this.errStr='OP_BIN2NUM requires at least one item to be on the stack.'
1720
- }
1721
- let tmp1 = this.stack[this.stack.length - 1]
1722
-
1723
- let tmp2 = minimallyEncode(tmp1)
1724
- // The resulting number must be a valid number.
1725
- this.stack[this.stack.length - 1] = new Bn().fromScriptNumBuffer(tmp2, fRequireMinimal, Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS)
1726
-
1727
- break
1728
- default:
1729
- this.errStr = 'SCRIPT_ERR_BAD_OPCODE'
1730
- return false
1731
- }
1732
- }
1733
-
1734
- return true
1735
- }
1736
-
1737
- /**
1738
- * This function has the same interface as bitcoin core's VerifyScript and is
1739
- * the function you want to use to know if a particular input in a
1740
- * transaction is valid or not. It simply iterates over the results generated
1741
- * by the results method.
1742
- */
1743
- verify (scriptSig, scriptPubKey, tx, nIn, flags, valueBn) {
1744
- let results = this.results(
1745
- scriptSig,
1746
- scriptPubKey,
1747
- tx,
1748
- nIn,
1749
- flags,
1750
- valueBn
1751
- )
1752
- for (let success of results) {
1753
- if (!success) {
1754
- return false
1755
- }
1756
- }
1757
- return true
1758
- }
1759
-
1760
- /**
1761
- * Gives you the results of the execution each operation of the scripSig and
1762
- * scriptPubKey corresponding to a particular input (nIn) for the concerned
1763
- * transaction (tx). Each result can be either true or false. If true, then
1764
- * the operation did not invalidate the transaction. If false, then the
1765
- * operation has invalidated the script, and the transaction is not valid.
1766
- * flags is a number that can pass in some special flags, such as whether or
1767
- * not to execute the redeemScript in a p2sh transaction.
1768
- *
1769
- * This method is translated from bitcoin core's VerifyScript. This function
1770
- * is a generator, thus you can and need to iterate through it. To
1771
- * automatically return true or false, use the verify method.
1772
- */
1773
- * results (scriptSig, scriptPubKey, tx, nIn, flags, valueBn) {
1774
- let stackCopy
1775
-
1776
- this.fromObject({
1777
- script: scriptSig,
1778
- tx: tx,
1779
- nIn: nIn,
1780
- flags: flags,
1781
- valueBn: valueBn
1782
- })
1783
-
1784
- if (
1785
- (flags & Interp.SCRIPT_VERIFY_SIGPUSHONLY) !== 0 &&
1786
- !scriptSig.isPushOnly()
1787
- ) {
1788
- this.errStr = this.errStr || 'SCRIPT_ERR_SIG_PUSHONLY'
1789
- yield false
1790
- }
1791
-
1792
- yield * this.eval()
1793
-
1794
- if (flags & Interp.SCRIPT_VERIFY_P2SH) {
1795
- stackCopy = this.stack.slice()
1796
- }
1797
-
1798
- let stack = this.stack
1799
- this.initialize()
1800
- this.fromObject({
1801
- script: scriptPubKey,
1802
- stack: stack,
1803
- tx: tx,
1804
- nIn: nIn,
1805
- flags: flags,
1806
- valueBn: valueBn
1807
- })
1808
-
1809
- yield * this.eval()
1810
-
1811
- if (this.stack.length === 0) {
1812
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1813
- yield false
1814
- }
1815
-
1816
- let buf = this.stack[this.stack.length - 1]
1817
- if (!Interp.castToBool(buf)) {
1818
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1819
- yield false
1820
- }
1821
-
1822
- // Additional validation for spend-to-script-hash transactions:
1823
- if (flags & Interp.SCRIPT_VERIFY_P2SH && scriptPubKey.isScriptHashOut()) {
1824
- // scriptSig must be literals-only or validation fails
1825
- if (!scriptSig.isPushOnly()) {
1826
- this.errStr = this.errStr || 'SCRIPT_ERR_SIG_PUSHONLY'
1827
- yield false
1828
- }
1829
-
1830
- // Restore stack.
1831
- let tmp = stack
1832
- stack = stackCopy
1833
- stackCopy = tmp
1834
-
1835
- // stack cannot be empty here, because if it was the
1836
- // P2SH HASH <> EQUAL scriptPubKey would be evaluated with
1837
- // an empty stack and the EvalScript above would yield false.
1838
- if (stack.length === 0) {
1839
- throw new Error('internal error - stack copy empty')
1840
- }
1841
-
1842
- let pubKeySerialized = stack[stack.length - 1]
1843
- let scriptPubKey2 = new Script().fromBuffer(pubKeySerialized)
1844
- stack.pop()
1845
-
1846
- this.initialize()
1847
- this.fromObject({
1848
- script: scriptPubKey2,
1849
- stack: stack,
1850
- tx: tx,
1851
- nIn: nIn,
1852
- flags: flags,
1853
- valueBn: valueBn
1854
- })
1855
-
1856
- yield * this.eval()
1857
-
1858
- if (stack.length === 0) {
1859
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1860
- yield false
1861
- }
1862
-
1863
- if (!Interp.castToBool(stack[stack.length - 1])) {
1864
- this.errStr = this.errStr || 'SCRIPT_ERR_EVAL_FALSE'
1865
- yield false
1866
- } else {
1867
- yield true
1868
- }
1869
- }
1870
-
1871
- // The CLEANSTACK check is only performed after potential P2SH evaluation,
1872
- // as the non-P2SH evaluation of a P2SH script will obviously not result in
1873
- // a clean stack (the P2SH inputs remain).
1874
- if ((flags & Interp.SCRIPT_VERIFY_CLEANSTACK) !== 0) {
1875
- // Disallow CLEANSTACK without P2SH, as otherwise a switch
1876
- // CLEANSTACK->P2SH+CLEANSTACK would be possible, which is not a softfork
1877
- // (and P2SH should be one).
1878
- if (!(flags & Interp.SCRIPT_VERIFY_P2SH)) {
1879
- throw new Error('cannot use CLEANSTACK without P2SH')
1880
- }
1881
- if (stack.length !== 1) {
1882
- this.errStr = this.errStr || 'SCRIPT_ERR_CLEANSTACK'
1883
- yield false
1884
- }
1885
- }
1886
-
1887
- yield true
1888
- }
1889
-
1890
- /**
1891
- * If the script has failed, this methods returns valuable debug
1892
- * information about exactly where the script failed. It is a
1893
- * JSON-compatible object so it can be easily stringified. pc refers to the
1894
- * currently executing opcode.
1895
- */
1896
- getDebugObject () {
1897
- let pc = this.pc - 1 // pc is incremented immediately after getting
1898
- return {
1899
- errStr: this.errStr,
1900
- scriptStr: this.script ? this.script.toString() : 'no script found',
1901
- pc: pc,
1902
- stack: this.stack.map(buf => buf.toString('hex')),
1903
- altStack: this.altStack.map(buf => buf.toString('hex')),
1904
- opCodeStr: this.script
1905
- ? OpCode.fromNumber(this.script.chunks[pc].opCodeNum).toString()
1906
- : 'no script found'
1907
- }
1908
- }
1909
-
1910
- getDebugString () {
1911
- return JSON.stringify(this.getDebugObject(), null, 2)
1912
- }
1913
- }
1914
-
1915
- Interp.true = Buffer.from([1])
1916
- Interp.false = Buffer.from([])
1917
- // TODO: https://github.com/bitcoin-sv/bitcoin-sv/blob/b489c32ef55d428c5c3825d5526de018031a20af/src/policy/policy.h
1918
- /** Maximum stack memory usage (used instead of MAX_SCRIPT_ELEMENT_SIZE and MAX_STACK_ELEMENTS) after Genesis. **/
1919
-
1920
- Interp.MAX_SCRIPT_ELEMENT_SIZE = 500*1000
1921
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS = 10*1000 // 10KB
1922
-
1923
- Interp.LOCKTIME_THRESHOLD = 500000000 // Tue Nov 5 00:53:20 1985 UTC
1924
-
1925
- // flags taken from bitcoin core
1926
- // bitcoin core commit: b5d1b1092998bc95313856d535c632ea5a8f9104
1927
- Interp.SCRIPT_VERIFY_NONE = 0
1928
-
1929
- // Evaluate P2SH subScripts (softfork safe, Bip16).
1930
- Interp.SCRIPT_VERIFY_P2SH = 1 << 0
1931
-
1932
- // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
1933
- // Passing a pubKey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) to checksig causes that pubKey to be
1934
- // skipped (not softfork safe: this flag can widen the validity of OP_CHECKSIG OP_NOT).
1935
- Interp.SCRIPT_VERIFY_STRICTENC = 1 << 1
1936
-
1937
- // Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, Bip62 rule 1)
1938
- Interp.SCRIPT_VERIFY_DERSIG = 1 << 2
1939
-
1940
- // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
1941
- // (softfork safe, Bip62 rule 5).
1942
- Interp.SCRIPT_VERIFY_LOW_S = 1 << 3
1943
-
1944
- // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, Bip62 rule 7).
1945
- Interp.SCRIPT_VERIFY_NULLDUMMY = 1 << 4
1946
-
1947
- // Using a non-push operator in the scriptSig causes script failure (softfork safe, Bip62 rule 2).
1948
- Interp.SCRIPT_VERIFY_SIGPUSHONLY = 1 << 5
1949
-
1950
- // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
1951
- // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
1952
- // any other push causes the script to fail (Bip62 rule 3).
1953
- // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (Bip62 rule 4).
1954
- // (softfork safe)
1955
- Interp.SCRIPT_VERIFY_MINIMALDATA = 1 << 6
1956
-
1957
- // Discourage use of NOPs reserved for upgrades (NOP1-10)
1958
- //
1959
- // Provided so that nodes can avoid accepting or mining transactions
1960
- // containing executed NOP's whose meaning may change after a soft-fork,
1961
- // thus rendering the script invalid; with this flag set executing
1962
- // discouraged NOPs fails the script. This verification flag will never be
1963
- // a mandatory flag applied to scripts in a block. NOPs that are not
1964
- // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
1965
- Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = 1 << 7
1966
-
1967
- // Require that only a single stack element remains after evaluation. This
1968
- // changes the success criterion from "At least one stack element must
1969
- // remain, and when interpreted as a boolean, it must be true" to "Exactly
1970
- // one stack element must remain, and when interpreted as a boolean, it must
1971
- // be true". (softfork safe, Bip62 rule 6)
1972
- // Note: CLEANSTACK should never be used without P2SH.
1973
- Interp.SCRIPT_VERIFY_CLEANSTACK = 1 << 8
1974
-
1975
- // Verify CHECKLOCKTIMEVERIFY
1976
- //
1977
- // See Bip65 for details.
1978
- Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = 1 << 9
1979
-
1980
- // support CHECKSEQUENCEVERIFY opCode
1981
- //
1982
- // See Bip112 for details
1983
- Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = 1 << 10
1984
-
1985
- // used for UAHF
1986
- // https://github.com/Bitcoin-UAHF/spec/blob/master/replay-protected-sighash.md
1987
- Interp.SCRIPT_ENABLE_SIGHASH_FORKID = 1 << 16
1988
-
1989
- // These are the things we wish to verify by default. At the time of writing,
1990
- // P2SH and CHECKLOCKTIMEVERIFY are both active, but CHECKSEQUENCEVERIFY is
1991
- // not.
1992
-
1993
-
1994
- // https://github.com/bitcoin-sv/bitcoin-sv/blob/86eb5e8bdf5573c3cd844a1d81bd4fb151b909e0/src/policy/policy.h#L176
1995
- // https://github.com/bitcoin-sv/bitcoin-sv/blob/86eb5e8bdf5573c3cd844a1d81bd4fb151b909e0/src/script/standard.h#L41
1996
- Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH |
1997
- Interp.SCRIPT_VERIFY_STRICTENC |
1998
- Interp.SCRIPT_ENABLE_SIGHASH_FORKID |
1999
- Interp.SCRIPT_VERIFY_LOW_S |
2000
- Interp.SCRIPT_VERIFY_NULLDUMMY |
2001
- Interp.SCRIPT_VERIFY_DERSIG |
2002
- Interp.SCRIPT_VERIFY_MINIMALDATA |
2003
- Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
2004
- Interp.SCRIPT_VERIFY_CLEANSTACK |
2005
- Interp.SCRIPT_VERIFY_SIGPUSHONLY |
2006
- Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY |
2007
- Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
2008
-
2009
-
2010
- // Interp.SCRIPT_VERIFY_P2SH | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
2011
- // Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH |
2012
-
2013
- export { Interp }