bsv2 2.0.23 → 2.0.24

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.
package/lib/interp.js CHANGED
@@ -17,6 +17,7 @@
17
17
  */
18
18
  'use strict'
19
19
 
20
+ import { Br } from './br'
20
21
  import { Bn } from './bn'
21
22
  import { Bw } from './bw'
22
23
  import { cmp } from './cmp'
@@ -28,6 +29,51 @@ import { Sig } from './sig'
28
29
  import { Struct } from './struct'
29
30
  import { Tx } from './tx'
30
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
+ }
31
77
 
32
78
  class Interp extends Struct {
33
79
  constructor (
@@ -154,7 +200,9 @@ import { TxIn } from './tx-in'
154
200
  nOpCount: this.nOpCount,
155
201
  ifStack: this.ifStack,
156
202
  errStr: this.errStr,
157
- flags: this.flags
203
+ flags: this.flags,
204
+ preImage: this?.preImage ? this.preImage.toString("hex"):"",
205
+ hashBuf: this?.hashBuf ? this.hashBuf.toString("hex"):""
158
206
  }
159
207
  }
160
208
 
@@ -446,11 +494,10 @@ import { TxIn } from './tx-in'
446
494
  this.errStr = 'SCRIPT_ERR_BAD_OPCODE'
447
495
  return false
448
496
  }
449
- /* REMOVING LIMIT
450
497
  if (chunk.buf && chunk.buf.length > Interp.MAX_SCRIPT_ELEMENT_SIZE) {
451
498
  this.errStr = 'SCRIPT_ERR_PUSH_SIZE'
452
499
  return false
453
- }*/
500
+ }
454
501
 
455
502
  // Note how OpCode.OP_RESERVED does not count towards the opCode limit.
456
503
  if (opCodeNum > OpCode.OP_16 && ++this.nOpCount > 201) {
@@ -459,8 +506,6 @@ import { TxIn } from './tx-in'
459
506
  }
460
507
 
461
508
  if (
462
- opCodeNum === OpCode.OP_LEFT ||
463
- opCodeNum === OpCode.OP_RIGHT ||
464
509
  opCodeNum === OpCode.OP_2MUL ||
465
510
  opCodeNum === OpCode.OP_2DIV
466
511
  ) {
@@ -1319,13 +1364,19 @@ import { TxIn } from './tx-in'
1319
1364
 
1320
1365
  let bufSig = this.stack[this.stack.length - 2]
1321
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
+ }
1322
1374
 
1323
1375
  // Subset of script starting at the most recent codeseparator
1324
1376
  // CScript scriptCode(pBeginCodeHash, pend)
1325
1377
  let subScript = new Script().fromObject({
1326
1378
  chunks: this.script.chunks.slice(this.pBeginCodeHash)
1327
1379
  })
1328
-
1329
1380
  // https://github.com/Bitcoin-UAHF/spec/blob/master/replay-protected-sighash.md
1330
1381
  let nHashType =
1331
1382
  bufSig.length > 0 ? bufSig.readUInt8(bufSig.length - 1) : 0
@@ -1338,18 +1389,39 @@ import { TxIn } from './tx-in'
1338
1389
  subScript.findAndDelete(new Script().writeBuffer(bufSig))
1339
1390
  }
1340
1391
 
1341
- if (
1342
- !this.checkSigEncoding(bufSig) ||
1343
- !this.checkPubKeyEncoding(bufPubKey)
1344
- ) {
1345
- // serror is set
1346
- return false
1347
- }
1348
-
1349
1392
  let fSuccess
1350
1393
  try {
1351
1394
  let sig = new Sig().fromTxFormat(bufSig)
1352
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
+ }
1353
1425
  fSuccess = this.tx.verify(
1354
1426
  sig,
1355
1427
  pubKey,
@@ -1357,8 +1429,9 @@ import { TxIn } from './tx-in'
1357
1429
  subScript,
1358
1430
  Boolean(this.flags & Interp.SCRIPT_VERIFY_LOW_S),
1359
1431
  this.valueBn,
1360
- this.flags
1432
+ Tx.SCRIPT_ENABLE_SIGHASH_FORKID
1361
1433
  )
1434
+
1362
1435
  } catch (e) {
1363
1436
  // invalid sig or pubKey
1364
1437
  fSuccess = false
@@ -1581,7 +1654,77 @@ import { TxIn } from './tx-in'
1581
1654
  this.stack.push(n2)
1582
1655
 
1583
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
+ }
1584
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
1585
1728
  default:
1586
1729
  this.errStr = 'SCRIPT_ERR_BAD_OPCODE'
1587
1730
  return false
@@ -1774,8 +1917,8 @@ import { TxIn } from './tx-in'
1774
1917
  // TODO: https://github.com/bitcoin-sv/bitcoin-sv/blob/b489c32ef55d428c5c3825d5526de018031a20af/src/policy/policy.h
1775
1918
  /** Maximum stack memory usage (used instead of MAX_SCRIPT_ELEMENT_SIZE and MAX_STACK_ELEMENTS) after Genesis. **/
1776
1919
 
1777
- //Interp.MAX_SCRIPT_ELEMENT_SIZE = 5000000 // To be removed
1778
- Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS = 10000 // 10KB
1920
+ Interp.MAX_SCRIPT_ELEMENT_SIZE = 500*1000
1921
+ Interp.DEFAULT_SCRIPT_NUM_LENGTH_POLICY_AFTER_GENESIS = 10*1000 // 10KB
1779
1922
 
1780
1923
  Interp.LOCKTIME_THRESHOLD = 500000000 // Tue Nov 5 00:53:20 1985 UTC
1781
1924
 
@@ -1846,8 +1989,25 @@ import { TxIn } from './tx-in'
1846
1989
  // These are the things we wish to verify by default. At the time of writing,
1847
1990
  // P2SH and CHECKLOCKTIMEVERIFY are both active, but CHECKSEQUENCEVERIFY is
1848
1991
  // not.
1849
- Interp.defaultFlags =
1850
- Interp.SCRIPT_VERIFY_P2SH | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
1851
- // Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY | Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
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 |
1852
2012
 
1853
2013
  export { Interp }
package/lib/tx.js CHANGED
@@ -140,7 +140,7 @@ class Tx extends Struct {
140
140
  * p2sh transaction, subScript is usually the redeemScript. If you're not
141
141
  * normal because you're using OP_CODESEPARATORs, you know what to do.
142
142
  */
143
- sighash (nHashType, nIn, subScript, valueBn, flags = 0, hashCache = new HashCache()) {
143
+ sighash (nHashType, nIn, subScript, valueBn, flags = Tx.SCRIPT_ENABLE_SIGHASH_FORKID, hashCache = new HashCache()) {
144
144
  const buf = this.sighashPreimage(nHashType, nIn, subScript, valueBn, flags, hashCache)
145
145
  if (buf.compare(Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')) === 0) {
146
146
  return buf
@@ -160,7 +160,7 @@ class Tx extends Struct {
160
160
  return workersResult.resbuf
161
161
  }
162
162
 
163
- sighashPreimage (nHashType, nIn, subScript, valueBn, flags = 0, hashCache = new HashCache()) {
163
+ sighashPreimage (nHashType, nIn, subScript, valueBn, flags = Tx.SCRIPT_ENABLE_SIGHASH_FORKID, hashCache = new HashCache()) {
164
164
  // start with UAHF part (Bitcoin SV)
165
165
  // https://github.com/Bitcoin-UAHF/spec/blob/master/replay-protected-sighash.md
166
166
  if (
@@ -208,7 +208,7 @@ class Tx extends Struct {
208
208
  bw.write(hashOutputs)
209
209
  bw.writeUInt32LE(this.nLockTime)
210
210
  bw.writeUInt32LE(nHashType >>> 0)
211
-
211
+
212
212
  return bw.toBuffer()
213
213
  }
214
214
 
@@ -295,6 +295,7 @@ class Tx extends Struct {
295
295
  // This function returns a signature but does not update any inputs
296
296
  sign (keyPair, nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID, nIn, subScript, valueBn, flags = Tx.SCRIPT_ENABLE_SIGHASH_FORKID, hashCache = {}) {
297
297
  const hashBuf = this.sighash(nHashType, nIn, subScript, valueBn, flags, hashCache)
298
+ this.preImageBuf = hashBuf
298
299
  const sig = Ecdsa.sign(hashBuf, keyPair, 'little').fromObject({
299
300
  nHashType: nHashType
300
301
  })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bsv2",
3
3
  "description": "Javascript library for Bitcoin SV (BSV).",
4
- "version": "2.0.23",
4
+ "version": "2.0.24",
5
5
  "author": "Tomás Díaz",
6
6
  "homepage": "https://github.com/samooth/bsv",
7
7
  "source": "entry.js",