UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 5.77 kB
{"version":3,"file":"manageAccounts.cjs","sourceRoot":"","sources":["../../src/restricted/manageAccounts.ts"],"names":[],"mappings":";;;AAKA,2EAA8E;AAM9E,uDAO+B;AAE/B,2CAA6C;AAE7C,MAAM,iBAAiB,GAAG,IAAA,mBAAK,EAAC;IAC9B,IAAA,oBAAM,EAAC;QACL,MAAM,EAAE,IAAA,oBAAM,GAAE;KACjB,CAAC;IACF,IAAA,oBAAM,EAAC;QACL,MAAM,EAAE,IAAA,oBAAM,GAAE;QAChB,MAAM,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,mBAAK,EAAC,kBAAU,CAAC,EAAE,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC;KACjE,CAAC;CACH,CAAC,CAAC;AAIU,QAAA,UAAU,GAAG,qBAAqB,CAAC;AAiChD;;;;;;;;GAQG;AACI,MAAM,oBAAoB,GAI7B,CAAC,EACH,cAAc,GAAG,IAAI,EACrB,WAAW,GAC+B,EAAE,EAAE;IAC9C,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU,EAAE,kBAAU;QACtB,cAAc;QACd,oBAAoB,EAAE,4BAA4B,CAAC,WAAW,CAAC;QAC/D,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,oBAAoB,wBAe/B;AAEF;;;;;;;;;GASG;AACH,SAAgB,4BAA4B,CAAC,EAC3C,cAAc,EACd,gBAAgB,GACU;IAC1B,OAAO,KAAK,UAAU,cAAc,CAClC,OAAsD;QAEtD,MAAM,EACJ,OAAO,EAAE,EAAE,MAAM,EAAE,EACnB,MAAM,GACP,GAAG,OAAO,CAAC;QAEZ,IAAA,oBAAM,EAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;QAElC,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;QAC7C,OAAO,MAAM,OAAO,CAAC,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChE,CAAC,CAAC;AACJ,CAAC;AAnBD,oEAmBC;AAED;;;;;;;;;GASG;AACU,QAAA,qBAAqB,GAAG,MAAM,CAAC,MAAM,CAAC;IACjD,UAAU,EAAE,kBAAU;IACtB,oBAAoB,EAApB,4BAAoB;IACpB,WAAW,EAAE;QACX,cAAc,EAAE,IAAI;QACpB,gBAAgB,EAAE,IAAI;KACvB;CACO,CAAC,CAAC","sourcesContent":["import type {\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n PermissionSpecificationBuilder,\n} from '@metamask/permission-controller';\nimport { SubjectType, PermissionType } from '@metamask/permission-controller';\nimport type {\n ManageAccountsParams,\n ManageAccountsResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport {\n assert,\n string,\n object,\n union,\n array,\n record,\n} from '@metamask/superstruct';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { JsonStruct } from '@metamask/utils';\n\nconst SnapMessageStruct = union([\n object({\n method: string(),\n }),\n object({\n method: string(),\n params: union([array(JsonStruct), record(string(), JsonStruct)]),\n }),\n]);\n\ntype Message = InferMatching<typeof SnapMessageStruct, ManageAccountsParams>;\n\nexport const methodName = 'snap_manageAccounts';\n\nexport type ManageAccountsMethodHooks = {\n /**\n * Gets the snap keyring implementation.\n */\n getSnapKeyring: (snapOrigin: string) => Promise<{\n handleKeyringSnapMessage: (\n snapId: string,\n message: Message,\n ) => Promise<Json>;\n }>;\n\n /**\n * Wait for the client to be unlocked.\n *\n * @returns A promise that resolves once the client is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\ntype ManageAccountsSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: ManageAccountsMethodHooks;\n};\n\ntype ManageAccountsSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof manageAccountsImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_manageAccounts` permission.\n * `snap_manageAccounts` lets the Snap manage a set of accounts via a custom keyring.\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_manageAccounts` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n ManageAccountsSpecificationBuilderOptions,\n ManageAccountsSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: ManageAccountsSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: manageAccountsImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\n/**\n * Builds the method implementation for `snap_manageAccounts`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getSnapKeyring - A function to get the snap keyring.\n * @param hooks.getUnlockPromise - The function to get the unlock promise.\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 manageAccountsImplementation({\n getSnapKeyring,\n getUnlockPromise,\n}: ManageAccountsMethodHooks) {\n return async function manageAccounts(\n options: RestrictedMethodOptions<ManageAccountsParams>,\n ): Promise<ManageAccountsResult> {\n const {\n context: { origin },\n params,\n } = options;\n\n assert(params, SnapMessageStruct);\n\n await getUnlockPromise(true);\n\n const keyring = await getSnapKeyring(origin);\n return await keyring.handleKeyringSnapMessage(origin, params);\n };\n}\n\n/**\n * Manage account management Snap accounts. This method is organized into\n * multiple sub-methods which each take their own parameters:\n *\n * - `createAccount`\n * - `updateAccount`\n * - `deleteAccount`\n * - `listAccounts`\n * - `submitResponse`\n */\nexport const manageAccountsBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks: {\n getSnapKeyring: true,\n getUnlockPromise: true,\n },\n} as const);\n"]}