-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move execution service to separate file
- Loading branch information
Showing
10 changed files
with
221 additions
and
39 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
65 changes: 65 additions & 0 deletions
65
app/scripts/controller-init/snaps/execution-service-init.test.ts
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,65 @@ | ||
import { | ||
IframeExecutionService, | ||
OffscreenExecutionService, | ||
} from '@metamask/snaps-controllers'; | ||
import { Messenger } from '@metamask/base-controller'; | ||
import { ControllerInitRequest } from '../types'; | ||
import { buildControllerInitRequestMock } from '../test/utils'; | ||
import { | ||
ExecutionServiceMessenger, | ||
getExecutionServiceMessenger, | ||
} from './execution-service-messenger'; | ||
import { ExecutionServiceInit } from './execution-service-init'; | ||
|
||
jest.mock('@metamask/snaps-controllers'); | ||
jest.mock('../../../../shared/modules/mv3.utils', () => ({ | ||
isManifestV3: true, | ||
})); | ||
|
||
function getInitRequestMock(): jest.Mocked< | ||
ControllerInitRequest<ExecutionServiceMessenger> | ||
> { | ||
const baseMessenger = new Messenger<never, never>(); | ||
|
||
const requestMock = { | ||
...buildControllerInitRequestMock(), | ||
controllerMessenger: getExecutionServiceMessenger(baseMessenger), | ||
}; | ||
|
||
return requestMock; | ||
} | ||
|
||
describe('ExecutionServiceInit', () => { | ||
it('initializes the iframe execution service if `chrome.offscreen` is not available', () => { | ||
const { controller } = ExecutionServiceInit(getInitRequestMock()); | ||
expect(controller).toBeInstanceOf(IframeExecutionService); | ||
}); | ||
|
||
it('initializes the offscreen execution service if `chrome.offscreen` is available', () => { | ||
Object.defineProperty(global, 'chrome', { | ||
value: { | ||
offscreen: {}, | ||
}, | ||
}); | ||
|
||
const { controller } = ExecutionServiceInit(getInitRequestMock()); | ||
expect(controller).toBeInstanceOf(OffscreenExecutionService); | ||
}); | ||
|
||
it('passes the proper arguments to the service', () => { | ||
Object.defineProperty(global, 'chrome', { | ||
value: { | ||
offscreen: {}, | ||
}, | ||
}); | ||
|
||
ExecutionServiceInit(getInitRequestMock()); | ||
|
||
const controllerMock = jest.mocked(OffscreenExecutionService); | ||
expect(controllerMock).toHaveBeenCalledWith({ | ||
messenger: expect.any(Object), | ||
offscreenPromise: expect.any(Promise), | ||
setupSnapProvider: expect.any(Function), | ||
}); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
app/scripts/controller-init/snaps/execution-service-init.ts
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,75 @@ | ||
import { | ||
AbstractExecutionService, | ||
IframeExecutionService, | ||
OffscreenExecutionService, | ||
ExecutionServiceArgs, | ||
} from '@metamask/snaps-controllers'; | ||
import { assert } from '@metamask/utils'; | ||
import { SubjectType } from '@metamask/permission-controller'; | ||
import { Duplex } from 'readable-stream'; | ||
import { ControllerInitFunction } from '../types'; | ||
import { isManifestV3 } from '../../../../shared/modules/mv3.utils'; | ||
import { ExecutionServiceMessenger } from './execution-service-messenger'; | ||
|
||
/** | ||
* Initialize the Snaps execution service. | ||
* | ||
* @param request - The request object. | ||
* @param request.controllerMessenger - The messenger to use for the service. | ||
* @param request.offscreenPromise - The promise that resolves when the | ||
* offscreen document is ready. | ||
* @param request.setupUntrustedCommunicationEip1193 - The setup function for | ||
* EIP-1193 communication. | ||
* @returns The initialized controller. | ||
*/ | ||
export const ExecutionServiceInit: ControllerInitFunction< | ||
AbstractExecutionService<unknown>, | ||
ExecutionServiceMessenger | ||
> = ({ | ||
controllerMessenger, | ||
offscreenPromise, | ||
setupUntrustedCommunicationEip1193, | ||
}) => { | ||
const useOffscreenDocument = | ||
isManifestV3 && | ||
typeof chrome !== 'undefined' && | ||
typeof chrome.offscreen !== 'undefined'; | ||
|
||
/** | ||
* Set up the EIP-1193 provider for the given Snap. | ||
* | ||
* @param snapId - The ID of the Snap. | ||
* @param connectionStream - The stream to connect to the Snap. | ||
*/ | ||
function setupSnapProvider(snapId: string, connectionStream: Duplex) { | ||
return setupUntrustedCommunicationEip1193({ | ||
connectionStream, | ||
sender: { snapId }, | ||
subjectType: SubjectType.Snap, | ||
}); | ||
} | ||
|
||
const args: ExecutionServiceArgs = { | ||
messenger: controllerMessenger, | ||
setupSnapProvider, | ||
}; | ||
|
||
if (useOffscreenDocument) { | ||
return { | ||
controller: new OffscreenExecutionService({ | ||
...args, | ||
offscreenPromise, | ||
}), | ||
}; | ||
} | ||
|
||
const iframeUrl = process.env.IFRAME_EXECUTION_ENVIRONMENT_URL; | ||
assert(iframeUrl, 'Missing iframe URL.'); | ||
|
||
return { | ||
controller: new IframeExecutionService({ | ||
...args, | ||
iframeUrl: new URL(iframeUrl), | ||
}), | ||
}; | ||
}; |
12 changes: 12 additions & 0 deletions
12
app/scripts/controller-init/snaps/execution-service-messenger.test.ts
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,12 @@ | ||
import { Messenger, RestrictedMessenger } from '@metamask/base-controller'; | ||
import { getExecutionServiceMessenger } from './execution-service-messenger.ts'; | ||
|
||
describe('getExecutionServiceMessenger', () => { | ||
it('returns a restricted messenger', () => { | ||
const messenger = new Messenger<never, never>(); | ||
const executionServiceMessenger = | ||
getExecutionServiceMessenger(messenger); | ||
|
||
expect(executionServiceMessenger).toBeInstanceOf(RestrictedMessenger); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
app/scripts/controller-init/snaps/execution-service-messenger.ts
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,22 @@ | ||
import { Messenger } from '@metamask/base-controller'; | ||
|
||
export type ExecutionServiceMessenger = ReturnType< | ||
typeof getExecutionServiceMessenger | ||
>; | ||
|
||
/** | ||
* Get a restricted messenger for the execution service. This is scoped to the | ||
* actions and events that the execution service is allowed to handle. | ||
* | ||
* @param messenger - The messenger to restrict. | ||
* @returns The restricted messenger. | ||
*/ | ||
export function getExecutionServiceMessenger( | ||
messenger: Messenger<never, never>, | ||
) { | ||
return messenger.getRestricted({ | ||
name: 'ExecutionService', | ||
allowedEvents: [], | ||
allowedActions: [], | ||
}); | ||
} |
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