@arkade-os/swap 0.1.7 → 0.1.9

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 (41) hide show
  1. package/dist/advanced.cjs +653 -262
  2. package/dist/advanced.d.cts +9 -8
  3. package/dist/advanced.d.ts +9 -8
  4. package/dist/advanced.js +4 -3
  5. package/dist/{chunk-7BBQ74BG.js → chunk-552GDPBU.js} +484 -128
  6. package/dist/chunk-5QPJWOCN.js +16 -0
  7. package/dist/{chunk-QFM55KP2.js → chunk-FQNG4XMF.js} +9 -3
  8. package/dist/{chunk-SDPNWPGY.js → chunk-I7WEXXZX.js} +27 -16
  9. package/dist/index.cjs +669 -278
  10. package/dist/index.d.cts +7 -6
  11. package/dist/index.d.ts +7 -6
  12. package/dist/index.js +4 -3
  13. package/dist/{lockupContract-CKPFpIt9.d.ts → lockupContract-BUOsghl1.d.ts} +1 -1
  14. package/dist/{lockupContract-CpJXTjX_.d.cts → lockupContract-DguuT7NK.d.cts} +1 -1
  15. package/dist/nostr.cjs +290 -17
  16. package/dist/nostr.d.cts +2 -1
  17. package/dist/nostr.d.ts +2 -1
  18. package/dist/nostr.js +2 -1
  19. package/dist/{offer-Drw1k_jt.d.ts → offer-BhonWuKR.d.ts} +22 -17
  20. package/dist/{offer-Boxive2u.d.cts → offer-D_qHpWo2.d.cts} +22 -17
  21. package/dist/{offerFillPlan-BxNkWLtw.d.cts → offerFillPlan-Bp6-z6dS.d.cts} +3 -3
  22. package/dist/{offerFillPlan-D2N4s7Xe.d.ts → offerFillPlan-_AOLo6kh.d.ts} +3 -3
  23. package/dist/protocol.cjs +594 -198
  24. package/dist/protocol.d.cts +9 -8
  25. package/dist/protocol.d.ts +9 -8
  26. package/dist/protocol.js +14 -2
  27. package/dist/repositories/realm/index.d.cts +3 -2
  28. package/dist/repositories/realm/index.d.ts +3 -2
  29. package/dist/repositories/sqlite/index.d.cts +3 -2
  30. package/dist/repositories/sqlite/index.d.ts +3 -2
  31. package/dist/{repository-DFDNvcZ7.d.cts → repository--pfikw2R.d.cts} +1 -1
  32. package/dist/{repository-Dn1V17T2.d.ts → repository-CkKK0dGO.d.ts} +1 -1
  33. package/dist/{rfq-CP-P0PSL.d.ts → rfq-BRpo5uWU.d.cts} +14 -2
  34. package/dist/{rfq-CP-P0PSL.d.cts → rfq-IzhOCG02.d.ts} +14 -2
  35. package/dist/rfq-contract.cjs +45 -0
  36. package/dist/rfq-contract.d.cts +9 -0
  37. package/dist/rfq-contract.d.ts +9 -0
  38. package/dist/rfq-contract.js +16 -0
  39. package/dist/{watch-D-mc_wfD.d.ts → watch-DDNycuUa.d.ts} +1 -1
  40. package/dist/{watch-DFNMY2K-.d.cts → watch-pvTVFp6-.d.cts} +1 -1
  41. package/package.json +13 -3
@@ -1,7 +1,367 @@
1
+ import {
2
+ ASSET_SWAP_BASELINE,
3
+ ASSET_SWAP_V2,
4
+ ASSET_SWAP_V3,
5
+ RFQ_CONTRACT_VERSIONS_KEY,
6
+ RFQ_CONTRACT_VERSION_KEY,
7
+ RFQ_CONTRACT_VERSION_UNSUPPORTED
8
+ } from "./chunk-5QPJWOCN.js";
1
9
  import {
2
10
  collectAssetSwaps
3
11
  } from "./chunk-A3SHHRX4.js";
4
12
 
13
+ // src/program.ts
14
+ import { arkade } from "@arkade-os/sdk";
15
+
16
+ // src/contracts/asset-swap.json
17
+ var asset_swap_default = {
18
+ formatVersion: 1,
19
+ contractName: "AssetSwap",
20
+ constructorInputs: [
21
+ {
22
+ name: "receiver",
23
+ type: "pubkey"
24
+ },
25
+ {
26
+ name: "amount",
27
+ type: "int"
28
+ },
29
+ {
30
+ name: "assetTxid",
31
+ type: "bytes32"
32
+ },
33
+ {
34
+ name: "assetGroupIndex",
35
+ type: "int"
36
+ },
37
+ {
38
+ name: "user",
39
+ type: "pubkey"
40
+ }
41
+ ],
42
+ functions: [
43
+ {
44
+ name: "fulfill",
45
+ arkade: {
46
+ inputs: [],
47
+ asm: [
48
+ "OP_PUSHCURRENTINPUTINDEX",
49
+ "OP_DUP",
50
+ "<assetTxid>",
51
+ "<assetGroupIndex>",
52
+ "OP_INSPECTOUTASSETLOOKUP",
53
+ "OP_VERIFY",
54
+ "<amount>",
55
+ "OP_GREATERTHANOREQUAL",
56
+ "OP_VERIFY",
57
+ "OP_INSPECTOUTPUTSCRIPTPUBKEY",
58
+ "1",
59
+ "OP_NUMEQUALVERIFY",
60
+ "<receiver>",
61
+ "OP_EQUAL"
62
+ ]
63
+ },
64
+ leaves: [
65
+ {
66
+ name: "fulfill",
67
+ witness: [
68
+ {
69
+ name: "serverSig",
70
+ type: "signature",
71
+ encoding: "schnorr-64",
72
+ injected: true
73
+ },
74
+ {
75
+ name: "emulatorSig",
76
+ type: "signature",
77
+ encoding: "schnorr-64",
78
+ injected: true
79
+ }
80
+ ],
81
+ asm: [
82
+ "<SERVER_KEY>",
83
+ "OP_CHECKSIGVERIFY",
84
+ "<EMULATOR_KEY:fulfill>",
85
+ "OP_CHECKSIG"
86
+ ]
87
+ }
88
+ ]
89
+ },
90
+ {
91
+ name: "cancel",
92
+ leaves: [
93
+ {
94
+ name: "cancel",
95
+ witness: [
96
+ {
97
+ name: "userSig",
98
+ type: "signature",
99
+ encoding: "schnorr-64"
100
+ },
101
+ {
102
+ name: "serverSig",
103
+ type: "signature",
104
+ encoding: "schnorr-64",
105
+ injected: true
106
+ }
107
+ ],
108
+ asm: [
109
+ "<user>",
110
+ "OP_CHECKSIGVERIFY",
111
+ "<SERVER_KEY>",
112
+ "OP_CHECKSIG"
113
+ ]
114
+ }
115
+ ]
116
+ }
117
+ ],
118
+ source: {
119
+ entry: "asset-swap.ark",
120
+ files: {
121
+ "asset-swap.ark": "pragma arkade ^0.1.0;\n\n// The output at this input's index pays `receiver` at least `amount` of the asset.\n// Coordination of who funds it lives outside the contract.\ncontract AssetSwap(\n pubkey receiver,\n int amount,\n bytes32 assetTxid,\n int assetGroupIndex,\n pubkey user\n) {\n function fulfill() {\n int i = this.activeInputIndex;\n require(tx.outputs[i].assets.lookup(assetTxid, assetGroupIndex) >= amount);\n let paid = tx.outputs[i].script;\n require(paid.version == 1);\n require(paid.program == receiver);\n }\n\n function cancel(signature userSig, signature serverSig) tapscript {\n require(checkMultisig([user, server], [userSig, serverSig], 2));\n }\n}\n"
122
+ }
123
+ },
124
+ compiler: {
125
+ name: "arkadec",
126
+ version: "0.1.0-rc.1",
127
+ options: {
128
+ optimize: true
129
+ }
130
+ },
131
+ updatedAt: "2026-11-02T17:43:42.615113722+00:00",
132
+ fingerprint: "sha256:e2b80add6237f6c7e2c69a96ae46d55abe6de5f71c3718106f4d5b22ac9e6d0a"
133
+ };
134
+
135
+ // src/contracts/sats-swap.json
136
+ var sats_swap_default = {
137
+ formatVersion: 1,
138
+ contractName: "SatsSwap",
139
+ constructorInputs: [
140
+ {
141
+ name: "receiver",
142
+ type: "pubkey"
143
+ },
144
+ {
145
+ name: "amount",
146
+ type: "int"
147
+ },
148
+ {
149
+ name: "user",
150
+ type: "pubkey"
151
+ }
152
+ ],
153
+ functions: [
154
+ {
155
+ name: "fulfill",
156
+ arkade: {
157
+ inputs: [],
158
+ asm: [
159
+ "OP_PUSHCURRENTINPUTINDEX",
160
+ "OP_DUP",
161
+ "OP_INSPECTOUTPUTVALUE",
162
+ "<amount>",
163
+ "OP_GREATERTHANOREQUAL",
164
+ "OP_VERIFY",
165
+ "OP_INSPECTOUTPUTSCRIPTPUBKEY",
166
+ "1",
167
+ "OP_NUMEQUALVERIFY",
168
+ "<receiver>",
169
+ "OP_EQUAL"
170
+ ]
171
+ },
172
+ leaves: [
173
+ {
174
+ name: "fulfill",
175
+ witness: [
176
+ {
177
+ name: "serverSig",
178
+ type: "signature",
179
+ encoding: "schnorr-64",
180
+ injected: true
181
+ },
182
+ {
183
+ name: "emulatorSig",
184
+ type: "signature",
185
+ encoding: "schnorr-64",
186
+ injected: true
187
+ }
188
+ ],
189
+ asm: [
190
+ "<SERVER_KEY>",
191
+ "OP_CHECKSIGVERIFY",
192
+ "<EMULATOR_KEY:fulfill>",
193
+ "OP_CHECKSIG"
194
+ ]
195
+ }
196
+ ]
197
+ },
198
+ {
199
+ name: "cancel",
200
+ leaves: [
201
+ {
202
+ name: "cancel",
203
+ witness: [
204
+ {
205
+ name: "userSig",
206
+ type: "signature",
207
+ encoding: "schnorr-64"
208
+ },
209
+ {
210
+ name: "serverSig",
211
+ type: "signature",
212
+ encoding: "schnorr-64",
213
+ injected: true
214
+ }
215
+ ],
216
+ asm: [
217
+ "<user>",
218
+ "OP_CHECKSIGVERIFY",
219
+ "<SERVER_KEY>",
220
+ "OP_CHECKSIG"
221
+ ]
222
+ }
223
+ ]
224
+ }
225
+ ],
226
+ source: {
227
+ entry: "sats-swap.ark",
228
+ files: {
229
+ "sats-swap.ark": "pragma arkade ^0.1.0;\n\n// The output at this input's index pays `receiver` at least `amount` sats.\n// Coordination of who funds it lives outside the contract.\ncontract SatsSwap(\n pubkey receiver,\n int amount,\n pubkey user\n) {\n function fulfill() {\n int i = this.activeInputIndex;\n require(tx.outputs[i].value >= amount);\n let paid = tx.outputs[i].script;\n require(paid.version == 1);\n require(paid.program == receiver);\n }\n\n function cancel(signature userSig, signature serverSig) tapscript {\n require(checkMultisig([user, server], [userSig, serverSig], 2));\n }\n}\n"
230
+ }
231
+ },
232
+ compiler: {
233
+ name: "arkadec",
234
+ version: "0.1.0-rc.1",
235
+ options: {
236
+ optimize: true
237
+ }
238
+ },
239
+ updatedAt: "2026-11-02T17:43:42.616824535+00:00",
240
+ fingerprint: "sha256:d7a60ab2178ede40a2e4867816689ea022641531cc662b562bebd7caa1ef96c4"
241
+ };
242
+
243
+ // src/legacy/swap-want-asset.program.v1.json
244
+ var swap_want_asset_program_v1_default = {
245
+ version: 1,
246
+ name: "banco-btc-to-asset",
247
+ params: [
248
+ { name: "makerWP", type: "pubkey" },
249
+ { name: "wantAmount", type: "int" },
250
+ { name: "wantAssetTxid", type: "bytes32" },
251
+ { name: "wantAssetGroupIndex", type: "int" },
252
+ { name: "server", type: "pubkey" },
253
+ { name: "user", type: "pubkey" }
254
+ ],
255
+ functions: {
256
+ fulfill: {
257
+ tapscript: { signers: ["$server"] },
258
+ arkadeScript: {
259
+ asm: [
260
+ 0,
261
+ "$wantAssetTxid",
262
+ "$wantAssetGroupIndex",
263
+ "INSPECTOUTASSETLOOKUP",
264
+ "VERIFY",
265
+ "$wantAmount",
266
+ "GREATERTHANOREQUAL",
267
+ "VERIFY",
268
+ 0,
269
+ "INSPECTOUTPUTSCRIPTPUBKEY",
270
+ 1,
271
+ "EQUALVERIFY",
272
+ "$makerWP",
273
+ "EQUAL"
274
+ ]
275
+ }
276
+ },
277
+ cancel: {
278
+ tapscript: { signers: ["$user", "$server"] }
279
+ }
280
+ }
281
+ };
282
+
283
+ // src/legacy/swap-want-btc.program.v1.json
284
+ var swap_want_btc_program_v1_default = {
285
+ version: 1,
286
+ name: "banco-asset-to-btc",
287
+ params: [
288
+ { name: "makerWP", type: "pubkey" },
289
+ { name: "wantAmount", type: "int" },
290
+ { name: "server", type: "pubkey" },
291
+ { name: "user", type: "pubkey" }
292
+ ],
293
+ functions: {
294
+ fulfill: {
295
+ tapscript: { signers: ["$server"] },
296
+ arkadeScript: {
297
+ asm: [
298
+ 0,
299
+ "INSPECTOUTPUTVALUE",
300
+ "$wantAmount",
301
+ "GREATERTHANOREQUAL",
302
+ "VERIFY",
303
+ 0,
304
+ "INSPECTOUTPUTSCRIPTPUBKEY",
305
+ 1,
306
+ "EQUALVERIFY",
307
+ "$makerWP",
308
+ "EQUAL"
309
+ ]
310
+ }
311
+ },
312
+ cancel: {
313
+ tapscript: { signers: ["$user", "$server"] }
314
+ }
315
+ }
316
+ };
317
+
318
+ // src/program.ts
319
+ var swapPrograms = {
320
+ wantAsset: arkade.programFromArtifact(asset_swap_default),
321
+ wantBtc: arkade.programFromArtifact(sats_swap_default)
322
+ };
323
+ var legacySwapPrograms = {
324
+ wantAsset: arkade.parseArtifact(swap_want_asset_program_v1_default),
325
+ wantBtc: arkade.parseArtifact(swap_want_btc_program_v1_default)
326
+ };
327
+ var sharedArgs = ({ user, server, exitDelay }) => ({
328
+ user,
329
+ server,
330
+ ...exitDelay && { exitDelay }
331
+ });
332
+ var ASSET_SWAP_CONTRACTS = {
333
+ [ASSET_SWAP_V2]: {
334
+ programs: legacySwapPrograms,
335
+ args: (inputs) => ({
336
+ makerWP: inputs.receiver,
337
+ wantAmount: inputs.amount,
338
+ ...inputs.asset && {
339
+ wantAssetTxid: inputs.asset.txid,
340
+ wantAssetGroupIndex: inputs.asset.groupIndex
341
+ },
342
+ ...sharedArgs(inputs)
343
+ })
344
+ },
345
+ [ASSET_SWAP_V3]: {
346
+ programs: swapPrograms,
347
+ args: (inputs) => ({
348
+ receiver: inputs.receiver,
349
+ amount: inputs.amount,
350
+ ...inputs.asset && {
351
+ assetTxid: inputs.asset.txid,
352
+ assetGroupIndex: inputs.asset.groupIndex
353
+ },
354
+ ...sharedArgs(inputs)
355
+ })
356
+ }
357
+ };
358
+ var ASSET_SWAP_CONTRACT_VERSIONS = [ASSET_SWAP_V3, ASSET_SWAP_V2];
359
+ var assetSwapContractVersion = (id) => {
360
+ if (id === void 0) return ASSET_SWAP_BASELINE;
361
+ if (Object.hasOwn(ASSET_SWAP_CONTRACTS, id)) return id;
362
+ throw new Error(`unknown contract version ${id}`);
363
+ };
364
+
5
365
  // src/store.ts
6
366
  import { hex } from "@scure/base";
7
367
  import { sha256 } from "@noble/hashes/sha2.js";
@@ -243,88 +603,13 @@ import {
243
603
  RestOperatorProvider,
244
604
  RestEmulatorProvider,
245
605
  RestIndexerProvider,
246
- arkade,
606
+ arkade as arkade2,
247
607
  asset,
248
608
  networkFromOperatorInfo,
249
609
  resolveEmulatorPubkey,
250
610
  toXOnlySignerHex
251
611
  } from "@arkade-os/sdk";
252
612
 
253
- // src/swap-want-asset.program.json
254
- var swap_want_asset_program_default = {
255
- version: 1,
256
- name: "banco-btc-to-asset",
257
- params: [
258
- { name: "makerWP", type: "pubkey" },
259
- { name: "wantAmount", type: "int" },
260
- { name: "wantAssetTxid", type: "bytes32" },
261
- { name: "wantAssetGroupIndex", type: "int" },
262
- { name: "server", type: "pubkey" },
263
- { name: "user", type: "pubkey" }
264
- ],
265
- functions: {
266
- fulfill: {
267
- tapscript: { signers: ["$server"] },
268
- arkadeScript: {
269
- asm: [
270
- 0,
271
- "$wantAssetTxid",
272
- "$wantAssetGroupIndex",
273
- "INSPECTOUTASSETLOOKUP",
274
- "VERIFY",
275
- "$wantAmount",
276
- "GREATERTHANOREQUAL",
277
- "VERIFY",
278
- 0,
279
- "INSPECTOUTPUTSCRIPTPUBKEY",
280
- 1,
281
- "EQUALVERIFY",
282
- "$makerWP",
283
- "EQUAL"
284
- ]
285
- }
286
- },
287
- cancel: {
288
- tapscript: { signers: ["$user", "$server"] }
289
- }
290
- }
291
- };
292
-
293
- // src/swap-want-btc.program.json
294
- var swap_want_btc_program_default = {
295
- version: 1,
296
- name: "banco-asset-to-btc",
297
- params: [
298
- { name: "makerWP", type: "pubkey" },
299
- { name: "wantAmount", type: "int" },
300
- { name: "server", type: "pubkey" },
301
- { name: "user", type: "pubkey" }
302
- ],
303
- functions: {
304
- fulfill: {
305
- tapscript: { signers: ["$server"] },
306
- arkadeScript: {
307
- asm: [
308
- 0,
309
- "INSPECTOUTPUTVALUE",
310
- "$wantAmount",
311
- "GREATERTHANOREQUAL",
312
- "VERIFY",
313
- 0,
314
- "INSPECTOUTPUTSCRIPTPUBKEY",
315
- 1,
316
- "EQUALVERIFY",
317
- "$makerWP",
318
- "EQUAL"
319
- ]
320
- }
321
- },
322
- cancel: {
323
- tapscript: { signers: ["$user", "$server"] }
324
- }
325
- }
326
- };
327
-
328
613
  // src/coverage.ts
329
614
  var RETIRABLE = ["fulfilled", "cancelled"];
330
615
  var LOCKUP_RETIRABLE = ["settled", "refunded"];
@@ -405,10 +690,6 @@ var registerContractCoverage = async (script, register, options = {}) => seriali
405
690
  });
406
691
 
407
692
  // src/offer.ts
408
- var swapPrograms = {
409
- wantAsset: arkade.parseArtifact(swap_want_asset_program_default),
410
- wantBtc: arkade.parseArtifact(swap_want_btc_program_default)
411
- };
412
693
  function withExitClosure(program, exit) {
413
694
  if (!exit) return program;
414
695
  return {
@@ -424,37 +705,61 @@ function withExitClosure(program, exit) {
424
705
  }
425
706
  };
426
707
  }
427
- function swapProgramBinding(offer, operatorPubkey) {
708
+ function swapProgramBinding(offer, operatorPubkey, contractVersion = ASSET_SWAP_BASELINE) {
428
709
  if (offer.makerPkScript.length !== FIELDS.makerPkScript.width) {
429
710
  throw new Error("makerPkScript is not a 34-byte taproot scriptPubKey");
430
711
  }
712
+ const expected = offer.swapPkScript;
713
+ if (!expected?.length) return bindSwapProgram(offer, operatorPubkey, contractVersion);
714
+ const funded = hex2.encode(expected);
715
+ const order = [
716
+ ASSET_SWAP_BASELINE,
717
+ ...ASSET_SWAP_CONTRACT_VERSIONS.filter((v) => v !== ASSET_SWAP_BASELINE)
718
+ ];
719
+ for (const version of order) {
720
+ const binding = bindSwapProgram(offer, operatorPubkey, version);
721
+ if (pkOf(binding) === funded) return binding;
722
+ }
723
+ return bindSwapProgram(offer, operatorPubkey, contractVersion);
724
+ }
725
+ function bindSwapProgram(offer, operatorPubkey, contractVersion) {
726
+ const { programs, args } = ASSET_SWAP_CONTRACTS[contractVersion];
727
+ const user = offer.makerPublicKey;
728
+ const assetId = offer.wantAsset;
431
729
  return {
432
- program: withExitClosure(
433
- offer.wantAsset ? swapPrograms.wantAsset : swapPrograms.wantBtc,
434
- offer.exitDelay
435
- ),
436
- args: {
437
- makerWP: offer.makerPkScript.subarray(2),
438
- wantAmount: offer.wantAmount,
439
- server: operatorPubkey,
440
- user: offer.makerPublicKey,
730
+ program: withExitClosure(assetId ? programs.wantAsset : programs.wantBtc, offer.exitDelay),
731
+ args: args({
732
+ receiver: offer.makerPkScript.subarray(2),
733
+ amount: offer.wantAmount,
441
734
  // internal byte order
442
- ...offer.wantAsset && {
443
- wantAssetTxid: offer.wantAsset.txid.slice().reverse(),
444
- wantAssetGroupIndex: offer.wantAsset.groupIndex
735
+ ...assetId && {
736
+ asset: { txid: assetId.txid.slice().reverse(), groupIndex: assetId.groupIndex }
445
737
  },
738
+ user,
739
+ server: operatorPubkey,
446
740
  ...offer.exitDelay && { exitDelay: offer.exitDelay.value }
447
- },
741
+ }),
448
742
  keys: {
449
743
  serverKey: operatorPubkey,
450
- userKey: offer.makerPublicKey,
744
+ userKey: user,
451
745
  emulatorKey: offer.emulatorPubkey
452
746
  }
453
747
  };
454
748
  }
455
- function offerContract(offer, operatorPubkey) {
456
- const { program, args, keys } = swapProgramBinding(offer, operatorPubkey);
457
- return new arkade.ArkadeProgramScript(program, args, keys);
749
+ var compiledBindings = /* @__PURE__ */ new WeakMap();
750
+ function compileSwapBinding(binding) {
751
+ let script = compiledBindings.get(binding);
752
+ if (!script) {
753
+ script = new arkade2.ArkadeProgramScript(binding.program, binding.args, binding.keys);
754
+ compiledBindings.set(binding, script);
755
+ }
756
+ return script;
757
+ }
758
+ function pkOf(binding) {
759
+ return hex2.encode(compileSwapBinding(binding).pkScript);
760
+ }
761
+ function offerContract(offer, operatorPubkey, contractVersion) {
762
+ return compileSwapBinding(swapProgramBinding(offer, operatorPubkey, contractVersion));
458
763
  }
459
764
  var OFFER_PACKET_TYPE = 3;
460
765
  var FIELDS = {
@@ -619,15 +924,18 @@ async function registerOfferContract(wallet, info, binding, operatorPubkey, expe
619
924
  scope: wallet
620
925
  });
621
926
  }
622
- var derivingClient = (wallet, info, contractManager) => arkade.Arkade.connect({
927
+ var derivingClient = (wallet, info, contractManager) => arkade2.Arkade.connect({
623
928
  arkade: { getInfo: async () => info },
624
929
  identity: wallet.identity,
625
930
  network: networkFromOperatorInfo(info),
626
931
  contractManager
627
932
  });
628
933
  function offerContractParams(client, binding, operatorPubkey, expectedPkScript) {
629
- const { program, args, keys } = swapProgramBinding(binding, operatorPubkey);
630
- const contract = new arkade.ArkadeContract(client, program, args, keys);
934
+ const { program, args, keys } = swapProgramBinding(
935
+ { ...binding, swapPkScript: expectedPkScript },
936
+ operatorPubkey
937
+ );
938
+ const contract = new arkade2.ArkadeContract(client, program, args, keys);
631
939
  if (hex2.encode(contract.pkScript) !== hex2.encode(expectedPkScript)) {
632
940
  throw new Error("derived covenant does not match the offer's swapPkScript");
633
941
  }
@@ -769,7 +1077,7 @@ async function deriveOffer(wallet, params) {
769
1077
  // checked before registration below. @see assertExitDelay
770
1078
  exitDelay: params.noExit ? void 0 : params.exitDelay ? assertExitDelay(params.exitDelay) : serverExitDelay(info.unilateralExitDelay)
771
1079
  });
772
- const script = offerContract(binding, operatorPubKey);
1080
+ const script = offerContract(binding, operatorPubKey, params.contractVersion);
773
1081
  const offer = { ...binding, swapPkScript: script.pkScript };
774
1082
  const payload = encodeOffer(offer);
775
1083
  return Object.freeze({
@@ -848,7 +1156,7 @@ async function prepareOfferCancel(wallet, offerHex, opts) {
848
1156
  wallet.getArkadeReader(),
849
1157
  wallet.getArkadeBroadcaster()
850
1158
  ]);
851
- const client = await arkade.Arkade.connect({
1159
+ const client = await arkade2.Arkade.connect({
852
1160
  arkade: { getInfo: async () => info, ...broadcaster },
853
1161
  indexer: reader,
854
1162
  identity: wallet.identity,
@@ -859,17 +1167,18 @@ async function prepareOfferCancel(wallet, offerHex, opts) {
859
1167
  oneShotSubmit: wallet.oneShotSubmit
860
1168
  });
861
1169
  const operatorPubkey = swapAddress ? ArkAddress.decode(swapAddress).serverPubKey : client.serverKey;
862
- const { program, args, keys } = swapProgramBinding(offer, operatorPubkey);
1170
+ let binding;
863
1171
  let rebuilt;
864
1172
  try {
865
- rebuilt = new arkade.ArkadeProgramScript(program, args, keys);
1173
+ binding = swapProgramBinding(offer, operatorPubkey);
1174
+ rebuilt = compileSwapBinding(binding);
866
1175
  } catch (cause) {
867
1176
  throw new OfferCovenantMismatchError(hex2.encode(offer.swapPkScript), { cause });
868
1177
  }
869
1178
  if (hex2.encode(rebuilt.pkScript) !== hex2.encode(offer.swapPkScript)) {
870
1179
  throw new OfferCovenantMismatchError(hex2.encode(offer.swapPkScript));
871
1180
  }
872
- const contract = new arkade.ArkadeContract(client, program, args, keys);
1181
+ const contract = new arkade2.ArkadeContract(client, binding.program, binding.args, binding.keys);
873
1182
  const [vtxos, makerAddress] = await Promise.all([contract.getUtxos(), wallet.getAddress()]);
874
1183
  const vtxo = resolveDeposit(vtxos, { fundingTxid, fundingOutpoint });
875
1184
  const makerPkScript = ArkAddress.decode(makerAddress).pkScript;
@@ -921,7 +1230,7 @@ async function fillOffer(wallet, arkServerUrl, offerHex, opts) {
921
1230
  async function connectFillContract(wallet, arkServerUrl, offer, opts = {}) {
922
1231
  const { swapAddress, emulator, emulatorPubkey } = opts;
923
1232
  const contractManager = await wallet.getContractManager();
924
- const client = await arkade.Arkade.connect({
1233
+ const client = await arkade2.Arkade.connect({
925
1234
  arkade: new RestOperatorProvider(arkServerUrl),
926
1235
  indexer: new RestIndexerProvider(arkServerUrl),
927
1236
  identity: wallet.identity,
@@ -932,14 +1241,14 @@ async function connectFillContract(wallet, arkServerUrl, offer, opts = {}) {
932
1241
  ...emulatorPubkey ? { emulatorPubkey } : {}
933
1242
  });
934
1243
  const serverKey = swapAddress ? ArkAddress.decode(swapAddress).serverPubKey : client.serverKey;
935
- const { program, args, keys } = swapProgramBinding(offer, serverKey);
936
- const rebuilt = new arkade.ArkadeProgramScript(program, args, keys);
1244
+ const binding = swapProgramBinding(offer, serverKey);
1245
+ const rebuilt = compileSwapBinding(binding);
937
1246
  if (hex2.encode(rebuilt.pkScript) !== hex2.encode(offer.swapPkScript)) {
938
1247
  throw new Error(
939
1248
  "rebuilt covenant does not match the offer's swapPkScript \u2014 the server signing key has likely rotated since funding; pass swapAddress (the funded address) to pin the original key"
940
1249
  );
941
1250
  }
942
- return new arkade.ArkadeContract(client, program, args, keys);
1251
+ return new arkade2.ArkadeContract(client, binding.program, binding.args, binding.keys);
943
1252
  }
944
1253
  function resolveDeposit(vtxos, sel) {
945
1254
  const { fundingTxid, fundingOutpoint } = sel;
@@ -1373,9 +1682,9 @@ function onchainHtlcScript(params, network) {
1373
1682
  if (params.claimKey.length !== 32 || params.refundKey.length !== 32) {
1374
1683
  throw new Error("claimKey and refundKey must be 32-byte x-only keys");
1375
1684
  }
1376
- if (!Number.isInteger(params.refundLocktime) || params.refundLocktime <= 0) {
1685
+ if (!Number.isInteger(params.refundLocktime) || params.refundLocktime <= 0 || params.refundLocktime > 4294967295) {
1377
1686
  throw new Error(
1378
- `refundLocktime must be a positive unix timestamp, got ${params.refundLocktime}`
1687
+ `refundLocktime must be a positive uint32 unix timestamp, got ${params.refundLocktime}`
1379
1688
  );
1380
1689
  }
1381
1690
  if (params.refundLocktime < LOCKTIME_THRESHOLD) {
@@ -1550,15 +1859,20 @@ async function claimOnchainFill(chain, input) {
1550
1859
  }
1551
1860
  async function classifyOnchainHtlc(chain, input) {
1552
1861
  const utxos = await chain.getScriptUtxos(input.htlc.pkScript);
1553
- const best = utxos.sort((a, b) => b.amount > a.amount ? 1 : -1)[0];
1554
- if (!best) {
1555
- if (!input.funding) return { phase: "unfunded" };
1556
- const spend = await chain.getSpendingTx(
1557
- input.funding.txid,
1558
- input.funding.vout,
1559
- input.htlc.pkScript
1560
- );
1561
- if (!spend) return { phase: "unfunded" };
1862
+ const deep = (u) => u.confirmations >= input.minConfirmations;
1863
+ const best = utxos.sort(
1864
+ (a, b) => deep(a) !== deep(b) ? deep(a) ? -1 : 1 : b.amount > a.amount ? 1 : -1
1865
+ )[0];
1866
+ const { funding } = input;
1867
+ let spend = null;
1868
+ if (funding && !utxos.some((u) => u.txid === funding.txid && u.vout === funding.vout)) {
1869
+ try {
1870
+ spend = await chain.getSpendingTx(funding.txid, funding.vout, input.htlc.pkScript);
1871
+ } catch (error) {
1872
+ if (!best) throw error;
1873
+ }
1874
+ }
1875
+ if (spend) {
1562
1876
  const preimage = extractPreimage(spend.txHex, input.htlc.paymentHash);
1563
1877
  const txid = btc.Transaction.fromRaw(hex4.decode(spend.txHex), {
1564
1878
  allowUnknownInputs: true,
@@ -1566,6 +1880,7 @@ async function classifyOnchainHtlc(chain, input) {
1566
1880
  }).id;
1567
1881
  return preimage ? { phase: "claimed", txid, preimage } : { phase: "swept", txid };
1568
1882
  }
1883
+ if (!best) return { phase: "unfunded" };
1569
1884
  if (best.confirmations < input.minConfirmations)
1570
1885
  return { phase: "awaiting_confirmations", utxo: best };
1571
1886
  const mtp = await chain.getMtp();
@@ -1731,6 +2046,7 @@ var ONCHAIN_RECEIVE_PAIR = rfqPair(ONCHAIN_BTC, ARKADE_BTC);
1731
2046
  var RFQ_TERMINAL_STATES = ["settled", "refused", "expired", "refunded", "stuck"];
1732
2047
  var RFQ_REFUSAL_ERROR_CODES = [
1733
2048
  "amount_side_unsupported",
2049
+ RFQ_CONTRACT_VERSION_UNSUPPORTED,
1734
2050
  "exact_out_unsupported",
1735
2051
  "invalid_amount",
1736
2052
  "invalid_payout_address",
@@ -1846,6 +2162,7 @@ var arkadeSwapRequest = (input) => {
1846
2162
  pair,
1847
2163
  amount_side: input.amountSide ?? "from",
1848
2164
  amount: canonicalAssetAmount(input.amount),
2165
+ ...input.contractVersions && input.contractVersions.length > 0 ? { [RFQ_CONTRACT_VERSIONS_KEY]: [...input.contractVersions] } : {},
1849
2166
  // The solver's `.strict()` schema refuses any undeclared key as `unsupported_payload`.
1850
2167
  profile: {
1851
2168
  maker_pk_script: normalizeMakerPkScript(input.makerPkScript),
@@ -2033,6 +2350,34 @@ var expectQuote = (payload, rfqId, requestedPair) => {
2033
2350
  }
2034
2351
  return payload;
2035
2352
  };
2353
+ var requestVersionedQuote = async (transport, request, advertised) => {
2354
+ if (advertised.length === 0) {
2355
+ throw new Error("contractVersions is empty: advertise at least one contract version");
2356
+ }
2357
+ let sent = request;
2358
+ let quote;
2359
+ try {
2360
+ quote = await transport.requestQuote(request);
2361
+ } catch (error) {
2362
+ if (!(error instanceof SwapRefusal) || error.reason !== "unsupported_payload" || error.errorCode !== void 0 || !advertised.includes(ASSET_SWAP_BASELINE)) {
2363
+ throw error;
2364
+ }
2365
+ const { [RFQ_CONTRACT_VERSIONS_KEY]: _, ...baseline } = request;
2366
+ sent = { ...baseline, rfq_id: newRfqId() };
2367
+ quote = await transport.requestQuote(sent);
2368
+ }
2369
+ const selected = quote[RFQ_CONTRACT_VERSION_KEY];
2370
+ if (sent !== request && selected !== void 0) {
2371
+ throw new Error(`solver chose contract version ${selected}, but none was advertised`);
2372
+ }
2373
+ const version = selected ?? ASSET_SWAP_BASELINE;
2374
+ if (!advertised.includes(version)) {
2375
+ throw new Error(
2376
+ `solver chose contract version ${version}, not one of ${advertised.join(", ")}`
2377
+ );
2378
+ }
2379
+ return { quote, rfqId: sent.rfq_id, contractVersion: assetSwapContractVersion(version) };
2380
+ };
2036
2381
  var pairOf = (payload) => typeof payload.pair === "string" ? payload.pair : void 0;
2037
2382
  var httpTransport = (baseUrl, options = {}) => {
2038
2383
  const fetchImpl = options.fetchImpl ?? fetch;
@@ -2366,7 +2711,6 @@ async function requestArkadeSwap(wallet, transport, params) {
2366
2711
  if (!params.wantAsset && !params.offerAsset) {
2367
2712
  throw new Error("set at least one of wantAsset or offerAsset; BTC-to-BTC is not a swap");
2368
2713
  }
2369
- const rfqId = params.rfqId ?? newRfqId();
2370
2714
  const amountSide = params.amountSide ?? "from";
2371
2715
  const receiveAddress = params.receiveAddress;
2372
2716
  const [makerAddress, makerPublicKey] = await Promise.all([
@@ -2385,16 +2729,20 @@ async function requestArkadeSwap(wallet, transport, params) {
2385
2729
  params.offerAsset ? arkadeAssetLeg(params.offerAsset) : ARKADE_BTC,
2386
2730
  params.wantAsset ? arkadeAssetLeg(params.wantAsset) : ARKADE_BTC
2387
2731
  );
2388
- const quote = await transport.requestQuote(
2732
+ const contractVersions = [...params.contractVersions ?? ASSET_SWAP_CONTRACT_VERSIONS];
2733
+ const { quote, rfqId, contractVersion } = await requestVersionedQuote(
2734
+ transport,
2389
2735
  arkadeSwapRequest({
2390
- rfqId,
2736
+ rfqId: params.rfqId ?? newRfqId(),
2391
2737
  ...params.offerAsset !== void 0 ? { offerAsset: params.offerAsset } : {},
2392
2738
  ...params.wantAsset !== void 0 ? { wantAsset: params.wantAsset } : {},
2393
2739
  amount: params.amount,
2394
2740
  amountSide,
2395
2741
  makerPkScript,
2396
- makerPublicKey
2397
- })
2742
+ makerPublicKey,
2743
+ contractVersions
2744
+ }),
2745
+ contractVersions
2398
2746
  );
2399
2747
  if (quote.pair !== pair) {
2400
2748
  throw new Error(`solver quoted ${JSON.stringify(quote.pair)}, not the requested ${pair}`);
@@ -2435,7 +2783,8 @@ async function requestArkadeSwap(wallet, transport, params) {
2435
2783
  wantAmount: terms.wantAmount,
2436
2784
  ...terms.wantAsset !== void 0 ? { wantAsset: terms.wantAsset } : {},
2437
2785
  ...terms.offerAsset !== void 0 ? { offerAsset: terms.offerAsset } : {},
2438
- ...params.emulatorPubkey !== void 0 ? { emulatorPubkey: params.emulatorPubkey } : {}
2786
+ ...params.emulatorPubkey !== void 0 ? { emulatorPubkey: params.emulatorPubkey } : {},
2787
+ contractVersion
2439
2788
  });
2440
2789
  verifyOfferAddress(quote, offer);
2441
2790
  const fundAmount = BigInt(quote.from_amount);
@@ -2682,6 +3031,9 @@ var assertReceivable = (input) => {
2682
3031
  if (input.now >= input.payDeadline) {
2683
3032
  throw gateError("quote_expired", "quote or invoice already expired \u2014 request a fresh one");
2684
3033
  }
3034
+ if (quoteAmountSats(input.quote.from_amount, "from_amount") <= 0n || quoteAmountSats(input.quote.to_amount, "to_amount") <= 0n) {
3035
+ throw gateError("non_positive_amount", "quote carries a non-positive amount");
3036
+ }
2685
3037
  if (input.quote.refund_locktime === void 0) {
2686
3038
  throw gateError("missing_refund_locktime", "receive quote carries no refund_locktime");
2687
3039
  }
@@ -2936,6 +3288,10 @@ async function requestOnchainReceive(wallet, transport, params) {
2936
3288
  }
2937
3289
 
2938
3290
  export {
3291
+ swapPrograms,
3292
+ legacySwapPrograms,
3293
+ ASSET_SWAP_CONTRACT_VERSIONS,
3294
+ assetSwapContractVersion,
2939
3295
  RETIRABLE,
2940
3296
  LOCKUP_RETIRABLE,
2941
3297
  promoteOfferContract,
@@ -2955,7 +3311,6 @@ export {
2955
3311
  rfqSignerOf,
2956
3312
  rfqClaimSecretOf,
2957
3313
  rfqClaimDestinationOf,
2958
- swapPrograms,
2959
3314
  withExitClosure,
2960
3315
  offerContract,
2961
3316
  OFFER_PACKET_TYPE,
@@ -3037,6 +3392,7 @@ export {
3037
3392
  verifyLockupAddress,
3038
3393
  assertFundable,
3039
3394
  expectQuote,
3395
+ requestVersionedQuote,
3040
3396
  pairOf,
3041
3397
  httpTransport,
3042
3398
  relayTransport,