import type { PermissionSpecificationBuilder, EndowmentGetterParams, PermissionValidatorConstraint, EndowmentCaveatSpecificationConstraint } from '@metamask/permission-controller'; import { CaveatMutatorOperation, PermissionType } from '@metamask/permission-controller'; import type { CaipAccountId, CaipChainId, Json, Hex, NonEmptyArray } from '@metamask/utils'; import { CaveatTypes, PermissionKeys } from './constants.js'; import type { ExternalScopeString, InternalScopeObject, InternalScopesObject } from './scope/types.js'; /** * The CAIP-25 permission caveat value. * This permission contains the required and optional scopes and session properties from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request that initiated the permission session. * It also contains a boolean (isMultichainOrigin) indicating if the permission session is multichain, which may be needed to determine implicit permissioning. */ export type Caip25CaveatValue = { requiredScopes: InternalScopesObject; optionalScopes: InternalScopesObject; sessionProperties: Record; isMultichainOrigin: boolean; }; /** * The name of the CAIP-25 permission caveat. */ export declare const Caip25CaveatType = "authorizedScopes"; /** * The target name of the CAIP-25 endowment permission. */ export declare const Caip25EndowmentPermissionName = "endowment:caip25"; /** * Creates a CAIP-25 permission caveat. * * @param value - The CAIP-25 permission caveat value. * @returns The CAIP-25 permission caveat (now including the type). */ export declare const createCaip25Caveat: (value: Caip25CaveatValue) => { type: string; value: Caip25CaveatValue; }; type Caip25EndowmentCaveatSpecificationBuilderOptions = { findNetworkClientIdByChainId: (chainId: Hex) => string; listAccounts: () => { type: string; address: Hex; }[]; isNonEvmScopeSupported: (scope: CaipChainId) => boolean; getNonEvmAccountAddresses: (scope: CaipChainId) => string[]; }; /** * Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time. * * @param originalValue - The existing CAIP-25 permission caveat value. * @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values. * @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request. * @returns The difference between original and merged CAIP-25 permission caveat values. */ export declare function diffScopesForCaip25CaveatValue(originalValue: Caip25CaveatValue, mergedValue: Caip25CaveatValue, scopeToDiff: 'optionalScopes' | 'requiredScopes'): Caip25CaveatValue; /** * Helper that returns a `authorizedScopes` CAIP-25 caveat specification * that can be passed into the PermissionController constructor. * * @param options - The specification builder options. * @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId. * @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts. * @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported. * @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope. * @returns The specification for the `caip25` caveat. */ export declare const caip25CaveatBuilder: ({ findNetworkClientIdByChainId, listAccounts, isNonEvmScopeSupported, getNonEvmAccountAddresses, }: Caip25EndowmentCaveatSpecificationBuilderOptions) => EndowmentCaveatSpecificationConstraint & Required>; /** * The `caip25` endowment specification builder. Passed to the * `PermissionController` for constructing and validating the * `endowment:caip25` permission. */ export declare const caip25EndowmentBuilder: Readonly<{ readonly targetName: "endowment:caip25"; readonly specificationBuilder: PermissionSpecificationBuilder, { permissionType: PermissionType.Endowment; targetName: typeof Caip25EndowmentPermissionName; endowmentGetter: (_options?: EndowmentGetterParams) => null; validator: PermissionValidatorConstraint; allowedCaveats: Readonly> | null; }>; }>; /** * Factories that construct caveat mutator functions that are passed to * PermissionController.updatePermissionsByCaveat. */ export declare const Caip25CaveatMutators: { authorizedScopes: { removeScope: typeof removeScope; removeAccount: typeof removeAccount; }; }; /** * Removes the target account from the scope object. * * @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes). * @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope. * @returns The updated scope object. */ declare function removeAccount(caip25CaveatValue: Caip25CaveatValue, targetAddress: Hex): { operation: CaveatMutatorOperation; value?: undefined; } | { operation: CaveatMutatorOperation; value: Caip25CaveatValue; }; /** * Removes the target scope from the value arrays of the given * `endowment:caip25` caveat. No-ops if the target scopeString is not in * the existing scopes. * * @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from. * @param targetScopeString - The scope that is being removed. * @returns The updated CAIP-25 permission caveat value. */ declare function removeScope(caip25CaveatValue: Caip25CaveatValue, targetScopeString: ExternalScopeString): { value?: undefined; operation: CaveatMutatorOperation; } | { operation: CaveatMutatorOperation; value: { sessionProperties: Record; isMultichainOrigin: boolean; requiredScopes: { [k: string]: InternalScopeObject; }; optionalScopes: { [k: string]: InternalScopeObject; }; }; }; /** * Modifies the requested CAIP-25 permissions object after UI confirmation. * * @param caip25CaveatValue - The requested CAIP-25 caveat value to modify. * @param accountAddresses - The list of permitted eth addresses. * @param chainIds - The list of permitted eth chainIds. * @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set. */ export declare const generateCaip25Caveat: (caip25CaveatValue: Caip25CaveatValue, accountAddresses: CaipAccountId[], chainIds: CaipChainId[]) => { [Caip25EndowmentPermissionName]: { caveats: [{ type: string; value: Caip25CaveatValue; }]; }; }; /** * Helper to get the CAIP-25 caveat from a permission * * @param [caip25Permission] - The CAIP-25 permission object * @param caip25Permission.caveats - The caveats of the CAIP-25 permission * @returns The CAIP-25 caveat or undefined if not found */ export declare function getCaip25CaveatFromPermission(caip25Permission?: { caveats: ({ type: string; value: unknown; } | { type: typeof Caip25CaveatType; value: Caip25CaveatValue; })[]; }): { type: typeof Caip25CaveatType; value: Caip25CaveatValue; } | undefined; /** * Requests user approval for the CAIP-25 permission * and returns a granted permissions object. * * @param requestedPermissions - The legacy permissions to request approval for. * @param requestedPermissions.caveats - The legacy caveats processed by the function. * - `restrictReturnedAccounts`: Restricts which Ethereum accounts can be accessed * - `restrictNetworkSwitching`: Restricts which blockchain networks can be used * @returns The converted CAIP-25 permission object. */ export declare const getCaip25PermissionFromLegacyPermissions: (requestedPermissions?: { [PermissionKeys.eth_accounts]?: { caveats?: { type: keyof typeof CaveatTypes; value: Hex[]; }[]; }; [PermissionKeys.permittedChains]?: { caveats?: { type: keyof typeof CaveatTypes; value: Hex[]; }[]; }; }) => { [Caip25EndowmentPermissionName]: { caveats: NonEmptyArray<{ type: typeof Caip25CaveatType; value: Caip25CaveatValue; }>; }; }; /** * Requests incremental permittedChains permission for the specified origin. * and updates the existing CAIP-25 permission. * Allows for granting without prompting for user approval which * would be used as part of flows like `wallet_addEthereumChain` * requests where the addition of the network and the permitting * of the chain are combined into one approval. * * @param options - The options object * @param options.origin - The origin to request approval for. * @param options.chainId - The chainId to add to the existing permittedChains. * @param options.autoApprove - If the chain should be granted without prompting for user approval. * @param options.metadata - Request data for the approval. * @param options.metadata.options - Additional metadata about the permission request. * @param options.hooks - Permission controller hooks for incremental operations. * @param options.hooks.requestPermissionsIncremental - Initiates an incremental permission request that prompts for user approval. * Incremental permission requests allow the caller to replace existing and/or add brand new permissions and caveats for the specified subject. * @param options.hooks.grantPermissionsIncremental - Incrementally grants approved permissions to the specified subject without prompting for user approval. * Every permission and caveat is stringently validated and an error is thrown if validation fails. */ export declare const requestPermittedChainsPermissionIncremental: ({ origin, chainId, autoApprove, hooks, metadata, }: { origin: string; chainId: Hex; autoApprove: boolean; hooks: { requestPermissionsIncremental: (subject: { origin: string; }, requestedPermissions: Record, options?: { metadata?: Record; }) => Promise<[Partial>, { data?: Record; id: string; origin: string; }] | []>; grantPermissionsIncremental: (params: { subject: { origin: string; }; approvedPermissions: Record; requestData?: Record; }) => Partial>; }; metadata?: { options: Record; }; }) => Promise; export {}; //# sourceMappingURL=caip25Permission.d.ts.map