Skip to content

Commit d5c9158

Browse files
authored
chore: Move handlers related code to a handlers folder in snaps-utils (#3289)
This PR moves the handlers related code to the `handlers` folder in `snaps-utils` to debloat the `handlers.ts` file.
1 parent 2150888 commit d5c9158

File tree

14 files changed

+164
-127
lines changed

14 files changed

+164
-127
lines changed

packages/snaps-utils/coverage.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"branches": 99.75,
33
"functions": 98.94,
4-
"lines": 99.62,
5-
"statements": 96.99
4+
"lines": 98.55,
5+
"statements": 97.03
66
}

packages/snaps-utils/src/eval-worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'ses/lockdown';
66

77
import { readFileSync } from 'fs';
88

9-
import type { HandlerType } from './handler-types';
10-
import { SNAP_EXPORT_NAMES } from './handler-types';
9+
import type { HandlerType } from './handlers';
10+
import { SNAP_EXPORT_NAMES } from './handlers';
1111
import { generateMockEndowments } from './mock';
1212

1313
declare let lockdown: any, Compartment: any;

packages/snaps-utils/src/handlers.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { SNAP_EXPORT_NAMES, SNAP_EXPORTS } from './exports';
2+
3+
describe('SNAP_EXPORTS', () => {
4+
describe('validator', () => {
5+
it.each(Object.values(SNAP_EXPORTS))(
6+
'validates that the snap export is a function',
7+
({ validator }) => {
8+
expect(validator(() => undefined)).toBe(true);
9+
expect(validator('')).toBe(false);
10+
},
11+
);
12+
});
13+
});
14+
15+
describe('SNAP_EXPORT_NAMES', () => {
16+
it('is an array of all handler types', () => {
17+
expect(SNAP_EXPORT_NAMES).toStrictEqual([
18+
'onRpcRequest',
19+
'onSignature',
20+
'onTransaction',
21+
'onCronjob',
22+
'onInstall',
23+
'onUpdate',
24+
'onNameLookup',
25+
'onKeyringRequest',
26+
'onHomePage',
27+
'onSettingsPage',
28+
'onUserInput',
29+
'onAssetsLookup',
30+
'onAssetsConversion',
31+
'onProtocolRequest',
32+
]);
33+
});
34+
});

packages/snaps-utils/src/handlers.ts renamed to packages/snaps-utils/src/handlers/exports.ts

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,8 @@ import type {
1414
OnUpdateHandler,
1515
OnUserInputHandler,
1616
} from '@metamask/snaps-sdk';
17-
import { ComponentOrElementStruct, SeverityLevel } from '@metamask/snaps-sdk';
18-
import {
19-
assign,
20-
literal,
21-
nullable,
22-
object,
23-
optional,
24-
string,
25-
array,
26-
size,
27-
union,
28-
} from '@metamask/superstruct';
2917

30-
import type { SnapHandler } from './handler-types';
31-
import { HandlerType } from './handler-types';
32-
33-
export type SnapRpcHookArgs = {
34-
origin: string;
35-
handler: HandlerType;
36-
request: Record<string, unknown>;
37-
};
18+
import { HandlerType } from './types';
3819

3920
export const SNAP_EXPORTS = {
4021
[HandlerType.OnRpcRequest]: {
@@ -141,89 +122,4 @@ export const SNAP_EXPORTS = {
141122
},
142123
} as const;
143124

144-
export const OnTransactionSeverityResponseStruct = object({
145-
severity: optional(literal(SeverityLevel.Critical)),
146-
});
147-
148-
export const OnTransactionResponseWithIdStruct = assign(
149-
OnTransactionSeverityResponseStruct,
150-
object({
151-
id: string(),
152-
}),
153-
);
154-
155-
export const OnTransactionResponseWithContentStruct = assign(
156-
OnTransactionSeverityResponseStruct,
157-
object({
158-
content: ComponentOrElementStruct,
159-
}),
160-
);
161-
162-
export const OnTransactionResponseStruct = nullable(
163-
union([
164-
OnTransactionResponseWithContentStruct,
165-
OnTransactionResponseWithIdStruct,
166-
]),
167-
);
168-
169-
export const OnSignatureResponseStruct = OnTransactionResponseStruct;
170-
171-
export const OnHomePageResponseWithContentStruct = object({
172-
content: ComponentOrElementStruct,
173-
});
174-
175-
export const OnHomePageResponseWithIdStruct = object({
176-
id: string(),
177-
});
178-
179-
export const OnHomePageResponseStruct = union([
180-
OnHomePageResponseWithContentStruct,
181-
OnHomePageResponseWithIdStruct,
182-
]);
183-
184-
export const OnSettingsPageResponseStruct = OnHomePageResponseStruct;
185-
186-
export const AddressResolutionStruct = object({
187-
protocol: string(),
188-
resolvedDomain: string(),
189-
});
190-
191-
export const DomainResolutionStruct = object({
192-
protocol: string(),
193-
resolvedAddress: string(),
194-
domainName: string(),
195-
});
196-
197-
export const AddressResolutionResponseStruct = object({
198-
resolvedDomains: size(array(AddressResolutionStruct), 1, Infinity),
199-
});
200-
201-
export const DomainResolutionResponseStruct = object({
202-
resolvedAddresses: size(array(DomainResolutionStruct), 1, Infinity),
203-
});
204-
205-
export const OnNameLookupResponseStruct = nullable(
206-
union([AddressResolutionResponseStruct, DomainResolutionResponseStruct]),
207-
);
208-
209-
/**
210-
* Utility type for getting the handler function type from a handler type.
211-
*/
212-
export type HandlerFunction<Type extends SnapHandler> =
213-
Type['validator'] extends (snapExport: unknown) => snapExport is infer Handler
214-
? Handler
215-
: never;
216-
217-
/**
218-
* All the function-based handlers that a snap can implement.
219-
*/
220-
export type SnapFunctionExports = {
221-
[Key in keyof typeof SNAP_EXPORTS]?: HandlerFunction<
222-
(typeof SNAP_EXPORTS)[Key]
223-
>;
224-
};
225-
226-
/**
227-
* All handlers that a snap can implement.
228-
*/
229-
export type SnapExports = SnapFunctionExports;
125+
export const SNAP_EXPORT_NAMES = Object.values(HandlerType);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ComponentOrElementStruct } from '@metamask/snaps-sdk';
2+
import { object, string, union } from '@metamask/superstruct';
3+
4+
export const OnHomePageResponseWithContentStruct = object({
5+
content: ComponentOrElementStruct,
6+
});
7+
8+
export const OnHomePageResponseWithIdStruct = object({
9+
id: string(),
10+
});
11+
12+
export const OnHomePageResponseStruct = union([
13+
OnHomePageResponseWithContentStruct,
14+
OnHomePageResponseWithIdStruct,
15+
]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from './exports';
2+
export * from './home-page';
3+
export * from './name-lookup';
4+
export * from './settings-page';
5+
export * from './signature';
6+
export * from './transaction';
7+
export * from './types';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
array,
3+
nullable,
4+
object,
5+
size,
6+
string,
7+
union,
8+
} from '@metamask/superstruct';
9+
10+
export const AddressResolutionStruct = object({
11+
protocol: string(),
12+
resolvedDomain: string(),
13+
});
14+
15+
export const DomainResolutionStruct = object({
16+
protocol: string(),
17+
resolvedAddress: string(),
18+
domainName: string(),
19+
});
20+
21+
export const AddressResolutionResponseStruct = object({
22+
resolvedDomains: size(array(AddressResolutionStruct), 1, Infinity),
23+
});
24+
25+
export const DomainResolutionResponseStruct = object({
26+
resolvedAddresses: size(array(DomainResolutionStruct), 1, Infinity),
27+
});
28+
29+
export const OnNameLookupResponseStruct = nullable(
30+
union([AddressResolutionResponseStruct, DomainResolutionResponseStruct]),
31+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { OnHomePageResponseStruct } from './home-page';
2+
3+
export const OnSettingsPageResponseStruct = OnHomePageResponseStruct;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { OnTransactionResponseStruct } from './transaction';
2+
3+
export const OnSignatureResponseStruct = OnTransactionResponseStruct;

0 commit comments

Comments
 (0)