Skip to content

Commit 4abc716

Browse files
committed
stash
1 parent 50472d0 commit 4abc716

16 files changed

+99
-66
lines changed

app/scripts/lib/accounts/BalancesController.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ export class BalancesController extends BaseController<
224224
* @param accountId - The account ID.
225225
*/
226226
#getAccount(accountId: string): InternalAccount {
227-
const account: InternalAccount = this.#listMultichainAccounts().find(
228-
(multichainAccount) => multichainAccount.id === accountId,
229-
);
227+
const account: InternalAccount | undefined =
228+
this.#listMultichainAccounts().find(
229+
(multichainAccount) => multichainAccount.id === accountId,
230+
);
230231

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

250-
partialState.balances[account.id] = await this.#getBalances(
251-
account.id,
252-
account.metadata.snap.id,
253-
isBtcMainnetAddress(account.address)
254-
? BTC_MAINNET_ASSETS
255-
: BTC_TESTNET_ASSETS,
256-
);
251+
if (account.metadata.snap) {
252+
partialState.balances[account.id] = await this.#getBalances(
253+
account.id,
254+
account.metadata.snap.id,
255+
isBtcMainnetAddress(account.address)
256+
? BTC_MAINNET_ASSETS
257+
: BTC_TESTNET_ASSETS,
258+
);
259+
}
257260

258261
this.update((state: Draft<BalancesControllerState>) => ({
259262
...state,
@@ -292,7 +295,7 @@ export class BalancesController extends BaseController<
292295
return (
293296
!isEvmAccountType(account.type) &&
294297
// Non-EVM accounts are backed by a Snap for now
295-
account.metadata.snap
298+
account.metadata.snap !== undefined
296299
);
297300
}
298301

app/scripts/lib/createDupeReqFilterStream.test.ts

+29-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import OurReadableStream from 'readable-stream';
33
import ReadableStream2 from 'readable-stream-2';
44
import ReadableStream3 from 'readable-stream-3';
55

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

2828
function runStreamTest(
29-
requests: JsonRpcRequest[] = [],
29+
requests: (JsonRpcRequest | JsonRpcNotification)[] = [],
3030
advanceTimersTime = 10,
3131
S = Transform,
3232
) {
@@ -52,13 +52,13 @@ describe('createDupeReqFilterStream', () => {
5252

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

5959
const expectedOutput = [
60-
{ id: 1, method: 'foo' },
61-
{ id: 2, method: 'bar' },
60+
{ id: 1, method: 'foo', jsonrpc: '2.0' },
61+
{ id: 2, method: 'bar', jsonrpc: '2.0' },
6262
];
6363

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

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

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

7676
const output = await runStreamTest(requests);
7777
expect(output).toEqual(expectedOutput);
7878
});
7979

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

83-
const expectedOutput = [{ method: 'notify1' }, { method: 'notify2' }];
86+
const expectedOutput = [
87+
{ method: 'notify1', jsonrpc: '2.0' },
88+
{ method: 'notify2', jsonrpc: '2.0' },
89+
];
8490

8591
const output = await runStreamTest(requests);
8692
expect(output).toEqual(expectedOutput);
8793
});
8894

8995
it('handles a mix of request types', async () => {
9096
const requests = [
91-
{ id: 1, method: 'foo' },
92-
{ method: 'notify1' },
93-
{ id: 1, method: 'foo' },
94-
{ id: 2, method: 'bar' },
95-
{ method: 'notify2' },
96-
{ id: 2, method: 'bar' },
97-
{ id: 3, method: 'baz' },
97+
{ id: 1, method: 'foo', jsonrpc: '2.0' as const },
98+
{ method: 'notify1', jsonrpc: '2.0' as const },
99+
{ id: 1, method: 'foo', jsonrpc: '2.0' as const },
100+
{ id: 2, method: 'bar', jsonrpc: '2.0' as const },
101+
{ method: 'notify2', jsonrpc: '2.0' as const },
102+
{ id: 2, method: 'bar', jsonrpc: '2.0' as const },
103+
{ id: 3, method: 'baz', jsonrpc: '2.0' as const },
98104
];
99105

100106
const expectedOutput = [
101-
{ id: 1, method: 'foo' },
102-
{ method: 'notify1' },
103-
{ id: 2, method: 'bar' },
104-
{ method: 'notify2' },
105-
{ id: 3, method: 'baz' },
107+
{ id: 1, method: 'foo', jsonrpc: '2.0' },
108+
{ method: 'notify1', jsonrpc: '2.0' },
109+
{ id: 2, method: 'bar', jsonrpc: '2.0' },
110+
{ method: 'notify2', jsonrpc: '2.0' },
111+
{ id: 3, method: 'baz', jsonrpc: '2.0' },
106112
];
107113

108114
const output = await runStreamTest(requests);

app/scripts/lib/createDupeReqFilterStream.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default function createDupeReqFilterStream() {
5454
transform(chunk: JsonRpcRequest, _, cb) {
5555
// JSON-RPC notifications have no ids; our only recourse is to let them through.
5656
const hasNoId = chunk.id === undefined;
57-
const requestNotYetSeen = seenRequestIds.add(chunk.id);
57+
const requestNotYetSeen =
58+
chunk.id !== null && seenRequestIds.add(chunk.id);
5859

5960
if (hasNoId || requestNotYetSeen) {
6061
cb(null, chunk);

app/scripts/lib/ppom/ppom-middleware.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export function createPPOMMiddleware<
6161
) => void,
6262
) {
6363
return async (
64-
req: JsonRpcRequest<Params>,
64+
req: JsonRpcRequest<Params> & {
65+
securityAlertResponse: SecurityAlertResponse;
66+
},
6567
_res: JsonRpcResponse<Result>,
6668
next: () => void,
6769
) => {
@@ -80,13 +82,15 @@ export function createPPOMMiddleware<
8082
return;
8183
}
8284

83-
const { isSIWEMessage } = detectSIWE({ data: req?.params?.[0] });
85+
const data =
86+
(Array.isArray(req.params) ? req.params[0] : req.params) ?? {};
87+
const { isSIWEMessage } = detectSIWE({ data });
8488
if (isSIWEMessage) {
8589
return;
8690
}
8791

8892
if (req.method === MESSAGE_TYPE.ETH_SEND_TRANSACTION) {
89-
const { to: toAddress } = req?.params?.[0] ?? {};
93+
const { to: toAddress } = data;
9094
const internalAccounts = accountsController.listAccounts();
9195
const isToInternalAccount = internalAccounts.some(
9296
({ address }) => address?.toLowerCase() === toAddress?.toLowerCase(),

app/scripts/lib/ppom/ppom-util.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const SECURITY_ALERT_RESPONSE_ERROR = {
3232
reason: BlockaidReason.errored,
3333
};
3434

35+
type PPOMRequest = Exclude<JsonRpcRequest, 'method' | 'params'> & {
36+
method: typeof METHOD_SEND_TRANSACTION;
37+
params: [TransactionParams];
38+
};
39+
3540
export async function validateRequestWithPPOM({
3641
ppomController,
3742
request,
@@ -129,12 +134,14 @@ export async function isChainSupported(chainId: Hex): Promise<boolean> {
129134
return supportedChainIds.includes(chainId);
130135
}
131136

132-
function normalizePPOMRequest(request: JsonRpcRequest): JsonRpcRequest {
137+
function normalizePPOMRequest(
138+
request: JsonRpcRequest,
139+
): PPOMRequest | typeof request {
133140
if (request.method !== METHOD_SEND_TRANSACTION) {
134141
return request;
135142
}
136143

137-
const transactionParams = (request.params?.[0] || {}) as TransactionParams;
144+
const transactionParams = request.params[0] ?? {};
138145
const normalizedParams = normalizeTransactionParams(transactionParams);
139146

140147
return {
@@ -210,7 +217,7 @@ async function validateWithController(
210217
async function validateWithAPI(
211218
ppomController: PPOMController,
212219
chainId: string,
213-
request: JsonRpcRequest,
220+
request: PPOMRequest,
214221
): Promise<SecurityAlertResponse> {
215222
try {
216223
const response = await validateWithSecurityAlertsAPI(chainId, request);

app/scripts/lib/snap-keyring/snap-keyring.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const mockInternalAccount = {
5050
name: mockSnapName,
5151
},
5252
name: accountNameSuggestion,
53+
keyring: {
54+
type: '',
55+
},
56+
importTime: 0,
5357
},
5458
};
5559

app/scripts/lib/transaction/util.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ describe('Transaction Utils', () => {
472472
expect(validateRequestWithPPOMMock).toHaveBeenCalledTimes(0);
473473
});
474474

475-
it('send to users own acccount', async () => {
475+
it('send to users own account', async () => {
476476
const sendRequest = {
477477
...request,
478478
transactionParams: {
@@ -484,9 +484,10 @@ describe('Transaction Utils', () => {
484484
...sendRequest,
485485
securityAlertsEnabled: false,
486486
chainId: '0x1',
487-
internalAccounts: {
488-
address: INTERNAL_ACCOUNT_ADDRESS,
489-
} as InternalAccount,
487+
internalAccounts: [
488+
// @ts-expect-error Passing incomplete account type for mocking purposes
489+
{ address: INTERNAL_ACCOUNT_ADDRESS },
490+
],
490491
});
491492

492493
expect(

app/scripts/lib/transaction/util.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ async function validateSecurity(request: AddTransactionRequest) {
264264
params: [
265265
{
266266
from,
267-
to,
268-
value,
269-
data,
267+
to: to ?? '',
268+
value: value ?? '',
269+
data: data ?? '',
270270
},
271271
],
272+
jsonrpc: '2.0' as const,
272273
};
273274

274275
const securityAlertId = generateSecurityAlertId();

app/scripts/migrations/105.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function expectedInternalAccount(
7272
type: 'HD Key Tree',
7373
},
7474
lastSelected: lastSelected ? expect.any(Number) : undefined,
75+
importTime: Date.now(),
7576
},
7677
options: {},
7778
methods: ETH_EOA_METHODS,

app/scripts/migrations/105.ts

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function createInternalAccountsForAccountsController(
103103
// initial updateAccounts call.
104104
type: 'HD Key Tree',
105105
},
106+
importTime: Date.now(),
106107
},
107108
methods: ETH_EOA_METHODS,
108109
type: EthAccountType.Eoa,

app/scripts/migrations/119.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ function transformState(state: Record<string, any>) {
4646
.accounts,
4747
).length > 0
4848
) {
49-
Object.values(accountsController.internalAccounts.accounts).forEach(
50-
(internalAccount: InternalAccount) => {
51-
if (!internalAccount.metadata?.importTime) {
52-
internalAccount.metadata.importTime = Date.now();
53-
}
54-
},
55-
);
49+
Object.values<InternalAccount>(
50+
accountsController.internalAccounts.accounts,
51+
).forEach((internalAccount) => {
52+
if (!internalAccount.metadata?.importTime) {
53+
internalAccount.metadata.importTime = Date.now();
54+
}
55+
});
5656
}
5757

5858
return {

app/scripts/snaps/preinstalled-snaps.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import MessageSigningSnap from '@metamask/message-signing-snap/dist/preinstalled
44
import BitcoinWalletSnap from '@metamask/bitcoin-wallet-snap/dist/preinstalled-snap.json';
55
///: END:ONLY_INCLUDE_IF
66

7-
const PREINSTALLED_SNAPS: readonly PreinstalledSnap[] = Object.freeze([
7+
const PREINSTALLED_SNAPS = Object.freeze<PreinstalledSnap[]>([
88
MessageSigningSnap as PreinstalledSnap,
99
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
10+
// @ts-expect-error Type 'string' is not comparable to type '{ [brand]: unique symbol; }'.ts(2352)
1011
BitcoinWalletSnap as PreinstalledSnap,
1112
///: END:ONLY_INCLUDE_IF
1213
]);

test/e2e/tests/api-usage/account-tracker-api-usage.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async function getAllInfuraJsonRpcRequests(
7272
);
7373

7474
for (const r of seenProviderRequests) {
75-
const json = await r.body.getJson();
75+
const json = (await r.body.getJson()) as JsonRpcRequest | undefined;
7676
if (json !== undefined) {
7777
allInfuraJsonRpcRequests.push(json);
7878
}

ui/contexts/snaps/snap-interface.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ export const SnapInterfaceContextProvider: FunctionComponent<
110110
params: {
111111
event: {
112112
type: event,
113-
// TODO: Allow null in the types and simplify this
114-
...(name !== undefined && name !== null ? { name } : {}),
115-
...(value !== undefined && value !== null ? { value } : {}),
113+
name,
114+
value,
116115
},
117116
id: interfaceId,
118117
context,

ui/hooks/useMultichainSelector.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('useMultichainSelector', () => {
6060
});
6161

6262
it('uses selectedAccount if account is not provided', () => {
63+
// @ts-expect-error Intentionally passing in invalid input for testing purposes
6364
const { result } = renderUseMultichainHook(getMultichainIsEvm, null);
6465

6566
expect(result.current).toBe(true);

ui/selectors/accounts.test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('Accounts Selectors', () => {
101101
...MOCK_STATE.metamask,
102102
internalAccounts: {
103103
selectedAccount: MOCK_ACCOUNT_EOA.id,
104-
accounts: [MOCK_ACCOUNT_EOA],
104+
accounts: { mock_account_eoa: MOCK_ACCOUNT_EOA },
105105
},
106106
},
107107
};
@@ -118,10 +118,11 @@ describe('Accounts Selectors', () => {
118118
...MOCK_STATE.metamask,
119119
internalAccounts: {
120120
selectedAccount: MOCK_ACCOUNT_BIP122_P2WPKH.id,
121-
accounts: [
122-
MOCK_ACCOUNT_BIP122_P2WPKH,
123-
MOCK_ACCOUNT_BIP122_P2WPKH_TESTNET,
124-
],
121+
accounts: {
122+
mock_account_bip122_pwpkh: MOCK_ACCOUNT_BIP122_P2WPKH,
123+
mock_account_bip122_p2wpkh_testnet:
124+
MOCK_ACCOUNT_BIP122_P2WPKH_TESTNET,
125+
},
125126
},
126127
},
127128
};
@@ -136,7 +137,9 @@ describe('Accounts Selectors', () => {
136137
...MOCK_STATE.metamask,
137138
internalAccounts: {
138139
selectedAccount: MOCK_ACCOUNT_BIP122_P2WPKH.id,
139-
accounts: [MOCK_ACCOUNT_BIP122_P2WPKH],
140+
accounts: {
141+
mock_account_bip122_p2wpkh: MOCK_ACCOUNT_BIP122_P2WPKH,
142+
},
140143
},
141144
},
142145
};

0 commit comments

Comments
 (0)