thunder-bridge 0.8.1 → 0.8.3
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/README.md +37 -13
- package/dist/index.cjs +247 -360
- package/dist/index.d.cts +20 -323
- package/dist/index.d.ts +20 -323
- package/dist/index.js +243 -359
- package/dist/rail-Cyc5zTtA.d.cts +375 -0
- package/dist/rail-Cyc5zTtA.d.ts +375 -0
- package/dist/server.cjs +805 -0
- package/dist/server.d.cts +64 -0
- package/dist/server.d.ts +64 -0
- package/dist/server.js +767 -0
- package/package.json +13 -4
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/** Where a payment stands, `paid` is the only status that carries a preimage */
|
|
2
|
+
type PaymentStatus = "pending" | "paid" | "expired";
|
|
3
|
+
/** A payment as the gateway reports it, every field is checkable against the recipient */
|
|
4
|
+
interface Payment {
|
|
5
|
+
id: string;
|
|
6
|
+
lnAddress: string;
|
|
7
|
+
amountMsat: number;
|
|
8
|
+
status: PaymentStatus;
|
|
9
|
+
paymentHash: string;
|
|
10
|
+
bolt11: string;
|
|
11
|
+
preimage: string | null;
|
|
12
|
+
expiresAt: number;
|
|
13
|
+
createdAt: number;
|
|
14
|
+
verifyUrl: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* `lnAddresses` is a priority list, the gateway takes the first one that can
|
|
18
|
+
* issue a provable invoice for `amountMsat` and the rest are the fallback
|
|
19
|
+
*/
|
|
20
|
+
interface CreatePaymentParams {
|
|
21
|
+
lnAddresses: string[];
|
|
22
|
+
amountMsat: number;
|
|
23
|
+
webhookUrl?: string;
|
|
24
|
+
webhookSecret?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* `lnAddresses` is the same priority list `createPayment` takes, and quoting it
|
|
28
|
+
* mints nothing and charges the recipient's wallet nothing
|
|
29
|
+
*/
|
|
30
|
+
interface CreateQuoteParams {
|
|
31
|
+
lnAddresses: string[];
|
|
32
|
+
amountMsat: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Which address would serve an amount, and what the ones ahead of it refused.
|
|
36
|
+
* `feeMsat` is always zero, the payer pays the recipient's own invoice and the
|
|
37
|
+
* gateway is never in the money's path
|
|
38
|
+
*/
|
|
39
|
+
interface Quote {
|
|
40
|
+
lnAddress: string;
|
|
41
|
+
amountMsat: number;
|
|
42
|
+
feeMsat: number;
|
|
43
|
+
minMsat: number;
|
|
44
|
+
maxMsat: number;
|
|
45
|
+
metadata: string;
|
|
46
|
+
refusals: WalletFailure[];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* What the gateway reports about a payment it is watching, for a trigger stream
|
|
50
|
+
* or for one you minted yourself. `lnAddress` and `amountMsat` are null when the
|
|
51
|
+
* gateway was never told them, which is the point of `watchPayment`: what the
|
|
52
|
+
* watcher needs but the gateway should not know travels in `sealed` instead
|
|
53
|
+
*/
|
|
54
|
+
interface TriggerEvent {
|
|
55
|
+
id: string;
|
|
56
|
+
paymentHash: string;
|
|
57
|
+
verifyUrl: string;
|
|
58
|
+
status: PaymentStatus;
|
|
59
|
+
preimage: string | null;
|
|
60
|
+
expiresAt: number;
|
|
61
|
+
createdAt: number;
|
|
62
|
+
sealed: string | null;
|
|
63
|
+
lnAddress: string | null;
|
|
64
|
+
amountMsat: number | null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* An invoice you obtained yourself, handed over to be watched. The gateway is
|
|
68
|
+
* given no address and no amount, so it cannot refuse one recipient rather than
|
|
69
|
+
* all of them
|
|
70
|
+
*/
|
|
71
|
+
interface WatchPaymentParams {
|
|
72
|
+
paymentHash: string;
|
|
73
|
+
verifyUrl: string;
|
|
74
|
+
expiresAt: number;
|
|
75
|
+
trigger?: string;
|
|
76
|
+
sealed?: string;
|
|
77
|
+
}
|
|
78
|
+
/** Why one wallet in the list could not be used */
|
|
79
|
+
type WalletReason = "address-unusable" | "unreachable" | "amount-not-accepted" | "cannot-prove-delivery" | "invoice-refused";
|
|
80
|
+
interface WalletFailure {
|
|
81
|
+
address: string;
|
|
82
|
+
reason: WalletReason;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface ThunderBridgeOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Prove every payment against the recipient's own server before handing it
|
|
88
|
+
* back, and refuse a reported settlement whose preimage does not hash to the
|
|
89
|
+
* payment hash, defaults to true
|
|
90
|
+
*/
|
|
91
|
+
verify?: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Sent as `Authorization: Bearer`, which a gateway started with
|
|
94
|
+
* `GATEWAY_TOKEN` requires on every call, the socket handshake included. No
|
|
95
|
+
* browser WebSocket can carry a header, so setting this also puts every socket
|
|
96
|
+
* through a ticket
|
|
97
|
+
*/
|
|
98
|
+
token?: string;
|
|
99
|
+
}
|
|
100
|
+
interface WaitOptions {
|
|
101
|
+
/** Give up when this aborts, `AbortSignal.timeout(ms)` covers the usual case */
|
|
102
|
+
signal?: AbortSignal;
|
|
103
|
+
/**
|
|
104
|
+
* Mint a short-lived ticket and put that in the socket URL instead of the
|
|
105
|
+
* payment id. Implied by `token`. The id stays readable inside the ticket,
|
|
106
|
+
* what changes is that a URL out of a log stops opening anything after a
|
|
107
|
+
* minute
|
|
108
|
+
*/
|
|
109
|
+
tickets?: boolean;
|
|
110
|
+
}
|
|
111
|
+
interface CreateOptions {
|
|
112
|
+
/**
|
|
113
|
+
* Makes the POST safe to retry. A repeat of a finished request replays its
|
|
114
|
+
* payment instead of asking a wallet for a second invoice, a repeat that
|
|
115
|
+
* arrives while the first is still resolving throws
|
|
116
|
+
* `IdempotencyConflictError`, and the key is held for 24 hours
|
|
117
|
+
*/
|
|
118
|
+
idempotencyKey?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Groups this payment with every other one carrying the same secret, so
|
|
121
|
+
* `followTrigger` can watch the place rather than the payment. Registering
|
|
122
|
+
* sends only its sha256, which is also all the gateway stores, so a stolen
|
|
123
|
+
* ledger cannot subscribe. Following sends the secret itself, because the
|
|
124
|
+
* gateway hashes what it is given to find the stream, so the operator of a
|
|
125
|
+
* gateway you do not own learns it the first time you connect. Keep it apart
|
|
126
|
+
* from any URL a payer sees
|
|
127
|
+
*/
|
|
128
|
+
trigger?: string;
|
|
129
|
+
}
|
|
130
|
+
interface FollowOptions {
|
|
131
|
+
/** Called for the recent settlements replayed on connect, then for each new one */
|
|
132
|
+
onPayment: (settled: TriggerEvent) => void;
|
|
133
|
+
/** Called when a connection drops or a frame is refused, the follow keeps going */
|
|
134
|
+
onError?: (error: unknown) => void;
|
|
135
|
+
/** Reconnect after a drop, defaults to true */
|
|
136
|
+
reconnect?: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* The first wait after a drop, doubling up to 30 seconds and jittered so a
|
|
139
|
+
* fleet does not come back in lockstep, defaults to 3000. A connection that
|
|
140
|
+
* opens puts it back to the first wait
|
|
141
|
+
*/
|
|
142
|
+
reconnectDelayMs?: number;
|
|
143
|
+
/**
|
|
144
|
+
* Mint a short-lived ticket and put that in the socket URL instead of the
|
|
145
|
+
* secret, one per connection. Keeps the secret out of access logs, at the cost
|
|
146
|
+
* of a POST before each connect. Implied by `token`. Leave it off for a
|
|
147
|
+
* microcontroller, where one hardcoded URL and a dumb reconnect loop is the
|
|
148
|
+
* whole point
|
|
149
|
+
*/
|
|
150
|
+
tickets?: boolean;
|
|
151
|
+
}
|
|
152
|
+
/** Talks to a Thunder Bridge gateway and trusts it for nothing it can check itself */
|
|
153
|
+
declare class ThunderBridge {
|
|
154
|
+
private readonly baseUrl;
|
|
155
|
+
private readonly verify;
|
|
156
|
+
private readonly token;
|
|
157
|
+
private strangers;
|
|
158
|
+
constructor(baseUrl: string, options?: ThunderBridgeOptions);
|
|
159
|
+
/**
|
|
160
|
+
* Whether a token was given, which is what makes an instance yours: a gateway
|
|
161
|
+
* started with `GATEWAY_TOKEN` answers nobody else, so anything you hand it
|
|
162
|
+
* stays between you and it
|
|
163
|
+
*/
|
|
164
|
+
get isPrivate(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Whether the gateway turns away a caller carrying no token, asked by making
|
|
167
|
+
* one unauthenticated read it would have to refuse. `isPrivate` answers only
|
|
168
|
+
* whether you configured a token, which is your side of the arrangement and
|
|
169
|
+
* says nothing about the gateway's, so a made-up token against a public
|
|
170
|
+
* instance reads as private and is not. Asked once and remembered, because an
|
|
171
|
+
* instance does not change its mind. Anything other than a refusal counts as
|
|
172
|
+
* open, so an unreachable gateway fails closed
|
|
173
|
+
*/
|
|
174
|
+
refusesStrangers(): Promise<boolean>;
|
|
175
|
+
/**
|
|
176
|
+
* Ask the gateway for an invoice payable to the first address on your list
|
|
177
|
+
* that can issue a provable one, throws `NoWalletAvailableError` when none can
|
|
178
|
+
* and `GatewayCheatError` when what comes back is not what you asked for
|
|
179
|
+
*/
|
|
180
|
+
createPayment(params: CreatePaymentParams, options?: CreateOptions): Promise<Payment>;
|
|
181
|
+
/**
|
|
182
|
+
* Ask which address would serve an amount without minting anything, throws
|
|
183
|
+
* `NoWalletAvailableError` when none would. A quote is a probe and not a
|
|
184
|
+
* promise: the address it names can still be refused at create time, because
|
|
185
|
+
* whether a wallet returns a provable invoice cannot be known without asking
|
|
186
|
+
* it for one, and asking mints it
|
|
187
|
+
*/
|
|
188
|
+
createQuote(params: CreateQuoteParams): Promise<Quote>;
|
|
189
|
+
/** Read a payment back, null when the gateway has never heard of it */
|
|
190
|
+
getPayment(id: string): Promise<Payment | null>;
|
|
191
|
+
/**
|
|
192
|
+
* Read back a payment the gateway is only watching, null when it has never
|
|
193
|
+
* heard of it. A watched payment carries no address, amount or invoice, so
|
|
194
|
+
* `getPayment` refuses it and this reads the shape both rails share
|
|
195
|
+
*/
|
|
196
|
+
getWatched(id: string): Promise<TriggerEvent | null>;
|
|
197
|
+
/**
|
|
198
|
+
* List what this gateway is watching, newest first. Only a gateway started
|
|
199
|
+
* with `GATEWAY_TOKEN` serves this, because on a shared one it would hand
|
|
200
|
+
* every caller everyone else's payments, so a public gateway answers 404.
|
|
201
|
+
*
|
|
202
|
+
* `scanned` says how many settled records were looked at to build the page.
|
|
203
|
+
* Anything older than that window is not in the answer, and the list does not
|
|
204
|
+
* pretend otherwise
|
|
205
|
+
*/
|
|
206
|
+
listPayments(limit?: number): Promise<{
|
|
207
|
+
payments: TriggerEvent[];
|
|
208
|
+
scanned: number;
|
|
209
|
+
}>;
|
|
210
|
+
/**
|
|
211
|
+
* Follow a payment over WebSocket until it is paid or expired, reconnecting
|
|
212
|
+
* through a drop. A payment that never answers gives up after a few tries, and
|
|
213
|
+
* one that has answered is followed until its own expiry, so the wait always
|
|
214
|
+
* ends by itself
|
|
215
|
+
*/
|
|
216
|
+
waitForPayment(id: string, options?: WaitOptions): Promise<Payment>;
|
|
217
|
+
/**
|
|
218
|
+
* Follow a payment the gateway is only watching, one it did not mint, until it
|
|
219
|
+
* is paid or expired.
|
|
220
|
+
*
|
|
221
|
+
* A watched payment carries no address, no amount and no invoice, because the
|
|
222
|
+
* gateway was told none of them, so it reads back as the shape a trigger
|
|
223
|
+
* streams rather than as a `Payment`. That is every bank transfer, and every
|
|
224
|
+
* Lightning invoice registered with `watchPayment` instead of `createPayment`.
|
|
225
|
+
*/
|
|
226
|
+
waitForWatched(id: string, options?: WaitOptions): Promise<TriggerEvent>;
|
|
227
|
+
private followed;
|
|
228
|
+
/**
|
|
229
|
+
* Wait on several payments and keep the first one that is really paid, then stop
|
|
230
|
+
* waiting on the losers, which closes their sockets.
|
|
231
|
+
*
|
|
232
|
+
* This is how one order offers two rails. A Lightning invoice and a bank
|
|
233
|
+
* transfer for the same thing are two payments here, and the payer picks one, so
|
|
234
|
+
* what you want is the one that arrives and nothing further from the other.
|
|
235
|
+
*
|
|
236
|
+
* A leg that expires is a loser, not a winner, which is the whole reason this is
|
|
237
|
+
* not a race: `waitForPayment` ends on `paid` and on `expired` alike, and a
|
|
238
|
+
* Lightning invoice expires in an hour while a bank transfer takes days. `null`
|
|
239
|
+
* means every leg ended without being paid.
|
|
240
|
+
*
|
|
241
|
+
* Stopping the wait is not revoking the invoice. Nobody can revoke one, because
|
|
242
|
+
* the recipient's own wallet minted it, so a payer who pays the loser afterwards
|
|
243
|
+
* really does pay twice and that shows up on `followTrigger` as a second
|
|
244
|
+
* settlement to refund.
|
|
245
|
+
*/
|
|
246
|
+
firstToSettle(ids: string[], options?: WaitOptions): Promise<TriggerEvent | null>;
|
|
247
|
+
/**
|
|
248
|
+
* Hand over an invoice you obtained yourself so the gateway watches it without
|
|
249
|
+
* being told the address or the amount. It can then only refuse everyone
|
|
250
|
+
* rather than one recipient, which is what makes leaving it cheap. Anything
|
|
251
|
+
* the watcher needs goes in `sealed`, which the gateway cannot read
|
|
252
|
+
*/
|
|
253
|
+
watchPayment(params: WatchPaymentParams): Promise<TriggerEvent>;
|
|
254
|
+
/**
|
|
255
|
+
* Follow every payment made to one trigger, replayed from the recent ones on
|
|
256
|
+
* connect and then live, reconnecting on its own until the returned function
|
|
257
|
+
* is called. A trigger has no terminal state, so this never resolves
|
|
258
|
+
*/
|
|
259
|
+
followTrigger(secret: string, options: FollowOptions): () => void;
|
|
260
|
+
private needsTicket;
|
|
261
|
+
private wsTicket;
|
|
262
|
+
private sending;
|
|
263
|
+
private reading;
|
|
264
|
+
private proven;
|
|
265
|
+
private checked;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** What a shop knows about a sale before any rail exists */
|
|
269
|
+
interface Order {
|
|
270
|
+
/** The bank matches it on the statement, and Lightning keys idempotency on it */
|
|
271
|
+
reference: string;
|
|
272
|
+
/** The price in the smallest unit of `currency`, so 48055 is 480.55 CZK */
|
|
273
|
+
amountMinor: number;
|
|
274
|
+
/** ISO 4217. The bank rail moves this, Lightning reads it only through your own `amountMsat` */
|
|
275
|
+
currency: string;
|
|
276
|
+
}
|
|
277
|
+
/** One way to pay one order, already registered with the gateway */
|
|
278
|
+
interface Leg {
|
|
279
|
+
/** The watched payment's id, which is what `firstToSettle`, `getWatched` and `waitForWatched` take */
|
|
280
|
+
id: string;
|
|
281
|
+
/** Which rail made it, so a shop can label a leg without knowing how it was built */
|
|
282
|
+
rail: string;
|
|
283
|
+
/** What the payer reads, a BOLT11 invoice or a Short Payment Descriptor */
|
|
284
|
+
scan: string;
|
|
285
|
+
/** The same thing as a QR has to encode it, which is not always `scan` itself */
|
|
286
|
+
qr: string;
|
|
287
|
+
expiresAt: number;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* A payment method. Everything that differs between rails is bound once when the
|
|
291
|
+
* rail is built, so the only thing passed per sale is which sale it is
|
|
292
|
+
*/
|
|
293
|
+
type Rail = (order: Order) => Promise<Leg>;
|
|
294
|
+
interface BankRailConfig {
|
|
295
|
+
/** The gateway that will watch these transfers. It has to be one of your own */
|
|
296
|
+
gateway: ThunderBridge;
|
|
297
|
+
/** Long lived and server side. Every preimage is derived from it, so losing it loses every proof */
|
|
298
|
+
secret: string;
|
|
299
|
+
/** The account the money goes to, as an IBAN */
|
|
300
|
+
iban: string;
|
|
301
|
+
/** Where `bankVerifyEndpoint` is mounted, a public https URL with no query of its own */
|
|
302
|
+
verifyUrl: string;
|
|
303
|
+
/**
|
|
304
|
+
* When this order stops being payable, in unix seconds. Re-offering one order
|
|
305
|
+
* has to return the same second every time, because the gateway compares the
|
|
306
|
+
* expiry to decide whether a repeated watch is the same watch
|
|
307
|
+
*/
|
|
308
|
+
expiresAt: (order: Order) => number;
|
|
309
|
+
/** Groups every leg on the same secret, so one `followTrigger` socket hears them all */
|
|
310
|
+
trigger?: string;
|
|
311
|
+
/** Handed back untouched on that stream. Stable across re-offers, for the reason `expiresAt` is */
|
|
312
|
+
sealed?: (order: Order) => string | Promise<string>;
|
|
313
|
+
/** Up to ten digits, for accounting systems that still want one */
|
|
314
|
+
variableSymbol?: (order: Order) => string | undefined;
|
|
315
|
+
/** Register on a gateway you do not own anyway, on the terms `bankTransfer` sets out */
|
|
316
|
+
allowPublicGateway?: boolean;
|
|
317
|
+
/** What `Leg.rail` reads, for a shop running more than one account */
|
|
318
|
+
name?: string;
|
|
319
|
+
}
|
|
320
|
+
interface LightningRailConfig {
|
|
321
|
+
/** The gateway that mints the invoice */
|
|
322
|
+
gateway: ThunderBridge;
|
|
323
|
+
/** Priority list, the gateway takes the first that can issue a provable invoice */
|
|
324
|
+
lnAddresses: string[];
|
|
325
|
+
/** What this order costs in millisatoshi. A shop pricing in fiat writes `msatFor` and its own ticker */
|
|
326
|
+
amountMsat: (order: Order) => number | Promise<number>;
|
|
327
|
+
/** Groups every leg on the same secret, so one `followTrigger` socket hears them all */
|
|
328
|
+
trigger?: string;
|
|
329
|
+
/**
|
|
330
|
+
* Makes the mint safe to retry. Unset nothing is sent, because a key stable
|
|
331
|
+
* across re-offers is one the gateway can join against the bank leg's reference
|
|
332
|
+
*/
|
|
333
|
+
idempotencyKey?: (order: Order) => string | undefined;
|
|
334
|
+
webhookUrl?: string;
|
|
335
|
+
webhookSecret?: string;
|
|
336
|
+
/** What `Leg.rail` reads, for a shop running more than one wallet */
|
|
337
|
+
name?: string;
|
|
338
|
+
}
|
|
339
|
+
interface BlindLightningRailConfig {
|
|
340
|
+
/** The gateway that watches an invoice it was never allowed to mint */
|
|
341
|
+
gateway: ThunderBridge;
|
|
342
|
+
/** Priority list, resolved here rather than by the gateway */
|
|
343
|
+
lnAddresses: string[];
|
|
344
|
+
/** What this order costs in millisatoshi */
|
|
345
|
+
amountMsat: (order: Order) => number | Promise<number>;
|
|
346
|
+
/** Groups every leg on the same secret, so one `followTrigger` socket hears them all */
|
|
347
|
+
trigger?: string;
|
|
348
|
+
/** Only a watched leg has anywhere to carry this */
|
|
349
|
+
sealed?: (order: Order) => string | Promise<string>;
|
|
350
|
+
/** What `Leg.rail` reads, for a shop running more than one wallet */
|
|
351
|
+
name?: string;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Sell for a bank transfer. The money moves straight to your account and the
|
|
355
|
+
* gateway is told a hash, a URL and an expiry, never the amount or the reference.
|
|
356
|
+
*
|
|
357
|
+
* Which bank is read back is `bankVerifyEndpoint`'s business, not this one's, so
|
|
358
|
+
* a rail built here serves Fio and anything else behind a `Statement`.
|
|
359
|
+
*/
|
|
360
|
+
declare function bankRail(config: BankRailConfig): Rail;
|
|
361
|
+
/**
|
|
362
|
+
* Sell for Lightning, with the gateway minting the invoice. It is told the
|
|
363
|
+
* address list and the amount, which is the round trip `blindLightningRail`
|
|
364
|
+
* spends to avoid.
|
|
365
|
+
*/
|
|
366
|
+
declare function lightningRail(config: LightningRailConfig): Rail;
|
|
367
|
+
/**
|
|
368
|
+
* Sell for Lightning, resolving the address here and handing the gateway only a
|
|
369
|
+
* hash and a URL to poll. It costs one more round trip and the gateway learns
|
|
370
|
+
* neither who is being paid nor how much, so the only refusal left to it is
|
|
371
|
+
* refusing everyone.
|
|
372
|
+
*/
|
|
373
|
+
declare function blindLightningRail(config: BlindLightningRailConfig): Rail;
|
|
374
|
+
|
|
375
|
+
export { type BankRailConfig as B, type CreatePaymentParams as C, type FollowOptions as F, type Leg as L, type Order as O, type Payment as P, type Quote as Q, type Rail as R, ThunderBridge as T, type WalletFailure as W, type CreateOptions as a, type CreateQuoteParams as b, type LightningRailConfig as c, type PaymentStatus as d, type ThunderBridgeOptions as e, type TriggerEvent as f, type WaitOptions as g, type WalletReason as h, type WatchPaymentParams as i, bankRail as j, type BlindLightningRailConfig as k, lightningRail as l, blindLightningRail as m };
|