@bsv/sdk 2.0.13 → 2.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/README.md +2 -0
  2. package/dist/cjs/package.json +14 -14
  3. package/dist/cjs/src/identity/IdentityClient.js +3 -3
  4. package/dist/cjs/src/identity/IdentityClient.js.map +1 -1
  5. package/dist/cjs/src/identity/types/index.js +1 -1
  6. package/dist/cjs/src/identity/types/index.js.map +1 -1
  7. package/dist/cjs/src/primitives/Hash.js +1 -1
  8. package/dist/cjs/src/primitives/Hash.js.map +1 -1
  9. package/dist/cjs/src/primitives/TransactionSignature.js +10 -3
  10. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  11. package/dist/cjs/src/script/Script.js +60 -13
  12. package/dist/cjs/src/script/Script.js.map +1 -1
  13. package/dist/cjs/src/script/Spend.js +434 -59
  14. package/dist/cjs/src/script/Spend.js.map +1 -1
  15. package/dist/cjs/src/storage/StorageUploader.js +315 -96
  16. package/dist/cjs/src/storage/StorageUploader.js.map +1 -1
  17. package/dist/cjs/src/storage/index.js +3 -1
  18. package/dist/cjs/src/storage/index.js.map +1 -1
  19. package/dist/cjs/src/transaction/http/BinaryFetchClient.js +6 -2
  20. package/dist/cjs/src/transaction/http/BinaryFetchClient.js.map +1 -1
  21. package/dist/cjs/src/transaction/http/DefaultHttpClient.js +8 -4
  22. package/dist/cjs/src/transaction/http/DefaultHttpClient.js.map +1 -1
  23. package/dist/cjs/src/wallet/WERR_REVIEW_ACTIONS.js +2 -2
  24. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  25. package/dist/esm/src/identity/IdentityClient.js +3 -3
  26. package/dist/esm/src/identity/IdentityClient.js.map +1 -1
  27. package/dist/esm/src/identity/types/index.js +1 -1
  28. package/dist/esm/src/identity/types/index.js.map +1 -1
  29. package/dist/esm/src/primitives/Hash.js +1 -1
  30. package/dist/esm/src/primitives/Hash.js.map +1 -1
  31. package/dist/esm/src/primitives/TransactionSignature.js +10 -3
  32. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
  33. package/dist/esm/src/script/Script.js +60 -13
  34. package/dist/esm/src/script/Script.js.map +1 -1
  35. package/dist/esm/src/script/Spend.js +438 -59
  36. package/dist/esm/src/script/Spend.js.map +1 -1
  37. package/dist/esm/src/storage/StorageUploader.js +319 -95
  38. package/dist/esm/src/storage/StorageUploader.js.map +1 -1
  39. package/dist/esm/src/storage/index.js +1 -1
  40. package/dist/esm/src/storage/index.js.map +1 -1
  41. package/dist/esm/src/transaction/http/BinaryFetchClient.js +6 -2
  42. package/dist/esm/src/transaction/http/BinaryFetchClient.js.map +1 -1
  43. package/dist/esm/src/transaction/http/DefaultHttpClient.js +8 -4
  44. package/dist/esm/src/transaction/http/DefaultHttpClient.js.map +1 -1
  45. package/dist/esm/src/wallet/WERR_REVIEW_ACTIONS.js +2 -2
  46. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  47. package/dist/types/src/primitives/TransactionSignature.d.ts +1 -0
  48. package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -1
  49. package/dist/types/src/script/Script.d.ts +1 -0
  50. package/dist/types/src/script/Script.d.ts.map +1 -1
  51. package/dist/types/src/script/ScriptChunk.d.ts +1 -0
  52. package/dist/types/src/script/ScriptChunk.d.ts.map +1 -1
  53. package/dist/types/src/script/Spend.d.ts +29 -0
  54. package/dist/types/src/script/Spend.d.ts.map +1 -1
  55. package/dist/types/src/storage/StorageUploader.d.ts +94 -55
  56. package/dist/types/src/storage/StorageUploader.d.ts.map +1 -1
  57. package/dist/types/src/storage/index.d.ts +1 -1
  58. package/dist/types/src/storage/index.d.ts.map +1 -1
  59. package/dist/types/src/transaction/http/BinaryFetchClient.d.ts.map +1 -1
  60. package/dist/types/src/transaction/http/DefaultHttpClient.d.ts +2 -2
  61. package/dist/types/src/transaction/http/DefaultHttpClient.d.ts.map +1 -1
  62. package/dist/types/src/wallet/WERR_REVIEW_ACTIONS.d.ts +2 -2
  63. package/dist/types/src/wallet/Wallet.interfaces.d.ts +2 -2
  64. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  65. package/dist/umd/bundle.js +4 -5
  66. package/docs/index.md +3 -1
  67. package/docs/reference/identity.md +1 -1
  68. package/docs/reference/primitives.md +1 -0
  69. package/docs/reference/script.md +7 -0
  70. package/docs/reference/storage.md +214 -85
  71. package/docs/reference/transaction.md +4 -4
  72. package/docs/reference/wallet.md +4 -4
  73. package/package.json +14 -14
  74. package/src/identity/IdentityClient.ts +3 -3
  75. package/src/identity/__tests/IdentityClient.additional.test.ts +3 -3
  76. package/src/identity/types/index.ts +1 -1
  77. package/src/primitives/Hash.ts +1 -1
  78. package/src/primitives/TransactionSignature.ts +11 -3
  79. package/src/script/Script.ts +59 -13
  80. package/src/script/ScriptChunk.ts +1 -0
  81. package/src/script/Spend.ts +483 -61
  82. package/src/script/__tests/NormativeVectors.test.ts +465 -0
  83. package/src/script/__tests/fixtures/SOURCES.md +25 -0
  84. package/src/script/__tests/fixtures/bitcoin-sv/script_tests.json +2591 -0
  85. package/src/script/__tests/fixtures/bitcoin-sv/sighash.json +1003 -0
  86. package/src/script/__tests/fixtures/bitcoin-sv/tx_invalid.json +285 -0
  87. package/src/script/__tests/fixtures/bitcoin-sv/tx_valid.json +367 -0
  88. package/src/script/__tests/fixtures/teranode/script_tests.json +2432 -0
  89. package/src/script/__tests/fixtures/teranode/sighash.json +1003 -0
  90. package/src/script/__tests/fixtures/teranode/tx_invalid.json +285 -0
  91. package/src/script/__tests/fixtures/teranode/tx_valid.json +367 -0
  92. package/src/storage/StorageUploader.ts +427 -105
  93. package/src/storage/__tests/StorageUploader.test.ts +881 -64
  94. package/src/storage/index.ts +1 -1
  95. package/src/transaction/broadcasters/__tests/ARC.test.ts +26 -4
  96. package/src/transaction/broadcasters/__tests/WhatsOnChainBroadcaster.test.ts +26 -4
  97. package/src/transaction/chaintrackers/__tests/WhatsOnChainChainTracker.test.ts +32 -10
  98. package/src/transaction/http/BinaryFetchClient.ts +5 -2
  99. package/src/transaction/http/DefaultHttpClient.ts +7 -4
  100. package/src/transaction/http/__tests/DefaultHttpClient.additional.test.ts +19 -1
  101. package/src/wallet/WERR_REVIEW_ACTIONS.ts +2 -2
  102. package/src/wallet/Wallet.interfaces.ts +2 -2
  103. package/dist/cjs/src/auth/clients/__tests__/AuthFetch.additional.test.js +0 -827
  104. package/dist/cjs/src/auth/clients/__tests__/AuthFetch.additional.test.js.map +0 -1
  105. package/dist/cjs/src/auth/clients/__tests__/AuthFetch.test.js +0 -266
  106. package/dist/cjs/src/auth/clients/__tests__/AuthFetch.test.js.map +0 -1
  107. package/dist/cjs/src/auth/transports/__tests__/SimplifiedFetchTransport.additional.test.js +0 -654
  108. package/dist/cjs/src/auth/transports/__tests__/SimplifiedFetchTransport.additional.test.js.map +0 -1
  109. package/dist/cjs/src/auth/transports/__tests__/SimplifiedFetchTransport.test.js +0 -144
  110. package/dist/cjs/src/auth/transports/__tests__/SimplifiedFetchTransport.test.js.map +0 -1
  111. package/dist/esm/src/auth/clients/__tests__/AuthFetch.additional.test.js +0 -825
  112. package/dist/esm/src/auth/clients/__tests__/AuthFetch.additional.test.js.map +0 -1
  113. package/dist/esm/src/auth/clients/__tests__/AuthFetch.test.js +0 -264
  114. package/dist/esm/src/auth/clients/__tests__/AuthFetch.test.js.map +0 -1
  115. package/dist/esm/src/auth/transports/__tests__/SimplifiedFetchTransport.additional.test.js +0 -619
  116. package/dist/esm/src/auth/transports/__tests__/SimplifiedFetchTransport.additional.test.js.map +0 -1
  117. package/dist/esm/src/auth/transports/__tests__/SimplifiedFetchTransport.test.js +0 -109
  118. package/dist/esm/src/auth/transports/__tests__/SimplifiedFetchTransport.test.js.map +0 -1
  119. package/dist/types/src/auth/clients/__tests__/AuthFetch.additional.test.d.ts +0 -21
  120. package/dist/types/src/auth/clients/__tests__/AuthFetch.additional.test.d.ts.map +0 -1
  121. package/dist/types/src/auth/clients/__tests__/AuthFetch.test.d.ts +0 -2
  122. package/dist/types/src/auth/clients/__tests__/AuthFetch.test.d.ts.map +0 -1
  123. package/dist/types/src/auth/transports/__tests__/SimplifiedFetchTransport.additional.test.d.ts +0 -2
  124. package/dist/types/src/auth/transports/__tests__/SimplifiedFetchTransport.additional.test.d.ts.map +0 -1
  125. package/dist/types/src/auth/transports/__tests__/SimplifiedFetchTransport.test.d.ts +0 -2
  126. package/dist/types/src/auth/transports/__tests__/SimplifiedFetchTransport.test.d.ts.map +0 -1
  127. package/dist/umd/bundle.js.map +0 -1
@@ -3,7 +3,6 @@ import BigNumber from './BigNumber.js'
3
3
  import * as Hash from './Hash.js'
4
4
  import { toArray, Writer } from './utils.js'
5
5
  import Script from '../script/Script.js'
6
- import OP from '../script/OP.js'
7
6
  import TransactionInput from '../transaction/TransactionInput.js'
8
7
  import TransactionOutput from '../transaction/TransactionOutput.js'
9
8
 
@@ -56,8 +55,8 @@ export default class TransactionSignature extends Signature {
56
55
  const isNone = (params.scope & 31) === TransactionSignature.SIGHASH_NONE
57
56
  const isAll = (params.scope & 31) === TransactionSignature.SIGHASH_ALL || (!isSingle && !isNone)
58
57
 
59
- const subscript = new Script([...params.subscript.chunks])
60
- subscript.findAndDelete(new Script().writeOpCode(OP.OP_CODESEPARATOR))
58
+ const subscript = Script.fromBinary(params.subscript.toBinary())
59
+ subscript.removeCodeseparators()
61
60
 
62
61
  const currentInput = {
63
62
  sourceTXID: params.sourceTXID,
@@ -332,6 +331,15 @@ export default class TransactionSignature extends Signature {
332
331
  return new Uint8Array(0)
333
332
  }
334
333
 
334
+ static usesOtdaSingleBug (params: TransactionSignatureFormatParams): boolean {
335
+ const hasForkId = (params.scope & TransactionSignature.SIGHASH_FORKID) !== 0
336
+ const hasChronicle = params.ignoreChronicle !== true && (params.scope & TransactionSignature.SIGHASH_CHRONICLE) !== 0
337
+ const usesOtda = !hasForkId || (hasForkId && hasChronicle)
338
+ return usesOtda &&
339
+ (params.scope & 31) === TransactionSignature.SIGHASH_SINGLE &&
340
+ params.inputIndex >= params.outputs.length
341
+ }
342
+
335
343
  // The format used in a tx
336
344
  static fromChecksigFormat (buf: number[]): TransactionSignature {
337
345
  if (buf.length === 0) {
@@ -335,14 +335,11 @@ export default class Script {
335
335
  * @returns This script instance for chaining.
336
336
  */
337
337
  removeCodeseparators (): Script {
338
- this.invalidateSerializationCaches()
339
- const chunks: ScriptChunk[] = []
340
- for (let i = 0; i < this.chunks.length; i++) {
341
- if (this.chunks[i].op !== OP.OP_CODESEPARATOR) {
342
- chunks.push(this.chunks[i])
343
- }
344
- }
345
- this.chunks = chunks
338
+ const bytes = this.toUint8Array()
339
+ this.rawBytesCache = Uint8Array.from(Script.removeOpcodeBytes(bytes, OP.OP_CODESEPARATOR))
340
+ this.hexCache = undefined
341
+ this._chunks = []
342
+ this.parsed = false
346
343
  return this
347
344
  }
348
345
 
@@ -585,18 +582,22 @@ export default class Script {
585
582
  const end = Math.min(pos + len, length)
586
583
  chunks.push({
587
584
  data: Script.copyRange(bytes, pos, end),
588
- op
585
+ op,
586
+ invalidLength: end - pos !== len
589
587
  })
590
588
  pos = end
591
589
  } else if (op === OP.OP_PUSHDATA1) {
592
- const len = pos < length ? bytes[pos++] ?? 0 : 0
590
+ const hasLength = pos < length
591
+ const len = hasLength ? bytes[pos++] ?? 0 : 0
593
592
  const end = Math.min(pos + len, length)
594
593
  chunks.push({
595
594
  data: Script.copyRange(bytes, pos, end),
596
- op
595
+ op,
596
+ invalidLength: !hasLength || end - pos !== len
597
597
  })
598
598
  pos = end
599
599
  } else if (op === OP.OP_PUSHDATA2) {
600
+ const hasLength = pos + 1 < length
600
601
  const b0 = bytes[pos] ?? 0
601
602
  const b1 = bytes[pos + 1] ?? 0
602
603
  const len = b0 | (b1 << 8)
@@ -604,10 +605,12 @@ export default class Script {
604
605
  const end = Math.min(pos + len, length)
605
606
  chunks.push({
606
607
  data: Script.copyRange(bytes, pos, end),
607
- op
608
+ op,
609
+ invalidLength: !hasLength || end - pos !== len
608
610
  })
609
611
  pos = end
610
612
  } else if (op === OP.OP_PUSHDATA4) {
613
+ const hasLength = pos + 3 < length
611
614
  const len =
612
615
  ((bytes[pos] ?? 0) |
613
616
  ((bytes[pos + 1] ?? 0) << 8) |
@@ -618,7 +621,8 @@ export default class Script {
618
621
  const end = Math.min(pos + len, length)
619
622
  chunks.push({
620
623
  data: Script.copyRange(bytes, pos, end),
621
- op
624
+ op,
625
+ invalidLength: !hasLength || end - pos !== len
622
626
  })
623
627
  pos = end
624
628
  } else {
@@ -629,6 +633,48 @@ export default class Script {
629
633
  return chunks
630
634
  }
631
635
 
636
+ private static removeOpcodeBytes (bytes: ArrayLike<number>, opcode: number): number[] {
637
+ const out: number[] = []
638
+ const length = bytes.length
639
+ let pos = 0
640
+
641
+ while (pos < length) {
642
+ const start = pos
643
+ const op = bytes[pos++] ?? 0
644
+ let dataStart = pos
645
+ let dataLength = 0
646
+
647
+ if (op > 0 && op < OP.OP_PUSHDATA1) {
648
+ dataLength = op
649
+ } else if (op === OP.OP_PUSHDATA1) {
650
+ if (pos < length) dataLength = bytes[pos++] ?? 0
651
+ dataStart = pos
652
+ } else if (op === OP.OP_PUSHDATA2) {
653
+ if (pos + 1 < length) dataLength = (bytes[pos] ?? 0) | ((bytes[pos + 1] ?? 0) << 8)
654
+ pos = Math.min(pos + 2, length)
655
+ dataStart = pos
656
+ } else if (op === OP.OP_PUSHDATA4) {
657
+ if (pos + 3 < length) {
658
+ dataLength =
659
+ ((bytes[pos] ?? 0) |
660
+ ((bytes[pos + 1] ?? 0) << 8) |
661
+ ((bytes[pos + 2] ?? 0) << 16) |
662
+ ((bytes[pos + 3] ?? 0) << 24)) >>> 0
663
+ }
664
+ pos = Math.min(pos + 4, length)
665
+ dataStart = pos
666
+ }
667
+
668
+ const end = Math.min(dataStart + dataLength, length)
669
+ if (op !== opcode) {
670
+ for (let i = start; i < end; i++) out.push(bytes[i] ?? 0)
671
+ }
672
+ pos = end
673
+ }
674
+
675
+ return out
676
+ }
677
+
632
678
  private static copyRange (
633
679
  bytes: ArrayLike<number>,
634
680
  start: number,
@@ -4,4 +4,5 @@
4
4
  export default interface ScriptChunk {
5
5
  op: number
6
6
  data?: number[]
7
+ invalidLength?: boolean
7
8
  }