@profullstack/coinpay 0.6.8 → 0.6.11

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,218 @@
1
+ /**
2
+ * Lightning Module for CoinPay SDK
3
+ *
4
+ * Manages Lightning Network operations: wallet provisioning (via LNbits),
5
+ * lightning address registration, invoice creation, payment sending, and history.
6
+ *
7
+ * Lightning wallets are custodial — funds are held on CoinPay's server.
8
+ */
9
+
10
+ /**
11
+ * Lightning client for LN operations
12
+ */
13
+ export class LightningClient {
14
+ #client;
15
+
16
+ /**
17
+ * @param {import('./client.js').CoinPayClient} client - Parent CoinPay client
18
+ */
19
+ constructor(client) {
20
+ this.#client = client;
21
+ }
22
+
23
+ // ──────────────────────────────────────────────
24
+ // Nodes / Wallet Provisioning
25
+ // ──────────────────────────────────────────────
26
+
27
+ /**
28
+ * Enable Lightning for a wallet (provisions an LNbits custodial wallet).
29
+ * @param {Object} params
30
+ * @param {string} params.wallet_id - Wallet UUID
31
+ * @param {string} params.mnemonic - BIP39 mnemonic
32
+ * @param {string} [params.business_id] - Optional business UUID
33
+ * @returns {Promise<Object>} The provisioned node record
34
+ */
35
+ async enableWallet({ wallet_id, mnemonic, business_id }) {
36
+ return this.#client.request('/lightning/nodes', {
37
+ method: 'POST',
38
+ body: JSON.stringify({ wallet_id, mnemonic, business_id }),
39
+ });
40
+ }
41
+
42
+ /**
43
+ * @deprecated Use enableWallet() instead
44
+ */
45
+ async provisionNode(params) {
46
+ return this.enableWallet(params);
47
+ }
48
+
49
+ /**
50
+ * Get node status by ID.
51
+ * @param {string} nodeId
52
+ * @returns {Promise<Object>}
53
+ */
54
+ async getNode(nodeId) {
55
+ return this.#client.request(`/lightning/nodes/${nodeId}`);
56
+ }
57
+
58
+ /**
59
+ * Get node by wallet ID.
60
+ * @param {string} walletId - Wallet UUID
61
+ * @returns {Promise<Object>}
62
+ */
63
+ async getNodeByWallet(walletId) {
64
+ return this.#client.request(`/lightning/nodes?wallet_id=${walletId}`);
65
+ }
66
+
67
+ // ──────────────────────────────────────────────
68
+ // Lightning Address
69
+ // ──────────────────────────────────────────────
70
+
71
+ /**
72
+ * Register a Lightning Address (username@coinpayportal.com).
73
+ * Requires Lightning to be enabled first.
74
+ * @param {Object} params
75
+ * @param {string} params.wallet_id - Wallet UUID
76
+ * @param {string} params.username - Desired username (3-32 chars, lowercase alphanumeric)
77
+ * @returns {Promise<Object>} { lightning_address, username }
78
+ */
79
+ async registerAddress({ wallet_id, username }) {
80
+ return this.#client.request('/lightning/address', {
81
+ method: 'POST',
82
+ body: JSON.stringify({ wallet_id, username }),
83
+ });
84
+ }
85
+
86
+ /**
87
+ * Get Lightning Address for a wallet.
88
+ * @param {string} walletId - Wallet UUID
89
+ * @returns {Promise<Object>} { lightning_address, username } or { lightning_address: null }
90
+ */
91
+ async getAddress(walletId) {
92
+ return this.#client.request(`/lightning/address?wallet_id=${walletId}`);
93
+ }
94
+
95
+ /**
96
+ * Check if a Lightning Address username is available.
97
+ * @param {string} username
98
+ * @returns {Promise<Object>} { available: boolean }
99
+ */
100
+ async checkAddressAvailable(username) {
101
+ return this.#client.request(`/lightning/address?username=${encodeURIComponent(username)}`);
102
+ }
103
+
104
+ // ──────────────────────────────────────────────
105
+ // Invoices
106
+ // ──────────────────────────────────────────────
107
+
108
+ /**
109
+ * Create a BOLT11 invoice to receive a payment.
110
+ * @param {Object} params
111
+ * @param {string} params.wallet_id - Wallet UUID
112
+ * @param {number} params.amount_sats - Amount in satoshis
113
+ * @param {string} [params.description] - Invoice description/memo
114
+ * @returns {Promise<Object>} { payment_request, payment_hash, ... }
115
+ */
116
+ async createInvoice({ wallet_id, amount_sats, description }) {
117
+ return this.#client.request('/lightning/invoices', {
118
+ method: 'POST',
119
+ body: JSON.stringify({ wallet_id, amount_sats, description }),
120
+ });
121
+ }
122
+
123
+ // ──────────────────────────────────────────────
124
+ // Offers (BOLT12) — currently not supported
125
+ // ──────────────────────────────────────────────
126
+
127
+ /**
128
+ * Create a BOLT12 offer.
129
+ * @deprecated BOLT12 offers are not currently supported (LNbits limitation).
130
+ * Use createInvoice() or registerAddress() instead.
131
+ */
132
+ async createOffer(params) {
133
+ return this.#client.request('/lightning/offers', {
134
+ method: 'POST',
135
+ body: JSON.stringify(params),
136
+ });
137
+ }
138
+
139
+ /**
140
+ * Get offer by ID.
141
+ * @param {string} offerId
142
+ * @returns {Promise<Object>}
143
+ */
144
+ async getOffer(offerId) {
145
+ return this.#client.request(`/lightning/offers/${offerId}`);
146
+ }
147
+
148
+ /**
149
+ * List offers with optional filters.
150
+ * @param {Object} [params]
151
+ * @param {string} [params.business_id]
152
+ * @param {string} [params.node_id]
153
+ * @param {string} [params.status]
154
+ * @param {number} [params.limit]
155
+ * @param {number} [params.offset]
156
+ * @returns {Promise<Object>} { offers, total, limit, offset }
157
+ */
158
+ async listOffers(params = {}) {
159
+ const qs = new URLSearchParams();
160
+ for (const [k, v] of Object.entries(params)) {
161
+ if (v !== undefined && v !== null) qs.set(k, String(v));
162
+ }
163
+ return this.#client.request(`/lightning/offers?${qs}`);
164
+ }
165
+
166
+ // ──────────────────────────────────────────────
167
+ // Payments
168
+ // ──────────────────────────────────────────────
169
+
170
+ /**
171
+ * Send a Lightning payment. Accepts:
172
+ * - BOLT11 invoice string
173
+ * - Lightning address (user@domain)
174
+ * @param {Object} params
175
+ * @param {string} params.wallet_id - Wallet UUID
176
+ * @param {string} params.destination - BOLT11 invoice or lightning address (user@domain)
177
+ * @param {number} [params.amount_sats] - Amount in satoshis (required for lightning addresses, optional for invoices with amount)
178
+ * @returns {Promise<Object>} { payment_hash, status }
179
+ */
180
+ async sendPayment({ wallet_id, destination, amount_sats }) {
181
+ return this.#client.request('/lightning/payments', {
182
+ method: 'POST',
183
+ body: JSON.stringify({ wallet_id, bolt12: destination, amount_sats }),
184
+ });
185
+ }
186
+
187
+ /**
188
+ * List Lightning payments.
189
+ * @param {Object} [params]
190
+ * @param {string} [params.wallet_id]
191
+ * @param {string} [params.business_id]
192
+ * @param {string} [params.node_id]
193
+ * @param {string} [params.offer_id]
194
+ * @param {string} [params.direction] - "incoming" | "outgoing"
195
+ * @param {string} [params.status] - "pending" | "settled" | "failed"
196
+ * @param {number} [params.limit] - Default 50
197
+ * @param {number} [params.offset] - Default 0
198
+ * @returns {Promise<Object>} { payments, total, limit, offset }
199
+ */
200
+ async listPayments(params = {}) {
201
+ const qs = new URLSearchParams();
202
+ for (const [k, v] of Object.entries(params)) {
203
+ if (v !== undefined && v !== null) qs.set(k, String(v));
204
+ }
205
+ return this.#client.request(`/lightning/payments?${qs}`);
206
+ }
207
+
208
+ /**
209
+ * Get payment status by payment hash.
210
+ * @param {string} paymentHash
211
+ * @returns {Promise<Object>}
212
+ */
213
+ async getPayment(paymentHash) {
214
+ return this.#client.request(`/lightning/payments/${paymentHash}`);
215
+ }
216
+ }
217
+
218
+ export default LightningClient;