@bsv/sdk 1.2.9 → 1.2.10

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.
@@ -592,6 +592,7 @@ export class Beef {
592
592
  constructor(version?: BeefVersion)
593
593
  get magic(): number
594
594
  findTxid(txid: string): BeefTx | undefined
595
+ makeTxidOnly(txid: string): BeefTx | undefined
595
596
  findBump(txid: string): MerklePath | undefined
596
597
  findTransactionForSigning(txid: string): Transaction | undefined
597
598
  findAtomicTransaction(txid: string): Transaction | undefined
@@ -779,6 +780,23 @@ Argument Details
779
780
  + **allowTxidOnly**
780
781
  + optional. If true, transaction txid only is assumed valid
781
782
 
783
+ #### Method makeTxidOnly
784
+
785
+ Replaces `BeefTx` for this txid with txidOnly.
786
+
787
+ Replacement is done so that a `clone()` can be
788
+ updated by this method without affecting the
789
+ original.
790
+
791
+ ```ts
792
+ makeTxidOnly(txid: string): BeefTx | undefined
793
+ ```
794
+ See also: [BeefTx](#class-beeftx)
795
+
796
+ Returns
797
+
798
+ undefined if txid is unknown.
799
+
782
800
  #### Method mergeBump
783
801
 
784
802
  Merge a MerklePath that is assumed to be fully valid.
@@ -847,7 +865,7 @@ Argument Details
847
865
  #### Method sortTxs
848
866
 
849
867
  Sort the `txs` by input txid dependency order:
850
- - Oldest Tx Anchored by Path
868
+ - Oldest Tx Anchored by Path or txid only
851
869
  - Newer Txs depending on Older parents
852
870
  - Newest Tx
853
871
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.2.9",
3
+ "version": "1.2.10",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -98,6 +98,26 @@ export class Beef {
98
98
  return this.txs.find(tx => tx.txid === txid)
99
99
  }
100
100
 
101
+ /**
102
+ * Replaces `BeefTx` for this txid with txidOnly.
103
+ *
104
+ * Replacement is done so that a `clone()` can be
105
+ * updated by this method without affecting the
106
+ * original.
107
+ *
108
+ * @param txid
109
+ * @returns undefined if txid is unknown.
110
+ */
111
+ makeTxidOnly (txid: string): BeefTx | undefined {
112
+ const i = this.txs.findIndex(tx => tx.txid === txid)
113
+ if (i === -1) return undefined
114
+ let btx = this.txs[i]
115
+ if (btx.isTxidOnly) { return btx }
116
+ this.txs.slice(i, i + 1)
117
+ btx = this.mergeTxidOnly(txid)
118
+ return btx
119
+ }
120
+
101
121
  /**
102
122
  * @returns `MerklePath` with level zero hash equal to txid or undefined.
103
123
  */
@@ -518,7 +538,7 @@ export class Beef {
518
538
 
519
539
  /**
520
540
  * Sort the `txs` by input txid dependency order:
521
- * - Oldest Tx Anchored by Path
541
+ * - Oldest Tx Anchored by Path or txid only
522
542
  * - Newer Txs depending on Older parents
523
543
  * - Newest Tx
524
544
  *
@@ -553,7 +573,10 @@ export class Beef {
553
573
  if (tx.isValid) {
554
574
  validTxids[tx.txid] = true
555
575
  result.push(tx)
556
- } else if (tx.isTxidOnly) { txidOnly.push(tx) } else { queue.push(tx) }
576
+ } else if (tx.isTxidOnly && tx.inputTxids.length === 0) {
577
+ validTxids[tx.txid] = true
578
+ txidOnly.push(tx)
579
+ } else { queue.push(tx) }
557
580
  }
558
581
 
559
582
  // Hashtable of unknown input txids used to fund transactions without their own proof.
@@ -564,17 +587,18 @@ export class Beef {
564
587
  const possiblyMissingInputs = queue
565
588
  queue = []
566
589
 
590
+ // all tx are isValid false, hasProof false.
591
+ // if isTxidOnly then has inputTxids
567
592
  for (const tx of possiblyMissingInputs) {
568
593
  let hasMissingInput = false
569
- if (!tx.isValid) {
570
- // For all the unproven transactions,
571
- // link their inputs that exist in this beef,
572
- // make a note of missing inputs.
573
- for (const inputTxid of tx.inputTxids) {
574
- if (!txidToTx[inputTxid]) {
575
- missingInputs[inputTxid] = true
576
- hasMissingInput = true
577
- }
594
+
595
+ // For all the unproven transactions,
596
+ // link their inputs that exist in this beef,
597
+ // make a note of missing inputs.
598
+ for (const inputTxid of tx.inputTxids) {
599
+ if (!txidToTx[inputTxid]) {
600
+ missingInputs[inputTxid] = true
601
+ hasMissingInput = true
578
602
  }
579
603
  }
580
604
  if (hasMissingInput) { txsMissingInputs.push(tx) } else { queue.push(tx) }
@@ -584,6 +608,8 @@ export class Beef {
584
608
  while (queue.length > 0) {
585
609
  const oldQueue = queue
586
610
  queue = []
611
+ // all tx are isValid false, hasProof false.
612
+ // if isTxidOnly then has inputTxids
587
613
  for (const tx of oldQueue) {
588
614
  if (tx.inputTxids.every(txid => validTxids[txid])) {
589
615
  validTxids[tx.txid] = true
@@ -596,8 +622,8 @@ export class Beef {
596
622
  // transactions that don't have proofs and don't chain to proofs
597
623
  const txsNotValid = queue
598
624
 
599
- // New order of txs is sorted, unsortable, txidOnly (no raw transaction)
600
- this.txs = result.concat(queue).concat(txidOnly).concat(txsMissingInputs)
625
+ // New order of txs is unsortable (missing inputs or depends on missing inputs), txidOnly, sorted (so newest sorted is last)
626
+ this.txs = txsMissingInputs.concat(txsNotValid).concat(txidOnly).concat(result)
601
627
 
602
628
  return {
603
629
  missingInputs: Object.keys(missingInputs),
@@ -305,6 +305,24 @@ describe('Beef tests', () => {
305
305
  expect(atomic).toEqual(beef2)
306
306
  }
307
307
  })
308
+ test('9_sortTxs', async () => {
309
+ {
310
+ const beef = new Beef()
311
+ beef.mergeTxidOnly('a')
312
+ const btx = beef.mergeTxidOnly('b')
313
+ btx.inputTxids = ['a']
314
+ beef.sortTxs()
315
+ expect(beef.txs[1].txid).toBe('b')
316
+ }
317
+ {
318
+ const beef = new Beef()
319
+ const atx = beef.mergeTxidOnly('a')
320
+ const btx = beef.mergeTxidOnly('b')
321
+ atx.inputTxids = ['b']
322
+ beef.sortTxs()
323
+ expect(beef.txs[1].txid).toBe('a')
324
+ }
325
+ })
308
326
  })
309
327
 
310
328
  const txs: string[] = [