import { B as name, F as getDefaultClient, R as BaseError, V as version, i as DYNAMIC_WAAS_METADATA, s as __getChainFromVerifiedCredentialChain_wrapped, w as assertDefined } from "./InvalidParamError-CY4JGkjq.native.esm.js";
import { c as findWaasWalletProviderByChain, h as __getWalletProviderFromWalletAccount_wrapped, l as isWaasWalletProvider, n as getWalletProviderByKey, s as isWaasWalletAccount, t as __getVerifiedCredentialForWalletAccount_wrapped, x as formatWalletProviderKey, y as NoWalletProviderFoundError } from "./getVerifiedCredentialForWalletAccount-BCPvaJ1V.native.esm.js";
import { n as __refreshAuth_wrapped, t as NotWaasWalletAccountError } from "./NotWaasWalletAccountError-DYuQuYeI.native.esm.js";
import { assertPackageVersion } from "@dynamic-labs-sdk/assert-package-version";
import { EmbeddedWalletVersionEnum, JwtVerifiedCredentialFormatEnum, WalletProviderEnum } from "@dynamic-labs/sdk-api-core";

//#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/getWaasWalletProviderFromWalletAccount/getWaasWalletProviderFromWalletAccount.ts
/** @not-instrumented */
const getWaasWalletProviderFromWalletAccount = ({ walletAccount }, client) => {
	const walletProvider = __getWalletProviderFromWalletAccount_wrapped({ walletAccount }, client);
	if (!isWaasWalletProvider(walletProvider)) throw new NotWaasWalletAccountError({ walletAddress: walletAccount.address });
	return walletProvider;
};

//#endregion
//#region src/modules/waas/backupWaasKeySharesToGoogleDrive/backupWaasKeySharesToGoogleDrive.ts
/**
* Backs up WaaS wallet key shares to Google Drive for secure recovery.
*
* This function creates a backup of the wallet's cryptographic key shares
* to Google Drive, allowing users to recover their wallet later.
*
* @param params.walletAccount - The WaaS wallet account to backup.
* @param [params.password] - Optional password to encrypt the backup.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the backup process is complete.
* @not-instrumented
*/
const backupWaasKeySharesToGoogleDrive = async ({ password, walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).backupKeySharesToGoogleDrive({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/isWalletCreationRequirement/isWalletCreationRequirement.ts
/**
* Type guard to check if an item is a WalletCreationRequirement object.
* Chain values are strings, so we can distinguish by checking if it's an object with a 'chain' property.
*
* @param item - The item to check, either a WalletCreationRequirement or Chain string
* @returns True if the item is a WalletCreationRequirement object
* @not-instrumented
*/
const isWalletCreationRequirement = (item) => typeof item === "object" && item !== null && "chain" in item;

//#endregion
//#region src/modules/waas/normalizeWalletRequirements/normalizeWalletRequirements.ts
const DEFAULT_BITCOIN_ADDRESS_TYPE = "native_segwit";
/**
* Normalizes wallet creation input from either legacy Chain[] format
* or new WalletCreationRequirement[] format to WalletCreationRequirement[].
*
* For BTC chains without explicit bitcoinConfig, defaults to native_segwit.
*
* @param params.input - Either an array of Chain strings or WalletCreationRequirement objects
* @param params.defaultBitcoinConfig - Optional default bitcoin config to use for BTC chains when converting from legacy format
* @returns Array of WalletCreationRequirement objects
* @not-instrumented
*/
const normalizeWalletRequirements = ({ defaultBitcoinConfig, input }) => {
	if (input.length === 0) return [];
	const [firstItem] = input;
	if (isWalletCreationRequirement(firstItem)) return input.map((requirement) => {
		if (requirement.chain === "BTC" && !requirement.bitcoinConfig) return {
			...requirement,
			bitcoinConfig: { addressType: DEFAULT_BITCOIN_ADDRESS_TYPE }
		};
		return requirement;
	});
	return input.map((chain) => {
		const requirement = { chain };
		if (chain === "BTC") requirement.bitcoinConfig = defaultBitcoinConfig ?? { addressType: DEFAULT_BITCOIN_ADDRESS_TYPE };
		return requirement;
	});
};

//#endregion
//#region src/modules/waas/createWaasWalletAccounts/createWaasWalletAccounts.ts
/**
* Creates wallet accounts for the specified chains.
*
* This function creates wallet accounts for the specified chains.
* If the same chain is passed multiple times, just as many wallet accounts will be created
* for that chain.
*
* @example
* ```ts
* // Legacy format - will create wallet accounts for EVM and SOL, BTC defaults to native_segwit
* await createWaasWalletAccounts({ chains: ['EVM', 'SOL', 'BTC'] });
*
* // New format with chain-specific config
* await createWaasWalletAccounts({
*   chains: [
*     { chain: 'BTC', bitcoinConfig: { addressType: 'taproot' } },
*     { chain: 'EVM' },
*   ]
* });
*
* // Create both BTC address types
* await createWaasWalletAccounts({
*   chains: [
*     { chain: 'BTC', bitcoinConfig: { addressType: 'native_segwit' } },
*     { chain: 'BTC', bitcoinConfig: { addressType: 'taproot' } },
*   ]
* });
* ```
*
* @param params.chains - The chains to create wallet accounts for.
* @param params.password - The optional password to use for the wallet accounts.
* @param params.thresholdSignatureScheme - The threshold signature scheme to use for the wallet accounts. (Default: TWO_OF_TWO)
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when all wallet accounts are created.
* @not-instrumented
*/
const createWaasWalletAccounts = async ({ chains, password, thresholdSignatureScheme }, client = getDefaultClient()) => {
	const normalizedRequirements = normalizeWalletRequirements({ input: chains });
	const supportedChains = [
		"BTC",
		"EVM",
		"SOL",
		"SUI",
		"TON"
	];
	const filteredRequirements = normalizedRequirements.filter((requirement) => supportedChains.includes(requirement.chain));
	if (filteredRequirements.length === 0) return;
	const walletProviderMap = /* @__PURE__ */ new Map();
	for (const requirement of filteredRequirements) {
		const walletProviderKey = formatWalletProviderKey({
			chain: requirement.chain,
			displayName: DYNAMIC_WAAS_METADATA.displayName,
			walletProviderType: WalletProviderEnum.EmbeddedWallet
		});
		if (!walletProviderMap.has(walletProviderKey)) {
			const walletProvider = getWalletProviderByKey({ walletProviderKey }, client);
			if (!walletProvider) throw new NoWalletProviderFoundError({ walletProviderKey });
			if (!isWaasWalletProvider(walletProvider)) throw new NotWaasWalletProviderError({ walletProviderKey });
			walletProviderMap.set(walletProviderKey, walletProvider);
		}
	}
	await Promise.all(filteredRequirements.map((requirement) => {
		const walletProviderKey = formatWalletProviderKey({
			chain: requirement.chain,
			displayName: DYNAMIC_WAAS_METADATA.displayName,
			walletProviderType: WalletProviderEnum.EmbeddedWallet
		});
		const walletProvider = walletProviderMap.get(walletProviderKey);
		if (!walletProvider || !isWaasWalletProvider(walletProvider)) return Promise.resolve();
		return walletProvider.createWalletAccount({
			bitcoinConfig: requirement.bitcoinConfig,
			password,
			thresholdSignatureScheme
		});
	}));
	await __refreshAuth_wrapped(client);
};

//#endregion
//#region src/modules/waas/delegateWaasKeyShares/delegateWaasKeyShares.ts
/**
* Delegates cryptographic key shares for a WaaS wallet account.
*
* This function allows the delegation of key management responsibilities,
* enabling distributed key management for enhanced security.
*
* @param params.walletAccount - The WaaS wallet account to delegate shares for.
* @param [params.password] - Optional password for key share operations.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the key delegation is complete.
* @not-instrumented
*/
const delegateWaasKeyShares = async ({ password, walletAccount }, client = getDefaultClient()) => {
	await getWaasWalletProviderFromWalletAccount({ walletAccount }, client).delegateKeyShares({
		password,
		walletAccount
	});
	await __refreshAuth_wrapped(client);
};

//#endregion
//#region src/modules/waas/exportWaasClientKeyshares/exportWaasClientKeyshares.ts
/**
* Exports the client-side key shares for a WaaS wallet account.
*
* This function retrieves the cryptographic key shares that are stored
* on the client side for backup or migration purposes.
*
* @param params.walletAccount - The WaaS wallet account to export shares from.
* @param [params.password] - Optional password to encrypt the exported shares.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves to the exported key shares.
* @not-instrumented
*/
const exportWaasClientKeyshares = async ({ password, walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).exportClientKeyshares({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/exportWaasPrivateKey/exportWaasPrivateKey.ts
/**
* Exports the private key for a WaaS wallet account with secure display.
*
* This function reveals the private key for a WaaS wallet and displays it
* securely within an iframe dynamically inserted into the specified HTML container element.
*
* @param params.walletAccount - The WaaS wallet account to export the private key from.
* @param params.displayContainer - The HTML element where the iframe will be injected.
* @param [params.password] - Optional password to decrypt the private key.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the private key export is complete.
* @not-instrumented
*/
const exportWaasPrivateKey = async ({ displayContainer, password, walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).exportPrivateKey({
		displayContainer,
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/wallets/userHasEmbeddedWalletForChain/userHasEmbeddedWalletForChain.ts
/** @not-instrumented */
const userHasEmbeddedWalletForChain = ({ user, chain }) => {
	return user.verifiedCredentials.some((vc) => vc.walletProvider === WalletProviderEnum.EmbeddedWallet && vc.chain && __getChainFromVerifiedCredentialChain_wrapped(vc.chain) === chain);
};

//#endregion
//#region src/modules/waas/isDynamicWaasEnabled/isDynamicWaasEnabled.ts
/**
* Checks if Dynamic WaaS (Wallet as a Service) is enabled for the current project.
*
* This function examines the project settings to determine if Dynamic WaaS is
* enabled in the Dynamic Dashboard or not.
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if Dynamic WaaS is enabled, false otherwise.
* @not-instrumented
*/
const isDynamicWaasEnabled = (client = getDefaultClient()) => {
	const projectSettings = client.projectSettings;
	assertDefined(projectSettings, "Project settings are not available");
	return projectSettings.sdk.embeddedWallets?.defaultWalletVersion === EmbeddedWalletVersionEnum.V3;
};

//#endregion
//#region src/modules/waas/getChainsMissingWaasWalletAccounts/getChainsMissingWaasWalletAccounts.ts
/**
* Gets all chains missing a waas wallet account for the current user.
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns An array of all chains that have waas wallet accounts enabled
* but for which the current user does not yet have an embedded wallet account.
* @not-instrumented
*/
const getChainsMissingWaasWalletAccounts = (client = getDefaultClient()) => {
	const projectSettings = client.projectSettings;
	assertDefined(projectSettings, "Project settings are not available");
	const embeddedWalletSettings = projectSettings.sdk.embeddedWallets;
	const isWaasEnabled = isDynamicWaasEnabled(client);
	const user = client.user;
	if (!user || !isWaasEnabled) return [];
	return (embeddedWalletSettings?.chainConfigurations?.filter((chain) => chain.enabled) ?? []).map((chain) => chain.name).filter((chain) => !userHasEmbeddedWalletForChain({
		chain,
		user
	}));
};

//#endregion
//#region src/modules/waas/getWalletRecoveryState/getWalletRecoveryState.ts
/**
* Retrieves the recovery state of a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to get the recovery state for.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves with the wallet recovery state.
* @not-instrumented
*/
const getWalletRecoveryState = async ({ walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).getWalletRecoveryState({ walletAccount });
};

//#endregion
//#region src/modules/waas/hasDelegatedAccess/hasDelegatedAccess.ts
/**
* Checks if a WaaS wallet account has delegated access.
*
* @param params.walletAccount - The WaaS wallet account to check for delegated access.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if the wallet account has delegated access, false otherwise.
* @not-instrumented
*/
const hasDelegatedAccess = ({ walletAccount }, client = getDefaultClient()) => {
	const verifiedCredential = __getVerifiedCredentialForWalletAccount_wrapped({ walletAccount }, client);
	assertDefined(verifiedCredential, "Verified credential not found for Waas wallet account");
	return !!verifiedCredential.walletProperties?.keyShares?.some((keyShare) => keyShare.backupLocation === "delegated");
};

//#endregion
//#region src/modules/waas/getWaasWalletProviderByChain/getWaasWalletProviderByChain.ts
/** @not-instrumented */
const getWaasWalletProviderByChain = ({ chain }, client) => {
	const waasProvider = findWaasWalletProviderByChain({ chain }, client);
	if (!waasProvider) throw new NoWalletProviderFoundError({ walletProviderKey: formatWalletProviderKey({
		chain,
		displayName: DYNAMIC_WAAS_METADATA.displayName,
		walletProviderType: WalletProviderEnum.EmbeddedWallet
	}) });
	return waasProvider;
};

//#endregion
//#region src/modules/waas/importWaasPrivateKey/importWaasPrivateKey.ts
/**
* Imports an existing private key to create a new WaaS wallet account.
*
* This function allows users to import their existing private key into
* Dynamic's WaaS infrastructure for secure management.
*
* @param params.chain - The chain for the imported wallet.
* @param params.privateKey - The private key to import.
* @param [params.thresholdSignatureScheme] - Optional threshold signature scheme configuration. Default is TWO_OF_TWO.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the private key is successfully imported.
* @not-instrumented
*/
const importWaasPrivateKey = async ({ chain, privateKey, thresholdSignatureScheme }, client = getDefaultClient()) => {
	return getWaasWalletProviderByChain({ chain }, client).importPrivateKey({
		privateKey,
		thresholdSignatureScheme
	});
};

//#endregion
//#region src/modules/waas/isPasswordRequiredForWaasWallets/isPasswordRequiredForWaasWallets.ts
/**
* Checks if the user Waas wallets are password protected.
*
* If the user's Waas wallets are password protected, it means that user's will have to be prompted for a
* password for the first wallet action that happens on each session.
* The password is also required for creating any new Waas wallets.
*
* @param client - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if the user has any password protected Waas wallets, false otherwise
* @not-instrumented
*/
const isPasswordRequiredForWaasWallets = (client = getDefaultClient()) => {
	const { user } = client;
	assertDefined(user, "User is not authenticated");
	return user.verifiedCredentials.some((credential) => credential.walletProperties?.keyShares?.some((keyShare) => keyShare.passwordEncrypted));
};

//#endregion
//#region src/modules/waas/refreshWaasWalletAccountShares/refreshWaasWalletAccountShares.ts
/**
* Refreshes the cryptographic key shares for a WaaS wallet account.
*
* This function generates new key shares for the wallet while maintaining
* the same threshold signature scheme.
*
* @param params.walletAccount - The WaaS wallet account to refresh shares for.
* @param [params.password] - Optional password for key share operations.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the key shares are refreshed.
* @not-instrumented
*/
const refreshWaasWalletAccountShares = async ({ walletAccount, password }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).refreshWalletAccountShares({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/revokeWaasDelegation/revokeWaasDelegation.ts
/**
* Revokes the delegation of a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to revoke delegation for.
* @param [params.password] - Optional password for delegation operations.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the delegation is revoked.
* @not-instrumented
*/
const revokeWaasDelegation = async ({ password, walletAccount }, client = getDefaultClient()) => {
	await getWaasWalletProviderFromWalletAccount({ walletAccount }, client).revokeDelegation({
		password,
		walletAccount
	});
	await __refreshAuth_wrapped(client);
};

//#endregion
//#region src/modules/waas/setWaasWalletAccountPassword/setWaasWalletAccountPassword.ts
/**
* Sets a password for a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to set the password for.
* @param params.password - The password to set for the wallet.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the password is successfully set.
* @not-instrumented
*/
const setWaasWalletAccountPassword = async ({ password, walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).setWaasWalletAccountPassword({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/wallets/userHasExternalWallet/userHasExternalWallet.ts
/** @not-instrumented */
const userHasExternalWallet = (user) => {
	return user?.verifiedCredentials.some((vc) => vc.format === JwtVerifiedCredentialFormatEnum.Blockchain && vc.walletProvider !== WalletProviderEnum.EmbeddedWallet);
};

//#endregion
//#region src/modules/waas/shouldAutoCreateWalletForChain/shouldAutoCreateWalletForChain.ts
/**
* Checks if a WaaS wallet should be automatically created for the specified blockchain.
*
* This function determines whether the current user configuration and project
* settings require automatic wallet creation for the given chain, considering
* whether the user already has an embedded wallet for the enabled chains or an external wallet.
*
* @param params.chain - The blockchain network to check.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if a wallet should be auto-created for the chain, false otherwise.
* @not-instrumented
*/
const shouldAutoCreateWalletForChain = ({ chain }, client = getDefaultClient()) => {
	assertDefined(client.projectSettings, "Project settings are not available");
	const embeddedWalletSettings = client.projectSettings.sdk.embeddedWallets;
	if (!embeddedWalletSettings?.automaticEmbeddedWalletCreation || !client.user) return false;
	const automaticEmbeddedWalletCreationForExternalEnabled = embeddedWalletSettings?.automaticEmbeddedWalletCreationForExternal;
	if (!(!userHasExternalWallet(client.user) || automaticEmbeddedWalletCreationForExternalEnabled)) return false;
	return getChainsMissingWaasWalletAccounts(client).includes(chain);
};

//#endregion
//#region src/modules/waas/unlockWallet/unlockWallet.ts
/**
* Unlocks a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to unlock.
* @param params.password - The password to unlock the wallet.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves with the wallet information.
* @not-instrumented
*/
const unlockWallet = async ({ password, walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).unlockWallet({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/updateWaasPassword/updateWaasPassword.ts
/**
* Updates the password for a WaaS wallet account.
*
* This function changes the password used to encrypt and protect the
* cryptographic key shares for the specified wallet account.
*
* @param params.walletAccount - The WaaS wallet account to update the password for.
* @param params.currentPassword - The current password for the wallet.
* @param params.newPassword - The new password to set for the wallet.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the password is successfully updated.
* @not-instrumented
*/
const updateWaasPassword = async ({ currentPassword, newPassword, walletAccount }, client = getDefaultClient()) => {
	return getWaasWalletProviderFromWalletAccount({ walletAccount }, client).updatePassword({
		currentPassword,
		newPassword,
		walletAccount
	});
};

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

//#endregion
export { NotWaasWalletProviderError, backupWaasKeySharesToGoogleDrive, createWaasWalletAccounts, delegateWaasKeyShares, exportWaasClientKeyshares, exportWaasPrivateKey, getChainsMissingWaasWalletAccounts, getWalletRecoveryState, hasDelegatedAccess, importWaasPrivateKey, isDynamicWaasEnabled, isPasswordRequiredForWaasWallets, isWaasWalletAccount, isWalletCreationRequirement, normalizeWalletRequirements, refreshWaasWalletAccountShares, revokeWaasDelegation, setWaasWalletAccountPassword, shouldAutoCreateWalletForChain, unlockWallet, updateWaasPassword };
//# sourceMappingURL=waas.native.esm.js.map