@bitpay-labs/bitcore-wallet-service 11.6.0 → 11.6.2

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.
@@ -375,61 +375,66 @@ export const Utils = {
375
375
 
376
376
  /**
377
377
  * Sort array by keys in ascending order
378
- * @param {Array<any>} arr Array to be sorted
379
- * @param {...string|Array<string>} keys Keys to sort by in order. If a key is an array, it will be treated as a nested key.
378
+ * @param {Array<T>} arr Array to be sorted
379
+ * @param {...(string|string[])} keys Keys to sort by in order. If a key is an array, it will be treated as a nested key.
380
380
  * e.g.: sortAsc(arr, 'a', 'b', ['c', 'd']) will sort by a, then b, then c.d
381
381
  * @returns
382
382
  */
383
- sortAsc(arr, ...keys) {
384
- function transformVals(val1, val2) {
385
- if (val1 === undefined) {
386
- val1 = '\uFFFF'; // highest possible string value, otherwise 'undefined' < 'xyz'
387
- } else if (val1 === null || val1 === false || val1.toString() === 'NaN') {
388
- val1 = '0';
389
- } else if (val1 === true) {
390
- val1 = '1';
383
+ sortAsc<T = any>(arr: Array<T>, ...keys: (string | string[])[]): Array<T> {
384
+ function normalizeVal(v) {
385
+ if (v === undefined) return '\uFFFF'; // highest possible value, sorts last
386
+ if (v === null || v === false || v?.toString() === 'NaN') return 0;
387
+ if (v === true) return 1;
388
+ return v;
389
+ }
390
+
391
+ function compareVals(a, b): number {
392
+ a = normalizeVal(a);
393
+ b = normalizeVal(b);
394
+
395
+ // Compare bigints directly to avoid loss of precision via Number()
396
+ if (typeof a === 'bigint' && typeof b === 'bigint') {
397
+ return a < b ? -1 : a > b ? 1 : 0;
391
398
  }
392
399
 
393
- if (val2 === undefined) {
394
- val2 = '\uFFFF'; // highest possible string value, otherwise 'undefined' < 'xyz'
395
- } else if (val2 === null || val2 === false || val2.toString() === 'NaN') {
396
- val2 = '0';
397
- } else if (val2 === true) {
398
- val2 = '1';
400
+ // Numeric comparison for numbers, bigints, and non-empty numeric strings
401
+ const aIsNumeric = typeof a !== 'string' ? !isNaN(Number(a)) : a.trim() !== '' && !isNaN(Number(a));
402
+ const bIsNumeric = typeof b !== 'string' ? !isNaN(Number(b)) : b.trim() !== '' && !isNaN(Number(b));
403
+ if (aIsNumeric && bIsNumeric) {
404
+ const na = Number(a);
405
+ const nb = Number(b);
406
+ return na < nb ? -1 : na > nb ? 1 : 0;
399
407
  }
400
408
 
401
- return [Buffer.from(val1.toString()), Buffer.from(val2.toString())];
402
- };
409
+ // String comparison fallback
410
+ const sa = String(a);
411
+ const sb = String(b);
412
+ return sa < sb ? -1 : sa > sb ? 1 : 0;
413
+ }
403
414
 
404
415
  if (!keys.length) {
405
- return arr.sort((a, b) => { const [_a, _b] = transformVals(a, b); return _a.compare(_b); });
406
- } else if (keys.length === 1 && !Array.isArray(keys[0])) {
407
- return arr.sort((a, b) => { const [_a, _b] = transformVals(a[keys[0]], b[keys[0]]); return _a.compare(_b); });
416
+ return arr.sort((a, b) => compareVals(a, b));
408
417
  }
418
+
409
419
  return arr.sort((a, b) => {
410
- // compare concatenated strings for multiple key sorting
411
- let aVal = '';
412
- let bVal = '';
413
420
  for (const k of keys) {
414
- const [aTemp, bTemp] = transformVals(
421
+ const cmp = compareVals(
415
422
  Array.isArray(k) ? k.reduce((val, key) => val[key], a) : a[k],
416
423
  Array.isArray(k) ? k.reduce((val, key) => val[key], b) : b[k]
417
424
  );
418
-
419
- aVal += aTemp.toString();
420
- bVal += bTemp.toString();
425
+ if (cmp !== 0) return cmp;
421
426
  }
422
- return Buffer.from(aVal).compare(Buffer.from(bVal));
427
+ return 0;
423
428
  });
424
429
  },
425
430
 
426
431
  /**
427
432
  * Sort array by keys in descending order
428
- * @param {Array<any>} arr Array to be sorted
429
- * @param {...string|Array<string>} keys Keys to sort by in order. If a key is an array, it will be treated as a nested key.
433
+ * @param {Array<T>} arr Array to be sorted
434
+ * @param {...(string|string[])} keys Keys to sort by in order. If a key is an array, it will be treated as a nested key.
430
435
  * e.g.: sortDesc(arr, 'a', 'b', ['c', 'd']) will sort by a, then b, then c.d
431
436
  */
432
- sortDesc(arr: Array<any>, ...keys: string[]) {
437
+ sortDesc<T = any>(arr: Array<T>, ...keys: (string | string[])[]): Array<T> {
433
438
  return Utils.sortAsc(arr, ...keys).reverse();
434
439
  },
435
440
 
package/src/lib/tss.ts CHANGED
@@ -185,12 +185,14 @@ class TssKeyGenClass {
185
185
  id: string;
186
186
  message: {
187
187
  publicKey: ITssKeyMessageObject['publicKey'];
188
+ /** unused */
188
189
  encryptedKeyChain: string;
189
190
  };
191
+ /** unused - only needed for storing the encryptedKeyChain */
190
192
  copayerId: string;
191
193
  }) {
192
- const { id, message, copayerId } = params;
193
- const { publicKey, encryptedKeyChain } = message;
194
+ const { id, message } = params;
195
+ const { publicKey } = message;
194
196
  if (!publicKey) {
195
197
  throw Errors.TSS_GENERIC_ERROR.withMessage('No public key provided');
196
198
  }
@@ -210,15 +212,21 @@ class TssKeyGenClass {
210
212
  }
211
213
  }
212
214
 
213
- if (encryptedKeyChain) {
214
- const partyId = session.participants.indexOf(copayerId);
215
- const result = await storage.storeTssKeyShare({ id, partyId, encryptedKeyChain });
215
+ /**
216
+ * 2026-03-10:
217
+ * Removing this feature until we decide to support keychain backup to our DB (probably never).
218
+ * If we ever decide to do this, we may need to add support in BWC for restoring it.
219
+ */
216
220
 
217
- if (!result.result.ok) {
218
- logger.error('Failed to store TSS key generation public key %o %o', id, result);
219
- throw Errors.TSS_GENERIC_ERROR.withMessage('Failed to store TSS key generation public key');
220
- }
221
- }
221
+ // if (encryptedKeyChain) {
222
+ // const partyId = session.participants.indexOf(copayerId);
223
+ // const result = await storage.storeTssKeyShare({ id, partyId, encryptedKeyChain });
224
+
225
+ // if (!result.result.ok) {
226
+ // logger.error('Failed to store TSS key generation public key %o %o', id, result);
227
+ // throw Errors.TSS_GENERIC_ERROR.withMessage('Failed to store TSS key generation public key');
228
+ // }
229
+ // }
222
230
  }
223
231
 
224
232
  async storeBwsJoinSecret(params: {