Skip to content

Commit

Permalink
Add controller action
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Jul 21, 2022
1 parent eaa5c25 commit a90587f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module.exports = {
coverageThreshold: {
global: {
branches: 72.31,
functions: 86.26,
lines: 85.2,
statements: 85.28,
functions: 86.32,
lines: 85.23,
statements: 85.31,
},
},
globals: {
Expand Down
42 changes: 42 additions & 0 deletions packages/controllers/src/snaps/SnapController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const getSnapControllerMessenger = (
'SnapController:add',
'SnapController:get',
'SnapController:handleRpcRequest',
'SnapController:handleTransactionInsightRequest',
'SnapController:getSnapState',
'SnapController:has',
'SnapController:updateSnapState',
Expand Down Expand Up @@ -3363,6 +3364,47 @@ describe('SnapController', () => {
});
});

describe('SnapController:handleTransactionInsightRequest', () => {
it('handles a transaction insight request', async () => {
const messenger = getSnapControllerMessenger(undefined, false);
const fooSnapObject = getSnapObject({
initialPermissions: {},
permissionName: 'fooperm',
version: '0.0.1',
sourceCode: MOCK_SNAP_SOURCE_CODE,
id: 'npm:fooSnap',
manifest: getSnapManifest(),
enabled: true,
status: SnapStatus.running,
});

const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: {
'npm:fooSnap': fooSnapObject,
},
},
}),
);

const handleRequestSpy = jest
.spyOn(snapController, 'handleTransactionInsightRequest')
.mockResolvedValueOnce(true);

expect(
await messenger.call(
'SnapController:handleTransactionInsightRequest',
'npm:fooSnap',
'foo',
{},
),
).toStrictEqual(true);
expect(handleRequestSpy).toHaveBeenCalledTimes(1);
});
});

describe('SnapController:getSnapState', () => {
it(`gets the snap's state`, async () => {
const messenger = getSnapControllerMessenger(undefined, false);
Expand Down
14 changes: 14 additions & 0 deletions packages/controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ export type HandleSnapRpcRequest = {
handler: SnapController['handleRpcRequest'];
};

/**
* Handles sending an inbound transaction insight message to a snap and returns its result.
*/
export type HandleSnapTransactionInsightRequest = {
type: `${typeof controllerName}:handleTransactionInsightRequest`;
handler: SnapController['handleTransactionInsightRequest'];
};

/**
* Gets the specified Snap's persisted state.
*/
Expand Down Expand Up @@ -315,6 +323,7 @@ export type SnapControllerActions =
| GetSnap
| GetSnapState
| HandleSnapRpcRequest
| HandleSnapTransactionInsightRequest
| HasSnap
| UpdateBlockedSnaps
| UpdateSnapState;
Expand Down Expand Up @@ -774,6 +783,11 @@ export class SnapController extends BaseController<
(...args) => this.handleRpcRequest(...args),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:handleTransactionInsightRequest`,
(...args) => this.handleTransactionInsightRequest(...args),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:has`,
(...args) => this.has(...args),
Expand Down

0 comments on commit a90587f

Please sign in to comment.