Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { | ||
RestrictedControllerMessenger, | ||
HasPermission, | ||
GetPermissions, | ||
} from '@metamask/controllers'; | ||
import { HandlerType, SnapId } from '@metamask/snap-utils'; | ||
import { parseExpression } from 'cron-parser'; | ||
import { nanoid } from 'nanoid'; | ||
import { GetSnap, HandleSnapRequest, SnapAdded, SnapEndowments } from '..'; | ||
import { Timer } from '../snaps/Timer'; | ||
|
||
export type CronjobServiceActions = | ||
| GetSnap | ||
| HandleSnapRequest | ||
| HasPermission | ||
| GetPermissions; | ||
|
||
export type CronjobServiceEvents = SnapAdded; | ||
|
||
export type CronjobServiceMessenger = RestrictedControllerMessenger< | ||
'CronjobService', | ||
CronjobServiceActions, | ||
CronjobServiceEvents, | ||
CronjobServiceActions['type'], | ||
CronjobServiceEvents['type'] | ||
>; | ||
|
||
export type CronjobServiceArgs = { | ||
messenger: CronjobServiceMessenger; | ||
}; | ||
|
||
export type CronjobData = { | ||
jobs: Cronjob[]; | ||
}; | ||
|
||
export type CronjobDefinition = { | ||
expression: string; | ||
request: Record<string, unknown>; | ||
}; | ||
|
||
export type Cronjob = { | ||
timer?: Timer; | ||
id: string; | ||
} & CronjobDefinition; | ||
|
||
export class CronjobService { | ||
private _messenger: CronjobServiceMessenger; | ||
|
||
private _snaps: Map<SnapId, string[]>; | ||
|
||
private _jobs: Map<string, Cronjob>; | ||
|
||
constructor({ messenger }: CronjobServiceArgs) { | ||
this._snaps = new Map(); | ||
this._jobs = new Map(); | ||
this._messenger = messenger; | ||
} | ||
|
||
async register(snapId: SnapId) { | ||
const hasCronjob = await this._messenger.call( | ||
'PermissionController:hasPermission', | ||
snapId, | ||
SnapEndowments.cronjob, | ||
); | ||
if (!hasCronjob) { | ||
return; | ||
} | ||
const permissions = await this._messenger.call( | ||
'PermissionController:getPermissions', | ||
snapId, | ||
); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const cronjobPermission = permissions![SnapEndowments.cronjob]!; | ||
// @todo Figure out how to get this from the permission | ||
const definitions: CronjobDefinition[] = (cronjobPermission as any).jobs; | ||
|
||
const jobs = definitions.map((definition) => { | ||
const id = nanoid(); | ||
return { ...definition, id }; | ||
}); | ||
|
||
this._snaps.set( | ||
snapId, | ||
jobs.map((job) => job.id), | ||
); | ||
|
||
jobs.forEach((job) => this.schedule(snapId, job)); | ||
} | ||
|
||
unregister(snapId: SnapId) { | ||
const jobs = this._snaps.get(snapId); | ||
jobs?.forEach((id) => { | ||
const job = this._jobs.get(id); | ||
job?.timer?.cancel(); | ||
}); | ||
this._snaps.delete(snapId); | ||
} | ||
|
||
schedule(snapId: SnapId, job: Cronjob) { | ||
const parsed = parseExpression(job.expression); | ||
if (!parsed.hasNext()) { | ||
return; | ||
} | ||
const next = parsed.next(); | ||
const now = new Date(); | ||
const ms = now.getTime() - next.getTime(); | ||
const timer = new Timer(ms); | ||
timer.start(() => { | ||
this._messenger.call('SnapController:handleRequest', { | ||
snapId, | ||
// @todo Decide on origin for requests like this | ||
origin: 'METAMASK', | ||
handler: HandlerType.OnCronjob, | ||
request: job.request, | ||
}); | ||
this.schedule(snapId, job); | ||
}); | ||
this._jobs.set(job.id, { ...job, timer }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { PermissionType } from '@metamask/controllers'; | ||
import { cronjobEndowmentBuilder } from './cronjob'; | ||
import { SnapEndowments } from '.'; | ||
|
||
describe('endowment:cronjob', () => { | ||
it('builds the expected permission specification', () => { | ||
const specification = cronjobEndowmentBuilder.specificationBuilder({}); | ||
expect(specification).toStrictEqual({ | ||
permissionType: PermissionType.Endowment, | ||
targetKey: SnapEndowments.cronjob, | ||
endowmentGetter: expect.any(Function), | ||
allowedCaveats: ['cronjobCaveat'], | ||
}); | ||
|
||
expect(specification.endowmentGetter()).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
PermissionSpecificationBuilder, | ||
PermissionType, | ||
EndowmentGetterParams, | ||
ValidPermissionSpecification, | ||
} from '@metamask/controllers'; | ||
import { SnapEndowments } from './enum'; | ||
|
||
const permissionName = SnapEndowments.cronjob; | ||
|
||
type CronjobEndowmentSpecification = ValidPermissionSpecification<{ | ||
permissionType: PermissionType.Endowment; | ||
targetKey: typeof permissionName; | ||
endowmentGetter: (_options?: any) => undefined; | ||
allowedCaveats: null; | ||
}>; | ||
|
||
/** | ||
* `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs. | ||
* | ||
* @param _builderOptions - Optional specification builder options. | ||
* @returns The specification for the cronjob endowment. | ||
*/ | ||
const specificationBuilder: PermissionSpecificationBuilder< | ||
PermissionType.Endowment, | ||
any, | ||
CronjobEndowmentSpecification | ||
> = (_builderOptions?: any) => { | ||
return { | ||
permissionType: PermissionType.Endowment, | ||
targetKey: permissionName, | ||
// @todo not allowed by types? | ||
allowedCaveats: ['cronjobCaveat'] as any, | ||
endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined, | ||
}; | ||
}; | ||
|
||
export const cronjobEndowmentBuilder = Object.freeze({ | ||
targetKey: permissionName, | ||
specificationBuilder, | ||
} as const); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters