@atomiqlabs/lp-lib 13.0.1 → 14.0.0-dev.4
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/storagemanager/IntermediaryStorageManager.d.ts +1 -0
- package/dist/storagemanager/IntermediaryStorageManager.js +9 -2
- package/dist/storagemanager/StorageManager.d.ts +1 -0
- package/dist/storagemanager/StorageManager.js +9 -2
- package/dist/swaps/SwapHandler.d.ts +2 -6
- package/dist/swaps/SwapHandler.js +3 -7
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +6 -2
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +6 -2
- package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +1 -1
- package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +1 -6
- package/dist/swaps/spv_vault_swap/SpvVaults.js +2 -7
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +1 -1
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +3 -4
- package/dist/utils/Utils.d.ts +7 -7
- package/dist/utils/Utils.js +12 -11
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +8 -6
- package/package.json +2 -2
- package/src/storagemanager/IntermediaryStorageManager.ts +11 -2
- package/src/storagemanager/StorageManager.ts +12 -2
- package/src/swaps/SwapHandler.ts +3 -7
- package/src/swaps/escrow/EscrowHandler.ts +3 -3
- package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +7 -4
- package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +7 -4
- package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +1 -1
- package/src/swaps/spv_vault_swap/SpvVaults.ts +3 -8
- package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +1 -1
- package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +3 -5
- package/src/utils/Utils.ts +19 -17
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +9 -6
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.IntermediaryStorageManager = void 0;
|
|
4
4
|
const fs = require("fs/promises");
|
|
5
|
+
const fsSync = require("fs");
|
|
6
|
+
const Utils_1 = require("../utils/Utils");
|
|
5
7
|
class IntermediaryStorageManager {
|
|
6
8
|
constructor(directory) {
|
|
7
9
|
this.data = {};
|
|
8
10
|
this.directory = directory;
|
|
11
|
+
this.logger = (0, Utils_1.getLogger)("IntermediaryStorageManager(" + directory + "): ");
|
|
9
12
|
}
|
|
10
13
|
async init() {
|
|
11
14
|
try {
|
|
@@ -79,17 +82,21 @@ class IntermediaryStorageManager {
|
|
|
79
82
|
await fs.rm(this.directory + "/" + identifier + ".json");
|
|
80
83
|
}
|
|
81
84
|
catch (e) {
|
|
82
|
-
|
|
85
|
+
this.logger.error("removeData(): Error when removing data: ", e);
|
|
83
86
|
}
|
|
84
87
|
}
|
|
85
88
|
async loadData(type) {
|
|
86
89
|
this.type = type;
|
|
90
|
+
if (!fsSync.existsSync(this.directory)) {
|
|
91
|
+
this.logger.debug("loadData(): Data directory not found!");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
87
94
|
let files;
|
|
88
95
|
try {
|
|
89
96
|
files = await fs.readdir(this.directory);
|
|
90
97
|
}
|
|
91
98
|
catch (e) {
|
|
92
|
-
|
|
99
|
+
this.logger.error("loadData(): Error when checking directory: ", e);
|
|
93
100
|
return;
|
|
94
101
|
}
|
|
95
102
|
for (let file of files) {
|
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StorageManager = void 0;
|
|
4
4
|
const fs = require("fs/promises");
|
|
5
|
+
const Utils_1 = require("../utils/Utils");
|
|
6
|
+
const fs_1 = require("fs");
|
|
5
7
|
class StorageManager {
|
|
6
8
|
constructor(directory) {
|
|
7
9
|
this.data = {};
|
|
8
10
|
this.directory = directory;
|
|
11
|
+
this.logger = (0, Utils_1.getLogger)("StorageManager(" + directory + "): ");
|
|
9
12
|
}
|
|
10
13
|
async init() {
|
|
11
14
|
try {
|
|
@@ -30,16 +33,20 @@ class StorageManager {
|
|
|
30
33
|
await fs.rm(this.directory + "/" + paymentHash + ".json");
|
|
31
34
|
}
|
|
32
35
|
catch (e) {
|
|
33
|
-
|
|
36
|
+
this.logger.error("removeData(): Error when removing data: ", e);
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
async loadData(type) {
|
|
40
|
+
if (!fs_1.default.existsSync(this.directory)) {
|
|
41
|
+
this.logger.debug("loadData(): Data directory not found!");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
37
44
|
let files;
|
|
38
45
|
try {
|
|
39
46
|
files = await fs.readdir(this.directory);
|
|
40
47
|
}
|
|
41
48
|
catch (e) {
|
|
42
|
-
|
|
49
|
+
this.logger.error("loadData(): Error when checking directory: ", e);
|
|
43
50
|
return [];
|
|
44
51
|
}
|
|
45
52
|
const arr = [];
|
|
@@ -4,6 +4,7 @@ import { ChainType } from "@atomiqlabs/base";
|
|
|
4
4
|
import { SwapHandlerSwap } from "./SwapHandlerSwap";
|
|
5
5
|
import { IIntermediaryStorage } from "../storage/IIntermediaryStorage";
|
|
6
6
|
import { IParamReader } from "../utils/paramcoders/IParamReader";
|
|
7
|
+
import { LoggerType } from "../utils/Utils";
|
|
7
8
|
export declare enum SwapHandlerType {
|
|
8
9
|
TO_BTC = "TO_BTC",
|
|
9
10
|
FROM_BTC = "FROM_BTC",
|
|
@@ -77,12 +78,7 @@ export declare abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHan
|
|
|
77
78
|
};
|
|
78
79
|
readonly swapPricing: ISwapPrice;
|
|
79
80
|
abstract config: SwapBaseConfig;
|
|
80
|
-
logger:
|
|
81
|
-
debug: (msg: string, ...args: any) => void;
|
|
82
|
-
info: (msg: string, ...args: any) => void;
|
|
83
|
-
warn: (msg: string, ...args: any) => void;
|
|
84
|
-
error: (msg: string, ...args: any) => void;
|
|
85
|
-
};
|
|
81
|
+
logger: LoggerType;
|
|
86
82
|
protected swapLogger: {
|
|
87
83
|
debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
|
|
88
84
|
info: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SwapHandler = exports.SwapHandlerType = void 0;
|
|
4
4
|
const PluginManager_1 = require("../plugins/PluginManager");
|
|
5
|
+
const Utils_1 = require("../utils/Utils");
|
|
5
6
|
var SwapHandlerType;
|
|
6
7
|
(function (SwapHandlerType) {
|
|
7
8
|
SwapHandlerType["TO_BTC"] = "TO_BTC";
|
|
@@ -17,12 +18,7 @@ var SwapHandlerType;
|
|
|
17
18
|
*/
|
|
18
19
|
class SwapHandler {
|
|
19
20
|
constructor(storageDirectory, path, chainsData, swapPricing) {
|
|
20
|
-
this.logger =
|
|
21
|
-
debug: (msg, ...args) => console.debug("SwapHandler(" + this.type + "): " + msg, ...args),
|
|
22
|
-
info: (msg, ...args) => console.info("SwapHandler(" + this.type + "): " + msg, ...args),
|
|
23
|
-
warn: (msg, ...args) => console.warn("SwapHandler(" + this.type + "): " + msg, ...args),
|
|
24
|
-
error: (msg, ...args) => console.error("SwapHandler(" + this.type + "): " + msg, ...args)
|
|
25
|
-
};
|
|
21
|
+
this.logger = (0, Utils_1.getLogger)(() => "SwapHandler(" + this.type + "): ");
|
|
26
22
|
this.swapLogger = {
|
|
27
23
|
debug: (swap, msg, ...args) => this.logger.debug(swap.getIdentifier() + ": " + msg, ...args),
|
|
28
24
|
info: (swap, msg, ...args) => this.logger.info(swap.getIdentifier() + ": " + msg, ...args),
|
|
@@ -57,7 +53,7 @@ class SwapHandler {
|
|
|
57
53
|
async startWatchdog() {
|
|
58
54
|
let rerun;
|
|
59
55
|
rerun = async () => {
|
|
60
|
-
await this.processPastSwaps().catch(e =>
|
|
56
|
+
await this.processPastSwaps().catch(e => this.logger.error("startWatchdog(): Error when processing past swaps: ", e));
|
|
61
57
|
setTimeout(rerun, this.config.swapCheckInterval);
|
|
62
58
|
};
|
|
63
59
|
await rerun();
|
|
@@ -100,14 +100,18 @@ class ToBtcAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
|
|
|
100
100
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
101
101
|
if (!isCommited) {
|
|
102
102
|
const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
|
|
103
|
-
if (status === base_1.
|
|
103
|
+
if (status.type === base_1.SwapCommitStateType.PAID) {
|
|
104
104
|
this.swapLogger.info(swap, "processPastSwap(state=BTC_SENT): swap claimed (detected from processPastSwap), address: " + swap.address);
|
|
105
105
|
this.unsubscribePayment(swap);
|
|
106
|
+
swap.txIds ?? (swap.txIds = {});
|
|
107
|
+
swap.txIds.claim = await status.getClaimTxId();
|
|
106
108
|
await this.removeSwapData(swap, ToBtcSwapAbs_1.ToBtcSwapState.CLAIMED);
|
|
107
109
|
}
|
|
108
|
-
else if (status === base_1.
|
|
110
|
+
else if (status.type === base_1.SwapCommitStateType.EXPIRED) {
|
|
109
111
|
this.swapLogger.warn(swap, "processPastSwap(state=BTC_SENT): swap expired, but bitcoin was probably already sent, txId: " + swap.txId + " address: " + swap.address);
|
|
110
112
|
this.unsubscribePayment(swap);
|
|
113
|
+
swap.txIds ?? (swap.txIds = {});
|
|
114
|
+
swap.txIds.refund = status.getRefundTxId == null ? null : await status.getRefundTxId();
|
|
111
115
|
await this.removeSwapData(swap, ToBtcSwapAbs_1.ToBtcSwapState.REFUNDED);
|
|
112
116
|
}
|
|
113
117
|
return;
|
|
@@ -120,13 +120,17 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
|
|
|
120
120
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
121
121
|
if (!isCommited) {
|
|
122
122
|
const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
|
|
123
|
-
if (status === base_1.
|
|
123
|
+
if (status?.type === base_1.SwapCommitStateType.PAID) {
|
|
124
124
|
//This is alright, we got the money
|
|
125
|
+
swap.txIds ?? (swap.txIds = {});
|
|
126
|
+
swap.txIds.claim = await status.getClaimTxId();
|
|
125
127
|
await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CLAIMED);
|
|
126
128
|
return true;
|
|
127
129
|
}
|
|
128
|
-
else if (status === base_1.
|
|
130
|
+
else if (status?.type === base_1.SwapCommitStateType.EXPIRED) {
|
|
129
131
|
//This means the user was able to refund before we were able to claim, no good
|
|
132
|
+
swap.txIds ?? (swap.txIds = {});
|
|
133
|
+
swap.txIds.refund = status.getRefundTxId == null ? null : await status.getRefundTxId();
|
|
130
134
|
await this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.REFUNDED);
|
|
131
135
|
}
|
|
132
136
|
this.swapLogger.warn(swap, "processPaymentResult(): tried to claim but escrow doesn't exist anymore," +
|
|
@@ -366,7 +366,7 @@ class SpvVaultSwapHandler extends SwapHandler_1.SwapHandler {
|
|
|
366
366
|
msg: "PSBT transaction cannot be parsed!"
|
|
367
367
|
};
|
|
368
368
|
}
|
|
369
|
-
if (data.
|
|
369
|
+
if (!data.isRecipient(swap.recipient) ||
|
|
370
370
|
data.callerFeeRate !== swap.callerFeeShare ||
|
|
371
371
|
data.frontingFeeRate !== swap.frontingFeeShare ||
|
|
372
372
|
data.executionFeeRate !== swap.executionFeeShare ||
|
|
@@ -15,12 +15,7 @@ export declare class SpvVaults {
|
|
|
15
15
|
maxUnclaimedWithdrawals?: number;
|
|
16
16
|
};
|
|
17
17
|
readonly getChain: (chainId: string) => ChainData;
|
|
18
|
-
readonly logger:
|
|
19
|
-
debug: (msg: string, ...args: any) => void;
|
|
20
|
-
info: (msg: string, ...args: any) => void;
|
|
21
|
-
warn: (msg: string, ...args: any) => void;
|
|
22
|
-
error: (msg: string, ...args: any) => void;
|
|
23
|
-
};
|
|
18
|
+
readonly logger: import("../../utils/Utils").LoggerType;
|
|
24
19
|
constructor(vaultStorage: IStorageManager<SpvVault>, bitcoin: IBitcoinWallet, vaultSigner: ISpvVaultSigner, bitcoinRpc: BitcoinRpc<any>, getChain: (chainId: string) => ChainData, config: {
|
|
25
20
|
vaultsCheckInterval: number;
|
|
26
21
|
maxUnclaimedWithdrawals?: number;
|
|
@@ -11,12 +11,7 @@ const VAULT_INIT_CONFIRMATIONS = 2;
|
|
|
11
11
|
const BTC_FINALIZATION_CONFIRMATIONS = 6;
|
|
12
12
|
class SpvVaults {
|
|
13
13
|
constructor(vaultStorage, bitcoin, vaultSigner, bitcoinRpc, getChain, config) {
|
|
14
|
-
this.logger =
|
|
15
|
-
debug: (msg, ...args) => console.debug("SpvVaults: " + msg, ...args),
|
|
16
|
-
info: (msg, ...args) => console.info("SpvVaults: " + msg, ...args),
|
|
17
|
-
warn: (msg, ...args) => console.warn("SpvVaults: " + msg, ...args),
|
|
18
|
-
error: (msg, ...args) => console.error("SpvVaults: " + msg, ...args)
|
|
19
|
-
};
|
|
14
|
+
this.logger = (0, Utils_1.getLogger)("SpvVaults: ");
|
|
20
15
|
this.vaultStorage = vaultStorage;
|
|
21
16
|
this.bitcoin = bitcoin;
|
|
22
17
|
this.vaultSigner = vaultSigner;
|
|
@@ -357,7 +352,7 @@ class SpvVaults {
|
|
|
357
352
|
async startVaultsWatchdog() {
|
|
358
353
|
let rerun;
|
|
359
354
|
rerun = async () => {
|
|
360
|
-
await this.checkVaults().catch(e =>
|
|
355
|
+
await this.checkVaults().catch(e => this.logger.error("startVaultsWatchdog(): Error when periodically checking SPV vaults: ", e));
|
|
361
356
|
setTimeout(rerun, this.config.vaultsCheckInterval);
|
|
362
357
|
};
|
|
363
358
|
await rerun();
|
|
@@ -618,7 +618,7 @@ class FromBtcTrusted extends SwapHandler_1.SwapHandler {
|
|
|
618
618
|
async startDoubleSpendWatchdog() {
|
|
619
619
|
let rerun;
|
|
620
620
|
rerun = async () => {
|
|
621
|
-
await this.checkDoubleSpends().catch(e =>
|
|
621
|
+
await this.checkDoubleSpends().catch(e => this.logger.error("startDoubleSpendWatchdog(): Error when checking double spends: ", e));
|
|
622
622
|
setTimeout(rerun, this.config.doubleSpendCheckInterval);
|
|
623
623
|
};
|
|
624
624
|
await rerun();
|
|
@@ -53,7 +53,7 @@ class FromBtcLnTrusted extends SwapHandler_1.SwapHandler {
|
|
|
53
53
|
this.swapLogger.debug(invoiceData, "subscribeToInvoice(): invoice_updated: ", invoice);
|
|
54
54
|
if (invoice.status !== "held")
|
|
55
55
|
return;
|
|
56
|
-
this.htlcReceived(invoiceData, invoice).catch(e =>
|
|
56
|
+
this.htlcReceived(invoiceData, invoice).catch(e => this.swapLogger.error(invoiceData, "subscribeToInvoice(): Error calling htlcReceived(): ", e));
|
|
57
57
|
this.activeSubscriptions.delete(hash);
|
|
58
58
|
});
|
|
59
59
|
this.swapLogger.debug(invoiceData, "subscribeToInvoice(): Subscribed to invoice payment");
|
|
@@ -79,7 +79,7 @@ class FromBtcLnTrusted extends SwapHandler_1.SwapHandler {
|
|
|
79
79
|
//Result is either FromBtcLnTrustedSwapState.RECEIVED or FromBtcLnTrustedSwapState.CANCELED
|
|
80
80
|
}
|
|
81
81
|
catch (e) {
|
|
82
|
-
|
|
82
|
+
this.swapLogger.error(swap, "processPastSwap(): Error calling htlcReceived(): ", e);
|
|
83
83
|
}
|
|
84
84
|
return false;
|
|
85
85
|
case "confirmed":
|
|
@@ -182,7 +182,7 @@ class FromBtcLnTrusted extends SwapHandler_1.SwapHandler {
|
|
|
182
182
|
await invoiceData.setState(FromBtcLnTrustedSwap_1.FromBtcLnTrustedSwapState.SENT);
|
|
183
183
|
await this.storageManager.saveData(invoice.id, null, invoiceData);
|
|
184
184
|
}
|
|
185
|
-
}).catch(e =>
|
|
185
|
+
}).catch(e => this.swapLogger.error(invoiceData, "htlcReceived(): Error sending transfer txns", e));
|
|
186
186
|
if (result == null) {
|
|
187
187
|
//Cancel invoice
|
|
188
188
|
await invoiceData.setState(FromBtcLnTrustedSwap_1.FromBtcLnTrustedSwapState.REFUNDED);
|
|
@@ -385,7 +385,6 @@ class FromBtcLnTrusted extends SwapHandler_1.SwapHandler {
|
|
|
385
385
|
abortController.signal.throwIfAborted();
|
|
386
386
|
metadata.times.invoiceCreated = Date.now();
|
|
387
387
|
metadata.invoiceResponse = { ...hodlInvoice };
|
|
388
|
-
console.log("[From BTC-LN: REST.CreateInvoice] hodl invoice created: ", hodlInvoice);
|
|
389
388
|
const createdSwap = new FromBtcLnTrustedSwap_1.FromBtcLnTrustedSwap(chainIdentifier, hodlInvoice.request, hodlInvoice.mtokens, swapFee, swapFeeInToken, totalInToken, secret.toString("hex"), parsedBody.address, useToken);
|
|
390
389
|
metadata.times.swapCreated = Date.now();
|
|
391
390
|
createdSwap.metadata = metadata;
|
package/dist/utils/Utils.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { Request, Response } from "express";
|
|
2
2
|
import { ServerParamEncoder } from "./paramcoders/server/ServerParamEncoder";
|
|
3
|
+
export type LoggerType = {
|
|
4
|
+
debug: (msg: string, ...args: any[]) => void;
|
|
5
|
+
info: (msg: string, ...args: any[]) => void;
|
|
6
|
+
warn: (msg: string, ...args: any[]) => void;
|
|
7
|
+
error: (msg: string, ...args: any[]) => void;
|
|
8
|
+
};
|
|
9
|
+
export declare function getLogger(prefix: string | (() => string)): LoggerType;
|
|
3
10
|
export type DefinedRuntimeError = {
|
|
4
11
|
code: number;
|
|
5
12
|
msg?: string;
|
|
@@ -10,13 +17,6 @@ export declare function isDefinedRuntimeError(obj: any): obj is DefinedRuntimeEr
|
|
|
10
17
|
export declare function expressHandlerWrapper(func: (req: Request, res: Response) => Promise<void>): ((req: Request, res: Response & {
|
|
11
18
|
responseStream: ServerParamEncoder;
|
|
12
19
|
}) => void);
|
|
13
|
-
export type LoggerType = {
|
|
14
|
-
debug: (msg: string, ...args: any[]) => void;
|
|
15
|
-
info: (msg: string, ...args: any[]) => void;
|
|
16
|
-
warn: (msg: string, ...args: any[]) => void;
|
|
17
|
-
error: (msg: string, ...args: any[]) => void;
|
|
18
|
-
};
|
|
19
|
-
export declare function getLogger(prefix: string): LoggerType;
|
|
20
20
|
export declare const HEX_REGEX: RegExp;
|
|
21
21
|
export declare function serializeBN(bn: bigint | null): string | null;
|
|
22
22
|
export declare function deserializeBN(str: string | null): bigint | null;
|
package/dist/utils/Utils.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAbortController = exports.bigIntSorter = exports.deserializeBN = exports.serializeBN = exports.HEX_REGEX = exports.
|
|
3
|
+
exports.getAbortController = exports.bigIntSorter = exports.deserializeBN = exports.serializeBN = exports.HEX_REGEX = exports.expressHandlerWrapper = exports.isDefinedRuntimeError = exports.getLogger = void 0;
|
|
4
|
+
function getLogger(prefix) {
|
|
5
|
+
return {
|
|
6
|
+
debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug((typeof (prefix) === "function" ? prefix() : prefix) + msg, ...args),
|
|
7
|
+
info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info((typeof (prefix) === "function" ? prefix() : prefix) + msg, ...args),
|
|
8
|
+
warn: (msg, ...args) => (global.atomiqLogLevel == null || global.atomiqLogLevel >= 1) && console.warn((typeof (prefix) === "function" ? prefix() : prefix) + msg, ...args),
|
|
9
|
+
error: (msg, ...args) => (global.atomiqLogLevel == null || global.atomiqLogLevel >= 0) && console.error((typeof (prefix) === "function" ? prefix() : prefix) + msg, ...args)
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
exports.getLogger = getLogger;
|
|
4
13
|
function isDefinedRuntimeError(obj) {
|
|
5
14
|
if (obj.code != null && typeof (obj.code) === "number") {
|
|
6
15
|
if (obj.msg != null && typeof (obj.msg) !== "string")
|
|
@@ -12,6 +21,7 @@ function isDefinedRuntimeError(obj) {
|
|
|
12
21
|
return false;
|
|
13
22
|
}
|
|
14
23
|
exports.isDefinedRuntimeError = isDefinedRuntimeError;
|
|
24
|
+
const expressHandlerWrapperLogger = getLogger("ExpressHandlerWrapper: ");
|
|
15
25
|
function expressHandlerWrapper(func) {
|
|
16
26
|
return (req, res) => {
|
|
17
27
|
(async () => {
|
|
@@ -19,7 +29,7 @@ function expressHandlerWrapper(func) {
|
|
|
19
29
|
await func(req, res);
|
|
20
30
|
}
|
|
21
31
|
catch (e) {
|
|
22
|
-
|
|
32
|
+
expressHandlerWrapperLogger.error("Error in called function " + req.path + ": ", e);
|
|
23
33
|
let statusCode = 500;
|
|
24
34
|
const obj = {
|
|
25
35
|
code: 0,
|
|
@@ -46,15 +56,6 @@ function expressHandlerWrapper(func) {
|
|
|
46
56
|
};
|
|
47
57
|
}
|
|
48
58
|
exports.expressHandlerWrapper = expressHandlerWrapper;
|
|
49
|
-
function getLogger(prefix) {
|
|
50
|
-
return {
|
|
51
|
-
debug: (msg, ...args) => console.debug(prefix + msg, ...args),
|
|
52
|
-
info: (msg, ...args) => console.info(prefix + msg, ...args),
|
|
53
|
-
warn: (msg, ...args) => console.warn(prefix + msg, ...args),
|
|
54
|
-
error: (msg, ...args) => console.error(prefix + msg, ...args)
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
exports.getLogger = getLogger;
|
|
58
59
|
exports.HEX_REGEX = /[0-9a-fA-F]+/;
|
|
59
60
|
function serializeBN(bn) {
|
|
60
61
|
return bn == null ? null : bn.toString(10);
|
|
@@ -4,6 +4,7 @@ exports.serverParamDecoder = exports.RequestParsingError = exports.RequestTimeou
|
|
|
4
4
|
const SchemaVerifier_1 = require("../SchemaVerifier");
|
|
5
5
|
const ParamDecoder_1 = require("../ParamDecoder");
|
|
6
6
|
const ServerParamEncoder_1 = require("./ServerParamEncoder");
|
|
7
|
+
const Utils_1 = require("../../Utils");
|
|
7
8
|
class RequestTimeoutError extends Error {
|
|
8
9
|
constructor() {
|
|
9
10
|
super("Request timed out");
|
|
@@ -20,6 +21,7 @@ class RequestParsingError extends Error {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
exports.RequestParsingError = RequestParsingError;
|
|
24
|
+
const logger = (0, Utils_1.getLogger)("ServerParamDecoder: ");
|
|
23
25
|
const serverParamDecoder = (timeoutMillis) => (req, res, next) => {
|
|
24
26
|
let timeout;
|
|
25
27
|
res.responseStream = new ServerParamEncoder_1.ServerParamEncoder(res, 200, req);
|
|
@@ -43,14 +45,14 @@ const serverParamDecoder = (timeoutMillis) => (req, res, next) => {
|
|
|
43
45
|
next();
|
|
44
46
|
}
|
|
45
47
|
catch (e) {
|
|
46
|
-
|
|
48
|
+
logger.error("error reading legacy (non-streaming) http request", e);
|
|
47
49
|
req.destroy(new RequestParsingError());
|
|
48
50
|
res.destroy(new RequestParsingError());
|
|
49
51
|
}
|
|
50
52
|
clearTimeout(timeout);
|
|
51
53
|
});
|
|
52
54
|
req.on("error", (e) => {
|
|
53
|
-
|
|
55
|
+
logger.error("error reading legacy (non-streaming) http request", e);
|
|
54
56
|
});
|
|
55
57
|
timeout = setTimeout(() => {
|
|
56
58
|
req.destroy(new RequestTimeoutError());
|
|
@@ -64,7 +66,7 @@ const serverParamDecoder = (timeoutMillis) => (req, res, next) => {
|
|
|
64
66
|
decoder.onData(data);
|
|
65
67
|
}
|
|
66
68
|
catch (e) {
|
|
67
|
-
|
|
69
|
+
logger.error("error reading streaming http request: on(\"data\")", e);
|
|
68
70
|
req.destroy(new RequestParsingError());
|
|
69
71
|
res.destroy(new RequestParsingError());
|
|
70
72
|
}
|
|
@@ -74,7 +76,7 @@ const serverParamDecoder = (timeoutMillis) => (req, res, next) => {
|
|
|
74
76
|
decoder.onEnd();
|
|
75
77
|
}
|
|
76
78
|
catch (e) {
|
|
77
|
-
|
|
79
|
+
logger.error("error reading streaming http request: on(\"end\")", e);
|
|
78
80
|
req.destroy(new RequestParsingError());
|
|
79
81
|
res.destroy(new RequestParsingError());
|
|
80
82
|
}
|
|
@@ -85,7 +87,7 @@ const serverParamDecoder = (timeoutMillis) => (req, res, next) => {
|
|
|
85
87
|
decoder.onError(e);
|
|
86
88
|
}
|
|
87
89
|
catch (e) {
|
|
88
|
-
|
|
90
|
+
logger.error("error reading streaming http request: on(\"error\")", e);
|
|
89
91
|
}
|
|
90
92
|
});
|
|
91
93
|
timeout = setTimeout(() => {
|
|
@@ -93,7 +95,7 @@ const serverParamDecoder = (timeoutMillis) => (req, res, next) => {
|
|
|
93
95
|
decoder.onEnd();
|
|
94
96
|
}
|
|
95
97
|
catch (e) {
|
|
96
|
-
|
|
98
|
+
logger.error("error reading streaming http request: timeout", e);
|
|
97
99
|
}
|
|
98
100
|
req.destroy(new RequestTimeoutError());
|
|
99
101
|
res.destroy(new RequestTimeoutError());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomiqlabs/lp-lib",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.0.0-dev.4",
|
|
4
4
|
"description": "Main functionality implementation for atomiq LP node",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types:": "./dist/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"author": "adambor",
|
|
23
23
|
"license": "ISC",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@atomiqlabs/base": "^
|
|
25
|
+
"@atomiqlabs/base": "^10.0.0-dev.1",
|
|
26
26
|
"@atomiqlabs/server-base": "2.0.0",
|
|
27
27
|
"@scure/btc-signer": "1.6.0",
|
|
28
28
|
"express": "4.21.1",
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {StorageObject} from "@atomiqlabs/base";
|
|
2
2
|
import * as fs from "fs/promises";
|
|
3
|
+
import * as fsSync from "fs";
|
|
3
4
|
import {IIntermediaryStorage, StorageQueryParam} from "../storage/IIntermediaryStorage";
|
|
5
|
+
import {getLogger, LoggerType} from "../utils/Utils";
|
|
4
6
|
|
|
5
7
|
export class IntermediaryStorageManager<T extends StorageObject> implements IIntermediaryStorage<T> {
|
|
6
8
|
|
|
@@ -9,9 +11,11 @@ export class IntermediaryStorageManager<T extends StorageObject> implements IInt
|
|
|
9
11
|
private data: {
|
|
10
12
|
[key: string]: T
|
|
11
13
|
} = {};
|
|
14
|
+
private logger: LoggerType;
|
|
12
15
|
|
|
13
16
|
constructor(directory: string) {
|
|
14
17
|
this.directory = directory;
|
|
18
|
+
this.logger = getLogger("IntermediaryStorageManager("+directory+"): ");
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
async init(): Promise<void> {
|
|
@@ -82,18 +86,23 @@ export class IntermediaryStorageManager<T extends StorageObject> implements IInt
|
|
|
82
86
|
if(this.data[identifier]!=null) delete this.data[identifier];
|
|
83
87
|
await fs.rm(this.directory+"/"+identifier+".json");
|
|
84
88
|
} catch (e) {
|
|
85
|
-
|
|
89
|
+
this.logger.error("removeData(): Error when removing data: ", e);
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
async loadData(type: new(data: any) => T): Promise<void> {
|
|
90
94
|
this.type = type;
|
|
91
95
|
|
|
96
|
+
if(!fsSync.existsSync(this.directory)) {
|
|
97
|
+
this.logger.debug("loadData(): Data directory not found!");
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
92
101
|
let files: string[];
|
|
93
102
|
try {
|
|
94
103
|
files = await fs.readdir(this.directory);
|
|
95
104
|
} catch (e) {
|
|
96
|
-
|
|
105
|
+
this.logger.error("loadData(): Error when checking directory: ", e);
|
|
97
106
|
return;
|
|
98
107
|
}
|
|
99
108
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {StorageObject, IStorageManager} from "@atomiqlabs/base";
|
|
2
2
|
import * as fs from "fs/promises";
|
|
3
|
+
import {getLogger, LoggerType} from "../utils/Utils";
|
|
4
|
+
import fsSync from "fs";
|
|
3
5
|
|
|
4
6
|
export class StorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
5
7
|
|
|
@@ -8,8 +10,11 @@ export class StorageManager<T extends StorageObject> implements IStorageManager<
|
|
|
8
10
|
[key: string]: T
|
|
9
11
|
} = {};
|
|
10
12
|
|
|
13
|
+
private logger: LoggerType;
|
|
14
|
+
|
|
11
15
|
constructor(directory: string) {
|
|
12
16
|
this.directory = directory;
|
|
17
|
+
this.logger = getLogger("StorageManager("+directory+"): ");
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
async init(): Promise<void> {
|
|
@@ -38,16 +43,21 @@ export class StorageManager<T extends StorageObject> implements IStorageManager<
|
|
|
38
43
|
if(this.data[paymentHash]!=null) delete this.data[paymentHash];
|
|
39
44
|
await fs.rm(this.directory+"/"+paymentHash+".json");
|
|
40
45
|
} catch (e) {
|
|
41
|
-
|
|
46
|
+
this.logger.error("removeData(): Error when removing data: ", e);
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
async loadData(type: new(data: any) => T): Promise<T[]> {
|
|
51
|
+
if(!fsSync.existsSync(this.directory)) {
|
|
52
|
+
this.logger.debug("loadData(): Data directory not found!");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
let files;
|
|
47
57
|
try {
|
|
48
58
|
files = await fs.readdir(this.directory);
|
|
49
59
|
} catch (e) {
|
|
50
|
-
|
|
60
|
+
this.logger.error("loadData(): Error when checking directory: ", e);
|
|
51
61
|
return [];
|
|
52
62
|
}
|
|
53
63
|
|
package/src/swaps/SwapHandler.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {SwapHandlerSwap} from "./SwapHandlerSwap";
|
|
|
8
8
|
import {PluginManager} from "../plugins/PluginManager";
|
|
9
9
|
import {IIntermediaryStorage} from "../storage/IIntermediaryStorage";
|
|
10
10
|
import {IParamReader} from "../utils/paramcoders/IParamReader";
|
|
11
|
+
import {getLogger, LoggerType} from "../utils/Utils";
|
|
11
12
|
|
|
12
13
|
export enum SwapHandlerType {
|
|
13
14
|
TO_BTC = "TO_BTC",
|
|
@@ -85,12 +86,7 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
85
86
|
|
|
86
87
|
abstract config: SwapBaseConfig;
|
|
87
88
|
|
|
88
|
-
logger =
|
|
89
|
-
debug: (msg: string, ...args: any) => console.debug("SwapHandler("+this.type+"): "+msg, ...args),
|
|
90
|
-
info: (msg: string, ...args: any) => console.info("SwapHandler("+this.type+"): "+msg, ...args),
|
|
91
|
-
warn: (msg: string, ...args: any) => console.warn("SwapHandler("+this.type+"): "+msg, ...args),
|
|
92
|
-
error: (msg: string, ...args: any) => console.error("SwapHandler("+this.type+"): "+msg, ...args)
|
|
93
|
-
};
|
|
89
|
+
logger: LoggerType = getLogger(() => "SwapHandler("+this.type+"): ");
|
|
94
90
|
|
|
95
91
|
protected swapLogger = {
|
|
96
92
|
debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => this.logger.debug(swap.getIdentifier()+": "+msg, ...args),
|
|
@@ -137,7 +133,7 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
137
133
|
async startWatchdog() {
|
|
138
134
|
let rerun: () => Promise<void>;
|
|
139
135
|
rerun = async () => {
|
|
140
|
-
await this.processPastSwaps().catch( e =>
|
|
136
|
+
await this.processPastSwaps().catch( e => this.logger.error("startWatchdog(): Error when processing past swaps: ", e));
|
|
141
137
|
setTimeout(rerun, this.config.swapCheckInterval);
|
|
142
138
|
};
|
|
143
139
|
await rerun();
|
|
@@ -46,7 +46,7 @@ export abstract class EscrowHandler<V extends EscrowHandlerSwap<SwapData, S>, S>
|
|
|
46
46
|
const swap = this.getSwapByEscrowHash(chainIdentifier, event.escrowHash);
|
|
47
47
|
if(swap==null) continue;
|
|
48
48
|
|
|
49
|
-
swap.txIds.init =
|
|
49
|
+
swap.txIds.init = event.meta?.txId;
|
|
50
50
|
if(swap.metadata!=null) swap.metadata.times.initTxReceived = Date.now();
|
|
51
51
|
|
|
52
52
|
await this.processInitializeEvent(chainIdentifier, swap, event);
|
|
@@ -54,7 +54,7 @@ export abstract class EscrowHandler<V extends EscrowHandlerSwap<SwapData, S>, S>
|
|
|
54
54
|
const swap = this.getSwapByEscrowHash(chainIdentifier, event.escrowHash);
|
|
55
55
|
if(swap==null) continue;
|
|
56
56
|
|
|
57
|
-
swap.txIds.claim =
|
|
57
|
+
swap.txIds.claim = event.meta?.txId;
|
|
58
58
|
if(swap.metadata!=null) swap.metadata.times.claimTxReceived = Date.now();
|
|
59
59
|
|
|
60
60
|
await this.processClaimEvent(chainIdentifier, swap, event);
|
|
@@ -62,7 +62,7 @@ export abstract class EscrowHandler<V extends EscrowHandlerSwap<SwapData, S>, S>
|
|
|
62
62
|
const swap = this.getSwapByEscrowHash(chainIdentifier, event.escrowHash);
|
|
63
63
|
if(swap==null) continue;
|
|
64
64
|
|
|
65
|
-
swap.txIds.refund =
|
|
65
|
+
swap.txIds.refund = event.meta?.txId;
|
|
66
66
|
if(swap.metadata!=null) swap.metadata.times.refundTxReceived = Date.now();
|
|
67
67
|
|
|
68
68
|
await this.processRefundEvent(chainIdentifier, swap, event);
|
|
@@ -8,10 +8,9 @@ import {
|
|
|
8
8
|
ClaimEvent,
|
|
9
9
|
InitializeEvent,
|
|
10
10
|
RefundEvent,
|
|
11
|
-
SwapCommitStatus,
|
|
12
11
|
SwapData,
|
|
13
12
|
BitcoinRpc,
|
|
14
|
-
BtcBlock, BigIntBufferUtils
|
|
13
|
+
BtcBlock, BigIntBufferUtils, SwapCommitStateType
|
|
15
14
|
} from "@atomiqlabs/base";
|
|
16
15
|
import {expressHandlerWrapper, getAbortController, HEX_REGEX, isDefinedRuntimeError} from "../../../utils/Utils";
|
|
17
16
|
import {PluginManager} from "../../../plugins/PluginManager";
|
|
@@ -169,13 +168,17 @@ export class ToBtcAbs extends ToBtcBaseSwapHandler<ToBtcSwapAbs, ToBtcSwapState>
|
|
|
169
168
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
170
169
|
if(!isCommited) {
|
|
171
170
|
const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
|
|
172
|
-
if(status===
|
|
171
|
+
if(status.type===SwapCommitStateType.PAID) {
|
|
173
172
|
this.swapLogger.info(swap, "processPastSwap(state=BTC_SENT): swap claimed (detected from processPastSwap), address: "+swap.address);
|
|
174
173
|
this.unsubscribePayment(swap);
|
|
174
|
+
swap.txIds ??= {};
|
|
175
|
+
swap.txIds.claim = await status.getClaimTxId();
|
|
175
176
|
await this.removeSwapData(swap, ToBtcSwapState.CLAIMED);
|
|
176
|
-
} else if(status===
|
|
177
|
+
} else if(status.type===SwapCommitStateType.EXPIRED) {
|
|
177
178
|
this.swapLogger.warn(swap, "processPastSwap(state=BTC_SENT): swap expired, but bitcoin was probably already sent, txId: "+swap.txId+" address: "+swap.address);
|
|
178
179
|
this.unsubscribePayment(swap);
|
|
180
|
+
swap.txIds ??= {};
|
|
181
|
+
swap.txIds.refund = status.getRefundTxId==null ? null : await status.getRefundTxId();
|
|
179
182
|
await this.removeSwapData(swap, ToBtcSwapState.REFUNDED);
|
|
180
183
|
}
|
|
181
184
|
return;
|
|
@@ -7,8 +7,7 @@ import {
|
|
|
7
7
|
ChainSwapType,
|
|
8
8
|
ClaimEvent,
|
|
9
9
|
InitializeEvent,
|
|
10
|
-
RefundEvent,
|
|
11
|
-
SwapCommitStatus,
|
|
10
|
+
RefundEvent, SwapCommitStateType,
|
|
12
11
|
SwapData
|
|
13
12
|
} from "@atomiqlabs/base";
|
|
14
13
|
import {expressHandlerWrapper, getAbortController, HEX_REGEX, isDefinedRuntimeError} from "../../../utils/Utils";
|
|
@@ -218,12 +217,16 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
|
|
|
218
217
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
219
218
|
if(!isCommited) {
|
|
220
219
|
const status = await swapContract.getCommitStatus(signer.getAddress(), swap.data);
|
|
221
|
-
if(status===
|
|
220
|
+
if(status?.type===SwapCommitStateType.PAID) {
|
|
222
221
|
//This is alright, we got the money
|
|
222
|
+
swap.txIds ??= {};
|
|
223
|
+
swap.txIds.claim = await status.getClaimTxId();
|
|
223
224
|
await this.removeSwapData(swap, ToBtcLnSwapState.CLAIMED);
|
|
224
225
|
return true;
|
|
225
|
-
} else if(status===
|
|
226
|
+
} else if(status?.type===SwapCommitStateType.EXPIRED) {
|
|
226
227
|
//This means the user was able to refund before we were able to claim, no good
|
|
228
|
+
swap.txIds ??= {};
|
|
229
|
+
swap.txIds.refund = status.getRefundTxId==null ? null : await status.getRefundTxId();
|
|
227
230
|
await this.removeSwapData(swap, ToBtcLnSwapState.REFUNDED);
|
|
228
231
|
}
|
|
229
232
|
this.swapLogger.warn(swap, "processPaymentResult(): tried to claim but escrow doesn't exist anymore,"+
|
|
@@ -496,7 +496,7 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
|
|
|
496
496
|
}
|
|
497
497
|
|
|
498
498
|
if(
|
|
499
|
-
data.
|
|
499
|
+
!data.isRecipient(swap.recipient) ||
|
|
500
500
|
data.callerFeeRate!==swap.callerFeeShare ||
|
|
501
501
|
data.frontingFeeRate!==swap.frontingFeeShare ||
|
|
502
502
|
data.executionFeeRate!==swap.executionFeeShare ||
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
SpvVaultOpenEvent, SpvWithdrawalTransactionData
|
|
9
9
|
} from "@atomiqlabs/base";
|
|
10
10
|
import {SpvVaultSwap} from "./SpvVaultSwap";
|
|
11
|
-
import {bigIntSorter} from "../../utils/Utils";
|
|
11
|
+
import {bigIntSorter, getLogger} from "../../utils/Utils";
|
|
12
12
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
13
13
|
import {IBitcoinWallet} from "../../wallets/IBitcoinWallet";
|
|
14
14
|
import {ISpvVaultSigner} from "../../wallets/ISpvVaultSigner";
|
|
@@ -30,12 +30,7 @@ export class SpvVaults {
|
|
|
30
30
|
readonly config: {vaultsCheckInterval: number, maxUnclaimedWithdrawals?: number};
|
|
31
31
|
readonly getChain: (chainId: string) => ChainData
|
|
32
32
|
|
|
33
|
-
readonly logger =
|
|
34
|
-
debug: (msg: string, ...args: any) => console.debug("SpvVaults: "+msg, ...args),
|
|
35
|
-
info: (msg: string, ...args: any) => console.info("SpvVaults: "+msg, ...args),
|
|
36
|
-
warn: (msg: string, ...args: any) => console.warn("SpvVaults: "+msg, ...args),
|
|
37
|
-
error: (msg: string, ...args: any) => console.error("SpvVaults: "+msg, ...args)
|
|
38
|
-
};
|
|
33
|
+
readonly logger = getLogger("SpvVaults: ");
|
|
39
34
|
|
|
40
35
|
constructor(
|
|
41
36
|
vaultStorage: IStorageManager<SpvVault>,
|
|
@@ -428,7 +423,7 @@ export class SpvVaults {
|
|
|
428
423
|
async startVaultsWatchdog() {
|
|
429
424
|
let rerun: () => Promise<void>;
|
|
430
425
|
rerun = async () => {
|
|
431
|
-
await this.checkVaults().catch( e =>
|
|
426
|
+
await this.checkVaults().catch( e => this.logger.error("startVaultsWatchdog(): Error when periodically checking SPV vaults: ", e));
|
|
432
427
|
setTimeout(rerun, this.config.vaultsCheckInterval);
|
|
433
428
|
};
|
|
434
429
|
await rerun();
|
|
@@ -713,7 +713,7 @@ export class FromBtcTrusted extends SwapHandler<FromBtcTrustedSwap, FromBtcTrust
|
|
|
713
713
|
private async startDoubleSpendWatchdog() {
|
|
714
714
|
let rerun: () => Promise<void>;
|
|
715
715
|
rerun = async () => {
|
|
716
|
-
await this.checkDoubleSpends().catch( e =>
|
|
716
|
+
await this.checkDoubleSpends().catch( e => this.logger.error("startDoubleSpendWatchdog(): Error when checking double spends: ", e));
|
|
717
717
|
setTimeout(rerun, this.config.doubleSpendCheckInterval);
|
|
718
718
|
};
|
|
719
719
|
await rerun();
|
|
@@ -90,7 +90,7 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
90
90
|
this.lightning.waitForInvoice(hash, abortController.signal).then(invoice => {
|
|
91
91
|
this.swapLogger.debug(invoiceData, "subscribeToInvoice(): invoice_updated: ", invoice);
|
|
92
92
|
if(invoice.status!=="held") return;
|
|
93
|
-
this.htlcReceived(invoiceData, invoice).catch(e =>
|
|
93
|
+
this.htlcReceived(invoiceData, invoice).catch(e => this.swapLogger.error(invoiceData, "subscribeToInvoice(): Error calling htlcReceived(): ", e));
|
|
94
94
|
this.activeSubscriptions.delete(hash);
|
|
95
95
|
});
|
|
96
96
|
|
|
@@ -117,7 +117,7 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
117
117
|
await this.htlcReceived(swap, invoice);
|
|
118
118
|
//Result is either FromBtcLnTrustedSwapState.RECEIVED or FromBtcLnTrustedSwapState.CANCELED
|
|
119
119
|
} catch (e) {
|
|
120
|
-
|
|
120
|
+
this.swapLogger.error(swap, "processPastSwap(): Error calling htlcReceived(): ", e);
|
|
121
121
|
}
|
|
122
122
|
return false;
|
|
123
123
|
case "confirmed":
|
|
@@ -226,7 +226,7 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
226
226
|
await invoiceData.setState(FromBtcLnTrustedSwapState.SENT);
|
|
227
227
|
await this.storageManager.saveData(invoice.id, null, invoiceData);
|
|
228
228
|
}
|
|
229
|
-
}).catch(e =>
|
|
229
|
+
}).catch(e => this.swapLogger.error(invoiceData, "htlcReceived(): Error sending transfer txns", e));
|
|
230
230
|
|
|
231
231
|
if(result==null) {
|
|
232
232
|
//Cancel invoice
|
|
@@ -459,8 +459,6 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
459
459
|
metadata.times.invoiceCreated = Date.now();
|
|
460
460
|
metadata.invoiceResponse = {...hodlInvoice};
|
|
461
461
|
|
|
462
|
-
console.log("[From BTC-LN: REST.CreateInvoice] hodl invoice created: ", hodlInvoice);
|
|
463
|
-
|
|
464
462
|
const createdSwap = new FromBtcLnTrustedSwap(
|
|
465
463
|
chainIdentifier,
|
|
466
464
|
hodlInvoice.request,
|
package/src/utils/Utils.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import {Request, Response} from "express";
|
|
2
2
|
import {ServerParamEncoder} from "./paramcoders/server/ServerParamEncoder";
|
|
3
3
|
|
|
4
|
+
export type LoggerType = {
|
|
5
|
+
debug: (msg: string, ...args: any[]) => void,
|
|
6
|
+
info: (msg: string, ...args: any[]) => void,
|
|
7
|
+
warn: (msg: string, ...args: any[]) => void,
|
|
8
|
+
error: (msg: string, ...args: any[]) => void
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function getLogger(prefix: string | (() => string)): LoggerType {
|
|
12
|
+
return {
|
|
13
|
+
debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
|
|
14
|
+
info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
|
|
15
|
+
warn: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 1) && console.warn((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
|
|
16
|
+
error: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 0) && console.error((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
4
20
|
export type DefinedRuntimeError = {
|
|
5
21
|
code: number;
|
|
6
22
|
msg?: string;
|
|
@@ -17,6 +33,8 @@ export function isDefinedRuntimeError(obj: any): obj is DefinedRuntimeError {
|
|
|
17
33
|
return false;
|
|
18
34
|
}
|
|
19
35
|
|
|
36
|
+
const expressHandlerWrapperLogger = getLogger("ExpressHandlerWrapper: ");
|
|
37
|
+
|
|
20
38
|
export function expressHandlerWrapper(func: (
|
|
21
39
|
req: Request,
|
|
22
40
|
res: Response
|
|
@@ -32,7 +50,7 @@ export function expressHandlerWrapper(func: (
|
|
|
32
50
|
try {
|
|
33
51
|
await func(req, res);
|
|
34
52
|
} catch (e) {
|
|
35
|
-
|
|
53
|
+
expressHandlerWrapperLogger.error("Error in called function "+req.path+": ", e);
|
|
36
54
|
let statusCode = 500;
|
|
37
55
|
const obj: {code: number, msg: string, data?: any} = {
|
|
38
56
|
code: 0,
|
|
@@ -56,22 +74,6 @@ export function expressHandlerWrapper(func: (
|
|
|
56
74
|
}
|
|
57
75
|
}
|
|
58
76
|
|
|
59
|
-
export type LoggerType = {
|
|
60
|
-
debug: (msg: string, ...args: any[]) => void,
|
|
61
|
-
info: (msg: string, ...args: any[]) => void,
|
|
62
|
-
warn: (msg: string, ...args: any[]) => void,
|
|
63
|
-
error: (msg: string, ...args: any[]) => void
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export function getLogger(prefix: string): LoggerType {
|
|
67
|
-
return {
|
|
68
|
-
debug: (msg, ...args) => console.debug(prefix+msg, ...args),
|
|
69
|
-
info: (msg, ...args) => console.info(prefix+msg, ...args),
|
|
70
|
-
warn: (msg, ...args) => console.warn(prefix+msg, ...args),
|
|
71
|
-
error: (msg, ...args) => console.error(prefix+msg, ...args)
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
77
|
export const HEX_REGEX = /[0-9a-fA-F]+/;
|
|
76
78
|
|
|
77
79
|
export function serializeBN(bn: bigint | null): string | null {
|
|
@@ -3,6 +3,7 @@ import {RequestSchema, verifySchema} from "../SchemaVerifier";
|
|
|
3
3
|
import {ParamDecoder} from "../ParamDecoder";
|
|
4
4
|
import {ServerParamEncoder} from "./ServerParamEncoder";
|
|
5
5
|
import {IParamReader} from "../IParamReader";
|
|
6
|
+
import {getLogger} from "../../Utils";
|
|
6
7
|
|
|
7
8
|
export class RequestTimeoutError extends Error {
|
|
8
9
|
|
|
@@ -24,6 +25,8 @@ export class RequestParsingError extends Error {
|
|
|
24
25
|
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
const logger = getLogger("ServerParamDecoder: ");
|
|
29
|
+
|
|
27
30
|
export const serverParamDecoder = (timeoutMillis: number) => (req: Request, res: Response, next: () => void) => {
|
|
28
31
|
|
|
29
32
|
let timeout;
|
|
@@ -50,14 +53,14 @@ export const serverParamDecoder = (timeoutMillis: number) => (req: Request, res:
|
|
|
50
53
|
(req as any).paramReader = paramReader;
|
|
51
54
|
next();
|
|
52
55
|
} catch (e) {
|
|
53
|
-
|
|
56
|
+
logger.error("error reading legacy (non-streaming) http request", e);
|
|
54
57
|
req.destroy(new RequestParsingError());
|
|
55
58
|
res.destroy(new RequestParsingError());
|
|
56
59
|
}
|
|
57
60
|
clearTimeout(timeout);
|
|
58
61
|
});
|
|
59
62
|
req.on("error", (e) => {
|
|
60
|
-
|
|
63
|
+
logger.error("error reading legacy (non-streaming) http request",e);
|
|
61
64
|
});
|
|
62
65
|
|
|
63
66
|
timeout = setTimeout(() => {
|
|
@@ -74,7 +77,7 @@ export const serverParamDecoder = (timeoutMillis: number) => (req: Request, res:
|
|
|
74
77
|
try {
|
|
75
78
|
decoder.onData(data);
|
|
76
79
|
} catch (e) {
|
|
77
|
-
|
|
80
|
+
logger.error("error reading streaming http request: on(\"data\")", e);
|
|
78
81
|
req.destroy(new RequestParsingError());
|
|
79
82
|
res.destroy(new RequestParsingError());
|
|
80
83
|
}
|
|
@@ -83,7 +86,7 @@ export const serverParamDecoder = (timeoutMillis: number) => (req: Request, res:
|
|
|
83
86
|
try {
|
|
84
87
|
decoder.onEnd();
|
|
85
88
|
} catch (e) {
|
|
86
|
-
|
|
89
|
+
logger.error("error reading streaming http request: on(\"end\")", e);
|
|
87
90
|
req.destroy(new RequestParsingError());
|
|
88
91
|
res.destroy(new RequestParsingError());
|
|
89
92
|
}
|
|
@@ -93,7 +96,7 @@ export const serverParamDecoder = (timeoutMillis: number) => (req: Request, res:
|
|
|
93
96
|
try {
|
|
94
97
|
decoder.onError(e);
|
|
95
98
|
} catch(e) {
|
|
96
|
-
|
|
99
|
+
logger.error("error reading streaming http request: on(\"error\")", e);
|
|
97
100
|
}
|
|
98
101
|
});
|
|
99
102
|
|
|
@@ -101,7 +104,7 @@ export const serverParamDecoder = (timeoutMillis: number) => (req: Request, res:
|
|
|
101
104
|
try {
|
|
102
105
|
decoder.onEnd();
|
|
103
106
|
} catch(e) {
|
|
104
|
-
|
|
107
|
+
logger.error("error reading streaming http request: timeout", e);
|
|
105
108
|
}
|
|
106
109
|
req.destroy(new RequestTimeoutError());
|
|
107
110
|
res.destroy(new RequestTimeoutError());
|