@atomiqlabs/lp-lib 10.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/fees/IBtcFeeEstimator.d.ts +3 -0
- package/dist/fees/IBtcFeeEstimator.js +2 -0
- package/dist/fees/OneDollarFeeEstimator.d.ts +16 -0
- package/dist/fees/OneDollarFeeEstimator.js +71 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +52 -0
- package/dist/info/InfoHandler.d.ts +17 -0
- package/dist/info/InfoHandler.js +70 -0
- package/dist/plugins/IPlugin.d.ts +118 -0
- package/dist/plugins/IPlugin.js +33 -0
- package/dist/plugins/PluginManager.d.ts +89 -0
- package/dist/plugins/PluginManager.js +263 -0
- package/dist/prices/BinanceSwapPrice.d.ts +27 -0
- package/dist/prices/BinanceSwapPrice.js +106 -0
- package/dist/prices/CoinGeckoSwapPrice.d.ts +31 -0
- package/dist/prices/CoinGeckoSwapPrice.js +76 -0
- package/dist/storage/IIntermediaryStorage.d.ts +15 -0
- package/dist/storage/IIntermediaryStorage.js +2 -0
- package/dist/storagemanager/IntermediaryStorageManager.d.ts +15 -0
- package/dist/storagemanager/IntermediaryStorageManager.js +113 -0
- package/dist/storagemanager/StorageManager.d.ts +12 -0
- package/dist/storagemanager/StorageManager.js +74 -0
- package/dist/swaps/FromBtcBaseSwap.d.ts +12 -0
- package/dist/swaps/FromBtcBaseSwap.js +16 -0
- package/dist/swaps/FromBtcBaseSwapHandler.d.ts +118 -0
- package/dist/swaps/FromBtcBaseSwapHandler.js +294 -0
- package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +25 -0
- package/dist/swaps/FromBtcLnBaseSwapHandler.js +55 -0
- package/dist/swaps/ISwapPrice.d.ts +44 -0
- package/dist/swaps/ISwapPrice.js +73 -0
- package/dist/swaps/SwapHandler.d.ts +186 -0
- package/dist/swaps/SwapHandler.js +292 -0
- package/dist/swaps/SwapHandlerSwap.d.ts +75 -0
- package/dist/swaps/SwapHandlerSwap.js +72 -0
- package/dist/swaps/ToBtcBaseSwap.d.ts +35 -0
- package/dist/swaps/ToBtcBaseSwap.js +61 -0
- package/dist/swaps/ToBtcBaseSwapHandler.d.ts +94 -0
- package/dist/swaps/ToBtcBaseSwapHandler.js +233 -0
- package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +92 -0
- package/dist/swaps/frombtc_abstract/FromBtcAbs.js +386 -0
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +26 -0
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +63 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +55 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +586 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +43 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +99 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +105 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +731 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +29 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +64 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +79 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +514 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +28 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +66 -0
- package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +290 -0
- package/dist/swaps/tobtc_abstract/ToBtcAbs.js +1056 -0
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +29 -0
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +70 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +246 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +1169 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +27 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +65 -0
- package/dist/utils/Utils.d.ts +32 -0
- package/dist/utils/Utils.js +109 -0
- package/dist/utils/coinselect2/accumulative.d.ts +6 -0
- package/dist/utils/coinselect2/accumulative.js +44 -0
- package/dist/utils/coinselect2/blackjack.d.ts +6 -0
- package/dist/utils/coinselect2/blackjack.js +41 -0
- package/dist/utils/coinselect2/index.d.ts +16 -0
- package/dist/utils/coinselect2/index.js +40 -0
- package/dist/utils/coinselect2/utils.d.ts +64 -0
- package/dist/utils/coinselect2/utils.js +121 -0
- package/dist/utils/paramcoders/IParamReader.d.ts +5 -0
- package/dist/utils/paramcoders/IParamReader.js +2 -0
- package/dist/utils/paramcoders/IParamWriter.d.ts +4 -0
- package/dist/utils/paramcoders/IParamWriter.js +2 -0
- package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -0
- package/dist/utils/paramcoders/LegacyParamEncoder.js +33 -0
- package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -0
- package/dist/utils/paramcoders/ParamDecoder.js +234 -0
- package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -0
- package/dist/utils/paramcoders/ParamEncoder.js +22 -0
- package/dist/utils/paramcoders/SchemaVerifier.d.ts +22 -0
- package/dist/utils/paramcoders/SchemaVerifier.js +85 -0
- package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -0
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +105 -0
- package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -0
- package/dist/utils/paramcoders/server/ServerParamEncoder.js +76 -0
- package/package.json +43 -0
- package/src/fees/IBtcFeeEstimator.ts +7 -0
- package/src/fees/OneDollarFeeEstimator.ts +95 -0
- package/src/index.ts +46 -0
- package/src/info/InfoHandler.ts +106 -0
- package/src/plugins/IPlugin.ts +155 -0
- package/src/plugins/PluginManager.ts +310 -0
- package/src/prices/BinanceSwapPrice.ts +114 -0
- package/src/prices/CoinGeckoSwapPrice.ts +88 -0
- package/src/storage/IIntermediaryStorage.ts +21 -0
- package/src/storagemanager/IntermediaryStorageManager.ts +101 -0
- package/src/storagemanager/StorageManager.ts +68 -0
- package/src/swaps/FromBtcBaseSwap.ts +21 -0
- package/src/swaps/FromBtcBaseSwapHandler.ts +375 -0
- package/src/swaps/FromBtcLnBaseSwapHandler.ts +48 -0
- package/src/swaps/ISwapPrice.ts +94 -0
- package/src/swaps/SwapHandler.ts +404 -0
- package/src/swaps/SwapHandlerSwap.ts +133 -0
- package/src/swaps/ToBtcBaseSwap.ts +76 -0
- package/src/swaps/ToBtcBaseSwapHandler.ts +309 -0
- package/src/swaps/frombtc_abstract/FromBtcAbs.ts +484 -0
- package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +77 -0
- package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +661 -0
- package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +158 -0
- package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +864 -0
- package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +82 -0
- package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +592 -0
- package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +90 -0
- package/src/swaps/tobtc_abstract/ToBtcAbs.ts +1249 -0
- package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +112 -0
- package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +1422 -0
- package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +87 -0
- package/src/utils/Utils.ts +108 -0
- package/src/utils/coinselect2/accumulative.js +32 -0
- package/src/utils/coinselect2/accumulative.ts +58 -0
- package/src/utils/coinselect2/blackjack.js +29 -0
- package/src/utils/coinselect2/blackjack.ts +54 -0
- package/src/utils/coinselect2/index.js +16 -0
- package/src/utils/coinselect2/index.ts +50 -0
- package/src/utils/coinselect2/utils.js +110 -0
- package/src/utils/coinselect2/utils.ts +183 -0
- package/src/utils/paramcoders/IParamReader.ts +8 -0
- package/src/utils/paramcoders/IParamWriter.ts +8 -0
- package/src/utils/paramcoders/LegacyParamEncoder.ts +28 -0
- package/src/utils/paramcoders/ParamDecoder.ts +219 -0
- package/src/utils/paramcoders/ParamEncoder.ts +30 -0
- package/src/utils/paramcoders/SchemaVerifier.ts +97 -0
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +115 -0
- package/src/utils/paramcoders/server/ServerParamEncoder.ts +76 -0
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FromBtcLnAbs = void 0;
|
|
13
|
+
const BN = require("bn.js");
|
|
14
|
+
const lncli = require("ln-service");
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
const bolt11 = require("@atomiqlabs/bolt11");
|
|
17
|
+
const FromBtcLnSwapAbs_1 = require("./FromBtcLnSwapAbs");
|
|
18
|
+
const SwapHandler_1 = require("../SwapHandler");
|
|
19
|
+
const base_1 = require("@atomiqlabs/base");
|
|
20
|
+
const Utils_1 = require("../../utils/Utils");
|
|
21
|
+
const PluginManager_1 = require("../../plugins/PluginManager");
|
|
22
|
+
const SchemaVerifier_1 = require("../../utils/paramcoders/SchemaVerifier");
|
|
23
|
+
const ServerParamDecoder_1 = require("../../utils/paramcoders/server/ServerParamDecoder");
|
|
24
|
+
const FromBtcLnBaseSwapHandler_1 = require("../FromBtcLnBaseSwapHandler");
|
|
25
|
+
/**
|
|
26
|
+
* Swap handler handling from BTCLN swaps using submarine swaps
|
|
27
|
+
*/
|
|
28
|
+
class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
29
|
+
constructor(storageDirectory, path, chains, lnd, swapPricing, config) {
|
|
30
|
+
super(storageDirectory, path, chains, lnd, swapPricing);
|
|
31
|
+
this.type = SwapHandler_1.SwapHandlerType.FROM_BTCLN;
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
|
|
34
|
+
}
|
|
35
|
+
processPastSwap(swap) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const { swapContract } = this.getChain(swap.chainIdentifier);
|
|
38
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED) {
|
|
39
|
+
//Check if already paid
|
|
40
|
+
const parsedPR = bolt11.decode(swap.pr);
|
|
41
|
+
const invoice = yield lncli.getInvoice({
|
|
42
|
+
id: parsedPR.tagsObject.payment_hash,
|
|
43
|
+
lnd: this.LND
|
|
44
|
+
});
|
|
45
|
+
const isBeingPaid = invoice.is_held;
|
|
46
|
+
if (!isBeingPaid) {
|
|
47
|
+
//Not paid
|
|
48
|
+
const isInvoiceExpired = parsedPR.timeExpireDate < Date.now() / 1000;
|
|
49
|
+
if (!isInvoiceExpired)
|
|
50
|
+
return null;
|
|
51
|
+
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: " + swap.pr);
|
|
52
|
+
yield swap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED);
|
|
53
|
+
return "CANCEL";
|
|
54
|
+
}
|
|
55
|
+
//Adjust the state of the swap and expiry
|
|
56
|
+
try {
|
|
57
|
+
yield this.htlcReceived(swap, invoice);
|
|
58
|
+
//Result is either FromBtcLnSwapState.RECEIVED or FromBtcLnSwapState.CANCELED
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
this.swapLogger.error(swap, "processPastSwap(state=CREATED): htlcReceived error", e);
|
|
62
|
+
}
|
|
63
|
+
// @ts-ignore Previous call (htlcReceived) mutates the state of the swap, so this is valid
|
|
64
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED) {
|
|
65
|
+
this.swapLogger.info(swap, "processPastSwap(state=CREATED): invoice CANCELED after htlcReceived(), cancelling, invoice: " + swap.pr);
|
|
66
|
+
return "CANCEL";
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED) {
|
|
71
|
+
const parsedPR = bolt11.decode(swap.pr);
|
|
72
|
+
const isAuthorizationExpired = yield swapContract.isInitAuthorizationExpired(swap.data, swap);
|
|
73
|
+
if (isAuthorizationExpired) {
|
|
74
|
+
const isCommited = yield swapContract.isCommited(swap.data);
|
|
75
|
+
if (!isCommited) {
|
|
76
|
+
this.swapLogger.info(swap, "processPastSwap(state=RECEIVED): swap not committed before authorization expiry, cancelling the LN invoice, invoice: " + swap.pr);
|
|
77
|
+
yield swap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED);
|
|
78
|
+
return "CANCEL";
|
|
79
|
+
}
|
|
80
|
+
this.swapLogger.info(swap, "processPastSwap(state=RECEIVED): swap committed (detected from processPastSwap), invoice: " + swap.pr);
|
|
81
|
+
yield swap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.COMMITED);
|
|
82
|
+
yield this.storageManager.saveData(swap.data.getHash(), null, swap);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED || swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.COMMITED) {
|
|
86
|
+
const expiryTime = swap.data.getExpiry();
|
|
87
|
+
const currentTime = new BN(Math.floor(Date.now() / 1000) - this.config.maxSkew);
|
|
88
|
+
const isExpired = expiryTime != null && expiryTime.lt(currentTime);
|
|
89
|
+
if (!isExpired)
|
|
90
|
+
return null;
|
|
91
|
+
const isCommited = yield swapContract.isCommited(swap.data);
|
|
92
|
+
if (isCommited) {
|
|
93
|
+
this.swapLogger.info(swap, "processPastSwap(state=COMMITED): swap timed out, refunding to self, invoice: " + swap.pr);
|
|
94
|
+
return "REFUND";
|
|
95
|
+
}
|
|
96
|
+
this.swapLogger.info(swap, "processPastSwap(state=RECEIVED): swap timed out, cancelling the LN invoice, invoice: " + swap.pr);
|
|
97
|
+
return "CANCEL";
|
|
98
|
+
}
|
|
99
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CLAIMED)
|
|
100
|
+
return "SETTLE";
|
|
101
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED)
|
|
102
|
+
return "CANCEL";
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
refundSwaps(refundSwaps) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
for (let refundSwap of refundSwaps) {
|
|
108
|
+
const { swapContract, signer } = this.getChain(refundSwap.chainIdentifier);
|
|
109
|
+
const unlock = refundSwap.lock(swapContract.refundTimeout);
|
|
110
|
+
if (unlock == null)
|
|
111
|
+
continue;
|
|
112
|
+
this.swapLogger.debug(refundSwap, "refundSwaps(): initiate refund of swap");
|
|
113
|
+
yield swapContract.refund(signer, refundSwap.data, true, false, { waitForConfirmation: true });
|
|
114
|
+
this.swapLogger.info(refundSwap, "refundsSwaps(): swap refunded, invoice: " + refundSwap.pr);
|
|
115
|
+
yield refundSwap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.REFUNDED);
|
|
116
|
+
unlock();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
cancelInvoices(swaps) {
|
|
121
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
+
for (let swap of swaps) {
|
|
123
|
+
//Refund
|
|
124
|
+
const paymentHash = swap.data.getHash();
|
|
125
|
+
try {
|
|
126
|
+
yield lncli.cancelHodlInvoice({
|
|
127
|
+
lnd: this.LND,
|
|
128
|
+
id: paymentHash
|
|
129
|
+
});
|
|
130
|
+
this.swapLogger.info(swap, "cancelInvoices(): invoice cancelled!");
|
|
131
|
+
yield this.removeSwapData(swap);
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
this.swapLogger.error(swap, "cancelInvoices(): cannot cancel hodl invoice id", e);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
settleInvoices(swaps) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
for (let swap of swaps) {
|
|
142
|
+
try {
|
|
143
|
+
yield lncli.settleHodlInvoice({
|
|
144
|
+
lnd: this.LND,
|
|
145
|
+
secret: swap.secret
|
|
146
|
+
});
|
|
147
|
+
if (swap.metadata != null)
|
|
148
|
+
swap.metadata.times.htlcSettled = Date.now();
|
|
149
|
+
yield this.removeSwapData(swap, FromBtcLnSwapAbs_1.FromBtcLnSwapState.SETTLED);
|
|
150
|
+
this.swapLogger.info(swap, "settleInvoices(): invoice settled, secret: " + swap.secret);
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
this.swapLogger.error(swap, "settleInvoices(): cannot settle invoice", e);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Checks past swaps, refunds and deletes ones that are already expired.
|
|
160
|
+
*/
|
|
161
|
+
processPastSwaps() {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
const settleInvoices = [];
|
|
164
|
+
const cancelInvoices = [];
|
|
165
|
+
const refundSwaps = [];
|
|
166
|
+
const queriedData = yield this.storageManager.query([
|
|
167
|
+
{
|
|
168
|
+
key: "state",
|
|
169
|
+
value: [
|
|
170
|
+
FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED,
|
|
171
|
+
FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED,
|
|
172
|
+
FromBtcLnSwapAbs_1.FromBtcLnSwapState.COMMITED,
|
|
173
|
+
FromBtcLnSwapAbs_1.FromBtcLnSwapState.CLAIMED,
|
|
174
|
+
FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED,
|
|
175
|
+
]
|
|
176
|
+
}
|
|
177
|
+
]);
|
|
178
|
+
for (let swap of queriedData) {
|
|
179
|
+
switch (yield this.processPastSwap(swap)) {
|
|
180
|
+
case "CANCEL":
|
|
181
|
+
cancelInvoices.push(swap);
|
|
182
|
+
break;
|
|
183
|
+
case "SETTLE":
|
|
184
|
+
settleInvoices.push(swap);
|
|
185
|
+
break;
|
|
186
|
+
case "REFUND":
|
|
187
|
+
refundSwaps.push(swap);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
yield this.refundSwaps(refundSwaps);
|
|
192
|
+
yield this.cancelInvoices(cancelInvoices);
|
|
193
|
+
yield this.settleInvoices(settleInvoices);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
processInitializeEvent(chainIdentifier, event) {
|
|
197
|
+
var _a;
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
//Only process HTLC requests
|
|
200
|
+
if (event.swapType !== base_1.ChainSwapType.HTLC)
|
|
201
|
+
return;
|
|
202
|
+
const swapData = yield event.swapData();
|
|
203
|
+
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
204
|
+
if (!swapData.isOfferer(signer.getAddress()))
|
|
205
|
+
return;
|
|
206
|
+
if (swapData.isPayIn())
|
|
207
|
+
return;
|
|
208
|
+
const paymentHash = event.paymentHash;
|
|
209
|
+
const savedSwap = yield this.storageManager.getData(paymentHash, null);
|
|
210
|
+
if (savedSwap == null || savedSwap.chainIdentifier !== chainIdentifier)
|
|
211
|
+
return;
|
|
212
|
+
this.swapLogger.info(savedSwap, "SC: InitializeEvent: HTLC initialized by the client, invoice: " + savedSwap.pr);
|
|
213
|
+
savedSwap.txIds.init = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
214
|
+
if (savedSwap.metadata != null)
|
|
215
|
+
savedSwap.metadata.times.initTxReceived = Date.now();
|
|
216
|
+
if (savedSwap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED) {
|
|
217
|
+
yield savedSwap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.COMMITED);
|
|
218
|
+
savedSwap.data = swapData;
|
|
219
|
+
yield this.storageManager.saveData(paymentHash, null, savedSwap);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
processClaimEvent(chainIdentifier, event) {
|
|
224
|
+
var _a;
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
//Claim
|
|
227
|
+
//This is the important part, we need to catch the claim TX, else we may lose money
|
|
228
|
+
const secret = Buffer.from(event.secret, "hex");
|
|
229
|
+
const paymentHash = (0, crypto_1.createHash)("sha256").update(secret).digest();
|
|
230
|
+
const secretHex = secret.toString("hex");
|
|
231
|
+
const paymentHashHex = paymentHash.toString("hex");
|
|
232
|
+
const savedSwap = yield this.storageManager.getData(paymentHashHex, null);
|
|
233
|
+
if (savedSwap == null || savedSwap.chainIdentifier !== chainIdentifier)
|
|
234
|
+
return;
|
|
235
|
+
savedSwap.txIds.claim = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
236
|
+
if (savedSwap.metadata != null)
|
|
237
|
+
savedSwap.metadata.times.claimTxReceived = Date.now();
|
|
238
|
+
this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap HTLC successfully claimed by the client, invoice: " + savedSwap.pr);
|
|
239
|
+
try {
|
|
240
|
+
yield lncli.settleHodlInvoice({
|
|
241
|
+
lnd: this.LND,
|
|
242
|
+
secret: secretHex
|
|
243
|
+
});
|
|
244
|
+
this.swapLogger.info(savedSwap, "SC: ClaimEvent: invoice settled, secret: " + secretHex);
|
|
245
|
+
savedSwap.secret = secretHex;
|
|
246
|
+
if (savedSwap.metadata != null)
|
|
247
|
+
savedSwap.metadata.times.htlcSettled = Date.now();
|
|
248
|
+
yield this.removeSwapData(savedSwap, FromBtcLnSwapAbs_1.FromBtcLnSwapState.SETTLED);
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
this.swapLogger.error(savedSwap, "SC: ClaimEvent: cannot settle invoice", e);
|
|
252
|
+
savedSwap.secret = secretHex;
|
|
253
|
+
yield savedSwap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.CLAIMED);
|
|
254
|
+
yield this.storageManager.saveData(paymentHashHex, null, savedSwap);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
processRefundEvent(chainIdentifier, event) {
|
|
259
|
+
var _a;
|
|
260
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
261
|
+
//Refund
|
|
262
|
+
//Try to get the hash from the refundMap
|
|
263
|
+
if (event.paymentHash == null)
|
|
264
|
+
return;
|
|
265
|
+
const savedSwap = yield this.storageManager.getData(event.paymentHash, null);
|
|
266
|
+
if (savedSwap == null || savedSwap.chainIdentifier !== chainIdentifier)
|
|
267
|
+
return;
|
|
268
|
+
savedSwap.txIds.refund = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
269
|
+
this.swapLogger.info(savedSwap, "SC: RefundEvent: swap refunded to us, invoice: " + savedSwap.pr);
|
|
270
|
+
try {
|
|
271
|
+
yield lncli.cancelHodlInvoice({
|
|
272
|
+
lnd: this.LND,
|
|
273
|
+
id: event.paymentHash
|
|
274
|
+
});
|
|
275
|
+
this.swapLogger.info(savedSwap, "SC: RefundEvent: invoice cancelled");
|
|
276
|
+
yield this.removeSwapData(savedSwap, FromBtcLnSwapAbs_1.FromBtcLnSwapState.REFUNDED);
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
this.swapLogger.error(savedSwap, "SC: RefundEvent: cannot cancel invoice", e);
|
|
280
|
+
yield savedSwap.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED);
|
|
281
|
+
// await PluginManager.swapStateChange(savedSwap);
|
|
282
|
+
yield this.storageManager.saveData(event.paymentHash, null, savedSwap);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
|
|
288
|
+
* smart chain authorization starts ticking as soon as this HTLC is received
|
|
289
|
+
*
|
|
290
|
+
* @param invoiceData
|
|
291
|
+
* @param invoice
|
|
292
|
+
*/
|
|
293
|
+
htlcReceived(invoiceData, invoice) {
|
|
294
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
295
|
+
this.swapLogger.debug(invoiceData, "htlcReceived(): invoice: ", invoice);
|
|
296
|
+
if (invoiceData.metadata != null)
|
|
297
|
+
invoiceData.metadata.times.htlcReceived = Date.now();
|
|
298
|
+
const useToken = invoiceData.data.getToken();
|
|
299
|
+
const sendAmount = invoiceData.data.getAmount();
|
|
300
|
+
//Create abort controller for parallel fetches
|
|
301
|
+
const abortController = new AbortController();
|
|
302
|
+
//Pre-fetch data
|
|
303
|
+
const balancePrefetch = this.getBalancePrefetch(invoiceData.chainIdentifier, useToken, abortController);
|
|
304
|
+
const blockheightPrefetch = this.getBlockheightPrefetch(abortController);
|
|
305
|
+
const signDataPrefetchPromise = this.getSignDataPrefetch(invoiceData.chainIdentifier, abortController);
|
|
306
|
+
let expiryTimeout;
|
|
307
|
+
try {
|
|
308
|
+
//Check if we have enough liquidity to proceed
|
|
309
|
+
yield this.checkBalance(sendAmount, balancePrefetch, abortController.signal);
|
|
310
|
+
if (invoiceData.metadata != null)
|
|
311
|
+
invoiceData.metadata.times.htlcBalanceChecked = Date.now();
|
|
312
|
+
//Check if HTLC expiry is long enough
|
|
313
|
+
expiryTimeout = yield this.checkHtlcExpiry(invoice, blockheightPrefetch, abortController.signal);
|
|
314
|
+
if (invoiceData.metadata != null)
|
|
315
|
+
invoiceData.metadata.times.htlcTimeoutCalculated = Date.now();
|
|
316
|
+
}
|
|
317
|
+
catch (e) {
|
|
318
|
+
if (!abortController.signal.aborted) {
|
|
319
|
+
if (invoiceData.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED)
|
|
320
|
+
yield this.cancelSwapAndInvoice(invoiceData);
|
|
321
|
+
}
|
|
322
|
+
throw e;
|
|
323
|
+
}
|
|
324
|
+
const { swapContract, signer } = this.getChain(invoiceData.chainIdentifier);
|
|
325
|
+
//Create real swap data
|
|
326
|
+
const payInvoiceObject = yield swapContract.createSwapData(base_1.ChainSwapType.HTLC, signer.getAddress(), invoiceData.data.getClaimer(), useToken, sendAmount, invoice.id, new BN(0), new BN(Math.floor(Date.now() / 1000)).add(expiryTimeout), new BN(0), 0, false, true, invoiceData.data.getSecurityDeposit(), new BN(0));
|
|
327
|
+
abortController.signal.throwIfAborted();
|
|
328
|
+
if (invoiceData.metadata != null)
|
|
329
|
+
invoiceData.metadata.times.htlcSwapCreated = Date.now();
|
|
330
|
+
//Sign swap data
|
|
331
|
+
const sigData = yield swapContract.getInitSignature(signer, payInvoiceObject, this.config.authorizationTimeout, signDataPrefetchPromise == null ? null : yield signDataPrefetchPromise, invoiceData.feeRate);
|
|
332
|
+
//No need to check abortController anymore since all pending promises are resolved by now
|
|
333
|
+
if (invoiceData.metadata != null)
|
|
334
|
+
invoiceData.metadata.times.htlcSwapSigned = Date.now();
|
|
335
|
+
//Important to prevent race condition and issuing 2 signed init messages at the same time
|
|
336
|
+
if (invoiceData.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED) {
|
|
337
|
+
invoiceData.data = payInvoiceObject;
|
|
338
|
+
invoiceData.prefix = sigData.prefix;
|
|
339
|
+
invoiceData.timeout = sigData.timeout;
|
|
340
|
+
invoiceData.signature = sigData.signature;
|
|
341
|
+
//Setting the state variable is done outside the promise, so is done synchronously
|
|
342
|
+
yield invoiceData.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED);
|
|
343
|
+
yield this.storageManager.saveData(invoice.id, null, invoiceData);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Checks invoice description hash
|
|
350
|
+
*
|
|
351
|
+
* @param descriptionHash
|
|
352
|
+
* @throws {DefinedRuntimeError} will throw an error if the description hash is invalid
|
|
353
|
+
*/
|
|
354
|
+
checkDescriptionHash(descriptionHash) {
|
|
355
|
+
if (descriptionHash != null) {
|
|
356
|
+
if (typeof (descriptionHash) !== "string" || !Utils_1.HEX_REGEX.test(descriptionHash) || descriptionHash.length !== 64) {
|
|
357
|
+
throw {
|
|
358
|
+
code: 20100,
|
|
359
|
+
msg: "Invalid request body (descriptionHash)"
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Starts LN channels pre-fetch
|
|
366
|
+
*
|
|
367
|
+
* @param abortController
|
|
368
|
+
*/
|
|
369
|
+
getBlockheightPrefetch(abortController) {
|
|
370
|
+
return lncli.getHeight({ lnd: this.LND }).then(res => res.current_block_height).catch(e => {
|
|
371
|
+
this.logger.error("getBlockheightPrefetch(): error", e);
|
|
372
|
+
abortController.abort(e);
|
|
373
|
+
return null;
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Asynchronously sends the LN node's public key to the client, so he can pre-fetch the node's channels from 1ml api
|
|
378
|
+
*
|
|
379
|
+
* @param responseStream
|
|
380
|
+
*/
|
|
381
|
+
sendPublicKeyAsync(responseStream) {
|
|
382
|
+
lncli.getWalletInfo({ lnd: this.LND }).then(resp => responseStream.writeParams({
|
|
383
|
+
lnPublicKey: resp.public_key
|
|
384
|
+
})).catch(e => {
|
|
385
|
+
this.logger.error("sendPublicKeyAsync(): error", e);
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Returns the CLTV timeout (blockheight) of the received HTLC corresponding to the invoice. If multiple HTLCs are
|
|
390
|
+
* received (MPP) it returns the lowest of the timeouts
|
|
391
|
+
*
|
|
392
|
+
* @param invoice
|
|
393
|
+
*/
|
|
394
|
+
getInvoicePaymentsTimeout(invoice) {
|
|
395
|
+
let timeout = null;
|
|
396
|
+
invoice.payments.forEach((curr) => {
|
|
397
|
+
if (timeout == null || timeout > curr.timeout)
|
|
398
|
+
timeout = curr.timeout;
|
|
399
|
+
});
|
|
400
|
+
return timeout;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Checks if the received HTLC's CLTV timeout is large enough to still process the swap
|
|
404
|
+
*
|
|
405
|
+
* @param invoice
|
|
406
|
+
* @param blockheightPrefetch
|
|
407
|
+
* @param signal
|
|
408
|
+
* @throws {DefinedRuntimeError} Will throw if HTLC expires too soon and therefore cannot be processed
|
|
409
|
+
* @returns expiry timeout in seconds
|
|
410
|
+
*/
|
|
411
|
+
checkHtlcExpiry(invoice, blockheightPrefetch, signal) {
|
|
412
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
413
|
+
const timeout = this.getInvoicePaymentsTimeout(invoice);
|
|
414
|
+
const current_block_height = yield blockheightPrefetch;
|
|
415
|
+
signal.throwIfAborted();
|
|
416
|
+
const blockDelta = new BN(timeout - current_block_height);
|
|
417
|
+
const htlcExpiresTooSoon = blockDelta.lt(this.config.minCltv);
|
|
418
|
+
if (htlcExpiresTooSoon) {
|
|
419
|
+
throw {
|
|
420
|
+
code: 20002,
|
|
421
|
+
msg: "Not enough time to reliably process the swap",
|
|
422
|
+
data: {
|
|
423
|
+
requiredDelta: this.config.minCltv.toString(10),
|
|
424
|
+
actualDelta: blockDelta.toString(10)
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
return this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Cancels the swap (CANCELED state) & also cancels the LN invoice (including all pending HTLCs)
|
|
433
|
+
*
|
|
434
|
+
* @param invoiceData
|
|
435
|
+
*/
|
|
436
|
+
cancelSwapAndInvoice(invoiceData) {
|
|
437
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
438
|
+
if (invoiceData.state !== FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED)
|
|
439
|
+
return;
|
|
440
|
+
yield invoiceData.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED);
|
|
441
|
+
const paymentHash = invoiceData.data.getHash();
|
|
442
|
+
yield lncli.cancelHodlInvoice({
|
|
443
|
+
id: paymentHash,
|
|
444
|
+
lnd: this.LND
|
|
445
|
+
});
|
|
446
|
+
yield this.removeSwapData(invoiceData);
|
|
447
|
+
this.swapLogger.info(invoiceData, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", invoiceData.pr);
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
;
|
|
451
|
+
getDummySwapData(chainIdentifier, useToken, address, paymentHash) {
|
|
452
|
+
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
453
|
+
return swapContract.createSwapData(base_1.ChainSwapType.HTLC, signer.getAddress(), address, useToken, null, paymentHash, new BN(0), null, null, 0, false, true, null, new BN(0));
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
*
|
|
457
|
+
* Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
|
|
458
|
+
*
|
|
459
|
+
* @param paymentHash
|
|
460
|
+
* @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
|
|
461
|
+
* @returns the fetched lightning invoice
|
|
462
|
+
*/
|
|
463
|
+
checkInvoiceStatus(paymentHash) {
|
|
464
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
465
|
+
const invoice = yield lncli.getInvoice({
|
|
466
|
+
id: paymentHash,
|
|
467
|
+
lnd: this.LND
|
|
468
|
+
});
|
|
469
|
+
if (invoice == null)
|
|
470
|
+
throw {
|
|
471
|
+
_httpStatus: 200,
|
|
472
|
+
code: 10001,
|
|
473
|
+
msg: "Invoice expired/canceled"
|
|
474
|
+
};
|
|
475
|
+
const arr = invoice.description.split("-");
|
|
476
|
+
let chainIdentifier;
|
|
477
|
+
let address;
|
|
478
|
+
if (arr.length > 1) {
|
|
479
|
+
chainIdentifier = arr[0];
|
|
480
|
+
address = arr[1];
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
chainIdentifier = this.chains.default;
|
|
484
|
+
address = invoice.description;
|
|
485
|
+
}
|
|
486
|
+
const { swapContract } = this.getChain(chainIdentifier);
|
|
487
|
+
if (!swapContract.isValidAddress(address))
|
|
488
|
+
throw {
|
|
489
|
+
_httpStatus: 200,
|
|
490
|
+
code: 10001,
|
|
491
|
+
msg: "Invoice expired/canceled"
|
|
492
|
+
};
|
|
493
|
+
const isBeingPaid = invoice.is_held;
|
|
494
|
+
if (!isBeingPaid) {
|
|
495
|
+
if (invoice.is_canceled)
|
|
496
|
+
throw {
|
|
497
|
+
_httpStatus: 200,
|
|
498
|
+
code: 10001,
|
|
499
|
+
msg: "Invoice expired/canceled"
|
|
500
|
+
};
|
|
501
|
+
if (invoice.is_confirmed)
|
|
502
|
+
throw {
|
|
503
|
+
_httpStatus: 200,
|
|
504
|
+
code: 10002,
|
|
505
|
+
msg: "Invoice already paid"
|
|
506
|
+
};
|
|
507
|
+
throw {
|
|
508
|
+
_httpStatus: 200,
|
|
509
|
+
code: 10003,
|
|
510
|
+
msg: "Invoice yet unpaid"
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
return invoice;
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
startRestServer(restServer) {
|
|
517
|
+
restServer.use(this.path + "/createInvoice", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
|
|
518
|
+
restServer.post(this.path + "/createInvoice", (0, Utils_1.expressHandlerWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
519
|
+
var _a;
|
|
520
|
+
const metadata = { request: {}, times: {} };
|
|
521
|
+
const chainIdentifier = (_a = req.query.chain) !== null && _a !== void 0 ? _a : this.chains.default;
|
|
522
|
+
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
523
|
+
metadata.times.requestReceived = Date.now();
|
|
524
|
+
/**
|
|
525
|
+
* address: string solana address of the recipient
|
|
526
|
+
* paymentHash: string payment hash of the to-be-created invoice
|
|
527
|
+
* amount: string amount (in sats) of the invoice
|
|
528
|
+
* token: string Desired token to swap
|
|
529
|
+
* exactOut: boolean Whether the swap should be an exact out instead of exact in swap
|
|
530
|
+
* descriptionHash: string Description hash of the invoice
|
|
531
|
+
*
|
|
532
|
+
*Sent later:
|
|
533
|
+
* feeRate: string Fee rate to use for the init signature
|
|
534
|
+
*/
|
|
535
|
+
const parsedBody = yield req.paramReader.getParams({
|
|
536
|
+
address: (val) => val != null &&
|
|
537
|
+
typeof (val) === "string" &&
|
|
538
|
+
swapContract.isValidAddress(val) ? val : null,
|
|
539
|
+
paymentHash: (val) => val != null &&
|
|
540
|
+
typeof (val) === "string" &&
|
|
541
|
+
val.length === 64 &&
|
|
542
|
+
Utils_1.HEX_REGEX.test(val) ? val : null,
|
|
543
|
+
amount: SchemaVerifier_1.FieldTypeEnum.BN,
|
|
544
|
+
token: (val) => val != null &&
|
|
545
|
+
typeof (val) === "string" &&
|
|
546
|
+
this.isTokenSupported(chainIdentifier, val) ? val : null,
|
|
547
|
+
descriptionHash: SchemaVerifier_1.FieldTypeEnum.StringOptional,
|
|
548
|
+
exactOut: SchemaVerifier_1.FieldTypeEnum.BooleanOptional
|
|
549
|
+
});
|
|
550
|
+
if (parsedBody == null)
|
|
551
|
+
throw {
|
|
552
|
+
code: 20100,
|
|
553
|
+
msg: "Invalid request body"
|
|
554
|
+
};
|
|
555
|
+
metadata.request = parsedBody;
|
|
556
|
+
const requestedAmount = { input: !parsedBody.exactOut, amount: parsedBody.amount };
|
|
557
|
+
const request = {
|
|
558
|
+
chainIdentifier,
|
|
559
|
+
raw: req,
|
|
560
|
+
parsed: parsedBody,
|
|
561
|
+
metadata
|
|
562
|
+
};
|
|
563
|
+
const useToken = parsedBody.token;
|
|
564
|
+
//Check request params
|
|
565
|
+
this.checkDescriptionHash(parsedBody.descriptionHash);
|
|
566
|
+
const fees = yield this.preCheckAmounts(request, requestedAmount, useToken);
|
|
567
|
+
metadata.times.requestChecked = Date.now();
|
|
568
|
+
//Create abortController for parallel prefetches
|
|
569
|
+
const responseStream = res.responseStream;
|
|
570
|
+
const abortController = this.getAbortController(responseStream);
|
|
571
|
+
//Pre-fetch data
|
|
572
|
+
const { pricePrefetchPromise, securityDepositPricePrefetchPromise } = this.getFromBtcPricePrefetches(chainIdentifier, useToken, abortController);
|
|
573
|
+
const balancePrefetch = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
|
|
574
|
+
const channelsPrefetch = this.getChannelsPrefetch(abortController);
|
|
575
|
+
const dummySwapData = yield this.getDummySwapData(chainIdentifier, useToken, parsedBody.address, parsedBody.paymentHash);
|
|
576
|
+
abortController.signal.throwIfAborted();
|
|
577
|
+
const baseSDPromise = this.getBaseSecurityDepositPrefetch(chainIdentifier, dummySwapData, abortController);
|
|
578
|
+
//Asynchronously send the node's public key to the client
|
|
579
|
+
this.sendPublicKeyAsync(responseStream);
|
|
580
|
+
//Check valid amount specified (min/max)
|
|
581
|
+
const { amountBD, swapFee, swapFeeInToken, totalInToken } = yield this.checkFromBtcAmount(request, requestedAmount, fees, useToken, abortController.signal, pricePrefetchPromise);
|
|
582
|
+
metadata.times.priceCalculated = Date.now();
|
|
583
|
+
//Check if we have enough funds to honor the request
|
|
584
|
+
yield this.checkBalance(totalInToken, balancePrefetch, abortController.signal);
|
|
585
|
+
yield this.checkInboundLiquidity(amountBD, channelsPrefetch, abortController.signal);
|
|
586
|
+
metadata.times.balanceChecked = Date.now();
|
|
587
|
+
//Create swap
|
|
588
|
+
const hodlInvoiceObj = {
|
|
589
|
+
description: chainIdentifier + "-" + parsedBody.address,
|
|
590
|
+
cltv_delta: this.config.minCltv.add(new BN(5)).toString(10),
|
|
591
|
+
expires_at: new Date(Date.now() + (this.config.invoiceTimeoutSeconds * 1000)).toISOString(),
|
|
592
|
+
id: parsedBody.paymentHash,
|
|
593
|
+
tokens: amountBD.toString(10),
|
|
594
|
+
description_hash: parsedBody.descriptionHash
|
|
595
|
+
};
|
|
596
|
+
metadata.invoiceRequest = Object.assign({}, hodlInvoiceObj);
|
|
597
|
+
hodlInvoiceObj.lnd = this.LND;
|
|
598
|
+
const hodlInvoice = yield lncli.createHodlInvoice(hodlInvoiceObj);
|
|
599
|
+
abortController.signal.throwIfAborted();
|
|
600
|
+
metadata.times.invoiceCreated = Date.now();
|
|
601
|
+
metadata.invoiceResponse = Object.assign({}, hodlInvoice);
|
|
602
|
+
const createdSwap = new FromBtcLnSwapAbs_1.FromBtcLnSwapAbs(chainIdentifier, hodlInvoice.request, swapFee, swapFeeInToken);
|
|
603
|
+
//Pre-compute the security deposit
|
|
604
|
+
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod);
|
|
605
|
+
const totalSecurityDeposit = yield this.getSecurityDeposit(chainIdentifier, amountBD, swapFee, expiryTimeout, baseSDPromise, securityDepositPricePrefetchPromise, abortController.signal, metadata);
|
|
606
|
+
metadata.times.securityDepositCalculated = Date.now();
|
|
607
|
+
//Create swap data
|
|
608
|
+
createdSwap.data = yield swapContract.createSwapData(base_1.ChainSwapType.HTLC, signer.getAddress(), parsedBody.address, useToken, totalInToken, parsedBody.paymentHash, new BN(0), null, null, 0, false, true, totalSecurityDeposit, new BN(0));
|
|
609
|
+
abortController.signal.throwIfAborted();
|
|
610
|
+
metadata.times.swapCreated = Date.now();
|
|
611
|
+
createdSwap.metadata = metadata;
|
|
612
|
+
//Save the desired fee rate for the signature
|
|
613
|
+
const feeRateObj = yield req.paramReader.getParams({
|
|
614
|
+
feeRate: SchemaVerifier_1.FieldTypeEnum.String
|
|
615
|
+
}).catch(e => null);
|
|
616
|
+
abortController.signal.throwIfAborted();
|
|
617
|
+
createdSwap.feeRate = (feeRateObj === null || feeRateObj === void 0 ? void 0 : feeRateObj.feeRate) != null && typeof (feeRateObj.feeRate) === "string" ? feeRateObj.feeRate : null;
|
|
618
|
+
yield PluginManager_1.PluginManager.swapCreate(createdSwap);
|
|
619
|
+
yield this.storageManager.saveData(parsedBody.paymentHash, null, createdSwap);
|
|
620
|
+
this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: " + hodlInvoice.request + " amount: " + amountBD.toString(10));
|
|
621
|
+
yield responseStream.writeParamsAndEnd({
|
|
622
|
+
code: 20000,
|
|
623
|
+
msg: "Success",
|
|
624
|
+
data: {
|
|
625
|
+
pr: hodlInvoice.request,
|
|
626
|
+
swapFee: swapFeeInToken.toString(10),
|
|
627
|
+
total: totalInToken.toString(10),
|
|
628
|
+
intermediaryKey: signer.getAddress(),
|
|
629
|
+
securityDeposit: totalSecurityDeposit.toString(10)
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
})));
|
|
633
|
+
const getInvoiceStatus = (0, Utils_1.expressHandlerWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
634
|
+
/**
|
|
635
|
+
* paymentHash: string payment hash of the invoice
|
|
636
|
+
*/
|
|
637
|
+
const parsedBody = (0, SchemaVerifier_1.verifySchema)(Object.assign(Object.assign({}, req.body), req.query), {
|
|
638
|
+
paymentHash: (val) => val != null &&
|
|
639
|
+
typeof (val) === "string" &&
|
|
640
|
+
val.length === 64 &&
|
|
641
|
+
Utils_1.HEX_REGEX.test(val) ? val : null,
|
|
642
|
+
});
|
|
643
|
+
yield this.checkInvoiceStatus(parsedBody.paymentHash);
|
|
644
|
+
res.status(200).json({
|
|
645
|
+
code: 10000,
|
|
646
|
+
msg: "Success"
|
|
647
|
+
});
|
|
648
|
+
}));
|
|
649
|
+
restServer.post(this.path + "/getInvoiceStatus", getInvoiceStatus);
|
|
650
|
+
restServer.get(this.path + "/getInvoiceStatus", getInvoiceStatus);
|
|
651
|
+
const getInvoicePaymentAuth = (0, Utils_1.expressHandlerWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
652
|
+
/**
|
|
653
|
+
* paymentHash: string payment hash of the invoice
|
|
654
|
+
*/
|
|
655
|
+
const parsedBody = (0, SchemaVerifier_1.verifySchema)(Object.assign(Object.assign({}, req.body), req.query), {
|
|
656
|
+
paymentHash: (val) => val != null &&
|
|
657
|
+
typeof (val) === "string" &&
|
|
658
|
+
val.length === 64 &&
|
|
659
|
+
Utils_1.HEX_REGEX.test(val) ? val : null,
|
|
660
|
+
});
|
|
661
|
+
const invoice = yield this.checkInvoiceStatus(parsedBody.paymentHash);
|
|
662
|
+
const swap = yield this.storageManager.getData(parsedBody.paymentHash, null);
|
|
663
|
+
if (swap == null)
|
|
664
|
+
throw {
|
|
665
|
+
_httpStatus: 200,
|
|
666
|
+
code: 10001,
|
|
667
|
+
msg: "Invoice expired/canceled"
|
|
668
|
+
};
|
|
669
|
+
const { swapContract, signer } = this.getChain(swap.chainIdentifier);
|
|
670
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED) {
|
|
671
|
+
if (yield swapContract.isInitAuthorizationExpired(swap.data, swap))
|
|
672
|
+
throw {
|
|
673
|
+
_httpStatus: 200,
|
|
674
|
+
code: 10001,
|
|
675
|
+
msg: "Invoice expired/canceled"
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED) {
|
|
679
|
+
try {
|
|
680
|
+
yield this.htlcReceived(swap, invoice);
|
|
681
|
+
}
|
|
682
|
+
catch (e) {
|
|
683
|
+
if ((0, Utils_1.isDefinedRuntimeError)(e))
|
|
684
|
+
e._httpStatus = 200;
|
|
685
|
+
throw e;
|
|
686
|
+
}
|
|
687
|
+
this.swapLogger.info(swap, "REST: /getInvoicePaymentAuth: swap processed through htlcReceived, invoice: " + swap.pr);
|
|
688
|
+
}
|
|
689
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED)
|
|
690
|
+
throw {
|
|
691
|
+
_httpStatus: 200,
|
|
692
|
+
code: 10001,
|
|
693
|
+
msg: "Invoice expired/canceled"
|
|
694
|
+
};
|
|
695
|
+
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.COMMITED)
|
|
696
|
+
throw {
|
|
697
|
+
_httpStatus: 200,
|
|
698
|
+
code: 10004,
|
|
699
|
+
msg: "Invoice already committed"
|
|
700
|
+
};
|
|
701
|
+
res.status(200).json({
|
|
702
|
+
code: 10000,
|
|
703
|
+
msg: "Success",
|
|
704
|
+
data: {
|
|
705
|
+
address: signer.getAddress(),
|
|
706
|
+
data: swap.serialize().data,
|
|
707
|
+
nonce: swap.nonce,
|
|
708
|
+
prefix: swap.prefix,
|
|
709
|
+
timeout: swap.timeout,
|
|
710
|
+
signature: swap.signature
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
}));
|
|
714
|
+
restServer.post(this.path + "/getInvoicePaymentAuth", getInvoicePaymentAuth);
|
|
715
|
+
restServer.get(this.path + "/getInvoicePaymentAuth", getInvoicePaymentAuth);
|
|
716
|
+
this.logger.info("started at path: ", this.path);
|
|
717
|
+
}
|
|
718
|
+
init() {
|
|
719
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
720
|
+
yield this.storageManager.loadData(FromBtcLnSwapAbs_1.FromBtcLnSwapAbs);
|
|
721
|
+
this.subscribeToEvents();
|
|
722
|
+
yield PluginManager_1.PluginManager.serviceInitialize(this);
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
getInfoData() {
|
|
726
|
+
return {
|
|
727
|
+
minCltv: this.config.minCltv.toNumber()
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
exports.FromBtcLnAbs = FromBtcLnAbs;
|