@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,13 +1,14 @@
|
|
|
1
|
-
import {SwapData} from "@atomiqlabs/base";
|
|
1
|
+
import {BtcTx, SwapData} from "@atomiqlabs/base";
|
|
2
2
|
import {FromBtcBaseSwap} from "../FromBtcBaseSwap";
|
|
3
3
|
import * as BN from "bn.js";
|
|
4
4
|
import {deserializeBN, serializeBN} from "../../utils/Utils";
|
|
5
|
-
import {createHash} from "crypto";
|
|
5
|
+
import {createHash, randomBytes} from "crypto";
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
export enum FromBtcTrustedSwapState {
|
|
9
|
-
DOUBLE_SPENT = -
|
|
10
|
-
REFUNDED = -
|
|
9
|
+
DOUBLE_SPENT = -4,
|
|
10
|
+
REFUNDED = -3,
|
|
11
|
+
REFUNDABLE = -2,
|
|
11
12
|
EXPIRED = -1,
|
|
12
13
|
CREATED = 0,
|
|
13
14
|
RECEIVED = 1,
|
|
@@ -19,8 +20,8 @@ export enum FromBtcTrustedSwapState {
|
|
|
19
20
|
|
|
20
21
|
export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBaseSwap<T, FromBtcTrustedSwapState> {
|
|
21
22
|
|
|
23
|
+
readonly sequence: BN;
|
|
22
24
|
readonly btcAddress: string;
|
|
23
|
-
readonly inputSats: BN;
|
|
24
25
|
|
|
25
26
|
readonly dstAddress: string;
|
|
26
27
|
readonly outputTokens: BN;
|
|
@@ -28,7 +29,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
28
29
|
readonly createdHeight: number;
|
|
29
30
|
readonly expiresAt: number;
|
|
30
31
|
readonly recommendedFee: number;
|
|
31
|
-
|
|
32
|
+
refundAddress: string;
|
|
32
33
|
|
|
33
34
|
adjustedInput: BN;
|
|
34
35
|
adjustedOutput: BN;
|
|
@@ -36,7 +37,9 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
36
37
|
doubleSpent: boolean;
|
|
37
38
|
scRawTx: string;
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
btcTx: BtcTx;
|
|
41
|
+
txFee: number;
|
|
42
|
+
txSize: number;
|
|
40
43
|
txId: string;
|
|
41
44
|
vout: number;
|
|
42
45
|
|
|
@@ -72,11 +75,11 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
72
75
|
refundAddress?: string
|
|
73
76
|
) {
|
|
74
77
|
if(typeof(objOrChainIdentifier)==="string") {
|
|
75
|
-
super(objOrChainIdentifier, swapFee, swapFeeInToken);
|
|
78
|
+
super(objOrChainIdentifier, inputSats, swapFee, swapFeeInToken);
|
|
76
79
|
this.state = FromBtcTrustedSwapState.CREATED;
|
|
77
80
|
this.doubleSpent = false;
|
|
81
|
+
this.sequence = new BN(randomBytes(8));
|
|
78
82
|
this.btcAddress = btcAddress;
|
|
79
|
-
this.inputSats = inputSats;
|
|
80
83
|
this.dstAddress = dstAddress;
|
|
81
84
|
this.outputTokens = outputTokens;
|
|
82
85
|
this.createdHeight = createdHeight;
|
|
@@ -86,7 +89,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
86
89
|
} else {
|
|
87
90
|
super(objOrChainIdentifier);
|
|
88
91
|
this.btcAddress = objOrChainIdentifier.btcAddress;
|
|
89
|
-
this.
|
|
92
|
+
this.sequence = deserializeBN(objOrChainIdentifier.sequence);
|
|
90
93
|
this.dstAddress = objOrChainIdentifier.dstAddress;
|
|
91
94
|
this.outputTokens = deserializeBN(objOrChainIdentifier.outputTokens);
|
|
92
95
|
this.adjustedInput = deserializeBN(objOrChainIdentifier.adjustedInput);
|
|
@@ -97,7 +100,9 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
97
100
|
this.refundAddress = objOrChainIdentifier.refundAddress;
|
|
98
101
|
this.doubleSpent = objOrChainIdentifier.doubleSpent;
|
|
99
102
|
this.scRawTx = objOrChainIdentifier.scRawTx;
|
|
100
|
-
this.
|
|
103
|
+
this.btcTx = objOrChainIdentifier.btcTx;
|
|
104
|
+
this.txFee = objOrChainIdentifier.txFee;
|
|
105
|
+
this.txSize = objOrChainIdentifier.txSize;
|
|
101
106
|
this.txId = objOrChainIdentifier.txId;
|
|
102
107
|
this.vout = objOrChainIdentifier.vout;
|
|
103
108
|
this.burnTxId = objOrChainIdentifier.burnTxId;
|
|
@@ -108,7 +113,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
108
113
|
serialize(): any {
|
|
109
114
|
const partialSerialized = super.serialize();
|
|
110
115
|
partialSerialized.btcAddress = this.btcAddress;
|
|
111
|
-
partialSerialized.
|
|
116
|
+
partialSerialized.sequence = serializeBN(this.sequence);
|
|
112
117
|
partialSerialized.dstAddress = this.dstAddress;
|
|
113
118
|
partialSerialized.outputTokens = serializeBN(this.outputTokens);
|
|
114
119
|
partialSerialized.adjustedInput = serializeBN(this.adjustedInput);
|
|
@@ -119,7 +124,9 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
119
124
|
partialSerialized.refundAddress = this.refundAddress;
|
|
120
125
|
partialSerialized.doubleSpent = this.doubleSpent;
|
|
121
126
|
partialSerialized.scRawTx = this.scRawTx;
|
|
122
|
-
partialSerialized.
|
|
127
|
+
partialSerialized.btcTx = this.btcTx;
|
|
128
|
+
partialSerialized.txFee = this.txFee;
|
|
129
|
+
partialSerialized.txSize = this.txSize;
|
|
123
130
|
partialSerialized.txId = this.txId;
|
|
124
131
|
partialSerialized.vout = this.vout;
|
|
125
132
|
partialSerialized.burnTxId = this.burnTxId;
|
|
@@ -132,7 +139,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
132
139
|
}
|
|
133
140
|
|
|
134
141
|
getSequence(): BN {
|
|
135
|
-
return
|
|
142
|
+
return this.sequence;
|
|
136
143
|
}
|
|
137
144
|
|
|
138
145
|
getOutputAmount(): BN {
|
|
@@ -140,7 +147,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
140
147
|
}
|
|
141
148
|
|
|
142
149
|
getTotalInputAmount(): BN {
|
|
143
|
-
return this.adjustedInput || this.
|
|
150
|
+
return this.adjustedInput || this.amount;
|
|
144
151
|
}
|
|
145
152
|
|
|
146
153
|
isFailed(): boolean {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import * as BN from "bn.js";
|
|
2
2
|
import {Express, Request, Response} from "express";
|
|
3
|
-
import * as lncli from "ln-service";
|
|
4
3
|
import {createHash} from "crypto";
|
|
5
|
-
import * as bolt11 from "@atomiqlabs/bolt11";
|
|
6
4
|
import {FromBtcLnSwapAbs, FromBtcLnSwapState} from "./FromBtcLnSwapAbs";
|
|
7
5
|
import {MultichainData, SwapHandlerType} from "../SwapHandler";
|
|
8
6
|
import {ISwapPrice} from "../ISwapPrice";
|
|
@@ -13,7 +11,6 @@ import {
|
|
|
13
11
|
RefundEvent,
|
|
14
12
|
SwapData
|
|
15
13
|
} from "@atomiqlabs/base";
|
|
16
|
-
import {AuthenticatedLnd} from "lightning";
|
|
17
14
|
import {expressHandlerWrapper, HEX_REGEX, isDefinedRuntimeError} from "../../utils/Utils";
|
|
18
15
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
19
16
|
import {IIntermediaryStorage} from "../../storage/IIntermediaryStorage";
|
|
@@ -21,8 +18,14 @@ import {FieldTypeEnum, verifySchema} from "../../utils/paramcoders/SchemaVerifie
|
|
|
21
18
|
import {serverParamDecoder} from "../../utils/paramcoders/server/ServerParamDecoder";
|
|
22
19
|
import {ServerParamEncoder} from "../../utils/paramcoders/server/ServerParamEncoder";
|
|
23
20
|
import {IParamReader} from "../../utils/paramcoders/IParamReader";
|
|
24
|
-
import {FromBtcBaseConfig
|
|
21
|
+
import {FromBtcBaseConfig} from "../FromBtcBaseSwapHandler";
|
|
25
22
|
import {FromBtcLnBaseSwapHandler} from "../FromBtcLnBaseSwapHandler";
|
|
23
|
+
import {
|
|
24
|
+
HodlInvoiceInit,
|
|
25
|
+
ILightningWallet,
|
|
26
|
+
LightningNetworkChannel,
|
|
27
|
+
LightningNetworkInvoice
|
|
28
|
+
} from "../../wallets/ILightningWallet";
|
|
26
29
|
|
|
27
30
|
export type FromBtcLnConfig = FromBtcBaseConfig & {
|
|
28
31
|
invoiceTimeoutSeconds?: number,
|
|
@@ -52,29 +55,26 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
52
55
|
storageDirectory: IIntermediaryStorage<FromBtcLnSwapAbs>,
|
|
53
56
|
path: string,
|
|
54
57
|
chains: MultichainData,
|
|
55
|
-
|
|
58
|
+
lightning: ILightningWallet,
|
|
56
59
|
swapPricing: ISwapPrice,
|
|
57
60
|
config: FromBtcLnConfig
|
|
58
61
|
) {
|
|
59
|
-
super(storageDirectory, path, chains,
|
|
62
|
+
super(storageDirectory, path, chains, lightning, swapPricing);
|
|
60
63
|
this.config = config;
|
|
61
64
|
this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
|
|
62
65
|
}
|
|
63
66
|
|
|
64
67
|
protected async processPastSwap(swap: FromBtcLnSwapAbs): Promise<"REFUND" | "SETTLE" | "CANCEL" | null> {
|
|
65
|
-
const {swapContract} = this.getChain(swap.chainIdentifier);
|
|
68
|
+
const {swapContract, signer} = this.getChain(swap.chainIdentifier);
|
|
66
69
|
if(swap.state===FromBtcLnSwapState.CREATED) {
|
|
67
70
|
//Check if already paid
|
|
68
|
-
const parsedPR =
|
|
69
|
-
const invoice = await
|
|
70
|
-
id: parsedPR.tagsObject.payment_hash,
|
|
71
|
-
lnd: this.LND
|
|
72
|
-
});
|
|
71
|
+
const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
|
|
72
|
+
const invoice = await this.lightning.getInvoice(parsedPR.id);
|
|
73
73
|
|
|
74
|
-
const isBeingPaid = invoice.
|
|
74
|
+
const isBeingPaid = invoice.status==="held";
|
|
75
75
|
if(!isBeingPaid) {
|
|
76
76
|
//Not paid
|
|
77
|
-
const isInvoiceExpired = parsedPR.
|
|
77
|
+
const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
|
|
78
78
|
if(!isInvoiceExpired) return null;
|
|
79
79
|
|
|
80
80
|
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: "+swap.pr);
|
|
@@ -100,8 +100,6 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
if(swap.state===FromBtcLnSwapState.RECEIVED) {
|
|
103
|
-
const parsedPR = bolt11.decode(swap.pr);
|
|
104
|
-
|
|
105
103
|
const isAuthorizationExpired = await swapContract.isInitAuthorizationExpired(swap.data, swap);
|
|
106
104
|
if(isAuthorizationExpired) {
|
|
107
105
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
@@ -119,11 +117,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
119
117
|
}
|
|
120
118
|
|
|
121
119
|
if(swap.state===FromBtcLnSwapState.RECEIVED || swap.state===FromBtcLnSwapState.COMMITED) {
|
|
122
|
-
|
|
123
|
-
const currentTime = new BN(Math.floor(Date.now()/1000)-this.config.maxSkew);
|
|
124
|
-
|
|
125
|
-
const isExpired = expiryTime!=null && expiryTime.lt(currentTime);
|
|
126
|
-
if(!isExpired) return null;
|
|
120
|
+
if(!swapContract.isExpired(signer.getAddress(), swap.data)) return null;
|
|
127
121
|
|
|
128
122
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
129
123
|
if(isCommited) {
|
|
@@ -159,10 +153,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
159
153
|
//Refund
|
|
160
154
|
const paymentHash = swap.data.getHash();
|
|
161
155
|
try {
|
|
162
|
-
await
|
|
163
|
-
lnd: this.LND,
|
|
164
|
-
id: paymentHash
|
|
165
|
-
});
|
|
156
|
+
await this.lightning.cancelHodlInvoice(paymentHash);
|
|
166
157
|
this.swapLogger.info(swap, "cancelInvoices(): invoice cancelled!");
|
|
167
158
|
await this.removeSwapData(swap);
|
|
168
159
|
} catch (e) {
|
|
@@ -174,10 +165,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
174
165
|
protected async settleInvoices(swaps: FromBtcLnSwapAbs[]) {
|
|
175
166
|
for(let swap of swaps) {
|
|
176
167
|
try {
|
|
177
|
-
await
|
|
178
|
-
lnd: this.LND,
|
|
179
|
-
secret: swap.secret
|
|
180
|
-
});
|
|
168
|
+
await this.lightning.settleHodlInvoice(swap.secret);
|
|
181
169
|
if(swap.metadata!=null) swap.metadata.times.htlcSettled = Date.now();
|
|
182
170
|
await this.removeSwapData(swap, FromBtcLnSwapState.SETTLED);
|
|
183
171
|
|
|
@@ -272,10 +260,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
272
260
|
this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap HTLC successfully claimed by the client, invoice: "+savedSwap.pr);
|
|
273
261
|
|
|
274
262
|
try {
|
|
275
|
-
await
|
|
276
|
-
lnd: this.LND,
|
|
277
|
-
secret: secretHex
|
|
278
|
-
});
|
|
263
|
+
await this.lightning.settleHodlInvoice(secretHex);
|
|
279
264
|
this.swapLogger.info(savedSwap, "SC: ClaimEvent: invoice settled, secret: "+secretHex);
|
|
280
265
|
savedSwap.secret = secretHex;
|
|
281
266
|
if(savedSwap.metadata!=null) savedSwap.metadata.times.htlcSettled = Date.now();
|
|
@@ -302,10 +287,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
302
287
|
this.swapLogger.info(savedSwap, "SC: RefundEvent: swap refunded to us, invoice: "+savedSwap.pr);
|
|
303
288
|
|
|
304
289
|
try {
|
|
305
|
-
await
|
|
306
|
-
lnd: this.LND,
|
|
307
|
-
id: event.paymentHash
|
|
308
|
-
});
|
|
290
|
+
await this.lightning.cancelHodlInvoice(event.paymentHash);
|
|
309
291
|
this.swapLogger.info(savedSwap, "SC: RefundEvent: invoice cancelled");
|
|
310
292
|
await this.removeSwapData(savedSwap, FromBtcLnSwapState.REFUNDED);
|
|
311
293
|
} catch (e) {
|
|
@@ -425,7 +407,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
425
407
|
* @param abortController
|
|
426
408
|
*/
|
|
427
409
|
private getBlockheightPrefetch(abortController: AbortController): Promise<number> {
|
|
428
|
-
return
|
|
410
|
+
return this.lightning.getBlockheight().catch(e => {
|
|
429
411
|
this.logger.error("getBlockheightPrefetch(): error", e);
|
|
430
412
|
abortController.abort(e);
|
|
431
413
|
return null;
|
|
@@ -438,8 +420,8 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
438
420
|
* @param responseStream
|
|
439
421
|
*/
|
|
440
422
|
private sendPublicKeyAsync(responseStream: ServerParamEncoder) {
|
|
441
|
-
|
|
442
|
-
lnPublicKey:
|
|
423
|
+
this.lightning.getIdentityPublicKey().then(publicKey => responseStream.writeParams({
|
|
424
|
+
lnPublicKey: publicKey
|
|
443
425
|
})).catch(e => {
|
|
444
426
|
this.logger.error("sendPublicKeyAsync(): error", e);
|
|
445
427
|
});
|
|
@@ -451,7 +433,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
451
433
|
*
|
|
452
434
|
* @param invoice
|
|
453
435
|
*/
|
|
454
|
-
private getInvoicePaymentsTimeout(invoice:
|
|
436
|
+
private getInvoicePaymentsTimeout(invoice: LightningNetworkInvoice) {
|
|
455
437
|
let timeout: number = null;
|
|
456
438
|
invoice.payments.forEach((curr) => {
|
|
457
439
|
if (timeout == null || timeout > curr.timeout) timeout = curr.timeout;
|
|
@@ -468,7 +450,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
468
450
|
* @throws {DefinedRuntimeError} Will throw if HTLC expires too soon and therefore cannot be processed
|
|
469
451
|
* @returns expiry timeout in seconds
|
|
470
452
|
*/
|
|
471
|
-
private async checkHtlcExpiry(invoice:
|
|
453
|
+
private async checkHtlcExpiry(invoice: LightningNetworkInvoice, blockheightPrefetch: Promise<number>, signal: AbortSignal): Promise<BN> {
|
|
472
454
|
const timeout: number = this.getInvoicePaymentsTimeout(invoice);
|
|
473
455
|
const current_block_height = await blockheightPrefetch;
|
|
474
456
|
signal.throwIfAborted();
|
|
@@ -499,10 +481,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
499
481
|
if(invoiceData.state!==FromBtcLnSwapState.CREATED) return;
|
|
500
482
|
await invoiceData.setState(FromBtcLnSwapState.CANCELED);
|
|
501
483
|
const paymentHash = invoiceData.data.getHash();
|
|
502
|
-
await
|
|
503
|
-
id: paymentHash,
|
|
504
|
-
lnd: this.LND
|
|
505
|
-
});
|
|
484
|
+
await this.lightning.cancelHodlInvoice(paymentHash);
|
|
506
485
|
await this.removeSwapData(invoiceData);
|
|
507
486
|
this.swapLogger.info(invoiceData, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", invoiceData.pr);
|
|
508
487
|
};
|
|
@@ -536,10 +515,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
536
515
|
* @returns the fetched lightning invoice
|
|
537
516
|
*/
|
|
538
517
|
private async checkInvoiceStatus(paymentHash: string): Promise<any> {
|
|
539
|
-
const invoice = await
|
|
540
|
-
id: paymentHash,
|
|
541
|
-
lnd: this.LND
|
|
542
|
-
});
|
|
518
|
+
const invoice = await this.lightning.getInvoice(paymentHash);
|
|
543
519
|
if(invoice==null) throw {
|
|
544
520
|
_httpStatus: 200,
|
|
545
521
|
code: 10001,
|
|
@@ -563,26 +539,28 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
563
539
|
msg: "Invoice expired/canceled"
|
|
564
540
|
};
|
|
565
541
|
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
542
|
+
switch(invoice.status) {
|
|
543
|
+
case "canceled":
|
|
544
|
+
throw {
|
|
545
|
+
_httpStatus: 200,
|
|
546
|
+
code: 10001,
|
|
547
|
+
msg: "Invoice expired/canceled"
|
|
548
|
+
}
|
|
549
|
+
case "confirmed":
|
|
550
|
+
throw {
|
|
551
|
+
_httpStatus: 200,
|
|
552
|
+
code: 10002,
|
|
553
|
+
msg: "Invoice already paid"
|
|
554
|
+
};
|
|
555
|
+
case "unpaid":
|
|
556
|
+
throw {
|
|
557
|
+
_httpStatus: 200,
|
|
558
|
+
code: 10003,
|
|
559
|
+
msg: "Invoice yet unpaid"
|
|
560
|
+
};
|
|
561
|
+
default:
|
|
562
|
+
return invoice;
|
|
583
563
|
}
|
|
584
|
-
|
|
585
|
-
return invoice;
|
|
586
564
|
}
|
|
587
565
|
|
|
588
566
|
startRestServer(restServer: Express) {
|
|
@@ -654,7 +632,7 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
654
632
|
//Pre-fetch data
|
|
655
633
|
const {pricePrefetchPromise, securityDepositPricePrefetchPromise} = this.getFromBtcPricePrefetches(chainIdentifier, useToken, abortController);
|
|
656
634
|
const balancePrefetch: Promise<BN> = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
|
|
657
|
-
const channelsPrefetch: Promise<
|
|
635
|
+
const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.getChannelsPrefetch(abortController);
|
|
658
636
|
|
|
659
637
|
const dummySwapData = await this.getDummySwapData(chainIdentifier, useToken, parsedBody.address, parsedBody.paymentHash);
|
|
660
638
|
abortController.signal.throwIfAborted();
|
|
@@ -678,23 +656,28 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
678
656
|
metadata.times.balanceChecked = Date.now();
|
|
679
657
|
|
|
680
658
|
//Create swap
|
|
681
|
-
const hodlInvoiceObj:
|
|
659
|
+
const hodlInvoiceObj: HodlInvoiceInit = {
|
|
682
660
|
description: chainIdentifier+"-"+parsedBody.address,
|
|
683
|
-
|
|
684
|
-
|
|
661
|
+
cltvDelta: this.config.minCltv.add(new BN(5)).toNumber(),
|
|
662
|
+
expiresAt: Date.now()+(this.config.invoiceTimeoutSeconds*1000),
|
|
685
663
|
id: parsedBody.paymentHash,
|
|
686
|
-
|
|
687
|
-
|
|
664
|
+
mtokens: amountBD.mul(new BN(1000)),
|
|
665
|
+
descriptionHash: parsedBody.descriptionHash
|
|
688
666
|
};
|
|
689
|
-
metadata.invoiceRequest =
|
|
690
|
-
hodlInvoiceObj.lnd = this.LND;
|
|
667
|
+
metadata.invoiceRequest = hodlInvoiceObj;
|
|
691
668
|
|
|
692
|
-
const hodlInvoice = await
|
|
669
|
+
const hodlInvoice = await this.lightning.createHodlInvoice(hodlInvoiceObj);
|
|
693
670
|
abortController.signal.throwIfAborted();
|
|
694
671
|
metadata.times.invoiceCreated = Date.now();
|
|
695
672
|
metadata.invoiceResponse = {...hodlInvoice};
|
|
696
673
|
|
|
697
|
-
const createdSwap = new FromBtcLnSwapAbs(
|
|
674
|
+
const createdSwap = new FromBtcLnSwapAbs(
|
|
675
|
+
chainIdentifier,
|
|
676
|
+
hodlInvoice.request,
|
|
677
|
+
hodlInvoice.mtokens,
|
|
678
|
+
swapFee,
|
|
679
|
+
swapFeeInToken
|
|
680
|
+
);
|
|
698
681
|
|
|
699
682
|
//Pre-compute the security deposit
|
|
700
683
|
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod);
|
|
@@ -850,6 +833,13 @@ export class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, Fro
|
|
|
850
833
|
|
|
851
834
|
async init() {
|
|
852
835
|
await this.storageManager.loadData(FromBtcLnSwapAbs);
|
|
836
|
+
//Check if all swaps contain a valid amount
|
|
837
|
+
for(let swap of await this.storageManager.query([])) {
|
|
838
|
+
if(swap.amount==null) {
|
|
839
|
+
const parsedPR = await this.lightning.parsePaymentRequest(swap.pr);
|
|
840
|
+
swap.amount = parsedPR.mtokens.add(new BN(999)).div(new BN(1000));
|
|
841
|
+
}
|
|
842
|
+
}
|
|
853
843
|
this.subscribeToEvents();
|
|
854
844
|
await PluginManager.serviceInitialize(this);
|
|
855
845
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as BN from "bn.js";
|
|
2
2
|
import {SwapData} from "@atomiqlabs/base";
|
|
3
3
|
import {SwapHandlerType} from "../..";
|
|
4
|
-
import * as bolt11 from "@atomiqlabs/bolt11";
|
|
5
4
|
import {FromBtcBaseSwap} from "../FromBtcBaseSwap";
|
|
6
5
|
|
|
7
6
|
export enum FromBtcLnSwapState {
|
|
@@ -19,19 +18,15 @@ export class FromBtcLnSwapAbs<T extends SwapData = SwapData> extends FromBtcBase
|
|
|
19
18
|
readonly pr: string;
|
|
20
19
|
|
|
21
20
|
nonce: number;
|
|
22
|
-
prefix: string;
|
|
23
|
-
timeout: string;
|
|
24
|
-
signature: string;
|
|
25
|
-
feeRate: string;
|
|
26
21
|
|
|
27
22
|
secret: string;
|
|
28
23
|
|
|
29
|
-
constructor(chainIdentifier: string, pr: string, swapFee: BN, swapFeeInToken: BN);
|
|
24
|
+
constructor(chainIdentifier: string, pr: string, amountMtokens: BN, swapFee: BN, swapFeeInToken: BN);
|
|
30
25
|
constructor(obj: any);
|
|
31
26
|
|
|
32
|
-
constructor(chainIdOrObj: string | any, pr?: string, swapFee?: BN, swapFeeInToken?: BN) {
|
|
27
|
+
constructor(chainIdOrObj: string | any, pr?: string, amountMtokens?: BN, swapFee?: BN, swapFeeInToken?: BN) {
|
|
33
28
|
if(typeof(chainIdOrObj)==="string") {
|
|
34
|
-
super(chainIdOrObj, swapFee, swapFeeInToken);
|
|
29
|
+
super(chainIdOrObj, amountMtokens.add(new BN(999)).div(new BN(1000)), swapFee, swapFeeInToken);
|
|
35
30
|
this.state = FromBtcLnSwapState.CREATED;
|
|
36
31
|
this.pr = pr;
|
|
37
32
|
} else {
|
|
@@ -39,10 +34,6 @@ export class FromBtcLnSwapAbs<T extends SwapData = SwapData> extends FromBtcBase
|
|
|
39
34
|
this.pr = chainIdOrObj.pr;
|
|
40
35
|
this.secret = chainIdOrObj.secret;
|
|
41
36
|
this.nonce = chainIdOrObj.nonce;
|
|
42
|
-
this.prefix = chainIdOrObj.prefix;
|
|
43
|
-
this.timeout = chainIdOrObj.timeout;
|
|
44
|
-
this.signature = chainIdOrObj.signature;
|
|
45
|
-
this.feeRate = chainIdOrObj.feeRate;
|
|
46
37
|
}
|
|
47
38
|
this.type = SwapHandlerType.FROM_BTCLN;
|
|
48
39
|
}
|
|
@@ -52,10 +43,6 @@ export class FromBtcLnSwapAbs<T extends SwapData = SwapData> extends FromBtcBase
|
|
|
52
43
|
partialSerialized.pr = this.pr;
|
|
53
44
|
partialSerialized.secret = this.secret;
|
|
54
45
|
partialSerialized.nonce = this.nonce;
|
|
55
|
-
partialSerialized.prefix = this.prefix;
|
|
56
|
-
partialSerialized.timeout = this.timeout;
|
|
57
|
-
partialSerialized.signature = this.signature;
|
|
58
|
-
partialSerialized.feeRate = this.feeRate;
|
|
59
46
|
return partialSerialized;
|
|
60
47
|
}
|
|
61
48
|
|
|
@@ -75,8 +62,4 @@ export class FromBtcLnSwapAbs<T extends SwapData = SwapData> extends FromBtcBase
|
|
|
75
62
|
return this.state===FromBtcLnSwapState.SETTLED;
|
|
76
63
|
}
|
|
77
64
|
|
|
78
|
-
getTotalInputAmount(): BN {
|
|
79
|
-
return new BN(bolt11.decode(this.pr).millisatoshis).add(new BN(999)).div(new BN(1000));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
65
|
}
|