Skip to content

Commit

Permalink
Break out domainHash generation
Browse files Browse the repository at this point in the history
  • Loading branch information
danfinlay committed Mar 15, 2023
1 parent 2caa080 commit 2008708
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/sign-typed-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,26 @@ function sanitizeData<T extends MessageTypes>(
return sanitizedData as Required<TypedMessage<T>>;
}

/**
* Create a EIP-712 Domain Hash.
* This hash is used at the top of the EIP-712 encoding.
*
* @param typedData - The typed message to hash.
* @param version - The EIP-712 version the encoding should comply with.
* @returns The hash of the domain object.
*/
function eip712DomainHash<T extends MessageTypes>(
typedData: TypedMessage<T>,
version: SignTypedDataVersion.V3 | SignTypedDataVersion.V4,
): Buffer {
validateVersion(version, [SignTypedDataVersion.V3, SignTypedDataVersion.V4]);

const sanitizedData = sanitizeData(typedData);
const { domain } = sanitizedData;
const domainType = { EIP712Domain: sanitizedData.types.EIP712Domain };
return hashStruct('EIP712Domain', domain, domainType, version);
}

/**
* Hash a typed message according to EIP-712. The returned message starts with the EIP-712 prefix,
* which is "1901", followed by the hash of the domain separator, then the data (if any).
Expand All @@ -387,14 +407,7 @@ function eip712Hash<T extends MessageTypes>(

const sanitizedData = sanitizeData(typedData);
const parts = [Buffer.from('1901', 'hex')];
parts.push(
hashStruct(
'EIP712Domain',
sanitizedData.domain,
sanitizedData.types,
version,
),
);
parts.push(eip712DomainHash(typedData, version));

if (sanitizedData.primaryType !== 'EIP712Domain') {
parts.push(
Expand Down

0 comments on commit 2008708

Please sign in to comment.