Skip to content

Commit

Permalink
Refactor code (change approach)
Browse files Browse the repository at this point in the history
  • Loading branch information
david0xd committed Oct 9, 2024
1 parent d855bec commit 332b5c4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 70 deletions.
6 changes: 2 additions & 4 deletions packages/snaps-jest/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,8 @@ describe('installSnap', () => {
const { onKeyringRequest, close } = await installSnap(snapId);
const response = await onKeyringRequest({
origin: 'metamask.io',
request: {
params: {},
method: 'keyring_listAccounts',
},
params: {},
method: 'keyring_listAccounts',
});

expect(response).toStrictEqual(
Expand Down
17 changes: 5 additions & 12 deletions packages/snaps-simulation/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -472,32 +472,25 @@ describe('helpers', () => {
const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onKeyringRequest = async ({ origin, request }) => {
// return handleKeyringRequest(keyring, request);
return request;
return { success: true };
}
`,
});

const { onKeyringRequest, close } = await installSnap(snapId);
const response = await onKeyringRequest({
origin: 'metamask.io',
request: {
params: {
foo: 'bar',
},
params: {
foo: 'bar',
},
method: 'keyring_createAccount',
});

expect(response).toStrictEqual(
expect.objectContaining({
response: {
result: {
id: 1,
jsonrpc: '2.0',
method: '',
params: {
foo: 'bar',
},
success: true,
},
},
}),
Expand Down
8 changes: 1 addition & 7 deletions packages/snaps-simulation/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { addJsonRpcMock, removeJsonRpcMock } from './store';
import {
assertIsResponseWithInterface,
JsonRpcMockOptionsStruct,
KeyringOptionsStruct,
SignatureOptionsStruct,
TransactionOptionsStruct,
} from './structs';
Expand Down Expand Up @@ -234,19 +233,14 @@ export function getHelpers({
): Promise<SnapResponseWithoutInterface> => {
log('Sending keyring request %o.', request);

const { origin: keyringRequestOrigin, request: keyringRequest } = create(
request,
KeyringOptionsStruct,
);

const response = await handleRequest({
snapId,
store,
executionService,
runSaga,
controllerMessenger,
handler: HandlerType.OnKeyringRequest,
request: { origin: keyringRequestOrigin, ...keyringRequest },
request,
});

return response;
Expand Down
35 changes: 0 additions & 35 deletions packages/snaps-simulation/src/structs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import {
JsonStruct,
StrictHexStruct,
valueToBytes,
JsonRpcIdStruct,
JsonRpcParamsStruct,
} from '@metamask/utils';
import { randomBytes } from 'crypto';

Expand All @@ -39,28 +37,6 @@ const BytesLikeStruct = union([
instance(Uint8Array),
]);

export const RequestOptionsStruct = object({
/**
* The JSON-RPC request ID.
*/
id: optional(JsonRpcIdStruct),

/**
* The JSON-RPC method.
*/
method: string(),

/**
* The JSON-RPC params.
*/
params: optional(JsonRpcParamsStruct),

/**
* The origin to send the request from.
*/
origin: optional(string()),
});

export const TransactionOptionsStruct = object({
/**
* The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.
Expand Down Expand Up @@ -167,17 +143,6 @@ export const TransactionOptionsStruct = object({
),
});

export const KeyringOptionsStruct = object({
/**
* The origin making the Keyring request.
*/
origin: defaulted(string(), 'metamask.io'),
/**
* Keyring request object with params.
*/
request: RequestOptionsStruct,
});

export const SignatureOptionsStruct = object({
/**
* The origin making the signature request.
Expand Down
35 changes: 23 additions & 12 deletions packages/snaps-simulation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ import type { NotificationType, EnumToUnion } from '@metamask/snaps-sdk';
import type { JSXElement } from '@metamask/snaps-sdk/jsx';
import type { InferMatching } from '@metamask/snaps-utils';
import type { Infer } from '@metamask/superstruct';
import type { Json } from '@metamask/utils';
import type { Json, JsonRpcId, JsonRpcParams } from '@metamask/utils';

import type {
KeyringOptionsStruct,
RequestOptionsStruct,
SignatureOptionsStruct,
SnapOptionsStruct,
SnapResponseStruct,
TransactionOptionsStruct,
} from './structs';

/**
* JSON RPC Request object.
*/
export type RequestOptions = Infer<typeof RequestOptionsStruct>;
export type RequestOptions = {
/**
* The JSON-RPC request ID.
*/
id?: JsonRpcId;

/**
* The JSON-RPC method.
*/
method: string;

/**
* The JSON-RPC params.
*/
params?: JsonRpcParams;

/**
* The origin to send the request from.
*/
origin?: string;
};

/**
* The `runCronjob` options. This is the same as {@link RequestOptions}, except
Expand Down Expand Up @@ -49,12 +64,8 @@ export type TransactionOptions = Infer<typeof TransactionOptionsStruct>;

/**
* The options to use for keyring requests.
*
* @property origin - The origin to send the signature request from. Defaults to
* `metamask.io`.
* @property request - Keyring request with params.
*/
export type KeyringOptions = Infer<typeof KeyringOptionsStruct>;
export type KeyringOptions = RequestOptions;

/**
* The options to use for signature requests.
Expand Down

0 comments on commit 332b5c4

Please sign in to comment.