Skip to content

Commit

Permalink
Fix eip712Hash tests that included data (#184)
Browse files Browse the repository at this point in the history
The `eip712Hash` tests that included data had the wrong `primaryType`,
so the data wasn't actually included in the resulting hash. This was an
edge case that had not been considered, so another test was added to
capture it.
  • Loading branch information
Gudahtt authored Aug 19, 2021
1 parent ec0134f commit 6818ed3
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`TypedDataUtils.eip712Hash V3 should hash a minimal valid typed message

exports[`TypedDataUtils.eip712Hash V3 should hash a typed message with a domain separator that uses all fields 1`] = `"54ffed5209a17ac210ef3823740b3852ee9cd518b84ee39f0a3fa7f2f9b4205b"`;

exports[`TypedDataUtils.eip712Hash V3 should hash a typed message with data 1`] = `"54ffed5209a17ac210ef3823740b3852ee9cd518b84ee39f0a3fa7f2f9b4205b"`;
exports[`TypedDataUtils.eip712Hash V3 should hash a typed message with data 1`] = `"d2669f23b7849020ad41bcbff5b51372793f91320e0f901641945568ed7322be"`;

exports[`TypedDataUtils.eip712Hash V3 should hash a typed message with extra domain seperator fields 1`] = `"2323c742c20093ee44c2a0abef2b950062a3d70b3453ef4995a072dba9e47d7d"`;

Expand All @@ -14,7 +14,7 @@ exports[`TypedDataUtils.eip712Hash V4 should hash a minimal valid typed message

exports[`TypedDataUtils.eip712Hash V4 should hash a typed message with a domain separator that uses all fields. 1`] = `"54ffed5209a17ac210ef3823740b3852ee9cd518b84ee39f0a3fa7f2f9b4205b"`;

exports[`TypedDataUtils.eip712Hash V4 should hash a typed message with data 1`] = `"54ffed5209a17ac210ef3823740b3852ee9cd518b84ee39f0a3fa7f2f9b4205b"`;
exports[`TypedDataUtils.eip712Hash V4 should hash a typed message with data 1`] = `"d2669f23b7849020ad41bcbff5b51372793f91320e0f901641945568ed7322be"`;

exports[`TypedDataUtils.eip712Hash V4 should hash a typed message with extra domain seperator fields 1`] = `"2323c742c20093ee44c2a0abef2b950062a3d70b3453ef4995a072dba9e47d7d"`;

Expand Down
178 changes: 176 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3307,7 +3307,7 @@ describe('TypedDataUtils.eip712Hash', function () {
],
Message: [{ name: 'data', type: 'string' }],
},
primaryType: 'EIP712Domain',
primaryType: 'Message',
domain: {
name: 'example.metamask.io',
version: '1',
Expand All @@ -3324,6 +3324,93 @@ describe('TypedDataUtils.eip712Hash', function () {

expect(hash.toString('hex')).toMatchSnapshot();
});

it('should ignore message if the primary type is EIP712Domain', function () {
const hashWithMessage = sigUtil.TypedDataUtils.eip712Hash(
{
types: {
EIP712Domain: [
{
name: 'name',
type: 'string',
},
{
name: 'version',
type: 'string',
},
{
name: 'chainId',
type: 'uint256',
},
{
name: 'verifyingContract',
type: 'address',
},
{
name: 'salt',
type: 'bytes32',
},
],
Message: [{ name: 'data', type: 'string' }],
},
primaryType: 'EIP712Domain',
domain: {
name: 'example.metamask.io',
version: '1',
chainId: 1,
verifyingContract: '0x0000000000000000000000000000000000000000',
salt: Buffer.from(new Int32Array([1, 2, 3])),
},
message: {
data: 'Hello!',
},
},
'V3',
);
const hashWithoutMessage = sigUtil.TypedDataUtils.eip712Hash(
{
types: {
EIP712Domain: [
{
name: 'name',
type: 'string',
},
{
name: 'version',
type: 'string',
},
{
name: 'chainId',
type: 'uint256',
},
{
name: 'verifyingContract',
type: 'address',
},
{
name: 'salt',
type: 'bytes32',
},
],
Message: [{ name: 'data', type: 'string' }],
},
primaryType: 'EIP712Domain',
domain: {
name: 'example.metamask.io',
version: '1',
chainId: 1,
verifyingContract: '0x0000000000000000000000000000000000000000',
salt: Buffer.from(new Int32Array([1, 2, 3])),
},
message: {},
},
'V3',
);

expect(hashWithMessage.toString('hex')).toBe(
hashWithoutMessage.toString('hex'),
);
});
});

describe('V4', function () {
Expand Down Expand Up @@ -3572,7 +3659,7 @@ describe('TypedDataUtils.eip712Hash', function () {
],
Message: [{ name: 'data', type: 'string' }],
},
primaryType: 'EIP712Domain',
primaryType: 'Message',
domain: {
name: 'example.metamask.io',
version: '1',
Expand All @@ -3589,6 +3676,93 @@ describe('TypedDataUtils.eip712Hash', function () {

expect(hash.toString('hex')).toMatchSnapshot();
});

it('should ignore message if the primary type is EIP712Domain', function () {
const hashWithMessage = sigUtil.TypedDataUtils.eip712Hash(
{
types: {
EIP712Domain: [
{
name: 'name',
type: 'string',
},
{
name: 'version',
type: 'string',
},
{
name: 'chainId',
type: 'uint256',
},
{
name: 'verifyingContract',
type: 'address',
},
{
name: 'salt',
type: 'bytes32',
},
],
Message: [{ name: 'data', type: 'string' }],
},
primaryType: 'EIP712Domain',
domain: {
name: 'example.metamask.io',
version: '1',
chainId: 1,
verifyingContract: '0x0000000000000000000000000000000000000000',
salt: Buffer.from(new Int32Array([1, 2, 3])),
},
message: {
data: 'Hello!',
},
},
'V4',
);
const hashWithoutMessage = sigUtil.TypedDataUtils.eip712Hash(
{
types: {
EIP712Domain: [
{
name: 'name',
type: 'string',
},
{
name: 'version',
type: 'string',
},
{
name: 'chainId',
type: 'uint256',
},
{
name: 'verifyingContract',
type: 'address',
},
{
name: 'salt',
type: 'bytes32',
},
],
Message: [{ name: 'data', type: 'string' }],
},
primaryType: 'EIP712Domain',
domain: {
name: 'example.metamask.io',
version: '1',
chainId: 1,
verifyingContract: '0x0000000000000000000000000000000000000000',
salt: Buffer.from(new Int32Array([1, 2, 3])),
},
message: {},
},
'V4',
);

expect(hashWithMessage.toString('hex')).toBe(
hashWithoutMessage.toString('hex'),
);
});
});
});

Expand Down

0 comments on commit 6818ed3

Please sign in to comment.