@atomiqlabs/sdk 8.6.2 → 8.6.3

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.
@@ -2,6 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ParamDecoder = void 0;
4
4
  const buffer_1 = require("buffer");
5
+ function ensureBuffer(input) {
6
+ if (input instanceof buffer_1.Buffer)
7
+ return input;
8
+ return buffer_1.Buffer.from(input);
9
+ }
5
10
  class ParamDecoder {
6
11
  constructor() {
7
12
  this.frameData = [];
@@ -48,8 +53,8 @@ class ParamDecoder {
48
53
  leavesBuffer = null;
49
54
  }
50
55
  else {
51
- this.frameHeader = leavesBuffer.subarray(0, 4);
52
- leavesBuffer = leavesBuffer.subarray(4);
56
+ this.frameHeader = ensureBuffer(leavesBuffer.subarray(0, 4));
57
+ leavesBuffer = ensureBuffer(leavesBuffer.subarray(4));
53
58
  }
54
59
  }
55
60
  else if (this.frameHeader.length < 4) {
@@ -60,14 +65,14 @@ class ParamDecoder {
60
65
  }
61
66
  else {
62
67
  this.frameHeader = buffer_1.Buffer.concat([this.frameHeader, leavesBuffer.subarray(0, requiredLen)]);
63
- leavesBuffer = leavesBuffer.subarray(requiredLen);
68
+ leavesBuffer = ensureBuffer(leavesBuffer.subarray(requiredLen));
64
69
  }
65
70
  }
66
71
  if (leavesBuffer == null)
67
72
  continue;
68
73
  if (this.frameHeader == null || this.frameHeader.length < 4)
69
74
  continue;
70
- const frameLength = this.frameHeader.readUint32LE();
75
+ const frameLength = this.frameHeader.readUint32LE != null ? this.frameHeader.readUint32LE() : this.frameHeader.readUInt32LE();
71
76
  const requiredLen = frameLength - this.frameDataLength;
72
77
  if (leavesBuffer.length <= requiredLen) {
73
78
  this.frameData.push(leavesBuffer);
@@ -15,7 +15,12 @@ class ParamEncoder {
15
15
  writeParams(data) {
16
16
  const serialized = buffer_1.Buffer.from(JSON.stringify(data));
17
17
  const frameLengthBuffer = buffer_1.Buffer.alloc(4);
18
- frameLengthBuffer.writeUint32LE(serialized.length);
18
+ if (frameLengthBuffer.writeUint32LE != null) {
19
+ frameLengthBuffer.writeUint32LE(serialized.length);
20
+ }
21
+ else {
22
+ frameLengthBuffer.writeUInt32LE(serialized.length);
23
+ }
19
24
  return this.writeFN(buffer_1.Buffer.concat([
20
25
  frameLengthBuffer,
21
26
  serialized
@@ -129,7 +129,7 @@ class IntermediaryDiscovery extends events_1.EventEmitter {
129
129
  * @param abortSignal
130
130
  */
131
131
  async getNodeInfo(url, abortSignal) {
132
- const response = await (0, RetryUtils_1.tryWithRetries)(() => IntermediaryAPI_1.IntermediaryAPI.getIntermediaryInfo(url, this.httpRequestTimeout, abortSignal), { maxRetries: 3, delay: 100, exponential: true }, undefined, abortSignal);
132
+ const response = await (0, RetryUtils_1.tryWithRetries)(() => IntermediaryAPI_1.IntermediaryAPI.getIntermediaryInfo(url, this.httpRequestTimeout, abortSignal), { maxRetries: 3, delay: 100, exponential: true }, undefined, abortSignal, "debug");
133
133
  abortSignal?.throwIfAborted();
134
134
  const promises = [];
135
135
  const addresses = {};
@@ -142,7 +142,7 @@ class IntermediaryDiscovery extends events_1.EventEmitter {
142
142
  addresses[chain] = address;
143
143
  }
144
144
  catch (e) {
145
- logger.warn("Failed to verify " + chain + " signature for intermediary: " + url);
145
+ logger.warn("getNodeInfo(): Failed to verify " + chain + " signature for intermediary: " + url);
146
146
  }
147
147
  })());
148
148
  }
@@ -191,7 +191,8 @@ class IntermediaryDiscovery extends events_1.EventEmitter {
191
191
  return new Intermediary_1.Intermediary(url, nodeInfo.addresses, services);
192
192
  }
193
193
  catch (e) {
194
- logger.warn("fetchIntermediaries(): Error contacting intermediary " + url + ": ", e);
194
+ logger.warn("fetchIntermediaries(): Intermediary " + url + ` is unreachable due to ${e.name ?? e.message} error, skipping...`);
195
+ logger.debug("fetchIntermediaries(): Error contacting intermediary " + url + ": ", e);
195
196
  return null;
196
197
  }
197
198
  }
@@ -182,7 +182,7 @@ class Swapper extends events_1.EventEmitter {
182
182
  });
183
183
  }
184
184
  async _init() {
185
- this.logger.debug("init(): Initializing swapper, sdk-lib version 16.1.3");
185
+ this.logger.debug("init(): Initializing swapper...");
186
186
  const abortController = new AbortController();
187
187
  const promises = [];
188
188
  let automaticClockDriftCorrectionPromise = undefined;
@@ -218,7 +218,11 @@ class Swapper extends events_1.EventEmitter {
218
218
  const chainPromises = [];
219
219
  for (let chainIdentifier in this._chains) {
220
220
  chainPromises.push((async () => {
221
- const { swapContract, unifiedChainEvents, unifiedSwapStorage, wrappers, reviver } = this._chains[chainIdentifier];
221
+ const { chainInterface, swapContract, unifiedChainEvents, unifiedSwapStorage, wrappers, reviver } = this._chains[chainIdentifier];
222
+ const _chainInterface = chainInterface;
223
+ if (_chainInterface.verifyNetwork != null) {
224
+ await _chainInterface.verifyNetwork(this.bitcoinNetwork);
225
+ }
222
226
  await swapContract.start();
223
227
  this.logger.debug("init(): Intialized swap contract: " + chainIdentifier);
224
228
  await unifiedSwapStorage.init();
@@ -10,6 +10,7 @@ type Constructor<T = any> = new (...args: any[]) => T;
10
10
  * @param retryPolicy.exponential Whether to use exponentially increasing delays
11
11
  * @param errorAllowed A callback for determining whether a given error is allowed, and we should therefore not retry
12
12
  * @param abortSignal
13
+ * @param failureLogLevel
13
14
  * @returns Result of the action executing callback
14
15
  * @category Utilities
15
16
  */
@@ -17,5 +18,5 @@ export declare function tryWithRetries<T>(func: (retryCount: number) => Promise<
17
18
  maxRetries?: number;
18
19
  delay?: number;
19
20
  exponential?: boolean;
20
- }, errorAllowed?: ((e: any) => boolean) | Constructor<Error> | Constructor<Error>[], abortSignal?: AbortSignal): Promise<T>;
21
+ }, errorAllowed?: ((e: any) => boolean) | Constructor<Error> | Constructor<Error>[], abortSignal?: AbortSignal, failureLogLevel?: "debug" | "info" | "warn" | "error"): Promise<T>;
21
22
  export {};
@@ -36,10 +36,11 @@ function checkError(e, errorAllowed) {
36
36
  * @param retryPolicy.exponential Whether to use exponentially increasing delays
37
37
  * @param errorAllowed A callback for determining whether a given error is allowed, and we should therefore not retry
38
38
  * @param abortSignal
39
+ * @param failureLogLevel
39
40
  * @returns Result of the action executing callback
40
41
  * @category Utilities
41
42
  */
42
- async function tryWithRetries(func, retryPolicy, errorAllowed, abortSignal) {
43
+ async function tryWithRetries(func, retryPolicy, errorAllowed, abortSignal, failureLogLevel = "warn") {
43
44
  retryPolicy = retryPolicy || {};
44
45
  retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
45
46
  retryPolicy.delay = retryPolicy.delay || 500;
@@ -53,7 +54,7 @@ async function tryWithRetries(func, retryPolicy, errorAllowed, abortSignal) {
53
54
  if (errorAllowed != null && checkError(e, errorAllowed))
54
55
  throw e;
55
56
  err = e;
56
- logger.debug("tryWithRetries(): Error on try number: " + i, e);
57
+ logger[failureLogLevel]("tryWithRetries(): Error on try number: " + i, e);
57
58
  }
58
59
  if (abortSignal != null && abortSignal.aborted)
59
60
  throw (abortSignal.reason || new Error("Aborted"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/sdk",
3
- "version": "8.6.2",
3
+ "version": "8.6.3",
4
4
  "description": "atomiq labs SDK for cross-chain swaps between smart chains and bitcoin",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -1,6 +1,10 @@
1
1
  import {IParamReader} from "./IParamReader";
2
2
  import {Buffer} from "buffer";
3
3
 
4
+ function ensureBuffer(input: any): Buffer {
5
+ if(input instanceof Buffer) return input;
6
+ return Buffer.from(input);
7
+ }
4
8
 
5
9
  export class ParamDecoder implements IParamReader {
6
10
 
@@ -56,8 +60,8 @@ export class ParamDecoder implements IParamReader {
56
60
  this.frameHeader = leavesBuffer;
57
61
  leavesBuffer = null;
58
62
  } else {
59
- this.frameHeader = leavesBuffer.subarray(0, 4);
60
- leavesBuffer = leavesBuffer.subarray(4);
63
+ this.frameHeader = ensureBuffer(leavesBuffer.subarray(0, 4));
64
+ leavesBuffer = ensureBuffer(leavesBuffer.subarray(4));
61
65
  }
62
66
  } else if(this.frameHeader.length<4) {
63
67
  const requiredLen = 4-this.frameHeader.length;
@@ -66,13 +70,13 @@ export class ParamDecoder implements IParamReader {
66
70
  leavesBuffer = null;
67
71
  } else {
68
72
  this.frameHeader = Buffer.concat([this.frameHeader, leavesBuffer.subarray(0, requiredLen)]);
69
- leavesBuffer = leavesBuffer.subarray(requiredLen);
73
+ leavesBuffer = ensureBuffer(leavesBuffer.subarray(requiredLen));
70
74
  }
71
75
  }
72
76
  if(leavesBuffer==null) continue;
73
77
  if(this.frameHeader==null || this.frameHeader.length<4) continue;
74
78
 
75
- const frameLength = this.frameHeader.readUint32LE();
79
+ const frameLength = this.frameHeader.readUint32LE!=null ? this.frameHeader.readUint32LE() : this.frameHeader.readUInt32LE();
76
80
  const requiredLen = frameLength-this.frameDataLength;
77
81
 
78
82
  if(leavesBuffer.length<=requiredLen) {
@@ -19,7 +19,11 @@ export class ParamEncoder {
19
19
  const serialized: Buffer = Buffer.from(JSON.stringify(data));
20
20
 
21
21
  const frameLengthBuffer = Buffer.alloc(4);
22
- frameLengthBuffer.writeUint32LE(serialized.length);
22
+ if(frameLengthBuffer.writeUint32LE!=null) {
23
+ frameLengthBuffer.writeUint32LE(serialized.length);
24
+ } else {
25
+ frameLengthBuffer.writeUInt32LE(serialized.length);
26
+ }
23
27
 
24
28
  return this.writeFN(Buffer.concat([
25
29
  frameLengthBuffer,
@@ -241,7 +241,8 @@ export class IntermediaryDiscovery extends EventEmitter {
241
241
  () => IntermediaryAPI.getIntermediaryInfo(url, this.httpRequestTimeout, abortSignal),
242
242
  {maxRetries: 3, delay: 100, exponential: true},
243
243
  undefined,
244
- abortSignal
244
+ abortSignal,
245
+ "debug"
245
246
  );
246
247
  abortSignal?.throwIfAborted();
247
248
 
@@ -255,7 +256,7 @@ export class IntermediaryDiscovery extends EventEmitter {
255
256
  await this.swapContracts[chain].isValidDataSignature(Buffer.from(response.envelope), signature, address);
256
257
  addresses[chain] = address;
257
258
  } catch (e) {
258
- logger.warn("Failed to verify "+chain+" signature for intermediary: "+url);
259
+ logger.warn("getNodeInfo(): Failed to verify "+chain+" signature for intermediary: "+url);
259
260
  }
260
261
  })());
261
262
  }
@@ -303,8 +304,9 @@ export class IntermediaryDiscovery extends EventEmitter {
303
304
  services[swapHandlerTypeToSwapType(key as SwapHandlerType)] = nodeInfo.info.services[key as SwapHandlerType];
304
305
  }
305
306
  return new Intermediary(url, nodeInfo.addresses, services);
306
- } catch (e) {
307
- logger.warn("fetchIntermediaries(): Error contacting intermediary "+url+": ", e);
307
+ } catch (e: any) {
308
+ logger.warn("fetchIntermediaries(): Intermediary "+url+` is unreachable due to ${e.name ?? e.message} error, skipping...`);
309
+ logger.debug("fetchIntermediaries(): Error contacting intermediary "+url+": ", e);
308
310
  return null;
309
311
  }
310
312
  }
@@ -553,7 +553,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
553
553
  }
554
554
 
555
555
  private async _init(): Promise<void> {
556
- this.logger.debug("init(): Initializing swapper, sdk-lib version 16.1.3");
556
+ this.logger.debug("init(): Initializing swapper...");
557
557
 
558
558
  const abortController = new AbortController();
559
559
 
@@ -594,12 +594,19 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
594
594
  for(let chainIdentifier in this._chains) {
595
595
  chainPromises.push((async() => {
596
596
  const {
597
+ chainInterface,
597
598
  swapContract,
598
599
  unifiedChainEvents,
599
600
  unifiedSwapStorage,
600
601
  wrappers,
601
602
  reviver
602
603
  } = this._chains[chainIdentifier];
604
+
605
+ const _chainInterface: any = chainInterface;
606
+ if(_chainInterface.verifyNetwork!=null) {
607
+ await _chainInterface.verifyNetwork(this.bitcoinNetwork);
608
+ }
609
+
603
610
  await swapContract.start();
604
611
  this.logger.debug("init(): Intialized swap contract: "+chainIdentifier);
605
612
 
@@ -40,12 +40,19 @@ function checkError(e: any, errorAllowed: ((e: any) => boolean) | Constructor<Er
40
40
  * @param retryPolicy.exponential Whether to use exponentially increasing delays
41
41
  * @param errorAllowed A callback for determining whether a given error is allowed, and we should therefore not retry
42
42
  * @param abortSignal
43
+ * @param failureLogLevel
43
44
  * @returns Result of the action executing callback
44
45
  * @category Utilities
45
46
  */
46
- export async function tryWithRetries<T>(func: (retryCount: number) => Promise<T>, retryPolicy?: {
47
- maxRetries?: number, delay?: number, exponential?: boolean
48
- }, errorAllowed?: ((e: any) => boolean) | Constructor<Error> | Constructor<Error>[], abortSignal?: AbortSignal): Promise<T> {
47
+ export async function tryWithRetries<T>(
48
+ func: (retryCount: number) => Promise<T>,
49
+ retryPolicy?: {
50
+ maxRetries?: number, delay?: number, exponential?: boolean
51
+ },
52
+ errorAllowed?: ((e: any) => boolean) | Constructor<Error> | Constructor<Error>[],
53
+ abortSignal?: AbortSignal,
54
+ failureLogLevel: "debug" | "info" | "warn" | "error" = "warn"
55
+ ): Promise<T> {
49
56
  retryPolicy = retryPolicy || {};
50
57
  retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
51
58
  retryPolicy.delay = retryPolicy.delay || 500;
@@ -59,7 +66,7 @@ export async function tryWithRetries<T>(func: (retryCount: number) => Promise<T>
59
66
  } catch (e) {
60
67
  if (errorAllowed != null && checkError(e, errorAllowed)) throw e;
61
68
  err = e;
62
- logger.debug("tryWithRetries(): Error on try number: " + i, e);
69
+ logger[failureLogLevel]("tryWithRetries(): Error on try number: " + i, e);
63
70
  }
64
71
  if (abortSignal != null && abortSignal.aborted) throw (abortSignal.reason || new Error("Aborted"));
65
72
  if (i !== retryPolicy.maxRetries - 1) {