@atomiqlabs/lp-lib 10.3.11 → 11.0.0
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/index.d.ts +2 -1
- package/dist/index.js +2 -4
- package/dist/plugins/IPlugin.d.ts +3 -2
- package/dist/plugins/PluginManager.d.ts +3 -2
- package/dist/plugins/PluginManager.js +2 -2
- package/dist/swaps/FromBtcBaseSwap.d.ts +5 -1
- package/dist/swaps/FromBtcBaseSwap.js +20 -0
- package/dist/swaps/FromBtcBaseSwapHandler.d.ts +1 -0
- package/dist/swaps/FromBtcBaseSwapHandler.js +1 -1
- package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +8 -6
- package/dist/swaps/FromBtcLnBaseSwapHandler.js +7 -5
- package/dist/swaps/SwapHandler.d.ts +1 -4
- package/dist/swaps/SwapHandler.js +1 -2
- package/dist/swaps/SwapHandlerSwap.d.ts +4 -0
- package/dist/swaps/SwapHandlerSwap.js +9 -1
- package/dist/swaps/ToBtcBaseSwap.d.ts +3 -1
- package/dist/swaps/ToBtcBaseSwap.js +8 -2
- package/dist/swaps/ToBtcBaseSwapHandler.d.ts +1 -0
- package/dist/swaps/ToBtcBaseSwapHandler.js +1 -1
- package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +3 -5
- package/dist/swaps/frombtc_abstract/FromBtcAbs.js +18 -25
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +1 -4
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +3 -16
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +6 -9
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +238 -137
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +9 -6
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +15 -10
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +2 -2
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +42 -62
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +1 -6
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +2 -14
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +3 -5
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +64 -80
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +1 -2
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +5 -8
- package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +5 -125
- package/dist/swaps/tobtc_abstract/ToBtcAbs.js +41 -334
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +1 -4
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +2 -11
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +5 -55
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +152 -398
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +1 -6
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +2 -15
- package/dist/utils/Utils.d.ts +0 -10
- package/dist/utils/Utils.js +1 -34
- package/dist/wallets/IBitcoinWallet.d.ts +62 -0
- package/dist/wallets/IBitcoinWallet.js +2 -0
- package/dist/wallets/ILightningWallet.d.ts +118 -0
- package/dist/wallets/ILightningWallet.js +37 -0
- package/package.json +4 -9
- package/src/index.ts +3 -5
- package/src/plugins/IPlugin.ts +4 -2
- package/src/plugins/PluginManager.ts +6 -3
- package/src/swaps/FromBtcBaseSwap.ts +24 -1
- package/src/swaps/FromBtcBaseSwapHandler.ts +6 -2
- package/src/swaps/FromBtcLnBaseSwapHandler.ts +22 -6
- package/src/swaps/SwapHandler.ts +1 -8
- package/src/swaps/SwapHandlerSwap.ts +14 -1
- package/src/swaps/ToBtcBaseSwap.ts +12 -3
- package/src/swaps/ToBtcBaseSwapHandler.ts +6 -2
- package/src/swaps/frombtc_abstract/FromBtcAbs.ts +24 -28
- package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +3 -18
- package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +260 -159
- package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +22 -15
- package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +69 -79
- package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +3 -20
- package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +80 -97
- package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +6 -9
- package/src/swaps/tobtc_abstract/ToBtcAbs.ts +52 -410
- package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +3 -18
- package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +157 -434
- package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +3 -20
- package/src/utils/Utils.ts +0 -31
- package/src/wallets/IBitcoinWallet.ts +66 -0
- package/src/wallets/ILightningWallet.ts +179 -0
- package/dist/fees/OneDollarFeeEstimator.d.ts +0 -16
- package/dist/fees/OneDollarFeeEstimator.js +0 -71
- package/dist/utils/coinselect2/accumulative.d.ts +0 -6
- package/dist/utils/coinselect2/accumulative.js +0 -44
- package/dist/utils/coinselect2/blackjack.d.ts +0 -6
- package/dist/utils/coinselect2/blackjack.js +0 -41
- package/dist/utils/coinselect2/index.d.ts +0 -16
- package/dist/utils/coinselect2/index.js +0 -40
- package/dist/utils/coinselect2/utils.d.ts +0 -64
- package/dist/utils/coinselect2/utils.js +0 -121
- package/src/fees/OneDollarFeeEstimator.ts +0 -95
- package/src/utils/coinselect2/accumulative.js +0 -32
- package/src/utils/coinselect2/accumulative.ts +0 -58
- package/src/utils/coinselect2/blackjack.js +0 -29
- package/src/utils/coinselect2/blackjack.ts +0 -54
- package/src/utils/coinselect2/index.js +0 -16
- package/src/utils/coinselect2/index.ts +0 -50
- package/src/utils/coinselect2/utils.js +0 -110
- package/src/utils/coinselect2/utils.ts +0 -183
|
@@ -1,23 +1,14 @@
|
|
|
1
1
|
import * as BN from "bn.js";
|
|
2
2
|
import {Express, Request, Response} from "express";
|
|
3
3
|
import {createHash, randomBytes} from "crypto";
|
|
4
|
-
import * as bolt11 from "@atomiqlabs/bolt11";
|
|
5
4
|
import {
|
|
6
5
|
ClaimEvent,
|
|
7
6
|
InitializeEvent,
|
|
8
7
|
RefundEvent,
|
|
9
8
|
SwapData,
|
|
10
9
|
} from "@atomiqlabs/base";
|
|
11
|
-
import {
|
|
12
|
-
AuthenticatedLnd,
|
|
13
|
-
cancelHodlInvoice,
|
|
14
|
-
createHodlInvoice,
|
|
15
|
-
getInvoice, GetInvoiceResult,
|
|
16
|
-
settleHodlInvoice,
|
|
17
|
-
subscribeToInvoice, SubscribeToInvoiceInvoiceUpdatedEvent
|
|
18
|
-
} from "lightning";
|
|
19
10
|
import {FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState} from "./FromBtcLnTrustedSwap";
|
|
20
|
-
import {FromBtcBaseConfig
|
|
11
|
+
import {FromBtcBaseConfig} from "../FromBtcBaseSwapHandler";
|
|
21
12
|
import {ISwapPrice} from "../ISwapPrice";
|
|
22
13
|
import {MultichainData, SwapHandlerType} from "../SwapHandler";
|
|
23
14
|
import {IIntermediaryStorage} from "../../storage/IIntermediaryStorage";
|
|
@@ -27,8 +18,13 @@ import {ServerParamEncoder} from "../../utils/paramcoders/server/ServerParamEnco
|
|
|
27
18
|
import {FieldTypeEnum, verifySchema} from "../../utils/paramcoders/SchemaVerifier";
|
|
28
19
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
29
20
|
import {FromBtcLnBaseSwapHandler} from "../FromBtcLnBaseSwapHandler";
|
|
30
|
-
import EventEmitter from "node:events";
|
|
31
21
|
import {serverParamDecoder} from "../../utils/paramcoders/server/ServerParamDecoder";
|
|
22
|
+
import {
|
|
23
|
+
HodlInvoiceInit,
|
|
24
|
+
ILightningWallet,
|
|
25
|
+
LightningNetworkChannel,
|
|
26
|
+
LightningNetworkInvoice
|
|
27
|
+
} from "../../wallets/ILightningWallet";
|
|
32
28
|
|
|
33
29
|
export type SwapForGasServerConfig = FromBtcBaseConfig & {
|
|
34
30
|
minCltv: BN,
|
|
@@ -48,7 +44,7 @@ export type FromBtcLnTrustedRequestType = {
|
|
|
48
44
|
export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState> {
|
|
49
45
|
readonly type: SwapHandlerType = SwapHandlerType.FROM_BTCLN_TRUSTED;
|
|
50
46
|
|
|
51
|
-
activeSubscriptions: Map<string,
|
|
47
|
+
activeSubscriptions: Map<string, AbortController> = new Map<string, AbortController>();
|
|
52
48
|
processedTxIds: Map<string, string> = new Map<string, string>();
|
|
53
49
|
|
|
54
50
|
readonly config: SwapForGasServerConfig;
|
|
@@ -57,11 +53,11 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
57
53
|
storageDirectory: IIntermediaryStorage<FromBtcLnTrustedSwap>,
|
|
58
54
|
path: string,
|
|
59
55
|
chains: MultichainData,
|
|
60
|
-
|
|
56
|
+
lightning: ILightningWallet,
|
|
61
57
|
swapPricing: ISwapPrice,
|
|
62
58
|
config: SwapForGasServerConfig
|
|
63
59
|
) {
|
|
64
|
-
super(storageDirectory, path, chains,
|
|
60
|
+
super(storageDirectory, path, chains, lightning, swapPricing);
|
|
65
61
|
this.config = config;
|
|
66
62
|
this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
|
|
67
63
|
for(let chainId in chains.chains) {
|
|
@@ -76,9 +72,9 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
76
72
|
* @private
|
|
77
73
|
*/
|
|
78
74
|
private unsubscribeInvoice(paymentHash: string): boolean {
|
|
79
|
-
const
|
|
80
|
-
if(
|
|
81
|
-
|
|
75
|
+
const controller = this.activeSubscriptions.get(paymentHash);
|
|
76
|
+
if(controller==null) return false;
|
|
77
|
+
controller.abort("Unsubscribed");
|
|
82
78
|
this.activeSubscriptions.delete(paymentHash);
|
|
83
79
|
return true;
|
|
84
80
|
}
|
|
@@ -92,21 +88,17 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
92
88
|
const hash = invoiceData.getHash();
|
|
93
89
|
|
|
94
90
|
//Already subscribed
|
|
95
|
-
if(this.activeSubscriptions.has(
|
|
96
|
-
|
|
97
|
-
const sub = subscribeToInvoice({id: hash, lnd: this.LND});
|
|
98
|
-
|
|
99
|
-
this.swapLogger.debug(invoiceData, "subscribeToInvoice(): Subscribed to invoice payment");
|
|
91
|
+
if(this.activeSubscriptions.has(hash)) return;
|
|
100
92
|
|
|
101
|
-
|
|
93
|
+
const abortController = new AbortController();
|
|
94
|
+
this.lightning.waitForInvoice(hash, abortController.signal).then(invoice => {
|
|
102
95
|
this.swapLogger.debug(invoiceData, "subscribeToInvoice(): invoice_updated: ", invoice);
|
|
103
|
-
if(!invoice.is_held) return;
|
|
104
96
|
this.htlcReceived(invoiceData, invoice).catch(e => console.error(e));
|
|
105
|
-
sub.removeAllListeners();
|
|
106
97
|
this.activeSubscriptions.delete(hash);
|
|
107
98
|
});
|
|
108
99
|
|
|
109
|
-
this.
|
|
100
|
+
this.swapLogger.debug(invoiceData, "subscribeToInvoice(): Subscribed to invoice payment");
|
|
101
|
+
this.activeSubscriptions.set(hash, abortController);
|
|
110
102
|
}
|
|
111
103
|
|
|
112
104
|
/**
|
|
@@ -119,31 +111,29 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
119
111
|
if(swap.state===FromBtcLnTrustedSwapState.CANCELED) return true;
|
|
120
112
|
if(swap.state===FromBtcLnTrustedSwapState.REFUNDED) return true;
|
|
121
113
|
|
|
122
|
-
const parsedPR =
|
|
123
|
-
const invoice
|
|
124
|
-
id: parsedPR.tagsObject.payment_hash,
|
|
125
|
-
lnd: this.LND
|
|
126
|
-
});
|
|
114
|
+
const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
|
|
115
|
+
const invoice = await this.lightning.getInvoice(parsedPR.id);
|
|
127
116
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
117
|
+
switch (invoice.status) {
|
|
118
|
+
case "held":
|
|
119
|
+
try {
|
|
120
|
+
await this.htlcReceived(swap, invoice);
|
|
121
|
+
//Result is either FromBtcLnTrustedSwapState.RECEIVED or FromBtcLnTrustedSwapState.CANCELED
|
|
122
|
+
} catch (e) {
|
|
123
|
+
console.error(e);
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
case "confirmed":
|
|
127
|
+
return false;
|
|
128
|
+
default:
|
|
129
|
+
const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
|
|
130
|
+
if(isInvoiceExpired) {
|
|
131
|
+
await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
this.subscribeToInvoice(swap);
|
|
135
|
+
return false;
|
|
144
136
|
}
|
|
145
|
-
|
|
146
|
-
return false;
|
|
147
137
|
}
|
|
148
138
|
|
|
149
139
|
protected async cancelInvoices(swaps: FromBtcLnTrustedSwap[]) {
|
|
@@ -151,10 +141,7 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
151
141
|
//Cancel invoices
|
|
152
142
|
try {
|
|
153
143
|
const paymentHash = swap.getHash();
|
|
154
|
-
await cancelHodlInvoice(
|
|
155
|
-
lnd: this.LND,
|
|
156
|
-
id: paymentHash
|
|
157
|
-
});
|
|
144
|
+
await this.lightning.cancelHodlInvoice(paymentHash);
|
|
158
145
|
this.unsubscribeInvoice(paymentHash);
|
|
159
146
|
this.swapLogger.info(swap, "cancelInvoices(): invoice cancelled!");
|
|
160
147
|
await this.removeSwapData(swap);
|
|
@@ -195,10 +182,7 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
195
182
|
if(swap.state!==FromBtcLnTrustedSwapState.RECEIVED) return;
|
|
196
183
|
await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
|
|
197
184
|
const paymentHash = swap.getHash();
|
|
198
|
-
await cancelHodlInvoice(
|
|
199
|
-
id: paymentHash,
|
|
200
|
-
lnd: this.LND
|
|
201
|
-
});
|
|
185
|
+
await this.lightning.cancelHodlInvoice(paymentHash);
|
|
202
186
|
this.unsubscribeInvoice(paymentHash);
|
|
203
187
|
await this.removeSwapData(swap);
|
|
204
188
|
this.swapLogger.info(swap, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", swap.pr);
|
|
@@ -262,10 +246,7 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
262
246
|
//Cancel invoice
|
|
263
247
|
await invoiceData.setState(FromBtcLnTrustedSwapState.REFUNDED);
|
|
264
248
|
await this.storageManager.saveData(invoice.id, null, invoiceData);
|
|
265
|
-
await cancelHodlInvoice(
|
|
266
|
-
id: invoice.id,
|
|
267
|
-
lnd: this.LND
|
|
268
|
-
});
|
|
249
|
+
await this.lightning.cancelHodlInvoice(invoice.id);
|
|
269
250
|
this.unsubscribeInvoice(invoice.id);
|
|
270
251
|
await this.removeSwapData(invoice.id, null);
|
|
271
252
|
this.swapLogger.info(invoiceData, "htlcReceived(): transaction reverted, refunding lightning: ", invoiceData.pr);
|
|
@@ -282,10 +263,7 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
282
263
|
}
|
|
283
264
|
|
|
284
265
|
if(invoiceData.state===FromBtcLnTrustedSwapState.CONFIRMED) {
|
|
285
|
-
await settleHodlInvoice(
|
|
286
|
-
lnd: this.LND,
|
|
287
|
-
secret: invoiceData.secret
|
|
288
|
-
});
|
|
266
|
+
await this.lightning.settleHodlInvoice(invoiceData.secret);
|
|
289
267
|
|
|
290
268
|
if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcSettled = Date.now();
|
|
291
269
|
|
|
@@ -307,11 +285,8 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
307
285
|
* @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
|
|
308
286
|
* @returns the fetched lightning invoice
|
|
309
287
|
*/
|
|
310
|
-
private async checkInvoiceStatus(paymentHash: string): Promise<
|
|
311
|
-
const invoice = await getInvoice(
|
|
312
|
-
id: paymentHash,
|
|
313
|
-
lnd: this.LND
|
|
314
|
-
});
|
|
288
|
+
private async checkInvoiceStatus(paymentHash: string): Promise<LightningNetworkInvoice> {
|
|
289
|
+
const invoice = await this.lightning.getInvoice(paymentHash);
|
|
315
290
|
|
|
316
291
|
const isInvoiceFound = invoice!=null;
|
|
317
292
|
if (!isInvoiceFound) throw {
|
|
@@ -337,32 +312,33 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
337
312
|
msg: "Invoice expired/canceled"
|
|
338
313
|
};
|
|
339
314
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
315
|
+
switch(invoice.status) {
|
|
316
|
+
case "held":
|
|
317
|
+
return invoice;
|
|
318
|
+
case "canceled":
|
|
319
|
+
throw {
|
|
320
|
+
_httpStatus: 200,
|
|
321
|
+
code: 10001,
|
|
322
|
+
msg: "Invoice expired/canceled"
|
|
323
|
+
};
|
|
324
|
+
case "confirmed":
|
|
349
325
|
throw {
|
|
350
326
|
_httpStatus: 200,
|
|
351
327
|
code: 10000,
|
|
352
328
|
msg: "Invoice already paid",
|
|
353
329
|
data: {
|
|
354
|
-
txId:
|
|
330
|
+
txId: this.processedTxIds.get(paymentHash)
|
|
355
331
|
}
|
|
356
332
|
};
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
333
|
+
case "unpaid":
|
|
334
|
+
throw {
|
|
335
|
+
_httpStatus: 200,
|
|
336
|
+
code: 10010,
|
|
337
|
+
msg: "Invoice yet unpaid"
|
|
338
|
+
};
|
|
339
|
+
default:
|
|
340
|
+
throw new Error("Lightning invoice invalid state!");
|
|
363
341
|
}
|
|
364
|
-
|
|
365
|
-
return invoice;
|
|
366
342
|
}
|
|
367
343
|
|
|
368
344
|
startRestServer(restServer: Express) {
|
|
@@ -421,7 +397,7 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
421
397
|
abortController.abort(e);
|
|
422
398
|
return null;
|
|
423
399
|
});
|
|
424
|
-
const channelsPrefetch: Promise<
|
|
400
|
+
const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.getChannelsPrefetch(abortController);
|
|
425
401
|
|
|
426
402
|
//Check valid amount specified (min/max)
|
|
427
403
|
const {
|
|
@@ -439,18 +415,17 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
439
415
|
|
|
440
416
|
const secret = randomBytes(32);
|
|
441
417
|
const hash = createHash("sha256").update(secret).digest();
|
|
442
|
-
|
|
418
|
+
|
|
419
|
+
const hodlInvoiceObj: HodlInvoiceInit = {
|
|
443
420
|
description: chainIdentifier+"-GAS-"+parsedBody.address,
|
|
444
|
-
|
|
445
|
-
|
|
421
|
+
cltvDelta: this.config.minCltv.add(new BN(5)).toNumber(),
|
|
422
|
+
expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
|
|
446
423
|
id: hash.toString("hex"),
|
|
447
|
-
mtokens: amountBD.mul(new BN(1000))
|
|
448
|
-
lnd: null
|
|
424
|
+
mtokens: amountBD.mul(new BN(1000))
|
|
449
425
|
};
|
|
450
|
-
metadata.invoiceRequest =
|
|
451
|
-
hodlInvoiceObj.lnd = this.LND;
|
|
426
|
+
metadata.invoiceRequest = hodlInvoiceObj;
|
|
452
427
|
|
|
453
|
-
const hodlInvoice = await createHodlInvoice(hodlInvoiceObj);
|
|
428
|
+
const hodlInvoice = await this.lightning.createHodlInvoice(hodlInvoiceObj);
|
|
454
429
|
abortController.signal.throwIfAborted();
|
|
455
430
|
metadata.times.invoiceCreated = Date.now();
|
|
456
431
|
metadata.invoiceResponse = {...hodlInvoice};
|
|
@@ -460,6 +435,7 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
460
435
|
const createdSwap = new FromBtcLnTrustedSwap(
|
|
461
436
|
chainIdentifier,
|
|
462
437
|
hodlInvoice.request,
|
|
438
|
+
hodlInvoice.mtokens,
|
|
463
439
|
swapFee,
|
|
464
440
|
swapFeeInToken,
|
|
465
441
|
totalInToken,
|
|
@@ -567,6 +543,13 @@ export class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedS
|
|
|
567
543
|
|
|
568
544
|
async init() {
|
|
569
545
|
await this.storageManager.loadData(FromBtcLnTrustedSwap);
|
|
546
|
+
//Check if all swaps contain a valid amount
|
|
547
|
+
for(let swap of await this.storageManager.query([])) {
|
|
548
|
+
if(swap.amount==null) {
|
|
549
|
+
const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
|
|
550
|
+
swap.amount = parsedPR.mtokens.add(new BN(999)).div(new BN(1000));
|
|
551
|
+
}
|
|
552
|
+
}
|
|
570
553
|
await PluginManager.serviceInitialize(this);
|
|
571
554
|
}
|
|
572
555
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as BN from "bn.js";
|
|
2
2
|
import {SwapData} from "@atomiqlabs/base";
|
|
3
3
|
import {createHash} from "crypto";
|
|
4
|
-
import * as bolt11 from "@atomiqlabs/bolt11";
|
|
5
4
|
import {FromBtcBaseSwap} from "../FromBtcBaseSwap";
|
|
5
|
+
import {deserializeBN, serializeBN} from "../../utils/Utils";
|
|
6
6
|
|
|
7
7
|
export enum FromBtcLnTrustedSwapState {
|
|
8
8
|
REFUNDED = -2,
|
|
@@ -26,6 +26,7 @@ export class FromBtcLnTrustedSwap<T extends SwapData = SwapData> extends FromBtc
|
|
|
26
26
|
constructor(
|
|
27
27
|
chainIdentifier: string,
|
|
28
28
|
pr: string,
|
|
29
|
+
inputMtokens: BN,
|
|
29
30
|
swapFee: BN,
|
|
30
31
|
swapFeeInToken: BN,
|
|
31
32
|
output: BN,
|
|
@@ -34,9 +35,9 @@ export class FromBtcLnTrustedSwap<T extends SwapData = SwapData> extends FromBtc
|
|
|
34
35
|
);
|
|
35
36
|
constructor(obj: any);
|
|
36
37
|
|
|
37
|
-
constructor(chainIdOrObj: string | any, pr?: string, swapFee?: BN, swapFeeInToken?: BN, output?: BN, secret?: string, dstAddress?: string) {
|
|
38
|
+
constructor(chainIdOrObj: string | any, pr?: string, inputMtokens?: BN, swapFee?: BN, swapFeeInToken?: BN, output?: BN, secret?: string, dstAddress?: string) {
|
|
38
39
|
if(typeof(chainIdOrObj)==="string") {
|
|
39
|
-
super(chainIdOrObj, swapFee, swapFeeInToken);
|
|
40
|
+
super(chainIdOrObj, inputMtokens.add(new BN(999)).div(new BN(1000)), swapFee, swapFeeInToken);
|
|
40
41
|
this.state = FromBtcLnTrustedSwapState.CREATED;
|
|
41
42
|
this.pr = pr;
|
|
42
43
|
this.output = output;
|
|
@@ -45,7 +46,7 @@ export class FromBtcLnTrustedSwap<T extends SwapData = SwapData> extends FromBtc
|
|
|
45
46
|
} else {
|
|
46
47
|
super(chainIdOrObj);
|
|
47
48
|
this.pr = chainIdOrObj.pr;
|
|
48
|
-
this.output =
|
|
49
|
+
this.output = deserializeBN(chainIdOrObj.output);
|
|
49
50
|
this.secret = chainIdOrObj.secret;
|
|
50
51
|
this.dstAddress = chainIdOrObj.dstAddress;
|
|
51
52
|
this.scRawTx = chainIdOrObj.scRawTx;
|
|
@@ -64,17 +65,13 @@ export class FromBtcLnTrustedSwap<T extends SwapData = SwapData> extends FromBtc
|
|
|
64
65
|
serialize(): any {
|
|
65
66
|
const partialSerialized = super.serialize();
|
|
66
67
|
partialSerialized.pr = this.pr;
|
|
67
|
-
partialSerialized.output = this.output
|
|
68
|
+
partialSerialized.output = serializeBN(this.output);
|
|
68
69
|
partialSerialized.secret = this.secret;
|
|
69
70
|
partialSerialized.dstAddress = this.dstAddress;
|
|
70
71
|
partialSerialized.scRawTx = this.scRawTx;
|
|
71
72
|
return partialSerialized;
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
getTotalInputAmount(): BN {
|
|
75
|
-
return new BN(bolt11.decode(this.pr).millisatoshis).add(new BN(999)).div(new BN(1000));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
75
|
isFailed(): boolean {
|
|
79
76
|
return this.state===FromBtcLnTrustedSwapState.CANCELED || this.state===FromBtcLnTrustedSwapState.REFUNDED;
|
|
80
77
|
}
|