Skip to content

Commit

Permalink
chore: remove console.log
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Wessel <ethan.wessel@consensys.net>
  • Loading branch information
ejwessel committed Jun 5, 2024
1 parent 8d42c2d commit 12231cf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
53 changes: 53 additions & 0 deletions app/scripts/lib/tx-verification/tx-verification-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { hashMessage } from '@ethersproject/hash';
import { verifyMessage } from '@ethersproject/wallet';
import {
Json,
JsonRpcParams,
JsonRpcRequest,
JsonRpcResponse,
} from '@metamask/utils';
import {
JsonRpcEngineEndCallback,
JsonRpcEngineNextCallback,
} from 'json-rpc-engine';
import { TRUSTED_BRIDGE_SIGNER } from '../../../../shared/constants/bridge';
import { FIRST_PARTY_CONTRACT_NAMES } from '../../../../shared/constants/first-party-contracts';

export function txVerificationMiddleware(
req: JsonRpcRequest<JsonRpcParams>,
_res: JsonRpcResponse<Json>,
next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
) {
// ignore if not sendTransaction and if the params not an array
if (req.method !== 'eth_sendTransaction' || !Array.isArray(req.params)) {
return next();
}

// 0 tx object is the first element
const params = req.params[0];
const paramsToVerify = {
to: hashMessage(params.to.toLowerCase()),
from: hashMessage(params.from.toLowerCase()),
data: hashMessage(
params.data.toLowerCase().substr(0, params.data.length - 130),
),
value: hashMessage(params.value.toLowerCase()),
};
const h = hashMessage(JSON.stringify(paramsToVerify));
const signature = `0x${params.data.substr(-130)}`;
// signature is 130 chars in length at the end
const addressToVerify = verifyMessage(h, signature);
const canSubmit =
params.to.toLowerCase() ===
FIRST_PARTY_CONTRACT_NAMES['MetaMask Bridge'][params.chainId].toLowerCase()
? addressToVerify.toLowerCase() === TRUSTED_BRIDGE_SIGNER.toLowerCase()
: true;

if (!canSubmit) {
end(new Error('Validation Error'));
}

// successful validation
return next();
}
2 changes: 0 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5139,8 +5139,6 @@ export default class MetamaskController extends EventEmitter {
engine.push(createLoggerMiddleware({ origin }));
engine.push(this.permissionLogController.createMiddleware());

console.log('origin')
console.log(origin)
if (origin === 'https://portfolio.metamask.io' || 'http://localhost:3000') {
engine.push(txVerificationMiddleware);
}
Expand Down

0 comments on commit 12231cf

Please sign in to comment.