@metamask/snaps-rpc-methods 15.1.0 → 15.1.1

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 CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [15.1.1]
11
+
12
+ ### Fixed
13
+
14
+ - Make `KeyringOrigin` caveat explicitly optional ([#3955](https://github.com/MetaMask/snaps/pull/3955))
15
+
10
16
  ## [15.1.0]
11
17
 
12
18
  ### Added
@@ -609,7 +615,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
609
615
  - The version of the package no longer needs to match the version of all other
610
616
  MetaMask Snaps packages.
611
617
 
612
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.1.0...HEAD
618
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.1.1...HEAD
619
+ [15.1.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.1.0...@metamask/snaps-rpc-methods@15.1.1
613
620
  [15.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.2...@metamask/snaps-rpc-methods@15.1.0
614
621
  [15.0.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.1...@metamask/snaps-rpc-methods@15.0.2
615
622
  [15.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.0...@metamask/snaps-rpc-methods@15.0.1
@@ -26,7 +26,7 @@ const specificationBuilder = (_builderOptions) => {
26
26
  ],
27
27
  endowmentGetter: (_getterOptions) => null,
28
28
  validator: (0, caveats_1.createGenericPermissionValidator)([
29
- { type: snaps_utils_1.SnapCaveatType.KeyringOrigin },
29
+ { type: snaps_utils_1.SnapCaveatType.KeyringOrigin, optional: true },
30
30
  { type: snaps_utils_1.SnapCaveatType.KeyringCapabilities, optional: true },
31
31
  { type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true },
32
32
  ]),
@@ -102,13 +102,10 @@ exports.getKeyringCaveatMapper = getKeyringCaveatMapper;
102
102
  *
103
103
  * @param permission - The permission to get the caveat value from.
104
104
  * @returns The caveat value.
105
- * @throws If the permission does not have a valid {@link KeyringOrigins}
106
- * caveat.
107
105
  */
108
106
  function getKeyringCaveatOrigins(permission) {
109
107
  const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === snaps_utils_1.SnapCaveatType.KeyringOrigin);
110
- (0, utils_1.assert)(caveat);
111
- return caveat.value;
108
+ return caveat?.value ?? { allowedOrigins: [] };
112
109
  }
113
110
  exports.getKeyringCaveatOrigins = getKeyringCaveatOrigins;
114
111
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"keyring.cjs","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mappings":";;;AAUA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAI+B;AAE/B,2CAA+E;AAE/E,iDAA6D;AAC7D,qCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,OAAO,CAAC;AAW9C;;;;;;GAMG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAyB,EAAE,EAAE;IAChC,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,UAAU,EAAE,cAAc;QAC1B,cAAc,EAAE;YACd,4BAAc,CAAC,aAAa;YAC5B,4BAAc,CAAC,mBAAmB;YAClC,4BAAc,CAAC,cAAc;SAC9B;QACD,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,IAAI;QACjE,SAAS,EAAE,IAAA,0CAAgC,EAAC;YAC1C,EAAE,IAAI,EAAE,4BAAc,CAAC,aAAa,EAAE;YACtC,EAAE,IAAI,EAAE,4BAAc,CAAC,mBAAmB,EAAE,QAAQ,EAAE,IAAI,EAAE;YAC5D,EAAE,IAAI,EAAE,4BAAc,CAAC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AAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,CAAC;IACF,CAAC,4BAAc,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAClD,IAAI,EAAE,4BAAc,CAAC,mBAAmB;QACxC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CACzC,0BAA0B,CAAC,MAAM,CAAC;KACrC,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n CaveatConstraint,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n KeyringCapabilities,\n KeyringOrigins,\n} from '@metamask/snaps-utils';\nimport {\n assertIsKeyringCapabilities,\n assertIsKeyringOrigins,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Keyring;\n\ntype KeyringEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n subjectTypes: readonly SubjectType[];\n}>;\n\n/**\n * `endowment:keyring` returns nothing; it is intended to be used as a flag\n * by the client to detect whether the snap has keyring capabilities.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the keyring endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n KeyringEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.KeyringOrigin,\n SnapCaveatType.KeyringCapabilities,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.KeyringOrigin },\n { type: SnapCaveatType.KeyringCapabilities, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a keyring origins caveat. This does not validate the\n * type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringOrigins(value, rpcErrors.invalidParams);\n}\n\n/**\n * Validate the value of a keyring capabilities caveat. This does not validate\n * the type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatCapabilities(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring capabilities: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringCapabilities(value, rpcErrors.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n const caveats: CaveatConstraint[] = [\n {\n type: SnapCaveatType.KeyringOrigin,\n value: { allowedOrigins: value.allowedOrigins },\n },\n ];\n\n if (value.capabilities) {\n caveats.push({\n type: SnapCaveatType.KeyringCapabilities,\n value: { capabilities: value.capabilities },\n });\n }\n\n return { caveats: caveats as NonEmptyArray<CaveatConstraint> };\n}\n\n/**\n * Getter function to get the {@link KeyringOrigins} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n * @throws If the permission does not have a valid {@link KeyringOrigins}\n * caveat.\n */\nexport function getKeyringCaveatOrigins(\n permission?: PermissionConstraint,\n): KeyringOrigins {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringOrigin,\n ) as Caveat<string, KeyringOrigins> | undefined;\n\n assert(caveat);\n return caveat.value;\n}\n\n/**\n * Getter function to get the {@link KeyringCapabilities} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value, or `null` if the permission does not have a\n * {@link KeyringCapabilities} caveat.\n */\nexport function getKeyringCaveatCapabilities(\n permission?: PermissionConstraint,\n): KeyringCapabilities | null {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringCapabilities,\n ) as Caveat<string, KeyringCapabilities> | undefined;\n\n return caveat?.value ?? null;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.KeyringOrigin | SnapCaveatType.KeyringCapabilities,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.KeyringOrigin]: Object.freeze({\n type: SnapCaveatType.KeyringOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n [SnapCaveatType.KeyringCapabilities]: Object.freeze({\n type: SnapCaveatType.KeyringCapabilities,\n validator: (caveat: Caveat<string, any>) =>\n validateCaveatCapabilities(caveat),\n }),\n};\n"]}
1
+ {"version":3,"file":"keyring.cjs","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mapp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MAAM,GAAG,UAAU,EAAE,OAAO,EAAE,IAAI,CACtC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,KAAK,4BAAc,CAAC,aAAa,CACnB,CAAC;IAEhD,OAAO,MAAM,EAAE,KAAK,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC;AACjD,CAAC;AARD,0DAQC;AAED;;;;;;;GAOG;AACH,SAAgB,4BAA4B,CAC1C,UAAiC;IAEjC,MAAM,MAAM,GAAG,UAAU,EAAE,OAAO,EAAE,IAAI,CACtC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,KAAK,4BAAc,CAAC,mBAAmB,CACpB,CAAC;IAErD,OAAO,MAAM,EAAE,KAAK,IAAI,IAAI,CAAC;AAC/B,CAAC;AARD,oEAQC;AAEY,QAAA,2BAA2B,GAGpC;IACF,CAAC,4BAAc,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC5C,IAAI,EAAE,4BAAc,CAAC,aAAa;QAClC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,CAAC;IACF,CAAC,4BAAc,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAClD,IAAI,EAAE,4BAAc,CAAC,mBAAmB;QACxC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CACzC,0BAA0B,CAAC,MAAM,CAAC;KACrC,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n CaveatConstraint,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n KeyringCapabilities,\n KeyringOrigins,\n} from '@metamask/snaps-utils';\nimport {\n assertIsKeyringCapabilities,\n assertIsKeyringOrigins,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Keyring;\n\ntype KeyringEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n subjectTypes: readonly SubjectType[];\n}>;\n\n/**\n * `endowment:keyring` returns nothing; it is intended to be used as a flag\n * by the client to detect whether the snap has keyring capabilities.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the keyring endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n KeyringEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.KeyringOrigin,\n SnapCaveatType.KeyringCapabilities,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.KeyringOrigin, optional: true },\n { type: SnapCaveatType.KeyringCapabilities, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a keyring origins caveat. This does not validate the\n * type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringOrigins(value, rpcErrors.invalidParams);\n}\n\n/**\n * Validate the value of a keyring capabilities caveat. This does not validate\n * the type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatCapabilities(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring capabilities: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringCapabilities(value, rpcErrors.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n const caveats: CaveatConstraint[] = [\n {\n type: SnapCaveatType.KeyringOrigin,\n value: { allowedOrigins: value.allowedOrigins },\n },\n ];\n\n if (value.capabilities) {\n caveats.push({\n type: SnapCaveatType.KeyringCapabilities,\n value: { capabilities: value.capabilities },\n });\n }\n\n return { caveats: caveats as NonEmptyArray<CaveatConstraint> };\n}\n\n/**\n * Getter function to get the {@link KeyringOrigins} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n */\nexport function getKeyringCaveatOrigins(\n permission?: PermissionConstraint,\n): KeyringOrigins {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringOrigin,\n ) as Caveat<string, KeyringOrigins> | undefined;\n\n return caveat?.value ?? { allowedOrigins: [] };\n}\n\n/**\n * Getter function to get the {@link KeyringCapabilities} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value, or `null` if the permission does not have a\n * {@link KeyringCapabilities} caveat.\n */\nexport function getKeyringCaveatCapabilities(\n permission?: PermissionConstraint,\n): KeyringCapabilities | null {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringCapabilities,\n ) as Caveat<string, KeyringCapabilities> | undefined;\n\n return caveat?.value ?? null;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.KeyringOrigin | SnapCaveatType.KeyringCapabilities,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.KeyringOrigin]: Object.freeze({\n type: SnapCaveatType.KeyringOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n [SnapCaveatType.KeyringCapabilities]: Object.freeze({\n type: SnapCaveatType.KeyringCapabilities,\n validator: (caveat: Caveat<string, any>) =>\n validateCaveatCapabilities(caveat),\n }),\n};\n"]}
@@ -31,8 +31,6 @@ export declare function getKeyringCaveatMapper(value: Json): Pick<PermissionCons
31
31
  *
32
32
  * @param permission - The permission to get the caveat value from.
33
33
  * @returns The caveat value.
34
- * @throws If the permission does not have a valid {@link KeyringOrigins}
35
- * caveat.
36
34
  */
37
35
  export declare function getKeyringCaveatOrigins(permission?: PermissionConstraint): KeyringOrigins;
38
36
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"keyring.d.cts","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,6BAA6B,EAC7B,qBAAqB,EACrB,oBAAoB,EACpB,8BAA8B,EAC9B,6BAA6B,EAE9B,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EACV,mBAAmB,EACnB,cAAc,EACf,8BAA8B;AAC/B,OAAO,EAGL,cAAc,EACf,8BAA8B;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAI3D,OAAO,EAAE,cAAc,E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J,qBAAqB,KAAK,IAAI;wBAC3C,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;sBAC1B,SAAS,WAAW,EAAE;;EAoC3B,CAAC;AAsCZ;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,IAAI,GACV,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAoBvC;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,CAAC,EAAE,oBAAoB,GAChC,cAAc,CAMhB;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,UAAU,CAAC,EAAE,oBAAoB,GAChC,mBAAmB,GAAG,IAAI,CAM5B;AAED,eAAO,MAAM,2BAA2B,EAAE,MAAM,CAC9C,cAAc,CAAC,aAAa,GAAG,cAAc,CAAC,mBAAmB,EACjE,6BAA6B,CAW9B,CAAC"}
@@ -31,8 +31,6 @@ export declare function getKeyringCaveatMapper(value: Json): Pick<PermissionCons
31
31
  *
32
32
  * @param permission - The permission to get the caveat value from.
33
33
  * @returns The caveat value.
34
- * @throws If the permission does not have a valid {@link KeyringOrigins}
35
- * caveat.
36
34
  */
37
35
  export declare function getKeyringCaveatOrigins(permission?: PermissionConstraint): KeyringOrigins;
38
36
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"keyring.d.mts","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,6BAA6B,EAC7B,qBAAqB,EACrB,oBAAoB,EACpB,8BAA8B,EAC9B,6BAA6B,EAE9B,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EACV,mBAAmB,EACnB,cAAc,EACf,8BAA8B;AAC/B,OAAO,EAGL,cAAc,EACf,8BAA8B;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAI3D,OAAO,EAAE,cAAc,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ACJ,qBAAqB,KAAK,IAAI;wBAC3C,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;sBAC1B,SAAS,WAAW,EAAE;;EAoC3B,CAAC;AAsCZ;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,IAAI,GACV,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAoBvC;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,CAAC,EAAE,oBAAoB,GAChC,cAAc,CAMhB;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,UAAU,CAAC,EAAE,oBAAoB,GAChC,mBAAmB,GAAG,IAAI,CAM5B;AAED,eAAO,MAAM,2BAA2B,EAAE,MAAM,CAC9C,cAAc,CAAC,aAAa,GAAG,cAAc,CAAC,mBAAmB,EACjE,6BAA6B,CAW9B,CAAC"}
@@ -1,7 +1,7 @@
1
1
  import { PermissionType, SubjectType } from "@metamask/permission-controller";
2
2
  import { rpcErrors } from "@metamask/rpc-errors";
3
3
  import { assertIsKeyringCapabilities, assertIsKeyringOrigins, SnapCaveatType } from "@metamask/snaps-utils";
4
- import { assert, hasProperty, isObject, isPlainObject } from "@metamask/utils";
4
+ import { hasProperty, isObject, isPlainObject } from "@metamask/utils";
5
5
  import { createGenericPermissionValidator } from "./caveats/index.mjs";
6
6
  import { SnapEndowments } from "./enum.mjs";
7
7
  const permissionName = SnapEndowments.Keyring;
@@ -23,7 +23,7 @@ const specificationBuilder = (_builderOptions) => {
23
23
  ],
24
24
  endowmentGetter: (_getterOptions) => null,
25
25
  validator: createGenericPermissionValidator([
26
- { type: SnapCaveatType.KeyringOrigin },
26
+ { type: SnapCaveatType.KeyringOrigin, optional: true },
27
27
  { type: SnapCaveatType.KeyringCapabilities, optional: true },
28
28
  { type: SnapCaveatType.MaxRequestTime, optional: true },
29
29
  ]),
@@ -98,13 +98,10 @@ export function getKeyringCaveatMapper(value) {
98
98
  *
99
99
  * @param permission - The permission to get the caveat value from.
100
100
  * @returns The caveat value.
101
- * @throws If the permission does not have a valid {@link KeyringOrigins}
102
- * caveat.
103
101
  */
104
102
  export function getKeyringCaveatOrigins(permission) {
105
103
  const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === SnapCaveatType.KeyringOrigin);
106
- assert(caveat);
107
- return caveat.value;
104
+ return caveat?.value ?? { allowedOrigins: [] };
108
105
  }
109
106
  /**
110
107
  * Getter function to get the {@link KeyringCapabilities} caveat value from a
@@ -1 +1 @@
1
- {"version":3,"file":"keyring.mjs","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EACL,2BAA2B,EAC3B,sBAAsB,EACtB,cAAc,EACf,8BAA8B;AAE/B,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,aAAa,EAAE,wBAAwB;AAE/E,OAAO,EAAE,gCAAgC,EAAE,4BAAkB;AAC7D,OAAO,EAAE,cAAc,EAAE,mBAAe;AAExC,MAAM,cAAc,GAAG,cAAc,CAAC,OAAO,CAAC;AAW9C;;;;;;GAMG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAyB,EAAE,EAAE;IAChC,OAAO;QACL,cAAc,EAAE,cAAc,CAAC,SAAS;QACxC,UAAU,EAAE,cAAc;QAC1B,cAAc,EAAE;YACd,cAAc,CAAC,aAAa;YAC5B,cAAc,CAAC,mBAAmB;YAClC,cAAc,CAAC,cAAc;SAC9B;QACD,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,IAAI;QACjE,SAAS,EAAE,gCAAgC,CAAC;YAC1C,EAAE,IAAI,EAAE,cAAc,CAAC,aAAa,EAAE;YACtC,EAAE,IAAI,EAAE,cAAc,CAAC,mBAAmB,EAAE,QAAQ,EAAE,IAAI,EAAE;YAC5D,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AC;KAC1E,CAAC;IACF,CAAC,cAAc,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAClD,IAAI,EAAE,cAAc,CAAC,mBAAmB;QACxC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CACzC,0BAA0B,CAAC,MAAM,CAAC;KACrC,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n CaveatConstraint,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n KeyringCapabilities,\n KeyringOrigins,\n} from '@metamask/snaps-utils';\nimport {\n assertIsKeyringCapabilities,\n assertIsKeyringOrigins,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Keyring;\n\ntype KeyringEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n subjectTypes: readonly SubjectType[];\n}>;\n\n/**\n * `endowment:keyring` returns nothing; it is intended to be used as a flag\n * by the client to detect whether the snap has keyring capabilities.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the keyring endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n KeyringEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.KeyringOrigin,\n SnapCaveatType.KeyringCapabilities,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.KeyringOrigin },\n { type: SnapCaveatType.KeyringCapabilities, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a keyring origins caveat. This does not validate the\n * type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringOrigins(value, rpcErrors.invalidParams);\n}\n\n/**\n * Validate the value of a keyring capabilities caveat. This does not validate\n * the type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatCapabilities(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring capabilities: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringCapabilities(value, rpcErrors.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n const caveats: CaveatConstraint[] = [\n {\n type: SnapCaveatType.KeyringOrigin,\n value: { allowedOrigins: value.allowedOrigins },\n },\n ];\n\n if (value.capabilities) {\n caveats.push({\n type: SnapCaveatType.KeyringCapabilities,\n value: { capabilities: value.capabilities },\n });\n }\n\n return { caveats: caveats as NonEmptyArray<CaveatConstraint> };\n}\n\n/**\n * Getter function to get the {@link KeyringOrigins} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n * @throws If the permission does not have a valid {@link KeyringOrigins}\n * caveat.\n */\nexport function getKeyringCaveatOrigins(\n permission?: PermissionConstraint,\n): KeyringOrigins {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringOrigin,\n ) as Caveat<string, KeyringOrigins> | undefined;\n\n assert(caveat);\n return caveat.value;\n}\n\n/**\n * Getter function to get the {@link KeyringCapabilities} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value, or `null` if the permission does not have a\n * {@link KeyringCapabilities} caveat.\n */\nexport function getKeyringCaveatCapabilities(\n permission?: PermissionConstraint,\n): KeyringCapabilities | null {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringCapabilities,\n ) as Caveat<string, KeyringCapabilities> | undefined;\n\n return caveat?.value ?? null;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.KeyringOrigin | SnapCaveatType.KeyringCapabilities,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.KeyringOrigin]: Object.freeze({\n type: SnapCaveatType.KeyringOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n [SnapCaveatType.KeyringCapabilities]: Object.freeze({\n type: SnapCaveatType.KeyringCapabilities,\n validator: (caveat: Caveat<string, any>) =>\n validateCaveatCapabilities(caveat),\n }),\n};\n"]}
1
+ {"version":3,"file":"keyring.mjs","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,cAAc,EAAE,WAAW,EA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OAA0C,EAAE,CAAC;AACjE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CACrC,UAAiC;IAEjC,MAAM,MAAM,GAAG,UAAU,EAAE,OAAO,EAAE,IAAI,CACtC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,KAAK,cAAc,CAAC,aAAa,CACnB,CAAC;IAEhD,OAAO,MAAM,EAAE,KAAK,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC;AACjD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,4BAA4B,CAC1C,UAAiC;IAEjC,MAAM,MAAM,GAAG,UAAU,EAAE,OAAO,EAAE,IAAI,CACtC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,KAAK,cAAc,CAAC,mBAAmB,CACpB,CAAC;IAErD,OAAO,MAAM,EAAE,KAAK,IAAI,IAAI,CAAC;AAC/B,CAAC;AAED,MAAM,CAAC,MAAM,2BAA2B,GAGpC;IACF,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC5C,IAAI,EAAE,cAAc,CAAC,aAAa;QAClC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,CAAC;IACF,CAAC,cAAc,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAClD,IAAI,EAAE,cAAc,CAAC,mBAAmB;QACxC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CACzC,0BAA0B,CAAC,MAAM,CAAC;KACrC,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n CaveatConstraint,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n KeyringCapabilities,\n KeyringOrigins,\n} from '@metamask/snaps-utils';\nimport {\n assertIsKeyringCapabilities,\n assertIsKeyringOrigins,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Keyring;\n\ntype KeyringEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n subjectTypes: readonly SubjectType[];\n}>;\n\n/**\n * `endowment:keyring` returns nothing; it is intended to be used as a flag\n * by the client to detect whether the snap has keyring capabilities.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the keyring endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n KeyringEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.KeyringOrigin,\n SnapCaveatType.KeyringCapabilities,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.KeyringOrigin, optional: true },\n { type: SnapCaveatType.KeyringCapabilities, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a keyring origins caveat. This does not validate the\n * type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringOrigins(value, rpcErrors.invalidParams);\n}\n\n/**\n * Validate the value of a keyring capabilities caveat. This does not validate\n * the type of the caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatCapabilities(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring capabilities: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringCapabilities(value, rpcErrors.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n const caveats: CaveatConstraint[] = [\n {\n type: SnapCaveatType.KeyringOrigin,\n value: { allowedOrigins: value.allowedOrigins },\n },\n ];\n\n if (value.capabilities) {\n caveats.push({\n type: SnapCaveatType.KeyringCapabilities,\n value: { capabilities: value.capabilities },\n });\n }\n\n return { caveats: caveats as NonEmptyArray<CaveatConstraint> };\n}\n\n/**\n * Getter function to get the {@link KeyringOrigins} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n */\nexport function getKeyringCaveatOrigins(\n permission?: PermissionConstraint,\n): KeyringOrigins {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringOrigin,\n ) as Caveat<string, KeyringOrigins> | undefined;\n\n return caveat?.value ?? { allowedOrigins: [] };\n}\n\n/**\n * Getter function to get the {@link KeyringCapabilities} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value, or `null` if the permission does not have a\n * {@link KeyringCapabilities} caveat.\n */\nexport function getKeyringCaveatCapabilities(\n permission?: PermissionConstraint,\n): KeyringCapabilities | null {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.KeyringCapabilities,\n ) as Caveat<string, KeyringCapabilities> | undefined;\n\n return caveat?.value ?? null;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.KeyringOrigin | SnapCaveatType.KeyringCapabilities,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.KeyringOrigin]: Object.freeze({\n type: SnapCaveatType.KeyringOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n [SnapCaveatType.KeyringCapabilities]: Object.freeze({\n type: SnapCaveatType.KeyringCapabilities,\n validator: (caveat: Caveat<string, any>) =>\n validateCaveatCapabilities(caveat),\n }),\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-rpc-methods",
3
- "version": "15.1.0",
3
+ "version": "15.1.1",
4
4
  "description": "MetaMask Snaps JSON-RPC method implementations",
5
5
  "keywords": [
6
6
  "MetaMask",