@metamask/snaps-rpc-methods 11.7.0 → 11.9.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 +21 -1
- package/dist/endowments/cronjob.cjs +8 -0
- package/dist/endowments/cronjob.cjs.map +1 -1
- package/dist/endowments/cronjob.d.cts.map +1 -1
- package/dist/endowments/cronjob.d.mts.map +1 -1
- package/dist/endowments/cronjob.mjs +9 -1
- package/dist/endowments/cronjob.mjs.map +1 -1
- package/dist/endowments/enum.cjs +1 -0
- package/dist/endowments/enum.cjs.map +1 -1
- package/dist/endowments/enum.d.cts +2 -1
- package/dist/endowments/enum.d.cts.map +1 -1
- package/dist/endowments/enum.d.mts +2 -1
- package/dist/endowments/enum.d.mts.map +1 -1
- package/dist/endowments/enum.mjs +1 -0
- package/dist/endowments/enum.mjs.map +1 -1
- package/dist/endowments/index.cjs +4 -0
- package/dist/endowments/index.cjs.map +1 -1
- package/dist/endowments/index.d.cts +9 -0
- package/dist/endowments/index.d.cts.map +1 -1
- package/dist/endowments/index.d.mts +9 -0
- package/dist/endowments/index.d.mts.map +1 -1
- package/dist/endowments/index.mjs +4 -0
- package/dist/endowments/index.mjs.map +1 -1
- package/dist/endowments/settings-page.cjs +28 -0
- package/dist/endowments/settings-page.cjs.map +1 -0
- package/dist/endowments/settings-page.d.cts +16 -0
- package/dist/endowments/settings-page.d.cts.map +1 -0
- package/dist/endowments/settings-page.d.mts +16 -0
- package/dist/endowments/settings-page.d.mts.map +1 -0
- package/dist/endowments/settings-page.mjs +25 -0
- package/dist/endowments/settings-page.mjs.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.cjs +69 -0
- package/dist/permitted/cancelBackgroundEvent.cjs.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.d.cts +16 -0
- package/dist/permitted/cancelBackgroundEvent.d.cts.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.d.mts +16 -0
- package/dist/permitted/cancelBackgroundEvent.d.mts.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.mjs +66 -0
- package/dist/permitted/cancelBackgroundEvent.mjs.map +1 -0
- package/dist/permitted/clearState.cjs +73 -0
- package/dist/permitted/clearState.cjs.map +1 -0
- package/dist/permitted/clearState.d.cts +28 -0
- package/dist/permitted/clearState.d.cts.map +1 -0
- package/dist/permitted/clearState.d.mts +28 -0
- package/dist/permitted/clearState.d.mts.map +1 -0
- package/dist/permitted/clearState.mjs +70 -0
- package/dist/permitted/clearState.mjs.map +1 -0
- package/dist/permitted/createInterface.d.cts +2 -2
- package/dist/permitted/createInterface.d.mts +2 -2
- package/dist/permitted/getBackgroundEvents.cjs +42 -0
- package/dist/permitted/getBackgroundEvents.cjs.map +1 -0
- package/dist/permitted/getBackgroundEvents.d.cts +8 -0
- package/dist/permitted/getBackgroundEvents.d.cts.map +1 -0
- package/dist/permitted/getBackgroundEvents.d.mts +8 -0
- package/dist/permitted/getBackgroundEvents.d.mts.map +1 -0
- package/dist/permitted/getBackgroundEvents.mjs +39 -0
- package/dist/permitted/getBackgroundEvents.mjs.map +1 -0
- package/dist/permitted/getState.cjs +118 -0
- package/dist/permitted/getState.cjs.map +1 -0
- package/dist/permitted/getState.d.cts +53 -0
- package/dist/permitted/getState.d.cts.map +1 -0
- package/dist/permitted/getState.d.mts +53 -0
- package/dist/permitted/getState.d.mts.map +1 -0
- package/dist/permitted/getState.mjs +114 -0
- package/dist/permitted/getState.mjs.map +1 -0
- package/dist/permitted/handlers.cjs +12 -0
- package/dist/permitted/handlers.cjs.map +1 -1
- package/dist/permitted/handlers.d.cts +8 -2
- package/dist/permitted/handlers.d.cts.map +1 -1
- package/dist/permitted/handlers.d.mts +8 -2
- package/dist/permitted/handlers.d.mts.map +1 -1
- package/dist/permitted/handlers.mjs +12 -0
- package/dist/permitted/handlers.mjs.map +1 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +7 -1
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +7 -1
- package/dist/permitted/index.d.mts.map +1 -1
- package/dist/permitted/index.mjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.cjs +122 -0
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -0
- package/dist/permitted/scheduleBackgroundEvent.d.cts +33 -0
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -0
- package/dist/permitted/scheduleBackgroundEvent.d.mts +33 -0
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -0
- package/dist/permitted/scheduleBackgroundEvent.mjs +119 -0
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -0
- package/dist/permitted/setState.cjs +154 -0
- package/dist/permitted/setState.cjs.map +1 -0
- package/dist/permitted/setState.d.cts +65 -0
- package/dist/permitted/setState.d.cts.map +1 -0
- package/dist/permitted/setState.d.mts +65 -0
- package/dist/permitted/setState.d.mts.map +1 -0
- package/dist/permitted/setState.mjs +150 -0
- package/dist/permitted/setState.mjs.map +1 -0
- package/dist/permitted/updateInterface.d.cts +2 -2
- package/dist/permitted/updateInterface.d.mts +2 -2
- package/dist/restricted/dialog.d.cts +4 -4
- package/dist/restricted/dialog.d.mts +4 -4
- package/dist/restricted/notify.d.cts +3 -3
- package/dist/restricted/notify.d.mts +3 -3
- package/dist/utils.cjs +22 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +9 -0
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +9 -0
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +20 -0
- package/dist/utils.mjs.map +1 -1
- package/package.json +11 -9
|
@@ -1,16 +1,22 @@
|
|
|
1
|
+
import { cancelBackgroundEventHandler } from "./cancelBackgroundEvent.mjs";
|
|
2
|
+
import { clearStateHandler } from "./clearState.mjs";
|
|
1
3
|
import { createInterfaceHandler } from "./createInterface.mjs";
|
|
2
4
|
import { providerRequestHandler } from "./experimentalProviderRequest.mjs";
|
|
3
5
|
import { getAllSnapsHandler } from "./getAllSnaps.mjs";
|
|
6
|
+
import { getBackgroundEventsHandler } from "./getBackgroundEvents.mjs";
|
|
4
7
|
import { getClientStatusHandler } from "./getClientStatus.mjs";
|
|
5
8
|
import { getCurrencyRateHandler } from "./getCurrencyRate.mjs";
|
|
6
9
|
import { getFileHandler } from "./getFile.mjs";
|
|
7
10
|
import { getInterfaceContextHandler } from "./getInterfaceContext.mjs";
|
|
8
11
|
import { getInterfaceStateHandler } from "./getInterfaceState.mjs";
|
|
9
12
|
import { getSnapsHandler } from "./getSnaps.mjs";
|
|
13
|
+
import { getStateHandler } from "./getState.mjs";
|
|
10
14
|
import { invokeKeyringHandler } from "./invokeKeyring.mjs";
|
|
11
15
|
import { invokeSnapSugarHandler } from "./invokeSnapSugar.mjs";
|
|
12
16
|
import { requestSnapsHandler } from "./requestSnaps.mjs";
|
|
13
17
|
import { resolveInterfaceHandler } from "./resolveInterface.mjs";
|
|
18
|
+
import { scheduleBackgroundEventHandler } from "./scheduleBackgroundEvent.mjs";
|
|
19
|
+
import { setStateHandler } from "./setState.mjs";
|
|
14
20
|
import { updateInterfaceHandler } from "./updateInterface.mjs";
|
|
15
21
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
16
22
|
export const methodHandlers = {
|
|
@@ -19,8 +25,10 @@ export const methodHandlers = {
|
|
|
19
25
|
wallet_requestSnaps: requestSnapsHandler,
|
|
20
26
|
wallet_invokeSnap: invokeSnapSugarHandler,
|
|
21
27
|
wallet_invokeKeyring: invokeKeyringHandler,
|
|
28
|
+
snap_clearState: clearStateHandler,
|
|
22
29
|
snap_getClientStatus: getClientStatusHandler,
|
|
23
30
|
snap_getFile: getFileHandler,
|
|
31
|
+
snap_getState: getStateHandler,
|
|
24
32
|
snap_createInterface: createInterfaceHandler,
|
|
25
33
|
snap_updateInterface: updateInterfaceHandler,
|
|
26
34
|
snap_getInterfaceState: getInterfaceStateHandler,
|
|
@@ -28,6 +36,10 @@ export const methodHandlers = {
|
|
|
28
36
|
snap_resolveInterface: resolveInterfaceHandler,
|
|
29
37
|
snap_getCurrencyRate: getCurrencyRateHandler,
|
|
30
38
|
snap_experimentalProviderRequest: providerRequestHandler,
|
|
39
|
+
snap_scheduleBackgroundEvent: scheduleBackgroundEventHandler,
|
|
40
|
+
snap_cancelBackgroundEvent: cancelBackgroundEventHandler,
|
|
41
|
+
snap_getBackgroundEvents: getBackgroundEventsHandler,
|
|
42
|
+
snap_setState: setStateHandler,
|
|
31
43
|
};
|
|
32
44
|
/* eslint-enable @typescript-eslint/naming-convention */
|
|
33
45
|
export const handlers = Object.values(methodHandlers);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.mjs","sourceRoot":"","sources":["../../src/permitted/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,sBAAsB,EAAE,0CAAsC;AACvE,OAAO,EAAE,kBAAkB,EAAE,0BAAsB;AACnD,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,cAAc,EAAE,sBAAkB;AAC3C,OAAO,EAAE,0BAA0B,EAAE,kCAA8B;AACnE,OAAO,EAAE,wBAAwB,EAAE,gCAA4B;AAC/D,OAAO,EAAE,eAAe,EAAE,uBAAmB;AAC7C,OAAO,EAAE,oBAAoB,EAAE,4BAAwB;AACvD,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,mBAAmB,EAAE,2BAAuB;AACrD,OAAO,EAAE,uBAAuB,EAAE,+BAA2B;AAC7D,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAE3D,yDAAyD;AACzD,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,kBAAkB,EAAE,kBAAkB;IACtC,eAAe,EAAE,eAAe;IAChC,mBAAmB,EAAE,mBAAmB;IACxC,iBAAiB,EAAE,sBAAsB;IACzC,oBA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,GAAG;IAC5B,kBAAkB,EAAE,kBAAkB;IACtC,eAAe,EAAE,eAAe;IAChC,mBAAmB,EAAE,mBAAmB;IACxC,iBAAiB,EAAE,sBAAsB;IACzC,oBAAoB,EAAE,oBAAoB;IAC1C,eAAe,EAAE,iBAAiB;IAClC,oBAAoB,EAAE,sBAAsB;IAC5C,YAAY,EAAE,cAAc;IAC5B,aAAa,EAAE,eAAe;IAC9B,oBAAoB,EAAE,sBAAsB;IAC5C,oBAAoB,EAAE,sBAAsB;IAC5C,sBAAsB,EAAE,wBAAwB;IAChD,wBAAwB,EAAE,0BAA0B;IACpD,qBAAqB,EAAE,uBAAuB;IAC9C,oBAAoB,EAAE,sBAAsB;IAC5C,gCAAgC,EAAE,sBAAsB;IACxD,4BAA4B,EAAE,8BAA8B;IAC5D,0BAA0B,EAAE,4BAA4B;IACxD,wBAAwB,EAAE,0BAA0B;IACpD,aAAa,EAAE,eAAe;CAC/B,CAAC;AACF,wDAAwD;AAExD,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC","sourcesContent":["import { cancelBackgroundEventHandler } from './cancelBackgroundEvent';\nimport { clearStateHandler } from './clearState';\nimport { createInterfaceHandler } from './createInterface';\nimport { providerRequestHandler } from './experimentalProviderRequest';\nimport { getAllSnapsHandler } from './getAllSnaps';\nimport { getBackgroundEventsHandler } from './getBackgroundEvents';\nimport { getClientStatusHandler } from './getClientStatus';\nimport { getCurrencyRateHandler } from './getCurrencyRate';\nimport { getFileHandler } from './getFile';\nimport { getInterfaceContextHandler } from './getInterfaceContext';\nimport { getInterfaceStateHandler } from './getInterfaceState';\nimport { getSnapsHandler } from './getSnaps';\nimport { getStateHandler } from './getState';\nimport { invokeKeyringHandler } from './invokeKeyring';\nimport { invokeSnapSugarHandler } from './invokeSnapSugar';\nimport { requestSnapsHandler } from './requestSnaps';\nimport { resolveInterfaceHandler } from './resolveInterface';\nimport { scheduleBackgroundEventHandler } from './scheduleBackgroundEvent';\nimport { setStateHandler } from './setState';\nimport { updateInterfaceHandler } from './updateInterface';\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const methodHandlers = {\n wallet_getAllSnaps: getAllSnapsHandler,\n wallet_getSnaps: getSnapsHandler,\n wallet_requestSnaps: requestSnapsHandler,\n wallet_invokeSnap: invokeSnapSugarHandler,\n wallet_invokeKeyring: invokeKeyringHandler,\n snap_clearState: clearStateHandler,\n snap_getClientStatus: getClientStatusHandler,\n snap_getFile: getFileHandler,\n snap_getState: getStateHandler,\n snap_createInterface: createInterfaceHandler,\n snap_updateInterface: updateInterfaceHandler,\n snap_getInterfaceState: getInterfaceStateHandler,\n snap_getInterfaceContext: getInterfaceContextHandler,\n snap_resolveInterface: resolveInterfaceHandler,\n snap_getCurrencyRate: getCurrencyRateHandler,\n snap_experimentalProviderRequest: providerRequestHandler,\n snap_scheduleBackgroundEvent: scheduleBackgroundEventHandler,\n snap_cancelBackgroundEvent: cancelBackgroundEventHandler,\n snap_getBackgroundEvents: getBackgroundEventsHandler,\n snap_setState: setStateHandler,\n};\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport const handlers = Object.values(methodHandlers);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAkCA,iDAA2B;AAC3B,mDAA6B","sourcesContent":["import type { CancelBackgroundEventMethodHooks } from './cancelBackgroundEvent';\nimport type { ClearStateHooks } from './clearState';\nimport type { CreateInterfaceMethodHooks } from './createInterface';\nimport type { ProviderRequestMethodHooks } from './experimentalProviderRequest';\nimport type { GetAllSnapsHooks } from './getAllSnaps';\nimport type { GetBackgroundEventsMethodHooks } from './getBackgroundEvents';\nimport type { GetClientStatusHooks } from './getClientStatus';\nimport type { GetCurrencyRateMethodHooks } from './getCurrencyRate';\nimport type { GetInterfaceStateMethodHooks } from './getInterfaceState';\nimport type { GetSnapsHooks } from './getSnaps';\nimport type { GetStateHooks } from './getState';\nimport type { RequestSnapsHooks } from './requestSnaps';\nimport type { ResolveInterfaceMethodHooks } from './resolveInterface';\nimport type { ScheduleBackgroundEventMethodHooks } from './scheduleBackgroundEvent';\nimport type { SetStateHooks } from './setState';\nimport type { UpdateInterfaceMethodHooks } from './updateInterface';\n\nexport type PermittedRpcMethodHooks = ClearStateHooks &\n GetAllSnapsHooks &\n GetClientStatusHooks &\n GetSnapsHooks &\n GetStateHooks &\n RequestSnapsHooks &\n CreateInterfaceMethodHooks &\n UpdateInterfaceMethodHooks &\n GetInterfaceStateMethodHooks &\n ResolveInterfaceMethodHooks &\n GetCurrencyRateMethodHooks &\n ProviderRequestMethodHooks &\n ScheduleBackgroundEventMethodHooks &\n CancelBackgroundEventMethodHooks &\n GetBackgroundEventsMethodHooks &\n SetStateHooks;\n\nexport * from './handlers';\nexport * from './middleware';\n"]}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import type { CancelBackgroundEventMethodHooks } from "./cancelBackgroundEvent.cjs";
|
|
2
|
+
import type { ClearStateHooks } from "./clearState.cjs";
|
|
1
3
|
import type { CreateInterfaceMethodHooks } from "./createInterface.cjs";
|
|
2
4
|
import type { ProviderRequestMethodHooks } from "./experimentalProviderRequest.cjs";
|
|
3
5
|
import type { GetAllSnapsHooks } from "./getAllSnaps.cjs";
|
|
6
|
+
import type { GetBackgroundEventsMethodHooks } from "./getBackgroundEvents.cjs";
|
|
4
7
|
import type { GetClientStatusHooks } from "./getClientStatus.cjs";
|
|
5
8
|
import type { GetCurrencyRateMethodHooks } from "./getCurrencyRate.cjs";
|
|
6
9
|
import type { GetInterfaceStateMethodHooks } from "./getInterfaceState.cjs";
|
|
7
10
|
import type { GetSnapsHooks } from "./getSnaps.cjs";
|
|
11
|
+
import type { GetStateHooks } from "./getState.cjs";
|
|
8
12
|
import type { RequestSnapsHooks } from "./requestSnaps.cjs";
|
|
9
13
|
import type { ResolveInterfaceMethodHooks } from "./resolveInterface.cjs";
|
|
14
|
+
import type { ScheduleBackgroundEventMethodHooks } from "./scheduleBackgroundEvent.cjs";
|
|
15
|
+
import type { SetStateHooks } from "./setState.cjs";
|
|
10
16
|
import type { UpdateInterfaceMethodHooks } from "./updateInterface.cjs";
|
|
11
|
-
export type PermittedRpcMethodHooks = GetAllSnapsHooks & GetClientStatusHooks & GetSnapsHooks & RequestSnapsHooks & CreateInterfaceMethodHooks & UpdateInterfaceMethodHooks & GetInterfaceStateMethodHooks & ResolveInterfaceMethodHooks & GetCurrencyRateMethodHooks & ProviderRequestMethodHooks;
|
|
17
|
+
export type PermittedRpcMethodHooks = ClearStateHooks & GetAllSnapsHooks & GetClientStatusHooks & GetSnapsHooks & GetStateHooks & RequestSnapsHooks & CreateInterfaceMethodHooks & UpdateInterfaceMethodHooks & GetInterfaceStateMethodHooks & ResolveInterfaceMethodHooks & GetCurrencyRateMethodHooks & ProviderRequestMethodHooks & ScheduleBackgroundEventMethodHooks & CancelBackgroundEventMethodHooks & GetBackgroundEventsMethodHooks & SetStateHooks;
|
|
12
18
|
export * from "./handlers.cjs";
|
|
13
19
|
export * from "./middleware.cjs";
|
|
14
20
|
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,0BAA0B,EAAE,0CAAsC;AAChF,OAAO,KAAK,EAAE,gBAAgB,EAAE,0BAAsB;AACtD,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA0B;AAC9D,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,4BAA4B,EAAE,gCAA4B;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GAC1B,0BAA0B,GAC1B,kCAAkC,GAClC,gCAAgC,GAChC,8BAA8B,GAC9B,aAAa,CAAC;AAEhB,+BAA2B;AAC3B,iCAA6B"}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import type { CancelBackgroundEventMethodHooks } from "./cancelBackgroundEvent.mjs";
|
|
2
|
+
import type { ClearStateHooks } from "./clearState.mjs";
|
|
1
3
|
import type { CreateInterfaceMethodHooks } from "./createInterface.mjs";
|
|
2
4
|
import type { ProviderRequestMethodHooks } from "./experimentalProviderRequest.mjs";
|
|
3
5
|
import type { GetAllSnapsHooks } from "./getAllSnaps.mjs";
|
|
6
|
+
import type { GetBackgroundEventsMethodHooks } from "./getBackgroundEvents.mjs";
|
|
4
7
|
import type { GetClientStatusHooks } from "./getClientStatus.mjs";
|
|
5
8
|
import type { GetCurrencyRateMethodHooks } from "./getCurrencyRate.mjs";
|
|
6
9
|
import type { GetInterfaceStateMethodHooks } from "./getInterfaceState.mjs";
|
|
7
10
|
import type { GetSnapsHooks } from "./getSnaps.mjs";
|
|
11
|
+
import type { GetStateHooks } from "./getState.mjs";
|
|
8
12
|
import type { RequestSnapsHooks } from "./requestSnaps.mjs";
|
|
9
13
|
import type { ResolveInterfaceMethodHooks } from "./resolveInterface.mjs";
|
|
14
|
+
import type { ScheduleBackgroundEventMethodHooks } from "./scheduleBackgroundEvent.mjs";
|
|
15
|
+
import type { SetStateHooks } from "./setState.mjs";
|
|
10
16
|
import type { UpdateInterfaceMethodHooks } from "./updateInterface.mjs";
|
|
11
|
-
export type PermittedRpcMethodHooks = GetAllSnapsHooks & GetClientStatusHooks & GetSnapsHooks & RequestSnapsHooks & CreateInterfaceMethodHooks & UpdateInterfaceMethodHooks & GetInterfaceStateMethodHooks & ResolveInterfaceMethodHooks & GetCurrencyRateMethodHooks & ProviderRequestMethodHooks;
|
|
17
|
+
export type PermittedRpcMethodHooks = ClearStateHooks & GetAllSnapsHooks & GetClientStatusHooks & GetSnapsHooks & GetStateHooks & RequestSnapsHooks & CreateInterfaceMethodHooks & UpdateInterfaceMethodHooks & GetInterfaceStateMethodHooks & ResolveInterfaceMethodHooks & GetCurrencyRateMethodHooks & ProviderRequestMethodHooks & ScheduleBackgroundEventMethodHooks & CancelBackgroundEventMethodHooks & GetBackgroundEventsMethodHooks & SetStateHooks;
|
|
12
18
|
export * from "./handlers.mjs";
|
|
13
19
|
export * from "./middleware.mjs";
|
|
14
20
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,0BAA0B,EAAE,0CAAsC;AAChF,OAAO,KAAK,EAAE,gBAAgB,EAAE,0BAAsB;AACtD,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA0B;AAC9D,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,4BAA4B,EAAE,gCAA4B;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAmB;AAChD,OAAO,KAAK,EAAE,iBAAiB,EAAE,2BAAuB;AACxD,OAAO,KAAK,EAAE,2BAA2B,EAAE,+BAA2B;AACtE,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AAEpE,MAAM,MAAM,uBAAuB,GAAG,gBAAgB,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,oCAAgC;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,0BAA0B,EAAE,0CAAsC;AAChF,OAAO,KAAK,EAAE,gBAAgB,EAAE,0BAAsB;AACtD,OAAO,KAAK,EAAE,8BAA8B,EAAE,kCAA8B;AAC5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA0B;AAC9D,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,4BAA4B,EAAE,gCAA4B;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAmB;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAmB;AAChD,OAAO,KAAK,EAAE,iBAAiB,EAAE,2BAAuB;AACxD,OAAO,KAAK,EAAE,2BAA2B,EAAE,+BAA2B;AACtE,OAAO,KAAK,EAAE,kCAAkC,EAAE,sCAAkC;AACpF,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAmB;AAChD,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AAEpE,MAAM,MAAM,uBAAuB,GAAG,eAAe,GACnD,gBAAgB,GAChB,oBAAoB,GACpB,aAAa,GACb,aAAa,GACb,iBAAiB,GACjB,0BAA0B,GAC1B,0BAA0B,GAC1B,4BAA4B,GAC5B,2BAA2B,GAC3B,0BAA0B,GAC1B,0BAA0B,GAC1B,kCAAkC,GAClC,gCAAgC,GAChC,8BAA8B,GAC9B,aAAa,CAAC;AAEhB,+BAA2B;AAC3B,iCAA6B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAkCA,+BAA2B;AAC3B,iCAA6B","sourcesContent":["import type { CancelBackgroundEventMethodHooks } from './cancelBackgroundEvent';\nimport type { ClearStateHooks } from './clearState';\nimport type { CreateInterfaceMethodHooks } from './createInterface';\nimport type { ProviderRequestMethodHooks } from './experimentalProviderRequest';\nimport type { GetAllSnapsHooks } from './getAllSnaps';\nimport type { GetBackgroundEventsMethodHooks } from './getBackgroundEvents';\nimport type { GetClientStatusHooks } from './getClientStatus';\nimport type { GetCurrencyRateMethodHooks } from './getCurrencyRate';\nimport type { GetInterfaceStateMethodHooks } from './getInterfaceState';\nimport type { GetSnapsHooks } from './getSnaps';\nimport type { GetStateHooks } from './getState';\nimport type { RequestSnapsHooks } from './requestSnaps';\nimport type { ResolveInterfaceMethodHooks } from './resolveInterface';\nimport type { ScheduleBackgroundEventMethodHooks } from './scheduleBackgroundEvent';\nimport type { SetStateHooks } from './setState';\nimport type { UpdateInterfaceMethodHooks } from './updateInterface';\n\nexport type PermittedRpcMethodHooks = ClearStateHooks &\n GetAllSnapsHooks &\n GetClientStatusHooks &\n GetSnapsHooks &\n GetStateHooks &\n RequestSnapsHooks &\n CreateInterfaceMethodHooks &\n UpdateInterfaceMethodHooks &\n GetInterfaceStateMethodHooks &\n ResolveInterfaceMethodHooks &\n GetCurrencyRateMethodHooks &\n ProviderRequestMethodHooks &\n ScheduleBackgroundEventMethodHooks &\n CancelBackgroundEventMethodHooks &\n GetBackgroundEventsMethodHooks &\n SetStateHooks;\n\nexport * from './handlers';\nexport * from './middleware';\n"]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scheduleBackgroundEventHandler = void 0;
|
|
4
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
6
|
+
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
7
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
8
|
+
const utils_1 = require("@metamask/utils");
|
|
9
|
+
const luxon_1 = require("luxon");
|
|
10
|
+
const endowments_1 = require("../endowments/index.cjs");
|
|
11
|
+
const methodName = 'snap_scheduleBackgroundEvent';
|
|
12
|
+
const hookNames = {
|
|
13
|
+
scheduleBackgroundEvent: true,
|
|
14
|
+
hasPermission: true,
|
|
15
|
+
};
|
|
16
|
+
exports.scheduleBackgroundEventHandler = {
|
|
17
|
+
methodNames: [methodName],
|
|
18
|
+
implementation: getScheduleBackgroundEventImplementation,
|
|
19
|
+
hookNames,
|
|
20
|
+
};
|
|
21
|
+
const offsetRegex = /Z|([+-]\d{2}:?\d{2})$/u;
|
|
22
|
+
const ScheduleBackgroundEventParametersWithDateStruct = (0, superstruct_1.object)({
|
|
23
|
+
date: (0, superstruct_1.refine)((0, superstruct_1.string)(), 'date', (val) => {
|
|
24
|
+
const date = luxon_1.DateTime.fromISO(val);
|
|
25
|
+
if (date.isValid) {
|
|
26
|
+
// Luxon doesn't have a reliable way to check if timezone info was not provided
|
|
27
|
+
if (!offsetRegex.test(val)) {
|
|
28
|
+
return 'ISO 8601 date must have timezone information';
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return 'Not a valid ISO 8601 date';
|
|
33
|
+
}),
|
|
34
|
+
request: snaps_utils_1.CronjobRpcRequestStruct,
|
|
35
|
+
});
|
|
36
|
+
const ScheduleBackgroundEventParametersWithDurationStruct = (0, superstruct_1.object)({
|
|
37
|
+
duration: (0, superstruct_1.refine)((0, superstruct_1.string)(), 'duration', (val) => {
|
|
38
|
+
const duration = luxon_1.Duration.fromISO(val);
|
|
39
|
+
if (!duration.isValid) {
|
|
40
|
+
return 'Not a valid ISO 8601 duration';
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}),
|
|
44
|
+
request: snaps_utils_1.CronjobRpcRequestStruct,
|
|
45
|
+
});
|
|
46
|
+
const ScheduleBackgroundEventParametersStruct = (0, snaps_sdk_1.selectiveUnion)((val) => {
|
|
47
|
+
if ((0, utils_1.hasProperty)(val, 'date')) {
|
|
48
|
+
return ScheduleBackgroundEventParametersWithDateStruct;
|
|
49
|
+
}
|
|
50
|
+
return ScheduleBackgroundEventParametersWithDurationStruct;
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Generates a `DateTime` object based on if a duration or date is provided.
|
|
54
|
+
*
|
|
55
|
+
* @param params - The validated params from the `snap_scheduleBackgroundEvent` call.
|
|
56
|
+
* @returns A `DateTime` object.
|
|
57
|
+
*/
|
|
58
|
+
function getStartDate(params) {
|
|
59
|
+
if ('duration' in params) {
|
|
60
|
+
return luxon_1.DateTime.fromJSDate(new Date())
|
|
61
|
+
.toUTC()
|
|
62
|
+
.plus(luxon_1.Duration.fromISO(params.duration));
|
|
63
|
+
}
|
|
64
|
+
return luxon_1.DateTime.fromISO(params.date, { setZone: true });
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The `snap_scheduleBackgroundEvent` method implementation.
|
|
68
|
+
*
|
|
69
|
+
* @param req - The JSON-RPC request object.
|
|
70
|
+
* @param res - The JSON-RPC response object.
|
|
71
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
72
|
+
* function.
|
|
73
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
74
|
+
* @param hooks - The RPC method hooks.
|
|
75
|
+
* @param hooks.scheduleBackgroundEvent - The function to schedule a background event.
|
|
76
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.
|
|
77
|
+
* @returns An id representing the background event.
|
|
78
|
+
*/
|
|
79
|
+
async function getScheduleBackgroundEventImplementation(req, res, _next, end, { scheduleBackgroundEvent, hasPermission, }) {
|
|
80
|
+
const { params } = req;
|
|
81
|
+
if (!hasPermission(endowments_1.SnapEndowments.Cronjob)) {
|
|
82
|
+
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const validatedParams = getValidatedParams(params);
|
|
86
|
+
const { request } = validatedParams;
|
|
87
|
+
const date = getStartDate(validatedParams);
|
|
88
|
+
// Make sure any millisecond precision is removed.
|
|
89
|
+
const truncatedDate = date.startOf('second').toISO({
|
|
90
|
+
suppressMilliseconds: true,
|
|
91
|
+
});
|
|
92
|
+
(0, utils_1.assert)(truncatedDate);
|
|
93
|
+
const id = scheduleBackgroundEvent({ date: truncatedDate, request });
|
|
94
|
+
res.result = id;
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
return end(error);
|
|
98
|
+
}
|
|
99
|
+
return end();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct
|
|
103
|
+
* type. Throws if validation fails.
|
|
104
|
+
*
|
|
105
|
+
* @param params - The unvalidated params object from the method request.
|
|
106
|
+
* @returns The validated resolveInterface method parameter object.
|
|
107
|
+
*/
|
|
108
|
+
function getValidatedParams(params) {
|
|
109
|
+
try {
|
|
110
|
+
return (0, superstruct_1.create)(params, ScheduleBackgroundEventParametersStruct);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error instanceof superstruct_1.StructError) {
|
|
114
|
+
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
115
|
+
message: `Invalid params: ${error.message}.`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/* istanbul ignore next */
|
|
119
|
+
throw rpc_errors_1.rpcErrors.internal();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=scheduleBackgroundEvent.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.cjs","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AACjE,mDAK6B;AAE7B,uDAG+B;AAC/B,uDAM+B;AAC/B,2CAIyB;AACzB,iCAA2C;AAE3C,wDAA+C;AAG/C,MAAM,UAAU,GAAG,8BAA8B,CAAC;AAElD,MAAM,SAAS,GAA0D;IACvE,uBAAuB,EAAE,IAAI;IAC7B,aAAa,EAAE,IAAI;CACpB,CAAC;AAeW,QAAA,8BAA8B,GAIvC;IACF,WAAW,EAAE,CAAC,UAAU,CAAC;IACzB,cAAc,EAAE,wCAAwC;IACxD,SAAS;CACV,CAAC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AAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CACzB,MAAe;IAEf,IAAI,CAAC;QACH,OAAO,IAAA,oBAAM,EAAC,MAAM,EAAE,uCAAuC,CAAC,CAAC;IACjE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,yBAAW,EAAE,CAAC;YACjC,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,sBAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport {\n selectiveUnion,\n type JsonRpcRequest,\n type ScheduleBackgroundEventParams,\n type ScheduleBackgroundEventResult,\n} from '@metamask/snaps-sdk';\nimport type { CronjobRpcRequest } from '@metamask/snaps-utils';\nimport {\n CronjobRpcRequestStruct,\n type InferMatching,\n} from '@metamask/snaps-utils';\nimport {\n StructError,\n create,\n object,\n refine,\n string,\n} from '@metamask/superstruct';\nimport {\n assert,\n hasProperty,\n type PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { DateTime, Duration } from 'luxon';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_scheduleBackgroundEvent';\n\nconst hookNames: MethodHooksObject<ScheduleBackgroundEventMethodHooks> = {\n scheduleBackgroundEvent: true,\n hasPermission: true,\n};\n\ntype ScheduleBackgroundEventHookParams = {\n date: string;\n request: CronjobRpcRequest;\n};\n\nexport type ScheduleBackgroundEventMethodHooks = {\n scheduleBackgroundEvent: (\n snapEvent: ScheduleBackgroundEventHookParams,\n ) => string;\n\n hasPermission: (permissionName: string) => boolean;\n};\n\nexport const scheduleBackgroundEventHandler: PermittedHandlerExport<\n ScheduleBackgroundEventMethodHooks,\n ScheduleBackgroundEventParameters,\n ScheduleBackgroundEventResult\n> = {\n methodNames: [methodName],\n implementation: getScheduleBackgroundEventImplementation,\n hookNames,\n};\n\nconst offsetRegex = /Z|([+-]\\d{2}:?\\d{2})$/u;\n\nconst ScheduleBackgroundEventParametersWithDateStruct = object({\n date: refine(string(), 'date', (val) => {\n const date = DateTime.fromISO(val);\n if (date.isValid) {\n // Luxon doesn't have a reliable way to check if timezone info was not provided\n if (!offsetRegex.test(val)) {\n return 'ISO 8601 date must have timezone information';\n }\n return true;\n }\n return 'Not a valid ISO 8601 date';\n }),\n request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersWithDurationStruct = object({\n duration: refine(string(), 'duration', (val) => {\n const duration = Duration.fromISO(val);\n if (!duration.isValid) {\n return 'Not a valid ISO 8601 duration';\n }\n return true;\n }),\n request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {\n if (hasProperty(val, 'date')) {\n return ScheduleBackgroundEventParametersWithDateStruct;\n }\n return ScheduleBackgroundEventParametersWithDurationStruct;\n});\n\nexport type ScheduleBackgroundEventParameters = InferMatching<\n typeof ScheduleBackgroundEventParametersStruct,\n ScheduleBackgroundEventParams\n>;\n\n/**\n * Generates a `DateTime` object based on if a duration or date is provided.\n *\n * @param params - The validated params from the `snap_scheduleBackgroundEvent` call.\n * @returns A `DateTime` object.\n */\nfunction getStartDate(params: ScheduleBackgroundEventParams) {\n if ('duration' in params) {\n return DateTime.fromJSDate(new Date())\n .toUTC()\n .plus(Duration.fromISO(params.duration));\n }\n\n return DateTime.fromISO(params.date, { setZone: true });\n}\n\n/**\n * The `snap_scheduleBackgroundEvent` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.scheduleBackgroundEvent - The function to schedule a background event.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.\n * @returns An id representing the background event.\n */\nasync function getScheduleBackgroundEventImplementation(\n req: JsonRpcRequest<ScheduleBackgroundEventParameters>,\n res: PendingJsonRpcResponse<ScheduleBackgroundEventResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n scheduleBackgroundEvent,\n hasPermission,\n }: ScheduleBackgroundEventMethodHooks,\n): Promise<void> {\n const { params } = req;\n\n if (!hasPermission(SnapEndowments.Cronjob)) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { request } = validatedParams;\n\n const date = getStartDate(validatedParams);\n\n // Make sure any millisecond precision is removed.\n const truncatedDate = date.startOf('second').toISO({\n suppressMilliseconds: true,\n });\n\n assert(truncatedDate);\n\n const id = scheduleBackgroundEvent({ date: truncatedDate, request });\n res.result = id;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated resolveInterface method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n): ScheduleBackgroundEventParameters {\n try {\n return create(params, ScheduleBackgroundEventParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import { type ScheduleBackgroundEventParams, type ScheduleBackgroundEventResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { CronjobRpcRequest } from "@metamask/snaps-utils";
|
|
4
|
+
import { type InferMatching } from "@metamask/snaps-utils";
|
|
5
|
+
type ScheduleBackgroundEventHookParams = {
|
|
6
|
+
date: string;
|
|
7
|
+
request: CronjobRpcRequest;
|
|
8
|
+
};
|
|
9
|
+
export type ScheduleBackgroundEventMethodHooks = {
|
|
10
|
+
scheduleBackgroundEvent: (snapEvent: ScheduleBackgroundEventHookParams) => string;
|
|
11
|
+
hasPermission: (permissionName: string) => boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const scheduleBackgroundEventHandler: PermittedHandlerExport<ScheduleBackgroundEventMethodHooks, ScheduleBackgroundEventParameters, ScheduleBackgroundEventResult>;
|
|
14
|
+
declare const ScheduleBackgroundEventParametersStruct: import("@metamask/superstruct").Struct<{
|
|
15
|
+
date: string;
|
|
16
|
+
request: {
|
|
17
|
+
method: string;
|
|
18
|
+
id?: string | number | null | undefined;
|
|
19
|
+
jsonrpc?: "2.0" | undefined;
|
|
20
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
21
|
+
};
|
|
22
|
+
} | {
|
|
23
|
+
request: {
|
|
24
|
+
method: string;
|
|
25
|
+
id?: string | number | null | undefined;
|
|
26
|
+
jsonrpc?: "2.0" | undefined;
|
|
27
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
28
|
+
};
|
|
29
|
+
duration: string;
|
|
30
|
+
}, null>;
|
|
31
|
+
export type ScheduleBackgroundEventParameters = InferMatching<typeof ScheduleBackgroundEventParametersStruct, ScheduleBackgroundEventParams>;
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=scheduleBackgroundEvent.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.d.cts","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,EAGL,KAAK,6BAA6B,EAClC,KAAK,6BAA6B,EACnC,4BAA4B;AAC7B,OAAO,KAAK,EAAE,iBAAiB,EAAE,8BAA8B;AAC/D,OAAO,EAEL,KAAK,aAAa,EACnB,8BAA8B;AAyB/B,KAAK,iCAAiC,GAAG;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,iBAAiB,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,uBAAuB,EAAE,CACvB,SAAS,EAAE,iCAAiC,KACzC,MAAM,CAAC;IAEZ,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,sBAAsB,CACjE,kCAAkC,EAClC,iCAAiC,EACjC,6BAA6B,CAK9B,CAAC;AA8BF,QAAA,MAAM,uCAAuC;;;;;;;;;;;;;;;;QAK3C,CAAC;AAEH,MAAM,MAAM,iCAAiC,GAAG,aAAa,CAC3D,OAAO,uCAAuC,EAC9C,6BAA6B,CAC9B,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import { type ScheduleBackgroundEventParams, type ScheduleBackgroundEventResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { CronjobRpcRequest } from "@metamask/snaps-utils";
|
|
4
|
+
import { type InferMatching } from "@metamask/snaps-utils";
|
|
5
|
+
type ScheduleBackgroundEventHookParams = {
|
|
6
|
+
date: string;
|
|
7
|
+
request: CronjobRpcRequest;
|
|
8
|
+
};
|
|
9
|
+
export type ScheduleBackgroundEventMethodHooks = {
|
|
10
|
+
scheduleBackgroundEvent: (snapEvent: ScheduleBackgroundEventHookParams) => string;
|
|
11
|
+
hasPermission: (permissionName: string) => boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const scheduleBackgroundEventHandler: PermittedHandlerExport<ScheduleBackgroundEventMethodHooks, ScheduleBackgroundEventParameters, ScheduleBackgroundEventResult>;
|
|
14
|
+
declare const ScheduleBackgroundEventParametersStruct: import("@metamask/superstruct").Struct<{
|
|
15
|
+
date: string;
|
|
16
|
+
request: {
|
|
17
|
+
method: string;
|
|
18
|
+
id?: string | number | null | undefined;
|
|
19
|
+
jsonrpc?: "2.0" | undefined;
|
|
20
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
21
|
+
};
|
|
22
|
+
} | {
|
|
23
|
+
request: {
|
|
24
|
+
method: string;
|
|
25
|
+
id?: string | number | null | undefined;
|
|
26
|
+
jsonrpc?: "2.0" | undefined;
|
|
27
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
28
|
+
};
|
|
29
|
+
duration: string;
|
|
30
|
+
}, null>;
|
|
31
|
+
export type ScheduleBackgroundEventParameters = InferMatching<typeof ScheduleBackgroundEventParametersStruct, ScheduleBackgroundEventParams>;
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=scheduleBackgroundEvent.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.d.mts","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,EAGL,KAAK,6BAA6B,EAClC,KAAK,6BAA6B,EACnC,4BAA4B;AAC7B,OAAO,KAAK,EAAE,iBAAiB,EAAE,8BAA8B;AAC/D,OAAO,EAEL,KAAK,aAAa,EACnB,8BAA8B;AAyB/B,KAAK,iCAAiC,GAAG;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,iBAAiB,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,uBAAuB,EAAE,CACvB,SAAS,EAAE,iCAAiC,KACzC,MAAM,CAAC;IAEZ,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,sBAAsB,CACjE,kCAAkC,EAClC,iCAAiC,EACjC,6BAA6B,CAK9B,CAAC;AA8BF,QAAA,MAAM,uCAAuC;;;;;;;;;;;;;;;;QAK3C,CAAC;AAEH,MAAM,MAAM,iCAAiC,GAAG,aAAa,CAC3D,OAAO,uCAAuC,EAC9C,6BAA6B,CAC9B,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { selectiveUnion } from "@metamask/snaps-sdk";
|
|
3
|
+
import { CronjobRpcRequestStruct } from "@metamask/snaps-utils";
|
|
4
|
+
import { StructError, create, object, refine, string } from "@metamask/superstruct";
|
|
5
|
+
import { assert, hasProperty } from "@metamask/utils";
|
|
6
|
+
import { DateTime, Duration } from "luxon";
|
|
7
|
+
import { SnapEndowments } from "../endowments/index.mjs";
|
|
8
|
+
const methodName = 'snap_scheduleBackgroundEvent';
|
|
9
|
+
const hookNames = {
|
|
10
|
+
scheduleBackgroundEvent: true,
|
|
11
|
+
hasPermission: true,
|
|
12
|
+
};
|
|
13
|
+
export const scheduleBackgroundEventHandler = {
|
|
14
|
+
methodNames: [methodName],
|
|
15
|
+
implementation: getScheduleBackgroundEventImplementation,
|
|
16
|
+
hookNames,
|
|
17
|
+
};
|
|
18
|
+
const offsetRegex = /Z|([+-]\d{2}:?\d{2})$/u;
|
|
19
|
+
const ScheduleBackgroundEventParametersWithDateStruct = object({
|
|
20
|
+
date: refine(string(), 'date', (val) => {
|
|
21
|
+
const date = DateTime.fromISO(val);
|
|
22
|
+
if (date.isValid) {
|
|
23
|
+
// Luxon doesn't have a reliable way to check if timezone info was not provided
|
|
24
|
+
if (!offsetRegex.test(val)) {
|
|
25
|
+
return 'ISO 8601 date must have timezone information';
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return 'Not a valid ISO 8601 date';
|
|
30
|
+
}),
|
|
31
|
+
request: CronjobRpcRequestStruct,
|
|
32
|
+
});
|
|
33
|
+
const ScheduleBackgroundEventParametersWithDurationStruct = object({
|
|
34
|
+
duration: refine(string(), 'duration', (val) => {
|
|
35
|
+
const duration = Duration.fromISO(val);
|
|
36
|
+
if (!duration.isValid) {
|
|
37
|
+
return 'Not a valid ISO 8601 duration';
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}),
|
|
41
|
+
request: CronjobRpcRequestStruct,
|
|
42
|
+
});
|
|
43
|
+
const ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {
|
|
44
|
+
if (hasProperty(val, 'date')) {
|
|
45
|
+
return ScheduleBackgroundEventParametersWithDateStruct;
|
|
46
|
+
}
|
|
47
|
+
return ScheduleBackgroundEventParametersWithDurationStruct;
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* Generates a `DateTime` object based on if a duration or date is provided.
|
|
51
|
+
*
|
|
52
|
+
* @param params - The validated params from the `snap_scheduleBackgroundEvent` call.
|
|
53
|
+
* @returns A `DateTime` object.
|
|
54
|
+
*/
|
|
55
|
+
function getStartDate(params) {
|
|
56
|
+
if ('duration' in params) {
|
|
57
|
+
return DateTime.fromJSDate(new Date())
|
|
58
|
+
.toUTC()
|
|
59
|
+
.plus(Duration.fromISO(params.duration));
|
|
60
|
+
}
|
|
61
|
+
return DateTime.fromISO(params.date, { setZone: true });
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The `snap_scheduleBackgroundEvent` method implementation.
|
|
65
|
+
*
|
|
66
|
+
* @param req - The JSON-RPC request object.
|
|
67
|
+
* @param res - The JSON-RPC response object.
|
|
68
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
69
|
+
* function.
|
|
70
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
71
|
+
* @param hooks - The RPC method hooks.
|
|
72
|
+
* @param hooks.scheduleBackgroundEvent - The function to schedule a background event.
|
|
73
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.
|
|
74
|
+
* @returns An id representing the background event.
|
|
75
|
+
*/
|
|
76
|
+
async function getScheduleBackgroundEventImplementation(req, res, _next, end, { scheduleBackgroundEvent, hasPermission, }) {
|
|
77
|
+
const { params } = req;
|
|
78
|
+
if (!hasPermission(SnapEndowments.Cronjob)) {
|
|
79
|
+
return end(providerErrors.unauthorized());
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const validatedParams = getValidatedParams(params);
|
|
83
|
+
const { request } = validatedParams;
|
|
84
|
+
const date = getStartDate(validatedParams);
|
|
85
|
+
// Make sure any millisecond precision is removed.
|
|
86
|
+
const truncatedDate = date.startOf('second').toISO({
|
|
87
|
+
suppressMilliseconds: true,
|
|
88
|
+
});
|
|
89
|
+
assert(truncatedDate);
|
|
90
|
+
const id = scheduleBackgroundEvent({ date: truncatedDate, request });
|
|
91
|
+
res.result = id;
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
return end(error);
|
|
95
|
+
}
|
|
96
|
+
return end();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct
|
|
100
|
+
* type. Throws if validation fails.
|
|
101
|
+
*
|
|
102
|
+
* @param params - The unvalidated params object from the method request.
|
|
103
|
+
* @returns The validated resolveInterface method parameter object.
|
|
104
|
+
*/
|
|
105
|
+
function getValidatedParams(params) {
|
|
106
|
+
try {
|
|
107
|
+
return create(params, ScheduleBackgroundEventParametersStruct);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
if (error instanceof StructError) {
|
|
111
|
+
throw rpcErrors.invalidParams({
|
|
112
|
+
message: `Invalid params: ${error.message}.`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/* istanbul ignore next */
|
|
116
|
+
throw rpcErrors.internal();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=scheduleBackgroundEvent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.mjs","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AACjE,OAAO,EACL,cAAc,EAIf,4BAA4B;AAE7B,OAAO,EACL,uBAAuB,EAExB,8BAA8B;AAC/B,OAAO,EACL,WAAW,EACX,MAAM,EACN,MAAM,EACN,MAAM,EACN,MAAM,EACP,8BAA8B;AAC/B,OAAO,EACL,MAAM,EACN,WAAW,EAEZ,wBAAwB;AACzB,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc;AAE3C,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAG/C,MAAM,UAAU,GAAG,8BAA8B,CAAC;AAElD,MAAM,SAAS,GAA0D;IACvE,uBAAuB,EAAE,IAAI;IAC7B,aAAa,EAAE,IAAI;CACpB,CAAC;AAeF,MAAM,CAAC,MAAM,8BAA8B,GAIvC;IACF,WAAW,EAAE,CAAC,UAAU,CAAC;IACzB,cAAc,EAAE,wCAAwC;IACxD,SAAS;CACV,CAAC;AAEF,MAAM,WAAW,GAAG,wBAAwB,CAAC;AAE7C,MAAM,+CAA+C,GAAG,MAAM,CAAC;IAC7D,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE;QACrC,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,+EAA+E;YAC/E,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC3B,OAAO,8CAA8C,CAAC;YACxD,CAAC;YACD,OAAO,IAAI,CAAC;QACd,C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ermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport {\n selectiveUnion,\n type JsonRpcRequest,\n type ScheduleBackgroundEventParams,\n type ScheduleBackgroundEventResult,\n} from '@metamask/snaps-sdk';\nimport type { CronjobRpcRequest } from '@metamask/snaps-utils';\nimport {\n CronjobRpcRequestStruct,\n type InferMatching,\n} from '@metamask/snaps-utils';\nimport {\n StructError,\n create,\n object,\n refine,\n string,\n} from '@metamask/superstruct';\nimport {\n assert,\n hasProperty,\n type PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { DateTime, Duration } from 'luxon';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_scheduleBackgroundEvent';\n\nconst hookNames: MethodHooksObject<ScheduleBackgroundEventMethodHooks> = {\n scheduleBackgroundEvent: true,\n hasPermission: true,\n};\n\ntype ScheduleBackgroundEventHookParams = {\n date: string;\n request: CronjobRpcRequest;\n};\n\nexport type ScheduleBackgroundEventMethodHooks = {\n scheduleBackgroundEvent: (\n snapEvent: ScheduleBackgroundEventHookParams,\n ) => string;\n\n hasPermission: (permissionName: string) => boolean;\n};\n\nexport const scheduleBackgroundEventHandler: PermittedHandlerExport<\n ScheduleBackgroundEventMethodHooks,\n ScheduleBackgroundEventParameters,\n ScheduleBackgroundEventResult\n> = {\n methodNames: [methodName],\n implementation: getScheduleBackgroundEventImplementation,\n hookNames,\n};\n\nconst offsetRegex = /Z|([+-]\\d{2}:?\\d{2})$/u;\n\nconst ScheduleBackgroundEventParametersWithDateStruct = object({\n date: refine(string(), 'date', (val) => {\n const date = DateTime.fromISO(val);\n if (date.isValid) {\n // Luxon doesn't have a reliable way to check if timezone info was not provided\n if (!offsetRegex.test(val)) {\n return 'ISO 8601 date must have timezone information';\n }\n return true;\n }\n return 'Not a valid ISO 8601 date';\n }),\n request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersWithDurationStruct = object({\n duration: refine(string(), 'duration', (val) => {\n const duration = Duration.fromISO(val);\n if (!duration.isValid) {\n return 'Not a valid ISO 8601 duration';\n }\n return true;\n }),\n request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {\n if (hasProperty(val, 'date')) {\n return ScheduleBackgroundEventParametersWithDateStruct;\n }\n return ScheduleBackgroundEventParametersWithDurationStruct;\n});\n\nexport type ScheduleBackgroundEventParameters = InferMatching<\n typeof ScheduleBackgroundEventParametersStruct,\n ScheduleBackgroundEventParams\n>;\n\n/**\n * Generates a `DateTime` object based on if a duration or date is provided.\n *\n * @param params - The validated params from the `snap_scheduleBackgroundEvent` call.\n * @returns A `DateTime` object.\n */\nfunction getStartDate(params: ScheduleBackgroundEventParams) {\n if ('duration' in params) {\n return DateTime.fromJSDate(new Date())\n .toUTC()\n .plus(Duration.fromISO(params.duration));\n }\n\n return DateTime.fromISO(params.date, { setZone: true });\n}\n\n/**\n * The `snap_scheduleBackgroundEvent` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.scheduleBackgroundEvent - The function to schedule a background event.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.\n * @returns An id representing the background event.\n */\nasync function getScheduleBackgroundEventImplementation(\n req: JsonRpcRequest<ScheduleBackgroundEventParameters>,\n res: PendingJsonRpcResponse<ScheduleBackgroundEventResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n scheduleBackgroundEvent,\n hasPermission,\n }: ScheduleBackgroundEventMethodHooks,\n): Promise<void> {\n const { params } = req;\n\n if (!hasPermission(SnapEndowments.Cronjob)) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { request } = validatedParams;\n\n const date = getStartDate(validatedParams);\n\n // Make sure any millisecond precision is removed.\n const truncatedDate = date.startOf('second').toISO({\n suppressMilliseconds: true,\n });\n\n assert(truncatedDate);\n\n const id = scheduleBackgroundEvent({ date: truncatedDate, request });\n res.result = id;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated resolveInterface method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n): ScheduleBackgroundEventParameters {\n try {\n return create(params, ScheduleBackgroundEventParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|