@arkade-os/sdk 0.4.41 → 0.4.43

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 (69) hide show
  1. package/dist/adapters/expo.cjs +4 -4
  2. package/dist/adapters/expo.d.cts +2 -2
  3. package/dist/adapters/expo.d.ts +2 -2
  4. package/dist/adapters/expo.js +2 -2
  5. package/dist/adapters/indexedDB.cjs +3 -3
  6. package/dist/adapters/indexedDB.js +2 -2
  7. package/dist/{ark-CSqMJczW.d.cts → ark-I8jfe5VE.d.cts} +157 -2
  8. package/dist/{ark-CSqMJczW.d.ts → ark-I8jfe5VE.d.ts} +157 -2
  9. package/dist/{asyncStorageTaskQueue-SZPrhw23.d.ts → asyncStorageTaskQueue-CjoL-35w.d.ts} +1 -1
  10. package/dist/{asyncStorageTaskQueue-79XdGsdW.d.cts → asyncStorageTaskQueue-D1FfHfgK.d.cts} +1 -1
  11. package/dist/{chunk-ALISWEQ5.cjs → chunk-3AQNLQPM.cjs} +39 -4
  12. package/dist/chunk-3AQNLQPM.cjs.map +1 -0
  13. package/dist/{chunk-5XAYUPK4.js → chunk-763XHDAJ.js} +39 -5
  14. package/dist/chunk-763XHDAJ.js.map +1 -0
  15. package/dist/{chunk-D5MQYYYF.js → chunk-B4R36PRN.js} +1106 -357
  16. package/dist/chunk-B4R36PRN.js.map +1 -0
  17. package/dist/{chunk-KNASU3QI.cjs → chunk-JIWCE2IK.cjs} +7 -7
  18. package/dist/{chunk-KNASU3QI.cjs.map → chunk-JIWCE2IK.cjs.map} +1 -1
  19. package/dist/{chunk-WBF3FNS3.cjs → chunk-PLJZRIFZ.cjs} +11 -11
  20. package/dist/{chunk-WBF3FNS3.cjs.map → chunk-PLJZRIFZ.cjs.map} +1 -1
  21. package/dist/{chunk-3MPXMLP4.cjs → chunk-QGNTDGMN.cjs} +1190 -430
  22. package/dist/chunk-QGNTDGMN.cjs.map +1 -0
  23. package/dist/{chunk-MYI333YH.js → chunk-US274KZO.js} +3 -3
  24. package/dist/{chunk-MYI333YH.js.map → chunk-US274KZO.js.map} +1 -1
  25. package/dist/{chunk-DDG4RE5F.js → chunk-VKKLOT2E.js} +3 -3
  26. package/dist/{chunk-DDG4RE5F.js.map → chunk-VKKLOT2E.js.map} +1 -1
  27. package/dist/contracts/handlers/index.d.cts +3 -3
  28. package/dist/contracts/handlers/index.d.ts +3 -3
  29. package/dist/{delegate-Bqwe-kbj.d.cts → delegate-BRqbh2fW.d.cts} +1 -1
  30. package/dist/{delegate-BTn9BK8E.d.ts → delegate-CgjVjYOP.d.ts} +1 -1
  31. package/dist/{index-aYiHTMkX.d.cts → index-DHuXlDEg.d.cts} +2 -2
  32. package/dist/{index-kITujXlS.d.ts → index-DvWNB_rl.d.ts} +2 -2
  33. package/dist/index.cjs +150 -118
  34. package/dist/index.d.cts +113 -9
  35. package/dist/index.d.ts +113 -9
  36. package/dist/index.js +2 -2
  37. package/dist/repositories/realm/index.cjs +268 -12
  38. package/dist/repositories/realm/index.cjs.map +1 -1
  39. package/dist/repositories/realm/index.d.cts +101 -3
  40. package/dist/repositories/realm/index.d.ts +101 -3
  41. package/dist/repositories/realm/index.js +254 -4
  42. package/dist/repositories/realm/index.js.map +1 -1
  43. package/dist/repositories/sqlite/index.cjs +366 -40
  44. package/dist/repositories/sqlite/index.cjs.map +1 -1
  45. package/dist/repositories/sqlite/index.d.cts +52 -6
  46. package/dist/repositories/sqlite/index.d.ts +52 -6
  47. package/dist/repositories/sqlite/index.js +353 -32
  48. package/dist/repositories/sqlite/index.js.map +1 -1
  49. package/dist/{taskRunner-DRHElPMG.d.cts → taskRunner-BSnslNmq.d.cts} +1 -1
  50. package/dist/{taskRunner-BuBAZQwB.d.ts → taskRunner-Djlhfgi1.d.ts} +1 -1
  51. package/dist/wallet/expo/background.cjs +12 -12
  52. package/dist/wallet/expo/background.d.cts +3 -3
  53. package/dist/wallet/expo/background.d.ts +3 -3
  54. package/dist/wallet/expo/background.js +4 -4
  55. package/dist/wallet/expo/index.cjs +12 -12
  56. package/dist/wallet/expo/index.d.cts +4 -4
  57. package/dist/wallet/expo/index.d.ts +4 -4
  58. package/dist/wallet/expo/index.js +3 -3
  59. package/dist/{wallet-CTEQpKG9.d.ts → wallet-BsqRU8KM.d.cts} +29 -5
  60. package/dist/{wallet-BZgRfnAE.d.cts → wallet-D9z7wssl.d.ts} +29 -5
  61. package/dist/worker/expo/index.cjs +7 -7
  62. package/dist/worker/expo/index.d.cts +4 -4
  63. package/dist/worker/expo/index.d.ts +4 -4
  64. package/dist/worker/expo/index.js +3 -3
  65. package/package.json +2 -2
  66. package/dist/chunk-3MPXMLP4.cjs.map +0 -1
  67. package/dist/chunk-5XAYUPK4.js.map +0 -1
  68. package/dist/chunk-ALISWEQ5.cjs.map +0 -1
  69. package/dist/chunk-D5MQYYYF.js.map +0 -1
@@ -1,11 +1,36 @@
1
1
  'use strict';
2
2
 
3
- var chunk3MPXMLP4_cjs = require('../../chunk-3MPXMLP4.cjs');
4
- require('../../chunk-ALISWEQ5.cjs');
3
+ var chunkQGNTDGMN_cjs = require('../../chunk-QGNTDGMN.cjs');
4
+ require('../../chunk-3AQNLQPM.cjs');
5
5
  require('../../chunk-32YKLQPS.cjs');
6
6
  require('../../chunk-HXDZ2QTW.cjs');
7
7
  require('../../chunk-5BLDMQED.cjs');
8
8
 
9
+ // src/repositories/sqlite/transaction.ts
10
+ var writeChains = /* @__PURE__ */ new WeakMap();
11
+ function runInTransaction(db, fn) {
12
+ const prev = writeChains.get(db) ?? Promise.resolve();
13
+ const run = prev.then(async () => {
14
+ await db.run("BEGIN IMMEDIATE");
15
+ try {
16
+ await fn();
17
+ await db.run("COMMIT");
18
+ } catch (e) {
19
+ try {
20
+ await db.run("ROLLBACK");
21
+ } catch {
22
+ }
23
+ throw e;
24
+ }
25
+ });
26
+ writeChains.set(
27
+ db,
28
+ run.catch(() => {
29
+ })
30
+ );
31
+ return run;
32
+ }
33
+
9
34
  // src/repositories/sqlite/walletRepository.ts
10
35
  var SQLiteWalletRepository = class {
11
36
  constructor(db, options) {
@@ -96,29 +121,31 @@ var SQLiteWalletRepository = class {
96
121
  * indexer would have returned — new rows from the indexer always carry a
97
122
  * populated `script`, so the migration is idempotent.
98
123
  *
99
- * The rebuild path is wrapped in a transaction: without it, a crash
100
- * between the `DROP TABLE vtxos` and the `RENAME tmp vtxos` commits
101
- * would leave the next startup seeing no `vtxos` table and create a
102
- * fresh empty one, silently orphaning every row in the temp table.
124
+ * The whole check-and-migrate runs inside one transaction, not just the
125
+ * rebuild: the schema inspection is the decision point, so a second
126
+ * concurrent init on the same connection must not read a pre-migration
127
+ * schema and then re-run `ADD COLUMN` (duplicate column) after the first
128
+ * migration already committed. It also guards the crash window between the
129
+ * `DROP TABLE vtxos` and the `RENAME tmp → vtxos`, which would otherwise
130
+ * leave the next startup seeing no `vtxos` table.
103
131
  */
104
132
  async migrateVtxosTable() {
105
- const tableExists = await this.db.get(
106
- `SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
107
- [this.tables.vtxos]
108
- );
109
- if (!tableExists) {
110
- await this.db.run(this.vtxosCreateSql(this.tables.vtxos));
111
- return;
112
- }
113
- const cols = await this.db.all(
114
- `PRAGMA table_info(${this.tables.vtxos})`
115
- );
116
- const scriptCol = cols.find((c) => c.name === "script");
117
- if (scriptCol && scriptCol.notnull === 1) {
118
- return;
119
- }
120
- await this.db.run("BEGIN IMMEDIATE");
121
- try {
133
+ await runInTransaction(this.db, async () => {
134
+ const tableExists = await this.db.get(
135
+ `SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
136
+ [this.tables.vtxos]
137
+ );
138
+ if (!tableExists) {
139
+ await this.db.run(this.vtxosCreateSql(this.tables.vtxos));
140
+ return;
141
+ }
142
+ const cols = await this.db.all(
143
+ `PRAGMA table_info(${this.tables.vtxos})`
144
+ );
145
+ const scriptCol = cols.find((c) => c.name === "script");
146
+ if (scriptCol && scriptCol.notnull === 1) {
147
+ return;
148
+ }
122
149
  if (!scriptCol) {
123
150
  await this.db.run(`ALTER TABLE ${this.tables.vtxos} ADD COLUMN script TEXT`);
124
151
  }
@@ -126,7 +153,7 @@ var SQLiteWalletRepository = class {
126
153
  for (const row of nullRows) {
127
154
  await this.db.run(
128
155
  `UPDATE ${this.tables.vtxos} SET script = ? WHERE txid = ? AND vout = ?`,
129
- [chunk3MPXMLP4_cjs.scriptFromArkAddress(row.address), row.txid, row.vout]
156
+ [chunkQGNTDGMN_cjs.scriptFromArkAddress(row.address), row.txid, row.vout]
130
157
  );
131
158
  }
132
159
  const tempName = `${this.tables.vtxos}__migrate_tmp`;
@@ -148,14 +175,7 @@ var SQLiteWalletRepository = class {
148
175
  `);
149
176
  await this.db.run(`DROP TABLE ${this.tables.vtxos}`);
150
177
  await this.db.run(`ALTER TABLE ${tempName} RENAME TO ${this.tables.vtxos}`);
151
- await this.db.run("COMMIT");
152
- } catch (e) {
153
- try {
154
- await this.db.run("ROLLBACK");
155
- } catch {
156
- }
157
- throw e;
158
- }
178
+ });
159
179
  }
160
180
  vtxosCreateSql(tableName) {
161
181
  return `CREATE TABLE ${tableName} (
@@ -204,7 +224,7 @@ var SQLiteWalletRepository = class {
204
224
  async saveVtxos(address, vtxos) {
205
225
  await this.ensureInit();
206
226
  for (const vtxo of vtxos) {
207
- const s = chunk3MPXMLP4_cjs.serializeVtxo(vtxo);
227
+ const s = chunkQGNTDGMN_cjs.serializeVtxo(vtxo);
208
228
  await this.db.run(
209
229
  `INSERT OR REPLACE INTO ${this.tables.vtxos}
210
230
  (txid, vout, value, address,
@@ -259,7 +279,7 @@ var SQLiteWalletRepository = class {
259
279
  throw new Error("SQLiteWalletRepository requires an address");
260
280
  }
261
281
  for (const vtxo of vtxos) {
262
- if (!chunk3MPXMLP4_cjs.isVtxoForScript(vtxo, key.script)) {
282
+ if (!chunkQGNTDGMN_cjs.isVtxoForScript(vtxo, key.script)) {
263
283
  throw new Error(
264
284
  `VTXO ${vtxo.txid}:${vtxo.vout} script mismatch: expected ${key.script}, got ${vtxo.script}`
265
285
  );
@@ -283,7 +303,7 @@ var SQLiteWalletRepository = class {
283
303
  async saveUtxos(address, utxos) {
284
304
  await this.ensureInit();
285
305
  for (const utxo of utxos) {
286
- const s = chunk3MPXMLP4_cjs.serializeUtxo(utxo);
306
+ const s = chunkQGNTDGMN_cjs.serializeUtxo(utxo);
287
307
  await this.db.run(
288
308
  `INSERT OR REPLACE INTO ${this.tables.utxos}
289
309
  (txid, vout, value, address,
@@ -339,7 +359,7 @@ var SQLiteWalletRepository = class {
339
359
  tx.amount,
340
360
  tx.settled ? 1 : 0,
341
361
  tx.createdAt,
342
- tx.assets ? JSON.stringify(chunk3MPXMLP4_cjs.serializeAssets(tx.assets)) : null
362
+ tx.assets ? JSON.stringify(chunkQGNTDGMN_cjs.serializeAssets(tx.assets)) : null
343
363
  ]
344
364
  );
345
365
  }
@@ -413,9 +433,9 @@ function vtxoRowToDomain(row) {
413
433
  // Post-migration every row has `script`, but the backfill is
414
434
  // idempotent: derive from `address` if the legacy column is still
415
435
  // null (e.g. the migration hasn't run yet on this handle).
416
- script: row.script ?? chunk3MPXMLP4_cjs.scriptFromArkAddress(row.address)
436
+ script: row.script ?? chunkQGNTDGMN_cjs.scriptFromArkAddress(row.address)
417
437
  };
418
- return chunk3MPXMLP4_cjs.deserializeVtxo(serialized);
438
+ return chunkQGNTDGMN_cjs.deserializeVtxo(serialized);
419
439
  }
420
440
  function utxoRowToDomain(row) {
421
441
  const serialized = {
@@ -434,7 +454,7 @@ function utxoRowToDomain(row) {
434
454
  status: JSON.parse(row.status_json),
435
455
  extraWitness: row.extra_witness_json ? JSON.parse(row.extra_witness_json) : void 0
436
456
  };
437
- return chunk3MPXMLP4_cjs.deserializeUtxo(serialized);
457
+ return chunkQGNTDGMN_cjs.deserializeUtxo(serialized);
438
458
  }
439
459
  function txRowToDomain(row) {
440
460
  const tx = {
@@ -449,7 +469,7 @@ function txRowToDomain(row) {
449
469
  createdAt: row.created_at
450
470
  };
451
471
  if (row.assets_json) {
452
- tx.assets = chunk3MPXMLP4_cjs.deserializeAssets(JSON.parse(row.assets_json));
472
+ tx.assets = chunkQGNTDGMN_cjs.deserializeAssets(JSON.parse(row.assets_json));
453
473
  }
454
474
  return tx;
455
475
  }
@@ -582,7 +602,313 @@ function contractRowToDomain(row) {
582
602
  return contract;
583
603
  }
584
604
 
605
+ // src/repositories/sqlite/intentRepository.ts
606
+ var SAFE_PREFIX3 = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
607
+ function sanitizePrefix3(p) {
608
+ if (!SAFE_PREFIX3.test(p)) throw new Error(`Invalid table prefix "${p}"`);
609
+ return p;
610
+ }
611
+ var SQLiteIntentRepository = class {
612
+ constructor(db, options) {
613
+ this.db = db;
614
+ this.prefix = sanitizePrefix3(options?.prefix ?? "ark_");
615
+ this.t = `${this.prefix}intents`;
616
+ }
617
+ version = 1;
618
+ initPromise = null;
619
+ prefix;
620
+ t;
621
+ ensureInit() {
622
+ if (!this.initPromise) this.initPromise = this.init();
623
+ return this.initPromise;
624
+ }
625
+ async init() {
626
+ await this.db.run(`CREATE TABLE IF NOT EXISTS ${this.t} (
627
+ intent_tx_id TEXT PRIMARY KEY,
628
+ intent_id TEXT,
629
+ state TEXT NOT NULL,
630
+ valid_from INTEGER,
631
+ valid_until INTEGER,
632
+ created_at INTEGER NOT NULL,
633
+ updated_at INTEGER NOT NULL,
634
+ register_proof TEXT NOT NULL,
635
+ register_proof_message TEXT NOT NULL,
636
+ delete_proof TEXT NOT NULL,
637
+ delete_proof_message TEXT NOT NULL,
638
+ batch_id TEXT,
639
+ commitment_txid TEXT,
640
+ cancellation_reason TEXT,
641
+ partial_forfeits_json TEXT NOT NULL,
642
+ signer_descriptor TEXT,
643
+ intent_vtxos_json TEXT NOT NULL
644
+ )`);
645
+ await this.db.run(
646
+ `CREATE UNIQUE INDEX IF NOT EXISTS idx_${this.prefix}intent_id ON ${this.t} (intent_id) WHERE intent_id IS NOT NULL`
647
+ );
648
+ await this.db.run(
649
+ `CREATE INDEX IF NOT EXISTS idx_${this.prefix}intent_state ON ${this.t} (state)`
650
+ );
651
+ }
652
+ withTx(fn) {
653
+ return runInTransaction(this.db, fn);
654
+ }
655
+ async clear() {
656
+ await this.ensureInit();
657
+ await this.withTx(async () => {
658
+ await this.db.run(`DELETE FROM ${this.t}`);
659
+ });
660
+ }
661
+ async saveIntent(intent) {
662
+ await this.ensureInit();
663
+ const now = Date.now();
664
+ await this.withTx(async () => {
665
+ await this.db.run(
666
+ `INSERT INTO ${this.t}
667
+ (intent_tx_id, intent_id, state, valid_from, valid_until,
668
+ created_at, updated_at, register_proof, register_proof_message,
669
+ delete_proof, delete_proof_message, batch_id, commitment_txid,
670
+ cancellation_reason, partial_forfeits_json, signer_descriptor,
671
+ intent_vtxos_json)
672
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
673
+ ON CONFLICT(intent_tx_id) DO UPDATE SET
674
+ intent_id = excluded.intent_id,
675
+ state = excluded.state,
676
+ valid_from = excluded.valid_from,
677
+ valid_until = excluded.valid_until,
678
+ created_at = excluded.created_at,
679
+ updated_at = excluded.updated_at,
680
+ register_proof = excluded.register_proof,
681
+ register_proof_message = excluded.register_proof_message,
682
+ delete_proof = excluded.delete_proof,
683
+ delete_proof_message = excluded.delete_proof_message,
684
+ batch_id = excluded.batch_id,
685
+ commitment_txid = excluded.commitment_txid,
686
+ cancellation_reason = excluded.cancellation_reason,
687
+ partial_forfeits_json = excluded.partial_forfeits_json,
688
+ signer_descriptor = excluded.signer_descriptor,
689
+ intent_vtxos_json = excluded.intent_vtxos_json`,
690
+ [
691
+ intent.intentTxId,
692
+ intent.intentId ?? null,
693
+ intent.state,
694
+ intent.validFrom ?? null,
695
+ intent.validUntil ?? null,
696
+ intent.createdAt,
697
+ now,
698
+ intent.registerProof,
699
+ intent.registerProofMessage,
700
+ intent.deleteProof,
701
+ intent.deleteProofMessage,
702
+ intent.batchId ?? null,
703
+ intent.commitmentTransactionId ?? null,
704
+ intent.cancellationReason ?? null,
705
+ JSON.stringify(intent.partialForfeits),
706
+ intent.signerDescriptor ?? null,
707
+ JSON.stringify(intent.intentVtxos)
708
+ ]
709
+ );
710
+ });
711
+ }
712
+ async getIntents(filter) {
713
+ await this.ensureInit();
714
+ const rows = await this.db.all(
715
+ `SELECT * FROM ${this.t} ORDER BY created_at ASC, intent_tx_id ASC`
716
+ );
717
+ let out = rows.map(rowToIntent);
718
+ if (filter) out = out.filter((i) => chunkQGNTDGMN_cjs.intentMatchesFilter(i, filter));
719
+ const { skip, end } = chunkQGNTDGMN_cjs.intentPageBounds(filter, out.length);
720
+ return out.slice(skip, end);
721
+ }
722
+ async getLockedVtxoOutpoints() {
723
+ await this.ensureInit();
724
+ const rows = await this.db.all(`SELECT * FROM ${this.t}`);
725
+ const out = [];
726
+ for (const r of rows) {
727
+ if (chunkQGNTDGMN_cjs.isTerminalIntentState(r.state)) continue;
728
+ for (const o of JSON.parse(r.intent_vtxos_json)) out.push(o);
729
+ }
730
+ return out;
731
+ }
732
+ async [Symbol.asyncDispose]() {
733
+ }
734
+ };
735
+ function rowToIntent(r) {
736
+ return {
737
+ intentTxId: r.intent_tx_id,
738
+ intentId: r.intent_id ?? void 0,
739
+ state: r.state,
740
+ validFrom: r.valid_from ?? void 0,
741
+ validUntil: r.valid_until ?? void 0,
742
+ createdAt: r.created_at,
743
+ updatedAt: r.updated_at,
744
+ registerProof: r.register_proof,
745
+ registerProofMessage: r.register_proof_message,
746
+ deleteProof: r.delete_proof,
747
+ deleteProofMessage: r.delete_proof_message,
748
+ batchId: r.batch_id ?? void 0,
749
+ commitmentTransactionId: r.commitment_txid ?? void 0,
750
+ cancellationReason: r.cancellation_reason ?? void 0,
751
+ partialForfeits: JSON.parse(r.partial_forfeits_json),
752
+ signerDescriptor: r.signer_descriptor ?? void 0,
753
+ intentVtxos: JSON.parse(r.intent_vtxos_json)
754
+ };
755
+ }
756
+
757
+ // src/repositories/sqlite/virtualTxRepository.ts
758
+ var SAFE_PREFIX4 = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
759
+ function sanitizePrefix4(p) {
760
+ if (!SAFE_PREFIX4.test(p)) throw new Error(`Invalid table prefix "${p}"`);
761
+ return p;
762
+ }
763
+ var SQLiteVirtualTxRepository = class {
764
+ constructor(db, options) {
765
+ this.db = db;
766
+ this.prefix = sanitizePrefix4(options?.prefix ?? "ark_");
767
+ this.tTx = `${this.prefix}virtual_txs`;
768
+ this.tBranch = `${this.prefix}vtxo_branches`;
769
+ }
770
+ version = 1;
771
+ initPromise = null;
772
+ prefix;
773
+ tTx;
774
+ tBranch;
775
+ ensureInit() {
776
+ if (!this.initPromise) this.initPromise = this.init();
777
+ return this.initPromise;
778
+ }
779
+ async init() {
780
+ await this.db.run(`CREATE TABLE IF NOT EXISTS ${this.tTx} (
781
+ txid TEXT PRIMARY KEY,
782
+ psbt TEXT,
783
+ expires_at INTEGER,
784
+ type INTEGER NOT NULL DEFAULT 0
785
+ )`);
786
+ await this.db.run(`CREATE TABLE IF NOT EXISTS ${this.tBranch} (
787
+ vtxo_txid TEXT NOT NULL,
788
+ vtxo_vout INTEGER NOT NULL,
789
+ virtual_txid TEXT NOT NULL,
790
+ position INTEGER NOT NULL,
791
+ PRIMARY KEY (vtxo_txid, vtxo_vout, position)
792
+ )`);
793
+ await this.db.run(
794
+ `CREATE INDEX IF NOT EXISTS idx_${this.prefix}branch_vtxid ON ${this.tBranch} (virtual_txid)`
795
+ );
796
+ }
797
+ tx(fn) {
798
+ return runInTransaction(this.db, fn);
799
+ }
800
+ async clear() {
801
+ await this.ensureInit();
802
+ await this.tx(async () => {
803
+ await this.db.run(`DELETE FROM ${this.tTx}`);
804
+ await this.db.run(`DELETE FROM ${this.tBranch}`);
805
+ });
806
+ }
807
+ async upsertVirtualTxs(txs) {
808
+ if (txs.length === 0) return;
809
+ await this.ensureInit();
810
+ await this.tx(async () => {
811
+ const placeholders = txs.map(() => "?").join(", ");
812
+ const existing = await this.db.all(
813
+ `SELECT * FROM ${this.tTx} WHERE txid IN (${placeholders})`,
814
+ txs.map((t) => t.txid)
815
+ );
816
+ const prevByTxid = new Map(existing.map((r) => [r.txid, r]));
817
+ for (const t of txs) {
818
+ const prev = prevByTxid.get(t.txid);
819
+ const psbt = t.psbt ?? prev?.psbt ?? null;
820
+ const expires = t.expiresAt ?? prev?.expires_at ?? null;
821
+ const type = chunkQGNTDGMN_cjs.mergeChainedTxType(t.type, prev?.type);
822
+ await this.db.run(
823
+ `INSERT OR REPLACE INTO ${this.tTx} (txid, psbt, expires_at, type) VALUES (?, ?, ?, ?)`,
824
+ [t.txid, psbt, expires, type]
825
+ );
826
+ prevByTxid.set(t.txid, { txid: t.txid, psbt, expires_at: expires, type });
827
+ }
828
+ });
829
+ }
830
+ async getVirtualTx(txid) {
831
+ await this.ensureInit();
832
+ const row = await this.db.get(`SELECT * FROM ${this.tTx} WHERE txid = ?`, [txid]);
833
+ return row ? rowToTx(row) : null;
834
+ }
835
+ async setBranch(vtxo, branch) {
836
+ await this.ensureInit();
837
+ await this.tx(async () => {
838
+ await this.db.run(`DELETE FROM ${this.tBranch} WHERE vtxo_txid = ? AND vtxo_vout = ?`, [
839
+ vtxo.txid,
840
+ vtxo.vout
841
+ ]);
842
+ for (const b of branch) {
843
+ await this.db.run(
844
+ `INSERT OR REPLACE INTO ${this.tBranch} (vtxo_txid, vtxo_vout, virtual_txid, position) VALUES (?, ?, ?, ?)`,
845
+ [b.vtxoTxid, b.vtxoVout, b.virtualTxid, b.position]
846
+ );
847
+ }
848
+ });
849
+ }
850
+ async getBranch(vtxo) {
851
+ await this.ensureInit();
852
+ const rows = await this.db.all(
853
+ `SELECT t.txid, t.psbt, t.expires_at, t.type
854
+ FROM ${this.tBranch} b
855
+ JOIN ${this.tTx} t ON t.txid = b.virtual_txid
856
+ WHERE b.vtxo_txid = ? AND b.vtxo_vout = ?
857
+ ORDER BY b.position ASC`,
858
+ [vtxo.txid, vtxo.vout]
859
+ );
860
+ return rows.map(rowToTx);
861
+ }
862
+ async hasBranch(vtxo) {
863
+ await this.ensureInit();
864
+ const r = await this.db.get(
865
+ `SELECT COUNT(*) AS c FROM ${this.tBranch} WHERE vtxo_txid = ? AND vtxo_vout = ?`,
866
+ [vtxo.txid, vtxo.vout]
867
+ );
868
+ return (r?.c ?? 0) > 0;
869
+ }
870
+ async pruneForSpentVtxo(vtxo) {
871
+ await this.ensureInit();
872
+ await this.tx(async () => {
873
+ const removed = await this.db.all(
874
+ `SELECT DISTINCT virtual_txid FROM ${this.tBranch} WHERE vtxo_txid = ? AND vtxo_vout = ?`,
875
+ [vtxo.txid, vtxo.vout]
876
+ );
877
+ await this.db.run(`DELETE FROM ${this.tBranch} WHERE vtxo_txid = ? AND vtxo_vout = ?`, [
878
+ vtxo.txid,
879
+ vtxo.vout
880
+ ]);
881
+ if (removed.length === 0) return;
882
+ const placeholders = removed.map(() => "?").join(", ");
883
+ await this.db.run(
884
+ `DELETE FROM ${this.tTx}
885
+ WHERE txid IN (${placeholders})
886
+ AND NOT EXISTS (
887
+ SELECT 1 FROM ${this.tBranch} b WHERE b.virtual_txid = ${this.tTx}.txid
888
+ )`,
889
+ removed.map((r) => r.virtual_txid)
890
+ );
891
+ });
892
+ }
893
+ async [Symbol.asyncDispose]() {
894
+ }
895
+ };
896
+ function rowToTx(r) {
897
+ return {
898
+ txid: r.txid,
899
+ psbt: r.psbt ?? null,
900
+ expiresAt: r.expires_at ?? null,
901
+ type: r.type
902
+ };
903
+ }
904
+
905
+ Object.defineProperty(exports, "ChainedTxType", {
906
+ enumerable: true,
907
+ get: function () { return chunkQGNTDGMN_cjs.ChainedTxType; }
908
+ });
585
909
  exports.SQLiteContractRepository = SQLiteContractRepository;
910
+ exports.SQLiteIntentRepository = SQLiteIntentRepository;
911
+ exports.SQLiteVirtualTxRepository = SQLiteVirtualTxRepository;
586
912
  exports.SQLiteWalletRepository = SQLiteWalletRepository;
587
913
  //# sourceMappingURL=index.cjs.map
588
914
  //# sourceMappingURL=index.cjs.map