Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Transaction Insight API #642

Merged
merged 52 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
06d4ed3
Add support for multiple handler exports
FrederikBolding Jul 19, 2022
9f17c20
Add permission
FrederikBolding Jul 19, 2022
c42e480
Fix types and tests
FrederikBolding Jul 19, 2022
700db20
Use enum for HandlerType
FrederikBolding Jul 20, 2022
b555210
Add SnapEndowments enum
FrederikBolding Jul 20, 2022
94a8578
Rename export
FrederikBolding Jul 20, 2022
75eb7ed
Use named args and improve types
FrederikBolding Jul 20, 2022
4423b09
Add controller action
FrederikBolding Jul 21, 2022
450b290
Fix after rebase
FrederikBolding Jul 22, 2022
b826e12
Fix tests after rebase
FrederikBolding Jul 22, 2022
13c42e5
addressed PR comments
hmalik88 Aug 11, 2022
b2c2e59
fixed enum according to ts naming convention
hmalik88 Aug 11, 2022
6438e47
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 16, 2022
781c591
address more PR comments
hmalik88 Aug 17, 2022
888ced5
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 17, 2022
5f70c0f
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 17, 2022
6f1aa2c
more fixes
hmalik88 Aug 17, 2022
a3b0bbd
Add reference to utils
Mrtenz Aug 18, 2022
a93f472
Add support for multiple handler exports
FrederikBolding Jul 19, 2022
7cfeb7b
Add permission
FrederikBolding Jul 19, 2022
d80e669
Fix types and tests
FrederikBolding Jul 19, 2022
ee744b7
Use enum for HandlerType
FrederikBolding Jul 20, 2022
4ea3a57
Add SnapEndowments enum
FrederikBolding Jul 20, 2022
b880143
Rename export
FrederikBolding Jul 20, 2022
ea0ef5b
Use named args and improve types
FrederikBolding Jul 20, 2022
00b43db
Add controller action
FrederikBolding Jul 21, 2022
fdaa8ba
Fix after rebase
FrederikBolding Jul 22, 2022
7f5c804
Fix tests after rebase
FrederikBolding Jul 22, 2022
026c4bf
addressed PR comments
hmalik88 Aug 11, 2022
612abb5
fixed enum according to ts naming convention
hmalik88 Aug 11, 2022
72b8a4f
address more PR comments
hmalik88 Aug 17, 2022
46022c4
more fixes
hmalik88 Aug 17, 2022
f0d92d6
Add reference to utils
Mrtenz Aug 18, 2022
ab1c684
Fix circular import
FrederikBolding Aug 22, 2022
4198a5d
Fix build
FrederikBolding Aug 22, 2022
056ce89
Fix a bunch of tests
FrederikBolding Aug 22, 2022
5e4dde6
Update coverage
FrederikBolding Aug 22, 2022
42a417f
Fix test
FrederikBolding Aug 22, 2022
78c497a
Merge branch 'fb/tx-insights' of github.com:MetaMask/snaps-skunkworks…
hmalik88 Aug 22, 2022
e00458b
addressed PR comments
hmalik88 Aug 22, 2022
ee7d5fa
updated test
hmalik88 Aug 22, 2022
33e052b
fixed build issue
hmalik88 Aug 22, 2022
4cee36e
update jest config
hmalik88 Aug 22, 2022
91ca88a
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 22, 2022
f8a6af8
addressed nit
hmalik88 Aug 22, 2022
3591713
updated jest config again
hmalik88 Aug 23, 2022
e9fdf89
Merge branch 'main' into fb/tx-insights
rekmarks Aug 23, 2022
479498e
Remove ChainId type
rekmarks Aug 23, 2022
84f2328
Fix isCaipChainId implementation
rekmarks Aug 23, 2022
45af1c7
Remove needless 'any', re-enable lint rule, fixup
rekmarks Aug 23, 2022
286c080
Fix utils test coverage
rekmarks Aug 23, 2022
c524b38
fixup! Fix utils test coverage
rekmarks Aug 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module.exports = {
coveragePathIgnorePatterns: ['/node_modules/', '/mocks/', '/test/'],
coverageThreshold: {
global: {
branches: 82.53,
functions: 95.63,
lines: 93.84,
statements: 93.87,
branches: 82.31,
functions: 95.69,
lines: 93.79,
statements: 93.83,
},
},
globals: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerMessenger } from '@metamask/controllers';
import { HandlerType } from '@metamask/snap-utils';
import {
ErrorMessageEvent,
ExecutionServiceMessenger,
Expand Down Expand Up @@ -127,9 +128,13 @@ describe('AbstractExecutionService', () => {

const snapId = 'TestSnap';
await expect(
service.handleRpcRequest(snapId, 'foo.com', {
id: 6,
method: 'bar',
service.handleRpcRequest(snapId, {
origin: 'foo.com',
handler: HandlerType.OnRpcRequest,
request: {
id: 6,
method: 'bar',
},
}),
).rejects.toThrow(
`Snap execution service returned no RPC handler for running snap "${snapId}".`,
Expand Down
33 changes: 14 additions & 19 deletions packages/controllers/src/services/AbstractExecutionService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Duplex } from 'stream';
import ObjectMultiplex from '@metamask/object-multiplex';
import { ErrorJSON, SnapExecutionData } from '@metamask/snap-types';
import { SNAP_STREAM_NAMES } from '@metamask/execution-environments';
import {
ErrorJSON,
SnapExecutionData,
SnapRpcHook,
SnapRpcHookArgs,
} from '@metamask/snap-types';
import { SNAP_STREAM_NAMES } from '@metamask/snap-utils';
import {
Duration,
isJsonRpcRequest,
Expand Down Expand Up @@ -34,12 +39,6 @@ export type ExecutionServiceArgs = {
terminationTimeout?: number;
};

// The snap is the callee
export type SnapRpcHook = (
origin: string,
request: Record<string, unknown>,
) => Promise<unknown>;

export type JobStreams = {
command: Duplex;
rpc: Duplex;
Expand Down Expand Up @@ -93,8 +92,8 @@ export abstract class AbstractExecutionService<WorkerType>
private registerMessageHandlers(): void {
this._messenger.registerActionHandler(
`${controllerName}:handleRpcRequest`,
(snapId: string, origin: string, _request: Record<string, unknown>) =>
this.handleRpcRequest(snapId, origin, _request),
(snapId: string, options: SnapRpcHookArgs) =>
this.handleRpcRequest(snapId, options),
);

this._messenger.registerActionHandler(
Expand Down Expand Up @@ -379,16 +378,14 @@ export abstract class AbstractExecutionService<WorkerType>
}

protected _createSnapHooks(snapId: string, workerId: string) {
const rpcHook = async (
origin: string,
request: Record<string, unknown>,
) => {
const rpcHook = async ({ origin, handler, request }: SnapRpcHookArgs) => {
return await this._command(workerId, {
id: nanoid(),
jsonrpc: '2.0',
method: 'snapRpc',
params: {
origin,
handler,
request,
target: snapId,
},
Expand Down Expand Up @@ -438,14 +435,12 @@ export abstract class AbstractExecutionService<WorkerType>
* Handle RPC request.
*
* @param snapId - The ID of the recipient snap.
* @param origin - The origin of the RPC request.
* @param request - The JSON-RPC request object.
* @param options - Bag of options to pass to the RPC handler.
* @returns Promise that can handle the request.
*/
public async handleRpcRequest(
snapId: string,
origin: string,
request: Record<string, unknown>,
options: SnapRpcHookArgs,
): Promise<unknown> {
const rpcRequestHandler = await this.getRpcRequestHandler(snapId);

Expand All @@ -455,7 +450,7 @@ export abstract class AbstractExecutionService<WorkerType>
);
}

return rpcRequestHandler(origin, request);
return rpcRequestHandler(options);
}
}

Expand Down
11 changes: 8 additions & 3 deletions packages/controllers/src/services/ExecutionService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { RestrictedControllerMessenger } from '@metamask/controllers';
import { SnapExecutionData, SnapId, ErrorJSON } from '@metamask/snap-types';
import { SnapId } from '@metamask/snap-utils';
import {
SnapExecutionData,
ErrorJSON,
SnapRpcHookArgs,
} from '@metamask/snap-types';

type TerminateSnap = (snapId: string) => Promise<void>;
type TerminateAll = () => Promise<void>;
type ExecuteSnap = (snapData: SnapExecutionData) => Promise<unknown>;

type HandleRpcRequest = (
snapId: string,
origin: string,
_request: Record<string, unknown>,
options: SnapRpcHookArgs,
) => Promise<unknown>;

export interface ExecutionService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'assert';
import { ControllerMessenger } from '@metamask/controllers';
import { JsonRpcEngine } from 'json-rpc-engine';
import { createEngineStream } from 'json-rpc-middleware-stream';
import pump from 'pump';
import { HandlerType } from '@metamask/snap-utils';
import { ErrorMessageEvent } from '../ExecutionService';
import { setupMultiplex } from '../AbstractExecutionService';
import { IframeExecutionService } from './IframeExecutionService';
Expand Down Expand Up @@ -178,18 +178,16 @@ describe('IframeExecutionService', () => {

expect(executeResult).toBe('OK');

const result = await iframeExecutionService.handleRpcRequest(
snapId,
'foo',
{
const result = await iframeExecutionService.handleRpcRequest(snapId, {
origin: 'foo',
handler: HandlerType.OnRpcRequest,
request: {
jsonrpc: '2.0',
id: 1,
method: 'foobar',
params: [],
},
);

assert(result !== undefined);
});

expect(result).toBe(blockNumber);

Expand Down
8 changes: 2 additions & 6 deletions packages/controllers/src/services/iframe/test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ export async function start(port = PORT) {
reject(new Error(`Invalid port: "${port}"`));
}

const executionEnvPath = require.resolve(
'@metamask/execution-environments',
);
const bundlePath = path.resolve(
executionEnvPath,
'../../__test__/iframe-test/bundle.js',
const bundlePath = require.resolve(
'@metamask/execution-environments/__test__/iframe-test/bundle.js',
);
const publicPath = path.resolve(bundlePath, '../');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ControllerMessenger } from '@metamask/controllers';
import { ErrorJSON, SnapId } from '@metamask/snap-types';
import { ErrorJSON } from '@metamask/snap-types';
import { HandlerType, SnapId } from '@metamask/snap-utils';
import { JsonRpcEngine } from 'json-rpc-engine';
import { createEngineStream } from 'json-rpc-middleware-stream';
import pump from 'pump';
import { ErrorMessageEvent } from '../ExecutionService';
import { setupMultiplex } from '../AbstractExecutionService';
import { NodeProcessExecutionService } from './NodeProcessExecutionService';

const ON_RPC_REQUEST = HandlerType.OnRpcRequest;

describe('NodeProcessExecutionService', () => {
it('can boot', async () => {
const controllerMessenger = new ControllerMessenger<
Expand Down Expand Up @@ -119,11 +122,15 @@ describe('NodeProcessExecutionService', () => {
});

await expect(
service.handleRpcRequest(snapId, 'fooOrigin', {
jsonrpc: '2.0',
method: 'foo',
params: {},
id: 1,
service.handleRpcRequest(snapId, {
origin: 'fooOrigin',
handler: ON_RPC_REQUEST,
request: {
jsonrpc: '2.0',
method: 'foo',
params: {},
id: 1,
},
}),
).rejects.toThrow('foobar');
await service.terminateAllSnaps();
Expand Down Expand Up @@ -177,14 +184,18 @@ describe('NodeProcessExecutionService', () => {
);
});

const result = await service.handleRpcRequest(snapId, 'fooOrigin', {
jsonrpc: '2.0',
method: '',
params: {},
id: 1,
});

expect(result).toBe('foo');
expect(
await service.handleRpcRequest(snapId, {
origin: 'fooOrigin',
handler: ON_RPC_REQUEST,
request: {
jsonrpc: '2.0',
method: '',
params: {},
id: 1,
},
}),
).toBe('foo');

// eslint-disable-next-line jest/prefer-strict-equal
expect(await unhandledErrorPromise).toEqual({
Expand Down Expand Up @@ -245,11 +256,15 @@ describe('NodeProcessExecutionService', () => {

expect(executeResult).toBe('OK');

const result = await service.handleRpcRequest(snapId, 'foo', {
jsonrpc: '2.0',
id: 1,
method: 'foobar',
params: [],
const result = await service.handleRpcRequest(snapId, {
origin: 'foo',
handler: ON_RPC_REQUEST,
request: {
jsonrpc: '2.0',
id: 1,
method: 'foobar',
params: [],
},
});

expect(result).toBe(blockNumber);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ControllerMessenger } from '@metamask/controllers';
import { ErrorJSON, SnapId } from '@metamask/snap-types';
import { ErrorJSON } from '@metamask/snap-types';
import { HandlerType, SnapId } from '@metamask/snap-utils';
import { JsonRpcEngine } from 'json-rpc-engine';
import { createEngineStream } from 'json-rpc-middleware-stream';
import pump from 'pump';
import { ErrorMessageEvent } from '../ExecutionService';
import { setupMultiplex } from '../AbstractExecutionService';
import { NodeThreadExecutionService } from './NodeThreadExecutionService';

const ON_RPC_REQUEST = HandlerType.OnRpcRequest;

describe('NodeThreadExecutionService', () => {
it('can boot', async () => {
const controllerMessenger = new ControllerMessenger<
Expand Down Expand Up @@ -119,11 +122,15 @@ describe('NodeThreadExecutionService', () => {
});

await expect(
service.handleRpcRequest(snapId, 'fooOrigin', {
jsonrpc: '2.0',
method: 'foo',
params: {},
id: 1,
service.handleRpcRequest(snapId, {
origin: 'fooOrigin',
handler: ON_RPC_REQUEST,
request: {
jsonrpc: '2.0',
method: 'foo',
params: {},
id: 1,
},
}),
).rejects.toThrow('foobar');
await service.terminateAllSnaps();
Expand Down Expand Up @@ -177,14 +184,18 @@ describe('NodeThreadExecutionService', () => {
);
});

const result = await service.handleRpcRequest(snapId, 'fooOrigin', {
jsonrpc: '2.0',
method: '',
params: {},
id: 1,
});

expect(result).toBe('foo');
expect(
await service.handleRpcRequest(snapId, {
origin: 'fooOrigin',
handler: ON_RPC_REQUEST,
request: {
jsonrpc: '2.0',
method: '',
params: {},
id: 1,
},
}),
).toBe('foo');

// eslint-disable-next-line jest/prefer-strict-equal
expect(await unhandledErrorPromise).toEqual({
Expand Down Expand Up @@ -245,11 +256,15 @@ describe('NodeThreadExecutionService', () => {

expect(executeResult).toBe('OK');

const result = await service.handleRpcRequest(snapId, 'foo', {
jsonrpc: '2.0',
id: 1,
method: 'foobar',
params: [],
const result = await service.handleRpcRequest(snapId, {
origin: 'foo',
handler: ON_RPC_REQUEST,
request: {
jsonrpc: '2.0',
id: 1,
method: 'foobar',
params: [],
},
});

expect(result).toBe(blockNumber);
Expand Down
Loading