Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for sign typed data with bytes #146

Merged
merged 2 commits into from
Jul 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,105 @@ test('signedTypeData', (t) => {
);
});

test('signedTypeData with bytes', (t) => {
t.plan(8);

const typedDataWithBytes = {
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' },
{ name: 'payload', type: 'bytes' },
],
},
primaryType: 'Mail' as const,
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
payload: '0x25192142931f380985072cdd991e37f65cf8253ba7a0e675b54163a1d133b8ca',
},
};
const utils = sigUtil.TypedDataUtils;
const privateKey = ethUtil.sha3('cow');
const address = ethUtil.privateToAddress(privateKey);
const sig = sigUtil.signTypedData(privateKey, { data: typedDataWithBytes });

t.equal(
utils.encodeType('Mail', typedDataWithBytes.types),
'Mail(Person from,Person to,string contents,bytes payload)Person(string name,address wallet)'
);
t.equal(
ethUtil.bufferToHex(utils.hashType('Mail', typedDataWithBytes.types)),
'0x43999c52db673245777eb64b0330105de064e52179581a340a9856c32372528e'
);
t.equal(
ethUtil.bufferToHex(
utils.encodeData(
typedDataWithBytes.primaryType,
typedDataWithBytes.message,
typedDataWithBytes.types
)
),
'0x43999c52db673245777eb64b0330105de064e52179581a340a9856c32372528efc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8cd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1b5aadf3154a261abdd9086fc627b61efca26ae5702701d05cd2305f7c52a2fc8fac776d21ae071a32c362d4c20ba6586779708a56cad3a78d01b37ecb5744298'
);
t.equal(
ethUtil.bufferToHex(
utils.hashStruct(
typedDataWithBytes.primaryType,
typedDataWithBytes.message,
typedDataWithBytes.types
)
),
'0xe004bdc1ca57ba9ad5ea8c81e54dcbdb3bfce2d1d5ad92113f0871fb2a6eb052'
);
t.equal(
ethUtil.bufferToHex(
utils.hashStruct(
'EIP712Domain',
typedDataWithBytes.domain,
typedDataWithBytes.types
)
),
'0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f'
);
t.equal(
ethUtil.bufferToHex(utils.sign(typedDataWithBytes)),
'0xb4aaf457227fec401db772ec22d2095d1235ee5d0833f56f59108c9ffc90fb4b'
);
t.equal(
ethUtil.bufferToHex(address),
'0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826'
);
t.equal(
sig,
'0xdd17ea877a7da411c85ff94bc54180631d0e86efdcd68876aeb2e051417b68e76be6858d67b20baf7be9c6402d49930bfea2535e9ae150e85838ee265094fd081b'
);
});

test('signedTypeData with V3 string', (t) => {
t.plan(8);

Expand Down