@openreceive/browser 0.2.4 → 0.3.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.
package/dist/headless.js CHANGED
@@ -6,12 +6,14 @@ import {
6
6
  OPENRECEIVE_CHECKOUT_ELEMENT_EVENTS,
7
7
  OPENRECEIVE_CHECKOUT_ELEMENT_PARTS,
8
8
  OPENRECEIVE_CHECKOUT_ELEMENT_PART_SELECTORS,
9
+ OPENRECEIVE_CHECKOUT_ELEMENT_SLOTS,
9
10
  OPENRECEIVE_CHECKOUT_ELEMENT_TAG_NAME,
10
11
  OPENRECEIVE_COPY_FEEDBACK_MS,
11
12
  OPENRECEIVE_DEFAULT_POLL_INTERVAL_MS,
12
13
  OPENRECEIVE_DEFAULT_PREFIX,
13
14
  OPENRECEIVE_PAYMENT_WIZARD_ATTRIBUTES,
14
15
  OPENRECEIVE_PAYMENT_WIZARD_SELECTORS,
16
+ OPENRECEIVE_PROVIDER_PREVIEW_LIMIT,
15
17
  OPENRECEIVE_THEME_STORAGE_KEY,
16
18
  OPENRECEIVE_THEME_TOGGLE_ELEMENT_ATTRIBUTES,
17
19
  OPENRECEIVE_THEME_TOGGLE_ELEMENT_EVENTS,
@@ -23,7 +25,6 @@ import {
23
25
  buildMethodGridEntries,
24
26
  checkoutElementStyles,
25
27
  checkoutLabels,
26
- checkoutRoutes,
27
28
  copyInvoice,
28
29
  createBlockExplorerUrl,
29
30
  createCheckoutActionEvent,
@@ -36,12 +37,13 @@ import {
36
37
  createCheckoutStatusModel,
37
38
  createDetailExternalLink,
38
39
  createLightningInvoiceDecodeUrl,
39
- createPaymentDataEntries,
40
+ createMethodGridDisplay,
40
41
  createPaymentWizardController,
41
42
  createPaymentWizardModel,
42
43
  createPaymentWizardSelection,
43
44
  createQrPayloadSvg,
44
45
  createQrSvg,
46
+ createQrSvgController,
45
47
  createStatusFetcher,
46
48
  createSwapDisplayModel,
47
49
  createSwapUnavailableModel,
@@ -57,9 +59,7 @@ import {
57
59
  enterCheckoutResumePath,
58
60
  escapeHtml,
59
61
  findReusableLightningInvoice,
60
- findSwapGridGroup,
61
62
  formatAmountCaption,
62
- formatChooseNetworkHeading,
63
63
  formatDepositAmount,
64
64
  formatFiatAmount,
65
65
  formatMsats,
@@ -69,6 +69,7 @@ import {
69
69
  getExplorerNetwork,
70
70
  getNetworkIcon,
71
71
  getPaymentMethodIcon,
72
+ getRouteIconPath,
72
73
  getRouteNetworkLabel,
73
74
  getSwapOptionIcon,
74
75
  getSwapRefundFormError,
@@ -82,10 +83,9 @@ import {
82
83
  networkCheckClasses,
83
84
  networkMobileRevealClasses,
84
85
  networkSummaryIconClasses,
86
+ normalizeSwapStartInvoice,
85
87
  openWallet,
86
- optionalSafeInteger,
87
88
  orClasses,
88
- overlaySwapRefundStaging,
89
89
  parseBooleanAttribute,
90
90
  parseMethodPickerKey,
91
91
  parseOptionalInteger,
@@ -96,25 +96,22 @@ import {
96
96
  paymentIconPaths,
97
97
  paymentIconUrls,
98
98
  paymentMethods,
99
+ postJson,
99
100
  prepareCheckout,
100
- readJsonResponse,
101
101
  requestCheckout,
102
- requestHeaders,
103
- resolveBrowserLogger,
104
- resolvePreservedNetworkSelection,
102
+ requestSwapRefund,
105
103
  resolveTransactionDetailRows,
106
- sanitizeBrowserLogEntry,
104
+ resolveWizardSelection,
107
105
  selectCheckoutDisplayInvoice,
106
+ selectCurrentSwapInvoice,
107
+ startSwapRequest,
108
108
  swapAssetMatchesRoute,
109
109
  swapDepositRisk,
110
- swapGroupLimitOption,
111
110
  swapOptionLimitMessage,
112
111
  swapOptionLimitSentence,
113
112
  swapPickerKey,
114
- updatePaymentWizardSelection,
115
- updateSelectedSwapNetworks,
116
- wizardNetworkGroupIds
117
- } from "./chunk-GA4FMG7L.js";
113
+ updatePaymentWizardSelection
114
+ } from "./chunk-JR3DCSHN.js";
118
115
 
119
116
  // src/internal/checkout-props.ts
120
117
  var CREATE_MODE_ONLY_PROPS = [
@@ -144,201 +141,13 @@ function validateCheckoutProps(props) {
144
141
  }
145
142
 
146
143
  // src/internal/checkout-session.ts
147
- import { recordOrEmpty as recordOrEmpty2 } from "@openreceive/core";
148
-
149
- // src/internal/swap-http.ts
150
- import { nonEmptyString, recordOrEmpty } from "@openreceive/core";
151
- async function postJson(options) {
152
- const { body } = options;
153
- const routes = checkoutRoutes(options.prefix);
154
- const reference = nonEmptyString(body.reference);
155
- const action = nonEmptyString(body.action);
156
- if (action !== void 0 && action !== "swap_quote" && action !== "refund_swap") {
157
- throw new Error(`Unrecognized checkout action ${action}.`);
158
- }
159
- emitSwapActionLog(options.logger, "requested", body);
160
- try {
161
- const result = action === "swap_quote" ? await requestJson(options, routes.swapsQuote, {
162
- reference,
163
- pay_in_asset: body.pay_in_asset
164
- }) : action === "refund_swap" ? await refundRequest(options, routes, body, reference) : await requestJson(options, routes.paymentsCheck, withoutAction(body));
165
- if (action === "refund_swap") {
166
- emitSwapActionLog(options.logger, "succeeded", body, swapActionResultFields(result));
167
- }
168
- return result;
169
- } catch (error) {
170
- emitSwapActionLog(options.logger, "failed", body, {
171
- error_message: error instanceof Error ? error.message : String(error)
172
- });
173
- throw error;
174
- }
175
- }
176
- function normalizeSwapStartInvoice(body) {
177
- const outer = recordOrEmpty(body);
178
- const swap = recordOrEmpty(outer.swap ?? body);
179
- const checkout = recordOrEmpty(swap.checkout);
180
- const paymentHash = nonEmptyString(swap.payment_hash ?? checkout.payment_hash);
181
- if (paymentHash === void 0 || nonEmptyString(swap.provider) === void 0 || nonEmptyString(swap.pay_in_asset) === void 0 || nonEmptyString(swap.deposit_address) === void 0 || nonEmptyString(swap.deposit_amount) === void 0 || nonEmptyString(swap.provider_state) === void 0 || typeof swap.provider_expires_at !== "number") {
182
- throw new Error("Swap response did not include provider instructions.");
183
- }
184
- const amountMsats = optionalSafeInteger(checkout.amount_msats, "amount_msats");
185
- return {
186
- invoice_id: paymentHash,
187
- rail: "swap",
188
- payment_hash: paymentHash,
189
- ...amountMsats === void 0 ? {} : { amount_msats: amountMsats },
190
- transaction_state: "pending",
191
- workflow_state: "invoice_created",
192
- expires_at: swap.provider_expires_at,
193
- swap: {
194
- provider: swap.provider,
195
- pay_in_asset: swap.pay_in_asset,
196
- deposit_address: swap.deposit_address,
197
- deposit_amount: swap.deposit_amount,
198
- provider_state: swap.provider_state,
199
- provider_expires_at: swap.provider_expires_at,
200
- ...copyOptionalSwapFields(swap)
201
- }
202
- };
203
- }
204
- async function startSwapRequest(options) {
205
- const logBody = {
206
- action: "start_swap",
207
- reference: options.reference,
208
- pay_in_asset: options.payInAsset
209
- };
210
- emitSwapActionLog(options.logger, "requested", logBody);
211
- try {
212
- const body = await requestJson(options, checkoutRoutes(options.prefix).swaps, {
213
- reference: options.reference,
214
- pay_in_asset: options.payInAsset
215
- });
216
- emitSwapActionLog(options.logger, "succeeded", logBody, swapActionResultFields(body));
217
- return normalizeSwapStartInvoice(body);
218
- } catch (error) {
219
- emitSwapActionLog(options.logger, "failed", logBody, {
220
- error_message: error instanceof Error ? error.message : String(error)
221
- });
222
- throw error;
223
- }
224
- }
225
- async function requestSwapRefund(options) {
226
- const payment = options.invoices.find(
227
- (invoice) => invoice != null && (invoice.swap?.attempt_id ?? invoice.invoice_id) === options.attemptId
228
- );
229
- if (payment?.payment_hash === void 0) {
230
- throw new Error("Swap refund requires the original payment hash.");
231
- }
232
- const body = await postJson({
233
- fetch: options.fetch,
234
- prefix: options.prefix,
235
- ...options.logger === void 0 ? {} : { logger: options.logger },
236
- ...options.headers === void 0 ? {} : { headers: options.headers },
237
- body: {
238
- ...options.reference === void 0 ? {} : { reference: options.reference },
239
- payment_hash: payment.payment_hash,
240
- action: "refund_swap",
241
- attempt_id: options.attemptId,
242
- refund_address: options.refundAddress,
243
- confirm: options.confirm
244
- }
245
- });
246
- return normalizeSwapStartInvoice(body);
247
- }
248
- async function refundRequest(options, routes, body, reference) {
249
- if (reference === void 0) throw new Error("Swap refund requires reference.");
250
- const refundAddress = nonEmptyString(body.refund_address);
251
- if (refundAddress === void 0) throw new Error("Swap refund requires refund_address.");
252
- const paymentHash = nonEmptyString(body.payment_hash);
253
- if (paymentHash === void 0) throw new Error("Swap refund requires payment_hash.");
254
- if (body.confirm === true) {
255
- const status2 = recordOrEmpty(
256
- await requestJson(options, routes.swapsRefunds, {
257
- reference,
258
- payment_hash: paymentHash,
259
- refund_address: refundAddress
260
- })
261
- );
262
- return { swap: status2 };
263
- }
264
- const status = recordOrEmpty(
265
- await requestJson(options, routes.swapsStatus, {
266
- reference,
267
- payment_hash: paymentHash
268
- })
269
- );
270
- return { swap: { ...status, refund_address: refundAddress } };
271
- }
272
- function withoutAction(body) {
273
- const { action: _action, ...rest } = body;
274
- return rest;
275
- }
276
- async function requestJson(options, url, body) {
277
- const fetcher = options.fetch;
278
- const response = await fetcher(url, {
279
- method: "POST",
280
- headers: requestHeaders(options.headers),
281
- body: JSON.stringify(body)
282
- });
283
- return readJsonResponse(response, "OpenReceive request failed.");
284
- }
285
- function copyOptionalSwapFields(swap) {
286
- const output = {};
287
- for (const key of [
288
- "deposit_memo",
289
- "deposit_tx_id",
290
- "payout_tx_id",
291
- "refund_tx_id",
292
- "refund_reason",
293
- "refund_amount",
294
- "attention",
295
- "attempt_id",
296
- "provider_order_id",
297
- "refund_address",
298
- "attention_reason",
299
- "deposit_received_amount",
300
- "fee"
301
- ]) {
302
- if (swap[key] !== void 0) output[key] = swap[key];
303
- }
304
- return output;
305
- }
306
- function emitSwapActionLog(logger, outcome, body, extra = {}) {
307
- const sink = resolveBrowserLogger(logger);
308
- if (sink === void 0) return;
309
- const action = nonEmptyString(body.action);
310
- if (action !== "start_swap" && action !== "refund_swap") return;
311
- const event = action === "start_swap" ? `swap.start.${outcome}` : `swap.refund.${outcome}`;
312
- const level = outcome === "failed" ? "warn" : outcome === "requested" ? "debug" : "info";
313
- try {
314
- sink(
315
- sanitizeBrowserLogEntry({
316
- level,
317
- event,
318
- message: `${action === "start_swap" ? "Swap start" : "Swap refund"} ${outcome}.`,
319
- reference: nonEmptyString(body.reference),
320
- pay_in_asset: nonEmptyString(body.pay_in_asset),
321
- ...action === "refund_swap" ? { confirm: body.confirm === true } : {},
322
- ...extra
323
- })
324
- );
325
- } catch {
326
- }
327
- }
328
- function swapActionResultFields(body) {
329
- const swap = recordOrEmpty(recordOrEmpty(body).swap ?? body);
330
- return {
331
- payment_hash: nonEmptyString(swap.payment_hash),
332
- provider_state: nonEmptyString(swap.provider_state),
333
- attention: typeof swap.attention === "boolean" ? swap.attention : void 0
334
- };
335
- }
336
-
337
- // src/internal/checkout-session.ts
144
+ import { recordOrEmpty } from "@openreceive/core";
338
145
  var SWAP_START_FAILED = "Could not prepare the payment address. Please try again.";
146
+ var MISSING_SWAP_OPTIONS = "startSwap needs the session's `swap` options (selection, prefix, fetch) \u2014 this session was created without them, so it cannot start a swap. See docs/guides/headless-checkout.md.";
147
+ var MISSING_SWAP_WIRING = "startSwap cannot start a swap: the session was given no";
339
148
  var MINT_FAILED = "Could not create the Lightning invoice. Please try again.";
340
149
  function normalizeSwapQuote(body) {
341
- const quote = recordOrEmpty2(recordOrEmpty2(body).quote ?? body);
150
+ const quote = recordOrEmpty(recordOrEmpty(body).quote ?? body);
342
151
  const payInAsset = quote.pay_in_asset ?? quote.pay_asset;
343
152
  return typeof payInAsset === "string" ? { ...quote, pay_in_asset: payInAsset } : void 0;
344
153
  }
@@ -385,11 +194,23 @@ function createCheckoutSession(options) {
385
194
  }
386
195
  async function startSwap(payInAsset) {
387
196
  if (startingSwapAsset !== null) return;
388
- const selection = options.swapSelection;
389
- const prefix = options.swapPrefix?.();
390
- const fetcher = options.fetch?.();
197
+ const swap = options.swap;
198
+ if (swap === void 0) {
199
+ options.onError(new Error(MISSING_SWAP_OPTIONS));
200
+ return;
201
+ }
202
+ const selection = swap.selection;
203
+ const prefix = swap.prefix();
204
+ const fetcher = swap.fetch();
391
205
  const reference = options.reference();
392
- if (selection === void 0 || prefix === void 0 || fetcher === void 0) return;
206
+ if (prefix === void 0 || fetcher === void 0) {
207
+ const missing = [
208
+ ...prefix === void 0 ? ["swap.prefix()"] : [],
209
+ ...fetcher === void 0 ? ["swap.fetch()"] : []
210
+ ];
211
+ options.onError(new Error(`${MISSING_SWAP_WIRING} ${missing.join(" and ")}.`));
212
+ return;
213
+ }
393
214
  if (reference === void 0 || reference.length === 0) return;
394
215
  const alreadyStarted = selection.started();
395
216
  if (alreadyStarted?.swap?.pay_in_asset === payInAsset && alreadyStarted.invoice_id !== selection.dismissedInvoiceId()) {
@@ -412,7 +233,7 @@ function createCheckoutSession(options) {
412
233
  });
413
234
  selection.setStarted(started);
414
235
  selection.setDismissedInvoiceId(null);
415
- options.onSwapStarted?.(started);
236
+ swap.onStarted?.(started);
416
237
  selection.setSelectedAsset(payInAsset);
417
238
  options.onChange();
418
239
  } catch (error) {
@@ -610,6 +431,12 @@ function sharedElementAttributes(options) {
610
431
  if (options.decodeLinkUrl !== void 0) {
611
432
  attributes[OPENRECEIVE_CHECKOUT_ELEMENT_ATTRIBUTES.decodeLinkUrl] = options.decodeLinkUrl;
612
433
  }
434
+ if (isSet(options.assetBaseUrl)) {
435
+ attributes[OPENRECEIVE_CHECKOUT_ELEMENT_ATTRIBUTES.assetBaseUrl] = options.assetBaseUrl;
436
+ }
437
+ if (options.resumable !== void 0) {
438
+ attributes[OPENRECEIVE_CHECKOUT_ELEMENT_ATTRIBUTES.resumable] = String(options.resumable);
439
+ }
613
440
  if (options.polling !== void 0) {
614
441
  attributes[OPENRECEIVE_CHECKOUT_ELEMENT_ATTRIBUTES.polling] = String(options.polling);
615
442
  }
@@ -768,6 +595,10 @@ function createCheckoutShell(snapshot, options = {}) {
768
595
  themeToggle
769
596
  };
770
597
  }
598
+
599
+ // src/headless.ts
600
+ import { createAssetBaseUrlResolver } from "@openreceive/provider-data";
601
+ import { getPaymentWizardRoutes } from "@openreceive/provider-data";
771
602
  export {
772
603
  BrowserRequestError,
773
604
  OPENRECEIVE_CHECKOUT_DATA_ATTRIBUTES,
@@ -776,12 +607,14 @@ export {
776
607
  OPENRECEIVE_CHECKOUT_ELEMENT_EVENTS,
777
608
  OPENRECEIVE_CHECKOUT_ELEMENT_PARTS,
778
609
  OPENRECEIVE_CHECKOUT_ELEMENT_PART_SELECTORS,
610
+ OPENRECEIVE_CHECKOUT_ELEMENT_SLOTS,
779
611
  OPENRECEIVE_CHECKOUT_ELEMENT_TAG_NAME,
780
612
  OPENRECEIVE_COPY_FEEDBACK_MS,
781
613
  OPENRECEIVE_DEFAULT_POLL_INTERVAL_MS,
782
614
  OPENRECEIVE_DEFAULT_PREFIX,
783
615
  OPENRECEIVE_PAYMENT_WIZARD_ATTRIBUTES,
784
616
  OPENRECEIVE_PAYMENT_WIZARD_SELECTORS,
617
+ OPENRECEIVE_PROVIDER_PREVIEW_LIMIT,
785
618
  OPENRECEIVE_THEME_STORAGE_KEY,
786
619
  OPENRECEIVE_THEME_TOGGLE_ELEMENT_ATTRIBUTES,
787
620
  OPENRECEIVE_THEME_TOGGLE_ELEMENT_EVENTS,
@@ -795,6 +628,7 @@ export {
795
628
  checkoutElementStyles,
796
629
  checkoutLabels,
797
630
  copyInvoice,
631
+ createAssetBaseUrlResolver,
798
632
  createBlockExplorerUrl,
799
633
  createCheckoutActionEvent,
800
634
  createCheckoutController,
@@ -811,12 +645,13 @@ export {
811
645
  createCheckoutStatusModel,
812
646
  createDetailExternalLink,
813
647
  createLightningInvoiceDecodeUrl,
814
- createPaymentDataEntries,
648
+ createMethodGridDisplay,
815
649
  createPaymentWizardController,
816
650
  createPaymentWizardModel,
817
651
  createPaymentWizardSelection,
818
652
  createQrPayloadSvg,
819
653
  createQrSvg,
654
+ createQrSvgController,
820
655
  createStatusFetcher,
821
656
  createStoredThemeModel,
822
657
  createSwapDisplayModel,
@@ -835,9 +670,7 @@ export {
835
670
  deriveStatus,
836
671
  enterCheckoutResumePath,
837
672
  escapeHtml,
838
- findSwapGridGroup,
839
673
  formatAmountCaption,
840
- formatChooseNetworkHeading,
841
674
  formatDepositAmount,
842
675
  formatFiatAmount,
843
676
  formatMsats,
@@ -847,6 +680,8 @@ export {
847
680
  getExplorerNetwork,
848
681
  getNetworkIcon,
849
682
  getPaymentMethodIcon,
683
+ getPaymentWizardRoutes,
684
+ getRouteIconPath,
850
685
  getRouteNetworkLabel,
851
686
  getSwapOptionIcon,
852
687
  getSwapRefundFormError,
@@ -861,7 +696,6 @@ export {
861
696
  normalizeSwapStartInvoice,
862
697
  openWallet,
863
698
  orClasses,
864
- overlaySwapRefundStaging,
865
699
  parseBooleanAttribute,
866
700
  parseMethodPickerKey,
867
701
  parseOptionalInteger,
@@ -877,21 +711,19 @@ export {
877
711
  readThemePreference,
878
712
  requestCheckout,
879
713
  requestSwapRefund,
880
- resolvePreservedNetworkSelection,
881
714
  resolveTransactionDetailRows,
715
+ resolveWizardSelection,
882
716
  selectCheckoutDisplayInvoice,
717
+ selectCurrentSwapInvoice,
883
718
  startSwapRequest,
884
719
  swapAssetMatchesRoute,
885
720
  swapDepositRisk,
886
- swapGroupLimitOption,
887
721
  swapOptionLimitMessage,
888
722
  swapOptionLimitSentence,
889
723
  swapPickerKey,
890
724
  syncStoredThemeControls,
891
725
  toggleStoredThemeControls,
892
726
  updatePaymentWizardSelection,
893
- updateSelectedSwapNetworks,
894
727
  validateCheckoutProps,
895
- wizardNetworkGroupIds,
896
728
  writeThemePreference
897
729
  };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { aK as BrowserLogLevel } from './status-D3VhJsUp.js';
2
- export { aL as BrowserLogEntry, X as BrowserLogger, B as BrowserLoggerOption, Y as BrowserRequestError, Z as CheckoutController, _ as CheckoutControllerOptions, l as CheckoutSnapshot, i as CheckoutState, aM as CopyInvoiceOptions, aN as GuestCheckoutResumeController, aO as GuestCheckoutResumeOptions, aP as OpenWalletOptions, aQ as PrepareCheckoutOptions, aR as QrOptions, aS as RequestCheckoutOptions, ak as Status, aT as StatusInvoiceLike, U as UnixSeconds, an as copyInvoice, ap as createCheckoutController, aU as createGuestCheckoutResume, aV as createGuestOrderFetcher, aW as createLightningUri, aX as createQrPngDataUrl, au as createQrSvg, ay as deriveStatus, az as enterCheckoutResumePath, aH as openWallet, aI as prepareCheckout, aJ as requestCheckout } from './status-D3VhJsUp.js';
1
+ import { aQ as BrowserLogLevel } from './status-BZEGi6Ca.js';
2
+ export { aR as BrowserLogEntry, V as BrowserLogger, B as BrowserLoggerOption, X as BrowserRequestError, Y as CheckoutController, Z as CheckoutControllerOptions, g as CheckoutSnapshot, i as CheckoutState, aS as CopyInvoiceOptions, aT as GuestCheckoutResumeController, aU as GuestCheckoutResumeOptions, aV as OpenWalletOptions, aW as PrepareCheckoutOptions, aX as QrOptions, aY as RequestCheckoutOptions, an as Status, aZ as StatusInvoiceLike, U as UnixSeconds, as as copyInvoice, au as createCheckoutController, a_ as createGuestCheckoutResume, a$ as createGuestOrderFetcher, b0 as createLightningUri, b1 as createQrPngDataUrl, az as createQrSvg, aE as deriveStatus, aF as enterCheckoutResumePath, aN as openWallet, aO as prepareCheckout, aP as requestCheckout } from './status-BZEGi6Ca.js';
3
3
  import '@openreceive/provider-data';
4
4
  import '@openreceive/core';
5
5
 
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  openWallet,
14
14
  prepareCheckout,
15
15
  requestCheckout
16
- } from "./chunk-GA4FMG7L.js";
16
+ } from "./chunk-JR3DCSHN.js";
17
17
  export {
18
18
  BrowserRequestError,
19
19
  copyInvoice,