Skip to content

Commit

Permalink
fixed and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin committed Sep 10, 2024
1 parent 7d7cbc2 commit 04eda48
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,51 @@ describe('wallet', () => {
});
});

it('should not throw if verifyingContract is "cosmos"', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice();
const witnessedMsgParams: TypedMessageParams[] = [];
const processTypedMessageV3 = async (msgParams: TypedMessageParams) => {
witnessedMsgParams.push(msgParams);
// Assume testMsgSig is the expected signature result
return testMsgSig;
};

engine.push(createWalletMiddleware({ getAccounts, processTypedMessageV3 }));

const message = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
},
primaryType: 'EIP712Domain',
message: {},
domain: {
verifyingContract: 'cosmos',
},
};

const stringifiedMessage = JSON.stringify(message);

const payload = {
method: 'eth_signTypedData_v3',
params: [testAddresses[0], stringifiedMessage], // Assuming testAddresses[0] is a valid address from your setup
};

const promise = pify(engine.handle).call(engine, payload);
const result = await promise;
expect(result).toStrictEqual({
id: undefined,
jsonrpc: undefined,
result:
'0x68dc980608bceb5f99f691e62c32caccaee05317309015e9454eba1a14c3cd4505d1dd098b8339801239c9bcaac3c4df95569dcf307108b92f68711379be14d81c',
});
});

describe('signTypedDataV4', () => {
const getMsgParams = (verifyingContract?: string) => ({
types: {
Expand Down
12 changes: 11 additions & 1 deletion src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,20 @@ WalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {
* Validates verifyingContract of typedSignMessage.
*
* @param data - The data passed in typedSign request.
* This function allows the verifyingContract to be either:
* - A valid hex address
* - The string "cosmos" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)
* - An empty string
*/
function validateVerifyingContract(data: string) {
const { domain: { verifyingContract } = {} } = parseTypedMessage(data);
if (verifyingContract && !(isValidHexAddress(verifyingContract) || verifyingContract == 'cosmos')) {
if (
verifyingContract &&
!(
isValidHexAddress(verifyingContract) ||
(verifyingContract as any) === 'cosmos'
)
) {
throw rpcErrors.invalidInput();
}
}
Expand Down

0 comments on commit 04eda48

Please sign in to comment.