@arkade-os/swap 0.1.0-rc.21 → 0.1.0-rc.23

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 (37) hide show
  1. package/README.md +7 -2
  2. package/dist/advanced.cjs +875 -129
  3. package/dist/advanced.d.cts +158 -14
  4. package/dist/advanced.d.ts +158 -14
  5. package/dist/advanced.js +255 -4
  6. package/dist/{chunk-ITFCQWPG.js → chunk-DQ2DY2L2.js} +223 -11
  7. package/dist/{chunk-HUQQVBC4.js → chunk-MZX62KMU.js} +220 -12
  8. package/dist/{chunk-PVB7EBAM.js → chunk-UFJEO4VV.js} +47 -21
  9. package/dist/{errors-hm7aqnRM.d.cts → errors-5q4e3PnH.d.cts} +48 -16
  10. package/dist/{errors-C3h8ANmE.d.ts → errors-DQ35FKJf.d.ts} +48 -16
  11. package/dist/index.cjs +670 -185
  12. package/dist/index.d.cts +5 -5
  13. package/dist/index.d.ts +5 -5
  14. package/dist/index.js +5 -3
  15. package/dist/node/index.d.cts +26 -4
  16. package/dist/node/index.d.ts +26 -4
  17. package/dist/nostr.cjs +12 -9
  18. package/dist/nostr.d.cts +1 -1
  19. package/dist/nostr.d.ts +1 -1
  20. package/dist/nostr.js +3 -1
  21. package/dist/{offer-Bd9Yvd8Z.d.ts → offer--IpfzhIm.d.ts} +1 -1
  22. package/dist/{offer-F88rtL_I.d.cts → offer-DqTXQeP1.d.cts} +1 -1
  23. package/dist/protocol.cjs +395 -156
  24. package/dist/protocol.d.cts +45 -8
  25. package/dist/protocol.d.ts +45 -8
  26. package/dist/protocol.js +10 -3
  27. package/dist/repositories/realm/index.d.cts +2 -2
  28. package/dist/repositories/realm/index.d.ts +2 -2
  29. package/dist/repositories/sqlite/index.d.cts +2 -2
  30. package/dist/repositories/sqlite/index.d.ts +2 -2
  31. package/dist/{repository-DYnRLPt-.d.ts → repository-B4rQWmS2.d.cts} +88 -20
  32. package/dist/{repository-BuYWGAKz.d.cts → repository-D_qvJk1S.d.ts} +88 -20
  33. package/dist/{rfq-60PGpQ_x.d.cts → rfq-BgdgO9OO.d.cts} +16 -7
  34. package/dist/{rfq-60PGpQ_x.d.ts → rfq-BgdgO9OO.d.ts} +16 -7
  35. package/dist/{watch-BTyiBLSd.d.ts → watch-BkPz0vf8.d.ts} +1 -1
  36. package/dist/{watch-M_KY-5ho.d.cts → watch-RRhKuBkh.d.cts} +1 -1
  37. package/package.json +3 -3
package/dist/advanced.js CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  CORRIDOR_FACTORIES,
14
14
  ClientDisposed,
15
15
  DEFAULT_INVOICE_EXPIRY_SECONDS,
16
+ DEFAULT_MAX_REFUND_WINDOW_SECONDS,
16
17
  DiscoverySnapshotUnavailable,
17
18
  FEED_TTL_MS,
18
19
  INDEXED_NETWORKS,
@@ -136,6 +137,7 @@ import {
136
137
  verifyQuotedAmount,
137
138
  verifyReceiveInvoiceFacts,
138
139
  verifyReceiveWindow,
140
+ verifyRefundWindow,
139
141
  verifyResponder,
140
142
  verifySendInvoice,
141
143
  verifySendWindow,
@@ -144,7 +146,7 @@ import {
144
146
  withCanonicalAmount,
145
147
  withDepositFate,
146
148
  withRfqState
147
- } from "./chunk-HUQQVBC4.js";
149
+ } from "./chunk-MZX62KMU.js";
148
150
  import {
149
151
  ACTIVITY_TOKEN,
150
152
  CORRIDOR_PASS,
@@ -159,17 +161,20 @@ import {
159
161
  onchainCorridor,
160
162
  readsChain,
161
163
  recordOutcome
162
- } from "./chunk-ITFCQWPG.js";
164
+ } from "./chunk-DQ2DY2L2.js";
163
165
  import {
166
+ ARKADE_BTC,
164
167
  SwapRefusal,
168
+ assertPairLength,
165
169
  assertPreimageMatches,
170
+ rfqPair,
166
171
  sealClaimPacket
167
- } from "./chunk-PVB7EBAM.js";
172
+ } from "./chunk-UFJEO4VV.js";
168
173
  import "./chunk-A3SHHRX4.js";
169
174
 
170
175
  // src/reveal.ts
171
176
  import { base64, hex } from "@scure/base";
172
- import { ArkAddress } from "@arkade-os/sdk";
177
+ import { ArkAddress, assertTimerMs } from "@arkade-os/sdk";
173
178
  var CovclaimdRevealError = class extends Error {
174
179
  name = "CovclaimdRevealError";
175
180
  status;
@@ -209,6 +214,7 @@ var covclaimdClient = (baseUrl, options = {}) => {
209
214
  const url = baseUrl.replace(/\/+$/, "");
210
215
  const fetchImpl = options.fetchImpl ?? fetch;
211
216
  const timeoutMs = options.timeoutMs ?? 3e4;
217
+ assertTimerMs("timeoutMs", timeoutMs);
212
218
  const send = async (what, path, init) => {
213
219
  let response;
214
220
  try {
@@ -284,6 +290,235 @@ async function revealClaimPacket(client, input) {
284
290
  await client.reveal({ swapAddress: input.address, ciphertext, ...fields });
285
291
  return { ciphertext };
286
292
  }
293
+
294
+ // src/evmRfq.ts
295
+ import { hex as hex2 } from "@scure/base";
296
+ var EVM_CHAIN = "ethereum";
297
+ var EVM_ADDRESS = /^0x[0-9a-fA-F]{40}$/;
298
+ var TOKEN_AMOUNT = /^(0|[1-9][0-9]*)$/;
299
+ var MAX_UINT256 = (1n << 256n) - 1n;
300
+ var MAX_UINT256_DECIMAL = MAX_UINT256.toString();
301
+ var SEND_PAIR = /^arkade:BTC->ethereum:0x[0-9a-f]{40}$/;
302
+ var RECEIVE_PAIR = /^ethereum:0x[0-9a-f]{40}->arkade:BTC$/;
303
+ var evmTokenLeg = (tokenAddress) => `${EVM_CHAIN}:${assertEvmAddress(tokenAddress, "token address").toLowerCase()}`;
304
+ var evmSendPair = (tokenAddress) => {
305
+ const pair = rfqPair(ARKADE_BTC, evmTokenLeg(tokenAddress));
306
+ assertPairLength(pair);
307
+ return pair;
308
+ };
309
+ var evmReceivePair = (tokenAddress) => {
310
+ const pair = rfqPair(evmTokenLeg(tokenAddress), ARKADE_BTC);
311
+ assertPairLength(pair);
312
+ return pair;
313
+ };
314
+ var evmDirectionOf = (pair) => {
315
+ if (SEND_PAIR.test(pair)) return "send";
316
+ if (RECEIVE_PAIR.test(pair)) return "receive";
317
+ return null;
318
+ };
319
+ var evmTokenOf = (pair) => {
320
+ const direction = evmDirectionOf(pair);
321
+ if (direction === null) return null;
322
+ const [from, to] = pair.split("->");
323
+ return (direction === "send" ? to : from).slice(EVM_CHAIN.length + 1);
324
+ };
325
+ var evmAmountToWire = (units) => {
326
+ if (typeof units !== "bigint") throw new Error("token amount must be a bigint");
327
+ if (units < 0n) throw new Error(`token amount may not be negative, got ${units}`);
328
+ if (units > MAX_UINT256) throw new Error(`token amount must fit uint256, got ${units}`);
329
+ return units.toString();
330
+ };
331
+ var evmAmountFromWire = (value, field) => {
332
+ if (typeof value !== "string") {
333
+ throw new Error(
334
+ `${field} must be a decimal string of atomic units, not a JSON ${typeof value}`
335
+ );
336
+ }
337
+ if (!TOKEN_AMOUNT.test(value)) {
338
+ throw new Error(`${field} is not a canonical decimal amount: ${JSON.stringify(value)}`);
339
+ }
340
+ if (value.length > MAX_UINT256_DECIMAL.length || value.length === MAX_UINT256_DECIMAL.length && value > MAX_UINT256_DECIMAL) {
341
+ throw new Error(`${field} exceeds uint256`);
342
+ }
343
+ return BigInt(value);
344
+ };
345
+ var evmSendRequest = (input) => {
346
+ assertPositiveInteger(input.amountSats, "amountSats");
347
+ assertHash32(input.paymentHash, "paymentHash");
348
+ return {
349
+ v: 1,
350
+ type: "rfq_request",
351
+ rfq_id: input.rfqId,
352
+ pair: evmSendPair(input.tokenAddress),
353
+ amount_side: "from",
354
+ amount: input.amountSats,
355
+ profile: {
356
+ payment_hash: input.paymentHash,
357
+ evm_claim_address: assertEvmAddress(input.evmClaimAddress, "evmClaimAddress"),
358
+ refund_address: input.refundAddress,
359
+ client_refund_pubkey: hex2.encode(input.senderPubkey)
360
+ }
361
+ };
362
+ };
363
+ var evmReceiveRequest = (input) => {
364
+ assertPositiveInteger(input.evmTimeoutBlock, "evmTimeoutBlock");
365
+ assertHash32(input.paymentHash, "paymentHash");
366
+ if (input.evmAmount <= 0n) {
367
+ throw new Error(
368
+ `evmAmount must be a positive number of atomic units, got ${input.evmAmount}`
369
+ );
370
+ }
371
+ return {
372
+ v: 1,
373
+ type: "rfq_request",
374
+ rfq_id: input.rfqId,
375
+ pair: evmReceivePair(input.tokenAddress),
376
+ amount_side: "from",
377
+ profile: {
378
+ payment_hash: input.paymentHash,
379
+ evm_amount: evmAmountToWire(input.evmAmount),
380
+ evm_timeout_block: input.evmTimeoutBlock,
381
+ evm_refund_address: assertEvmAddress(input.evmRefundAddress, "evmRefundAddress"),
382
+ payout_address: input.payoutAddress,
383
+ payout_pubkey: hex2.encode(input.payoutPubkey)
384
+ }
385
+ };
386
+ };
387
+ var readEvmSendQuote = (payload, expected) => {
388
+ assertHash32(expected.paymentHash, "expected.paymentHash");
389
+ assertPositiveInteger(expected.chainId, "expected.chainId");
390
+ assertPositiveInteger(expected.amountSats, "expected.amountSats");
391
+ const { quote, profile } = readEvmQuoteEnvelope(
392
+ payload,
393
+ evmSendPair(expected.tokenAddress),
394
+ expected.rfqId
395
+ );
396
+ readCommonProfile(profile, expected.paymentHash, expected.chainId);
397
+ assertHex(profile.receiver_pk_script, "profile.receiver_pk_script");
398
+ assertEvmAddress(
399
+ asString(profile.evm_refund_address, "profile.evm_refund_address"),
400
+ "profile.evm_refund_address"
401
+ );
402
+ assertPositiveInteger(profile.evm_timeout_block, "profile.evm_timeout_block");
403
+ assertPositiveTokenAmount(quote.to_amount, "to_amount");
404
+ assertPositiveInteger(quote.from_amount, "from_amount");
405
+ if (quote.from_amount !== expected.amountSats) {
406
+ throw new Error("quote from_amount does not match the requested sats amount");
407
+ }
408
+ return quote;
409
+ };
410
+ var readEvmReceiveQuote = (payload, expected) => {
411
+ assertHash32(expected.paymentHash, "expected.paymentHash");
412
+ assertPositiveInteger(expected.chainId, "expected.chainId");
413
+ if (expected.evmAmount <= 0n) throw new Error("expected.evmAmount must be positive");
414
+ evmAmountToWire(expected.evmAmount);
415
+ const { quote, profile } = readEvmQuoteEnvelope(
416
+ payload,
417
+ evmReceivePair(expected.tokenAddress),
418
+ expected.rfqId
419
+ );
420
+ readCommonProfile(profile, expected.paymentHash, expected.chainId);
421
+ assertHex(profile.solver_refund_pk_script, "profile.solver_refund_pk_script");
422
+ assertEvmAddress(
423
+ asString(profile.evm_claim_address, "profile.evm_claim_address"),
424
+ "profile.evm_claim_address"
425
+ );
426
+ if (assertPositiveTokenAmount(quote.from_amount, "from_amount") !== expected.evmAmount) {
427
+ throw new Error("quote from_amount does not match the requested token amount");
428
+ }
429
+ assertPositiveInteger(quote.to_amount, "to_amount");
430
+ return quote;
431
+ };
432
+ var evmQuoteTokenAmount = (quote) => {
433
+ const direction = evmDirectionOf(quote.pair);
434
+ if (direction === null) throw new Error(`not an EVM pair: ${JSON.stringify(quote.pair)}`);
435
+ return direction === "send" ? evmAmountFromWire(quote.to_amount, "to_amount") : evmAmountFromWire(quote.from_amount, "from_amount");
436
+ };
437
+ var evmQuoteSats = (quote) => {
438
+ const direction = evmDirectionOf(quote.pair);
439
+ if (direction === null) throw new Error(`not an EVM pair: ${JSON.stringify(quote.pair)}`);
440
+ const field = direction === "send" ? "from_amount" : "to_amount";
441
+ const sats = quote[field];
442
+ assertPositiveInteger(sats, field);
443
+ return sats;
444
+ };
445
+ var assertEvmAddress = (value, field) => {
446
+ if (!EVM_ADDRESS.test(value)) {
447
+ throw new Error(`${field} must be 0x then 40 hex, got ${JSON.stringify(value)}`);
448
+ }
449
+ return value;
450
+ };
451
+ var assertPositiveInteger = (value, field) => {
452
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
453
+ throw new Error(`${field} must be a positive integer, got ${String(value)}`);
454
+ }
455
+ };
456
+ var assertNonNegativeInteger = (value, field) => {
457
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
458
+ throw new Error(`${field} must be a non-negative integer, got ${String(value)}`);
459
+ }
460
+ };
461
+ var assertHex = (value, field) => {
462
+ if (typeof value !== "string" || value.length === 0 || value.length % 2 !== 0 || !/^[0-9a-f]+$/.test(value)) {
463
+ throw new Error(`${field} must be lowercase hex of whole bytes, got ${String(value)}`);
464
+ }
465
+ };
466
+ var assertHash32 = (value, field) => {
467
+ if (typeof value !== "string" || !/^[0-9a-f]{64}$/.test(value)) {
468
+ throw new Error(`${field} must be 64 lowercase hex characters, got ${String(value)}`);
469
+ }
470
+ };
471
+ var assertPositiveTokenAmount = (value, field) => {
472
+ const amount = evmAmountFromWire(value, field);
473
+ if (amount === 0n) throw new Error(`${field} must be positive`);
474
+ return amount;
475
+ };
476
+ var asString = (value, field) => {
477
+ if (typeof value !== "string") {
478
+ throw new Error(`${field} must be a string, got ${String(value)}`);
479
+ }
480
+ return value;
481
+ };
482
+ var readEvmQuoteEnvelope = (payload, expectedPair, expectedRfqId) => {
483
+ if (!payload || typeof payload !== "object") throw new Error("EVM quote is not an object");
484
+ const quote = payload;
485
+ if (quote.v !== 1) throw new Error(`expected EVM quote version 1, got ${String(quote.v)}`);
486
+ if (quote.type !== "rfq_quote") {
487
+ throw new Error(`expected an rfq_quote, got ${String(quote.type)}`);
488
+ }
489
+ if (quote.rfq_id !== expectedRfqId) {
490
+ throw new Error(
491
+ `quote is for rfq_id ${JSON.stringify(quote.rfq_id)}, not this negotiation's ${expectedRfqId}`
492
+ );
493
+ }
494
+ if (quote.pair !== expectedPair) {
495
+ throw new Error(`solver quoted ${JSON.stringify(quote.pair)}, not ${expectedPair}`);
496
+ }
497
+ asString(quote.solver_pubkey, "solver_pubkey");
498
+ assertPositiveInteger(quote.valid_until, "valid_until");
499
+ assertPositiveInteger(quote.refund_locktime, "refund_locktime");
500
+ if (!quote.profile || typeof quote.profile !== "object") {
501
+ throw new Error("EVM quote carries no profile");
502
+ }
503
+ return { quote, profile: quote.profile };
504
+ };
505
+ var readCommonProfile = (profile, paymentHash, chainId) => {
506
+ assertHash32(profile.payment_hash, "profile.payment_hash");
507
+ asString(profile.lockup_address, "profile.lockup_address");
508
+ assertEvmAddress(
509
+ asString(profile.evm_contract_address, "profile.evm_contract_address"),
510
+ "profile.evm_contract_address"
511
+ );
512
+ assertPositiveInteger(profile.evm_chain_id, "profile.evm_chain_id");
513
+ assertPositiveInteger(profile.min_confirmations, "profile.min_confirmations");
514
+ assertNonNegativeInteger(profile.min_age_seconds, "profile.min_age_seconds");
515
+ if (profile.payment_hash !== paymentHash) {
516
+ throw new Error("quote payment_hash does not match the request");
517
+ }
518
+ if (profile.evm_chain_id !== chainId) {
519
+ throw new Error("quote evm_chain_id does not match the expected chain");
520
+ }
521
+ };
287
522
  export {
288
523
  ACTIVITY_TOKEN,
289
524
  ARKADE_ASSET_NAMESPACE,
@@ -302,7 +537,9 @@ export {
302
537
  ClientDisposed,
303
538
  CovclaimdRevealError,
304
539
  DEFAULT_INVOICE_EXPIRY_SECONDS,
540
+ DEFAULT_MAX_REFUND_WINDOW_SECONDS,
305
541
  DiscoverySnapshotUnavailable,
542
+ EVM_CHAIN,
306
543
  FEED_TTL_MS,
307
544
  INDEXED_NETWORKS,
308
545
  INVOICE_HRPS,
@@ -360,6 +597,17 @@ export {
360
597
  enforceFeeCeiling,
361
598
  esploraChainSource,
362
599
  esploraTip,
600
+ evmAmountFromWire,
601
+ evmAmountToWire,
602
+ evmDirectionOf,
603
+ evmQuoteSats,
604
+ evmQuoteTokenAmount,
605
+ evmReceivePair,
606
+ evmReceiveRequest,
607
+ evmSendPair,
608
+ evmSendRequest,
609
+ evmTokenLeg,
610
+ evmTokenOf,
363
611
  exchange,
364
612
  familyOfSwapId,
365
613
  fateMoved,
@@ -403,6 +651,8 @@ export {
403
651
  quoteViaRfq,
404
652
  railOf,
405
653
  railOfCorridor,
654
+ readEvmReceiveQuote,
655
+ readEvmSendQuote,
406
656
  readableRecord,
407
657
  readsChain,
408
658
  receive,
@@ -440,6 +690,7 @@ export {
440
690
  verifyQuotedAmount,
441
691
  verifyReceiveInvoiceFacts,
442
692
  verifyReceiveWindow,
693
+ verifyRefundWindow,
443
694
  verifyResponder,
444
695
  verifySendInvoice,
445
696
  verifySendWindow,