@arkade-os/swap 0.1.0-rc.9 → 0.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 (57) hide show
  1. package/README.md +261 -252
  2. package/dist/advanced.cjs +6613 -3224
  3. package/dist/advanced.d.cts +328 -283
  4. package/dist/advanced.d.ts +328 -283
  5. package/dist/advanced.js +347 -34
  6. package/dist/chunk-A3SHHRX4.js +104 -0
  7. package/dist/chunk-FMRAV77W.js +3065 -0
  8. package/dist/chunk-T4FHBWZI.js +3459 -0
  9. package/dist/{chunk-NS3K6BZG.js → chunk-YHESEL4I.js} +1792 -636
  10. package/dist/index.cjs +6901 -3901
  11. package/dist/index.d.cts +71 -243
  12. package/dist/index.d.ts +71 -243
  13. package/dist/index.js +131 -167
  14. package/dist/lockupContract-CIXkJPxV.d.ts +148 -0
  15. package/dist/lockupContract-CkhWMKCo.d.cts +148 -0
  16. package/dist/node/index.cjs +61 -10
  17. package/dist/node/index.d.cts +307 -635
  18. package/dist/node/index.d.ts +307 -635
  19. package/dist/node/index.js +65 -10
  20. package/dist/nostr.cjs +427 -49
  21. package/dist/nostr.d.cts +59 -70
  22. package/dist/nostr.d.ts +59 -70
  23. package/dist/nostr.js +268 -36
  24. package/dist/offer-BPD2ortO.d.ts +269 -0
  25. package/dist/offer-Diff5Flc.d.cts +269 -0
  26. package/dist/offerFillPlan-B-5ae_gQ.d.cts +1671 -0
  27. package/dist/offerFillPlan-CJzneX6F.d.ts +1671 -0
  28. package/dist/protocol.cjs +5717 -4275
  29. package/dist/protocol.d.cts +258 -328
  30. package/dist/protocol.d.ts +258 -328
  31. package/dist/protocol.js +200 -224
  32. package/dist/repositories/realm/index.cjs +55 -11
  33. package/dist/repositories/realm/index.d.cts +18 -30
  34. package/dist/repositories/realm/index.d.ts +18 -30
  35. package/dist/repositories/realm/index.js +49 -12
  36. package/dist/repositories/sqlite/index.cjs +61 -10
  37. package/dist/repositories/sqlite/index.d.cts +7 -6
  38. package/dist/repositories/sqlite/index.d.ts +7 -6
  39. package/dist/repositories/sqlite/index.js +57 -11
  40. package/dist/repository-BBVRh9ez.d.ts +2357 -0
  41. package/dist/repository-C4pvymhs.d.cts +2357 -0
  42. package/dist/{rfq-BO6o7NwS.d.ts → rfq-C0tHOvn6.d.cts} +323 -444
  43. package/dist/{rfq-BO6o7NwS.d.cts → rfq-C0tHOvn6.d.ts} +323 -444
  44. package/dist/watch-7BiRa4oB.d.ts +74 -0
  45. package/dist/watch-Bxq-lx_6.d.cts +74 -0
  46. package/package.json +10 -10
  47. package/dist/chunk-JFKGMPMH.js +0 -3456
  48. package/dist/chunk-LLG7RD2T.js +0 -1499
  49. package/dist/chunk-U7VWWTL6.js +0 -66
  50. package/dist/errors-CNISbN0M.d.ts +0 -2218
  51. package/dist/errors-DwNhIP0N.d.cts +0 -2218
  52. package/dist/lockupContract-C8-Mcg69.d.ts +0 -664
  53. package/dist/lockupContract-mIt4syse.d.cts +0 -664
  54. package/dist/repository-CGPLbjBe.d.ts +0 -3489
  55. package/dist/repository-DSGGq0ct.d.cts +0 -3489
  56. package/dist/watch-DWyVqyEN.d.cts +0 -96
  57. package/dist/watch-DmvHNX6T.d.ts +0 -96
@@ -0,0 +1,148 @@
1
+ import { C as CoverageRegistrationOptions } from './offer-Diff5Flc.cjs';
2
+ import { VHTLC, IContractManager } from '@arkade-os/sdk';
3
+
4
+ interface SealedClaimPacket {
5
+ /** `ephPub(33) ‖ nonce(12) ‖ ciphertext`, base64 (93 bytes decoded). The legacy
6
+ * `claim_packet` shape: usable only via covclaimd's Reveal API, which silently
7
+ * needs both sides on the SAME covclaimd, and opens only beside the same `min_amount`
8
+ * when sealed with one. Prefer {@link SealedClaimPacket.packet}. */
9
+ ciphertext: string;
10
+ /**
11
+ * covclaimd's serialised `ClaimPacket` body, base64: TLV `0x01` ciphertext and `0x03`
12
+ * covclaimd_pub_key. **This is what `claim_packet` should carry** (§ 7.1.2). The
13
+ * solver stamps it as Arkade extension packet `0x04`; covclaimd filters on `0x03`, so
14
+ * the client alone chooses which covclaimd.
15
+ *
16
+ * `0x02` arkade_script is deliberately absent: the covenant commits to
17
+ * `taggedHash("ArkScriptHash", script)`, so only the funder's copy is guaranteed to
18
+ * match (see {@link appendArkadeScript}); a client-derived one could strand the claim.
19
+ */
20
+ packet: string;
21
+ }
22
+ interface ClaimPacketInput {
23
+ preimage: Uint8Array;
24
+ /** covclaimd's public key, 33-byte compressed (from its /v1 info). */
25
+ covclaimdPubkey: Uint8Array;
26
+ /** Sats the funded lockup must carry for covclaimd to claim it, as TLV `0x04` and in the
27
+ * AEAD's additional data so a courier cannot strip it. Unset: the unbound legacy bytes. */
28
+ minAmount?: bigint;
29
+ }
30
+ /**
31
+ * Seal a preimage to covclaimd.
32
+ *
33
+ * The ephemeral key and nonce CANNOT be caller-supplied: AES-GCM under a repeated
34
+ * (key, nonce) pair is a total break (forgery and plaintext recovery).
35
+ */
36
+ declare function sealClaimPacket(input: ClaimPacketInput): Promise<SealedClaimPacket>;
37
+ /** `ephPub(33) ‖ nonce(12) ‖ preimage(32) ‖ GCM tag(16)`. Distinguishes the two
38
+ * `claim_packet` shapes: a TLV body carrying it is ≥96 bytes, so lengths cannot collide. */
39
+ declare const SEALED_CIPHERTEXT_LENGTH = 93;
40
+ /** The Arkade extension packet type covclaimd scans the arkd tx stream for. */
41
+ declare const CLAIM_PACKET_TYPE = 4;
42
+ /**
43
+ * Stamp the covenant script into a client's packet — the solver's half.
44
+ *
45
+ * Appending rather than rebuilding is safe: covclaimd's `DeserializeClaim` is
46
+ * order-agnostic, so `0x01, 0x03, 0x02` reads back like its own `0x01, 0x02, 0x03`.
47
+ */
48
+ declare const appendArkadeScript: (body: Uint8Array, arkadeScript: Uint8Array) => Uint8Array;
49
+ type ClaimPacketShape = {
50
+ kind: "ciphertext";
51
+ } | {
52
+ kind: "packet";
53
+ body: Uint8Array;
54
+ covclaimdPubkey?: Uint8Array;
55
+ needsArkadeScript: boolean;
56
+ };
57
+ /**
58
+ * Which of the two `claim_packet` shapes a base64 field carries.
59
+ *
60
+ * Never throws: anything not unambiguously a packet reads as the legacy ciphertext,
61
+ * keeping the Reveal-API path rather than failing a swap over an unfamiliar field.
62
+ */
63
+ declare const claimPacketShape: (b64: string) => ClaimPacketShape;
64
+
65
+ /**
66
+ * The contract row an RFQ lockup registers as — one definition, two writers (`request*Send` and
67
+ * `RfqSwapManager.ensureRegistered`). `createContract` is first-writer-wins, so the second write
68
+ * is a no-op only while both write the SAME row, hence this module.
69
+ */
70
+
71
+ /** The contract type a swap lockup registers under: the SDK's handler for `VHTLC.ScriptV2`.
72
+ *
73
+ * @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`.
74
+ */
75
+ declare const SWAP_LOCKUP_CONTRACT_TYPE = "vhtlc-v2";
76
+ /** @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`. */
77
+ declare const SWAP_LOCKUP_CONTRACT_LABEL = "Arkade RFQ swap lockup";
78
+ /** @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`. */
79
+ declare const SWAP_LOCKUP_CONTRACT_KIND = "rfq-swap-lockup";
80
+ /** The write seam registration needs; a real `ContractManager` satisfies it.
81
+ *
82
+ * @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`.
83
+ */
84
+ type LockupContractWriter = Pick<IContractManager, "createContract">;
85
+ /** The read seam {@link lockupContractParams} needs.
86
+ *
87
+ * @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`.
88
+ */
89
+ type LockupContractReader = Pick<IContractManager, "getContracts">;
90
+ /**
91
+ * No row for a lockup a record claims was funded. The record is fine and its money may be at the
92
+ * address; the wallet's copy of the covenant is missing (store cleared, or a different store
93
+ * registered it). The remedy is a store, not a re-quote.
94
+ *
95
+ * @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`.
96
+ */
97
+ declare class LockupContractMissing extends Error {
98
+ /** The lockup whose row is absent. */
99
+ readonly address: string;
100
+ /** Its pkScript hex — the key the row would have been under. */
101
+ readonly script: string;
102
+ constructor(address: string, script: string);
103
+ }
104
+ /**
105
+ * The lockup could not be written locally: "the quote is fine and your own store failed", unlike
106
+ * {@link SwapRefusal} / {@link AddressMismatch} ("never fund it"). Thrown by `client.accept()`.
107
+ *
108
+ * Nothing is funded yet and on receive legs the invoice never left, so retrying the request is
109
+ * the recovery. `script` lets a caller still holding the swap retry `registerLockupContract`
110
+ * alone; it is NOT enough to resume a request that threw here (no invoice or `secrets`).
111
+ */
112
+ declare class LockupRegistrationFailed extends Error {
113
+ /** The lockup address that was never registered — never fund it: nothing
114
+ * is watching it. */
115
+ readonly address: string;
116
+ /** The covenant the row would have been written from, so the write is retryable without a
117
+ * quote. */
118
+ readonly script: InstanceType<typeof VHTLC.ScriptV2>;
119
+ constructor(script: InstanceType<typeof VHTLC.ScriptV2>, address: string, cause: unknown);
120
+ }
121
+ /**
122
+ * Register a lockup covenant so its VTXOs are watched and — via `vhtlc-v2`'s handler, never
123
+ * generically spendable — kept out of ordinary coin selection.
124
+ *
125
+ * The row carries script-level facts only: rows are keyed by script and first-writer-wins, so
126
+ * anything per-swap written here is stale from the second swap onward. Taking the derived script
127
+ * means the row cannot describe a script other than its key.
128
+ *
129
+ * @throws {LockupRegistrationFailed} so a caller can tell local storage trouble from a bad quote.
130
+ *
131
+ * @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`.
132
+ */
133
+ declare function registerLockupContract(contracts: LockupContractWriter, script: InstanceType<typeof VHTLC.ScriptV2>, address: string, options?: CoverageRegistrationOptions): Promise<void>;
134
+ /**
135
+ * The stored covenant parameters of a funded lockup — the other half of `rebuildRfqSwap`. The row
136
+ * is the wallet's own copy of the tree (`createContract` refuses params that do not reproduce the
137
+ * script), which is why an RFQ swap record stores no tree parameters.
138
+ *
139
+ * Looked up by script, decoded from the address here, so a bad address fails as such rather than
140
+ * as a missing row.
141
+ *
142
+ * @throws {LockupContractMissing} when there is no row.
143
+ *
144
+ * @deprecated Lockup registration is internal to `accept()`; no replacement. Moved off the package root to `@arkade-os/swap/protocol`.
145
+ */
146
+ declare function lockupContractParams(contracts: LockupContractReader, lockupAddress: string): Promise<Record<string, string>>;
147
+
148
+ export { CLAIM_PACKET_TYPE as C, LockupRegistrationFailed as L, SEALED_CIPHERTEXT_LENGTH as S, type ClaimPacketShape as a, appendArkadeScript as b, claimPacketShape as c, type ClaimPacketInput as d, LockupContractMissing as e, type LockupContractReader as f, type LockupContractWriter as g, SWAP_LOCKUP_CONTRACT_KIND as h, SWAP_LOCKUP_CONTRACT_LABEL as i, SWAP_LOCKUP_CONTRACT_TYPE as j, type SealedClaimPacket as k, lockupContractParams as l, registerLockupContract as r, sealClaimPacket as s };
@@ -59,9 +59,16 @@ var import_node_path2 = require("node:path");
59
59
 
60
60
  // src/repositories/sqlite/repository.ts
61
61
  var import_sqlite = require("@arkade-os/sdk/repositories/sqlite");
62
+ var import_sdk2 = require("@arkade-os/sdk");
62
63
 
63
64
  // src/repository.ts
65
+ var import_sdk = require("@arkade-os/sdk");
64
66
  var marketsCacheKey = (network, registry) => `arkade-intents-markets-${network}-${registry}`;
67
+ function assertRfqSwapPageFilter(filter) {
68
+ if (filter.since !== void 0 && (!Number.isSafeInteger(filter.since) || filter.since < 0)) {
69
+ throw new RangeError("RFQ history since must be non-negative Unix seconds");
70
+ }
71
+ }
65
72
 
66
73
  // src/repositories/sqlite/repository.ts
67
74
  var DEFAULT_PREFIX = "arkade_";
@@ -120,6 +127,13 @@ var SQLiteAssetSwapRepository = class {
120
127
  updated_at INTEGER NOT NULL,
121
128
  data TEXT NOT NULL
122
129
  )`);
130
+ await this.db.run(
131
+ `CREATE INDEX IF NOT EXISTS idx_${this.prefix}rfq_updated ON ${this.rfqSwaps} (updated_at, rfq_id)`
132
+ );
133
+ await this.db.run(
134
+ `CREATE INDEX IF NOT EXISTS idx_${this.prefix}rfq_swaps_history ON ${this.rfqSwaps} (state, updated_at, rfq_id)`
135
+ );
136
+ await this.db.run(`DROP INDEX IF EXISTS idx_${this.prefix}rfq_swaps_page`);
123
137
  await this.db.run(
124
138
  `CREATE INDEX IF NOT EXISTS idx_${this.prefix}rfq_swaps_state ON ${this.rfqSwaps} (state)`
125
139
  );
@@ -157,10 +171,8 @@ var SQLiteAssetSwapRepository = class {
157
171
  );
158
172
  });
159
173
  }
160
- async getAllSwaps() {
161
- await this.ensureInit();
162
- const rows = await this.db.all(`SELECT data FROM ${this.swaps}`);
163
- return rows.map((r) => JSON.parse(r.data));
174
+ async getAssetSwapsPage(page) {
175
+ return this.pageTable(this.swaps, "id", page);
164
176
  }
165
177
  async saveRfqSwap(record) {
166
178
  await this.ensureInit();
@@ -180,10 +192,38 @@ var SQLiteAssetSwapRepository = class {
180
192
  );
181
193
  return row ? JSON.parse(row.data) : void 0;
182
194
  }
183
- async getAllRfqSwaps() {
195
+ async getRfqSwapsPage(filter, page) {
196
+ (0, import_sdk2.assertPageRequest)(page);
197
+ assertRfqSwapPageFilter(filter);
184
198
  await this.ensureInit();
185
- const rows = await this.db.all(`SELECT data FROM ${this.rfqSwaps}`);
186
- return rows.map((r) => JSON.parse(r.data));
199
+ const conditions = [];
200
+ const params = [];
201
+ if (filter.state !== void 0) {
202
+ conditions.push("state = ?");
203
+ params.push(filter.state);
204
+ }
205
+ if (filter.since !== void 0) {
206
+ conditions.push("updated_at >= ?");
207
+ params.push(filter.since);
208
+ }
209
+ if (page.after !== void 0) {
210
+ conditions.push("(updated_at, rfq_id) > (?, ?)");
211
+ params.push(page.after.updatedAt, page.after.rfqId);
212
+ }
213
+ const where = conditions.length ? ` WHERE ${conditions.join(" AND ")}` : "";
214
+ const rows = await this.db.all(
215
+ `SELECT data, updated_at, rfq_id FROM ${this.rfqSwaps}${where}
216
+ ORDER BY updated_at, rfq_id LIMIT ?`,
217
+ [...params, page.limit + 1]
218
+ );
219
+ const result = (0, import_sdk2.pageResult)(rows, page.limit, (row) => ({
220
+ updatedAt: row.updated_at,
221
+ rfqId: row.rfq_id
222
+ }));
223
+ return {
224
+ items: result.items.map((row) => JSON.parse(row.data)),
225
+ ...result.nextCursor === void 0 ? {} : { nextCursor: result.nextCursor }
226
+ };
187
227
  }
188
228
  async removeRfqSwap(rfqId) {
189
229
  await this.ensureInit();
@@ -209,10 +249,21 @@ var SQLiteAssetSwapRepository = class {
209
249
  );
210
250
  return row ? JSON.parse(row.data) : void 0;
211
251
  }
212
- async getAllSwapRecords() {
252
+ async getSwapRecordsPage(page) {
253
+ return this.pageTable(this.swapRecords, "id", page);
254
+ }
255
+ async pageTable(table, key, page) {
256
+ (0, import_sdk2.assertPageRequest)(page);
213
257
  await this.ensureInit();
214
- const rows = await this.db.all(`SELECT data FROM ${this.swapRecords}`);
215
- return rows.map((r) => JSON.parse(r.data));
258
+ const rows = await this.db.all(
259
+ page.after === void 0 ? `SELECT ${key} AS key, data FROM ${table} ORDER BY ${key} LIMIT ?` : `SELECT ${key} AS key, data FROM ${table} WHERE ${key} > ? ORDER BY ${key} LIMIT ?`,
260
+ page.after === void 0 ? [page.limit + 1] : [page.after, page.limit + 1]
261
+ );
262
+ const result = (0, import_sdk2.pageResult)(rows, page.limit, (row) => row.key);
263
+ return {
264
+ items: result.items.map((row) => JSON.parse(row.data)),
265
+ ...result.nextCursor === void 0 ? {} : { nextCursor: result.nextCursor }
266
+ };
216
267
  }
217
268
  async removeSwapRecord(id) {
218
269
  await this.ensureInit();