@nodana/phoenixd-mpp-sdk 0.0.0-stage → 0.1.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.
@@ -0,0 +1,140 @@
1
+ import {
2
+ invoiceDetails,
3
+ paymentHash
4
+ } from "../chunk-7BE7G2UI.js";
5
+ import {
6
+ PhoenixdPaymentError,
7
+ __export,
8
+ charge
9
+ } from "../chunk-EQRLZHAJ.js";
10
+
11
+ // src/client/index.ts
12
+ import { Mppx } from "mppx/client";
13
+
14
+ // src/client/methods.ts
15
+ var methods_exports = {};
16
+ __export(methods_exports, {
17
+ charge: () => charge2
18
+ });
19
+
20
+ // src/client/charge.ts
21
+ import { Credential, Method } from "mppx";
22
+ function charge2({
23
+ client,
24
+ maxAmountSat,
25
+ network = "mainnet"
26
+ }) {
27
+ if (!Number.isSafeInteger(maxAmountSat) || maxAmountSat < 1)
28
+ throw new TypeError("maxAmountSat must be a positive integer");
29
+ const pending = /* @__PURE__ */ new Map();
30
+ const verifiedProof = (preimage, hash) => {
31
+ let actual;
32
+ try {
33
+ actual = paymentHash(preimage);
34
+ } catch {
35
+ throw new PhoenixdPaymentError(
36
+ "payment_unknown",
37
+ "Phoenixd returned an invalid payment proof"
38
+ );
39
+ }
40
+ if (actual !== hash)
41
+ throw new PhoenixdPaymentError(
42
+ "payment_unknown",
43
+ "Phoenixd returned a payment proof that does not match the invoice"
44
+ );
45
+ return preimage;
46
+ };
47
+ async function pay(invoice, hash) {
48
+ const existing = await client.getOutgoingPaymentByHash(hash);
49
+ if (existing?.isPaid) {
50
+ if (!existing.preimage)
51
+ throw new PhoenixdPaymentError(
52
+ "payment_unknown",
53
+ "The invoice is paid but its proof is unavailable; check outgoing payment history"
54
+ );
55
+ return verifiedProof(existing.preimage, hash);
56
+ }
57
+ if (existing && existing.completedAt == null)
58
+ throw new PhoenixdPaymentError(
59
+ "payment_unknown",
60
+ "A payment for this invoice is already pending; check outgoing payment history"
61
+ );
62
+ let result;
63
+ try {
64
+ result = await client.payInvoice({ invoice });
65
+ } catch {
66
+ try {
67
+ const payment = await client.getOutgoingPaymentByHash(hash);
68
+ if (payment?.isPaid && payment.preimage)
69
+ return verifiedProof(payment.preimage, hash);
70
+ } catch {
71
+ }
72
+ throw new PhoenixdPaymentError(
73
+ "payment_unknown",
74
+ "The payment outcome is unknown; check outgoing payment history before retrying"
75
+ );
76
+ }
77
+ if (result.type === "payment_failed")
78
+ throw new PhoenixdPaymentError(
79
+ "payment_failed",
80
+ "Phoenixd rejected the payment"
81
+ );
82
+ if (result.paymentHash !== hash)
83
+ throw new PhoenixdPaymentError(
84
+ "payment_unknown",
85
+ "Phoenixd returned a different payment hash"
86
+ );
87
+ return verifiedProof(result.paymentPreimage, hash);
88
+ }
89
+ return Method.toClient(charge, {
90
+ async createCredential({ challenge }) {
91
+ const request = charge.schema.request.parse(challenge.request);
92
+ let details;
93
+ try {
94
+ details = invoiceDetails(
95
+ await client.decodeInvoice({
96
+ invoice: request.methodDetails.invoice
97
+ }),
98
+ request,
99
+ network
100
+ );
101
+ if (request.methodDetails.paymentHash !== void 0 && request.methodDetails.paymentHash !== details.paymentHash)
102
+ throw new TypeError("Challenge payment hash does not match invoice");
103
+ if (request.methodDetails.network !== void 0 && request.methodDetails.network !== details.network)
104
+ throw new TypeError("Challenge network does not match invoice");
105
+ if (challenge.expires !== void 0 && Date.parse(challenge.expires) <= Date.now())
106
+ throw new TypeError("The challenge is expired");
107
+ } catch {
108
+ throw new PhoenixdPaymentError(
109
+ "invalid_invoice",
110
+ "The challenge contains an invalid, expired or mismatched invoice"
111
+ );
112
+ }
113
+ if (Number(request.amount) > maxAmountSat)
114
+ throw new PhoenixdPaymentError(
115
+ "amount_limit",
116
+ "The requested payment exceeds maxAmountSat"
117
+ );
118
+ let promise = pending.get(details.paymentHash);
119
+ if (!promise) {
120
+ promise = pay(request.methodDetails.invoice, details.paymentHash);
121
+ pending.set(details.paymentHash, promise);
122
+ }
123
+ let preimage;
124
+ try {
125
+ preimage = await promise;
126
+ } finally {
127
+ if (pending.get(details.paymentHash) === promise)
128
+ pending.delete(details.paymentHash);
129
+ }
130
+ return Credential.serialize({ challenge, payload: { preimage } });
131
+ }
132
+ });
133
+ }
134
+ export {
135
+ Mppx,
136
+ PhoenixdPaymentError,
137
+ charge2 as charge,
138
+ methods_exports as phoenixd
139
+ };
140
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/client/index.ts","../../src/client/methods.ts","../../src/client/charge.ts"],"sourcesContent":["export { Mppx } from \"mppx/client\";\nexport * as phoenixd from \"./methods\";\nexport { charge } from \"./charge\";\nexport type { ChargeOptions, ClientChargeMethod } from \"./charge\";\nexport { PhoenixdPaymentError } from \"../errors\";\n","export { charge } from \"./charge\";\n","import { Credential, Method } from \"mppx\";\nimport type { PhoenixdClient, PaymentResult } from \"@nodana/phoenixd-ts\";\nimport * as Methods from \"../Methods\";\nimport { PhoenixdPaymentError } from \"../errors\";\nimport { invoiceDetails, paymentHash } from \"../shared\";\n\nexport interface ChargeOptions {\n client: Pick<\n PhoenixdClient,\n \"decodeInvoice\" | \"payInvoice\" | \"getOutgoingPaymentByHash\"\n >;\n /** Maximum payment amount per challenge, in satoshis. */\n maxAmountSat: number;\n /** Expected invoice network. Defaults to mainnet. */\n network?: Methods.LightningNetwork;\n}\n\nexport type ClientChargeMethod = Method.Client<typeof Methods.charge>;\n\nexport function charge({\n client,\n maxAmountSat,\n network = \"mainnet\",\n}: ChargeOptions): ClientChargeMethod {\n if (!Number.isSafeInteger(maxAmountSat) || maxAmountSat < 1)\n throw new TypeError(\"maxAmountSat must be a positive integer\");\n\n const pending = new Map<string, Promise<string>>();\n\n const verifiedProof = (preimage: string, hash: string) => {\n let actual: string;\n\n try {\n actual = paymentHash(preimage);\n } catch {\n throw new PhoenixdPaymentError(\n \"payment_unknown\",\n \"Phoenixd returned an invalid payment proof\",\n );\n }\n\n if (actual !== hash)\n throw new PhoenixdPaymentError(\n \"payment_unknown\",\n \"Phoenixd returned a payment proof that does not match the invoice\",\n );\n\n return preimage;\n };\n\n async function pay(invoice: string, hash: string): Promise<string> {\n // Check history before sending, including after a previous uncertain result.\n const existing = await client.getOutgoingPaymentByHash(hash);\n\n if (existing?.isPaid) {\n if (!existing.preimage)\n throw new PhoenixdPaymentError(\n \"payment_unknown\",\n \"The invoice is paid but its proof is unavailable; check outgoing payment history\",\n );\n\n return verifiedProof(existing.preimage, hash);\n }\n\n if (existing && existing.completedAt == null)\n throw new PhoenixdPaymentError(\n \"payment_unknown\",\n \"A payment for this invoice is already pending; check outgoing payment history\",\n );\n\n let result: PaymentResult;\n\n try {\n result = await client.payInvoice({ invoice });\n } catch {\n try {\n const payment = await client.getOutgoingPaymentByHash(hash);\n\n if (payment?.isPaid && payment.preimage)\n return verifiedProof(payment.preimage, hash);\n } catch {\n /* The payment outcome remains unknown. */\n }\n\n throw new PhoenixdPaymentError(\n \"payment_unknown\",\n \"The payment outcome is unknown; check outgoing payment history before retrying\",\n );\n }\n\n if (result.type === \"payment_failed\")\n throw new PhoenixdPaymentError(\n \"payment_failed\",\n \"Phoenixd rejected the payment\",\n );\n\n if (result.paymentHash !== hash)\n throw new PhoenixdPaymentError(\n \"payment_unknown\",\n \"Phoenixd returned a different payment hash\",\n );\n\n return verifiedProof(result.paymentPreimage, hash);\n }\n\n return Method.toClient(Methods.charge, {\n async createCredential({ challenge }) {\n const request = Methods.charge.schema.request.parse(challenge.request);\n let details: ReturnType<typeof invoiceDetails>;\n\n try {\n details = invoiceDetails(\n await client.decodeInvoice({\n invoice: request.methodDetails.invoice,\n }),\n request,\n network,\n );\n\n if (\n request.methodDetails.paymentHash !== undefined &&\n request.methodDetails.paymentHash !== details.paymentHash\n )\n throw new TypeError(\"Challenge payment hash does not match invoice\");\n\n if (\n request.methodDetails.network !== undefined &&\n request.methodDetails.network !== details.network\n )\n throw new TypeError(\"Challenge network does not match invoice\");\n\n if (\n challenge.expires !== undefined &&\n Date.parse(challenge.expires) <= Date.now()\n )\n throw new TypeError(\"The challenge is expired\");\n } catch {\n throw new PhoenixdPaymentError(\n \"invalid_invoice\",\n \"The challenge contains an invalid, expired or mismatched invoice\",\n );\n }\n\n if (Number(request.amount) > maxAmountSat)\n throw new PhoenixdPaymentError(\n \"amount_limit\",\n \"The requested payment exceeds maxAmountSat\",\n );\n\n let promise = pending.get(details.paymentHash);\n\n if (!promise) {\n promise = pay(request.methodDetails.invoice, details.paymentHash);\n pending.set(details.paymentHash, promise);\n }\n\n let preimage: string;\n\n try {\n preimage = await promise;\n } finally {\n if (pending.get(details.paymentHash) === promise)\n pending.delete(details.paymentHash);\n }\n\n return Credential.serialize({ challenge, payload: { preimage } });\n },\n });\n}\n"],"mappings":"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","names":["charge","charge"]}
@@ -0,0 +1,54 @@
1
+ import { z, Errors } from 'mppx';
2
+
3
+ declare const charge: {
4
+ readonly name: "lightning";
5
+ readonly intent: "charge";
6
+ readonly schema: {
7
+ readonly credential: {
8
+ readonly payload: z.ZodMiniObject<{
9
+ preimage: z.ZodMiniString<string>;
10
+ }, z.core.$strip>;
11
+ };
12
+ readonly request: z.ZodMiniObject<{
13
+ amount: z.ZodMiniString<string>;
14
+ currency: z.ZodMiniLiteral<"sat">;
15
+ description: z.ZodMiniOptional<z.ZodMiniString<string>>;
16
+ methodDetails: z.ZodMiniObject<{
17
+ invoice: z.ZodMiniString<string>;
18
+ paymentHash: z.ZodMiniOptional<z.ZodMiniString<string>>;
19
+ network: z.ZodMiniOptional<z.ZodMiniEnum<{
20
+ mainnet: "mainnet";
21
+ regtest: "regtest";
22
+ signet: "signet";
23
+ }>>;
24
+ }, z.core.$strip>;
25
+ }, z.core.$strip>;
26
+ };
27
+ };
28
+ type ChargeRequest = z.output<typeof charge.schema.request>;
29
+ type LightningNetwork = NonNullable<ChargeRequest["methodDetails"]["network"]>;
30
+
31
+ type Methods_ChargeRequest = ChargeRequest;
32
+ type Methods_LightningNetwork = LightningNetwork;
33
+ declare const Methods_charge: typeof charge;
34
+ declare namespace Methods {
35
+ export { type Methods_ChargeRequest as ChargeRequest, type Methods_LightningNetwork as LightningNetwork, Methods_charge as charge };
36
+ }
37
+
38
+ type ChargeErrorCode = "unknown_challenge" | "invalid_challenge" | "invalid_preimage" | "invoice_expired" | "already_consumed" | "invalid_invoice" | "store_unavailable";
39
+ declare class ChargeError extends Errors.PaymentError {
40
+ readonly code: ChargeErrorCode;
41
+ readonly name = "ChargeError";
42
+ readonly title = "Lightning payment rejected";
43
+ readonly type: string;
44
+ readonly status: number;
45
+ constructor(code: ChargeErrorCode, message: string);
46
+ }
47
+ type ClientErrorCode = "invalid_invoice" | "amount_limit" | "payment_failed" | "payment_unknown";
48
+ declare class PhoenixdPaymentError extends Error {
49
+ readonly code: ClientErrorCode;
50
+ readonly name = "PhoenixdPaymentError";
51
+ constructor(code: ClientErrorCode, message: string);
52
+ }
53
+
54
+ export { ChargeError as C, type LightningNetwork as L, Methods as M, PhoenixdPaymentError as P, type ChargeErrorCode as a, type ChargeRequest as b, charge as c, type ClientErrorCode as d };
@@ -0,0 +1,54 @@
1
+ import { z, Errors } from 'mppx';
2
+
3
+ declare const charge: {
4
+ readonly name: "lightning";
5
+ readonly intent: "charge";
6
+ readonly schema: {
7
+ readonly credential: {
8
+ readonly payload: z.ZodMiniObject<{
9
+ preimage: z.ZodMiniString<string>;
10
+ }, z.core.$strip>;
11
+ };
12
+ readonly request: z.ZodMiniObject<{
13
+ amount: z.ZodMiniString<string>;
14
+ currency: z.ZodMiniLiteral<"sat">;
15
+ description: z.ZodMiniOptional<z.ZodMiniString<string>>;
16
+ methodDetails: z.ZodMiniObject<{
17
+ invoice: z.ZodMiniString<string>;
18
+ paymentHash: z.ZodMiniOptional<z.ZodMiniString<string>>;
19
+ network: z.ZodMiniOptional<z.ZodMiniEnum<{
20
+ mainnet: "mainnet";
21
+ regtest: "regtest";
22
+ signet: "signet";
23
+ }>>;
24
+ }, z.core.$strip>;
25
+ }, z.core.$strip>;
26
+ };
27
+ };
28
+ type ChargeRequest = z.output<typeof charge.schema.request>;
29
+ type LightningNetwork = NonNullable<ChargeRequest["methodDetails"]["network"]>;
30
+
31
+ type Methods_ChargeRequest = ChargeRequest;
32
+ type Methods_LightningNetwork = LightningNetwork;
33
+ declare const Methods_charge: typeof charge;
34
+ declare namespace Methods {
35
+ export { type Methods_ChargeRequest as ChargeRequest, type Methods_LightningNetwork as LightningNetwork, Methods_charge as charge };
36
+ }
37
+
38
+ type ChargeErrorCode = "unknown_challenge" | "invalid_challenge" | "invalid_preimage" | "invoice_expired" | "already_consumed" | "invalid_invoice" | "store_unavailable";
39
+ declare class ChargeError extends Errors.PaymentError {
40
+ readonly code: ChargeErrorCode;
41
+ readonly name = "ChargeError";
42
+ readonly title = "Lightning payment rejected";
43
+ readonly type: string;
44
+ readonly status: number;
45
+ constructor(code: ChargeErrorCode, message: string);
46
+ }
47
+ type ClientErrorCode = "invalid_invoice" | "amount_limit" | "payment_failed" | "payment_unknown";
48
+ declare class PhoenixdPaymentError extends Error {
49
+ readonly code: ClientErrorCode;
50
+ readonly name = "PhoenixdPaymentError";
51
+ constructor(code: ClientErrorCode, message: string);
52
+ }
53
+
54
+ export { ChargeError as C, type LightningNetwork as L, Methods as M, PhoenixdPaymentError as P, type ChargeErrorCode as a, type ChargeRequest as b, charge as c, type ClientErrorCode as d };
package/dist/index.cjs ADDED
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ChargeError: () => ChargeError,
24
+ Methods: () => Methods_exports,
25
+ PhoenixdPaymentError: () => PhoenixdPaymentError
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/Methods.ts
30
+ var Methods_exports = {};
31
+ __export(Methods_exports, {
32
+ charge: () => charge
33
+ });
34
+ var import_mppx = require("mppx");
35
+ var charge = import_mppx.Method.from({
36
+ name: "lightning",
37
+ intent: "charge",
38
+ schema: {
39
+ credential: {
40
+ payload: import_mppx.z.object({
41
+ preimage: import_mppx.z.string().check(import_mppx.z.regex(/^[0-9a-f]{64}$/))
42
+ })
43
+ },
44
+ request: import_mppx.z.object({
45
+ amount: import_mppx.z.string().check(import_mppx.z.regex(/^[1-9][0-9]*$/)),
46
+ currency: import_mppx.z.literal("sat"),
47
+ description: import_mppx.z.optional(import_mppx.z.string()),
48
+ methodDetails: import_mppx.z.object({
49
+ invoice: import_mppx.z.string(),
50
+ paymentHash: import_mppx.z.optional(import_mppx.z.string().check(import_mppx.z.regex(/^[0-9a-f]{64}$/))),
51
+ network: import_mppx.z.optional(import_mppx.z.enum(["mainnet", "regtest", "signet"]))
52
+ })
53
+ })
54
+ }
55
+ });
56
+
57
+ // src/errors.ts
58
+ var import_mppx2 = require("mppx");
59
+ var ChargeError = class extends import_mppx2.Errors.PaymentError {
60
+ constructor(code, message) {
61
+ super(message);
62
+ this.code = code;
63
+ const problem = code === "store_unavailable" || code === "invalid_invoice" ? "internal-payment-error" : code === "invoice_expired" ? "payment-expired" : code === "invalid_preimage" || code === "already_consumed" ? "verification-failed" : "invalid-challenge";
64
+ this.type = `https://paymentauth.org/problems/${problem}`;
65
+ this.status = code === "store_unavailable" ? 503 : code === "invalid_invoice" ? 502 : 402;
66
+ }
67
+ code;
68
+ name = "ChargeError";
69
+ title = "Lightning payment rejected";
70
+ type;
71
+ status;
72
+ };
73
+ var PhoenixdPaymentError = class extends Error {
74
+ constructor(code, message) {
75
+ super(message);
76
+ this.code = code;
77
+ }
78
+ code;
79
+ name = "PhoenixdPaymentError";
80
+ };
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ ChargeError,
84
+ Methods,
85
+ PhoenixdPaymentError
86
+ });
87
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/Methods.ts","../src/errors.ts"],"sourcesContent":["export * as Methods from \"./Methods\";\nexport type { ChargeRequest, LightningNetwork } from \"./Methods\";\nexport { ChargeError, PhoenixdPaymentError } from \"./errors\";\nexport type { ChargeErrorCode, ClientErrorCode } from \"./errors\";\n","// Lightning wire schema adapted from buildonspark/lightning-mpp-sdk (MIT).\n// Copyright (c) 2026 Lightspark Group, Inc. See NOTICE.\nimport { Method, z } from \"mppx\";\n\nexport const charge = Method.from({\n name: \"lightning\",\n intent: \"charge\",\n schema: {\n credential: {\n payload: z.object({\n preimage: z.string().check(z.regex(/^[0-9a-f]{64}$/)),\n }),\n },\n request: z.object({\n amount: z.string().check(z.regex(/^[1-9][0-9]*$/)),\n currency: z.literal(\"sat\"),\n description: z.optional(z.string()),\n methodDetails: z.object({\n invoice: z.string(),\n paymentHash: z.optional(z.string().check(z.regex(/^[0-9a-f]{64}$/))),\n network: z.optional(z.enum([\"mainnet\", \"regtest\", \"signet\"])),\n }),\n }),\n },\n});\n\nexport type ChargeRequest = z.output<typeof charge.schema.request>;\n\nexport type LightningNetwork = NonNullable<\n ChargeRequest[\"methodDetails\"][\"network\"]\n>;\n","import { Errors } from \"mppx\";\n\nexport type ChargeErrorCode =\n | \"unknown_challenge\"\n | \"invalid_challenge\"\n | \"invalid_preimage\"\n | \"invoice_expired\"\n | \"already_consumed\"\n | \"invalid_invoice\"\n | \"store_unavailable\";\n\nexport class ChargeError extends Errors.PaymentError {\n readonly name = \"ChargeError\";\n readonly title = \"Lightning payment rejected\";\n readonly type: string;\n override readonly status: number;\n\n constructor(\n public readonly code: ChargeErrorCode,\n message: string,\n ) {\n super(message);\n\n const problem =\n code === \"store_unavailable\" || code === \"invalid_invoice\"\n ? \"internal-payment-error\"\n : code === \"invoice_expired\"\n ? \"payment-expired\"\n : code === \"invalid_preimage\" || code === \"already_consumed\"\n ? \"verification-failed\"\n : \"invalid-challenge\";\n\n this.type = `https://paymentauth.org/problems/${problem}`;\n this.status =\n code === \"store_unavailable\"\n ? 503\n : code === \"invalid_invoice\"\n ? 502\n : 402;\n }\n}\n\nexport type ClientErrorCode =\n \"invalid_invoice\" | \"amount_limit\" | \"payment_failed\" | \"payment_unknown\";\n\nexport class PhoenixdPaymentError extends Error {\n readonly name = \"PhoenixdPaymentError\";\n\n constructor(\n public readonly code: ClientErrorCode,\n message: string,\n ) {\n super(message);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAEA,kBAA0B;AAEnB,IAAM,SAAS,mBAAO,KAAK;AAAA,EAChC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,QAAQ;AAAA,IACN,YAAY;AAAA,MACV,SAAS,cAAE,OAAO;AAAA,QAChB,UAAU,cAAE,OAAO,EAAE,MAAM,cAAE,MAAM,gBAAgB,CAAC;AAAA,MACtD,CAAC;AAAA,IACH;AAAA,IACA,SAAS,cAAE,OAAO;AAAA,MAChB,QAAQ,cAAE,OAAO,EAAE,MAAM,cAAE,MAAM,eAAe,CAAC;AAAA,MACjD,UAAU,cAAE,QAAQ,KAAK;AAAA,MACzB,aAAa,cAAE,SAAS,cAAE,OAAO,CAAC;AAAA,MAClC,eAAe,cAAE,OAAO;AAAA,QACtB,SAAS,cAAE,OAAO;AAAA,QAClB,aAAa,cAAE,SAAS,cAAE,OAAO,EAAE,MAAM,cAAE,MAAM,gBAAgB,CAAC,CAAC;AAAA,QACnE,SAAS,cAAE,SAAS,cAAE,KAAK,CAAC,WAAW,WAAW,QAAQ,CAAC,CAAC;AAAA,MAC9D,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF,CAAC;;;ACxBD,IAAAA,eAAuB;AAWhB,IAAM,cAAN,cAA0B,oBAAO,aAAa;AAAA,EAMnD,YACkB,MAChB,SACA;AACA,UAAM,OAAO;AAHG;AAKhB,UAAM,UACJ,SAAS,uBAAuB,SAAS,oBACrC,2BACA,SAAS,oBACP,oBACA,SAAS,sBAAsB,SAAS,qBACtC,wBACA;AAEV,SAAK,OAAO,oCAAoC,OAAO;AACvD,SAAK,SACH,SAAS,sBACL,MACA,SAAS,oBACP,MACA;AAAA,EACV;AAAA,EArBkB;AAAA,EANT,OAAO;AAAA,EACP,QAAQ;AAAA,EACR;AAAA,EACS;AAyBpB;AAKO,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAG9C,YACkB,MAChB,SACA;AACA,UAAM,OAAO;AAHG;AAAA,EAIlB;AAAA,EAJkB;AAAA,EAHT,OAAO;AAQlB;","names":["import_mppx"]}
@@ -0,0 +1,2 @@
1
+ export { C as ChargeError, a as ChargeErrorCode, b as ChargeRequest, d as ClientErrorCode, L as LightningNetwork, M as Methods, P as PhoenixdPaymentError } from './index-3V4vmG3I.cjs';
2
+ import 'mppx';
@@ -0,0 +1,2 @@
1
+ export { C as ChargeError, a as ChargeErrorCode, b as ChargeRequest, d as ClientErrorCode, L as LightningNetwork, M as Methods, P as PhoenixdPaymentError } from './index-3V4vmG3I.js';
2
+ import 'mppx';
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import {
2
+ ChargeError,
3
+ Methods_exports,
4
+ PhoenixdPaymentError
5
+ } from "./chunk-EQRLZHAJ.js";
6
+ export {
7
+ ChargeError,
8
+ Methods_exports as Methods,
9
+ PhoenixdPaymentError
10
+ };
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}