import { L as CHAINS_INFO_MAP, St as getCore, T as getWalletProviderFromWalletAccount, c as isWaasWalletProvider, d as DYNAMIC_WAAS_METADATA, f as getWalletProviders, ht as CLIENT_SDK_NAME, k as NoWalletProviderFoundError, l as DEFAULT_WAAS_BASE_API_URL, lt as isCookieEnabled, u as DEFAULT_WAAS_BASE_MPC_RELAY_API_URL, v as __logoutWithReason_wrapped, vt as getDefaultClient, xt as BaseError } from "./updateAuthFromVerifyResponse-BEoc7f0m.esm.js";
import { n as NotWaasWalletAccountError, r as __refreshAuth_wrapped } from "./isDynamicWaasEnabled-DwY38pjA.esm.js";
import { t as getSignedSessionId } from "./getSignedSessionId-C4igTuKk.esm.js";
import { DynamicWalletClient } from "@dynamic-labs-wallet/browser-wallet-client";

//#region src/errors/NotWaasWalletProviderError.ts
var NotWaasWalletProviderError = class extends BaseError {
	constructor({ walletProviderKey }) {
		super({
			cause: null,
			code: "not_waas_wallet_provider_error",
			docsUrl: null,
			name: "NotWaasWalletProviderError",
			shortMessage: `Wallet provider ${walletProviderKey} is not a Dynamic WaaS wallet provider`
		});
	}
};

//#endregion
//#region src/modules/waas/createWaasClient/createWaasClient.ts
/** @notInstrumented */
const createWaasClient = ({ chainName }, client) => {
	const core = getCore(client);
	const additionalTrustedOrigins = core.waas?.additionalTrustedOrigins;
	const baseClientKeysharesRelayApiUrl = client.projectSettings?.sdk.waas?.customKeyshareRelayBaseUrl;
	return new DynamicWalletClient({
		authMode: isCookieEnabled(client) ? "cookie" : "header",
		authToken: client.token || "",
		baseApiUrl: (core.apiBaseUrl || DEFAULT_WAAS_BASE_API_URL).replace("/api/v0", ""),
		baseMPCRelayApiUrl: client.projectSettings?.sdk.waas?.relayUrl || DEFAULT_WAAS_BASE_MPC_RELAY_API_URL,
		chainName,
		eagerlyRecoverKeyShares: true,
		environmentId: core.environmentId,
		sdkVersion: `${CLIENT_SDK_NAME}/${core.version}`,
		...additionalTrustedOrigins == void 0 ? {} : { additionalTrustedOrigins },
		...baseClientKeysharesRelayApiUrl == void 0 ? {} : { baseClientKeysharesRelayApiUrl },
		...core.waas?.loadTimeoutMs == void 0 ? {} : { iframeLoadTimeout: core.waas.loadTimeoutMs },
		...core.waas?.maxLoadRetries == void 0 ? {} : { maxRetryAttempts: core.waas.maxLoadRetries }
	}, {
		getSignedSessionId: () => getSignedSessionId(client).then(({ signature }) => signature),
		onUnauthorized: () => {
			__logoutWithReason_wrapped({ reason: "session-refresh-unauthorized" }, client).catch((error) => {
				core.logger.error("Failed to log out after WaaS unauthorized response", error);
			});
		}
	});
};

//#endregion
//#region src/modules/waas/getWaasChainNameFromChain/getWaasChainNameFromChain.ts
/** @notInstrumented */
const getWaasChainNameFromChain = (chain) => {
	return CHAINS_INFO_MAP[chain].waasChainNameOverride || chain;
};

//#endregion
//#region src/modules/waas/getWaasWalletProviderFromWalletAccount/getWaasWalletProviderFromWalletAccount.ts
/** @notInstrumented */
const getWaasWalletProviderFromWalletAccount = ({ walletAccount }, client) => {
	const walletProvider = getWalletProviderFromWalletAccount({ walletAccount }, client);
	if (!isWaasWalletProvider(walletProvider)) throw new NotWaasWalletAccountError({ walletAddress: walletAccount.address });
	return walletProvider;
};

//#endregion
//#region src/modules/waas/migrateFromFireblocks/migrateFromFireblocks.ts
/**
* Migrates an existing Fireblocks NCW wallet into Dynamic's WaaS infrastructure.
*
* Migration is a single cross-chain operation: one call takes over the Fireblocks
* key material and provisions Dynamic WaaS wallet accounts for every supported
* chain. Because it is not per-chain, this resolves a single WaaS wallet provider
* (any registered chain) and delegates the takeover to it.
*
* @param params.deviceId - The Fireblocks NCW device id being migrated.
* @param params.jwt - The Fireblocks-issued JWT authorizing the takeover.
* @param params.storage - The serialized Fireblocks NCW device storage.
* @param params.password - The password that encrypted the legacy device storage.
* @param params.expectedAddresses - Required per-chain expected address; its keys are the chains migration runs on, and each imported address is verified against it.
* @param [params.coinTypes] - Optional SLIP-0044 coin types per chain name.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise resolving to the per-chain migration results.
* @notInstrumented
*/
const migrateFromFireblocks = async ({ coinTypes, deviceId, expectedAddresses, jwt, password, storage }, client = getDefaultClient()) => {
	const providers = getWalletProviders(client);
	if (providers.length === 0) throw new NoWalletProviderFoundError({ walletProviderKey: DYNAMIC_WAAS_METADATA.displayName });
	const waasProvider = providers.find(isWaasWalletProvider);
	if (!waasProvider) throw new NotWaasWalletProviderError({ walletProviderKey: providers[0].key });
	const result = await waasProvider.migrateFromFireblocks({
		coinTypes,
		deviceId,
		expectedAddresses,
		jwt,
		password,
		storage
	});
	await __refreshAuth_wrapped(client);
	return result;
};

//#endregion
export { NotWaasWalletProviderError as a, createWaasClient as i, getWaasWalletProviderFromWalletAccount as n, getWaasChainNameFromChain as r, migrateFromFireblocks as t };
//# sourceMappingURL=migrateFromFireblocks-Vic3tb61.esm.js.map