import { B as name, C as isCookieEnabled, M as CLIENT_SDK_NAME, V as version, c as CHAINS_INFO_MAP, i as DYNAMIC_WAAS_METADATA, n as DEFAULT_WAAS_BASE_API_URL, r as DEFAULT_WAAS_BASE_MPC_RELAY_API_URL, s as __getChainFromVerifiedCredentialChain_wrapped, t as InvalidParamError, z as getCore } from "./InvalidParamError-0lP4zZBe.esm.js";
import "./isMfaRequiredForAction-CTghrtq8.esm.js";
import { n as __consumeMfaTokenIfRequiredForAction_wrapped, t as __getSignedSessionId_wrapped } from "./getSignedSessionId-DQr-WJ3a.esm.js";
import { assertPackageVersion } from "@dynamic-labs-sdk/assert-package-version";
import { MFAAction, WalletProviderEnum } from "@dynamic-labs/sdk-api-core";
import { DynamicWalletClient, ThresholdSignatureScheme, WalletOperation } from "@dynamic-labs-wallet/browser-wallet-client";

//#region src/modules/waas/createWaasClient/createWaasClient.ts
/** @not-instrumented */
const createWaasClient = ({ chainName }, client) => {
	const core = getCore(client);
	const additionalTrustedOrigins = core.waas?.additionalTrustedOrigins;
	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,
		environmentId: core.environmentId,
		sdkVersion: `${CLIENT_SDK_NAME}/${core.version}`,
		...additionalTrustedOrigins == void 0 ? {} : { additionalTrustedOrigins }
	});
};

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

//#endregion
//#region src/modules/waas/createWaasProvider/createWaasProvider.ts
const RAW_MESSAGE_MESSAGE_REQUIRED_LENGTH = 64;
/** @not-instrumented */
const createWaasProvider = ({ sdkClient, chain }) => {
	const logger = getCore(sdkClient).logger;
	let _waasClient;
	const getWaasClient = async () => {
		if (!_waasClient) {
			_waasClient = createWaasClient({ chainName: getWaasChainNameFromChain(chain) }, sdkClient);
			await _waasClient.initialize();
		}
		return _waasClient;
	};
	const backupKeySharesToGoogleDrive = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).backupKeySharesToGoogleDrive({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			password,
			signedSessionId
		});
	};
	const createWalletAccount = async (args = {}) => {
		logger.debug("[createWaasProvider] createWalletAccount", {
			bitcoinConfig: args.bitcoinConfig,
			thresholdSignatureScheme: args.thresholdSignatureScheme
		});
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return await (await getWaasClient()).createWalletAccount({
			authToken: sdkClient.token ?? void 0,
			bitcoinConfig: args.bitcoinConfig,
			password: args.password,
			signedSessionId,
			thresholdSignatureScheme: args.thresholdSignatureScheme ?? ThresholdSignatureScheme.TWO_OF_TWO
		});
	};
	const delegateKeyShares = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).delegateKeyShares({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			password,
			signedSessionId
		});
	};
	const exportClientKeyshares = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		await (await getWaasClient()).exportClientKeyshares({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			password,
			signedSessionId
		});
	};
	const exportPrivateKey = async ({ displayContainer, walletAccount, password }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		const waasClient = await getWaasClient();
		const mfaToken = await __consumeMfaTokenIfRequiredForAction_wrapped({ mfaAction: MFAAction.WalletWaasExport }, sdkClient);
		await waasClient.exportPrivateKey({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			displayContainer,
			mfaToken,
			password,
			signedSessionId
		});
	};
	const restoreUserShareForWalletAccount = async ({ walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		await (await getWaasClient()).getWallet({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			signedSessionId,
			walletOperation: WalletOperation.SIGN_MESSAGE
		});
	};
	const importPrivateKey = async ({ privateKey, thresholdSignatureScheme = ThresholdSignatureScheme.TWO_OF_TWO }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		await (await getWaasClient()).importPrivateKey({
			authToken: sdkClient.token ?? void 0,
			privateKey,
			signedSessionId,
			thresholdSignatureScheme
		});
	};
	const refreshWalletAccountShares = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		const walletClient = await getWaasClient();
		const mfaToken = await __consumeMfaTokenIfRequiredForAction_wrapped({ mfaAction: MFAAction.WalletWaasRefresh }, sdkClient);
		return walletClient.refreshWalletAccountShares({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			mfaToken,
			password,
			signedSessionId
		});
	};
	const revokeDelegation = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).revokeDelegation({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			password,
			signedSessionId
		});
	};
	const setWaasWalletAccountPassword = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).setPassword({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			newPassword: password,
			signedSessionId
		});
	};
	const signRawMessage = async ({ message, password, walletAccount }) => {
		if (message.length !== RAW_MESSAGE_MESSAGE_REQUIRED_LENGTH) throw new InvalidParamError(`Message must be ${RAW_MESSAGE_MESSAGE_REQUIRED_LENGTH} characters long`);
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		const mfaToken = await __consumeMfaTokenIfRequiredForAction_wrapped({ mfaAction: MFAAction.WalletWaasSign }, sdkClient);
		return { signature: await (await getWaasClient()).signRawMessage({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			message,
			mfaToken,
			password,
			signedSessionId
		}) };
	};
	const terminate = async (args) => {
		if (!_waasClient) return;
		const waasClient = await getWaasClient();
		if (args?.reason !== "token-expired") await waasClient.cleanup();
		_waasClient = void 0;
	};
	const updatePassword = async ({ walletAccount, currentPassword, newPassword }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).updatePassword({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			existingPassword: currentPassword,
			newPassword,
			signedSessionId
		});
	};
	const signMessage = async ({ message, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		const mfaToken = await __consumeMfaTokenIfRequiredForAction_wrapped({ mfaAction: MFAAction.WalletWaasSign }, sdkClient);
		return { signature: await (await getWaasClient()).signMessage({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			message,
			mfaToken,
			signedSessionId
		}) };
	};
	const signSerializedTransaction = async ({ chainId, serializedTransaction, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		const mfaToken = await __consumeMfaTokenIfRequiredForAction_wrapped({ mfaAction: MFAAction.WalletWaasSign }, sdkClient);
		return { signature: await (await getWaasClient()).signTransaction({
			authToken: sdkClient.token ?? void 0,
			chainId,
			mfaToken,
			senderAddress: walletAccount.address,
			signedSessionId,
			transaction: serializedTransaction
		}) };
	};
	const getWalletRecoveryState = async ({ walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).getWalletRecoveryState({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			signedSessionId
		});
	};
	const unlockWallet = async ({ password, walletAccount }) => {
		const { signature: signedSessionId } = await __getSignedSessionId_wrapped(sdkClient);
		return (await getWaasClient()).unlockWallet({
			accountAddress: walletAccount.address,
			authToken: sdkClient.token ?? void 0,
			password,
			signedSessionId
		});
	};
	return {
		backupKeySharesToGoogleDrive,
		createWalletAccount,
		delegateKeyShares,
		exportClientKeyshares,
		exportPrivateKey,
		getWaasClient,
		getWalletRecoveryState,
		importPrivateKey,
		refreshWalletAccountShares,
		restoreUserShareForWalletAccount,
		revokeDelegation,
		setWaasWalletAccountPassword,
		signMessage,
		signRawMessage,
		signSerializedTransaction,
		terminate,
		unlockWallet,
		updatePassword
	};
};

//#endregion
//#region src/modules/waas/getAllUserWaasAddressesForChain/getAllUserWaasAddressesForChain.ts
/** @not-instrumented */
const getAllUserWaasAddressesForChain = ({ chain }, client) => {
	if (!client.user) return [];
	return (client.user.verifiedCredentials.filter((credential) => credential.walletProvider === WalletProviderEnum.EmbeddedWallet && credential.walletName?.toLowerCase().startsWith(DYNAMIC_WAAS_METADATA.normalizedWalletName) && credential.address && credential.chain && __getChainFromVerifiedCredentialChain_wrapped(credential.chain) === chain) ?? []).map((credential) => credential.address);
};

//#endregion
//#region src/exports/waasCore.ts
assertPackageVersion(name, version);

//#endregion
export { DYNAMIC_WAAS_METADATA, createWaasClient, createWaasProvider, getAllUserWaasAddressesForChain, getWaasChainNameFromChain };
//# sourceMappingURL=waasCore.esm.js.map