* Compute EIP-712 domain separator hash.
(domain: {
name: string
version: string
chainId: number
verifyingContract: string
})
| 146 | * Compute EIP-712 domain separator hash. |
| 147 | */ |
| 148 | function computeDomainSeparator(domain: { |
| 149 | name: string |
| 150 | version: string |
| 151 | chainId: number |
| 152 | verifyingContract: string |
| 153 | }): Buffer { |
| 154 | const EIP712_DOMAIN_TYPEHASH = createHash('sha3-256') |
| 155 | .update( |
| 156 | 'EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)', |
| 157 | ) |
| 158 | .digest() |
| 159 | |
| 160 | const nameHash = createHash('sha3-256').update(domain.name).digest() |
| 161 | const versionHash = createHash('sha3-256').update(domain.version).digest() |
| 162 | |
| 163 | // ABI encode: typeHash + nameHash + versionHash + chainId + verifyingContract |
| 164 | const encoded = Buffer.alloc(5 * 32) |
| 165 | EIP712_DOMAIN_TYPEHASH.copy(encoded, 0) |
| 166 | nameHash.copy(encoded, 32) |
| 167 | versionHash.copy(encoded, 64) |
| 168 | // chainId as uint256 |
| 169 | const chainIdBuf = Buffer.alloc(32) |
| 170 | chainIdBuf.writeBigUInt64BE(BigInt(domain.chainId), 24) |
| 171 | chainIdBuf.copy(encoded, 96) |
| 172 | // verifyingContract as address (left-padded to 32 bytes) |
| 173 | const addrBuf = Buffer.alloc(32) |
| 174 | Buffer.from(domain.verifyingContract.replace('0x', ''), 'hex').copy( |
| 175 | addrBuf, |
| 176 | 12, |
| 177 | ) |
| 178 | addrBuf.copy(encoded, 128) |
| 179 | |
| 180 | return createHash('sha3-256').update(encoded).digest() |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Compute the EIP-712 struct hash for TransferWithAuthorization. |