@metamask/snaps-rpc-methods 4.1.0 → 6.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 +24 -2
- package/dist/cjs/endowments/caveats/generic.js +40 -0
- package/dist/cjs/endowments/caveats/generic.js.map +1 -0
- package/dist/cjs/endowments/caveats/index.js +21 -0
- package/dist/cjs/endowments/caveats/index.js.map +1 -0
- package/dist/cjs/endowments/caveats/requestTime.js +99 -0
- package/dist/cjs/endowments/caveats/requestTime.js.map +1 -0
- package/dist/cjs/endowments/cronjob.js +100 -0
- package/dist/cjs/endowments/cronjob.js.map +1 -0
- package/dist/cjs/endowments/enum.js +26 -0
- package/dist/cjs/endowments/enum.js.map +1 -0
- package/dist/cjs/endowments/ethereum-provider.js +43 -0
- package/dist/cjs/endowments/ethereum-provider.js.map +1 -0
- package/dist/cjs/endowments/home-page.js +37 -0
- package/dist/cjs/endowments/home-page.js.map +1 -0
- package/dist/cjs/endowments/index.js +121 -0
- package/dist/cjs/endowments/index.js.map +1 -0
- package/dist/cjs/endowments/keyring.js +104 -0
- package/dist/cjs/endowments/keyring.js.map +1 -0
- package/dist/cjs/endowments/lifecycle-hooks.js +37 -0
- package/dist/cjs/endowments/lifecycle-hooks.js.map +1 -0
- package/dist/cjs/endowments/name-lookup.js +148 -0
- package/dist/cjs/endowments/name-lookup.js.map +1 -0
- package/dist/cjs/endowments/network-access.js +44 -0
- package/dist/cjs/endowments/network-access.js.map +1 -0
- package/dist/cjs/endowments/rpc.js +103 -0
- package/dist/cjs/endowments/rpc.js.map +1 -0
- package/dist/cjs/endowments/signature-insight.js +110 -0
- package/dist/cjs/endowments/signature-insight.js.map +1 -0
- package/dist/cjs/endowments/transaction-insight.js +111 -0
- package/dist/cjs/endowments/transaction-insight.js.map +1 -0
- package/dist/cjs/endowments/web-assembly.js +42 -0
- package/dist/cjs/endowments/web-assembly.js.map +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/permissions.js +62 -0
- package/dist/cjs/permissions.js.map +1 -0
- package/dist/cjs/permitted/createInterface.js +68 -0
- package/dist/cjs/permitted/createInterface.js.map +1 -0
- package/dist/cjs/permitted/getClientStatus.js +40 -0
- package/dist/cjs/permitted/getClientStatus.js.map +1 -0
- package/dist/cjs/permitted/getFile.js +1 -1
- package/dist/cjs/permitted/getFile.js.map +1 -1
- package/dist/cjs/permitted/getInterfaceState.js +67 -0
- package/dist/cjs/permitted/getInterfaceState.js.map +1 -0
- package/dist/cjs/permitted/handlers.js +9 -1
- package/dist/cjs/permitted/handlers.js.map +1 -1
- package/dist/cjs/permitted/index.js.map +1 -1
- package/dist/cjs/permitted/invokeKeyring.js +2 -3
- package/dist/cjs/permitted/invokeKeyring.js.map +1 -1
- package/dist/cjs/permitted/updateInterface.js +70 -0
- package/dist/cjs/permitted/updateInterface.js.map +1 -0
- package/dist/cjs/restricted/dialog.js +51 -24
- package/dist/cjs/restricted/dialog.js.map +1 -1
- package/dist/cjs/restricted/index.js +3 -0
- package/dist/cjs/restricted/index.js.map +1 -1
- package/dist/cjs/restricted/manageState.js +4 -13
- package/dist/cjs/restricted/manageState.js.map +1 -1
- package/dist/esm/endowments/caveats/generic.js +40 -0
- package/dist/esm/endowments/caveats/generic.js.map +1 -0
- package/dist/esm/endowments/caveats/index.js +4 -0
- package/dist/esm/endowments/caveats/index.js.map +1 -0
- package/dist/esm/endowments/caveats/requestTime.js +93 -0
- package/dist/esm/endowments/caveats/requestTime.js.map +1 -0
- package/dist/esm/endowments/cronjob.js +99 -0
- package/dist/esm/endowments/cronjob.js.map +1 -0
- package/dist/esm/endowments/enum.js +16 -0
- package/dist/esm/endowments/enum.js.map +1 -0
- package/dist/esm/endowments/ethereum-provider.js +33 -0
- package/dist/esm/endowments/ethereum-provider.js.map +1 -0
- package/dist/esm/endowments/home-page.js +27 -0
- package/dist/esm/endowments/home-page.js.map +1 -0
- package/dist/esm/endowments/index.js +68 -0
- package/dist/esm/endowments/index.js.map +1 -0
- package/dist/esm/endowments/keyring.js +95 -0
- package/dist/esm/endowments/keyring.js.map +1 -0
- package/dist/esm/endowments/lifecycle-hooks.js +27 -0
- package/dist/esm/endowments/lifecycle-hooks.js.map +1 -0
- package/dist/esm/endowments/name-lookup.js +146 -0
- package/dist/esm/endowments/name-lookup.js.map +1 -0
- package/dist/esm/endowments/network-access.js +34 -0
- package/dist/esm/endowments/network-access.js.map +1 -0
- package/dist/esm/endowments/rpc.js +92 -0
- package/dist/esm/endowments/rpc.js.map +1 -0
- package/dist/esm/endowments/signature-insight.js +103 -0
- package/dist/esm/endowments/signature-insight.js.map +1 -0
- package/dist/esm/endowments/transaction-insight.js +104 -0
- package/dist/esm/endowments/transaction-insight.js.map +1 -0
- package/dist/esm/endowments/web-assembly.js +32 -0
- package/dist/esm/endowments/web-assembly.js.map +1 -0
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/permissions.js +51 -0
- package/dist/esm/permissions.js.map +1 -0
- package/dist/esm/permitted/createInterface.js +58 -0
- package/dist/esm/permitted/createInterface.js.map +1 -0
- package/dist/esm/permitted/getClientStatus.js +32 -0
- package/dist/esm/permitted/getClientStatus.js.map +1 -0
- package/dist/esm/permitted/getFile.js +1 -1
- package/dist/esm/permitted/getFile.js.map +1 -1
- package/dist/esm/permitted/getInterfaceState.js +57 -0
- package/dist/esm/permitted/getInterfaceState.js.map +1 -0
- package/dist/esm/permitted/handlers.js +9 -1
- package/dist/esm/permitted/handlers.js.map +1 -1
- package/dist/esm/permitted/index.js.map +1 -1
- package/dist/esm/permitted/invokeKeyring.js +2 -3
- package/dist/esm/permitted/invokeKeyring.js.map +1 -1
- package/dist/esm/permitted/updateInterface.js +60 -0
- package/dist/esm/permitted/updateInterface.js.map +1 -0
- package/dist/esm/restricted/dialog.js +55 -29
- package/dist/esm/restricted/dialog.js.map +1 -1
- package/dist/esm/restricted/index.js +1 -0
- package/dist/esm/restricted/index.js.map +1 -1
- package/dist/esm/restricted/manageState.js +1 -1
- package/dist/esm/restricted/manageState.js.map +1 -1
- package/dist/types/endowments/caveats/generic.d.ts +19 -0
- package/dist/types/endowments/caveats/index.d.ts +2 -0
- package/dist/types/endowments/caveats/requestTime.d.ts +29 -0
- package/dist/types/endowments/cronjob.d.ts +51 -0
- package/dist/types/endowments/enum.d.ts +13 -0
- package/dist/types/endowments/ethereum-provider.d.ts +14 -0
- package/dist/types/endowments/home-page.d.ts +15 -0
- package/dist/types/endowments/index.d.ts +131 -0
- package/dist/types/endowments/keyring.d.ts +39 -0
- package/dist/types/endowments/lifecycle-hooks.d.ts +15 -0
- package/dist/types/endowments/name-lookup.d.ts +49 -0
- package/dist/types/endowments/network-access.d.ts +14 -0
- package/dist/types/endowments/rpc.d.ts +38 -0
- package/dist/types/endowments/signature-insight.d.ts +39 -0
- package/dist/types/endowments/transaction-insight.d.ts +39 -0
- package/dist/types/endowments/web-assembly.d.ts +14 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/types/permissions.d.ts +16 -0
- package/dist/types/permitted/createInterface.d.ts +150 -0
- package/dist/types/permitted/getClientStatus.d.ts +13 -0
- package/dist/types/permitted/getInterfaceState.d.ts +18 -0
- package/dist/types/permitted/handlers.d.ts +5 -1
- package/dist/types/permitted/index.d.ts +5 -1
- package/dist/types/permitted/invokeKeyring.d.ts +3 -3
- package/dist/types/permitted/updateInterface.d.ts +152 -0
- package/dist/types/restricted/dialog.d.ts +776 -14
- package/dist/types/restricted/index.d.ts +1 -0
- package/dist/types/restricted/manageState.d.ts +18 -0
- package/package.json +8 -8
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
|
+
import { rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
+
import { SnapCaveatType, isCronjobSpecificationArray } from '@metamask/snaps-utils';
|
|
4
|
+
import { assert, hasProperty, isPlainObject } from '@metamask/utils';
|
|
5
|
+
import { SnapEndowments } from './enum';
|
|
6
|
+
const permissionName = SnapEndowments.Cronjob;
|
|
7
|
+
/**
|
|
8
|
+
* `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.
|
|
9
|
+
*
|
|
10
|
+
* @param _builderOptions - Optional specification builder options.
|
|
11
|
+
* @returns The specification for the cronjob endowment.
|
|
12
|
+
*/ const specificationBuilder = (_builderOptions)=>{
|
|
13
|
+
return {
|
|
14
|
+
permissionType: PermissionType.Endowment,
|
|
15
|
+
targetName: permissionName,
|
|
16
|
+
allowedCaveats: [
|
|
17
|
+
SnapCaveatType.SnapCronjob
|
|
18
|
+
],
|
|
19
|
+
endowmentGetter: (_getterOptions)=>undefined,
|
|
20
|
+
subjectTypes: [
|
|
21
|
+
SubjectType.Snap
|
|
22
|
+
]
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export const cronjobEndowmentBuilder = Object.freeze({
|
|
26
|
+
targetName: permissionName,
|
|
27
|
+
specificationBuilder
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
31
|
+
* Note that this function does not do any validation, that's handled by the
|
|
32
|
+
* PermissionsController when the permission is requested.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The raw value from the `initialPermissions`.
|
|
35
|
+
* @returns The caveat specification.
|
|
36
|
+
*/ export function getCronjobCaveatMapper(value) {
|
|
37
|
+
return {
|
|
38
|
+
caveats: [
|
|
39
|
+
{
|
|
40
|
+
type: SnapCaveatType.SnapCronjob,
|
|
41
|
+
value
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Getter function to get the cronjobs from a permission.
|
|
48
|
+
*
|
|
49
|
+
* This does basic validation of the caveat, but does not validate the type or
|
|
50
|
+
* value of the namespaces object itself, as this is handled by the
|
|
51
|
+
* `PermissionsController` when the permission is requested.
|
|
52
|
+
*
|
|
53
|
+
* @param permission - The permission to get the keyring namespaces from.
|
|
54
|
+
* @returns The cronjobs, or `null` if the permission does not have a
|
|
55
|
+
* cronjob caveat.
|
|
56
|
+
*/ export function getCronjobCaveatJobs(permission) {
|
|
57
|
+
if (!permission?.caveats) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
assert(permission.caveats.length === 1);
|
|
61
|
+
assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);
|
|
62
|
+
const caveat = permission.caveats[0];
|
|
63
|
+
return (caveat.value?.jobs) ?? null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Validate the cronjob specification values associated with a caveat.
|
|
67
|
+
* This validates that the value is a non-empty array with valid
|
|
68
|
+
* cronjob expression and request object.
|
|
69
|
+
*
|
|
70
|
+
* @param caveat - The caveat to validate.
|
|
71
|
+
* @throws If the value is invalid.
|
|
72
|
+
*/ export function validateCronjobCaveat(caveat) {
|
|
73
|
+
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {
|
|
74
|
+
throw rpcErrors.invalidParams({
|
|
75
|
+
message: 'Expected a plain object.'
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const { value } = caveat;
|
|
79
|
+
if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {
|
|
80
|
+
throw rpcErrors.invalidParams({
|
|
81
|
+
message: 'Expected a plain object.'
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (!isCronjobSpecificationArray(value.jobs)) {
|
|
85
|
+
throw rpcErrors.invalidParams({
|
|
86
|
+
message: 'Expected a valid cronjob specification array.'
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Caveat specification for the Cronjob.
|
|
92
|
+
*/ export const cronjobCaveatSpecifications = {
|
|
93
|
+
[SnapCaveatType.SnapCronjob]: Object.freeze({
|
|
94
|
+
type: SnapCaveatType.SnapCronjob,
|
|
95
|
+
validator: (caveat)=>validateCronjobCaveat(caveat)
|
|
96
|
+
})
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=cronjob.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/cronjob.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionConstraint,\n Caveat,\n CaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { CronjobSpecification } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n isCronjobSpecificationArray,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isPlainObject } from '@metamask/utils';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Cronjob;\n\ntype CronjobEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the cronjob endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n CronjobEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [SnapCaveatType.SnapCronjob],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const cronjobEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\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 getCronjobCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapCronjob,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the cronjobs from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The cronjobs, or `null` if the permission does not have a\n * cronjob caveat.\n */\nexport function getCronjobCaveatJobs(\n permission?: PermissionConstraint,\n): CronjobSpecification[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);\n\n const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;\n\n return (caveat.value?.jobs as CronjobSpecification[]) ?? null;\n}\n\n/**\n * Validate the cronjob specification values associated with a caveat.\n * This validates that the value is a non-empty array with valid\n * cronjob expression and request object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateCronjobCaveat(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n if (!isCronjobSpecificationArray(value.jobs)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a valid cronjob specification array.',\n });\n }\n}\n\n/**\n * Caveat specification for the Cronjob.\n */\nexport const cronjobCaveatSpecifications: Record<\n SnapCaveatType.SnapCronjob,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapCronjob]: Object.freeze({\n type: SnapCaveatType.SnapCronjob,\n validator: (caveat) => validateCronjobCaveat(caveat),\n }),\n};\n"],"names":["PermissionType","SubjectType","rpcErrors","SnapCaveatType","isCronjobSpecificationArray","assert","hasProperty","isPlainObject","SnapEndowments","permissionName","Cronjob","specificationBuilder","_builderOptions","permissionType","Endowment","targetName","allowedCaveats","SnapCronjob","endowmentGetter","_getterOptions","undefined","subjectTypes","Snap","cronjobEndowmentBuilder","Object","freeze","getCronjobCaveatMapper","value","caveats","type","getCronjobCaveatJobs","permission","length","caveat","jobs","validateCronjobCaveat","invalidParams","message","cronjobCaveatSpecifications","validator"],"mappings":"AAQA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAC9E,SAASC,SAAS,QAAQ,uBAAuB;AAEjD,SACEC,cAAc,EACdC,2BAA2B,QACtB,wBAAwB;AAE/B,SAASC,MAAM,EAAEC,WAAW,EAAEC,aAAa,QAAQ,kBAAkB;AAErE,SAASC,cAAc,QAAQ,SAAS;AAExC,MAAMC,iBAAiBD,eAAeE,OAAO;AAS7C;;;;;CAKC,GACD,MAAMC,uBAIF,CAACC;IACH,OAAO;QACLC,gBAAgBb,eAAec,SAAS;QACxCC,YAAYN;QACZO,gBAAgB;YAACb,eAAec,WAAW;SAAC;QAC5CC,iBAAiB,CAACC,iBAA2CC;QAC7DC,cAAc;YAACpB,YAAYqB,IAAI;SAAC;IAClC;AACF;AAEA,OAAO,MAAMC,0BAA0BC,OAAOC,MAAM,CAAC;IACnDV,YAAYN;IACZE;AACF,GAAY;AAEZ;;;;;;;CAOC,GACD,OAAO,SAASe,uBACdC,KAAW;IAEX,OAAO;QACLC,SAAS;YACP;gBACEC,MAAM1B,eAAec,WAAW;gBAChCU;YACF;SACD;IACH;AACF;AAEA;;;;;;;;;;CAUC,GACD,OAAO,SAASG,qBACdC,UAAiC;IAEjC,IAAI,CAACA,YAAYH,SAAS;QACxB,OAAO;IACT;IAEAvB,OAAO0B,WAAWH,OAAO,CAACI,MAAM,KAAK;IACrC3B,OAAO0B,WAAWH,OAAO,CAAC,EAAE,CAACC,IAAI,KAAK1B,eAAec,WAAW;IAEhE,MAAMgB,SAASF,WAAWH,OAAO,CAAC,EAAE;IAEpC,OAAO,CAACK,OAAON,KAAK,EAAEO,IAA8B,KAAK;AAC3D;AAEA;;;;;;;CAOC,GACD,OAAO,SAASC,sBAAsBF,MAA2B;IAC/D,IAAI,CAAC3B,YAAY2B,QAAQ,YAAY,CAAC1B,cAAc0B,OAAON,KAAK,GAAG;QACjE,MAAMzB,UAAUkC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,MAAM,EAAEV,KAAK,EAAE,GAAGM;IAElB,IAAI,CAAC3B,YAAYqB,OAAO,WAAW,CAACpB,cAAcoB,QAAQ;QACxD,MAAMzB,UAAUkC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,IAAI,CAACjC,4BAA4BuB,MAAMO,IAAI,GAAG;QAC5C,MAAMhC,UAAUkC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;AACF;AAEA;;CAEC,GACD,OAAO,MAAMC,8BAGT;IACF,CAACnC,eAAec,WAAW,CAAC,EAAEO,OAAOC,MAAM,CAAC;QAC1CI,MAAM1B,eAAec,WAAW;QAChCsB,WAAW,CAACN,SAAWE,sBAAsBF;IAC/C;AACF,EAAE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export var SnapEndowments;
|
|
2
|
+
(function(SnapEndowments) {
|
|
3
|
+
SnapEndowments["NetworkAccess"] = 'endowment:network-access';
|
|
4
|
+
SnapEndowments["SignatureInsight"] = 'endowment:signature-insight';
|
|
5
|
+
SnapEndowments["TransactionInsight"] = 'endowment:transaction-insight';
|
|
6
|
+
SnapEndowments["Cronjob"] = 'endowment:cronjob';
|
|
7
|
+
SnapEndowments["EthereumProvider"] = 'endowment:ethereum-provider';
|
|
8
|
+
SnapEndowments["Rpc"] = 'endowment:rpc';
|
|
9
|
+
SnapEndowments["WebAssemblyAccess"] = 'endowment:webassembly';
|
|
10
|
+
SnapEndowments["NameLookup"] = 'endowment:name-lookup';
|
|
11
|
+
SnapEndowments["LifecycleHooks"] = 'endowment:lifecycle-hooks';
|
|
12
|
+
SnapEndowments["Keyring"] = 'endowment:keyring';
|
|
13
|
+
SnapEndowments["HomePage"] = 'endowment:page-home';
|
|
14
|
+
})(SnapEndowments || (SnapEndowments = {}));
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=enum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/enum.ts"],"sourcesContent":["export enum SnapEndowments {\n NetworkAccess = 'endowment:network-access',\n SignatureInsight = 'endowment:signature-insight',\n TransactionInsight = 'endowment:transaction-insight',\n Cronjob = 'endowment:cronjob',\n EthereumProvider = 'endowment:ethereum-provider',\n Rpc = 'endowment:rpc',\n WebAssemblyAccess = 'endowment:webassembly',\n NameLookup = 'endowment:name-lookup',\n LifecycleHooks = 'endowment:lifecycle-hooks',\n Keyring = 'endowment:keyring',\n HomePage = 'endowment:page-home',\n}\n"],"names":["SnapEndowments","NetworkAccess","SignatureInsight","TransactionInsight","Cronjob","EthereumProvider","Rpc","WebAssemblyAccess","NameLookup","LifecycleHooks","Keyring","HomePage"],"mappings":"WAAO;UAAKA,cAAc;IAAdA,eACVC,mBAAgB;IADND,eAEVE,sBAAmB;IAFTF,eAGVG,wBAAqB;IAHXH,eAIVI,aAAU;IAJAJ,eAKVK,sBAAmB;IALTL,eAMVM,SAAM;IANIN,eAOVO,uBAAoB;IAPVP,eAQVQ,gBAAa;IARHR,eASVS,oBAAiB;IATPT,eAUVU,aAAU;IAVAV,eAWVW,cAAW;GAXDX,mBAAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
|
+
import { SnapEndowments } from './enum';
|
|
3
|
+
const permissionName = SnapEndowments.EthereumProvider;
|
|
4
|
+
/**
|
|
5
|
+
* `endowment:ethereum-provider` returns the name of the ethereum global browser API.
|
|
6
|
+
* This is intended to populate the endowments of the
|
|
7
|
+
* SES Compartment in which a Snap executes.
|
|
8
|
+
*
|
|
9
|
+
* This populates the global scope with an EIP-1193 provider, which DOES NOT implement all legacy functionality exposed to dapps.
|
|
10
|
+
*
|
|
11
|
+
* @param _builderOptions - Optional specification builder options.
|
|
12
|
+
* @returns The specification for the network endowment.
|
|
13
|
+
*/ const specificationBuilder = (_builderOptions)=>{
|
|
14
|
+
return {
|
|
15
|
+
permissionType: PermissionType.Endowment,
|
|
16
|
+
targetName: permissionName,
|
|
17
|
+
allowedCaveats: null,
|
|
18
|
+
endowmentGetter: (_getterOptions)=>{
|
|
19
|
+
return [
|
|
20
|
+
'ethereum'
|
|
21
|
+
];
|
|
22
|
+
},
|
|
23
|
+
subjectTypes: [
|
|
24
|
+
SubjectType.Snap
|
|
25
|
+
]
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export const ethereumProviderEndowmentBuilder = Object.freeze({
|
|
29
|
+
targetName: permissionName,
|
|
30
|
+
specificationBuilder
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=ethereum-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/ethereum-provider.ts"],"sourcesContent":["import type {\n EndowmentGetterParams,\n PermissionSpecificationBuilder,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.EthereumProvider;\n\ntype EthereumProviderEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: any) => ['ethereum'];\n allowedCaveats: null;\n}>;\n\n/**\n * `endowment:ethereum-provider` returns the name of the ethereum global browser API.\n * This is intended to populate the endowments of the\n * SES Compartment in which a Snap executes.\n *\n * This populates the global scope with an EIP-1193 provider, which DOES NOT implement all legacy functionality exposed to dapps.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the network endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n EthereumProviderEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: null,\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => {\n return ['ethereum'];\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const ethereumProviderEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n"],"names":["PermissionType","SubjectType","SnapEndowments","permissionName","EthereumProvider","specificationBuilder","_builderOptions","permissionType","Endowment","targetName","allowedCaveats","endowmentGetter","_getterOptions","subjectTypes","Snap","ethereumProviderEndowmentBuilder","Object","freeze"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAE9E,SAASC,cAAc,QAAQ,SAAS;AAExC,MAAMC,iBAAiBD,eAAeE,gBAAgB;AAStD;;;;;;;;;CASC,GACD,MAAMC,uBAIF,CAACC;IACH,OAAO;QACLC,gBAAgBP,eAAeQ,SAAS;QACxCC,YAAYN;QACZO,gBAAgB;QAChBC,iBAAiB,CAACC;YAChB,OAAO;gBAAC;aAAW;QACrB;QACAC,cAAc;YAACZ,YAAYa,IAAI;SAAC;IAClC;AACF;AAEA,OAAO,MAAMC,mCAAmCC,OAAOC,MAAM,CAAC;IAC5DR,YAAYN;IACZE;AACF,GAAY"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
|
+
import { SnapEndowments } from './enum';
|
|
3
|
+
const permissionName = SnapEndowments.HomePage;
|
|
4
|
+
/**
|
|
5
|
+
* `endowment:page-home` returns nothing; it is intended to be used as a
|
|
6
|
+
* flag by the snap controller to detect whether the snap has the capability to
|
|
7
|
+
* use the snap home page feature.
|
|
8
|
+
*
|
|
9
|
+
* @param _builderOptions - Optional specification builder options.
|
|
10
|
+
* @returns The specification for the `snap-pages` endowment.
|
|
11
|
+
*/ const specificationBuilder = (_builderOptions)=>{
|
|
12
|
+
return {
|
|
13
|
+
permissionType: PermissionType.Endowment,
|
|
14
|
+
targetName: permissionName,
|
|
15
|
+
allowedCaveats: null,
|
|
16
|
+
endowmentGetter: (_getterOptions)=>undefined,
|
|
17
|
+
subjectTypes: [
|
|
18
|
+
SubjectType.Snap
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export const homePageEndowmentBuilder = Object.freeze({
|
|
23
|
+
targetName: permissionName,
|
|
24
|
+
specificationBuilder
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=home-page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/home-page.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.HomePage;\n\ntype HomePageEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:page-home` returns nothing; it is intended to be used as a\n * flag by the snap controller to detect whether the snap has the capability to\n * use the snap home page feature.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the `snap-pages` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n HomePageEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: null,\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const homePageEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n"],"names":["PermissionType","SubjectType","SnapEndowments","permissionName","HomePage","specificationBuilder","_builderOptions","permissionType","Endowment","targetName","allowedCaveats","endowmentGetter","_getterOptions","undefined","subjectTypes","Snap","homePageEndowmentBuilder","Object","freeze"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAG9E,SAASC,cAAc,QAAQ,SAAS;AAExC,MAAMC,iBAAiBD,eAAeE,QAAQ;AAS9C;;;;;;;CAOC,GACD,MAAMC,uBAIF,CAACC;IACH,OAAO;QACLC,gBAAgBP,eAAeQ,SAAS;QACxCC,YAAYN;QACZO,gBAAgB;QAChBC,iBAAiB,CAACC,iBAA2CC;QAC7DC,cAAc;YAACb,YAAYc,IAAI;SAAC;IAClC;AACF;AAEA,OAAO,MAAMC,2BAA2BC,OAAOC,MAAM,CAAC;IACpDT,YAAYN;IACZE;AACF,GAAY"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { HandlerType } from '@metamask/snaps-utils';
|
|
2
|
+
import { createMaxRequestTimeMapper, getMaxRequestTimeCaveatMapper, maxRequestTimeCaveatSpecifications } from './caveats';
|
|
3
|
+
import { cronjobCaveatSpecifications, cronjobEndowmentBuilder, getCronjobCaveatMapper } from './cronjob';
|
|
4
|
+
import { ethereumProviderEndowmentBuilder } from './ethereum-provider';
|
|
5
|
+
import { homePageEndowmentBuilder } from './home-page';
|
|
6
|
+
import { getKeyringCaveatMapper, keyringCaveatSpecifications, keyringEndowmentBuilder } from './keyring';
|
|
7
|
+
import { lifecycleHooksEndowmentBuilder } from './lifecycle-hooks';
|
|
8
|
+
import { getNameLookupCaveatMapper, nameLookupCaveatSpecifications, nameLookupEndowmentBuilder } from './name-lookup';
|
|
9
|
+
import { networkAccessEndowmentBuilder } from './network-access';
|
|
10
|
+
import { getRpcCaveatMapper, rpcCaveatSpecifications, rpcEndowmentBuilder } from './rpc';
|
|
11
|
+
import { getSignatureInsightCaveatMapper, signatureInsightCaveatSpecifications, signatureInsightEndowmentBuilder } from './signature-insight';
|
|
12
|
+
import { getTransactionInsightCaveatMapper, transactionInsightCaveatSpecifications, transactionInsightEndowmentBuilder } from './transaction-insight';
|
|
13
|
+
import { webAssemblyEndowmentBuilder } from './web-assembly';
|
|
14
|
+
export const endowmentPermissionBuilders = {
|
|
15
|
+
[networkAccessEndowmentBuilder.targetName]: networkAccessEndowmentBuilder,
|
|
16
|
+
[transactionInsightEndowmentBuilder.targetName]: transactionInsightEndowmentBuilder,
|
|
17
|
+
[cronjobEndowmentBuilder.targetName]: cronjobEndowmentBuilder,
|
|
18
|
+
[ethereumProviderEndowmentBuilder.targetName]: ethereumProviderEndowmentBuilder,
|
|
19
|
+
[rpcEndowmentBuilder.targetName]: rpcEndowmentBuilder,
|
|
20
|
+
[webAssemblyEndowmentBuilder.targetName]: webAssemblyEndowmentBuilder,
|
|
21
|
+
[nameLookupEndowmentBuilder.targetName]: nameLookupEndowmentBuilder,
|
|
22
|
+
[lifecycleHooksEndowmentBuilder.targetName]: lifecycleHooksEndowmentBuilder,
|
|
23
|
+
[keyringEndowmentBuilder.targetName]: keyringEndowmentBuilder,
|
|
24
|
+
[homePageEndowmentBuilder.targetName]: homePageEndowmentBuilder,
|
|
25
|
+
[signatureInsightEndowmentBuilder.targetName]: signatureInsightEndowmentBuilder
|
|
26
|
+
};
|
|
27
|
+
export const endowmentCaveatSpecifications = {
|
|
28
|
+
...cronjobCaveatSpecifications,
|
|
29
|
+
...transactionInsightCaveatSpecifications,
|
|
30
|
+
...rpcCaveatSpecifications,
|
|
31
|
+
...nameLookupCaveatSpecifications,
|
|
32
|
+
...keyringCaveatSpecifications,
|
|
33
|
+
...signatureInsightCaveatSpecifications,
|
|
34
|
+
...maxRequestTimeCaveatSpecifications
|
|
35
|
+
};
|
|
36
|
+
export const endowmentCaveatMappers = {
|
|
37
|
+
[cronjobEndowmentBuilder.targetName]: createMaxRequestTimeMapper(getCronjobCaveatMapper),
|
|
38
|
+
[transactionInsightEndowmentBuilder.targetName]: createMaxRequestTimeMapper(getTransactionInsightCaveatMapper),
|
|
39
|
+
[rpcEndowmentBuilder.targetName]: createMaxRequestTimeMapper(getRpcCaveatMapper),
|
|
40
|
+
[nameLookupEndowmentBuilder.targetName]: createMaxRequestTimeMapper(getNameLookupCaveatMapper),
|
|
41
|
+
[keyringEndowmentBuilder.targetName]: createMaxRequestTimeMapper(getKeyringCaveatMapper),
|
|
42
|
+
[signatureInsightEndowmentBuilder.targetName]: createMaxRequestTimeMapper(getSignatureInsightCaveatMapper),
|
|
43
|
+
[lifecycleHooksEndowmentBuilder.targetName]: getMaxRequestTimeCaveatMapper,
|
|
44
|
+
[homePageEndowmentBuilder.targetName]: getMaxRequestTimeCaveatMapper
|
|
45
|
+
};
|
|
46
|
+
// We allow null because a permitted handler does not have an endowment
|
|
47
|
+
export const handlerEndowments = {
|
|
48
|
+
[HandlerType.OnRpcRequest]: rpcEndowmentBuilder.targetName,
|
|
49
|
+
[HandlerType.OnTransaction]: transactionInsightEndowmentBuilder.targetName,
|
|
50
|
+
[HandlerType.OnCronjob]: cronjobEndowmentBuilder.targetName,
|
|
51
|
+
[HandlerType.OnNameLookup]: nameLookupEndowmentBuilder.targetName,
|
|
52
|
+
[HandlerType.OnInstall]: lifecycleHooksEndowmentBuilder.targetName,
|
|
53
|
+
[HandlerType.OnUpdate]: lifecycleHooksEndowmentBuilder.targetName,
|
|
54
|
+
[HandlerType.OnKeyringRequest]: keyringEndowmentBuilder.targetName,
|
|
55
|
+
[HandlerType.OnHomePage]: homePageEndowmentBuilder.targetName,
|
|
56
|
+
[HandlerType.OnSignature]: signatureInsightEndowmentBuilder.targetName,
|
|
57
|
+
[HandlerType.OnUserInput]: null
|
|
58
|
+
};
|
|
59
|
+
export * from './enum';
|
|
60
|
+
export { getRpcCaveatOrigins } from './rpc';
|
|
61
|
+
export { getSignatureOriginCaveat } from './signature-insight';
|
|
62
|
+
export { getTransactionOriginCaveat } from './transaction-insight';
|
|
63
|
+
export { getChainIdsCaveat, getLookupMatchersCaveat } from './name-lookup';
|
|
64
|
+
export { getKeyringCaveatOrigins } from './keyring';
|
|
65
|
+
export { getMaxRequestTimeCaveat } from './caveats';
|
|
66
|
+
export { getCronjobCaveatJobs } from './cronjob';
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/index.ts"],"sourcesContent":["import type { PermissionConstraint } from '@metamask/permission-controller';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\n\nimport {\n createMaxRequestTimeMapper,\n getMaxRequestTimeCaveatMapper,\n maxRequestTimeCaveatSpecifications,\n} from './caveats';\nimport {\n cronjobCaveatSpecifications,\n cronjobEndowmentBuilder,\n getCronjobCaveatMapper,\n} from './cronjob';\nimport { ethereumProviderEndowmentBuilder } from './ethereum-provider';\nimport { homePageEndowmentBuilder } from './home-page';\nimport {\n getKeyringCaveatMapper,\n keyringCaveatSpecifications,\n keyringEndowmentBuilder,\n} from './keyring';\nimport { lifecycleHooksEndowmentBuilder } from './lifecycle-hooks';\nimport {\n getNameLookupCaveatMapper,\n nameLookupCaveatSpecifications,\n nameLookupEndowmentBuilder,\n} from './name-lookup';\nimport { networkAccessEndowmentBuilder } from './network-access';\nimport {\n getRpcCaveatMapper,\n rpcCaveatSpecifications,\n rpcEndowmentBuilder,\n} from './rpc';\nimport {\n getSignatureInsightCaveatMapper,\n signatureInsightCaveatSpecifications,\n signatureInsightEndowmentBuilder,\n} from './signature-insight';\nimport {\n getTransactionInsightCaveatMapper,\n transactionInsightCaveatSpecifications,\n transactionInsightEndowmentBuilder,\n} from './transaction-insight';\nimport { webAssemblyEndowmentBuilder } from './web-assembly';\n\nexport const endowmentPermissionBuilders = {\n [networkAccessEndowmentBuilder.targetName]: networkAccessEndowmentBuilder,\n [transactionInsightEndowmentBuilder.targetName]:\n transactionInsightEndowmentBuilder,\n [cronjobEndowmentBuilder.targetName]: cronjobEndowmentBuilder,\n [ethereumProviderEndowmentBuilder.targetName]:\n ethereumProviderEndowmentBuilder,\n [rpcEndowmentBuilder.targetName]: rpcEndowmentBuilder,\n [webAssemblyEndowmentBuilder.targetName]: webAssemblyEndowmentBuilder,\n [nameLookupEndowmentBuilder.targetName]: nameLookupEndowmentBuilder,\n [lifecycleHooksEndowmentBuilder.targetName]: lifecycleHooksEndowmentBuilder,\n [keyringEndowmentBuilder.targetName]: keyringEndowmentBuilder,\n [homePageEndowmentBuilder.targetName]: homePageEndowmentBuilder,\n [signatureInsightEndowmentBuilder.targetName]:\n signatureInsightEndowmentBuilder,\n} as const;\n\nexport const endowmentCaveatSpecifications = {\n ...cronjobCaveatSpecifications,\n ...transactionInsightCaveatSpecifications,\n ...rpcCaveatSpecifications,\n ...nameLookupCaveatSpecifications,\n ...keyringCaveatSpecifications,\n ...signatureInsightCaveatSpecifications,\n ...maxRequestTimeCaveatSpecifications,\n};\n\nexport const endowmentCaveatMappers: Record<\n string,\n (value: Json) => Pick<PermissionConstraint, 'caveats'>\n> = {\n [cronjobEndowmentBuilder.targetName]: createMaxRequestTimeMapper(\n getCronjobCaveatMapper,\n ),\n [transactionInsightEndowmentBuilder.targetName]: createMaxRequestTimeMapper(\n getTransactionInsightCaveatMapper,\n ),\n [rpcEndowmentBuilder.targetName]:\n createMaxRequestTimeMapper(getRpcCaveatMapper),\n [nameLookupEndowmentBuilder.targetName]: createMaxRequestTimeMapper(\n getNameLookupCaveatMapper,\n ),\n [keyringEndowmentBuilder.targetName]: createMaxRequestTimeMapper(\n getKeyringCaveatMapper,\n ),\n [signatureInsightEndowmentBuilder.targetName]: createMaxRequestTimeMapper(\n getSignatureInsightCaveatMapper,\n ),\n [lifecycleHooksEndowmentBuilder.targetName]: getMaxRequestTimeCaveatMapper,\n [homePageEndowmentBuilder.targetName]: getMaxRequestTimeCaveatMapper,\n};\n\n// We allow null because a permitted handler does not have an endowment\nexport const handlerEndowments: Record<HandlerType, string | null> = {\n [HandlerType.OnRpcRequest]: rpcEndowmentBuilder.targetName,\n [HandlerType.OnTransaction]: transactionInsightEndowmentBuilder.targetName,\n [HandlerType.OnCronjob]: cronjobEndowmentBuilder.targetName,\n [HandlerType.OnNameLookup]: nameLookupEndowmentBuilder.targetName,\n [HandlerType.OnInstall]: lifecycleHooksEndowmentBuilder.targetName,\n [HandlerType.OnUpdate]: lifecycleHooksEndowmentBuilder.targetName,\n [HandlerType.OnKeyringRequest]: keyringEndowmentBuilder.targetName,\n [HandlerType.OnHomePage]: homePageEndowmentBuilder.targetName,\n [HandlerType.OnSignature]: signatureInsightEndowmentBuilder.targetName,\n [HandlerType.OnUserInput]: null,\n};\n\nexport * from './enum';\nexport { getRpcCaveatOrigins } from './rpc';\nexport { getSignatureOriginCaveat } from './signature-insight';\nexport { getTransactionOriginCaveat } from './transaction-insight';\nexport { getChainIdsCaveat, getLookupMatchersCaveat } from './name-lookup';\nexport { getKeyringCaveatOrigins } from './keyring';\nexport { getMaxRequestTimeCaveat } from './caveats';\nexport { getCronjobCaveatJobs } from './cronjob';\n"],"names":["HandlerType","createMaxRequestTimeMapper","getMaxRequestTimeCaveatMapper","maxRequestTimeCaveatSpecifications","cronjobCaveatSpecifications","cronjobEndowmentBuilder","getCronjobCaveatMapper","ethereumProviderEndowmentBuilder","homePageEndowmentBuilder","getKeyringCaveatMapper","keyringCaveatSpecifications","keyringEndowmentBuilder","lifecycleHooksEndowmentBuilder","getNameLookupCaveatMapper","nameLookupCaveatSpecifications","nameLookupEndowmentBuilder","networkAccessEndowmentBuilder","getRpcCaveatMapper","rpcCaveatSpecifications","rpcEndowmentBuilder","getSignatureInsightCaveatMapper","signatureInsightCaveatSpecifications","signatureInsightEndowmentBuilder","getTransactionInsightCaveatMapper","transactionInsightCaveatSpecifications","transactionInsightEndowmentBuilder","webAssemblyEndowmentBuilder","endowmentPermissionBuilders","targetName","endowmentCaveatSpecifications","endowmentCaveatMappers","handlerEndowments","OnRpcRequest","OnTransaction","OnCronjob","OnNameLookup","OnInstall","OnUpdate","OnKeyringRequest","OnHomePage","OnSignature","OnUserInput","getRpcCaveatOrigins","getSignatureOriginCaveat","getTransactionOriginCaveat","getChainIdsCaveat","getLookupMatchersCaveat","getKeyringCaveatOrigins","getMaxRequestTimeCaveat","getCronjobCaveatJobs"],"mappings":"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"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
|
+
import { rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
+
import { assertIsKeyringOrigins, SnapCaveatType } from '@metamask/snaps-utils';
|
|
4
|
+
import { assert, hasProperty, isPlainObject } from '@metamask/utils';
|
|
5
|
+
import { createGenericPermissionValidator } from './caveats';
|
|
6
|
+
import { SnapEndowments } from './enum';
|
|
7
|
+
const permissionName = SnapEndowments.Keyring;
|
|
8
|
+
/**
|
|
9
|
+
* `endowment:keyring` returns nothing; it is intended to be used as a flag
|
|
10
|
+
* by the client to detect whether the snap has keyring capabilities.
|
|
11
|
+
*
|
|
12
|
+
* @param _builderOptions - Optional specification builder options.
|
|
13
|
+
* @returns The specification for the keyring endowment.
|
|
14
|
+
*/ const specificationBuilder = (_builderOptions)=>{
|
|
15
|
+
return {
|
|
16
|
+
permissionType: PermissionType.Endowment,
|
|
17
|
+
targetName: permissionName,
|
|
18
|
+
allowedCaveats: [
|
|
19
|
+
SnapCaveatType.KeyringOrigin,
|
|
20
|
+
SnapCaveatType.MaxRequestTime
|
|
21
|
+
],
|
|
22
|
+
endowmentGetter: (_getterOptions)=>undefined,
|
|
23
|
+
validator: createGenericPermissionValidator([
|
|
24
|
+
{
|
|
25
|
+
type: SnapCaveatType.KeyringOrigin
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: SnapCaveatType.MaxRequestTime,
|
|
29
|
+
optional: true
|
|
30
|
+
}
|
|
31
|
+
]),
|
|
32
|
+
subjectTypes: [
|
|
33
|
+
SubjectType.Snap
|
|
34
|
+
]
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export const keyringEndowmentBuilder = Object.freeze({
|
|
38
|
+
targetName: permissionName,
|
|
39
|
+
specificationBuilder
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Validate the value of a caveat. This does not validate the type of the
|
|
43
|
+
* caveat itself, only the value of the caveat.
|
|
44
|
+
*
|
|
45
|
+
* @param caveat - The caveat to validate.
|
|
46
|
+
* @throws If the caveat value is invalid.
|
|
47
|
+
*/ function validateCaveatOrigins(caveat) {
|
|
48
|
+
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {
|
|
49
|
+
throw rpcErrors.invalidParams({
|
|
50
|
+
message: 'Invalid keyring origins: Expected a plain object.'
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const { value } = caveat;
|
|
54
|
+
assertIsKeyringOrigins(value, rpcErrors.invalidParams);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
58
|
+
* Note that this function does not do any validation, that's handled by the
|
|
59
|
+
* PermissionsController when the permission is requested.
|
|
60
|
+
*
|
|
61
|
+
* @param value - The raw value from the `initialPermissions`.
|
|
62
|
+
* @returns The caveat specification.
|
|
63
|
+
*/ export function getKeyringCaveatMapper(value) {
|
|
64
|
+
return {
|
|
65
|
+
caveats: [
|
|
66
|
+
{
|
|
67
|
+
type: SnapCaveatType.KeyringOrigin,
|
|
68
|
+
value
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Getter function to get the {@link KeyringOrigins} caveat value from a
|
|
75
|
+
* permission.
|
|
76
|
+
*
|
|
77
|
+
* @param permission - The permission to get the caveat value from.
|
|
78
|
+
* @returns The caveat value.
|
|
79
|
+
* @throws If the permission does not have a valid {@link KeyringOrigins}
|
|
80
|
+
* caveat.
|
|
81
|
+
*/ export function getKeyringCaveatOrigins(permission) {
|
|
82
|
+
assert(permission?.caveats);
|
|
83
|
+
assert(permission.caveats.length === 1);
|
|
84
|
+
assert(permission.caveats[0].type === SnapCaveatType.KeyringOrigin);
|
|
85
|
+
const caveat = permission.caveats[0];
|
|
86
|
+
return caveat.value;
|
|
87
|
+
}
|
|
88
|
+
export const keyringCaveatSpecifications = {
|
|
89
|
+
[SnapCaveatType.KeyringOrigin]: Object.freeze({
|
|
90
|
+
type: SnapCaveatType.KeyringOrigin,
|
|
91
|
+
validator: (caveat)=>validateCaveatOrigins(caveat)
|
|
92
|
+
})
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
//# sourceMappingURL=keyring.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/keyring.ts"],"sourcesContent":["import type {\n Caveat,\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 { KeyringOrigins } from '@metamask/snaps-utils';\nimport { assertIsKeyringOrigins, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, 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) => undefined;\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.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.KeyringOrigin },\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 caveat. This does not validate the type of the\n * 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 * 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 return {\n caveats: [\n {\n type: SnapCaveatType.KeyringOrigin,\n value,\n },\n ],\n };\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 | null {\n assert(permission?.caveats);\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.KeyringOrigin);\n\n const caveat = permission.caveats[0] as Caveat<string, KeyringOrigins>;\n return caveat.value;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.KeyringOrigin,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.KeyringOrigin]: Object.freeze({\n type: SnapCaveatType.KeyringOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n};\n"],"names":["PermissionType","SubjectType","rpcErrors","assertIsKeyringOrigins","SnapCaveatType","assert","hasProperty","isPlainObject","createGenericPermissionValidator","SnapEndowments","permissionName","Keyring","specificationBuilder","_builderOptions","permissionType","Endowment","targetName","allowedCaveats","KeyringOrigin","MaxRequestTime","endowmentGetter","_getterOptions","undefined","validator","type","optional","subjectTypes","Snap","keyringEndowmentBuilder","Object","freeze","validateCaveatOrigins","caveat","value","invalidParams","message","getKeyringCaveatMapper","caveats","getKeyringCaveatOrigins","permission","length","keyringCaveatSpecifications"],"mappings":"AASA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAC9E,SAASC,SAAS,QAAQ,uBAAuB;AAEjD,SAASC,sBAAsB,EAAEC,cAAc,QAAQ,wBAAwB;AAE/E,SAASC,MAAM,EAAEC,WAAW,EAAEC,aAAa,QAAQ,kBAAkB;AAErE,SAASC,gCAAgC,QAAQ,YAAY;AAC7D,SAASC,cAAc,QAAQ,SAAS;AAExC,MAAMC,iBAAiBD,eAAeE,OAAO;AAW7C;;;;;;CAMC,GACD,MAAMC,uBAIF,CAACC;IACH,OAAO;QACLC,gBAAgBd,eAAee,SAAS;QACxCC,YAAYN;QACZO,gBAAgB;YACdb,eAAec,aAAa;YAC5Bd,eAAee,cAAc;SAC9B;QACDC,iBAAiB,CAACC,iBAA2CC;QAC7DC,WAAWf,iCAAiC;YAC1C;gBAAEgB,MAAMpB,eAAec,aAAa;YAAC;YACrC;gBAAEM,MAAMpB,eAAee,cAAc;gBAAEM,UAAU;YAAK;SACvD;QACDC,cAAc;YAACzB,YAAY0B,IAAI;SAAC;IAClC;AACF;AAEA,OAAO,MAAMC,0BAA0BC,OAAOC,MAAM,CAAC;IACnDd,YAAYN;IACZE;AACF,GAAY;AAEZ;;;;;;CAMC,GACD,SAASmB,sBAAsBC,MAA2B;IACxD,IAAI,CAAC1B,YAAY0B,QAAQ,YAAY,CAACzB,cAAcyB,OAAOC,KAAK,GAAG;QACjE,MAAM/B,UAAUgC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,MAAM,EAAEF,KAAK,EAAE,GAAGD;IAClB7B,uBAAuB8B,OAAO/B,UAAUgC,aAAa;AACvD;AAEA;;;;;;;CAOC,GACD,OAAO,SAASE,uBACdH,KAAW;IAEX,OAAO;QACLI,SAAS;YACP;gBACEb,MAAMpB,eAAec,aAAa;gBAClCe;YACF;SACD;IACH;AACF;AAEA;;;;;;;;CAQC,GACD,OAAO,SAASK,wBACdC,UAAiC;IAEjClC,OAAOkC,YAAYF;IACnBhC,OAAOkC,WAAWF,OAAO,CAACG,MAAM,KAAK;IACrCnC,OAAOkC,WAAWF,OAAO,CAAC,EAAE,CAACb,IAAI,KAAKpB,eAAec,aAAa;IAElE,MAAMc,SAASO,WAAWF,OAAO,CAAC,EAAE;IACpC,OAAOL,OAAOC,KAAK;AACrB;AAEA,OAAO,MAAMQ,8BAGT;IACF,CAACrC,eAAec,aAAa,CAAC,EAAEW,OAAOC,MAAM,CAAC;QAC5CN,MAAMpB,eAAec,aAAa;QAClCK,WAAW,CAACS,SAAgCD,sBAAsBC;IACpE;AACF,EAAE"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
|
+
import { SnapEndowments } from './enum';
|
|
3
|
+
const permissionName = SnapEndowments.LifecycleHooks;
|
|
4
|
+
/**
|
|
5
|
+
* `endowment:lifecycle-hooks` returns nothing; it is intended to be used as a
|
|
6
|
+
* flag by the snap controller to detect whether the snap has the capability to
|
|
7
|
+
* use lifecycle hooks.
|
|
8
|
+
*
|
|
9
|
+
* @param _builderOptions - Optional specification builder options.
|
|
10
|
+
* @returns The specification for the `lifecycle-hooks` endowment.
|
|
11
|
+
*/ const specificationBuilder = (_builderOptions)=>{
|
|
12
|
+
return {
|
|
13
|
+
permissionType: PermissionType.Endowment,
|
|
14
|
+
targetName: permissionName,
|
|
15
|
+
allowedCaveats: null,
|
|
16
|
+
endowmentGetter: (_getterOptions)=>undefined,
|
|
17
|
+
subjectTypes: [
|
|
18
|
+
SubjectType.Snap
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export const lifecycleHooksEndowmentBuilder = Object.freeze({
|
|
23
|
+
targetName: permissionName,
|
|
24
|
+
specificationBuilder
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=lifecycle-hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/lifecycle-hooks.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.LifecycleHooks;\n\ntype LifecycleHooksEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:lifecycle-hooks` returns nothing; it is intended to be used as a\n * flag by the snap controller to detect whether the snap has the capability to\n * use lifecycle hooks.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the `lifecycle-hooks` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n LifecycleHooksEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: null,\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const lifecycleHooksEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n"],"names":["PermissionType","SubjectType","SnapEndowments","permissionName","LifecycleHooks","specificationBuilder","_builderOptions","permissionType","Endowment","targetName","allowedCaveats","endowmentGetter","_getterOptions","undefined","subjectTypes","Snap","lifecycleHooksEndowmentBuilder","Object","freeze"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAG9E,SAASC,cAAc,QAAQ,SAAS;AAExC,MAAMC,iBAAiBD,eAAeE,cAAc;AASpD;;;;;;;CAOC,GACD,MAAMC,uBAIF,CAACC;IACH,OAAO;QACLC,gBAAgBP,eAAeQ,SAAS;QACxCC,YAAYN;QACZO,gBAAgB;QAChBC,iBAAiB,CAACC,iBAA2CC;QAC7DC,cAAc;YAACb,YAAYc,IAAI;SAAC;IAClC;AACF;AAEA,OAAO,MAAMC,iCAAiCC,OAAOC,MAAM,CAAC;IAC1DT,YAAYN;IACZE;AACF,GAAY"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
|
+
import { rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
+
import { ChainIdsStruct, LookupMatchersStruct, SnapCaveatType } from '@metamask/snaps-utils';
|
|
4
|
+
import { assert, assertStruct, hasProperty, isObject, isPlainObject } from '@metamask/utils';
|
|
5
|
+
import { createGenericPermissionValidator } from './caveats';
|
|
6
|
+
import { SnapEndowments } from './enum';
|
|
7
|
+
const permissionName = SnapEndowments.NameLookup;
|
|
8
|
+
/**
|
|
9
|
+
* `endowment:name-lookup` returns nothing; it is intended to be used as a flag
|
|
10
|
+
* by the extension to detect whether the snap has the capability to resolve a domain/address.
|
|
11
|
+
*
|
|
12
|
+
* @param _builderOptions - Optional specification builder options.
|
|
13
|
+
* @returns The specification for the name-lookup endowment.
|
|
14
|
+
*/ const specificationBuilder = (_builderOptions)=>{
|
|
15
|
+
return {
|
|
16
|
+
permissionType: PermissionType.Endowment,
|
|
17
|
+
targetName: permissionName,
|
|
18
|
+
allowedCaveats: [
|
|
19
|
+
SnapCaveatType.ChainIds,
|
|
20
|
+
SnapCaveatType.LookupMatchers,
|
|
21
|
+
SnapCaveatType.MaxRequestTime
|
|
22
|
+
],
|
|
23
|
+
endowmentGetter: (_getterOptions)=>undefined,
|
|
24
|
+
validator: createGenericPermissionValidator([
|
|
25
|
+
{
|
|
26
|
+
type: SnapCaveatType.ChainIds,
|
|
27
|
+
optional: true
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: SnapCaveatType.LookupMatchers,
|
|
31
|
+
optional: true
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
type: SnapCaveatType.MaxRequestTime,
|
|
35
|
+
optional: true
|
|
36
|
+
}
|
|
37
|
+
]),
|
|
38
|
+
subjectTypes: [
|
|
39
|
+
SubjectType.Snap
|
|
40
|
+
]
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export const nameLookupEndowmentBuilder = Object.freeze({
|
|
44
|
+
targetName: permissionName,
|
|
45
|
+
specificationBuilder
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* Validates the type of the caveat value.
|
|
49
|
+
*
|
|
50
|
+
* @param caveat - The caveat to validate.
|
|
51
|
+
* @throws If the caveat value is invalid.
|
|
52
|
+
*/ function validateCaveat(caveat) {
|
|
53
|
+
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) {
|
|
54
|
+
throw rpcErrors.invalidParams({
|
|
55
|
+
message: 'Expected a plain object.'
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const { value } = caveat;
|
|
59
|
+
switch(caveat.type){
|
|
60
|
+
case SnapCaveatType.ChainIds:
|
|
61
|
+
assertStruct(value, ChainIdsStruct);
|
|
62
|
+
break;
|
|
63
|
+
case SnapCaveatType.LookupMatchers:
|
|
64
|
+
assertStruct(value, LookupMatchersStruct);
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
throw rpcErrors.invalidParams({
|
|
68
|
+
message: 'Invalid caveat type, must be one of the following: "chainIds", "matchers".'
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
74
|
+
* Note that this function does not do any validation, that's handled by the
|
|
75
|
+
* PermissionsController when the permission is requested.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The raw value from the `initialPermissions`.
|
|
78
|
+
* @returns The caveat specification.
|
|
79
|
+
*/ export function getNameLookupCaveatMapper(value) {
|
|
80
|
+
if (!value || !isObject(value) || Object.keys(value).length === 0) {
|
|
81
|
+
return {
|
|
82
|
+
caveats: null
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const caveats = [];
|
|
86
|
+
if (value.chains) {
|
|
87
|
+
caveats.push({
|
|
88
|
+
type: SnapCaveatType.ChainIds,
|
|
89
|
+
value: value.chains
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (value.matchers) {
|
|
93
|
+
caveats.push({
|
|
94
|
+
type: SnapCaveatType.LookupMatchers,
|
|
95
|
+
value: value.matchers
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
assert(caveats.length > 0);
|
|
99
|
+
return {
|
|
100
|
+
caveats: caveats
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Getter function to get the chainIds caveat from a permission.
|
|
105
|
+
*
|
|
106
|
+
* This does basic validation of the caveat, but does not validate the type or
|
|
107
|
+
* value of the namespaces object itself, as this is handled by the
|
|
108
|
+
* `PermissionsController` when the permission is requested.
|
|
109
|
+
*
|
|
110
|
+
* @param permission - The permission to get the `chainIds` caveat from.
|
|
111
|
+
* @returns An array of `chainIds` that the snap supports.
|
|
112
|
+
*/ export function getChainIdsCaveat(permission) {
|
|
113
|
+
if (!permission?.caveats) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const caveat = permission.caveats.find((permCaveat)=>permCaveat.type === SnapCaveatType.ChainIds);
|
|
117
|
+
return caveat ? caveat.value : null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Getter function to get the matchers caveat from a permission.
|
|
121
|
+
*
|
|
122
|
+
* This does basic validation of the caveat, but does not validate the type or
|
|
123
|
+
* value of the namespaces object itself, as this is handled by the
|
|
124
|
+
* `PermissionsController` when the permission is requested.
|
|
125
|
+
*
|
|
126
|
+
* @param permission - The permission to get the `matchers` caveat from.
|
|
127
|
+
* @returns A `matchers` object that defines the input that the snap supports.
|
|
128
|
+
*/ export function getLookupMatchersCaveat(permission) {
|
|
129
|
+
if (!permission?.caveats) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const caveat = permission.caveats.find((permCaveat)=>permCaveat.type === SnapCaveatType.LookupMatchers);
|
|
133
|
+
return caveat ? caveat.value : null;
|
|
134
|
+
}
|
|
135
|
+
export const nameLookupCaveatSpecifications = {
|
|
136
|
+
[SnapCaveatType.ChainIds]: Object.freeze({
|
|
137
|
+
type: SnapCaveatType.ChainIds,
|
|
138
|
+
validator: (caveat)=>validateCaveat(caveat)
|
|
139
|
+
}),
|
|
140
|
+
[SnapCaveatType.LookupMatchers]: Object.freeze({
|
|
141
|
+
type: SnapCaveatType.LookupMatchers,
|
|
142
|
+
validator: (caveat)=>validateCaveat(caveat)
|
|
143
|
+
})
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
//# sourceMappingURL=name-lookup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/endowments/name-lookup.ts"],"sourcesContent":["import type {\n EndowmentGetterParams,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n Caveat,\n CaveatSpecificationConstraint,\n PermissionConstraint,\n CaveatConstraint,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n ChainIdsStruct,\n LookupMatchersStruct,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport {\n assert,\n assertStruct,\n hasProperty,\n isObject,\n isPlainObject,\n} from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.NameLookup;\n\ntype NameLookupEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * `endowment:name-lookup` returns nothing; it is intended to be used as a flag\n * by the extension to detect whether the snap has the capability to resolve a domain/address.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the name-lookup endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n NameLookupEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.ChainIds,\n SnapCaveatType.LookupMatchers,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.ChainIds, optional: true },\n { type: SnapCaveatType.LookupMatchers, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const nameLookupEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validates the type of the caveat value.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveat(caveat: Caveat<string, any>): void {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n switch (caveat.type) {\n case SnapCaveatType.ChainIds:\n assertStruct(value, ChainIdsStruct);\n break;\n case SnapCaveatType.LookupMatchers:\n assertStruct(value, LookupMatchersStruct);\n break;\n default:\n throw rpcErrors.invalidParams({\n message:\n 'Invalid caveat type, must be one of the following: \"chainIds\", \"matchers\".',\n });\n }\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 getNameLookupCaveatMapper(\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 = [];\n\n if (value.chains) {\n caveats.push({\n type: SnapCaveatType.ChainIds,\n value: value.chains,\n });\n }\n\n if (value.matchers) {\n caveats.push({\n type: SnapCaveatType.LookupMatchers,\n value: value.matchers,\n });\n }\n\n assert(caveats.length > 0);\n\n return { caveats: caveats as NonEmptyArray<CaveatConstraint> };\n}\n\n/**\n * Getter function to get the chainIds caveat from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the `chainIds` caveat from.\n * @returns An array of `chainIds` that the snap supports.\n */\nexport function getChainIdsCaveat(\n permission?: PermissionConstraint,\n): string[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n const caveat = permission.caveats.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.ChainIds,\n ) as Caveat<string, string[]> | undefined;\n\n return caveat ? caveat.value : null;\n}\n\n/**\n * Getter function to get the matchers caveat from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the `matchers` caveat from.\n * @returns A `matchers` object that defines the input that the snap supports.\n */\nexport function getLookupMatchersCaveat(\n permission?: PermissionConstraint,\n): Record<string, string[]> | null {\n if (!permission?.caveats) {\n return null;\n }\n\n const caveat = permission.caveats.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.LookupMatchers,\n ) as Caveat<string, Record<string, string[]>> | undefined;\n\n return caveat ? caveat.value : null;\n}\n\nexport const nameLookupCaveatSpecifications: Record<\n SnapCaveatType.ChainIds | SnapCaveatType.LookupMatchers,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.ChainIds]: Object.freeze({\n type: SnapCaveatType.ChainIds,\n validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),\n }),\n [SnapCaveatType.LookupMatchers]: Object.freeze({\n type: SnapCaveatType.LookupMatchers,\n validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),\n }),\n};\n"],"names":["PermissionType","SubjectType","rpcErrors","ChainIdsStruct","LookupMatchersStruct","SnapCaveatType","assert","assertStruct","hasProperty","isObject","isPlainObject","createGenericPermissionValidator","SnapEndowments","permissionName","NameLookup","specificationBuilder","_builderOptions","permissionType","Endowment","targetName","allowedCaveats","ChainIds","LookupMatchers","MaxRequestTime","endowmentGetter","_getterOptions","undefined","validator","type","optional","subjectTypes","Snap","nameLookupEndowmentBuilder","Object","freeze","validateCaveat","caveat","invalidParams","message","value","getNameLookupCaveatMapper","keys","length","caveats","chains","push","matchers","getChainIdsCaveat","permission","find","permCaveat","getLookupMatchersCaveat","nameLookupCaveatSpecifications"],"mappings":"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"}
|