@metamask/snaps-rpc-methods 3.2.1 → 4.0.0
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/CHANGELOG.md +18 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/permitted/common/snapInstallation.js.map +1 -1
- package/dist/cjs/permitted/getFile.js +5 -4
- package/dist/cjs/permitted/getFile.js.map +1 -1
- package/dist/cjs/permitted/getSnaps.js.map +1 -1
- package/dist/cjs/permitted/invokeKeyring.js.map +1 -1
- package/dist/cjs/permitted/invokeSnapSugar.js.map +1 -1
- package/dist/cjs/permitted/requestSnaps.js.map +1 -1
- package/dist/cjs/restricted/caveats/permittedCoinTypes.js.map +1 -1
- package/dist/cjs/restricted/dialog.js +16 -25
- package/dist/cjs/restricted/dialog.js.map +1 -1
- package/dist/cjs/restricted/getBip32Entropy.js.map +1 -1
- package/dist/cjs/restricted/getBip32PublicKey.js.map +1 -1
- package/dist/cjs/restricted/getBip44Entropy.js +0 -1
- package/dist/cjs/restricted/getBip44Entropy.js.map +1 -1
- package/dist/cjs/restricted/getEntropy.js.map +1 -1
- package/dist/cjs/restricted/getLocale.js.map +1 -1
- package/dist/cjs/restricted/index.js +0 -9
- package/dist/cjs/restricted/index.js.map +1 -1
- package/dist/cjs/restricted/invokeSnap.js.map +1 -1
- package/dist/cjs/restricted/manageAccounts.js.map +1 -1
- package/dist/cjs/restricted/manageState.js +32 -31
- package/dist/cjs/restricted/manageState.js.map +1 -1
- package/dist/cjs/restricted/notify.js +6 -13
- package/dist/cjs/restricted/notify.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/permitted/common/snapInstallation.js.map +1 -1
- package/dist/esm/permitted/getFile.js +2 -1
- package/dist/esm/permitted/getFile.js.map +1 -1
- package/dist/esm/permitted/getSnaps.js.map +1 -1
- package/dist/esm/permitted/invokeKeyring.js.map +1 -1
- package/dist/esm/permitted/invokeSnapSugar.js.map +1 -1
- package/dist/esm/permitted/requestSnaps.js.map +1 -1
- package/dist/esm/restricted/caveats/permittedCoinTypes.js.map +1 -1
- package/dist/esm/restricted/dialog.js +3 -9
- package/dist/esm/restricted/dialog.js.map +1 -1
- package/dist/esm/restricted/getBip32Entropy.js.map +1 -1
- package/dist/esm/restricted/getBip32PublicKey.js.map +1 -1
- package/dist/esm/restricted/getBip44Entropy.js +0 -1
- package/dist/esm/restricted/getBip44Entropy.js.map +1 -1
- package/dist/esm/restricted/getEntropy.js.map +1 -1
- package/dist/esm/restricted/getLocale.js.map +1 -1
- package/dist/esm/restricted/index.js +0 -3
- package/dist/esm/restricted/index.js.map +1 -1
- package/dist/esm/restricted/invokeSnap.js.map +1 -1
- package/dist/esm/restricted/manageAccounts.js.map +1 -1
- package/dist/esm/restricted/manageState.js +28 -24
- package/dist/esm/restricted/manageState.js.map +1 -1
- package/dist/esm/restricted/notify.js +3 -7
- package/dist/esm/restricted/notify.js.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/permitted/common/snapInstallation.d.ts +3 -5
- package/dist/types/permitted/getFile.d.ts +5 -4
- package/dist/types/permitted/getSnaps.d.ts +3 -3
- package/dist/types/permitted/handlers.d.ts +6 -12
- package/dist/types/permitted/invokeKeyring.d.ts +5 -7
- package/dist/types/permitted/invokeSnapSugar.d.ts +5 -8
- package/dist/types/permitted/requestSnaps.d.ts +3 -2
- package/dist/types/restricted/caveats/permittedCoinTypes.d.ts +1 -1
- package/dist/types/restricted/dialog.d.ts +34 -35
- package/dist/types/restricted/getBip32Entropy.d.ts +2 -3
- package/dist/types/restricted/getBip32PublicKey.d.ts +2 -6
- package/dist/types/restricted/getBip44Entropy.d.ts +2 -5
- package/dist/types/restricted/getEntropy.d.ts +3 -2
- package/dist/types/restricted/getLocale.d.ts +2 -1
- package/dist/types/restricted/index.d.ts +9 -90
- package/dist/types/restricted/invokeSnap.d.ts +8 -7
- package/dist/types/restricted/manageAccounts.d.ts +4 -3
- package/dist/types/restricted/manageState.d.ts +6 -15
- package/dist/types/restricted/notify.d.ts +4 -7
- package/package.json +4 -5
- package/dist/cjs/request.js +0 -6
- package/dist/cjs/request.js.map +0 -1
- package/dist/esm/request.js +0 -3
- package/dist/esm/request.js.map +0 -1
- package/dist/types/request.d.ts +0 -99
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/restricted/manageState.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport { STATE_ENCRYPTION_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray, Hex } from '@metamask/utils';\nimport { isObject, getJsonSize, assert, isValidJson } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\n// The salt used for SIP-6-based entropy derivation.\nexport const STATE_ENCRYPTION_SALT = 'snap_manageState encryption';\n\nconst methodName = 'snap_manageState';\n\nexport type ManageStateMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * A function that clears the state of the requesting Snap.\n */\n clearSnapState: (snapId: string) => Promise<void>;\n\n /**\n * A function that gets the encrypted state of the requesting Snap.\n *\n * @returns The current state of the Snap.\n */\n getSnapState: (snapId: string) => Promise<string>;\n\n /**\n * A function that updates the state of the requesting Snap.\n *\n * @param newState - The new state of the Snap.\n */\n updateSnapState: (snapId: string, newState: string) => Promise<void>;\n\n /**\n * Encrypts data with a key. This is assumed to perform symmetric encryption.\n *\n * @param key - The key to use for encryption, in hexadecimal format.\n * @param data - The JSON data to encrypt.\n * @returns The ciphertext as a string. The format for this string is\n * dependent on the implementation, but MUST be a string.\n */\n encrypt: (key: string, data: Json) => Promise<string>;\n\n /**\n * Decrypts data with a key. This is assumed to perform symmetric decryption.\n *\n * @param key - The key to use for decryption, in hexadecimal format.\n * @param cipherText - The ciphertext to decrypt. The format for this string\n * is dependent on the implementation, but MUST be a string.\n * @returns The decrypted data as a JSON object.\n */\n decrypt: (key: Hex, cipherText: string) => Promise<unknown>;\n};\n\ntype ManageStateSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: ManageStateMethodHooks;\n};\n\ntype ManageStateSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getManageStateImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_manageState` permission.\n * `snap_manageState` lets the Snap store and manage some of its state on\n * your device.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_manageState` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n ManageStateSpecificationBuilderOptions,\n ManageStateSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: ManageStateSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getManageStateImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<ManageStateMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n clearSnapState: true,\n getSnapState: true,\n updateSnapState: true,\n encrypt: true,\n decrypt: true,\n};\n\nexport const manageStateBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport enum ManageStateOperation {\n ClearState = 'clear',\n GetState = 'get',\n UpdateState = 'update',\n}\n\nexport type ManageStateArgs = {\n operation: EnumToUnion<ManageStateOperation>;\n newState?: Record<string, Json>;\n};\n\nexport const STORAGE_SIZE_LIMIT = 104857600; // In bytes (100MB)\n\ntype GetEncryptionKeyArgs = {\n snapId: string;\n mnemonicPhrase: Uint8Array;\n};\n\n/**\n * Get a deterministic encryption key to use for encrypting and decrypting the\n * state.\n *\n * This key should only be used for state encryption using `snap_manageState`.\n * To get other encryption keys, a different salt can be used.\n *\n * @param args - The encryption key args.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The state encryption key.\n */\nasync function getEncryptionKey({\n mnemonicPhrase,\n snapId,\n}: GetEncryptionKeyArgs) {\n return await deriveEntropy({\n mnemonicPhrase,\n input: snapId,\n salt: STATE_ENCRYPTION_SALT,\n magic: STATE_ENCRYPTION_MAGIC_VALUE,\n });\n}\n\ntype EncryptStateArgs = GetEncryptionKeyArgs & {\n state: Json;\n encryptFunction: ManageStateMethodHooks['encrypt'];\n};\n\n/**\n * Encrypt the state using a deterministic encryption algorithm, based on the\n * snap ID and mnemonic phrase.\n *\n * @param args - The encryption args.\n * @param args.state - The state to encrypt.\n * @param args.encryptFunction - The function to use for encrypting the state.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The encrypted state.\n */\nasync function encryptState({\n state,\n encryptFunction,\n ...keyArgs\n}: EncryptStateArgs) {\n const encryptionKey = await getEncryptionKey(keyArgs);\n return await encryptFunction(encryptionKey, state);\n}\n\ntype DecryptStateArgs = GetEncryptionKeyArgs & {\n state: string;\n decryptFunction: ManageStateMethodHooks['decrypt'];\n};\n\n/**\n * Decrypt the state using a deterministic decryption algorithm, based on the\n * snap ID and mnemonic phrase.\n *\n * @param args - The encryption args.\n * @param args.state - The state to decrypt.\n * @param args.decryptFunction - The function to use for decrypting the state.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The encrypted state.\n */\nasync function decryptState({\n state,\n decryptFunction,\n ...keyArgs\n}: DecryptStateArgs) {\n try {\n const encryptionKey = await getEncryptionKey(keyArgs);\n const decryptedState = await decryptFunction(encryptionKey, state);\n\n assert(isValidJson(decryptedState));\n\n return decryptedState as Record<string, Json>;\n } catch {\n throw rpcErrors.internal({\n message: 'Failed to decrypt snap state, the state must be corrupted.',\n });\n }\n}\n\n/**\n * Builds the method implementation for `snap_manageState`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.clearSnapState - A function that clears the state stored for a\n * snap.\n * @param hooks.getSnapState - A function that fetches the persisted decrypted\n * state for a snap.\n * @param hooks.updateSnapState - A function that updates the state stored for a\n * snap.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @param hooks.encrypt - A function that encrypts the given state.\n * @param hooks.decrypt - A function that decrypts the given state.\n * @returns The method implementation which either returns `null` for a\n * successful state update/deletion or returns the decrypted state.\n * @throws If the params are invalid.\n */\nexport function getManageStateImplementation({\n getMnemonic,\n getUnlockPromise,\n clearSnapState,\n getSnapState,\n updateSnapState,\n encrypt,\n decrypt,\n}: ManageStateMethodHooks) {\n return async function manageState(\n options: RestrictedMethodOptions<ManageStateArgs>,\n ): Promise<null | Record<string, Json>> {\n const {\n params = {},\n method,\n context: { origin },\n } = options;\n const { operation, newState } = getValidatedParams(params, method);\n\n await getUnlockPromise(true);\n const mnemonicPhrase = await getMnemonic();\n\n switch (operation) {\n case ManageStateOperation.ClearState:\n await clearSnapState(origin);\n return null;\n\n case ManageStateOperation.GetState: {\n const state = await getSnapState(origin);\n if (state === null) {\n return state;\n }\n return await decryptState({\n state,\n decryptFunction: decrypt,\n mnemonicPhrase,\n snapId: origin,\n });\n }\n\n case ManageStateOperation.UpdateState: {\n assert(newState);\n\n const encryptedState = await encryptState({\n state: newState,\n encryptFunction: encrypt,\n mnemonicPhrase,\n snapId: origin,\n });\n\n await updateSnapState(origin, encryptedState);\n return null;\n }\n\n default:\n throw rpcErrors.invalidParams(\n `Invalid ${method} operation: \"${operation as string}\"`,\n );\n }\n };\n}\n\n/**\n * Validates the manageState method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @param method - RPC method name used for debugging errors.\n * @param storageSizeLimit - Maximum allowed size (in bytes) of a new state object.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(\n params: unknown,\n method: string,\n storageSizeLimit = STORAGE_SIZE_LIMIT,\n): ManageStateArgs {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { operation, newState } = params;\n\n if (\n !operation ||\n typeof operation !== 'string' ||\n !(Object.values(ManageStateOperation) as string[]).includes(operation)\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid manage state \"operation\".',\n });\n }\n\n if (operation === ManageStateOperation.UpdateState) {\n if (!isObject(newState)) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be a plain object.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n let size;\n try {\n // `getJsonSize` will throw if the state is not JSON serializable.\n size = getJsonSize(newState);\n } catch {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be JSON serializable.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n if (size > storageSizeLimit) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must not exceed ${storageSizeLimit} bytes in size.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n }\n\n return params as ManageStateArgs;\n}\n"],"names":["PermissionType","SubjectType","rpcErrors","STATE_ENCRYPTION_MAGIC_VALUE","isObject","getJsonSize","assert","isValidJson","deriveEntropy","STATE_ENCRYPTION_SALT","methodName","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getManageStateImplementation","subjectTypes","Snap","getMnemonic","getUnlockPromise","clearSnapState","getSnapState","updateSnapState","encrypt","decrypt","manageStateBuilder","Object","freeze","ManageStateOperation","ClearState","GetState","UpdateState","STORAGE_SIZE_LIMIT","getEncryptionKey","mnemonicPhrase","snapId","input","salt","magic","encryptState","state","encryptFunction","keyArgs","encryptionKey","decryptState","decryptFunction","decryptedState","internal","message","manageState","options","params","method","context","origin","operation","newState","getValidatedParams","encryptedState","invalidParams","storageSizeLimit","values","includes","data","receivedNewState","size"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAC9E,SAASC,SAAS,QAAQ,uBAAuB;AAEjD,SAASC,4BAA4B,QAAQ,wBAAwB;AAErE,SAASC,QAAQ,EAAEC,WAAW,EAAEC,MAAM,EAAEC,WAAW,QAAQ,kBAAkB;AAG7E,SAASC,aAAa,QAAQ,WAAW;AAEzC,oDAAoD;AACpD,OAAO,MAAMC,wBAAwB,8BAA8B;AAEnE,MAAMC,aAAa;AAmEnB;;;;;;;;;CASC,GACD,OAAO,MAAMC,uBAIT,CAAC,EACHC,iBAAiB,IAAI,EACrBC,WAAW,EAC4B;IACvC,OAAO;QACLC,gBAAgBd,eAAee,gBAAgB;QAC/CC,YAAYN;QACZE;QACAK,sBAAsBC,6BAA6BL;QACnDM,cAAc;YAAClB,YAAYmB,IAAI;SAAC;IAClC;AACF,EAAE;AAEF,MAAMP,cAAyD;IAC7DQ,aAAa;IACbC,kBAAkB;IAClBC,gBAAgB;IAChBC,cAAc;IACdC,iBAAiB;IACjBC,SAAS;IACTC,SAAS;AACX;AAEA,OAAO,MAAMC,qBAAqBC,OAAOC,MAA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BACE,OAAOR,aAAa,cAAc,cAAcA;gBACpD;YACF;QACF;QAEA,IAAIS;QACJ,IAAI;YACF,kEAAkE;YAClEA,OAAO/D,YAAYsD;QACrB,EAAE,OAAM;YACN,MAAMzD,UAAU4D,aAAa,CAAC;gBAC5BX,SAAS,CAAC,QAAQ,EAAEI,OAAO,kEAAkE,CAAC;gBAC9FW,MAAM;oBACJC,kBACE,OAAOR,aAAa,cAAc,cAAcA;gBACpD;YACF;QACF;QAEA,IAAIS,OAAOL,kBAAkB;YAC3B,MAAM7D,UAAU4D,aAAa,CAAC;gBAC5BX,SAAS,CAAC,QAAQ,EAAEI,OAAO,wDAAwD,EAAEQ,iBAAiB,eAAe,CAAC;gBACtHG,MAAM;oBACJC,kBACE,OAAOR,aAAa,cAAc,cAAcA;gBACpD;YACF;QACF;IACF;IAEA,OAAOL;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../../src/restricted/manageState.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { ManageStateParams, ManageStateResult } from '@metamask/snaps-sdk';\nimport { ManageStateOperation } from '@metamask/snaps-sdk';\nimport { STATE_ENCRYPTION_MAGIC_VALUE, parseJson } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray, Hex } from '@metamask/utils';\nimport { isObject, getJsonSize, assert, isValidJson } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\n// The salt used for SIP-6-based entropy derivation.\nexport const STATE_ENCRYPTION_SALT = 'snap_manageState encryption';\n\nconst methodName = 'snap_manageState';\n\nexport type ManageStateMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * A function that clears the state of the requesting Snap.\n */\n clearSnapState: (snapId: string, encrypted: boolean) => void;\n\n /**\n * A function that gets the encrypted state of the requesting Snap.\n *\n * @returns The current state of the Snap.\n */\n getSnapState: (snapId: string, encrypted: boolean) => string;\n\n /**\n * A function that updates the state of the requesting Snap.\n *\n * @param newState - The new state of the Snap.\n */\n updateSnapState: (\n snapId: string,\n newState: string,\n encrypted: boolean,\n ) => void;\n\n /**\n * Encrypts data with a key. This is assumed to perform symmetric encryption.\n *\n * @param key - The key to use for encryption, in hexadecimal format.\n * @param data - The JSON data to encrypt.\n * @returns The ciphertext as a string. The format for this string is\n * dependent on the implementation, but MUST be a string.\n */\n encrypt: (key: string, data: Json) => Promise<string>;\n\n /**\n * Decrypts data with a key. This is assumed to perform symmetric decryption.\n *\n * @param key - The key to use for decryption, in hexadecimal format.\n * @param cipherText - The ciphertext to decrypt. The format for this string\n * is dependent on the implementation, but MUST be a string.\n * @returns The decrypted data as a JSON object.\n */\n decrypt: (key: Hex, cipherText: string) => Promise<unknown>;\n};\n\ntype ManageStateSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: ManageStateMethodHooks;\n};\n\ntype ManageStateSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getManageStateImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_manageState` permission.\n * `snap_manageState` lets the Snap store and manage some of its state on\n * your device.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_manageState` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n ManageStateSpecificationBuilderOptions,\n ManageStateSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: ManageStateSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getManageStateImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<ManageStateMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n clearSnapState: true,\n getSnapState: true,\n updateSnapState: true,\n encrypt: true,\n decrypt: true,\n};\n\nexport const manageStateBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport const STORAGE_SIZE_LIMIT = 104857600; // In bytes (100MB)\n\ntype GetEncryptionKeyArgs = {\n snapId: string;\n mnemonicPhrase: Uint8Array;\n};\n\n/**\n * Get a deterministic encryption key to use for encrypting and decrypting the\n * state.\n *\n * This key should only be used for state encryption using `snap_manageState`.\n * To get other encryption keys, a different salt can be used.\n *\n * @param args - The encryption key args.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The state encryption key.\n */\nasync function getEncryptionKey({\n mnemonicPhrase,\n snapId,\n}: GetEncryptionKeyArgs) {\n return await deriveEntropy({\n mnemonicPhrase,\n input: snapId,\n salt: STATE_ENCRYPTION_SALT,\n magic: STATE_ENCRYPTION_MAGIC_VALUE,\n });\n}\n\ntype EncryptStateArgs = GetEncryptionKeyArgs & {\n state: Json;\n encryptFunction: ManageStateMethodHooks['encrypt'];\n};\n\n/**\n * Encrypt the state using a deterministic encryption algorithm, based on the\n * snap ID and mnemonic phrase.\n *\n * @param args - The encryption args.\n * @param args.state - The state to encrypt.\n * @param args.encryptFunction - The function to use for encrypting the state.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The encrypted state.\n */\nasync function encryptState({\n state,\n encryptFunction,\n ...keyArgs\n}: EncryptStateArgs) {\n const encryptionKey = await getEncryptionKey(keyArgs);\n return await encryptFunction(encryptionKey, state);\n}\n\ntype DecryptStateArgs = GetEncryptionKeyArgs & {\n state: string;\n decryptFunction: ManageStateMethodHooks['decrypt'];\n};\n\n/**\n * Decrypt the state using a deterministic decryption algorithm, based on the\n * snap ID and mnemonic phrase.\n *\n * @param args - The encryption args.\n * @param args.state - The state to decrypt.\n * @param args.decryptFunction - The function to use for decrypting the state.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The encrypted state.\n */\nasync function decryptState({\n state,\n decryptFunction,\n ...keyArgs\n}: DecryptStateArgs) {\n try {\n const encryptionKey = await getEncryptionKey(keyArgs);\n const decryptedState = await decryptFunction(encryptionKey, state);\n\n assert(isValidJson(decryptedState));\n\n return decryptedState as Record<string, Json>;\n } catch {\n throw rpcErrors.internal({\n message: 'Failed to decrypt snap state, the state must be corrupted.',\n });\n }\n}\n\n/**\n * Builds the method implementation for `snap_manageState`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.clearSnapState - A function that clears the state stored for a\n * snap.\n * @param hooks.getSnapState - A function that fetches the persisted decrypted\n * state for a snap.\n * @param hooks.updateSnapState - A function that updates the state stored for a\n * snap.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @param hooks.encrypt - A function that encrypts the given state.\n * @param hooks.decrypt - A function that decrypts the given state.\n * @returns The method implementation which either returns `null` for a\n * successful state update/deletion or returns the decrypted state.\n * @throws If the params are invalid.\n */\nexport function getManageStateImplementation({\n getMnemonic,\n getUnlockPromise,\n clearSnapState,\n getSnapState,\n updateSnapState,\n encrypt,\n decrypt,\n}: ManageStateMethodHooks) {\n return async function manageState(\n options: RestrictedMethodOptions<ManageStateParams>,\n ): Promise<ManageStateResult> {\n const {\n params = {},\n method,\n context: { origin },\n } = options;\n const validatedParams = getValidatedParams(params, method);\n\n // If the encrypted param is undefined or null we default to true.\n const shouldEncrypt = validatedParams.encrypted ?? true;\n\n // We only need to prompt the user when the mnemonic is needed\n // which it isn't for the clear operation or unencrypted storage.\n if (\n shouldEncrypt &&\n validatedParams.operation !== ManageStateOperation.ClearState\n ) {\n await getUnlockPromise(true);\n }\n\n switch (validatedParams.operation) {\n case ManageStateOperation.ClearState:\n clearSnapState(origin, shouldEncrypt);\n return null;\n\n case ManageStateOperation.GetState: {\n const state = getSnapState(origin, shouldEncrypt);\n if (state === null) {\n return state;\n }\n return shouldEncrypt\n ? await decryptState({\n state,\n decryptFunction: decrypt,\n mnemonicPhrase: await getMnemonic(),\n snapId: origin,\n })\n : parseJson<Record<string, Json>>(state);\n }\n\n case ManageStateOperation.UpdateState: {\n const finalizedState = shouldEncrypt\n ? await encryptState({\n state: validatedParams.newState,\n encryptFunction: encrypt,\n mnemonicPhrase: await getMnemonic(),\n snapId: origin,\n })\n : JSON.stringify(validatedParams.newState);\n\n updateSnapState(origin, finalizedState, shouldEncrypt);\n return null;\n }\n\n default:\n throw rpcErrors.invalidParams(\n `Invalid ${method} operation: \"${\n validatedParams.operation as string\n }\"`,\n );\n }\n };\n}\n\n/**\n * Validates the manageState method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @param method - RPC method name used for debugging errors.\n * @param storageSizeLimit - Maximum allowed size (in bytes) of a new state object.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(\n params: unknown,\n method: string,\n storageSizeLimit = STORAGE_SIZE_LIMIT,\n): ManageStateParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { operation, newState, encrypted } = params;\n\n if (\n !operation ||\n typeof operation !== 'string' ||\n !Object.values(ManageStateOperation).includes(\n operation as ManageStateOperation,\n )\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid manage state \"operation\".',\n });\n }\n\n if (encrypted !== undefined && typeof encrypted !== 'boolean') {\n throw rpcErrors.invalidParams({\n message: '\"encrypted\" parameter must be a boolean if specified.',\n });\n }\n\n if (operation === ManageStateOperation.UpdateState) {\n if (!isObject(newState)) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be a plain object.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n let size;\n try {\n // `getJsonSize` will throw if the state is not JSON serializable.\n size = getJsonSize(newState);\n } catch {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be JSON serializable.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n if (size > storageSizeLimit) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must not exceed ${storageSizeLimit} bytes in size.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n }\n\n return params as ManageStateParams;\n}\n"],"names":["PermissionType","SubjectType","rpcErrors","ManageStateOperation","STATE_ENCRYPTION_MAGIC_VALUE","parseJson","isObject","getJsonSize","assert","isValidJson","deriveEntropy","STATE_ENCRYPTION_SALT","methodName","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getManageStateImplementation","subjectTypes","Snap","getMnemonic","getUnlockPromise","clearSnapState","getSnapState","updateSnapState","encrypt","decrypt","manageStateBuilder","Object","freeze","STORAGE_SIZE_LIMIT","getEncryptionKey","mnemonicPhrase","snapId","input","salt","magic","encryptState","state","encryptFunction","keyArgs","encryptionKey","decryptState","decryptFunction","decryptedState","internal","message","manageState","options","params","method","context","origin","validatedParams","getValidatedParams","shouldEncrypt","encrypted","operation","ClearState","GetState","UpdateState","finalizedState","newState","JSON","stringify","invalidParams","storageSizeLimit","values","includes","undefined","data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SAAS,CAAC,QAAQ,EAAEI,OAAO,kEAAkE,CAAC;gBAC9FoB,MAAM;oBACJC,kBACE,OAAOT,aAAa,cAAc,cAAcA;gBACpD;YACF;QACF;QAEA,IAAIU,OAAON,kBAAkB;YAC3B,MAAMnE,UAAUkE,aAAa,CAAC;gBAC5BnB,SAAS,CAAC,QAAQ,EAAEI,OAAO,wDAAwD,EAAEgB,iBAAiB,eAAe,CAAC;gBACtHI,MAAM;oBACJC,kBACE,OAAOT,aAAa,cAAc,cAAcA;gBACpD;YACF;QACF;IACF;IAEA,OAAOb;AACT"}
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
2
|
import { rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
-
import {
|
|
3
|
+
import { NotificationType } from '@metamask/snaps-sdk';
|
|
4
|
+
import { validateTextLinks } from '@metamask/snaps-utils';
|
|
4
5
|
import { isObject } from '@metamask/utils';
|
|
5
6
|
const methodName = 'snap_notify';
|
|
6
|
-
export var NotificationType;
|
|
7
|
-
(function(NotificationType) {
|
|
8
|
-
NotificationType["InApp"] = 'inApp';
|
|
9
|
-
NotificationType["Native"] = 'native';
|
|
10
|
-
})(NotificationType || (NotificationType = {}));
|
|
11
7
|
/**
|
|
12
8
|
* The specification builder for the `snap_notify` permission.
|
|
13
9
|
* `snap_notify` allows snaps to send multiple types of notifications to its users.
|
|
@@ -53,7 +49,7 @@ export const notifyBuilder = Object.freeze({
|
|
|
53
49
|
const { params, context: { origin } } = args;
|
|
54
50
|
const validatedParams = getValidatedParams(params);
|
|
55
51
|
await maybeUpdatePhishingList();
|
|
56
|
-
|
|
52
|
+
validateTextLinks(validatedParams.message, isOnPhishingList);
|
|
57
53
|
switch(validatedParams.type){
|
|
58
54
|
case NotificationType.Native:
|
|
59
55
|
return await showNativeNotification(origin, validatedParams);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/restricted/notify.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../src/restricted/notify.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { NotificationType } from '@metamask/snaps-sdk';\nimport type {\n NotifyParams,\n NotifyResult,\n EnumToUnion,\n} from '@metamask/snaps-sdk';\nimport { validateTextLinks } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\nexport type NotificationArgs = {\n /**\n * Enum type to determine notification type.\n */\n type: EnumToUnion<NotificationType>;\n\n /**\n * A message to show on the notification.\n */\n message: string;\n};\n\nexport type NotifyMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showNativeNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showInAppNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n isOnPhishingList: (url: string) => boolean;\n\n maybeUpdatePhishingList: () => Promise<void>;\n};\n\ntype SpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: NotifyMethodHooks;\n};\n\ntype Specification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_notify` permission.\n * `snap_notify` allows snaps to send multiple types of notifications to its users.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_notify` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n SpecificationBuilderOptions,\n Specification\n> = ({ allowedCaveats = null, methodHooks }: SpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<NotifyMethodHooks> = {\n showNativeNotification: true,\n showInAppNotification: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n};\n\nexport const notifyBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_notify`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showNativeNotification - A function that shows a native browser notification.\n * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.\n * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @returns The method implementation which returns `null` on success.\n * @throws If the params are invalid.\n */\nexport function getImplementation({\n showNativeNotification,\n showInAppNotification,\n isOnPhishingList,\n maybeUpdatePhishingList,\n}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotifyParams>,\n ): Promise<NotifyResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedParams = getValidatedParams(params);\n\n await maybeUpdatePhishingList();\n\n validateTextLinks(validatedParams.message, isOnPhishingList);\n\n switch (validatedParams.type) {\n case NotificationType.Native:\n return await showNativeNotification(origin, validatedParams);\n case NotificationType.InApp:\n return await showInAppNotification(origin, validatedParams);\n default:\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n };\n}\n\n/**\n * Validates the notify method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(params: unknown): NotifyParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { type, message } = params;\n\n if (\n !type ||\n typeof type !== 'string' ||\n !Object.values(NotificationType).includes(type as NotificationType)\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n\n // Set to the max message length on a Mac notification for now.\n if (!message || typeof message !== 'string' || message.length >= 50) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n return params as NotificationArgs;\n}\n"],"names":["PermissionType","SubjectType","rpcErrors","NotificationType","validateTextLinks","isObject","methodName","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getImplementation","subjectTypes","Snap","showNativeNotification","showInAppNotification","isOnPhishingList","maybeUpdatePhishingList","notifyBuilder","Object","freeze","implementation","args","params","context","origin","validatedParams","getValidatedParams","message","type","Native","InApp","invalidParams","values","includes","length"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAC9E,SAASC,SAAS,QAAQ,uBAAuB;AACjD,SAASC,gBAAgB,QAAQ,sBAAsB;AAMvD,SAASC,iBAAiB,QAAQ,wBAAwB;AAE1D,SAASC,QAAQ,QAAQ,kBAAkB;AAI3C,MAAMC,aAAa;AAkDnB;;;;;;;;CAQC,GACD,OAAO,MAAMC,uBAIT,CAAC,EAAEC,iBAAiB,IAAI,EAAEC,WAAW,EAA+B;IACtE,OAAO;QACLC,gBAAgBV,eAAeW,gBAAgB;QAC/CC,YAAYN;QACZE;QACAK,sBAAsBC,kBAAkBL;QACxCM,cAAc;YAACd,YAAYe,IAAI;SAAC;IAClC;AACF,EA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;QACJ;IACF;IAEA,OAAOL;AACT"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,4 +3,3 @@ export { handlers as permittedMethods, createSnapsMethodMiddleware, } from './pe
|
|
|
3
3
|
export * from './restricted';
|
|
4
4
|
export { SnapCaveatType } from '@metamask/snaps-utils';
|
|
5
5
|
export { selectHooks } from './utils';
|
|
6
|
-
export type { RequestFunction, SnapsGlobalObject } from './request';
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export type { InstallSnapsResult } from '@metamask/snaps-utils';
|
|
4
|
-
export declare type InstallSnapsHook = (requestedSnaps: RequestedPermissions) => Promise<InstallSnapsResult>;
|
|
1
|
+
import type { RequestSnapsParams, RequestSnapsResult } from '@metamask/snaps-sdk';
|
|
2
|
+
export declare type InstallSnapsHook = (requestedSnaps: RequestSnapsParams) => Promise<RequestSnapsResult>;
|
|
5
3
|
/**
|
|
6
4
|
* Typechecks the requested snaps and passes them to the permissions
|
|
7
5
|
* controller for installation.
|
|
@@ -13,4 +11,4 @@ export declare type InstallSnapsHook = (requestedSnaps: RequestedPermissions) =>
|
|
|
13
11
|
* @returns An object containing the installed snaps.
|
|
14
12
|
* @throws If the params are invalid or the snap installation fails.
|
|
15
13
|
*/
|
|
16
|
-
export declare function handleInstallSnaps(requestedSnaps:
|
|
14
|
+
export declare function handleInstallSnaps(requestedSnaps: RequestSnapsParams, installSnaps: InstallSnapsHook): Promise<RequestSnapsResult>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
-
import type {
|
|
2
|
+
import type { GetFileParams } from '@metamask/snaps-sdk';
|
|
3
|
+
import type { InferMatching } from '@metamask/snaps-utils';
|
|
3
4
|
export declare const GetFileArgsStruct: import("superstruct").Struct<{
|
|
4
5
|
path: string;
|
|
5
6
|
encoding?: "base64" | "utf8" | "hex" | undefined;
|
|
@@ -7,8 +8,8 @@ export declare const GetFileArgsStruct: import("superstruct").Struct<{
|
|
|
7
8
|
path: import("superstruct").Struct<string, null>;
|
|
8
9
|
encoding: import("superstruct").Struct<"base64" | "utf8" | "hex" | undefined, null>;
|
|
9
10
|
}>;
|
|
10
|
-
export declare type
|
|
11
|
-
export declare const getFileHandler: PermittedHandlerExport<GetFileHooks,
|
|
11
|
+
export declare type InferredGetFileParams = InferMatching<typeof GetFileArgsStruct, GetFileParams>;
|
|
12
|
+
export declare const getFileHandler: PermittedHandlerExport<GetFileHooks, InferredGetFileParams, string>;
|
|
12
13
|
export declare type GetFileHooks = {
|
|
13
|
-
getSnapFile: (path:
|
|
14
|
+
getSnapFile: (path: InferredGetFileParams['path'], encoding: InferredGetFileParams['encoding']) => Promise<string>;
|
|
14
15
|
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
-
import type {
|
|
2
|
+
import type { GetSnapsResult } from '@metamask/snaps-sdk';
|
|
3
3
|
import type { JsonRpcParams } from '@metamask/utils';
|
|
4
4
|
/**
|
|
5
5
|
* `wallet_getSnaps` gets the requester's permitted and installed Snaps.
|
|
6
6
|
*/
|
|
7
|
-
export declare const getSnapsHandler: PermittedHandlerExport<GetSnapsHooks, JsonRpcParams,
|
|
7
|
+
export declare const getSnapsHandler: PermittedHandlerExport<GetSnapsHooks, JsonRpcParams, GetSnapsResult>;
|
|
8
8
|
export declare type GetSnapsHooks = {
|
|
9
9
|
/**
|
|
10
10
|
* @returns The permitted and installed snaps for the requesting origin.
|
|
11
11
|
*/
|
|
12
|
-
getSnaps: () => Promise<
|
|
12
|
+
getSnaps: () => Promise<GetSnapsResult>;
|
|
13
13
|
};
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
export declare const methodHandlers: {
|
|
2
|
-
wallet_getSnaps: import("@metamask/permission-controller").PermittedHandlerExport<import("./getSnaps").GetSnapsHooks, import("@metamask/utils").JsonRpcParams, import("@metamask/snaps-
|
|
3
|
-
wallet_requestSnaps: import("@metamask/permission-controller").PermittedHandlerExport<import("./requestSnaps").RequestSnapsHooks, import("@metamask/
|
|
4
|
-
wallet_invokeSnap: import("@metamask/permission-controller").PermittedHandlerExport<void, import("
|
|
5
|
-
wallet_invokeKeyring: import("@metamask/permission-controller").PermittedHandlerExport<import("./invokeKeyring").InvokeKeyringHooks, import("@metamask/
|
|
6
|
-
snap_getFile: import("@metamask/permission-controller").PermittedHandlerExport<import("./getFile").GetFileHooks,
|
|
7
|
-
path: string;
|
|
8
|
-
encoding?: "base64" | "utf8" | "hex" | undefined;
|
|
9
|
-
}, string>;
|
|
2
|
+
wallet_getSnaps: import("@metamask/permission-controller").PermittedHandlerExport<import("./getSnaps").GetSnapsHooks, import("@metamask/utils").JsonRpcParams, import("@metamask/snaps-sdk").GetSnapsResult>;
|
|
3
|
+
wallet_requestSnaps: import("@metamask/permission-controller").PermittedHandlerExport<import("./requestSnaps").RequestSnapsHooks, import("@metamask/snaps-sdk").RequestSnapsParams, import("@metamask/snaps-sdk").RequestSnapsResult>;
|
|
4
|
+
wallet_invokeSnap: import("@metamask/permission-controller").PermittedHandlerExport<void, import("@metamask/snaps-sdk").InvokeSnapParams, import("@metamask/utils").Json>;
|
|
5
|
+
wallet_invokeKeyring: import("@metamask/permission-controller").PermittedHandlerExport<import("./invokeKeyring").InvokeKeyringHooks, import("@metamask/snaps-sdk").InvokeSnapParams, import("@metamask/utils").Json>;
|
|
6
|
+
snap_getFile: import("@metamask/permission-controller").PermittedHandlerExport<import("./getFile").GetFileHooks, import("@metamask/snaps-sdk").GetFileParams, string>;
|
|
10
7
|
};
|
|
11
|
-
export declare const handlers: (import("@metamask/permission-controller").PermittedHandlerExport<import("./getSnaps").GetSnapsHooks, import("@metamask/utils").JsonRpcParams, import("@metamask/snaps-
|
|
12
|
-
path: string;
|
|
13
|
-
encoding?: "base64" | "utf8" | "hex" | undefined;
|
|
14
|
-
}, string> | import("@metamask/permission-controller").PermittedHandlerExport<void, import("./invokeSnapSugar").InvokeSnapSugarArgs, import("@metamask/utils").Json> | import("@metamask/permission-controller").PermittedHandlerExport<import("./invokeKeyring").InvokeKeyringHooks, import("@metamask/utils").JsonRpcRequest<import("@metamask/utils").JsonRpcParams>, import("@metamask/utils").Json>)[];
|
|
8
|
+
export declare const handlers: (import("@metamask/permission-controller").PermittedHandlerExport<import("./getSnaps").GetSnapsHooks, import("@metamask/utils").JsonRpcParams, import("@metamask/snaps-sdk").GetSnapsResult> | import("@metamask/permission-controller").PermittedHandlerExport<import("./requestSnaps").RequestSnapsHooks, import("@metamask/snaps-sdk").RequestSnapsParams, import("@metamask/snaps-sdk").RequestSnapsResult> | import("@metamask/permission-controller").PermittedHandlerExport<import("./getFile").GetFileHooks, import("@metamask/snaps-sdk").GetFileParams, string> | import("@metamask/permission-controller").PermittedHandlerExport<void, import("@metamask/snaps-sdk").InvokeSnapParams, import("@metamask/utils").Json> | import("@metamask/permission-controller").PermittedHandlerExport<import("./invokeKeyring").InvokeKeyringHooks, import("@metamask/snaps-sdk").InvokeSnapParams, import("@metamask/utils").Json>)[];
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
4
|
-
import type { JsonRpcRequest } from '@metamask/utils';
|
|
5
|
-
import { type Json } from '@metamask/utils';
|
|
2
|
+
import type { InvokeKeyringResult, InvokeSnapParams } from '@metamask/snaps-sdk';
|
|
3
|
+
import type { Snap, SnapRpcHookArgs } from '@metamask/snaps-utils';
|
|
6
4
|
/**
|
|
7
5
|
* `wallet_invokeKeyring` gets the requester's permitted and installed Snaps.
|
|
8
6
|
*/
|
|
9
|
-
export declare const invokeKeyringHandler: PermittedHandlerExport<InvokeKeyringHooks,
|
|
7
|
+
export declare const invokeKeyringHandler: PermittedHandlerExport<InvokeKeyringHooks, InvokeSnapParams, InvokeKeyringResult>;
|
|
10
8
|
export declare type InvokeKeyringHooks = {
|
|
11
9
|
hasPermission: (origin: string, permissionName: string) => boolean;
|
|
12
10
|
handleSnapRpcRequest: ({ snapId, origin, handler, request, }: SnapRpcHookArgs & {
|
|
13
|
-
snapId:
|
|
11
|
+
snapId: string;
|
|
14
12
|
}) => Promise<unknown>;
|
|
15
|
-
getSnap: (snapId:
|
|
13
|
+
getSnap: (snapId: string) => Snap | undefined;
|
|
16
14
|
getAllowedKeyringMethods: (origin: string) => string[];
|
|
17
15
|
};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
|
|
2
2
|
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
snapId: string;
|
|
6
|
-
request: Record<string, Json>;
|
|
7
|
-
};
|
|
3
|
+
import type { InvokeSnapParams, InvokeSnapResult } from '@metamask/snaps-sdk';
|
|
4
|
+
import type { JsonRpcRequest } from '@metamask/utils';
|
|
8
5
|
/**
|
|
9
6
|
* `wallet_invokeSnap` attempts to invoke an RPC method of the specified Snap.
|
|
10
7
|
*/
|
|
11
|
-
export declare const invokeSnapSugarHandler: PermittedHandlerExport<void,
|
|
8
|
+
export declare const invokeSnapSugarHandler: PermittedHandlerExport<void, InvokeSnapParams, InvokeSnapResult>;
|
|
12
9
|
/**
|
|
13
10
|
* The `wallet_invokeSnap` method implementation.
|
|
14
11
|
* Reroutes incoming JSON-RPC requests that are targeting snaps, by modifying the method and params.
|
|
@@ -21,7 +18,7 @@ export declare const invokeSnapSugarHandler: PermittedHandlerExport<void, Invoke
|
|
|
21
18
|
* @returns Nothing.
|
|
22
19
|
* @throws If the params are invalid.
|
|
23
20
|
*/
|
|
24
|
-
export declare function invokeSnapSugar(req: JsonRpcRequest<
|
|
21
|
+
export declare function invokeSnapSugar(req: JsonRpcRequest<InvokeSnapParams>, _res: unknown, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback): void;
|
|
25
22
|
/**
|
|
26
23
|
* Validates the wallet_invokeSnap method `params` and returns them cast to the correct
|
|
27
24
|
* type. Throws if validation fails.
|
|
@@ -29,4 +26,4 @@ export declare function invokeSnapSugar(req: JsonRpcRequest<InvokeSnapSugarArgs>
|
|
|
29
26
|
* @param params - The unvalidated params object from the method request.
|
|
30
27
|
* @returns The validated method parameter object.
|
|
31
28
|
*/
|
|
32
|
-
export declare function getValidatedParams(params: unknown):
|
|
29
|
+
export declare function getValidatedParams(params: unknown): InvokeSnapParams;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { PermissionConstraint, RequestedPermissions, PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
+
import type { RequestSnapsParams, RequestSnapsResult } from '@metamask/snaps-sdk';
|
|
2
3
|
import type { SnapsPermissionRequest } from '@metamask/snaps-utils';
|
|
3
|
-
import type { InstallSnapsHook
|
|
4
|
+
import type { InstallSnapsHook } from './common/snapInstallation';
|
|
4
5
|
/**
|
|
5
6
|
* `wallet_requestSnaps` installs the requested Snaps and requests permission to use them if necessary.
|
|
6
7
|
*/
|
|
7
|
-
export declare const requestSnapsHandler: PermittedHandlerExport<RequestSnapsHooks,
|
|
8
|
+
export declare const requestSnapsHandler: PermittedHandlerExport<RequestSnapsHooks, RequestSnapsParams, RequestSnapsResult>;
|
|
8
9
|
export declare type RequestSnapsHooks = {
|
|
9
10
|
/**
|
|
10
11
|
* Installs the requested snaps if they are permitted.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PermissionConstraint, RestrictedMethodCaveatSpecificationConstraint, Caveat } from '@metamask/permission-controller';
|
|
2
|
+
import type { GetBip44EntropyParams } from '@metamask/snaps-sdk';
|
|
2
3
|
import { SnapCaveatType } from '@metamask/snaps-utils';
|
|
3
4
|
import type { Json } from '@metamask/utils';
|
|
4
|
-
import type { GetBip44EntropyParams } from '../getBip44Entropy';
|
|
5
5
|
/**
|
|
6
6
|
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
7
7
|
* Note that this function does not do any validation, that's handled by the
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from '@metamask/permission-controller';
|
|
2
2
|
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
-
import
|
|
4
|
-
import type { EnumToUnion } from '@metamask/snaps-
|
|
3
|
+
import { DialogType } from '@metamask/snaps-sdk';
|
|
4
|
+
import type { DialogParams, EnumToUnion, Component } from '@metamask/snaps-sdk';
|
|
5
|
+
import type { InferMatching } from '@metamask/snaps-utils';
|
|
5
6
|
import type { NonEmptyArray } from '@metamask/utils';
|
|
6
7
|
import type { Infer, Struct } from 'superstruct';
|
|
7
8
|
import { type MethodHooksObject } from '../utils';
|
|
8
9
|
declare const methodName = "snap_dialog";
|
|
9
|
-
export declare enum DialogType {
|
|
10
|
-
Alert = "alert",
|
|
11
|
-
Confirmation = "confirmation",
|
|
12
|
-
Prompt = "prompt"
|
|
13
|
-
}
|
|
14
10
|
declare const PlaceholderStruct: Struct<string | undefined, null>;
|
|
15
11
|
export declare type Placeholder = Infer<typeof PlaceholderStruct>;
|
|
16
12
|
declare type ShowDialog = (snapId: string, type: EnumToUnion<DialogType>, content: Component, placeholder?: Placeholder) => Promise<null | boolean | string>;
|
|
@@ -46,67 +42,70 @@ export declare const dialogBuilder: Readonly<{
|
|
|
46
42
|
}>;
|
|
47
43
|
declare const DialogParametersStruct: Struct<{
|
|
48
44
|
type: "alert";
|
|
49
|
-
content: import("@metamask/snaps-
|
|
45
|
+
content: import("@metamask/snaps-sdk").Panel | {
|
|
50
46
|
value: string;
|
|
51
|
-
type: import("@metamask/snaps-
|
|
47
|
+
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
48
|
+
sensitive?: boolean | undefined;
|
|
52
49
|
} | {
|
|
53
|
-
type: import("@metamask/snaps-
|
|
50
|
+
type: import("@metamask/snaps-sdk").NodeType.Divider;
|
|
54
51
|
} | {
|
|
55
52
|
value: string;
|
|
56
|
-
type: import("@metamask/snaps-
|
|
57
|
-
} | {
|
|
58
|
-
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
53
|
+
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
59
54
|
} | {
|
|
60
55
|
value: string;
|
|
61
|
-
type: import("@metamask/snaps-
|
|
62
|
-
|
|
56
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
57
|
+
} | {
|
|
58
|
+
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
63
59
|
} | {
|
|
64
60
|
value: string;
|
|
65
|
-
type: import("@metamask/snaps-
|
|
61
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
62
|
+
markdown?: boolean | undefined;
|
|
66
63
|
};
|
|
67
64
|
} | {
|
|
68
65
|
type: "confirmation";
|
|
69
|
-
content: import("@metamask/snaps-
|
|
66
|
+
content: import("@metamask/snaps-sdk").Panel | {
|
|
70
67
|
value: string;
|
|
71
|
-
type: import("@metamask/snaps-
|
|
68
|
+
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
69
|
+
sensitive?: boolean | undefined;
|
|
72
70
|
} | {
|
|
73
|
-
type: import("@metamask/snaps-
|
|
71
|
+
type: import("@metamask/snaps-sdk").NodeType.Divider;
|
|
74
72
|
} | {
|
|
75
73
|
value: string;
|
|
76
|
-
type: import("@metamask/snaps-
|
|
77
|
-
} | {
|
|
78
|
-
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
74
|
+
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
79
75
|
} | {
|
|
80
76
|
value: string;
|
|
81
|
-
type: import("@metamask/snaps-
|
|
82
|
-
|
|
77
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
78
|
+
} | {
|
|
79
|
+
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
83
80
|
} | {
|
|
84
81
|
value: string;
|
|
85
|
-
type: import("@metamask/snaps-
|
|
82
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
83
|
+
markdown?: boolean | undefined;
|
|
86
84
|
};
|
|
87
85
|
} | {
|
|
88
86
|
type: "prompt";
|
|
89
|
-
content: import("@metamask/snaps-
|
|
87
|
+
content: import("@metamask/snaps-sdk").Panel | {
|
|
90
88
|
value: string;
|
|
91
|
-
type: import("@metamask/snaps-
|
|
89
|
+
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
90
|
+
sensitive?: boolean | undefined;
|
|
92
91
|
} | {
|
|
93
|
-
type: import("@metamask/snaps-
|
|
92
|
+
type: import("@metamask/snaps-sdk").NodeType.Divider;
|
|
94
93
|
} | {
|
|
95
94
|
value: string;
|
|
96
|
-
type: import("@metamask/snaps-
|
|
97
|
-
} | {
|
|
98
|
-
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
95
|
+
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
99
96
|
} | {
|
|
100
97
|
value: string;
|
|
101
|
-
type: import("@metamask/snaps-
|
|
102
|
-
|
|
98
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
99
|
+
} | {
|
|
100
|
+
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
103
101
|
} | {
|
|
104
102
|
value: string;
|
|
105
|
-
type: import("@metamask/snaps-
|
|
103
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
104
|
+
markdown?: boolean | undefined;
|
|
106
105
|
};
|
|
107
106
|
placeholder?: string | undefined;
|
|
108
107
|
}, null>;
|
|
109
|
-
export declare type DialogParameters =
|
|
108
|
+
export declare type DialogParameters = InferMatching<typeof DialogParametersStruct, DialogParams>;
|
|
110
109
|
/**
|
|
111
110
|
* Builds the method implementation for `snap_dialog`.
|
|
112
111
|
*
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { JsonSLIP10Node } from '@metamask/key-tree';
|
|
2
1
|
import type { PermissionSpecificationBuilder, PermissionValidatorConstraint, RestrictedMethodOptions } from '@metamask/permission-controller';
|
|
3
2
|
import { PermissionType } from '@metamask/permission-controller';
|
|
4
|
-
import type {
|
|
3
|
+
import type { GetBip32EntropyParams, GetBip32EntropyResult } from '@metamask/snaps-sdk';
|
|
5
4
|
import type { NonEmptyArray } from '@metamask/utils';
|
|
6
5
|
import type { MethodHooksObject } from '../utils';
|
|
7
6
|
declare const targetName = "snap_getBip32Entropy";
|
|
@@ -41,5 +40,5 @@ export declare const getBip32EntropyBuilder: Readonly<{
|
|
|
41
40
|
* @returns The method implementation which returns a `JsonSLIP10Node`.
|
|
42
41
|
* @throws If the params are invalid.
|
|
43
42
|
*/
|
|
44
|
-
export declare function getBip32EntropyImplementation({ getMnemonic, getUnlockPromise, }: GetBip32EntropyMethodHooks): (args: RestrictedMethodOptions<
|
|
43
|
+
export declare function getBip32EntropyImplementation({ getMnemonic, getUnlockPromise, }: GetBip32EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip32EntropyParams>) => Promise<GetBip32EntropyResult>;
|
|
45
44
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PermissionSpecificationBuilder, PermissionValidatorConstraint, RestrictedMethodOptions } from '@metamask/permission-controller';
|
|
2
2
|
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { GetBip32PublicKeyParams, GetBip32PublicKeyResult } from '@metamask/snaps-sdk';
|
|
3
4
|
import type { NonEmptyArray } from '@metamask/utils';
|
|
4
5
|
import type { MethodHooksObject } from '../utils';
|
|
5
6
|
declare const targetName = "snap_getBip32PublicKey";
|
|
@@ -18,11 +19,6 @@ export declare type GetBip32PublicKeyMethodHooks = {
|
|
|
18
19
|
declare type GetBip32PublicKeySpecificationBuilderOptions = {
|
|
19
20
|
methodHooks: GetBip32PublicKeyMethodHooks;
|
|
20
21
|
};
|
|
21
|
-
declare type GetBip32PublicKeyParameters = {
|
|
22
|
-
path: ['m', ...(`${number}` | `${number}'`)[]];
|
|
23
|
-
curve: 'secp256k1' | 'ed25519';
|
|
24
|
-
compressed?: boolean;
|
|
25
|
-
};
|
|
26
22
|
export declare const Bip32PublicKeyArgsStruct: import("superstruct").Struct<{
|
|
27
23
|
path: string[];
|
|
28
24
|
curve: "ed25519" | "secp256k1";
|
|
@@ -56,5 +52,5 @@ export declare const getBip32PublicKeyBuilder: Readonly<{
|
|
|
56
52
|
* @returns The method implementation which returns a public key.
|
|
57
53
|
* @throws If the params are invalid.
|
|
58
54
|
*/
|
|
59
|
-
export declare function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, }: GetBip32PublicKeyMethodHooks): (args: RestrictedMethodOptions<
|
|
55
|
+
export declare function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, }: GetBip32PublicKeyMethodHooks): (args: RestrictedMethodOptions<GetBip32PublicKeyParams>) => Promise<GetBip32PublicKeyResult>;
|
|
60
56
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { JsonBIP44CoinTypeNode } from '@metamask/key-tree';
|
|
2
1
|
import type { PermissionSpecificationBuilder, PermissionValidatorConstraint, RestrictedMethodOptions } from '@metamask/permission-controller';
|
|
3
2
|
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { GetBip44EntropyParams, GetBip44EntropyResult } from '@metamask/snaps-sdk';
|
|
4
4
|
import type { NonEmptyArray } from '@metamask/utils';
|
|
5
5
|
import type { MethodHooksObject } from '../utils';
|
|
6
6
|
declare const targetName = "snap_getBip44Entropy";
|
|
@@ -19,9 +19,6 @@ export declare type GetBip44EntropyMethodHooks = {
|
|
|
19
19
|
declare type GetBip44EntropySpecificationBuilderOptions = {
|
|
20
20
|
methodHooks: GetBip44EntropyMethodHooks;
|
|
21
21
|
};
|
|
22
|
-
export declare type GetBip44EntropyParams = {
|
|
23
|
-
coinType: number;
|
|
24
|
-
};
|
|
25
22
|
export declare const getBip44EntropyBuilder: Readonly<{
|
|
26
23
|
readonly targetName: "snap_getBip44Entropy";
|
|
27
24
|
readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, GetBip44EntropySpecificationBuilderOptions, {
|
|
@@ -45,5 +42,5 @@ export declare const getBip44EntropyBuilder: Readonly<{
|
|
|
45
42
|
* @returns The method implementation which returns a `BIP44CoinTypeNode`.
|
|
46
43
|
* @throws If the params are invalid.
|
|
47
44
|
*/
|
|
48
|
-
export declare function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, }: GetBip44EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip44EntropyParams>) => Promise<
|
|
45
|
+
export declare function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, }: GetBip44EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip44EntropyParams>) => Promise<GetBip44EntropyResult>;
|
|
49
46
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from '@metamask/permission-controller';
|
|
2
2
|
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
-
import type {
|
|
3
|
+
import type { GetEntropyParams, GetEntropyResult } from '@metamask/snaps-sdk';
|
|
4
|
+
import type { NonEmptyArray } from '@metamask/utils';
|
|
4
5
|
import type { Infer } from 'superstruct';
|
|
5
6
|
import type { MethodHooksObject } from '../utils';
|
|
6
7
|
declare const targetName = "snap_getEntropy";
|
|
@@ -56,5 +57,5 @@ export declare type GetEntropyHooks = {
|
|
|
56
57
|
* once the extension is unlocked.
|
|
57
58
|
* @returns The method implementation.
|
|
58
59
|
*/
|
|
59
|
-
declare function getEntropyImplementation({ getMnemonic, getUnlockPromise, }: GetEntropyHooks): (options: RestrictedMethodOptions<
|
|
60
|
+
declare function getEntropyImplementation({ getMnemonic, getUnlockPromise, }: GetEntropyHooks): (options: RestrictedMethodOptions<GetEntropyParams>) => Promise<GetEntropyResult>;
|
|
60
61
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PermissionSpecificationBuilder, ValidPermissionSpecification, RestrictedMethodOptions, RestrictedMethodParameters } from '@metamask/permission-controller';
|
|
2
2
|
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { GetLocaleResult } from '@metamask/snaps-sdk';
|
|
3
4
|
import type { NonEmptyArray } from '@metamask/utils';
|
|
4
5
|
import type { MethodHooksObject } from '../utils';
|
|
5
6
|
declare const methodName = "snap_getLocale";
|
|
@@ -43,5 +44,5 @@ export declare const getLocaleBuilder: Readonly<{
|
|
|
43
44
|
* @param hooks.getLocale - A function that returns the user selected locale.
|
|
44
45
|
* @returns The user selected locale.
|
|
45
46
|
*/
|
|
46
|
-
export declare function getImplementation({ getLocale }: GetLocaleMethodHooks): (_args: RestrictedMethodOptions<RestrictedMethodParameters>) => Promise<
|
|
47
|
+
export declare function getImplementation({ getLocale }: GetLocaleMethodHooks): (_args: RestrictedMethodOptions<RestrictedMethodParameters>) => Promise<GetLocaleResult>;
|
|
47
48
|
export {};
|