Skip to content

Commit 40e955a

Browse files
committed
Request validation should not throw if verifyingContract is not defined in typed signature (#328)
1 parent b418bf3 commit 40e955a

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

src/wallet.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,50 @@ describe('wallet', () => {
432432
const promise = pify(engine.handle).call(engine, payload);
433433
await expect(promise).rejects.toThrow('Invalid input.');
434434
});
435+
436+
it('should not throw if verifyingContract is undefined', async () => {
437+
const { engine } = createTestSetup();
438+
const getAccounts = async () => testAddresses.slice();
439+
const witnessedMsgParams: TypedMessageParams[] = [];
440+
const processTypedMessageV3 = async (msgParams: TypedMessageParams) => {
441+
witnessedMsgParams.push(msgParams);
442+
// Assume testMsgSig is the expected signature result
443+
return testMsgSig;
444+
};
445+
446+
engine.push(
447+
createWalletMiddleware({ getAccounts, processTypedMessageV3 }),
448+
);
449+
450+
const message = {
451+
types: {
452+
EIP712Domain: [
453+
{ name: 'name', type: 'string' },
454+
{ name: 'version', type: 'string' },
455+
{ name: 'chainId', type: 'uint256' },
456+
{ name: 'verifyingContract', type: 'address' },
457+
],
458+
},
459+
primaryType: 'EIP712Domain',
460+
message: {},
461+
};
462+
463+
const stringifiedMessage = JSON.stringify(message);
464+
465+
const payload = {
466+
method: 'eth_signTypedData_v3',
467+
params: [testAddresses[0], stringifiedMessage], // Assuming testAddresses[0] is a valid address from your setup
468+
};
469+
470+
const promise = pify(engine.handle).call(engine, payload);
471+
const result = await promise;
472+
expect(result).toStrictEqual({
473+
id: undefined,
474+
jsonrpc: undefined,
475+
result:
476+
'0x68dc980608bceb5f99f691e62c32caccaee05317309015e9454eba1a14c3cd4505d1dd098b8339801239c9bcaac3c4df95569dcf307108b92f68711379be14d81c',
477+
});
478+
});
435479
});
436480

437481
describe('signTypedDataV4', () => {
@@ -524,6 +568,35 @@ describe('wallet', () => {
524568
const promise = pify(engine.handle).call(engine, payload);
525569
await expect(promise).rejects.toThrow('Invalid input.');
526570
});
571+
572+
it('should not throw if request is permit with undefined value for verifyingContract address', async () => {
573+
const { engine } = createTestSetup();
574+
const getAccounts = async () => testAddresses.slice();
575+
const witnessedMsgParams: TypedMessageParams[] = [];
576+
const processTypedMessageV4 = async (msgParams: TypedMessageParams) => {
577+
witnessedMsgParams.push(msgParams);
578+
// Assume testMsgSig is the expected signature result
579+
return testMsgSig;
580+
};
581+
582+
engine.push(
583+
createWalletMiddleware({ getAccounts, processTypedMessageV4 }),
584+
);
585+
586+
const payload = {
587+
method: 'eth_signTypedData_v4',
588+
params: [testAddresses[0], JSON.stringify(getMsgParams())],
589+
};
590+
591+
const promise = pify(engine.handle).call(engine, payload);
592+
const result = await promise;
593+
expect(result).toStrictEqual({
594+
id: undefined,
595+
jsonrpc: undefined,
596+
result:
597+
'0x68dc980608bceb5f99f691e62c32caccaee05317309015e9454eba1a14c3cd4505d1dd098b8339801239c9bcaac3c4df95569dcf307108b92f68711379be14d81c',
598+
});
599+
});
527600
});
528601

529602
describe('sign', () => {

src/wallet.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,8 @@ WalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {
499499
* @param data - The data passed in typedSign request.
500500
*/
501501
function validateVerifyingContract(data: string) {
502-
const {
503-
domain: { verifyingContract },
504-
} = parseTypedMessage(data);
505-
if (!isValidHexAddress(verifyingContract)) {
502+
const { domain: { verifyingContract } = {} } = parseTypedMessage(data);
503+
if (verifyingContract && !isValidHexAddress(verifyingContract)) {
506504
throw rpcErrors.invalidInput();
507505
}
508506
}

0 commit comments

Comments
 (0)