@arkade-os/swap 0.0.11 → 0.0.13

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.
@@ -17,6 +17,7 @@ var L1_NETWORKS = {
17
17
  testnet: btc.TEST_NETWORK,
18
18
  regtest: { ...btc.TEST_NETWORK, bech32: "bcrt" }
19
19
  };
20
+ var l1ScriptForAddress = (address, network) => btc.OutScript.encode(btc.Address(L1_NETWORKS[network]).decode(address));
20
21
  function onchainHtlcScript(params, network) {
21
22
  if (params.claimKey.length !== 32 || params.refundKey.length !== 32) {
22
23
  throw new Error("claimKey and refundKey must be 32-byte x-only keys");
@@ -199,7 +200,11 @@ async function classifyOnchainHtlc(chain, input) {
199
200
  const best = utxos.sort((a, b) => b.amount > a.amount ? 1 : -1)[0];
200
201
  if (!best) {
201
202
  if (!input.funding) return { phase: "unfunded" };
202
- const spend = await chain.getSpendingTx(input.funding.txid, input.funding.vout);
203
+ const spend = await chain.getSpendingTx(
204
+ input.funding.txid,
205
+ input.funding.vout,
206
+ input.htlc.pkScript
207
+ );
203
208
  if (!spend) return { phase: "unfunded" };
204
209
  const preimage = extractPreimage(spend.txHex, input.htlc.paymentHash);
205
210
  const txid = btc.Transaction.fromRaw(hex.decode(spend.txHex), {
@@ -451,6 +456,43 @@ var assertFundable = (input) => {
451
456
  if (input.quote.refund_locktime !== void 0 && input.quote.refund_locktime - input.now < MIN_HEADROOM_SECONDS) {
452
457
  fail("insufficient_headroom", "refund deadline headroom below 90 minutes");
453
458
  }
459
+ if (input.maxFee) {
460
+ const { bps, sats, referenceRate } = input.maxFee;
461
+ if (bps === void 0 && sats === void 0) {
462
+ fail("max_fee_unbounded", "maxFee names neither bps nor sats");
463
+ }
464
+ if (bps !== void 0 && (!Number.isInteger(bps) || bps < 0 || bps > 1e4)) {
465
+ fail("max_fee_out_of_range", `maxFee.bps must be an integer in 0..10000, got ${bps}`);
466
+ }
467
+ if (sats !== void 0 && (!Number.isInteger(sats) || sats < 0)) {
468
+ fail("max_fee_out_of_range", `maxFee.sats must be a non-negative integer, got ${sats}`);
469
+ }
470
+ const legs = input.quote.pair.split("->");
471
+ const assetOf = (leg) => leg.slice(leg.indexOf(":") + 1);
472
+ const sameAsset = legs.length === 2 && assetOf(legs[0]) === assetOf(legs[1]);
473
+ if (!sameAsset && referenceRate === void 0) {
474
+ fail(
475
+ "fee_gate_unavailable",
476
+ `maxFee cannot gate ${input.quote.pair}: its legs name different assets, so from_amount - to_amount is not a fee. Supply maxFee.referenceRate (to-units per from-unit) from a source of your OWN \u2014 reading it off the solver's published feed would check the solver against its own number`
477
+ );
478
+ }
479
+ if (!sameAsset && (!Number.isFinite(referenceRate) || referenceRate <= 0)) {
480
+ fail(
481
+ "max_fee_out_of_range",
482
+ `maxFee.referenceRate must be a positive finite number, got ${referenceRate}`
483
+ );
484
+ }
485
+ const fee = sameAsset ? input.quote.from_amount - input.quote.to_amount : Math.ceil(
486
+ (input.quote.from_amount * referenceRate - input.quote.to_amount) / referenceRate
487
+ );
488
+ const allowed = Math.max(
489
+ sats ?? 0,
490
+ Math.floor(input.quote.from_amount * (bps ?? 0) / 1e4)
491
+ );
492
+ if (fee > allowed) {
493
+ fail("fee_too_high", `fee ${fee} exceeds the ${allowed} this client allows`);
494
+ }
495
+ }
454
496
  if (input.onchain) {
455
497
  const { htlcLocktime, minConfirmations, direction } = input.onchain;
456
498
  if (!Number.isInteger(minConfirmations) || minConfirmations < 1 || minConfirmations > MAX_MIN_CONFIRMATIONS) {
@@ -754,7 +796,7 @@ var lightningReceiveRequest = (input) => ({
754
796
  payment_hash: input.paymentHash,
755
797
  payout_address: input.payoutAddress,
756
798
  payout_pubkey: hex3.encode(input.payoutPubkey),
757
- claim_packet: input.claimPacket
799
+ ...input.claimPacket === void 0 ? {} : { claim_packet: input.claimPacket }
758
800
  }
759
801
  });
760
802
  var onchainReceiveRequest = (input) => ({
@@ -766,7 +808,7 @@ var onchainReceiveRequest = (input) => ({
766
808
  amount: input.amount,
767
809
  profile: {
768
810
  payment_hash: input.paymentHash,
769
- claim_packet: input.claimPacket,
811
+ ...input.claimPacket === void 0 ? {} : { claim_packet: input.claimPacket },
770
812
  refund_pubkey: hex3.encode(input.refundPubkey),
771
813
  payout_address: input.payoutAddress,
772
814
  payout_pubkey: hex3.encode(input.payoutPubkey)
@@ -846,6 +888,7 @@ async function requestOnchainSend(wallet, arkServerUrl, transport, params) {
846
888
  amountSide: params.amountSide
847
889
  })
848
890
  );
891
+ assertQuotedAmount(quote, params.amountSide, params.amount);
849
892
  const network = getNetwork(info.network);
850
893
  const derived = deriveOnchainSend({
851
894
  quote,
@@ -1036,17 +1079,17 @@ async function requestLightningReceive(wallet, arkServerUrl, transport, params)
1036
1079
  new RestArkProvider(arkServerUrl).getInfo(),
1037
1080
  wallet.getAddress()
1038
1081
  ]);
1039
- const claimPacket = await sealClaimPacket({
1082
+ const claimPacket = params.covclaimdPubkey ? await sealClaimPacket({
1040
1083
  preimage,
1041
1084
  covclaimdPubkey: params.covclaimdPubkey
1042
- });
1085
+ }) : void 0;
1043
1086
  const quote = await transport.requestQuote(
1044
1087
  lightningReceiveRequest({
1045
1088
  rfqId,
1046
1089
  paymentHash,
1047
1090
  payoutAddress,
1048
1091
  payoutPubkey,
1049
- claimPacket: claimPacket.ciphertext,
1092
+ claimPacket: claimPacket?.ciphertext,
1050
1093
  amount: params.amount,
1051
1094
  amountSide: params.amountSide
1052
1095
  })
@@ -1159,10 +1202,10 @@ async function requestOnchainReceive(wallet, arkServerUrl, transport, params) {
1159
1202
  new RestArkProvider(arkServerUrl).getInfo(),
1160
1203
  wallet.getAddress()
1161
1204
  ]);
1162
- const claimPacket = await sealClaimPacket({
1205
+ const claimPacket = params.covclaimdPubkey ? await sealClaimPacket({
1163
1206
  preimage,
1164
1207
  covclaimdPubkey: params.covclaimdPubkey
1165
- });
1208
+ }) : void 0;
1166
1209
  const quote = await transport.requestQuote(
1167
1210
  onchainReceiveRequest({
1168
1211
  rfqId,
@@ -1170,7 +1213,7 @@ async function requestOnchainReceive(wallet, arkServerUrl, transport, params) {
1170
1213
  payoutAddress,
1171
1214
  payoutPubkey,
1172
1215
  refundPubkey: params.refundPubkey,
1173
- claimPacket: claimPacket.ciphertext,
1216
+ claimPacket: claimPacket?.ciphertext,
1174
1217
  amount: params.amount,
1175
1218
  amountSide: params.amountSide
1176
1219
  })
@@ -1230,6 +1273,8 @@ export {
1230
1273
  ONCHAIN_DUST_SATS,
1231
1274
  newPreimage,
1232
1275
  paymentHashOf,
1276
+ L1_NETWORKS,
1277
+ l1ScriptForAddress,
1233
1278
  onchainHtlcScript,
1234
1279
  buildHtlcClaim,
1235
1280
  buildHtlcRefund,