Skip to content

Commit

Permalink
Fix type error regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Aug 12, 2024
1 parent b05b122 commit c6c724a
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 119 deletions.
25 changes: 14 additions & 11 deletions app/scripts/lib/accounts/BalancesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ export class BalancesController extends BaseController<
* @param accountId - The account ID.
*/
#getAccount(accountId: string): InternalAccount {
const account: InternalAccount = this.#listMultichainAccounts().find(
(multichainAccount) => multichainAccount.id === accountId,
);
const account: InternalAccount | undefined =
this.#listMultichainAccounts().find(
(multichainAccount) => multichainAccount.id === accountId,
);

if (!account) {
throw new Error(`Unknown account: ${accountId}`);
Expand All @@ -247,13 +248,15 @@ export class BalancesController extends BaseController<
const account = this.#getAccount(accountId);
const partialState: BalancesControllerState = { balances: {} };

partialState.balances[account.id] = await this.#getBalances(
account.id,
account.metadata.snap.id,
isBtcMainnetAddress(account.address)
? BTC_MAINNET_ASSETS
: BTC_TESTNET_ASSETS,
);
if (account.metadata.snap) {
partialState.balances[account.id] = await this.#getBalances(
account.id,
account.metadata.snap.id,
isBtcMainnetAddress(account.address)
? BTC_MAINNET_ASSETS
: BTC_TESTNET_ASSETS,
);
}

this.update((state: Draft<BalancesControllerState>) => ({
...state,
Expand Down Expand Up @@ -292,7 +295,7 @@ export class BalancesController extends BaseController<
return (
!isEvmAccountType(account.type) &&
// Non-EVM accounts are backed by a Snap for now
account.metadata.snap
account.metadata.snap !== undefined
);
}

Expand Down
52 changes: 29 additions & 23 deletions app/scripts/lib/createDupeReqFilterStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import OurReadableStream from 'readable-stream';
import ReadableStream2 from 'readable-stream-2';
import ReadableStream3 from 'readable-stream-3';

import type { JsonRpcRequest } from '@metamask/utils';
import { JsonRpcNotification, type JsonRpcRequest } from '@metamask/utils';
import createDupeReqFilterStream, {
THREE_MINUTES,
} from './createDupeReqFilterStream';
Expand All @@ -26,7 +26,7 @@ function createTestStream(output: JsonRpcRequest[] = [], S = Transform) {
}

function runStreamTest(
requests: JsonRpcRequest[] = [],
requests: (JsonRpcRequest | JsonRpcNotification)[] = [],
advanceTimersTime = 10,
S = Transform,
) {
Expand All @@ -52,13 +52,13 @@ describe('createDupeReqFilterStream', () => {

it('lets through requests with ids being seen for the first time', async () => {
const requests = [
{ id: 1, method: 'foo' },
{ id: 2, method: 'bar' },
{ id: 1, method: 'foo', jsonrpc: '2.0' as const },
{ id: 2, method: 'bar', jsonrpc: '2.0' as const },
];

const expectedOutput = [
{ id: 1, method: 'foo' },
{ id: 2, method: 'bar' },
{ id: 1, method: 'foo', jsonrpc: '2.0' },
{ id: 2, method: 'bar', jsonrpc: '2.0' },
];

const output = await runStreamTest(requests);
Expand All @@ -67,42 +67,48 @@ describe('createDupeReqFilterStream', () => {

it('does not let through the request if the id has been seen before', async () => {
const requests = [
{ id: 1, method: 'foo' },
{ id: 1, method: 'foo' }, // duplicate
{ id: 1, method: 'foo', jsonrpc: '2.0' as const },
{ id: 1, method: 'foo', jsonrpc: '2.0' as const }, // duplicate
];

const expectedOutput = [{ id: 1, method: 'foo' }];
const expectedOutput = [{ id: 1, method: 'foo', jsonrpc: '2.0' }];

const output = await runStreamTest(requests);
expect(output).toEqual(expectedOutput);
});

it("lets through requests if they don't have an id", async () => {
const requests = [{ method: 'notify1' }, { method: 'notify2' }];
const requests = [
{ method: 'notify1', jsonrpc: '2.0' as const },
{ method: 'notify2', jsonrpc: '2.0' as const },
];

const expectedOutput = [{ method: 'notify1' }, { method: 'notify2' }];
const expectedOutput = [
{ method: 'notify1', jsonrpc: '2.0' },
{ method: 'notify2', jsonrpc: '2.0' },
];

const output = await runStreamTest(requests);
expect(output).toEqual(expectedOutput);
});

it('handles a mix of request types', async () => {
const requests = [
{ id: 1, method: 'foo' },
{ method: 'notify1' },
{ id: 1, method: 'foo' },
{ id: 2, method: 'bar' },
{ method: 'notify2' },
{ id: 2, method: 'bar' },
{ id: 3, method: 'baz' },
{ id: 1, method: 'foo', jsonrpc: '2.0' as const },
{ method: 'notify1', jsonrpc: '2.0' as const },
{ id: 1, method: 'foo', jsonrpc: '2.0' as const },
{ id: 2, method: 'bar', jsonrpc: '2.0' as const },
{ method: 'notify2', jsonrpc: '2.0' as const },
{ id: 2, method: 'bar', jsonrpc: '2.0' as const },
{ id: 3, method: 'baz', jsonrpc: '2.0' as const },
];

const expectedOutput = [
{ id: 1, method: 'foo' },
{ method: 'notify1' },
{ id: 2, method: 'bar' },
{ method: 'notify2' },
{ id: 3, method: 'baz' },
{ id: 1, method: 'foo', jsonrpc: '2.0' },
{ method: 'notify1', jsonrpc: '2.0' },
{ id: 2, method: 'bar', jsonrpc: '2.0' },
{ method: 'notify2', jsonrpc: '2.0' },
{ id: 3, method: 'baz', jsonrpc: '2.0' },
];

const output = await runStreamTest(requests);
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/lib/createDupeReqFilterStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default function createDupeReqFilterStream() {
transform(chunk: JsonRpcRequest, _, cb) {
// JSON-RPC notifications have no ids; our only recourse is to let them through.
const hasNoId = chunk.id === undefined;
const requestNotYetSeen = seenRequestIds.add(chunk.id);
const requestNotYetSeen =
chunk.id !== null && seenRequestIds.add(chunk.id);

if (hasNoId || requestNotYetSeen) {
cb(null, chunk);
Expand Down
71 changes: 40 additions & 31 deletions app/scripts/lib/ppom/ppom-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
type Hex,
JsonRpcRequestStruct,
JsonRpcResponseStruct,
} from '@metamask/utils';
import { type Hex, JsonRpcResponseStruct } from '@metamask/utils';
import * as ControllerUtils from '@metamask/controller-utils';

import { CHAIN_IDS } from '../../../../shared/constants/network';
Expand Down Expand Up @@ -32,6 +28,12 @@ const SECURITY_ALERT_RESPONSE_MOCK: SecurityAlertResponse = {
reason: BlockaidReason.permitFarming,
};

const REQUEST_MOCK = {
params: [],
id: '',
jsonrpc: '2.0' as const,
};

const createMiddleware = (
options: {
chainId?: Hex;
Expand Down Expand Up @@ -117,14 +119,14 @@ describe('PPOMMiddleware', () => {
});

const req = {
...JsonRpcRequestStruct,
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
securityAlertResponse: { reason: '', result_type: '' },
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct },
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

Expand All @@ -142,14 +144,14 @@ describe('PPOMMiddleware', () => {
const middlewareFunction = createMiddleware();

const req = {
...JsonRpcRequestStruct,
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
securityAlertResponse: { reason: '', result_type: '' },
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct },
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

Expand All @@ -165,11 +167,12 @@ describe('PPOMMiddleware', () => {
});

const req = {
...JsonRpcRequestStruct,
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
securityAlertResponse: { reason: '', result_type: '' },
};

// @ts-expect-error Plugging in invalid input for testing purposes
await middlewareFunction(req, undefined, () => undefined);

expect(req.securityAlertResponse).toBeUndefined();
Expand All @@ -183,14 +186,14 @@ describe('PPOMMiddleware', () => {
});

const req = {
...JsonRpcRequestStruct,
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
securityAlertResponse: { reason: '', result_type: '' },
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct },
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

Expand All @@ -202,14 +205,14 @@ describe('PPOMMiddleware', () => {
const middlewareFunction = createMiddleware();

const req = {
...JsonRpcRequestStruct,
method: 'eth_someRequest',
securityAlertResponse: undefined,
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: { reason: '', result_type: '' },
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct },
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

Expand All @@ -221,15 +224,15 @@ describe('PPOMMiddleware', () => {
const middlewareFunction = createMiddleware();

const req = {
...JsonRpcRequestStruct,
params: [{ to: INTERNAL_ACCOUNT_ADDRESS }],
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
securityAlertResponse: { reason: '', result_type: '' },
params: [{ to: INTERNAL_ACCOUNT_ADDRESS }],
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct },
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

Expand All @@ -243,18 +246,19 @@ describe('PPOMMiddleware', () => {
});

const req = {
...REQUEST_MOCK,
securityAlertResponse: { reason: '', result_type: '' },
method: 'personal_sign',
params: [
'0x6d6574616d61736b2e6769746875622e696f2077616e747320796f7520746f207369676e20696e207769746820796f757220457468657265756d206163636f756e743a0a3078393335653733656462396666353265323362616337663765303433613165636430366430353437370a0a492061636365707420746865204d6574614d61736b205465726d73206f6620536572766963653a2068747470733a2f2f636f6d6d756e6974792e6d6574616d61736b2e696f2f746f730a0a5552493a2068747470733a2f2f6d6574616d61736b2e6769746875622e696f0a56657273696f6e3a20310a436861696e2049443a20310a4e6f6e63653a2033323839313735370a4973737565642041743a20323032312d30392d33305431363a32353a32342e3030305a',
'0x935e73edb9ff52e23bac7f7e043a1ecd06d05477',
'Example password',
],
jsonrpc: '2.0',
jsonrpc: '2.0' as const,
id: 2974202441,
origin: 'https://metamask.github.io',
networkClientId: 'mainnet',
tabId: 1048745900,
securityAlertResponse: undefined,
};
jest.spyOn(ControllerUtils, 'detectSIWE').mockReturnValue({
isSIWEMessage: true,
Expand All @@ -276,6 +280,7 @@ describe('PPOMMiddleware', () => {
},
});

// @ts-expect-error Plugging in invalid input for testing purposes
await middlewareFunction(req, undefined, () => undefined);

expect(req.securityAlertResponse).toBeUndefined();
Expand All @@ -287,8 +292,12 @@ describe('PPOMMiddleware', () => {
const nextMock = jest.fn();

await middlewareFunction(
{ ...JsonRpcRequestStruct, method: 'eth_sendTransaction' },
{ ...JsonRpcResponseStruct },
{
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: { reason: '', result_type: '' },
},
{ ...JsonRpcResponseStruct.TYPE },
nextMock,
);

Expand All @@ -304,12 +313,12 @@ describe('PPOMMiddleware', () => {
const middlewareFunction = createMiddleware({ error });

const req = {
...JsonRpcRequestStruct,
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
securityAlertResponse: { reason: '', result_type: '' },
};

await middlewareFunction(req, { ...JsonRpcResponseStruct }, nextMock);
await middlewareFunction(req, { ...JsonRpcResponseStruct.TYPE }, nextMock);

expect(req.securityAlertResponse).toStrictEqual(
SECURITY_ALERT_RESPONSE_MOCK,
Expand Down
Loading

0 comments on commit c6c724a

Please sign in to comment.