I would like to check if I am correctly performing the permit function call from my front end.
I am going step by step because it is complicated:
First, I capture the signer and the current time of the block:
const signerUser = await getSigner();
let actualTime = "";
actualTime = await getBlockTime()
I capture the nonce, the value, with the current time I generate a future deadline and I add 1 to the current nonce:
const nonce = await contractMiles.nonces($userAddressComplet);
const integerPriceChangeAirportWithMiles = parseInt(priceChangeAirportWithMiles); // 10000000000000000000
const deadlineOfSign = parseInt(actualTime) + 36000;
const intNonce = parseInt(nonce) + 1;
I take all this data and generate the hash using the ethers library in its version ^6.7.1"
.
let messageHash = ethers.solidityPackedKeccak256([ "address", "address", "uint256", "uint256", "uint256"], [$userAddressComplet, config.contractPassports, integerPriceChangeAirportWithMiles.toString(), intNonce.toString(), deadlineOfSign.toString()]);
Now we need the r, s, v and use the following functions:
const rawSig = await signerUser.signMessage(messageHash);
const signature = ethers.Signature.from(rawSig);
signature.r; // 0x96f68bacfedd996d40a3eb4db679314545ce3d24f07b7db007528f8507f1f865
signature.s; // 0x4dd988fba90bf798173fa6b28222c3afb402ff5bbeac5dcc24bd204ae0778b91
signature.v // 28
Finally we send all these data to the permit function that I share with you:
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
if (block.timestamp > deadline) {
revert ERC2612ExpiredSignature(deadline);
}
bytes32 structHash = keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
bytes32 hash = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(hash, v, r, s);
if (signer != owner) {
revert ERC2612InvalidSigner(signer, owner);
}
_approve(owner, spender, value);
}
It does not work for me.
I'm worried about passing a bytes32 that I don't enter in my hash, the PERMIT_TYPEHASH
.
bytes32 private constant PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
I use updatable contracts and do not receive readable error.
I share at least the whole process so that you can check if at least, the steps seem correct.
Any help will be always appreciated.