In the new version v5 of ERC20 we have the function permit to allowance accounts without spending gas, that is to say, with signature of the account:
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);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/ERC20Permit.sol)
pragma solidity ^0.8.20;
import {IERC20Permit} from "./IERC20Permit.sol";
import {ERC20} from "../ERC20.sol";
import {ECDSA} from "../../../utils/cryptography/ECDSA.sol";
import {EIP712} from "../../../utils/cryptography/EIP712.sol";
import {Nonces} from "../../../utils/Nonces.sol";
/**
* @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/
abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces {
This file has been truncated. show original
But in the parameters I am asked for some values that are returned with the splitSignature
signature in solidity:
https://solidity-by-example.org/signature/
How am I supposed to capture these values to pass them to you as a parameter?
The account signature will have to be done from the contract and not from a front end in order to call the splitSignature
function and capture those values requested in the parameters?
Hey @Cainuriel . In fact, the signature is just the values r,s
concatenated (and v depends on the chain), so you can either pick them up from the signature string, or use the ethers v6's Signature
class:
const { Signature } = require('ethers');
const signature = Signature.from('0x96f68bacfedd996d40a3eb4db679314545ce3d24f07b7db007528f8507f1f8654dd988fba90bf798173fa6b28222c3afb402ff5bbeac5dcc24bd204ae0778b911c');
signature.r; // 0x96f68bacfedd996d40a3eb4db679314545ce3d24f07b7db007528f8507f1f865
signature.s; // 0x4dd988fba90bf798173fa6b28222c3afb402ff5bbeac5dcc24bd204ae0778b91
signature.v // 27 | 28
Thanks @ernestognw
Since I see that you know ethers.js version 6 well, would you be so kind to answer me this question?
In our fronts, until version v5 of ethers.js we did it this way:
contract.on("TokenPurchase", (from, buyer, value, idToken) => {
console.log(`The account ${buyer} has purchased the nft with id #${idToken}`);
});
As I have researched this does not work in version 6. As discussed here:
it is recommended to downgrade to version 5.
The use of async is indifferent.
Do you know what is the new methodology in v6 to capture events in our fronts?
1 Like