@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,9 +1,10 @@
|
|
|
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
|
export declare enum FromBtcTrustedSwapState {
|
|
5
|
-
DOUBLE_SPENT = -
|
|
6
|
-
REFUNDED = -
|
|
5
|
+
DOUBLE_SPENT = -4,
|
|
6
|
+
REFUNDED = -3,
|
|
7
|
+
REFUNDABLE = -2,
|
|
7
8
|
EXPIRED = -1,
|
|
8
9
|
CREATED = 0,
|
|
9
10
|
RECEIVED = 1,
|
|
@@ -13,19 +14,21 @@ export declare enum FromBtcTrustedSwapState {
|
|
|
13
14
|
FINISHED = 5
|
|
14
15
|
}
|
|
15
16
|
export declare class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBaseSwap<T, FromBtcTrustedSwapState> {
|
|
17
|
+
readonly sequence: BN;
|
|
16
18
|
readonly btcAddress: string;
|
|
17
|
-
readonly inputSats: BN;
|
|
18
19
|
readonly dstAddress: string;
|
|
19
20
|
readonly outputTokens: BN;
|
|
20
21
|
readonly createdHeight: number;
|
|
21
22
|
readonly expiresAt: number;
|
|
22
23
|
readonly recommendedFee: number;
|
|
23
|
-
|
|
24
|
+
refundAddress: string;
|
|
24
25
|
adjustedInput: BN;
|
|
25
26
|
adjustedOutput: BN;
|
|
26
27
|
doubleSpent: boolean;
|
|
27
28
|
scRawTx: string;
|
|
28
|
-
|
|
29
|
+
btcTx: BtcTx;
|
|
30
|
+
txFee: number;
|
|
31
|
+
txSize: number;
|
|
29
32
|
txId: string;
|
|
30
33
|
vout: number;
|
|
31
34
|
burnTxId: string;
|
|
@@ -7,8 +7,9 @@ const Utils_1 = require("../../utils/Utils");
|
|
|
7
7
|
const crypto_1 = require("crypto");
|
|
8
8
|
var FromBtcTrustedSwapState;
|
|
9
9
|
(function (FromBtcTrustedSwapState) {
|
|
10
|
-
FromBtcTrustedSwapState[FromBtcTrustedSwapState["DOUBLE_SPENT"] = -
|
|
11
|
-
FromBtcTrustedSwapState[FromBtcTrustedSwapState["REFUNDED"] = -
|
|
10
|
+
FromBtcTrustedSwapState[FromBtcTrustedSwapState["DOUBLE_SPENT"] = -4] = "DOUBLE_SPENT";
|
|
11
|
+
FromBtcTrustedSwapState[FromBtcTrustedSwapState["REFUNDED"] = -3] = "REFUNDED";
|
|
12
|
+
FromBtcTrustedSwapState[FromBtcTrustedSwapState["REFUNDABLE"] = -2] = "REFUNDABLE";
|
|
12
13
|
FromBtcTrustedSwapState[FromBtcTrustedSwapState["EXPIRED"] = -1] = "EXPIRED";
|
|
13
14
|
FromBtcTrustedSwapState[FromBtcTrustedSwapState["CREATED"] = 0] = "CREATED";
|
|
14
15
|
FromBtcTrustedSwapState[FromBtcTrustedSwapState["RECEIVED"] = 1] = "RECEIVED";
|
|
@@ -20,11 +21,11 @@ var FromBtcTrustedSwapState;
|
|
|
20
21
|
class FromBtcTrustedSwap extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
21
22
|
constructor(objOrChainIdentifier, swapFee, swapFeeInToken, btcAddress, inputSats, dstAddress, outputTokens, createdHeight, expiresAt, recommendedFee, refundAddress) {
|
|
22
23
|
if (typeof (objOrChainIdentifier) === "string") {
|
|
23
|
-
super(objOrChainIdentifier, swapFee, swapFeeInToken);
|
|
24
|
+
super(objOrChainIdentifier, inputSats, swapFee, swapFeeInToken);
|
|
24
25
|
this.state = FromBtcTrustedSwapState.CREATED;
|
|
25
26
|
this.doubleSpent = false;
|
|
27
|
+
this.sequence = new BN((0, crypto_1.randomBytes)(8));
|
|
26
28
|
this.btcAddress = btcAddress;
|
|
27
|
-
this.inputSats = inputSats;
|
|
28
29
|
this.dstAddress = dstAddress;
|
|
29
30
|
this.outputTokens = outputTokens;
|
|
30
31
|
this.createdHeight = createdHeight;
|
|
@@ -35,7 +36,7 @@ class FromBtcTrustedSwap extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
35
36
|
else {
|
|
36
37
|
super(objOrChainIdentifier);
|
|
37
38
|
this.btcAddress = objOrChainIdentifier.btcAddress;
|
|
38
|
-
this.
|
|
39
|
+
this.sequence = (0, Utils_1.deserializeBN)(objOrChainIdentifier.sequence);
|
|
39
40
|
this.dstAddress = objOrChainIdentifier.dstAddress;
|
|
40
41
|
this.outputTokens = (0, Utils_1.deserializeBN)(objOrChainIdentifier.outputTokens);
|
|
41
42
|
this.adjustedInput = (0, Utils_1.deserializeBN)(objOrChainIdentifier.adjustedInput);
|
|
@@ -46,7 +47,9 @@ class FromBtcTrustedSwap extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
46
47
|
this.refundAddress = objOrChainIdentifier.refundAddress;
|
|
47
48
|
this.doubleSpent = objOrChainIdentifier.doubleSpent;
|
|
48
49
|
this.scRawTx = objOrChainIdentifier.scRawTx;
|
|
49
|
-
this.
|
|
50
|
+
this.btcTx = objOrChainIdentifier.btcTx;
|
|
51
|
+
this.txFee = objOrChainIdentifier.txFee;
|
|
52
|
+
this.txSize = objOrChainIdentifier.txSize;
|
|
50
53
|
this.txId = objOrChainIdentifier.txId;
|
|
51
54
|
this.vout = objOrChainIdentifier.vout;
|
|
52
55
|
this.burnTxId = objOrChainIdentifier.burnTxId;
|
|
@@ -56,7 +59,7 @@ class FromBtcTrustedSwap extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
56
59
|
serialize() {
|
|
57
60
|
const partialSerialized = super.serialize();
|
|
58
61
|
partialSerialized.btcAddress = this.btcAddress;
|
|
59
|
-
partialSerialized.
|
|
62
|
+
partialSerialized.sequence = (0, Utils_1.serializeBN)(this.sequence);
|
|
60
63
|
partialSerialized.dstAddress = this.dstAddress;
|
|
61
64
|
partialSerialized.outputTokens = (0, Utils_1.serializeBN)(this.outputTokens);
|
|
62
65
|
partialSerialized.adjustedInput = (0, Utils_1.serializeBN)(this.adjustedInput);
|
|
@@ -67,7 +70,9 @@ class FromBtcTrustedSwap extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
67
70
|
partialSerialized.refundAddress = this.refundAddress;
|
|
68
71
|
partialSerialized.doubleSpent = this.doubleSpent;
|
|
69
72
|
partialSerialized.scRawTx = this.scRawTx;
|
|
70
|
-
partialSerialized.
|
|
73
|
+
partialSerialized.btcTx = this.btcTx;
|
|
74
|
+
partialSerialized.txFee = this.txFee;
|
|
75
|
+
partialSerialized.txSize = this.txSize;
|
|
71
76
|
partialSerialized.txId = this.txId;
|
|
72
77
|
partialSerialized.vout = this.vout;
|
|
73
78
|
partialSerialized.burnTxId = this.burnTxId;
|
|
@@ -78,13 +83,13 @@ class FromBtcTrustedSwap extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
78
83
|
return (0, crypto_1.createHash)("sha256").update(this.btcAddress).digest().toString("hex");
|
|
79
84
|
}
|
|
80
85
|
getSequence() {
|
|
81
|
-
return
|
|
86
|
+
return this.sequence;
|
|
82
87
|
}
|
|
83
88
|
getOutputAmount() {
|
|
84
89
|
return this.adjustedOutput || this.outputTokens;
|
|
85
90
|
}
|
|
86
91
|
getTotalInputAmount() {
|
|
87
|
-
return this.adjustedInput || this.
|
|
92
|
+
return this.adjustedInput || this.amount;
|
|
88
93
|
}
|
|
89
94
|
isFailed() {
|
|
90
95
|
return this.state === FromBtcTrustedSwapState.EXPIRED || this.state === FromBtcTrustedSwapState.REFUNDED || this.state === FromBtcTrustedSwapState.DOUBLE_SPENT;
|
|
@@ -4,10 +4,10 @@ import { FromBtcLnSwapAbs, FromBtcLnSwapState } from "./FromBtcLnSwapAbs";
|
|
|
4
4
|
import { MultichainData, SwapHandlerType } from "../SwapHandler";
|
|
5
5
|
import { ISwapPrice } from "../ISwapPrice";
|
|
6
6
|
import { ClaimEvent, InitializeEvent, RefundEvent, SwapData } from "@atomiqlabs/base";
|
|
7
|
-
import { AuthenticatedLnd } from "lightning";
|
|
8
7
|
import { IIntermediaryStorage } from "../../storage/IIntermediaryStorage";
|
|
9
8
|
import { FromBtcBaseConfig } from "../FromBtcBaseSwapHandler";
|
|
10
9
|
import { FromBtcLnBaseSwapHandler } from "../FromBtcLnBaseSwapHandler";
|
|
10
|
+
import { ILightningWallet } from "../../wallets/ILightningWallet";
|
|
11
11
|
export type FromBtcLnConfig = FromBtcBaseConfig & {
|
|
12
12
|
invoiceTimeoutSeconds?: number;
|
|
13
13
|
minCltv: BN;
|
|
@@ -27,7 +27,7 @@ export type FromBtcLnRequestType = {
|
|
|
27
27
|
export declare class FromBtcLnAbs extends FromBtcLnBaseSwapHandler<FromBtcLnSwapAbs, FromBtcLnSwapState> {
|
|
28
28
|
readonly type = SwapHandlerType.FROM_BTCLN;
|
|
29
29
|
readonly config: FromBtcLnConfig;
|
|
30
|
-
constructor(storageDirectory: IIntermediaryStorage<FromBtcLnSwapAbs>, path: string, chains: MultichainData,
|
|
30
|
+
constructor(storageDirectory: IIntermediaryStorage<FromBtcLnSwapAbs>, path: string, chains: MultichainData, lightning: ILightningWallet, swapPricing: ISwapPrice, config: FromBtcLnConfig);
|
|
31
31
|
protected processPastSwap(swap: FromBtcLnSwapAbs): Promise<"REFUND" | "SETTLE" | "CANCEL" | null>;
|
|
32
32
|
protected refundSwaps(refundSwaps: FromBtcLnSwapAbs[]): Promise<void>;
|
|
33
33
|
protected cancelInvoices(swaps: FromBtcLnSwapAbs[]): Promise<void>;
|
|
@@ -11,9 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.FromBtcLnAbs = void 0;
|
|
13
13
|
const BN = require("bn.js");
|
|
14
|
-
const lncli = require("ln-service");
|
|
15
14
|
const crypto_1 = require("crypto");
|
|
16
|
-
const bolt11 = require("@atomiqlabs/bolt11");
|
|
17
15
|
const FromBtcLnSwapAbs_1 = require("./FromBtcLnSwapAbs");
|
|
18
16
|
const SwapHandler_1 = require("../SwapHandler");
|
|
19
17
|
const base_1 = require("@atomiqlabs/base");
|
|
@@ -26,26 +24,23 @@ const FromBtcLnBaseSwapHandler_1 = require("../FromBtcLnBaseSwapHandler");
|
|
|
26
24
|
* Swap handler handling from BTCLN swaps using submarine swaps
|
|
27
25
|
*/
|
|
28
26
|
class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
29
|
-
constructor(storageDirectory, path, chains,
|
|
30
|
-
super(storageDirectory, path, chains,
|
|
27
|
+
constructor(storageDirectory, path, chains, lightning, swapPricing, config) {
|
|
28
|
+
super(storageDirectory, path, chains, lightning, swapPricing);
|
|
31
29
|
this.type = SwapHandler_1.SwapHandlerType.FROM_BTCLN;
|
|
32
30
|
this.config = config;
|
|
33
31
|
this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
|
|
34
32
|
}
|
|
35
33
|
processPastSwap(swap) {
|
|
36
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
const { swapContract } = this.getChain(swap.chainIdentifier);
|
|
35
|
+
const { swapContract, signer } = this.getChain(swap.chainIdentifier);
|
|
38
36
|
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED) {
|
|
39
37
|
//Check if already paid
|
|
40
|
-
const parsedPR =
|
|
41
|
-
const invoice = yield
|
|
42
|
-
|
|
43
|
-
lnd: this.LND
|
|
44
|
-
});
|
|
45
|
-
const isBeingPaid = invoice.is_held;
|
|
38
|
+
const parsedPR = yield this.lightning.parsePaymentRequest(swap.pr);
|
|
39
|
+
const invoice = yield this.lightning.getInvoice(parsedPR.id);
|
|
40
|
+
const isBeingPaid = invoice.status === "held";
|
|
46
41
|
if (!isBeingPaid) {
|
|
47
42
|
//Not paid
|
|
48
|
-
const isInvoiceExpired = parsedPR.
|
|
43
|
+
const isInvoiceExpired = parsedPR.expiryEpochMillis < Date.now();
|
|
49
44
|
if (!isInvoiceExpired)
|
|
50
45
|
return null;
|
|
51
46
|
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: " + swap.pr);
|
|
@@ -68,7 +63,6 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
68
63
|
return null;
|
|
69
64
|
}
|
|
70
65
|
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED) {
|
|
71
|
-
const parsedPR = bolt11.decode(swap.pr);
|
|
72
66
|
const isAuthorizationExpired = yield swapContract.isInitAuthorizationExpired(swap.data, swap);
|
|
73
67
|
if (isAuthorizationExpired) {
|
|
74
68
|
const isCommited = yield swapContract.isCommited(swap.data);
|
|
@@ -83,10 +77,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
83
77
|
}
|
|
84
78
|
}
|
|
85
79
|
if (swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.RECEIVED || swap.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.COMMITED) {
|
|
86
|
-
|
|
87
|
-
const currentTime = new BN(Math.floor(Date.now() / 1000) - this.config.maxSkew);
|
|
88
|
-
const isExpired = expiryTime != null && expiryTime.lt(currentTime);
|
|
89
|
-
if (!isExpired)
|
|
80
|
+
if (!swapContract.isExpired(signer.getAddress(), swap.data))
|
|
90
81
|
return null;
|
|
91
82
|
const isCommited = yield swapContract.isCommited(swap.data);
|
|
92
83
|
if (isCommited) {
|
|
@@ -123,10 +114,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
123
114
|
//Refund
|
|
124
115
|
const paymentHash = swap.data.getHash();
|
|
125
116
|
try {
|
|
126
|
-
yield
|
|
127
|
-
lnd: this.LND,
|
|
128
|
-
id: paymentHash
|
|
129
|
-
});
|
|
117
|
+
yield this.lightning.cancelHodlInvoice(paymentHash);
|
|
130
118
|
this.swapLogger.info(swap, "cancelInvoices(): invoice cancelled!");
|
|
131
119
|
yield this.removeSwapData(swap);
|
|
132
120
|
}
|
|
@@ -140,10 +128,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
140
128
|
return __awaiter(this, void 0, void 0, function* () {
|
|
141
129
|
for (let swap of swaps) {
|
|
142
130
|
try {
|
|
143
|
-
yield
|
|
144
|
-
lnd: this.LND,
|
|
145
|
-
secret: swap.secret
|
|
146
|
-
});
|
|
131
|
+
yield this.lightning.settleHodlInvoice(swap.secret);
|
|
147
132
|
if (swap.metadata != null)
|
|
148
133
|
swap.metadata.times.htlcSettled = Date.now();
|
|
149
134
|
yield this.removeSwapData(swap, FromBtcLnSwapAbs_1.FromBtcLnSwapState.SETTLED);
|
|
@@ -237,10 +222,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
237
222
|
savedSwap.metadata.times.claimTxReceived = Date.now();
|
|
238
223
|
this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap HTLC successfully claimed by the client, invoice: " + savedSwap.pr);
|
|
239
224
|
try {
|
|
240
|
-
yield
|
|
241
|
-
lnd: this.LND,
|
|
242
|
-
secret: secretHex
|
|
243
|
-
});
|
|
225
|
+
yield this.lightning.settleHodlInvoice(secretHex);
|
|
244
226
|
this.swapLogger.info(savedSwap, "SC: ClaimEvent: invoice settled, secret: " + secretHex);
|
|
245
227
|
savedSwap.secret = secretHex;
|
|
246
228
|
if (savedSwap.metadata != null)
|
|
@@ -268,10 +250,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
268
250
|
savedSwap.txIds.refund = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
269
251
|
this.swapLogger.info(savedSwap, "SC: RefundEvent: swap refunded to us, invoice: " + savedSwap.pr);
|
|
270
252
|
try {
|
|
271
|
-
yield
|
|
272
|
-
lnd: this.LND,
|
|
273
|
-
id: event.paymentHash
|
|
274
|
-
});
|
|
253
|
+
yield this.lightning.cancelHodlInvoice(event.paymentHash);
|
|
275
254
|
this.swapLogger.info(savedSwap, "SC: RefundEvent: invoice cancelled");
|
|
276
255
|
yield this.removeSwapData(savedSwap, FromBtcLnSwapAbs_1.FromBtcLnSwapState.REFUNDED);
|
|
277
256
|
}
|
|
@@ -367,7 +346,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
367
346
|
* @param abortController
|
|
368
347
|
*/
|
|
369
348
|
getBlockheightPrefetch(abortController) {
|
|
370
|
-
return
|
|
349
|
+
return this.lightning.getBlockheight().catch(e => {
|
|
371
350
|
this.logger.error("getBlockheightPrefetch(): error", e);
|
|
372
351
|
abortController.abort(e);
|
|
373
352
|
return null;
|
|
@@ -379,8 +358,8 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
379
358
|
* @param responseStream
|
|
380
359
|
*/
|
|
381
360
|
sendPublicKeyAsync(responseStream) {
|
|
382
|
-
|
|
383
|
-
lnPublicKey:
|
|
361
|
+
this.lightning.getIdentityPublicKey().then(publicKey => responseStream.writeParams({
|
|
362
|
+
lnPublicKey: publicKey
|
|
384
363
|
})).catch(e => {
|
|
385
364
|
this.logger.error("sendPublicKeyAsync(): error", e);
|
|
386
365
|
});
|
|
@@ -439,10 +418,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
439
418
|
return;
|
|
440
419
|
yield invoiceData.setState(FromBtcLnSwapAbs_1.FromBtcLnSwapState.CANCELED);
|
|
441
420
|
const paymentHash = invoiceData.data.getHash();
|
|
442
|
-
yield
|
|
443
|
-
id: paymentHash,
|
|
444
|
-
lnd: this.LND
|
|
445
|
-
});
|
|
421
|
+
yield this.lightning.cancelHodlInvoice(paymentHash);
|
|
446
422
|
yield this.removeSwapData(invoiceData);
|
|
447
423
|
this.swapLogger.info(invoiceData, "cancelSwapAndInvoice(): swap removed & invoice cancelled, invoice: ", invoiceData.pr);
|
|
448
424
|
});
|
|
@@ -462,10 +438,7 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
462
438
|
*/
|
|
463
439
|
checkInvoiceStatus(paymentHash) {
|
|
464
440
|
return __awaiter(this, void 0, void 0, function* () {
|
|
465
|
-
const invoice = yield
|
|
466
|
-
id: paymentHash,
|
|
467
|
-
lnd: this.LND
|
|
468
|
-
});
|
|
441
|
+
const invoice = yield this.lightning.getInvoice(paymentHash);
|
|
469
442
|
if (invoice == null)
|
|
470
443
|
throw {
|
|
471
444
|
_httpStatus: 200,
|
|
@@ -490,27 +463,28 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
490
463
|
code: 10001,
|
|
491
464
|
msg: "Invoice expired/canceled"
|
|
492
465
|
};
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
if (invoice.is_canceled)
|
|
466
|
+
switch (invoice.status) {
|
|
467
|
+
case "canceled":
|
|
496
468
|
throw {
|
|
497
469
|
_httpStatus: 200,
|
|
498
470
|
code: 10001,
|
|
499
471
|
msg: "Invoice expired/canceled"
|
|
500
472
|
};
|
|
501
|
-
|
|
473
|
+
case "confirmed":
|
|
502
474
|
throw {
|
|
503
475
|
_httpStatus: 200,
|
|
504
476
|
code: 10002,
|
|
505
477
|
msg: "Invoice already paid"
|
|
506
478
|
};
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
479
|
+
case "unpaid":
|
|
480
|
+
throw {
|
|
481
|
+
_httpStatus: 200,
|
|
482
|
+
code: 10003,
|
|
483
|
+
msg: "Invoice yet unpaid"
|
|
484
|
+
};
|
|
485
|
+
default:
|
|
486
|
+
return invoice;
|
|
512
487
|
}
|
|
513
|
-
return invoice;
|
|
514
488
|
});
|
|
515
489
|
}
|
|
516
490
|
startRestServer(restServer) {
|
|
@@ -587,19 +561,18 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
587
561
|
//Create swap
|
|
588
562
|
const hodlInvoiceObj = {
|
|
589
563
|
description: chainIdentifier + "-" + parsedBody.address,
|
|
590
|
-
|
|
591
|
-
|
|
564
|
+
cltvDelta: this.config.minCltv.add(new BN(5)).toNumber(),
|
|
565
|
+
expiresAt: Date.now() + (this.config.invoiceTimeoutSeconds * 1000),
|
|
592
566
|
id: parsedBody.paymentHash,
|
|
593
|
-
|
|
594
|
-
|
|
567
|
+
mtokens: amountBD.mul(new BN(1000)),
|
|
568
|
+
descriptionHash: parsedBody.descriptionHash
|
|
595
569
|
};
|
|
596
|
-
metadata.invoiceRequest =
|
|
597
|
-
|
|
598
|
-
const hodlInvoice = yield lncli.createHodlInvoice(hodlInvoiceObj);
|
|
570
|
+
metadata.invoiceRequest = hodlInvoiceObj;
|
|
571
|
+
const hodlInvoice = yield this.lightning.createHodlInvoice(hodlInvoiceObj);
|
|
599
572
|
abortController.signal.throwIfAborted();
|
|
600
573
|
metadata.times.invoiceCreated = Date.now();
|
|
601
574
|
metadata.invoiceResponse = Object.assign({}, hodlInvoice);
|
|
602
|
-
const createdSwap = new FromBtcLnSwapAbs_1.FromBtcLnSwapAbs(chainIdentifier, hodlInvoice.request, swapFee, swapFeeInToken);
|
|
575
|
+
const createdSwap = new FromBtcLnSwapAbs_1.FromBtcLnSwapAbs(chainIdentifier, hodlInvoice.request, hodlInvoice.mtokens, swapFee, swapFeeInToken);
|
|
603
576
|
//Pre-compute the security deposit
|
|
604
577
|
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod);
|
|
605
578
|
const totalSecurityDeposit = yield this.getSecurityDeposit(chainIdentifier, amountBD, swapFee, expiryTimeout, baseSDPromise, securityDepositPricePrefetchPromise, abortController.signal, metadata);
|
|
@@ -718,6 +691,13 @@ class FromBtcLnAbs extends FromBtcLnBaseSwapHandler_1.FromBtcLnBaseSwapHandler {
|
|
|
718
691
|
init() {
|
|
719
692
|
return __awaiter(this, void 0, void 0, function* () {
|
|
720
693
|
yield this.storageManager.loadData(FromBtcLnSwapAbs_1.FromBtcLnSwapAbs);
|
|
694
|
+
//Check if all swaps contain a valid amount
|
|
695
|
+
for (let swap of yield this.storageManager.query([])) {
|
|
696
|
+
if (swap.amount == null) {
|
|
697
|
+
const parsedPR = yield this.lightning.parsePaymentRequest(swap.pr);
|
|
698
|
+
swap.amount = parsedPR.mtokens.add(new BN(999)).div(new BN(1000));
|
|
699
|
+
}
|
|
700
|
+
}
|
|
721
701
|
this.subscribeToEvents();
|
|
722
702
|
yield PluginManager_1.PluginManager.serviceInitialize(this);
|
|
723
703
|
});
|
|
@@ -13,17 +13,12 @@ export declare enum FromBtcLnSwapState {
|
|
|
13
13
|
export declare class FromBtcLnSwapAbs<T extends SwapData = SwapData> extends FromBtcBaseSwap<T, FromBtcLnSwapState> {
|
|
14
14
|
readonly pr: string;
|
|
15
15
|
nonce: number;
|
|
16
|
-
prefix: string;
|
|
17
|
-
timeout: string;
|
|
18
|
-
signature: string;
|
|
19
|
-
feeRate: string;
|
|
20
16
|
secret: string;
|
|
21
|
-
constructor(chainIdentifier: string, pr: string, swapFee: BN, swapFeeInToken: BN);
|
|
17
|
+
constructor(chainIdentifier: string, pr: string, amountMtokens: BN, swapFee: BN, swapFeeInToken: BN);
|
|
22
18
|
constructor(obj: any);
|
|
23
19
|
serialize(): any;
|
|
24
20
|
getSequence(): BN;
|
|
25
21
|
isInitiated(): boolean;
|
|
26
22
|
isFailed(): boolean;
|
|
27
23
|
isSuccess(): boolean;
|
|
28
|
-
getTotalInputAmount(): BN;
|
|
29
24
|
}
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FromBtcLnSwapAbs = exports.FromBtcLnSwapState = void 0;
|
|
4
4
|
const BN = require("bn.js");
|
|
5
5
|
const __1 = require("../..");
|
|
6
|
-
const bolt11 = require("@atomiqlabs/bolt11");
|
|
7
6
|
const FromBtcBaseSwap_1 = require("../FromBtcBaseSwap");
|
|
8
7
|
var FromBtcLnSwapState;
|
|
9
8
|
(function (FromBtcLnSwapState) {
|
|
@@ -16,9 +15,9 @@ var FromBtcLnSwapState;
|
|
|
16
15
|
FromBtcLnSwapState[FromBtcLnSwapState["SETTLED"] = 4] = "SETTLED";
|
|
17
16
|
})(FromBtcLnSwapState = exports.FromBtcLnSwapState || (exports.FromBtcLnSwapState = {}));
|
|
18
17
|
class FromBtcLnSwapAbs extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
19
|
-
constructor(chainIdOrObj, pr, swapFee, swapFeeInToken) {
|
|
18
|
+
constructor(chainIdOrObj, pr, amountMtokens, swapFee, swapFeeInToken) {
|
|
20
19
|
if (typeof (chainIdOrObj) === "string") {
|
|
21
|
-
super(chainIdOrObj, swapFee, swapFeeInToken);
|
|
20
|
+
super(chainIdOrObj, amountMtokens.add(new BN(999)).div(new BN(1000)), swapFee, swapFeeInToken);
|
|
22
21
|
this.state = FromBtcLnSwapState.CREATED;
|
|
23
22
|
this.pr = pr;
|
|
24
23
|
}
|
|
@@ -27,10 +26,6 @@ class FromBtcLnSwapAbs extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
27
26
|
this.pr = chainIdOrObj.pr;
|
|
28
27
|
this.secret = chainIdOrObj.secret;
|
|
29
28
|
this.nonce = chainIdOrObj.nonce;
|
|
30
|
-
this.prefix = chainIdOrObj.prefix;
|
|
31
|
-
this.timeout = chainIdOrObj.timeout;
|
|
32
|
-
this.signature = chainIdOrObj.signature;
|
|
33
|
-
this.feeRate = chainIdOrObj.feeRate;
|
|
34
29
|
}
|
|
35
30
|
this.type = __1.SwapHandlerType.FROM_BTCLN;
|
|
36
31
|
}
|
|
@@ -39,10 +34,6 @@ class FromBtcLnSwapAbs extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
39
34
|
partialSerialized.pr = this.pr;
|
|
40
35
|
partialSerialized.secret = this.secret;
|
|
41
36
|
partialSerialized.nonce = this.nonce;
|
|
42
|
-
partialSerialized.prefix = this.prefix;
|
|
43
|
-
partialSerialized.timeout = this.timeout;
|
|
44
|
-
partialSerialized.signature = this.signature;
|
|
45
|
-
partialSerialized.feeRate = this.feeRate;
|
|
46
37
|
return partialSerialized;
|
|
47
38
|
}
|
|
48
39
|
getSequence() {
|
|
@@ -57,8 +48,5 @@ class FromBtcLnSwapAbs extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
57
48
|
isSuccess() {
|
|
58
49
|
return this.state === FromBtcLnSwapState.SETTLED;
|
|
59
50
|
}
|
|
60
|
-
getTotalInputAmount() {
|
|
61
|
-
return new BN(bolt11.decode(this.pr).millisatoshis).add(new BN(999)).div(new BN(1000));
|
|
62
|
-
}
|
|
63
51
|
}
|
|
64
52
|
exports.FromBtcLnSwapAbs = FromBtcLnSwapAbs;
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import * as BN from "bn.js";
|
|
3
2
|
import { Express } from "express";
|
|
4
3
|
import { ClaimEvent, InitializeEvent, RefundEvent, SwapData } from "@atomiqlabs/base";
|
|
5
|
-
import { AuthenticatedLnd } from "lightning";
|
|
6
4
|
import { FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState } from "./FromBtcLnTrustedSwap";
|
|
7
5
|
import { FromBtcBaseConfig } from "../FromBtcBaseSwapHandler";
|
|
8
6
|
import { ISwapPrice } from "../ISwapPrice";
|
|
9
7
|
import { MultichainData, SwapHandlerType } from "../SwapHandler";
|
|
10
8
|
import { IIntermediaryStorage } from "../../storage/IIntermediaryStorage";
|
|
11
9
|
import { FromBtcLnBaseSwapHandler } from "../FromBtcLnBaseSwapHandler";
|
|
12
|
-
import
|
|
10
|
+
import { ILightningWallet } from "../../wallets/ILightningWallet";
|
|
13
11
|
export type SwapForGasServerConfig = FromBtcBaseConfig & {
|
|
14
12
|
minCltv: BN;
|
|
15
13
|
invoiceTimeoutSeconds?: number;
|
|
@@ -24,10 +22,10 @@ export type FromBtcLnTrustedRequestType = {
|
|
|
24
22
|
*/
|
|
25
23
|
export declare class FromBtcLnTrusted extends FromBtcLnBaseSwapHandler<FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState> {
|
|
26
24
|
readonly type: SwapHandlerType;
|
|
27
|
-
activeSubscriptions: Map<string,
|
|
25
|
+
activeSubscriptions: Map<string, AbortController>;
|
|
28
26
|
processedTxIds: Map<string, string>;
|
|
29
27
|
readonly config: SwapForGasServerConfig;
|
|
30
|
-
constructor(storageDirectory: IIntermediaryStorage<FromBtcLnTrustedSwap>, path: string, chains: MultichainData,
|
|
28
|
+
constructor(storageDirectory: IIntermediaryStorage<FromBtcLnTrustedSwap>, path: string, chains: MultichainData, lightning: ILightningWallet, swapPricing: ISwapPrice, config: SwapForGasServerConfig);
|
|
31
29
|
/**
|
|
32
30
|
* Unsubscribe from the pending lightning network invoice
|
|
33
31
|
*
|