@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.
Files changed (94) hide show
  1. package/dist/index.d.ts +2 -1
  2. package/dist/index.js +2 -4
  3. package/dist/plugins/IPlugin.d.ts +3 -2
  4. package/dist/plugins/PluginManager.d.ts +3 -2
  5. package/dist/plugins/PluginManager.js +2 -2
  6. package/dist/swaps/FromBtcBaseSwap.d.ts +5 -1
  7. package/dist/swaps/FromBtcBaseSwap.js +20 -0
  8. package/dist/swaps/FromBtcBaseSwapHandler.d.ts +1 -0
  9. package/dist/swaps/FromBtcBaseSwapHandler.js +1 -1
  10. package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +8 -6
  11. package/dist/swaps/FromBtcLnBaseSwapHandler.js +7 -5
  12. package/dist/swaps/SwapHandler.d.ts +1 -4
  13. package/dist/swaps/SwapHandler.js +1 -2
  14. package/dist/swaps/SwapHandlerSwap.d.ts +4 -0
  15. package/dist/swaps/SwapHandlerSwap.js +9 -1
  16. package/dist/swaps/ToBtcBaseSwap.d.ts +3 -1
  17. package/dist/swaps/ToBtcBaseSwap.js +8 -2
  18. package/dist/swaps/ToBtcBaseSwapHandler.d.ts +1 -0
  19. package/dist/swaps/ToBtcBaseSwapHandler.js +1 -1
  20. package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +3 -5
  21. package/dist/swaps/frombtc_abstract/FromBtcAbs.js +18 -25
  22. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +1 -4
  23. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +3 -16
  24. package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +6 -9
  25. package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +238 -137
  26. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +9 -6
  27. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +15 -10
  28. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +2 -2
  29. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +42 -62
  30. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +1 -6
  31. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +2 -14
  32. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +3 -5
  33. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +64 -80
  34. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +1 -2
  35. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +5 -8
  36. package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +5 -125
  37. package/dist/swaps/tobtc_abstract/ToBtcAbs.js +41 -334
  38. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +1 -4
  39. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +2 -11
  40. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +5 -55
  41. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +152 -398
  42. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +1 -6
  43. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +2 -15
  44. package/dist/utils/Utils.d.ts +0 -10
  45. package/dist/utils/Utils.js +1 -34
  46. package/dist/wallets/IBitcoinWallet.d.ts +62 -0
  47. package/dist/wallets/IBitcoinWallet.js +2 -0
  48. package/dist/wallets/ILightningWallet.d.ts +118 -0
  49. package/dist/wallets/ILightningWallet.js +37 -0
  50. package/package.json +4 -9
  51. package/src/index.ts +3 -5
  52. package/src/plugins/IPlugin.ts +4 -2
  53. package/src/plugins/PluginManager.ts +6 -3
  54. package/src/swaps/FromBtcBaseSwap.ts +24 -1
  55. package/src/swaps/FromBtcBaseSwapHandler.ts +6 -2
  56. package/src/swaps/FromBtcLnBaseSwapHandler.ts +22 -6
  57. package/src/swaps/SwapHandler.ts +1 -8
  58. package/src/swaps/SwapHandlerSwap.ts +14 -1
  59. package/src/swaps/ToBtcBaseSwap.ts +12 -3
  60. package/src/swaps/ToBtcBaseSwapHandler.ts +6 -2
  61. package/src/swaps/frombtc_abstract/FromBtcAbs.ts +24 -28
  62. package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +3 -18
  63. package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +260 -159
  64. package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +22 -15
  65. package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +69 -79
  66. package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +3 -20
  67. package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +80 -97
  68. package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +6 -9
  69. package/src/swaps/tobtc_abstract/ToBtcAbs.ts +52 -410
  70. package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +3 -18
  71. package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +157 -434
  72. package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +3 -20
  73. package/src/utils/Utils.ts +0 -31
  74. package/src/wallets/IBitcoinWallet.ts +66 -0
  75. package/src/wallets/ILightningWallet.ts +179 -0
  76. package/dist/fees/OneDollarFeeEstimator.d.ts +0 -16
  77. package/dist/fees/OneDollarFeeEstimator.js +0 -71
  78. package/dist/utils/coinselect2/accumulative.d.ts +0 -6
  79. package/dist/utils/coinselect2/accumulative.js +0 -44
  80. package/dist/utils/coinselect2/blackjack.d.ts +0 -6
  81. package/dist/utils/coinselect2/blackjack.js +0 -41
  82. package/dist/utils/coinselect2/index.d.ts +0 -16
  83. package/dist/utils/coinselect2/index.js +0 -40
  84. package/dist/utils/coinselect2/utils.d.ts +0 -64
  85. package/dist/utils/coinselect2/utils.js +0 -121
  86. package/src/fees/OneDollarFeeEstimator.ts +0 -95
  87. package/src/utils/coinselect2/accumulative.js +0 -32
  88. package/src/utils/coinselect2/accumulative.ts +0 -58
  89. package/src/utils/coinselect2/blackjack.js +0 -29
  90. package/src/utils/coinselect2/blackjack.ts +0 -54
  91. package/src/utils/coinselect2/index.js +0 -16
  92. package/src/utils/coinselect2/index.ts +0 -50
  93. package/src/utils/coinselect2/utils.js +0 -110
  94. 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, FromBtcBaseSwapHandler} from "../FromBtcBaseSwapHandler";
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, EventEmitter> = new Map<string, EventEmitter>();
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
- lnd: AuthenticatedLnd,
56
+ lightning: ILightningWallet,
61
57
  swapPricing: ISwapPrice,
62
58
  config: SwapForGasServerConfig
63
59
  ) {
64
- super(storageDirectory, path, chains, lnd, swapPricing);
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 sub = this.activeSubscriptions.get(paymentHash);
80
- if(sub==null) return false;
81
- sub.removeAllListeners();
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(invoiceData.getHash())) return;
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
- sub.on('invoice_updated', (invoice: SubscribeToInvoiceInvoiceUpdatedEvent) => {
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.activeSubscriptions.set(hash, sub);
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 = bolt11.decode(swap.pr);
123
- const invoice: GetInvoiceResult = await getInvoice({
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
- if(invoice.is_held) {
129
- //Adjust the state of the swap and expiry
130
- try {
131
- await this.htlcReceived(swap, invoice);
132
- //Result is either FromBtcLnTrustedSwapState.RECEIVED or FromBtcLnTrustedSwapState.CANCELED
133
- } catch (e) {
134
- console.error(e);
135
- }
136
- } else if(!invoice.is_confirmed) {
137
- //Not paid
138
- const isInvoiceExpired = parsedPR.timeExpireDate<Date.now()/1000;
139
- if(isInvoiceExpired) {
140
- await swap.setState(FromBtcLnTrustedSwapState.CANCELED);
141
- return true;
142
- }
143
- this.subscribeToInvoice(swap);
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<GetInvoiceResult> {
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
- const isBeingPaid = invoice.is_held;
341
- if (!isBeingPaid) {
342
- if (invoice.is_canceled) throw {
343
- _httpStatus: 200,
344
- code: 10001,
345
- msg: "Invoice expired/canceled"
346
- };
347
- if (invoice.is_confirmed) {
348
- const scTxId = this.processedTxIds.get(paymentHash);
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: scTxId
330
+ txId: this.processedTxIds.get(paymentHash)
355
331
  }
356
332
  };
357
- }
358
- throw {
359
- _httpStatus: 200,
360
- code: 10010,
361
- msg: "Invoice yet unpaid"
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<{channels: any[]}> = this.getChannelsPrefetch(abortController);
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
- const hodlInvoiceObj = {
418
+
419
+ const hodlInvoiceObj: HodlInvoiceInit = {
443
420
  description: chainIdentifier+"-GAS-"+parsedBody.address,
444
- cltv_delta: this.config.minCltv.add(new BN(5)).toNumber(),
445
- expires_at: new Date(Date.now()+(this.config.invoiceTimeoutSeconds*1000)).toISOString(),
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)).toString(10),
448
- lnd: null
424
+ mtokens: amountBD.mul(new BN(1000))
449
425
  };
450
- metadata.invoiceRequest = {...hodlInvoiceObj};
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 = new BN(chainIdOrObj.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.toString(10);
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
  }