@dynamic-labs-wallet/browser 1.0.80 → 1.0.82
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/index.cjs +284 -212
- package/index.esm.js +285 -213
- package/package.json +3 -3
- package/src/backup/password/errors.d.ts +5 -1
- package/src/backup/password/errors.d.ts.map +1 -1
- package/src/client.d.ts.map +1 -1
- package/src/collectDynamicKeyShareIds.d.ts +3 -0
- package/src/collectDynamicKeyShareIds.d.ts.map +1 -0
- package/src/services/signedSession.d.ts +8 -6
- package/src/services/signedSession.d.ts.map +1 -1
package/index.cjs
CHANGED
|
@@ -7,8 +7,8 @@ var primitives = require('@dynamic-labs-wallet/primitives');
|
|
|
7
7
|
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
|
|
8
8
|
var loadArgon2idWasm = require('argon2id');
|
|
9
9
|
var axios = require('axios');
|
|
10
|
-
var PQueue = require('p-queue');
|
|
11
10
|
var gte = require('semver/functions/gte');
|
|
11
|
+
var PQueue = require('p-queue');
|
|
12
12
|
|
|
13
13
|
function _extends() {
|
|
14
14
|
_extends = Object.assign || function assign(target) {
|
|
@@ -463,6 +463,237 @@ class EncryptionSelfTestFailedError extends Error {
|
|
|
463
463
|
}
|
|
464
464
|
}
|
|
465
465
|
|
|
466
|
+
/**
|
|
467
|
+
* Extracts the HTTP status from an `AxiosError`, or `undefined` for non-HTTP
|
|
468
|
+
* errors. Lives in its own leaf module (no `#internal/web` / WASM imports) so
|
|
469
|
+
* lightweight consumers like `services/signedSession.ts` can use it without
|
|
470
|
+
* pulling the MPC WASM bundle that `utils.ts` loads.
|
|
471
|
+
*/ const getHttpStatus = (error)=>{
|
|
472
|
+
var _error_response;
|
|
473
|
+
return error instanceof axios.AxiosError ? (_error_response = error.response) == null ? void 0 : _error_response.status : undefined;
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
const STORAGE_KEY = 'dynamic-waas-wallet-client';
|
|
477
|
+
const BACKUP_FILENAME = 'dynamicWalletKeyShareBackup.json';
|
|
478
|
+
const CLIENT_KEYSHARE_EXPORT_FILENAME_PREFIX = 'dynamicWalletKeyShareBackup';
|
|
479
|
+
const SIGNED_SESSION_ID_MIN_VERSION = '4.25.4';
|
|
480
|
+
// Namespace-specific version requirements
|
|
481
|
+
const SIGNED_SESSION_ID_MIN_VERSION_BY_NAMESPACE = {
|
|
482
|
+
WalletKit: '4.25.4',
|
|
483
|
+
ClientSDK: '0.1.0-alpha.0'
|
|
484
|
+
};
|
|
485
|
+
// Minimum SDK version that forwards `getSignedSessionId` to the iframe's postMessage
|
|
486
|
+
// reverse channel, distinct from SIGNED_SESSION_ID_MIN_VERSION above (which only
|
|
487
|
+
// gates whether a signed session value is sent at all).
|
|
488
|
+
// WalletKit: dynamic-auth#10577 (v4.74.0). ClientSDK: dynamic-js-sdk#1937 (v1.12.1).
|
|
489
|
+
const SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION = '4.74.0';
|
|
490
|
+
const SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION_BY_NAMESPACE = {
|
|
491
|
+
WalletKit: SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION,
|
|
492
|
+
ClientSDK: '1.12.1'
|
|
493
|
+
};
|
|
494
|
+
const ROOM_EXPIRATION_TIME = 1000 * 60 * 10; // 10 minutes
|
|
495
|
+
const ROOM_CACHE_COUNT = 5;
|
|
496
|
+
const ENVIRONMENT_SETTINGS_STORAGE_KEY = 'dynamic_environment_settings';
|
|
497
|
+
|
|
498
|
+
function meetsMinVersion(sdkVersion, { minVersionByNamespace, fallbackMinVersion, label }, logger) {
|
|
499
|
+
if (!sdkVersion) {
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
try {
|
|
503
|
+
const parsedVersion = core.parseNamespacedVersion(sdkVersion);
|
|
504
|
+
if (!parsedVersion) {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
const { namespace, version } = parsedVersion;
|
|
508
|
+
var _minVersionByNamespace_namespace;
|
|
509
|
+
return gte(version, (_minVersionByNamespace_namespace = minVersionByNamespace[namespace]) != null ? _minVersionByNamespace_namespace : fallbackMinVersion);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
logger.warn(`[DynamicWaasWalletClient] Error checking ${label} for version ${sdkVersion}`, {
|
|
512
|
+
error: error instanceof Error ? error.message : String(error)
|
|
513
|
+
});
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
/** Whether the given SDK version requires a signed session ID to be sent at all. */ function isRequiresSignedSessionId(sdkVersion, logger) {
|
|
518
|
+
return meetsMinVersion(sdkVersion, {
|
|
519
|
+
minVersionByNamespace: SIGNED_SESSION_ID_MIN_VERSION_BY_NAMESPACE,
|
|
520
|
+
fallbackMinVersion: SIGNED_SESSION_ID_MIN_VERSION,
|
|
521
|
+
label: 'requiresSignedSessionId'
|
|
522
|
+
}, logger);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Whether the SDK version is known to forward `getSignedSessionId` to the iframe's
|
|
526
|
+
* postMessage reverse channel — stricter than {@link isRequiresSignedSessionId},
|
|
527
|
+
* which only checks whether a session value is sent at all.
|
|
528
|
+
*/ function isReverseChannelSupported(sdkVersion, logger) {
|
|
529
|
+
return meetsMinVersion(sdkVersion, {
|
|
530
|
+
minVersionByNamespace: SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION_BY_NAMESPACE,
|
|
531
|
+
fallbackMinVersion: SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION,
|
|
532
|
+
label: 'supportsReverseChannel'
|
|
533
|
+
}, logger);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Datadog event name emitted on every signed-session nonce rejection (a 400 or
|
|
538
|
+
* 401 from the backup/recovery endpoints). Log-only: lets us split the failure
|
|
539
|
+
* bucket by `reason` and measure the true per-wallet unrecoverable rate
|
|
540
|
+
* without changing any retry behavior. See DYNT-1599.
|
|
541
|
+
*/ const NONCE_FAILURE_EVENT = 'waas.signed_session.nonce_failure';
|
|
542
|
+
/**
|
|
543
|
+
* Reasons that represent a genuine session-key signature mismatch (the 401 the
|
|
544
|
+
* server returns per DYNT-1282). Used to gate 401 instrumentation so callers
|
|
545
|
+
* that reuse 401 for unrelated rejections don't pollute the failure metric.
|
|
546
|
+
*/ const SESSION_SIGNATURE_FAILURE_REASONS = new Set([
|
|
547
|
+
'invalid_nonce_signature',
|
|
548
|
+
'invalid_session_signature'
|
|
549
|
+
]);
|
|
550
|
+
/** Best-effort extraction of the server error `code`/`message` from a 4xx body. */ const readErrorBody = (error)=>{
|
|
551
|
+
var _error_response;
|
|
552
|
+
if (!(error instanceof axios.AxiosError)) {
|
|
553
|
+
return {};
|
|
554
|
+
}
|
|
555
|
+
const data = (_error_response = error.response) == null ? void 0 : _error_response.data;
|
|
556
|
+
if (typeof data === 'string') {
|
|
557
|
+
return {
|
|
558
|
+
message: data
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
if (!data || typeof data !== 'object') {
|
|
562
|
+
return {};
|
|
563
|
+
}
|
|
564
|
+
const record = data;
|
|
565
|
+
// Prefer a string `code`, else fall back to `error_code` (the field the Key
|
|
566
|
+
// Share Service emits, see KSS #271) or a bare `error`.
|
|
567
|
+
const code = [
|
|
568
|
+
record['code'],
|
|
569
|
+
record['error_code'],
|
|
570
|
+
record['error']
|
|
571
|
+
].find((value)=>typeof value === 'string');
|
|
572
|
+
const message = typeof record['message'] === 'string' ? record['message'] : undefined;
|
|
573
|
+
return {
|
|
574
|
+
code,
|
|
575
|
+
message
|
|
576
|
+
};
|
|
577
|
+
};
|
|
578
|
+
/** Maps the server error `code`/`message` onto a {@link NonceFailureReason}. */ const classifyNonceFailure = ({ code, message })=>{
|
|
579
|
+
const haystack = `${code != null ? code : ''} ${message != null ? message : ''}`.toLowerCase();
|
|
580
|
+
if (haystack.includes('already_used') || haystack.includes('already used') || haystack.includes('consumption failed')) {
|
|
581
|
+
return 'nonce_already_used';
|
|
582
|
+
}
|
|
583
|
+
if (haystack.includes('invalid_nonce_signature') || haystack.includes('invalid nonce signature')) {
|
|
584
|
+
return 'invalid_nonce_signature';
|
|
585
|
+
}
|
|
586
|
+
if (haystack.includes('invalid_session_signature') || haystack.includes('invalid session public key') || haystack.includes('invalid session signature')) {
|
|
587
|
+
return 'invalid_session_signature';
|
|
588
|
+
}
|
|
589
|
+
return 'other';
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
592
|
+
* Owns everything about signed sessions and their single-use replay nonces:
|
|
593
|
+
* resolving a session from an explicit value or the host reverse channel,
|
|
594
|
+
* deciding whether the SDK version requires one, and building the
|
|
595
|
+
* refresh-on-replay-400 retry predicate shared by the backup and recovery
|
|
596
|
+
* flows.
|
|
597
|
+
*
|
|
598
|
+
* Extracted from `DynamicWalletClient` so this logic can be exercised in
|
|
599
|
+
* isolation. The host callback is read lazily (`getCallback`) so it stays in
|
|
600
|
+
* sync with the owning client even if reassigned after construction.
|
|
601
|
+
*/ class SignedSessionManager {
|
|
602
|
+
/** True when a reverse channel is configured to mint fresh signed sessions. */ get canRefresh() {
|
|
603
|
+
return this.getCallback() !== undefined;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Resolves the signed session ID from an explicit value or falls back to the
|
|
607
|
+
* host reverse channel. Throws if neither source provides a value.
|
|
608
|
+
*/ async resolve(signedSessionId) {
|
|
609
|
+
const callback = this.getCallback();
|
|
610
|
+
const resolved = signedSessionId != null ? signedSessionId : await (callback == null ? void 0 : callback());
|
|
611
|
+
if (!resolved) {
|
|
612
|
+
throw new Error(signedSessionId === undefined && callback ? 'signedSessionId callback returned an invalid value' : 'signedSessionId is required for backup but was not provided and no callback is configured');
|
|
613
|
+
}
|
|
614
|
+
return resolved;
|
|
615
|
+
}
|
|
616
|
+
/** Whether the configured SDK version requires a signed session ID. */ isRequired() {
|
|
617
|
+
return this.required;
|
|
618
|
+
}
|
|
619
|
+
/** Whether the host SDK version supports the getSignedSessionId reverse channel (see version-check.ts). */ supportsReverseChannel() {
|
|
620
|
+
return this.reverseChannelSupported;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Builds a `retryPromise` `shouldRetry` predicate that refreshes a consumed
|
|
624
|
+
* single-use nonce via the reverse channel on a replay 400 and retries once.
|
|
625
|
+
* The signed session carries a single-use replay nonce: it is valid on its
|
|
626
|
+
* first use, but once consumed (e.g. by a preceding operation) the server
|
|
627
|
+
* rejects with a 400, at which point a fresh session is fetched and the call
|
|
628
|
+
* retried. `alsoRetry` lets callers opt into additional retryable statuses
|
|
629
|
+
* (e.g. 429 / 5xx) on top of the nonce refresh.
|
|
630
|
+
*/ refreshShouldRetry({ onRefreshed, operationName, logContext, alsoRetry }) {
|
|
631
|
+
return async (error, attempt)=>{
|
|
632
|
+
const status = getHttpStatus(error);
|
|
633
|
+
// A 400 is a refreshable replay nonce; a 401 is a session-key mismatch the
|
|
634
|
+
// server returns to break the refresh loop (DYNT-1282) — logged only when the
|
|
635
|
+
// body classifies as a genuine session-signature failure so unrelated 401s
|
|
636
|
+
// (e.g. an exhausted add-signer grant) don't pollute the DYNT-1599 metric.
|
|
637
|
+
// Only the 400 can actually refresh.
|
|
638
|
+
const isSessionSignatureFailure = status === 401 && SESSION_SIGNATURE_FAILURE_REASONS.has(classifyNonceFailure(readErrorBody(error)));
|
|
639
|
+
const shouldInstrument = status === 400 || isSessionSignatureFailure;
|
|
640
|
+
if (shouldInstrument) {
|
|
641
|
+
const willRefresh = status === 400 && attempt === 1 && this.canRefresh && this.supportsReverseChannel();
|
|
642
|
+
this.instrumentNonceFailure({
|
|
643
|
+
error,
|
|
644
|
+
attempt,
|
|
645
|
+
status,
|
|
646
|
+
operationName,
|
|
647
|
+
willRefresh,
|
|
648
|
+
logContext
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
if (status === 400 && attempt === 1 && this.canRefresh) {
|
|
652
|
+
// canRefresh only proves a local callback exists, not that the host answers it.
|
|
653
|
+
if (!this.supportsReverseChannel()) {
|
|
654
|
+
this.logger.warn(`[${operationName}] host SDK version predates the signed-session reverse channel, declining nonce refresh`, _extends({
|
|
655
|
+
status
|
|
656
|
+
}, logContext));
|
|
657
|
+
return alsoRetry ? alsoRetry(status) : false;
|
|
658
|
+
}
|
|
659
|
+
onRefreshed(await this.resolve());
|
|
660
|
+
this.logger.info(`[${operationName}] refreshed signed session, retrying`, _extends({
|
|
661
|
+
status
|
|
662
|
+
}, logContext));
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
return alsoRetry ? alsoRetry(status) : false;
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Emits a single Datadog log per signed-session nonce rejection so the
|
|
670
|
+
* failure bucket can be split by `reason` and correlated per wallet/request.
|
|
671
|
+
* Purely observational — it never alters the retry decision.
|
|
672
|
+
*/ instrumentNonceFailure({ error, attempt, status, operationName, willRefresh, logContext }) {
|
|
673
|
+
const body = readErrorBody(error);
|
|
674
|
+
this.logger.info(NONCE_FAILURE_EVENT, _extends({}, logContext, {
|
|
675
|
+
operationName,
|
|
676
|
+
attempt,
|
|
677
|
+
status,
|
|
678
|
+
reason: classifyNonceFailure(body),
|
|
679
|
+
errorCode: body.code,
|
|
680
|
+
errorMessage: body.message,
|
|
681
|
+
willRefresh,
|
|
682
|
+
canRefresh: this.canRefresh,
|
|
683
|
+
reverseChannelSupported: this.reverseChannelSupported,
|
|
684
|
+
required: this.required,
|
|
685
|
+
sdkVersion: this.sdkVersion
|
|
686
|
+
}));
|
|
687
|
+
}
|
|
688
|
+
constructor({ logger, sdkVersion, getCallback }){
|
|
689
|
+
this.logger = logger;
|
|
690
|
+
this.getCallback = getCallback;
|
|
691
|
+
this.sdkVersion = sdkVersion;
|
|
692
|
+
this.required = isRequiresSignedSessionId(sdkVersion, this.logger);
|
|
693
|
+
this.reverseChannelSupported = isReverseChannelSupported(sdkVersion, this.logger);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
466
697
|
const ERROR_NO_KEY_SHARES_BACKED_UP = 'No key shares were backed up to Dynamic backend';
|
|
467
698
|
const ERROR_KEYGEN_FAILED = '[DynamicWaasWalletClient]: Error with keygen';
|
|
468
699
|
const ERROR_CREATE_WALLET_ACCOUNT = '[DynamicWaasWalletClient]: Error creating wallet account';
|
|
@@ -555,6 +786,10 @@ const ERROR_WALLETS_ALREADY_ENCRYPTED = '[DynamicWaasWalletClient]: Cannot set p
|
|
|
555
786
|
*/ const USER_ACTIONABLE_PASSWORD_BACKUP_ERROR_REASONS = {
|
|
556
787
|
wrong_password: true,
|
|
557
788
|
stale_local_shares: false,
|
|
789
|
+
nonce_already_used: false,
|
|
790
|
+
invalid_nonce_signature: false,
|
|
791
|
+
invalid_session_signature: false,
|
|
792
|
+
signed_session: false,
|
|
558
793
|
encryption_failure: false,
|
|
559
794
|
decryption_failure: false,
|
|
560
795
|
upload_failure: false,
|
|
@@ -577,6 +812,30 @@ const FETCH_NETWORK_ERROR_REGEX = /failed to fetch|networkerror|load failed|netw
|
|
|
577
812
|
const ENCRYPTION_FAILURE_MESSAGE_PREFIX = 'Error encrypting data:';
|
|
578
813
|
const DECRYPTION_FAILURE_MESSAGE_PREFIX = 'Decryption failed:';
|
|
579
814
|
const isStaleSharesError = (error)=>error instanceof StaleLocalSharesError || error instanceof Error && error.name === 'StaleLocalSharesError';
|
|
815
|
+
// The backup/recovery endpoints reject the chaining signature with a 400
|
|
816
|
+
// (stale signed session / replay nonce) or 401 (session-key mismatch,
|
|
817
|
+
// DYNT-1282); the preserved backend code splits those into their precise
|
|
818
|
+
// cause so the failure is actionable instead of a generic `unknown`.
|
|
819
|
+
const SIGNED_SESSION_FAILURE_STATUSES = new Set([
|
|
820
|
+
400,
|
|
821
|
+
401
|
|
822
|
+
]);
|
|
823
|
+
const NONCE_REASON_TO_BACKUP_REASON = {
|
|
824
|
+
nonce_already_used: 'nonce_already_used',
|
|
825
|
+
invalid_nonce_signature: 'invalid_nonce_signature',
|
|
826
|
+
invalid_session_signature: 'invalid_session_signature',
|
|
827
|
+
other: 'signed_session'
|
|
828
|
+
};
|
|
829
|
+
const classifyWalletApiError = (error)=>{
|
|
830
|
+
if (!SIGNED_SESSION_FAILURE_STATUSES.has(error.status)) {
|
|
831
|
+
return 'upload_failure';
|
|
832
|
+
}
|
|
833
|
+
const nonceReason = classifyNonceFailure({
|
|
834
|
+
code: error.code,
|
|
835
|
+
message: error.message
|
|
836
|
+
});
|
|
837
|
+
return NONCE_REASON_TO_BACKUP_REASON[nonceReason];
|
|
838
|
+
};
|
|
580
839
|
const isNetworkLikeError = (error)=>{
|
|
581
840
|
if (typeof error !== 'object' || error === null) {
|
|
582
841
|
return false;
|
|
@@ -628,6 +887,12 @@ const computeReason = (error)=>{
|
|
|
628
887
|
if (isStaleSharesError(error)) {
|
|
629
888
|
return 'stale_local_shares';
|
|
630
889
|
}
|
|
890
|
+
// WalletApiError is a wrapped HTTP failure carrying the resolved status; its
|
|
891
|
+
// message is a generic status string ('Invalid request', etc.) so classify
|
|
892
|
+
// by status ahead of the message-based checks below.
|
|
893
|
+
if (error instanceof core.WalletApiError) {
|
|
894
|
+
return classifyWalletApiError(error);
|
|
895
|
+
}
|
|
631
896
|
if (error instanceof Error) {
|
|
632
897
|
if (error.message.includes(ERROR_PASSWORD_MISMATCH) || error.message.includes(ERROR_INCORRECT_PASSWORD)) {
|
|
633
898
|
return 'wrong_password';
|
|
@@ -995,38 +1260,6 @@ const downloadFileFromGoogleDrive = async ({ accessToken, fileName })=>{
|
|
|
995
1260
|
}
|
|
996
1261
|
};
|
|
997
1262
|
|
|
998
|
-
/**
|
|
999
|
-
* Extracts the HTTP status from an `AxiosError`, or `undefined` for non-HTTP
|
|
1000
|
-
* errors. Lives in its own leaf module (no `#internal/web` / WASM imports) so
|
|
1001
|
-
* lightweight consumers like `services/signedSession.ts` can use it without
|
|
1002
|
-
* pulling the MPC WASM bundle that `utils.ts` loads.
|
|
1003
|
-
*/ const getHttpStatus = (error)=>{
|
|
1004
|
-
var _error_response;
|
|
1005
|
-
return error instanceof axios.AxiosError ? (_error_response = error.response) == null ? void 0 : _error_response.status : undefined;
|
|
1006
|
-
};
|
|
1007
|
-
|
|
1008
|
-
const STORAGE_KEY = 'dynamic-waas-wallet-client';
|
|
1009
|
-
const BACKUP_FILENAME = 'dynamicWalletKeyShareBackup.json';
|
|
1010
|
-
const CLIENT_KEYSHARE_EXPORT_FILENAME_PREFIX = 'dynamicWalletKeyShareBackup';
|
|
1011
|
-
const SIGNED_SESSION_ID_MIN_VERSION = '4.25.4';
|
|
1012
|
-
// Namespace-specific version requirements
|
|
1013
|
-
const SIGNED_SESSION_ID_MIN_VERSION_BY_NAMESPACE = {
|
|
1014
|
-
WalletKit: '4.25.4',
|
|
1015
|
-
ClientSDK: '0.1.0-alpha.0'
|
|
1016
|
-
};
|
|
1017
|
-
// Minimum SDK version that forwards `getSignedSessionId` to the iframe's postMessage
|
|
1018
|
-
// reverse channel, distinct from SIGNED_SESSION_ID_MIN_VERSION above (which only
|
|
1019
|
-
// gates whether a signed session value is sent at all).
|
|
1020
|
-
// WalletKit: dynamic-auth#10577 (v4.74.0). ClientSDK: dynamic-js-sdk#1937 (v1.12.1).
|
|
1021
|
-
const SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION = '4.74.0';
|
|
1022
|
-
const SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION_BY_NAMESPACE = {
|
|
1023
|
-
WalletKit: SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION,
|
|
1024
|
-
ClientSDK: '1.12.1'
|
|
1025
|
-
};
|
|
1026
|
-
const ROOM_EXPIRATION_TIME = 1000 * 60 * 10; // 10 minutes
|
|
1027
|
-
const ROOM_CACHE_COUNT = 5;
|
|
1028
|
-
const ENVIRONMENT_SETTINGS_STORAGE_KEY = 'dynamic_environment_settings';
|
|
1029
|
-
|
|
1030
1263
|
/**
|
|
1031
1264
|
* Normalizes an address to lowercase for consistent map key lookups.
|
|
1032
1265
|
* This ensures that addresses with different casing (e.g., EIP-55 checksummed vs lowercase)
|
|
@@ -2412,6 +2645,22 @@ const createDynamicOnlyDistribution = ({ allShares })=>({
|
|
|
2412
2645
|
/** Track which wallets are currently executing inside a sign operation.
|
|
2413
2646
|
* Used to prevent deadlocks when recovery is called from within a sign op. */ WalletQueueManager.walletsWithActiveSignOp = new Map();
|
|
2414
2647
|
|
|
2648
|
+
// A DYNAMIC backup can persist multiple shares (e.g. 2-of-3); the singular
|
|
2649
|
+
// externalKeyShareId is a fallback for older single-id shapes.
|
|
2650
|
+
const collectDynamicKeyShareIds = (locations)=>{
|
|
2651
|
+
return locations.filter((loc)=>loc.location === core.BackupLocation.DYNAMIC).flatMap((loc)=>{
|
|
2652
|
+
if (loc.externalKeyShareIds && loc.externalKeyShareIds.length > 0) {
|
|
2653
|
+
return loc.externalKeyShareIds;
|
|
2654
|
+
}
|
|
2655
|
+
if (loc.externalKeyShareId) {
|
|
2656
|
+
return [
|
|
2657
|
+
loc.externalKeyShareId
|
|
2658
|
+
];
|
|
2659
|
+
}
|
|
2660
|
+
return [];
|
|
2661
|
+
});
|
|
2662
|
+
};
|
|
2663
|
+
|
|
2415
2664
|
// Pure routing helper: maps a completed reshare ceremony's results into the
|
|
2416
2665
|
// ShareDistribution that storage/publish should follow. Lives outside the
|
|
2417
2666
|
// client class so it's straightforward to reason about and unit-test
|
|
@@ -2612,184 +2861,6 @@ const readEnvironmentSettings = ()=>{
|
|
|
2612
2861
|
}
|
|
2613
2862
|
});
|
|
2614
2863
|
|
|
2615
|
-
function meetsMinVersion(sdkVersion, { minVersionByNamespace, fallbackMinVersion, label }, logger) {
|
|
2616
|
-
if (!sdkVersion) {
|
|
2617
|
-
return false;
|
|
2618
|
-
}
|
|
2619
|
-
try {
|
|
2620
|
-
const parsedVersion = core.parseNamespacedVersion(sdkVersion);
|
|
2621
|
-
if (!parsedVersion) {
|
|
2622
|
-
return false;
|
|
2623
|
-
}
|
|
2624
|
-
const { namespace, version } = parsedVersion;
|
|
2625
|
-
var _minVersionByNamespace_namespace;
|
|
2626
|
-
return gte(version, (_minVersionByNamespace_namespace = minVersionByNamespace[namespace]) != null ? _minVersionByNamespace_namespace : fallbackMinVersion);
|
|
2627
|
-
} catch (error) {
|
|
2628
|
-
logger.warn(`[DynamicWaasWalletClient] Error checking ${label} for version ${sdkVersion}`, {
|
|
2629
|
-
error: error instanceof Error ? error.message : String(error)
|
|
2630
|
-
});
|
|
2631
|
-
return false;
|
|
2632
|
-
}
|
|
2633
|
-
}
|
|
2634
|
-
/** Whether the given SDK version requires a signed session ID to be sent at all. */ function isRequiresSignedSessionId(sdkVersion, logger) {
|
|
2635
|
-
return meetsMinVersion(sdkVersion, {
|
|
2636
|
-
minVersionByNamespace: SIGNED_SESSION_ID_MIN_VERSION_BY_NAMESPACE,
|
|
2637
|
-
fallbackMinVersion: SIGNED_SESSION_ID_MIN_VERSION,
|
|
2638
|
-
label: 'requiresSignedSessionId'
|
|
2639
|
-
}, logger);
|
|
2640
|
-
}
|
|
2641
|
-
/**
|
|
2642
|
-
* Whether the SDK version is known to forward `getSignedSessionId` to the iframe's
|
|
2643
|
-
* postMessage reverse channel — stricter than {@link isRequiresSignedSessionId},
|
|
2644
|
-
* which only checks whether a session value is sent at all.
|
|
2645
|
-
*/ function isReverseChannelSupported(sdkVersion, logger) {
|
|
2646
|
-
return meetsMinVersion(sdkVersion, {
|
|
2647
|
-
minVersionByNamespace: SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION_BY_NAMESPACE,
|
|
2648
|
-
fallbackMinVersion: SIGNED_SESSION_REVERSE_CHANNEL_MIN_VERSION,
|
|
2649
|
-
label: 'supportsReverseChannel'
|
|
2650
|
-
}, logger);
|
|
2651
|
-
}
|
|
2652
|
-
|
|
2653
|
-
/**
|
|
2654
|
-
* Datadog event name emitted on every signed-session nonce rejection (a 400
|
|
2655
|
-
* from the backup/recovery endpoints). Log-only: lets us split the failure
|
|
2656
|
-
* bucket by `reason` and measure the true per-wallet unrecoverable rate
|
|
2657
|
-
* without changing any retry behavior. See DYNT-1599.
|
|
2658
|
-
*/ const NONCE_FAILURE_EVENT = 'waas.signed_session.nonce_failure';
|
|
2659
|
-
/** Best-effort extraction of the server error `code`/`message` from a 400 body. */ const readErrorBody = (error)=>{
|
|
2660
|
-
var _error_response;
|
|
2661
|
-
if (!(error instanceof axios.AxiosError)) {
|
|
2662
|
-
return {};
|
|
2663
|
-
}
|
|
2664
|
-
const data = (_error_response = error.response) == null ? void 0 : _error_response.data;
|
|
2665
|
-
if (typeof data === 'string') {
|
|
2666
|
-
return {
|
|
2667
|
-
message: data
|
|
2668
|
-
};
|
|
2669
|
-
}
|
|
2670
|
-
if (!data || typeof data !== 'object') {
|
|
2671
|
-
return {};
|
|
2672
|
-
}
|
|
2673
|
-
const record = data;
|
|
2674
|
-
// Prefer a string `code`, else fall back to a string `error`.
|
|
2675
|
-
const code = [
|
|
2676
|
-
record['code'],
|
|
2677
|
-
record['error']
|
|
2678
|
-
].find((value)=>typeof value === 'string');
|
|
2679
|
-
const message = typeof record['message'] === 'string' ? record['message'] : undefined;
|
|
2680
|
-
return {
|
|
2681
|
-
code,
|
|
2682
|
-
message
|
|
2683
|
-
};
|
|
2684
|
-
};
|
|
2685
|
-
/** Maps the server error `code`/`message` onto a {@link NonceFailureReason}. */ const classifyNonceFailure = ({ code, message })=>{
|
|
2686
|
-
const haystack = `${code != null ? code : ''} ${message != null ? message : ''}`.toLowerCase();
|
|
2687
|
-
if (haystack.includes('already_used') || haystack.includes('already used') || haystack.includes('consumption failed')) {
|
|
2688
|
-
return 'nonce_already_used';
|
|
2689
|
-
}
|
|
2690
|
-
if (haystack.includes('invalid_nonce_signature') || haystack.includes('invalid nonce signature')) {
|
|
2691
|
-
return 'invalid_nonce_signature';
|
|
2692
|
-
}
|
|
2693
|
-
return 'other';
|
|
2694
|
-
};
|
|
2695
|
-
/**
|
|
2696
|
-
* Owns everything about signed sessions and their single-use replay nonces:
|
|
2697
|
-
* resolving a session from an explicit value or the host reverse channel,
|
|
2698
|
-
* deciding whether the SDK version requires one, and building the
|
|
2699
|
-
* refresh-on-replay-400 retry predicate shared by the backup and recovery
|
|
2700
|
-
* flows.
|
|
2701
|
-
*
|
|
2702
|
-
* Extracted from `DynamicWalletClient` so this logic can be exercised in
|
|
2703
|
-
* isolation. The host callback is read lazily (`getCallback`) so it stays in
|
|
2704
|
-
* sync with the owning client even if reassigned after construction.
|
|
2705
|
-
*/ class SignedSessionManager {
|
|
2706
|
-
/** True when a reverse channel is configured to mint fresh signed sessions. */ get canRefresh() {
|
|
2707
|
-
return this.getCallback() !== undefined;
|
|
2708
|
-
}
|
|
2709
|
-
/**
|
|
2710
|
-
* Resolves the signed session ID from an explicit value or falls back to the
|
|
2711
|
-
* host reverse channel. Throws if neither source provides a value.
|
|
2712
|
-
*/ async resolve(signedSessionId) {
|
|
2713
|
-
const callback = this.getCallback();
|
|
2714
|
-
const resolved = signedSessionId != null ? signedSessionId : await (callback == null ? void 0 : callback());
|
|
2715
|
-
if (!resolved) {
|
|
2716
|
-
throw new Error(signedSessionId === undefined && callback ? 'signedSessionId callback returned an invalid value' : 'signedSessionId is required for backup but was not provided and no callback is configured');
|
|
2717
|
-
}
|
|
2718
|
-
return resolved;
|
|
2719
|
-
}
|
|
2720
|
-
/** Whether the configured SDK version requires a signed session ID. */ isRequired() {
|
|
2721
|
-
return this.required;
|
|
2722
|
-
}
|
|
2723
|
-
/** Whether the host SDK version supports the getSignedSessionId reverse channel (see version-check.ts). */ supportsReverseChannel() {
|
|
2724
|
-
return this.reverseChannelSupported;
|
|
2725
|
-
}
|
|
2726
|
-
/**
|
|
2727
|
-
* Builds a `retryPromise` `shouldRetry` predicate that refreshes a consumed
|
|
2728
|
-
* single-use nonce via the reverse channel on a replay 400 and retries once.
|
|
2729
|
-
* The signed session carries a single-use replay nonce: it is valid on its
|
|
2730
|
-
* first use, but once consumed (e.g. by a preceding operation) the server
|
|
2731
|
-
* rejects with a 400, at which point a fresh session is fetched and the call
|
|
2732
|
-
* retried. `alsoRetry` lets callers opt into additional retryable statuses
|
|
2733
|
-
* (e.g. 429 / 5xx) on top of the nonce refresh.
|
|
2734
|
-
*/ refreshShouldRetry({ onRefreshed, operationName, logContext, alsoRetry }) {
|
|
2735
|
-
return async (error, attempt)=>{
|
|
2736
|
-
const status = getHttpStatus(error);
|
|
2737
|
-
if (status === 400) {
|
|
2738
|
-
const willRefresh = attempt === 1 && this.canRefresh && this.supportsReverseChannel();
|
|
2739
|
-
this.instrumentNonceFailure({
|
|
2740
|
-
error,
|
|
2741
|
-
attempt,
|
|
2742
|
-
operationName,
|
|
2743
|
-
willRefresh,
|
|
2744
|
-
logContext
|
|
2745
|
-
});
|
|
2746
|
-
if (attempt === 1 && this.canRefresh) {
|
|
2747
|
-
// canRefresh only proves a local callback exists, not that the host answers it.
|
|
2748
|
-
if (!this.supportsReverseChannel()) {
|
|
2749
|
-
this.logger.warn(`[${operationName}] host SDK version predates the signed-session reverse channel, declining nonce refresh`, _extends({
|
|
2750
|
-
status
|
|
2751
|
-
}, logContext));
|
|
2752
|
-
return alsoRetry ? alsoRetry(status) : false;
|
|
2753
|
-
}
|
|
2754
|
-
onRefreshed(await this.resolve());
|
|
2755
|
-
this.logger.info(`[${operationName}] refreshed signed session, retrying`, _extends({
|
|
2756
|
-
status
|
|
2757
|
-
}, logContext));
|
|
2758
|
-
return true;
|
|
2759
|
-
}
|
|
2760
|
-
}
|
|
2761
|
-
return alsoRetry ? alsoRetry(status) : false;
|
|
2762
|
-
};
|
|
2763
|
-
}
|
|
2764
|
-
/**
|
|
2765
|
-
* Emits a single Datadog log per signed-session nonce rejection so the
|
|
2766
|
-
* failure bucket can be split by `reason` and correlated per wallet/request.
|
|
2767
|
-
* Purely observational — it never alters the retry decision.
|
|
2768
|
-
*/ instrumentNonceFailure({ error, attempt, operationName, willRefresh, logContext }) {
|
|
2769
|
-
const body = readErrorBody(error);
|
|
2770
|
-
this.logger.info(NONCE_FAILURE_EVENT, _extends({}, logContext, {
|
|
2771
|
-
operationName,
|
|
2772
|
-
attempt,
|
|
2773
|
-
status: 400,
|
|
2774
|
-
reason: classifyNonceFailure(body),
|
|
2775
|
-
errorCode: body.code,
|
|
2776
|
-
errorMessage: body.message,
|
|
2777
|
-
willRefresh,
|
|
2778
|
-
canRefresh: this.canRefresh,
|
|
2779
|
-
reverseChannelSupported: this.reverseChannelSupported,
|
|
2780
|
-
required: this.required,
|
|
2781
|
-
sdkVersion: this.sdkVersion
|
|
2782
|
-
}));
|
|
2783
|
-
}
|
|
2784
|
-
constructor({ logger, sdkVersion, getCallback }){
|
|
2785
|
-
this.logger = logger;
|
|
2786
|
-
this.getCallback = getCallback;
|
|
2787
|
-
this.sdkVersion = sdkVersion;
|
|
2788
|
-
this.required = isRequiresSignedSessionId(sdkVersion, this.logger);
|
|
2789
|
-
this.reverseChannelSupported = isReverseChannelSupported(sdkVersion, this.logger);
|
|
2790
|
-
}
|
|
2791
|
-
}
|
|
2792
|
-
|
|
2793
2864
|
/**
|
|
2794
2865
|
* Determines the recovery state of a wallet based on backup info and local shares.
|
|
2795
2866
|
*
|
|
@@ -6351,6 +6422,7 @@ class DynamicWalletClient {
|
|
|
6351
6422
|
return {
|
|
6352
6423
|
location: core.BackupLocation.DYNAMIC,
|
|
6353
6424
|
externalKeyShareId: data.keyShareIds[0],
|
|
6425
|
+
externalKeyShareIds: data.keyShareIds,
|
|
6354
6426
|
passwordEncrypted: isPasswordEncrypted,
|
|
6355
6427
|
keygenId
|
|
6356
6428
|
};
|
|
@@ -6647,7 +6719,7 @@ class DynamicWalletClient {
|
|
|
6647
6719
|
// aborts the backup before activation, so an unrecoverable share is never
|
|
6648
6720
|
// marked active; the local share is never touched.
|
|
6649
6721
|
if (this.featureFlags[core.FEATURE_FLAGS.VERIFY_BACKUP_RECOVERABILITY_FLAG] === true) {
|
|
6650
|
-
const dynamicKeyShareIds = locations
|
|
6722
|
+
const dynamicKeyShareIds = collectDynamicKeyShareIds(locations);
|
|
6651
6723
|
await this.assertServerCopyRecoverable({
|
|
6652
6724
|
walletId: walletData.walletId,
|
|
6653
6725
|
shareSetId: targetShareSetId,
|