@arkade-os/swap 0.1.8 → 0.1.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.
Files changed (45) hide show
  1. package/dist/advanced.cjs +1125 -554
  2. package/dist/advanced.d.cts +19 -21
  3. package/dist/advanced.d.ts +19 -21
  4. package/dist/advanced.js +40 -34
  5. package/dist/{chunk-ISWLKKSS.js → chunk-2HQZLIVX.js} +106 -39
  6. package/dist/{chunk-5QPJWOCN.js → chunk-6RJVT65W.js} +2 -0
  7. package/dist/chunk-AUJDREZN.js +445 -0
  8. package/dist/{chunk-ZBJGLLYV.js → chunk-FWRDAL73.js} +157 -477
  9. package/dist/{chunk-PDNVKH5L.js → chunk-LCO6AJOS.js} +453 -70
  10. package/dist/index.cjs +1110 -542
  11. package/dist/index.d.cts +5 -6
  12. package/dist/index.d.ts +5 -6
  13. package/dist/index.js +24 -22
  14. package/dist/{lockupContract-DguuT7NK.d.cts → lockupContract-BTTRVwOt.d.cts} +1 -1
  15. package/dist/{lockupContract-BUOsghl1.d.ts → lockupContract-aFarhlxj.d.ts} +1 -1
  16. package/dist/node/index.d.cts +97 -67
  17. package/dist/node/index.d.ts +97 -67
  18. package/dist/nostr.cjs +925 -516
  19. package/dist/nostr.d.cts +1 -1
  20. package/dist/nostr.d.ts +1 -1
  21. package/dist/nostr.js +3 -2
  22. package/dist/{offerFillPlan-_AOLo6kh.d.ts → offerFillPlan-CJC5tnEd.d.ts} +10 -42
  23. package/dist/{offerFillPlan-Bp6-z6dS.d.cts → offerFillPlan-FmyaQoYT.d.cts} +10 -42
  24. package/dist/protocol.cjs +1082 -188
  25. package/dist/protocol.d.cts +42 -23
  26. package/dist/protocol.d.ts +42 -23
  27. package/dist/protocol.js +20 -7
  28. package/dist/repositories/realm/index.d.cts +3 -2
  29. package/dist/repositories/realm/index.d.ts +3 -2
  30. package/dist/repositories/sqlite/index.d.cts +3 -2
  31. package/dist/repositories/sqlite/index.d.ts +3 -2
  32. package/dist/{repository-CkKK0dGO.d.ts → repository-CwwhIAEd.d.cts} +406 -74
  33. package/dist/{repository--pfikw2R.d.cts → repository-DO6QKxBx.d.ts} +406 -74
  34. package/dist/{rfq-IzhOCG02.d.ts → rfq-CYxOyniL.d.ts} +52 -11
  35. package/dist/rfq-contract.cjs +3 -0
  36. package/dist/rfq-contract.d.cts +3 -1
  37. package/dist/rfq-contract.d.ts +3 -1
  38. package/dist/rfq-contract.js +3 -1
  39. package/dist/{rfq-BRpo5uWU.d.cts → rfq-nMGywkqI.d.cts} +52 -11
  40. package/dist/verify-ENO36VPK.js +33 -0
  41. package/dist/{watch-pvTVFp6-.d.cts → watch-DpDSP2tu.d.cts} +4 -3
  42. package/dist/{watch-DDNycuUa.d.ts → watch-_fZSgx_l.d.ts} +4 -3
  43. package/package.json +2 -2
  44. package/dist/offer-BhonWuKR.d.ts +0 -274
  45. package/dist/offer-D_qHpWo2.d.cts +0 -274
@@ -2,10 +2,11 @@ import {
2
2
  ASSET_SWAP_BASELINE,
3
3
  ASSET_SWAP_V2,
4
4
  ASSET_SWAP_V3,
5
+ ASSET_SWAP_V4,
5
6
  RFQ_CONTRACT_VERSIONS_KEY,
6
7
  RFQ_CONTRACT_VERSION_KEY,
7
8
  RFQ_CONTRACT_VERSION_UNSUPPORTED
8
- } from "./chunk-5QPJWOCN.js";
9
+ } from "./chunk-6RJVT65W.js";
9
10
  import {
10
11
  collectAssetSwaps
11
12
  } from "./chunk-A3SHHRX4.js";
@@ -132,6 +133,136 @@ var asset_swap_default = {
132
133
  fingerprint: "sha256:e2b80add6237f6c7e2c69a96ae46d55abe6de5f71c3718106f4d5b22ac9e6d0a"
133
134
  };
134
135
 
136
+ // src/contracts/asset-swap-v4.json
137
+ var asset_swap_v4_default = {
138
+ formatVersion: 1,
139
+ contractName: "AssetSwap",
140
+ constructorInputs: [
141
+ {
142
+ name: "receiver",
143
+ type: "pubkey"
144
+ },
145
+ {
146
+ name: "amount",
147
+ type: "int"
148
+ },
149
+ {
150
+ name: "assetTxid",
151
+ type: "bytes32"
152
+ },
153
+ {
154
+ name: "assetGroupIndex",
155
+ type: "int"
156
+ },
157
+ {
158
+ name: "user",
159
+ type: "pubkey"
160
+ },
161
+ {
162
+ name: "taker",
163
+ type: "pubkey"
164
+ }
165
+ ],
166
+ functions: [
167
+ {
168
+ name: "fulfill",
169
+ arkade: {
170
+ inputs: [],
171
+ asm: [
172
+ "OP_PUSHCURRENTINPUTINDEX",
173
+ "OP_DUP",
174
+ "<assetTxid>",
175
+ "<assetGroupIndex>",
176
+ "OP_INSPECTOUTASSETLOOKUP",
177
+ "OP_VERIFY",
178
+ "<amount>",
179
+ "OP_GREATERTHANOREQUAL",
180
+ "OP_VERIFY",
181
+ "OP_INSPECTOUTPUTSCRIPTPUBKEY",
182
+ "1",
183
+ "OP_NUMEQUALVERIFY",
184
+ "<receiver>",
185
+ "OP_EQUAL"
186
+ ]
187
+ },
188
+ leaves: [
189
+ {
190
+ name: "fulfill",
191
+ witness: [
192
+ {
193
+ name: "takerSig",
194
+ type: "signature",
195
+ encoding: "schnorr-64"
196
+ },
197
+ {
198
+ name: "serverSig",
199
+ type: "signature",
200
+ encoding: "schnorr-64",
201
+ injected: true
202
+ },
203
+ {
204
+ name: "emulatorSig",
205
+ type: "signature",
206
+ encoding: "schnorr-64",
207
+ injected: true
208
+ }
209
+ ],
210
+ asm: [
211
+ "<taker>",
212
+ "OP_CHECKSIGVERIFY",
213
+ "<SERVER_KEY>",
214
+ "OP_CHECKSIGVERIFY",
215
+ "<EMULATOR_KEY:fulfill>",
216
+ "OP_CHECKSIG"
217
+ ]
218
+ }
219
+ ]
220
+ },
221
+ {
222
+ name: "cancel",
223
+ leaves: [
224
+ {
225
+ name: "cancel",
226
+ witness: [
227
+ {
228
+ name: "userSig",
229
+ type: "signature",
230
+ encoding: "schnorr-64"
231
+ },
232
+ {
233
+ name: "serverSig",
234
+ type: "signature",
235
+ encoding: "schnorr-64",
236
+ injected: true
237
+ }
238
+ ],
239
+ asm: [
240
+ "<user>",
241
+ "OP_CHECKSIGVERIFY",
242
+ "<SERVER_KEY>",
243
+ "OP_CHECKSIG"
244
+ ]
245
+ }
246
+ ]
247
+ }
248
+ ],
249
+ source: {
250
+ entry: "asset-swap-v4.ark",
251
+ files: {
252
+ "asset-swap-v4.ark": "pragma arkade ^0.1.0;\n\n// The output at this input's index pays `receiver` at least `amount` of the asset,\n// and only `taker` can make that spend.\ncontract AssetSwap(\n pubkey receiver,\n int amount,\n bytes32 assetTxid,\n int assetGroupIndex,\n pubkey user,\n pubkey taker\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 // Replaces the synthesized collaborative leaf: the quoting solver is a third\n // conjunct, so an observer who reads the deposit cannot complete it.\n function fulfill(signature takerSig, signature serverSig, signature emulatorSig) tapscript {\n require(checkMultisig([taker, server, emulator], [takerSig, serverSig, emulatorSig], 3));\n }\n\n function cancel(signature userSig, signature serverSig) tapscript {\n require(checkMultisig([user, server], [userSig, serverSig], 2));\n }\n}\n"
253
+ }
254
+ },
255
+ compiler: {
256
+ name: "arkadec",
257
+ version: "0.1.0-rc.1",
258
+ options: {
259
+ optimize: true
260
+ }
261
+ },
262
+ updatedAt: "2026-11-19T20:57:50.605738400+00:00",
263
+ fingerprint: "sha256:d830de32cd1d286a0a1fac698ff114a6c8ef5338a69f9f813717d1038ab2fb4a"
264
+ };
265
+
135
266
  // src/contracts/sats-swap.json
136
267
  var sats_swap_default = {
137
268
  formatVersion: 1,
@@ -240,6 +371,125 @@ var sats_swap_default = {
240
371
  fingerprint: "sha256:d7a60ab2178ede40a2e4867816689ea022641531cc662b562bebd7caa1ef96c4"
241
372
  };
242
373
 
374
+ // src/contracts/sats-swap-v4.json
375
+ var sats_swap_v4_default = {
376
+ formatVersion: 1,
377
+ contractName: "SatsSwap",
378
+ constructorInputs: [
379
+ {
380
+ name: "receiver",
381
+ type: "pubkey"
382
+ },
383
+ {
384
+ name: "amount",
385
+ type: "int"
386
+ },
387
+ {
388
+ name: "user",
389
+ type: "pubkey"
390
+ },
391
+ {
392
+ name: "taker",
393
+ type: "pubkey"
394
+ }
395
+ ],
396
+ functions: [
397
+ {
398
+ name: "fulfill",
399
+ arkade: {
400
+ inputs: [],
401
+ asm: [
402
+ "OP_PUSHCURRENTINPUTINDEX",
403
+ "OP_DUP",
404
+ "OP_INSPECTOUTPUTVALUE",
405
+ "<amount>",
406
+ "OP_GREATERTHANOREQUAL",
407
+ "OP_VERIFY",
408
+ "OP_INSPECTOUTPUTSCRIPTPUBKEY",
409
+ "1",
410
+ "OP_NUMEQUALVERIFY",
411
+ "<receiver>",
412
+ "OP_EQUAL"
413
+ ]
414
+ },
415
+ leaves: [
416
+ {
417
+ name: "fulfill",
418
+ witness: [
419
+ {
420
+ name: "takerSig",
421
+ type: "signature",
422
+ encoding: "schnorr-64"
423
+ },
424
+ {
425
+ name: "serverSig",
426
+ type: "signature",
427
+ encoding: "schnorr-64",
428
+ injected: true
429
+ },
430
+ {
431
+ name: "emulatorSig",
432
+ type: "signature",
433
+ encoding: "schnorr-64",
434
+ injected: true
435
+ }
436
+ ],
437
+ asm: [
438
+ "<taker>",
439
+ "OP_CHECKSIGVERIFY",
440
+ "<SERVER_KEY>",
441
+ "OP_CHECKSIGVERIFY",
442
+ "<EMULATOR_KEY:fulfill>",
443
+ "OP_CHECKSIG"
444
+ ]
445
+ }
446
+ ]
447
+ },
448
+ {
449
+ name: "cancel",
450
+ leaves: [
451
+ {
452
+ name: "cancel",
453
+ witness: [
454
+ {
455
+ name: "userSig",
456
+ type: "signature",
457
+ encoding: "schnorr-64"
458
+ },
459
+ {
460
+ name: "serverSig",
461
+ type: "signature",
462
+ encoding: "schnorr-64",
463
+ injected: true
464
+ }
465
+ ],
466
+ asm: [
467
+ "<user>",
468
+ "OP_CHECKSIGVERIFY",
469
+ "<SERVER_KEY>",
470
+ "OP_CHECKSIG"
471
+ ]
472
+ }
473
+ ]
474
+ }
475
+ ],
476
+ source: {
477
+ entry: "sats-swap-v4.ark",
478
+ files: {
479
+ "sats-swap-v4.ark": "pragma arkade ^0.1.0;\n\n// The output at this input's index pays `receiver` at least `amount` sats,\n// and only `taker` can make that spend.\ncontract SatsSwap(\n pubkey receiver,\n int amount,\n pubkey user,\n pubkey taker\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 fulfill(signature takerSig, signature serverSig, signature emulatorSig) tapscript {\n require(checkMultisig([taker, server, emulator], [takerSig, serverSig, emulatorSig], 3));\n }\n\n function cancel(signature userSig, signature serverSig) tapscript {\n require(checkMultisig([user, server], [userSig, serverSig], 2));\n }\n}\n"
480
+ }
481
+ },
482
+ compiler: {
483
+ name: "arkadec",
484
+ version: "0.1.0-rc.1",
485
+ options: {
486
+ optimize: true
487
+ }
488
+ },
489
+ updatedAt: "2026-11-19T20:57:50.848496600+00:00",
490
+ fingerprint: "sha256:6ca61bf7ed8d8f65af3141da4e95fcc896863650127dd20949a84e72258053f4"
491
+ };
492
+
243
493
  // src/legacy/swap-want-asset.program.v1.json
244
494
  var swap_want_asset_program_v1_default = {
245
495
  version: 1,
@@ -324,38 +574,60 @@ var legacySwapPrograms = {
324
574
  wantAsset: arkade.parseArtifact(swap_want_asset_program_v1_default),
325
575
  wantBtc: arkade.parseArtifact(swap_want_btc_program_v1_default)
326
576
  };
577
+ var directedSwapPrograms = {
578
+ wantAsset: arkade.programFromArtifact(asset_swap_v4_default),
579
+ wantBtc: arkade.programFromArtifact(sats_swap_v4_default)
580
+ };
327
581
  var sharedArgs = ({ user, server, exitDelay }) => ({
328
582
  user,
329
583
  server,
330
584
  ...exitDelay && { exitDelay }
331
585
  });
586
+ var refuseTaker = (version, { taker }) => {
587
+ if (taker !== void 0) throw new Error(`${version} binds no taker key`);
588
+ };
589
+ var v3Args = (inputs) => ({
590
+ receiver: inputs.receiver,
591
+ amount: inputs.amount,
592
+ ...inputs.asset && {
593
+ assetTxid: inputs.asset.txid,
594
+ assetGroupIndex: inputs.asset.groupIndex
595
+ },
596
+ ...sharedArgs(inputs)
597
+ });
332
598
  var ASSET_SWAP_CONTRACTS = {
333
599
  [ASSET_SWAP_V2]: {
334
600
  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
- })
601
+ args: (inputs) => {
602
+ refuseTaker(ASSET_SWAP_V2, inputs);
603
+ return {
604
+ makerWP: inputs.receiver,
605
+ wantAmount: inputs.amount,
606
+ ...inputs.asset && {
607
+ wantAssetTxid: inputs.asset.txid,
608
+ wantAssetGroupIndex: inputs.asset.groupIndex
609
+ },
610
+ ...sharedArgs(inputs)
611
+ };
612
+ }
344
613
  },
345
614
  [ASSET_SWAP_V3]: {
346
615
  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
- })
616
+ args: (inputs) => {
617
+ refuseTaker(ASSET_SWAP_V3, inputs);
618
+ return v3Args(inputs);
619
+ }
620
+ },
621
+ [ASSET_SWAP_V4]: {
622
+ programs: directedSwapPrograms,
623
+ args: (inputs) => {
624
+ if (inputs.taker === void 0) throw new Error(`${ASSET_SWAP_V4} needs a taker key`);
625
+ return { ...v3Args(inputs), taker: inputs.taker };
626
+ }
356
627
  }
357
628
  };
358
- var ASSET_SWAP_CONTRACT_VERSIONS = [ASSET_SWAP_V3, ASSET_SWAP_V2];
629
+ var ASSET_SWAP_CONTRACT_VERSIONS = [ASSET_SWAP_V4];
630
+ var ASSET_SWAP_DERIVABLE_VERSIONS = [ASSET_SWAP_V4, ASSET_SWAP_V3, ASSET_SWAP_V2];
359
631
  var assetSwapContractVersion = (id) => {
360
632
  if (id === void 0) return ASSET_SWAP_BASELINE;
361
633
  if (Object.hasOwn(ASSET_SWAP_CONTRACTS, id)) return id;
@@ -370,9 +642,9 @@ var BTC_ASSET_ID = "btc";
370
642
  var byNewest = (a, b) => b.createdAt - a.createdAt;
371
643
  var getAssetSwapsOrThrow = async (repository) => {
372
644
  return (await collectAssetSwaps(repository)).filter(
373
- (s) => s && typeof s.id === "string" && // offer swaps carry the TLV; onchain-corridor swaps carry
374
- // the payment hash instead — either marks a valid record
375
- (typeof s.offerHex === "string" || typeof s.paymentHash === "string")
645
+ (s) => s && typeof s.id === "string" && // offer swaps carry the TLV or the binding; onchain-corridor swaps carry
646
+ // the payment hash instead — any of them marks a valid record
647
+ (typeof s.offerHex === "string" || s.binding !== void 0 || typeof s.paymentHash === "string")
376
648
  ).sort(byNewest);
377
649
  };
378
650
  var getAssetSwaps = async (repository) => {
@@ -705,28 +977,28 @@ function withExitClosure(program, exit) {
705
977
  }
706
978
  };
707
979
  }
708
- function swapProgramBinding(offer, operatorPubkey, contractVersion = ASSET_SWAP_BASELINE) {
980
+ function swapProgramBinding(offer, operatorPubkey, contractVersion) {
709
981
  if (offer.makerPkScript.length !== FIELDS.makerPkScript.width) {
710
982
  throw new Error("makerPkScript is not a 34-byte taproot scriptPubKey");
711
983
  }
984
+ const directed = offer.taker !== void 0;
985
+ const version = contractVersion ?? (directed ? ASSET_SWAP_V4 : ASSET_SWAP_BASELINE);
712
986
  const expected = offer.swapPkScript;
713
- if (!expected?.length) return bindSwapProgram(offer, operatorPubkey, contractVersion);
987
+ if (!expected?.length) return bindSwapProgram(offer, operatorPubkey, version);
714
988
  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);
989
+ const order = directed ? [ASSET_SWAP_V4] : [ASSET_SWAP_BASELINE, ASSET_SWAP_V3];
990
+ for (const candidate of order) {
991
+ const binding = bindSwapProgram(offer, operatorPubkey, candidate);
721
992
  if (pkOf(binding) === funded) return binding;
722
993
  }
723
- return bindSwapProgram(offer, operatorPubkey, contractVersion);
994
+ return bindSwapProgram(offer, operatorPubkey, directed ? ASSET_SWAP_V4 : version);
724
995
  }
725
996
  function bindSwapProgram(offer, operatorPubkey, contractVersion) {
726
997
  const { programs, args } = ASSET_SWAP_CONTRACTS[contractVersion];
727
998
  const user = offer.makerPublicKey;
728
999
  const assetId = offer.wantAsset;
729
1000
  return {
1001
+ version: contractVersion,
730
1002
  program: withExitClosure(assetId ? programs.wantAsset : programs.wantBtc, offer.exitDelay),
731
1003
  args: args({
732
1004
  receiver: offer.makerPkScript.subarray(2),
@@ -737,7 +1009,8 @@ function bindSwapProgram(offer, operatorPubkey, contractVersion) {
737
1009
  },
738
1010
  user,
739
1011
  server: operatorPubkey,
740
- ...offer.exitDelay && { exitDelay: offer.exitDelay.value }
1012
+ ...offer.exitDelay && { exitDelay: offer.exitDelay.value },
1013
+ ...offer.taker && { taker: offer.taker }
741
1014
  }),
742
1015
  keys: {
743
1016
  serverKey: operatorPubkey,
@@ -761,11 +1034,94 @@ function pkOf(binding) {
761
1034
  function offerContract(offer, operatorPubkey, contractVersion) {
762
1035
  return compileSwapBinding(swapProgramBinding(offer, operatorPubkey, contractVersion));
763
1036
  }
1037
+ var isOfferBinding = (offer) => "version" in offer;
1038
+ var termsOf = (binding) => ({
1039
+ wantAmount: binding.amount,
1040
+ ...binding.wantAsset && { wantAsset: binding.wantAsset },
1041
+ ...binding.offerAsset && { offerAsset: binding.offerAsset },
1042
+ makerPkScript: binding.receiverPkScript,
1043
+ makerPublicKey: binding.userPublicKey,
1044
+ emulatorPubkey: binding.emulatorPublicKey,
1045
+ ...binding.exitDelay && { exitDelay: binding.exitDelay },
1046
+ ...binding.taker && { taker: binding.taker }
1047
+ });
1048
+ var offerOf = (offer, swapPkScript) => isOfferBinding(offer) ? { ...termsOf(offer), swapPkScript } : offer;
1049
+ function bindingProgram(binding) {
1050
+ if (Boolean(binding.wantAsset) === Boolean(binding.offerAsset)) {
1051
+ throw new Error("an offer binding names exactly one of wantAsset or offerAsset");
1052
+ }
1053
+ return swapProgramBinding(termsOf(binding), binding.serverPublicKey, binding.version);
1054
+ }
1055
+ function bindingFromOffer(offer, operatorPubkey) {
1056
+ const program = swapProgramBinding(offer, operatorPubkey);
1057
+ if (pkOf(program) !== hex2.encode(offer.swapPkScript)) {
1058
+ throw new OfferCovenantMismatchError(hex2.encode(offer.swapPkScript));
1059
+ }
1060
+ return {
1061
+ version: program.version,
1062
+ receiverPkScript: offer.makerPkScript,
1063
+ amount: offer.wantAmount,
1064
+ ...offer.wantAsset && { wantAsset: offer.wantAsset },
1065
+ ...offer.offerAsset && { offerAsset: offer.offerAsset },
1066
+ userPublicKey: offer.makerPublicKey,
1067
+ serverPublicKey: operatorPubkey,
1068
+ emulatorPublicKey: offer.emulatorPubkey,
1069
+ ...offer.exitDelay && { exitDelay: offer.exitDelay },
1070
+ ...offer.taker && { taker: offer.taker }
1071
+ };
1072
+ }
1073
+ function offerFromBinding(binding) {
1074
+ return offerOf(binding, compileSwapBinding(bindingProgram(binding)).pkScript);
1075
+ }
1076
+ var recordOfferBinding = (binding) => ({
1077
+ version: binding.version,
1078
+ receiverPkScript: hex2.encode(binding.receiverPkScript),
1079
+ amount: binding.amount.toString(),
1080
+ ...binding.wantAsset && { wantAsset: binding.wantAsset.toString() },
1081
+ ...binding.offerAsset && { offerAsset: binding.offerAsset.toString() },
1082
+ userPublicKey: hex2.encode(binding.userPublicKey),
1083
+ serverPublicKey: hex2.encode(binding.serverPublicKey),
1084
+ emulatorPublicKey: hex2.encode(binding.emulatorPublicKey),
1085
+ ...binding.exitDelay && {
1086
+ exitDelay: { type: binding.exitDelay.type, value: binding.exitDelay.value.toString() }
1087
+ },
1088
+ ...binding.taker && { taker: hex2.encode(binding.taker) }
1089
+ });
1090
+ var decimal = (value, name) => {
1091
+ if (!/^\d+$/.test(value)) throw new Error(`recorded binding ${name} is not a decimal amount`);
1092
+ return BigInt(value);
1093
+ };
1094
+ var offerBindingOf = (recorded) => {
1095
+ if (typeof recorded.version !== "string" || !Object.hasOwn(ASSET_SWAP_CONTRACTS, recorded.version)) {
1096
+ throw new Error(`recorded binding names no known contract version (${recorded.version})`);
1097
+ }
1098
+ const { exitDelay, wantAsset, offerAsset, taker } = recorded;
1099
+ return {
1100
+ version: recorded.version,
1101
+ receiverPkScript: hex2.decode(recorded.receiverPkScript),
1102
+ amount: decimal(recorded.amount, "amount"),
1103
+ ...wantAsset !== void 0 && { wantAsset: asset.AssetId.fromString(wantAsset) },
1104
+ ...offerAsset !== void 0 && { offerAsset: asset.AssetId.fromString(offerAsset) },
1105
+ userPublicKey: hex2.decode(recorded.userPublicKey),
1106
+ serverPublicKey: hex2.decode(recorded.serverPublicKey),
1107
+ emulatorPublicKey: hex2.decode(recorded.emulatorPublicKey),
1108
+ ...exitDelay && {
1109
+ exitDelay: { type: exitDelay.type, value: decimal(exitDelay.value, "exitDelay") }
1110
+ },
1111
+ ...taker !== void 0 && { taker: hex2.decode(taker) }
1112
+ };
1113
+ };
1114
+ var storedOffer = (swap) => {
1115
+ if (swap.binding !== void 0) return offerBindingOf(swap.binding);
1116
+ if (typeof swap.offerHex === "string") return decodeOffer(hex2.decode(swap.offerHex));
1117
+ throw new Error("this offer swap carries neither a binding nor an offerHex to rebuild from");
1118
+ };
764
1119
  var OFFER_PACKET_TYPE = 3;
765
1120
  var FIELDS = {
766
1121
  swapPkScript: { tag: 1, width: 34 },
767
1122
  wantAmount: { tag: 2, width: 8 },
768
1123
  wantAsset: { tag: 3, width: void 0 },
1124
+ taker: { tag: 4, width: 32 },
769
1125
  makerPkScript: { tag: 5, width: 34 },
770
1126
  makerPublicKey: { tag: 7, width: 32 },
771
1127
  emulatorPubkey: { tag: 8, width: 32 },
@@ -808,6 +1164,9 @@ function encodeOffer(offer) {
808
1164
  throw new Error(`${name} must be ${FIELDS[name].width} bytes`);
809
1165
  }
810
1166
  }
1167
+ if (offer.taker !== void 0 && offer.taker.length !== FIELDS.taker.width) {
1168
+ throw new Error(`taker must be ${FIELDS.taker.width} bytes`);
1169
+ }
811
1170
  const ratioNum = setRatio("ratioNum", offer.ratioNum);
812
1171
  const ratioDen = setRatio("ratioDen", offer.ratioDen);
813
1172
  if (ratioNum === void 0 !== (ratioDen === void 0)) {
@@ -823,9 +1182,10 @@ function encodeOffer(offer) {
823
1182
  if (offer.offerAsset) recs.push(tlv(FIELDS.offerAsset.tag, offer.offerAsset.serialize()));
824
1183
  recs.push(
825
1184
  tlv(FIELDS.makerPkScript.tag, offer.makerPkScript),
826
- tlv(FIELDS.makerPublicKey.tag, offer.makerPublicKey),
827
- tlv(FIELDS.emulatorPubkey.tag, offer.emulatorPubkey)
1185
+ tlv(FIELDS.makerPublicKey.tag, offer.makerPublicKey)
828
1186
  );
1187
+ if (offer.taker) recs.push(tlv(FIELDS.taker.tag, offer.taker));
1188
+ recs.push(tlv(FIELDS.emulatorPubkey.tag, offer.emulatorPubkey));
829
1189
  if (offer.exitDelay) recs.push(tlv(FIELDS.exitTimelock.tag, encodeExitDelay(offer.exitDelay)));
830
1190
  return concatBytes(...recs);
831
1191
  }
@@ -903,7 +1263,8 @@ function decodeOffer(data) {
903
1263
  emulatorPubkey: need("emulatorPubkey"),
904
1264
  ...ratioNum !== void 0 && { ratioNum },
905
1265
  ...ratioDen !== void 0 && { ratioDen },
906
- ...fields.exitTimelock && { exitDelay: decodeExitDelay(fields.exitTimelock) }
1266
+ ...fields.exitTimelock && { exitDelay: decodeExitDelay(fields.exitTimelock) },
1267
+ ...fields.taker && { taker: fields.taker }
907
1268
  };
908
1269
  }
909
1270
  function decodeExitDelay(value) {
@@ -931,10 +1292,7 @@ var derivingClient = (wallet, info, contractManager) => arkade2.Arkade.connect({
931
1292
  contractManager
932
1293
  });
933
1294
  function offerContractParams(client, binding, operatorPubkey, expectedPkScript) {
934
- const { program, args, keys } = swapProgramBinding(
935
- { ...binding, swapPkScript: expectedPkScript },
936
- operatorPubkey
937
- );
1295
+ const { program, args, keys } = "version" in binding ? bindingProgram(binding) : swapProgramBinding({ ...binding, swapPkScript: expectedPkScript }, operatorPubkey);
938
1296
  const contract = new arkade2.ArkadeContract(client, program, args, keys);
939
1297
  if (hex2.encode(contract.pkScript) !== hex2.encode(expectedPkScript)) {
940
1298
  throw new Error("derived covenant does not match the offer's swapPkScript");
@@ -966,7 +1324,7 @@ async function restoreOfferCoverage(wallet, swaps) {
966
1324
  if (seen.has(swap.swapPkScript)) continue;
967
1325
  seen.add(swap.swapPkScript);
968
1326
  try {
969
- const { swapPkScript: _, ...binding } = decodeOffer(hex2.decode(swap.offerHex));
1327
+ const binding = storedOffer(swap);
970
1328
  const stored = fundedKey.get(swap.swapPkScript);
971
1329
  covenants.push({
972
1330
  script: swap.swapPkScript,
@@ -1075,7 +1433,8 @@ async function deriveOffer(wallet, params) {
1075
1433
  makerPublicKey: maker.publicKey,
1076
1434
  emulatorPubkey: emuKey,
1077
1435
  // checked before registration below. @see assertExitDelay
1078
- exitDelay: params.noExit ? void 0 : params.exitDelay ? assertExitDelay(params.exitDelay) : serverExitDelay(info.unilateralExitDelay)
1436
+ exitDelay: params.noExit ? void 0 : params.exitDelay ? assertExitDelay(params.exitDelay) : serverExitDelay(info.unilateralExitDelay),
1437
+ ...params.taker && { taker: params.taker }
1079
1438
  });
1080
1439
  const script = offerContract(binding, operatorPubKey, params.contractVersion);
1081
1440
  const offer = { ...binding, swapPkScript: script.pkScript };
@@ -1088,6 +1447,7 @@ async function deriveOffer(wallet, params) {
1088
1447
  address: script.address(network.hrp, operatorPubKey).encode(),
1089
1448
  swapPkScript: script.pkScript,
1090
1449
  binding,
1450
+ offerBinding: Object.freeze(bindingFromOffer(offer, operatorPubKey)),
1091
1451
  info,
1092
1452
  operatorPubkey: operatorPubKey
1093
1453
  });
@@ -1149,7 +1509,7 @@ var OfferCovenantMismatchError = class extends Error {
1149
1509
  };
1150
1510
  async function prepareOfferCancel(wallet, offerHex, opts) {
1151
1511
  const { fundingTxid, fundingOutpoint, swapAddress } = opts;
1152
- const offer = decodeOffer(hex2.decode(offerHex));
1512
+ const decoded = typeof offerHex === "string" ? decodeOffer(hex2.decode(offerHex)) : isOfferBinding(offerHex) ? void 0 : offerHex;
1153
1513
  const [contractManager, info, reader, broadcaster] = await Promise.all([
1154
1514
  wallet.getContractManager(),
1155
1515
  wallet.getArkadeInfo({ requireLive: true }),
@@ -1166,18 +1526,20 @@ async function prepareOfferCancel(wallet, offerHex, opts) {
1166
1526
  // no `network`: the row lookup is by script and the payout comes from wallet.getAddress()
1167
1527
  oneShotSubmit: wallet.oneShotSubmit
1168
1528
  });
1169
- const operatorPubkey = swapAddress ? ArkAddress.decode(swapAddress).serverPubKey : client.serverKey;
1529
+ const pinned = swapAddress ? ArkAddress.decode(swapAddress) : void 0;
1530
+ const funded = decoded ? decoded.swapPkScript : pinned?.pkScript;
1170
1531
  let binding;
1171
1532
  let rebuilt;
1172
1533
  try {
1173
- binding = swapProgramBinding(offer, operatorPubkey);
1534
+ binding = decoded ? swapProgramBinding(decoded, pinned?.serverPubKey ?? client.serverKey) : bindingProgram(offerHex);
1174
1535
  rebuilt = compileSwapBinding(binding);
1175
1536
  } catch (cause) {
1176
- throw new OfferCovenantMismatchError(hex2.encode(offer.swapPkScript), { cause });
1537
+ throw new OfferCovenantMismatchError(hex2.encode(funded ?? new Uint8Array()), { cause });
1177
1538
  }
1178
- if (hex2.encode(rebuilt.pkScript) !== hex2.encode(offer.swapPkScript)) {
1179
- throw new OfferCovenantMismatchError(hex2.encode(offer.swapPkScript));
1539
+ if (funded && hex2.encode(rebuilt.pkScript) !== hex2.encode(funded)) {
1540
+ throw new OfferCovenantMismatchError(hex2.encode(funded));
1180
1541
  }
1542
+ const offer = decoded ?? offerOf(offerHex, rebuilt.pkScript);
1181
1543
  const contract = new arkade2.ArkadeContract(client, binding.program, binding.args, binding.keys);
1182
1544
  const [vtxos, makerAddress] = await Promise.all([contract.getUtxos(), wallet.getAddress()]);
1183
1545
  const vtxo = resolveDeposit(vtxos, { fundingTxid, fundingOutpoint });
@@ -1208,8 +1570,8 @@ async function fillOffer(wallet, arkServerUrl, offerHex, opts) {
1208
1570
  emulator,
1209
1571
  emulatorPubkey
1210
1572
  } = opts;
1211
- const offer = decodeOffer(hex2.decode(offerHex));
1212
- const contract = await connectFillContract(wallet, arkServerUrl, offer, {
1573
+ const target = typeof offerHex === "string" ? decodeOffer(hex2.decode(offerHex)) : offerHex;
1574
+ const contract = await connectFillContract(wallet, arkServerUrl, target, {
1213
1575
  swapAddress,
1214
1576
  emulator,
1215
1577
  emulatorPubkey
@@ -1218,7 +1580,7 @@ async function fillOffer(wallet, arkServerUrl, offerHex, opts) {
1218
1580
  const vtxo = resolveDeposit(vtxos, { fundingTxid, fundingOutpoint });
1219
1581
  const fill = contract.functions.fulfill();
1220
1582
  assembleOfferFill(fill, {
1221
- offer,
1583
+ offer: offerOf(target, contract.pkScript),
1222
1584
  vtxo,
1223
1585
  solverFund: fund,
1224
1586
  solverPayout: payoutScript ?? ArkAddress.decode(takerAddress).pkScript,
@@ -1240,10 +1602,11 @@ async function connectFillContract(wallet, arkServerUrl, offer, opts = {}) {
1240
1602
  } : {},
1241
1603
  ...emulatorPubkey ? { emulatorPubkey } : {}
1242
1604
  });
1243
- const serverKey = swapAddress ? ArkAddress.decode(swapAddress).serverPubKey : client.serverKey;
1244
- const binding = swapProgramBinding(offer, serverKey);
1605
+ const pinned = swapAddress ? ArkAddress.decode(swapAddress) : void 0;
1606
+ const binding = isOfferBinding(offer) ? bindingProgram(offer) : swapProgramBinding(offer, pinned?.serverPubKey ?? client.serverKey);
1607
+ const funded = isOfferBinding(offer) ? pinned?.pkScript : offer.swapPkScript;
1245
1608
  const rebuilt = compileSwapBinding(binding);
1246
- if (hex2.encode(rebuilt.pkScript) !== hex2.encode(offer.swapPkScript)) {
1609
+ if (funded && hex2.encode(rebuilt.pkScript) !== hex2.encode(funded)) {
1247
1610
  throw new Error(
1248
1611
  "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"
1249
1612
  );
@@ -1682,9 +2045,9 @@ function onchainHtlcScript(params, network) {
1682
2045
  if (params.claimKey.length !== 32 || params.refundKey.length !== 32) {
1683
2046
  throw new Error("claimKey and refundKey must be 32-byte x-only keys");
1684
2047
  }
1685
- if (!Number.isInteger(params.refundLocktime) || params.refundLocktime <= 0) {
2048
+ if (!Number.isInteger(params.refundLocktime) || params.refundLocktime <= 0 || params.refundLocktime > 4294967295) {
1686
2049
  throw new Error(
1687
- `refundLocktime must be a positive unix timestamp, got ${params.refundLocktime}`
2050
+ `refundLocktime must be a positive uint32 unix timestamp, got ${params.refundLocktime}`
1688
2051
  );
1689
2052
  }
1690
2053
  if (params.refundLocktime < LOCKTIME_THRESHOLD) {
@@ -1859,15 +2222,20 @@ async function claimOnchainFill(chain, input) {
1859
2222
  }
1860
2223
  async function classifyOnchainHtlc(chain, input) {
1861
2224
  const utxos = await chain.getScriptUtxos(input.htlc.pkScript);
1862
- const best = utxos.sort((a, b) => b.amount > a.amount ? 1 : -1)[0];
1863
- if (!best) {
1864
- if (!input.funding) return { phase: "unfunded" };
1865
- const spend = await chain.getSpendingTx(
1866
- input.funding.txid,
1867
- input.funding.vout,
1868
- input.htlc.pkScript
1869
- );
1870
- if (!spend) return { phase: "unfunded" };
2225
+ const deep = (u) => u.confirmations >= input.minConfirmations;
2226
+ const best = utxos.sort(
2227
+ (a, b) => deep(a) !== deep(b) ? deep(a) ? -1 : 1 : b.amount > a.amount ? 1 : -1
2228
+ )[0];
2229
+ const { funding } = input;
2230
+ let spend = null;
2231
+ if (funding && !utxos.some((u) => u.txid === funding.txid && u.vout === funding.vout)) {
2232
+ try {
2233
+ spend = await chain.getSpendingTx(funding.txid, funding.vout, input.htlc.pkScript);
2234
+ } catch (error) {
2235
+ if (!best) throw error;
2236
+ }
2237
+ }
2238
+ if (spend) {
1871
2239
  const preimage = extractPreimage(spend.txHex, input.htlc.paymentHash);
1872
2240
  const txid = btc.Transaction.fromRaw(hex4.decode(spend.txHex), {
1873
2241
  allowUnknownInputs: true,
@@ -1875,6 +2243,7 @@ async function classifyOnchainHtlc(chain, input) {
1875
2243
  }).id;
1876
2244
  return preimage ? { phase: "claimed", txid, preimage } : { phase: "swept", txid };
1877
2245
  }
2246
+ if (!best) return { phase: "unfunded" };
1878
2247
  if (best.confirmations < input.minConfirmations)
1879
2248
  return { phase: "awaiting_confirmations", utxo: best };
1880
2249
  const mtp = await chain.getMtp();
@@ -2367,7 +2736,7 @@ var requestVersionedQuote = async (transport, request, advertised) => {
2367
2736
  const version = selected ?? ASSET_SWAP_BASELINE;
2368
2737
  if (!advertised.includes(version)) {
2369
2738
  throw new Error(
2370
- `solver chose contract version ${version}, not one of ${advertised.join(", ")}`
2739
+ selected === void 0 ? `solver negotiated no contract version; advertised ${advertised.join(", ")}` : `solver chose contract version ${version}, not one of ${advertised.join(", ")}`
2371
2740
  );
2372
2741
  }
2373
2742
  return { quote, rfqId: sent.rfq_id, contractVersion: assetSwapContractVersion(version) };
@@ -2723,7 +3092,6 @@ async function requestArkadeSwap(wallet, transport, params) {
2723
3092
  params.offerAsset ? arkadeAssetLeg(params.offerAsset) : ARKADE_BTC,
2724
3093
  params.wantAsset ? arkadeAssetLeg(params.wantAsset) : ARKADE_BTC
2725
3094
  );
2726
- const contractVersions = [...params.contractVersions ?? ASSET_SWAP_CONTRACT_VERSIONS];
2727
3095
  const { quote, rfqId, contractVersion } = await requestVersionedQuote(
2728
3096
  transport,
2729
3097
  arkadeSwapRequest({
@@ -2734,13 +3102,17 @@ async function requestArkadeSwap(wallet, transport, params) {
2734
3102
  amountSide,
2735
3103
  makerPkScript,
2736
3104
  makerPublicKey,
2737
- contractVersions
3105
+ contractVersions: ASSET_SWAP_CONTRACT_VERSIONS
2738
3106
  }),
2739
- contractVersions
3107
+ ASSET_SWAP_CONTRACT_VERSIONS
2740
3108
  );
2741
3109
  if (quote.pair !== pair) {
2742
3110
  throw new Error(`solver quoted ${JSON.stringify(quote.pair)}, not the requested ${pair}`);
2743
3111
  }
3112
+ const taker = contractVersion === ASSET_SWAP_V4 ? (await import("./verify-ENO36VPK.js")).verifyTaker({
3113
+ attested: transport.attestedResponder,
3114
+ solverPubkey: quote.solver_pubkey
3115
+ }) : void 0;
2744
3116
  assertArkadeFundable({ quote, ...params.now !== void 0 ? { now: params.now } : {} });
2745
3117
  const terms = offerTermsFromQuote(quote, {
2746
3118
  ...params.offerAsset !== void 0 ? { offerAsset: params.offerAsset } : {},
@@ -2778,7 +3150,8 @@ async function requestArkadeSwap(wallet, transport, params) {
2778
3150
  ...terms.wantAsset !== void 0 ? { wantAsset: terms.wantAsset } : {},
2779
3151
  ...terms.offerAsset !== void 0 ? { offerAsset: terms.offerAsset } : {},
2780
3152
  ...params.emulatorPubkey !== void 0 ? { emulatorPubkey: params.emulatorPubkey } : {},
2781
- contractVersion
3153
+ contractVersion,
3154
+ ...taker === void 0 ? {} : { taker }
2782
3155
  });
2783
3156
  verifyOfferAddress(quote, offer);
2784
3157
  const fundAmount = BigInt(quote.from_amount);
@@ -3025,6 +3398,9 @@ var assertReceivable = (input) => {
3025
3398
  if (input.now >= input.payDeadline) {
3026
3399
  throw gateError("quote_expired", "quote or invoice already expired \u2014 request a fresh one");
3027
3400
  }
3401
+ if (quoteAmountSats(input.quote.from_amount, "from_amount") <= 0n || quoteAmountSats(input.quote.to_amount, "to_amount") <= 0n) {
3402
+ throw gateError("non_positive_amount", "quote carries a non-positive amount");
3403
+ }
3028
3404
  if (input.quote.refund_locktime === void 0) {
3029
3405
  throw gateError("missing_refund_locktime", "receive quote carries no refund_locktime");
3030
3406
  }
@@ -3282,6 +3658,7 @@ export {
3282
3658
  swapPrograms,
3283
3659
  legacySwapPrograms,
3284
3660
  ASSET_SWAP_CONTRACT_VERSIONS,
3661
+ ASSET_SWAP_DERIVABLE_VERSIONS,
3285
3662
  assetSwapContractVersion,
3286
3663
  RETIRABLE,
3287
3664
  LOCKUP_RETIRABLE,
@@ -3304,6 +3681,12 @@ export {
3304
3681
  rfqClaimDestinationOf,
3305
3682
  withExitClosure,
3306
3683
  offerContract,
3684
+ offerOf,
3685
+ bindingProgram,
3686
+ bindingFromOffer,
3687
+ offerFromBinding,
3688
+ recordOfferBinding,
3689
+ storedOffer,
3307
3690
  OFFER_PACKET_TYPE,
3308
3691
  encodeOffer,
3309
3692
  decodeOffer,