Contract is using functions from:
node_modules/@openzeppelin/contracts-upgradeable/utils/cryptography/draft-EIP712Upgradeable.sol
How can I recreate this functionality:
hash = keccak256(abi.encode(data));
_hashTypedDataV4(hash).recover(signature) == signerAddress
with javascript (ethers.js or web3 or ...)
What would be the js equivalent to
keccak256(abi.encode(data))
_hashTypedDataV4
recover
functions
Code to reproduce
see example above
More details, the "data" passed:
javascript:
const Types = {
AssetType: [
{name: 'assetClass', type: 'bytes4'},
{name: 'data', type: 'bytes'}
],
Asset: [
{name: 'assetType', type: 'AssetType'},
{name: 'value', type: 'uint256'}
]
};
solidity contract:
library LibAsset {
struct AssetType {
bytes4 assetClass;
bytes data;
}
struct Asset {
AssetType assetType;
uint value;
}
function hash(AssetType memory assetType) internal pure returns (bytes32) {
return keccak256(abi.encode(
ASSET_TYPE_TYPEHASH,
assetType.assetClass,
keccak256(assetType.data)
));
}
}
contract Validate {
function validate(LibAsset.asset memory asset, bytes memory signature, address memory
validateAddress) internal {
bytes32 hash = LibAsset.hash(asset);
require(_hashTypedDataV4(hash).recover(signature) == validateAddress,
"User: signature verification error"
);
}
}
Environment
hardhat or truffle
latest hardhat or truffle version