Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Fix signTypedMessage parameter types (#250)
Browse files Browse the repository at this point in the history
The `signTypedMessage` parameter types accepted the data to sign as an
array. This works with `signTypedData_v1`, but is incompatible with
later versions.

The signature has been updated to accept data as either an array or an
object. Additionally, tests have been added to cover all three
supported versions of `signTypedData`.
  • Loading branch information
Gudahtt authored Jul 17, 2023
1 parent 3e102b5 commit ec8b5da
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 20 deletions.
172 changes: 153 additions & 19 deletions src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,27 +950,161 @@ describe('KeyringController', () => {
expect(result).toBe('SR6bQ1m3OTHvI1FLwcGzm+Uk6hffoFPxsQ0DTOeKMEc=');
});

it('signTypedMessage', async () => {
const inputParams = {
from: mockAddress,
data: [
{
type: 'string',
name: 'Message',
value: 'Hi, Alice!',
describe('signTypedMessage', () => {
it('signs a v1 typed message if no version is provided', async () => {
const inputParams = {
from: mockAddress,
data: [
{
type: 'string',
name: 'Message',
value: 'Hi, Alice!',
},
{
type: 'uint32',
name: 'A number',
value: '1337',
},
],
origin: 'https://metamask.github.io',
};
const result = await keyringController.signTypedMessage(inputParams);
expect(result).toMatchInlineSnapshot(
`"0x089bb031f5bf2b2cbdf49eb2bb37d6071ab71f950b9dc49e398ca2ba984aca3c189b3b8de6c14c56461460dd9f59443340f1b144aeeff73275ace41ac184e54f1c"`,
);
});

it('signs a v1 typed message', async () => {
const inputParams = {
from: mockAddress,
data: [
{
type: 'string',
name: 'Message',
value: 'Hi, Alice!',
},
{
type: 'uint32',
name: 'A number',
value: '1337',
},
],
origin: 'https://metamask.github.io',
};
const result = await keyringController.signTypedMessage(inputParams, {
version: 'V1',
});
expect(result).toMatchInlineSnapshot(
`"0x089bb031f5bf2b2cbdf49eb2bb37d6071ab71f950b9dc49e398ca2ba984aca3c189b3b8de6c14c56461460dd9f59443340f1b144aeeff73275ace41ac184e54f1c"`,
);
});

it('signs a v3 typed message', async () => {
const typedData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
},
{
type: 'uint32',
name: 'A number',
value: '1337',
primaryType: 'Mail' as const,
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
],
origin: 'https://metamask.github.io',
};
const result = await keyringController.signTypedMessage(inputParams);
expect(result).toBe(
'0x089bb031f5bf2b2cbdf49eb2bb37d6071ab71f950b9dc49e398ca2ba984aca3c189b3b8de6c14c56461460dd9f59443340f1b144aeeff73275ace41ac184e54f1c',
);
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
};
const inputParams = {
from: mockAddress,
data: typedData,
origin: 'https://metamask.github.io',
};
const result = await keyringController.signTypedMessage(inputParams, {
version: 'V3',
});
expect(result).toMatchInlineSnapshot(
`"0x1c496cc9f42fc8f8a30bef731b20a1b8722569473643c0cd92e3e494be9c62725043275475ca81d9691c6c31e188dfbd5884b4352ba21bd99f38e6d357c738b81b"`,
);
});

it('signs a v4 typed message', async () => {
const typedData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address[]' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person[]' },
{ name: 'contents', type: 'string' },
],
},
primaryType: 'Mail' as const,
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
message: {
from: {
name: 'Cow',
wallet: [
'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
'0xDD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
],
},
to: [
{
name: 'Bob',
wallet: ['0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'],
},
],
contents: 'Hello, Bob!',
},
};
const inputParams = {
from: mockAddress,
data: typedData,
origin: 'https://metamask.github.io',
};
const result = await keyringController.signTypedMessage(inputParams, {
version: 'V4',
});
expect(result).toMatchInlineSnapshot(
`"0xe8d6baed58a611bbe247aecf2a8cbe0e3877bf1828c6bd9402749ce9e16f557a5669102bd05f0c3e33c200ff965abf07dab9299cb4bcdc504c9a695205240b321c"`,
);
});
});
});
});
2 changes: 1 addition & 1 deletion src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class KeyringController extends EventEmitter {
async signTypedMessage(
msgParams: {
from: string;
data: Record<string, unknown>[];
data: Record<string, unknown> | Record<string, unknown>[];
},
opts: Record<string, unknown> = { version: 'V1' },
): Promise<string> {
Expand Down

0 comments on commit ec8b5da

Please sign in to comment.