eip712.js 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const { ethers } = require('hardhat');
  2. const types = require('./eip712-types');
  3. async function getDomain(contract) {
  4. const { fields, name, version, chainId, verifyingContract, salt, extensions } = await contract.eip712Domain();
  5. if (extensions.length > 0) {
  6. throw Error('Extensions not implemented');
  7. }
  8. const domain = {
  9. name,
  10. version,
  11. chainId,
  12. verifyingContract,
  13. salt,
  14. };
  15. for (const [i, { name }] of types.EIP712Domain.entries()) {
  16. if (!(fields & (1 << i))) {
  17. delete domain[name];
  18. }
  19. }
  20. return domain;
  21. }
  22. function domainType(domain) {
  23. return types.EIP712Domain.filter(({ name }) => domain[name] !== undefined);
  24. }
  25. function hashTypedData(domain, structHash) {
  26. return ethers.solidityPackedKeccak256(
  27. ['bytes', 'bytes32', 'bytes32'],
  28. ['0x1901', ethers.TypedDataEncoder.hashDomain(domain), structHash],
  29. );
  30. }
  31. module.exports = {
  32. getDomain,
  33. domainType,
  34. domainSeparator: ethers.TypedDataEncoder.hashDomain,
  35. hashTypedData,
  36. ...types,
  37. };