mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
openzeppelin-contracts/test/helpers/eip712.js

46 lines
1.1 KiB

const { ethers } = require('ethers');
const types = require('./eip712-types');
async function getDomain(contract) {
const { fields, name, version, chainId, verifyingContract, salt, extensions } = await contract.eip712Domain();
if (extensions.length > 0) {
throw Error('Extensions not implemented');
}
const domain = {
name,
version,
// TODO: remove check when contracts are all migrated to ethers
chainId: web3.utils.isBN(chainId) ? chainId.toNumber() : chainId,
verifyingContract,
salt,
};
for (const [i, { name }] of types.EIP712Domain.entries()) {
if (!(fields & (1 << i))) {
delete domain[name];
}
}
return domain;
}
function domainType(domain) {
return types.EIP712Domain.filter(({ name }) => domain[name] !== undefined);
}
function hashTypedData(domain, structHash) {
return ethers.solidityPackedKeccak256(
['bytes', 'bytes32', 'bytes32'],
['0x1901', ethers.TypedDataEncoder.hashDomain(domain), structHash],
);
}
module.exports = {
types,
getDomain,
domainType,
domainSeparator: ethers.TypedDataEncoder.hashDomain,
hashTypedData,
};