Skip to content

Commit

Permalink
rename TypedDataUtils.sign to TypedDataUtils.eip712Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinYardi committed Sep 24, 2020
1 parent 9f281f0 commit 109fc0b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ const TypedDataUtils = {
/**
* Signs a typed message as per EIP-712 and returns its sha3 hash
*
* @param {Object} typedData - Types message data to sign
* @param {Object} typedData - Types message data to hash as per eip-712
* @returns {Buffer} - sha3 hash of the resulting signed message
*/
sign<T extends MessageTypes> (typedData: Partial<TypedData | TypedMessage<T>>, useV4 = true): Buffer {
eip712Hash<T extends MessageTypes> (typedData: Partial<TypedData | TypedMessage<T>>, useV4 = true): Buffer {
const sanitizedData = this.sanitizeData(typedData);
const parts = [Buffer.from('1901', 'hex')];
parts.push(this.hashStruct('EIP712Domain', sanitizedData.domain, sanitizedData.types, useV4));
Expand Down Expand Up @@ -485,26 +485,26 @@ function recoverTypedMessage<T extends MessageTypes> (msgParams: SignedMsgParams
}

function signTypedData<T extends MessageTypes> (privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>): string {
const message = TypedDataUtils.sign(msgParams.data, false);
const message = TypedDataUtils.eip712Hash(msgParams.data, false);
const sig = ethUtil.ecsign(message, privateKey);
return ethUtil.bufferToHex(concatSig(sig.v, sig.r, sig.s));
}

function signTypedData_v4<T extends MessageTypes> (privateKey: Buffer, msgParams: MsgParams<TypedData | TypedMessage<T>>): string {
const message = TypedDataUtils.sign(msgParams.data);
const message = TypedDataUtils.eip712Hash(msgParams.data);
const sig = ethUtil.ecsign(message, privateKey);
return ethUtil.bufferToHex(concatSig(sig.v, sig.r, sig.s));
}

function recoverTypedSignature<T extends MessageTypes> (msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string {
const message = TypedDataUtils.sign(msgParams.data, false);
const message = TypedDataUtils.eip712Hash(msgParams.data, false);
const publicKey = recoverPublicKey(message, msgParams.sig);
const sender = ethUtil.publicToAddress(publicKey);
return ethUtil.bufferToHex(sender);
}

function recoverTypedSignature_v4<T extends MessageTypes> (msgParams: SignedMsgParams<TypedData | TypedMessage<T>>): string {
const message = TypedDataUtils.sign(msgParams.data);
const message = TypedDataUtils.eip712Hash(msgParams.data);
const publicKey = recoverPublicKey(message, msgParams.sig);
const sender = ethUtil.publicToAddress(publicKey);
return ethUtil.bufferToHex(sender);
Expand Down
12 changes: 6 additions & 6 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ test('signedTypeData', (t) => {
'0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e');
t.equal(ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types)),
'0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f');
t.equal(ethUtil.bufferToHex(utils.sign(typedData)),
t.equal(ethUtil.bufferToHex(utils.eip712Hash(typedData)),
'0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2');
t.equal(ethUtil.bufferToHex(address), '0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826');
t.equal(sig, '0x4355c47d63924e8a72e509b65029052eb6c299d53a04e167c5775fd466751c9d07299936d304c153f6443dfa05f40ff007d72911b6f72307f996231605b915621c');
Expand Down Expand Up @@ -563,7 +563,7 @@ test('signedTypeData with V3 string', (t) => {
'0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e');
t.equal(ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types)),
'0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f');
t.equal(ethUtil.bufferToHex(utils.sign(typedData)),
t.equal(ethUtil.bufferToHex(utils.eip712Hash(typedData)),
'0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2');
t.equal(ethUtil.bufferToHex(address), '0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826');
t.equal(sig, '0x4355c47d63924e8a72e509b65029052eb6c299d53a04e167c5775fd466751c9d07299936d304c153f6443dfa05f40ff007d72911b6f72307f996231605b915621c');
Expand Down Expand Up @@ -664,7 +664,7 @@ test('signedTypeData_v4', (t) => {
'0xeb4221181ff3f1a83ea7313993ca9218496e424604ba9492bb4052c03d5c3df8');
t.equal(ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types)),
'0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f');
t.equal(ethUtil.bufferToHex(utils.sign(typedData)),
t.equal(ethUtil.bufferToHex(utils.eip712Hash(typedData)),
'0xa85c2e2b118698e88db68a8105b794a8cc7cec074e89ef991cb4f5f533819cc2');

const privateKey = ethUtil.sha3('cow');
Expand Down Expand Up @@ -773,7 +773,7 @@ test('signedTypeData_v4', (t) => {
'0xeb4221181ff3f1a83ea7313993ca9218496e424604ba9492bb4052c03d5c3df8');
t.equal(ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types)),
'0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f');
t.equal(ethUtil.bufferToHex(utils.sign(typedData)),
t.equal(ethUtil.bufferToHex(utils.eip712Hash(typedData)),
'0xa85c2e2b118698e88db68a8105b794a8cc7cec074e89ef991cb4f5f533819cc2');

const privateKey = ethUtil.sha3('cow');
Expand Down Expand Up @@ -866,7 +866,7 @@ test('signedTypeData_v4 with recursive types', (t) => {
'0xfdc7b6d35bbd81f7fa78708604f57569a10edff2ca329c8011373f0667821a45');
t.equal(ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types)),
'0xfacb2c1888f63a780c84c216bd9a81b516fc501a19bae1fc81d82df590bbdc60');
t.equal(ethUtil.bufferToHex(utils.sign(typedData)),
t.equal(ethUtil.bufferToHex(utils.eip712Hash(typedData)),
'0x807773b9faa9879d4971b43856c4d60c2da15c6f8c062bd9d33afefb756de19c');

const privateKey = ethUtil.sha3('dragon');
Expand Down Expand Up @@ -959,7 +959,7 @@ test('signedTypeMessage V4 with recursive types', (t) => {
'0xfdc7b6d35bbd81f7fa78708604f57569a10edff2ca329c8011373f0667821a45');
t.equal(ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types)),
'0xfacb2c1888f63a780c84c216bd9a81b516fc501a19bae1fc81d82df590bbdc60');
t.equal(ethUtil.bufferToHex(utils.sign(typedData)),
t.equal(ethUtil.bufferToHex(utils.eip712Hash(typedData)),
'0x807773b9faa9879d4971b43856c4d60c2da15c6f8c062bd9d33afefb756de19c');

const privateKey = ethUtil.sha3('dragon');
Expand Down

0 comments on commit 109fc0b

Please sign in to comment.