Skip to content

Commit

Permalink
feat: update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s committed Mar 4, 2024
1 parent d1c609b commit e7234e8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
12 changes: 0 additions & 12 deletions app/core/BackgroundBridge/BackgroundBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,6 @@ export class BackgroundBridge extends EventEmitter {

async notifySelectedAddressChanged(selectedAddress) {
try {
// Remove following specific walletconnect block once WalletConnect has migrated away from DefaultPreferences
if (this.isWalletConnect) {
DevLogger.log(
`notifySelectedAddressChanged walletconnect hostname: ${this.hostname}: ${selectedAddress}`,
);
this.sendNotification({
method: NOTIFICATION_NAMES.accountsChanged,
params: [selectedAddress],
});
return;
}

let approvedAccounts = await getPermittedAccounts(
this.channelId ?? this.hostname,
);
Expand Down
1 change: 1 addition & 0 deletions app/core/RPCMethods/RPCMethodMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ describe('getRpcMethodMiddleware', () => {
setupGlobalState({
addTransactionResult: Promise.resolve('fake-hash'),
// Set minimal network controller state to support validation
permittedAccounts: { 'example.metamask.io': [] },
providerConfig: {
chainId: '0x1',
type: RPC,
Expand Down
38 changes: 11 additions & 27 deletions app/core/RPCMethods/RPCMethodMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ export const checkActiveAccountAndChainId = async ({
address,
chainId,
channelId,
checkSelectedAddress,
hostname,
}: {
address?: string;
chainId?: number;
channelId?: string;
checkSelectedAddress: boolean;
hostname: string;
}) => {
let isInvalidAccount = false;
Expand All @@ -114,7 +112,6 @@ export const checkActiveAccountAndChainId = async ({
address,
chainId,
channelId,
checkSelectedAddress,
hostname,
formattedAddress,
});
Expand All @@ -123,24 +120,17 @@ export const checkActiveAccountAndChainId = async ({
'',
);

if (checkSelectedAddress) {
const selectedAddress =
Engine.context.PreferencesController.state.selectedAddress;
if (formattedAddress !== safeToChecksumAddress(selectedAddress)) {
isInvalidAccount = true;
}
} else {
const accounts = await getPermittedAccounts(channelId ?? validHostname);
const normalizedAccounts = accounts.map(safeToChecksumAddress);

if (!normalizedAccounts.includes(formattedAddress)) {
isInvalidAccount = true;
if (accounts.length > 0) {
// Permissions issue --- requesting incorrect address
throw ethErrors.rpc.invalidParams({
message: `Invalid parameters: must provide a permitted Ethereum address.`,
});
}
const accounts =
(await getPermittedAccounts(channelId ?? validHostname)) ?? [];
const normalizedAccounts = accounts.map(safeToChecksumAddress);

if (!normalizedAccounts.includes(formattedAddress)) {
isInvalidAccount = true;
if (accounts.length > 0) {
// Permissions issue --- requesting incorrect address
throw ethErrors.rpc.invalidParams({
message: `Invalid parameters: must provide a permitted Ethereum address.`,
});
}
}

Expand Down Expand Up @@ -194,7 +184,6 @@ const generateRawSignature = async ({
title,
icon,
analytics,
isWalletConnect,
chainId,
channelId,
getSource,
Expand All @@ -220,7 +209,6 @@ const generateRawSignature = async ({
channelId,
address: req.params[0],
chainId,
checkSelectedAddress: isWalletConnect,
});

const rawSig = await SignatureController.newUnsignedTypedMessage(
Expand Down Expand Up @@ -512,7 +500,6 @@ export const getRpcMethodMiddleware = ({
address: from,
channelId,
chainId,
checkSelectedAddress: isWalletConnect,
});
},
});
Expand Down Expand Up @@ -551,7 +538,6 @@ export const getRpcMethodMiddleware = ({
hostname,
channelId,
address: req.params[0].from,
checkSelectedAddress: isWalletConnect,
});
PPOMUtil.validateRequest(req);
const rawSig = await SignatureController.newUnsignedMessage({
Expand Down Expand Up @@ -599,7 +585,6 @@ export const getRpcMethodMiddleware = ({
hostname,
channelId,
address: params.from,
checkSelectedAddress: isWalletConnect,
});

PPOMUtil.validateRequest(req);
Expand Down Expand Up @@ -647,7 +632,6 @@ export const getRpcMethodMiddleware = ({
hostname,
channelId,
address: req.params[1],
checkSelectedAddress: isWalletConnect,
});

PPOMUtil.validateRequest(req);
Expand Down

0 comments on commit e7234e8

Please sign in to comment.