Skip to content

Commit

Permalink
doc: Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
deluca-mike committed Mar 1, 2024
1 parent 865cdb0 commit 1d77e26
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 63 deletions.
14 changes: 6 additions & 8 deletions src/ERC20Extended.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ abstract contract ERC20Extended is IERC20Extended, ERC3009 {
bytes32 r_,
bytes32 s_
) external {
// NOTE: `_permit` returns the digest.
_revertIfInvalidSignature(owner_, _permit(owner_, spender_, value_, deadline_), v_, r_, s_);
_revertIfInvalidSignature(owner_, _permitAndGetDigest(owner_, spender_, value_, deadline_), v_, r_, s_);
}

/// @inheritdoc IERC20Extended
Expand All @@ -69,8 +68,7 @@ abstract contract ERC20Extended is IERC20Extended, ERC3009 {
uint256 deadline_,
bytes memory signature_
) external {
// NOTE: `_permit` returns the digest.
_revertIfInvalidSignature(owner_, _permit(owner_, spender_, value_, deadline_), signature_);
_revertIfInvalidSignature(owner_, _permitAndGetDigest(owner_, spender_, value_, deadline_), signature_);
}

/// @inheritdoc IERC20
Expand Down Expand Up @@ -110,7 +108,7 @@ abstract contract ERC20Extended is IERC20Extended, ERC3009 {
\******************************************************************************************************************/

/**
* @notice Approve `spender_` to spend `amount_` of tokens from `account_`.
* @dev Approve `spender_` to spend `amount_` of tokens from `account_`.
* @param account_ The address approving the allowance.
* @param spender_ The address approved to spend the tokens.
* @param amount_ The amount of tokens being approved for spending.
Expand All @@ -121,7 +119,7 @@ abstract contract ERC20Extended is IERC20Extended, ERC3009 {
}

/**
* @notice Set the `amount_` of tokens `spender_` is allowed to spend from `account_`.
* @dev Set the `amount_` of tokens `spender_` is allowed to spend from `account_`.
* @param account_ The address for which the allowance is set.
* @param spender_ The address allowed to spend the tokens.
* @param amount_ The amount of tokens being allowed for spending.
Expand All @@ -131,14 +129,14 @@ abstract contract ERC20Extended is IERC20Extended, ERC3009 {
}

/**
* @notice ERC-2612 permit extension for EIP-20 signed approvals.
* @dev Performs the approval based on the permit info, validates the deadline, and returns the digest.
* @param owner_ The address of the account approving the allowance.
* @param spender_ The address of the account being allowed to spend the tokens.
* @param amount_ The amount of tokens being approved for spending.
* @param deadline_ The deadline by which the signature must be used.
* @return digest_ The EIP-712 digest of the permit.
*/
function _permit(
function _permitAndGetDigest(
address owner_,
address spender_,
uint256 amount_,
Expand Down
56 changes: 28 additions & 28 deletions src/ERC3009.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {

/**
* @notice Construct the ERC3009 contract.
* @param name_ The name of the contract.
* @param name_ The name of the contract.
*/
constructor(string memory name_) StatefulERC712(name_) {}

Expand Down Expand Up @@ -192,7 +192,7 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
\******************************************************************************************************************/

/**
* @notice Returns the internal EIP-712 digest of a transferWithAuthorization call.
* @dev Returns the internal EIP-712 digest of a transferWithAuthorization call.
* @param from_ Payer's address (Authorizer).
* @param to_ Payee's address.
* @param value_ Amount to be transferred.
Expand Down Expand Up @@ -226,13 +226,13 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
}

/**
* @notice Common transfer function used by `transferWithAuthorization` and `_receiveWithAuthorization`.
* @param from_ Payer's address (Authorizer).
* @param to_ Payee's address.
* @param value_ Amount to be transferred.
* @param validAfter_ The time after which this is valid (unix time).
* @param validBefore_ The time before which this is valid (unix time).
* @param nonce_ Unique nonce.
* @dev Common transfer function used by `transferWithAuthorization` and `_receiveWithAuthorization`.
* @param from_ Payer's address (Authorizer).
* @param to_ Payee's address.
* @param value_ Amount to be transferred.
* @param validAfter_ The time after which this is valid (unix time).
* @param validBefore_ The time before which this is valid (unix time).
* @param nonce_ Unique nonce.
*/
function _transferWithAuthorization(
address from_,
Expand All @@ -255,7 +255,7 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
}

/**
* @notice Returns the internal EIP-712 digest of a receiveWithAuthorization call.
* @dev Returns the internal EIP-712 digest of a receiveWithAuthorization call.
* @param from_ Payer's address (Authorizer).
* @param to_ Payee's address.
* @param value_ Amount to be transferred.
Expand Down Expand Up @@ -289,13 +289,13 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
}

/**
* @notice Common receive function used by `receiveWithAuthorization`.
* @param from_ Payer's address (Authorizer).
* @param to_ Payee's address.
* @param value_ Amount to be transferred.
* @param validAfter_ The time after which this is valid (unix time).
* @param validBefore_ The time before which this is valid (unix time).
* @param nonce_ Unique nonce.
* @dev Common receive function used by `receiveWithAuthorization`.
* @param from_ Payer's address (Authorizer).
* @param to_ Payee's address.
* @param value_ Amount to be transferred.
* @param validAfter_ The time after which this is valid (unix time).
* @param validBefore_ The time before which this is valid (unix time).
* @param nonce_ Unique nonce.
*/
function _receiveWithAuthorization(
address from_,
Expand All @@ -311,7 +311,7 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
}

/**
* @notice Returns the internal EIP-712 digest of a cancelAuthorization call.
* @dev Returns the internal EIP-712 digest of a cancelAuthorization call.
* @param authorizer_ Authorizer's address.
* @param nonce_ Nonce of the authorization.
* @return The internal EIP-712 digest.
Expand All @@ -321,9 +321,9 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
}

/**
* @notice Common cancel function used by `cancelAuthorization`.
* @param authorizer_ Authorizer's address.
* @param nonce_ Nonce of the authorization.
* @dev Common cancel function used by `cancelAuthorization`.
* @param authorizer_ Authorizer's address.
* @param nonce_ Nonce of the authorization.
*/
function _cancelAuthorization(address authorizer_, bytes32 nonce_) internal {
_revertIfAuthorizationAlreadyUsed(authorizer_, nonce_);
Expand All @@ -334,19 +334,19 @@ abstract contract ERC3009 is IERC3009, StatefulERC712 {
}

/**
* @notice Reverts if the authorization is already used.
* @param authorizer_ The authorizer's address.
* @param nonce_ The nonce of the authorization.
* @dev Reverts if the authorization is already used.
* @param authorizer_ The authorizer's address.
* @param nonce_ The nonce of the authorization.
*/
function _revertIfAuthorizationAlreadyUsed(address authorizer_, bytes32 nonce_) internal view {
if (authorizationState[authorizer_][nonce_]) revert AuthorizationAlreadyUsed(authorizer_, nonce_);
}

/**
* @notice ERC20 transfer function that needs to be overridden by the inheriting contract.
* @param sender_ The sender's address.
* @param recipient_ The recipient's address.
* @param amount_ The amount to be transferred.
* @dev Internal ERC20 transfer function that needs to be implemented by the inheriting contract.
* @param sender_ The sender's address.
* @param recipient_ The recipient's address.
* @param amount_ The amount to be transferred.
*/
function _transfer(address sender_, address recipient_, uint256 amount_) internal virtual;
}
52 changes: 25 additions & 27 deletions src/ERC712Extended.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract contract ERC712Extended is IERC712Extended {
\******************************************************************************************************************/

/**
* @notice Computes the EIP-712 domain separator.
* @dev Computes the EIP-712 domain separator.
* @return The EIP-712 domain separator.
*/
function _getDomainSeparator() internal view returns (bytes32) {
Expand All @@ -93,7 +93,7 @@ abstract contract ERC712Extended is IERC712Extended {
}

/**
* @notice Returns the digest to be signed, via EIP-712, given an internal digest (i.e. hash struct).
* @dev Returns the digest to be signed, via EIP-712, given an internal digest (i.e. hash struct).
* @param internalDigest_ The internal digest.
* @return The digest to be signed.
*/
Expand All @@ -102,7 +102,7 @@ abstract contract ERC712Extended is IERC712Extended {
}

/**
* @notice Returns the signer of a signed digest, via EIP-712, and reverts if the signature is invalid.
* @dev Returns the signer of a signed digest, via EIP-712, and reverts if the signature is invalid.
* @param digest_ The digest that was signed.
* @param v_ v of the signature.
* @param r_ r of the signature.
Expand All @@ -123,23 +123,21 @@ abstract contract ERC712Extended is IERC712Extended {
}

/**
* @notice Revert if the signature is expired.
* @param expiry_ Timestamp at which the signature expires or max uint256 for no expiry.
* @dev Revert if the signature is expired.
* @param expiry_ Timestamp at which the signature expires or max uint256 for no expiry.
*/
function _revertIfExpired(uint256 expiry_) internal view {
if (block.timestamp > expiry_) revert SignatureExpired(expiry_, block.timestamp);
}

/**
* @notice Revert if the signature is invalid.
* @dev We first validate if the signature is a valid ECDSA signature
* and return early if it is the case.
* Then, we validate if it is a valid ERC-1271 signature,
* and return early if it is the case.
* If not, we revert with the error from the ECDSA signature validation.
* @param signer_ The signer of the signature.
* @param digest_ The digest that was signed.
* @param signature_ The signature.
* @dev Revert if the signature is invalid.
* @dev We first validate if the signature is a valid ECDSA signature and return early if it is the case.
* Then, we validate if it is a valid ERC-1271 signature, and return early if it is the case.
* If not, we revert with the error from the ECDSA signature validation.
* @param signer_ The signer of the signature.
* @param digest_ The digest that was signed.
* @param signature_ The signature.
*/
function _revertIfInvalidSignature(address signer_, bytes32 digest_, bytes memory signature_) internal view {
SignatureChecker.Error error_ = SignatureChecker.validateECDSASignature(signer_, digest_, signature_);
Expand All @@ -152,23 +150,23 @@ abstract contract ERC712Extended is IERC712Extended {
}

/**
* @notice Revert if the signature is invalid.
* @param signer_ The signer of the signature.
* @param digest_ The digest that was signed.
* @param r_ An ECDSA/secp256k1 signature parameter.
* @param vs_ An ECDSA/secp256k1 short signature parameter.
* @dev Revert if the signature is invalid.
* @param signer_ The signer of the signature.
* @param digest_ The digest that was signed.
* @param r_ An ECDSA/secp256k1 signature parameter.
* @param vs_ An ECDSA/secp256k1 short signature parameter.
*/
function _revertIfInvalidSignature(address signer_, bytes32 digest_, bytes32 r_, bytes32 vs_) internal pure {
_revertIfError(SignatureChecker.validateECDSASignature(signer_, digest_, r_, vs_));
}

/**
* @notice Revert if the signature is invalid.
* @param signer_ The signer of the signature.
* @param digest_ The digest that was signed.
* @param v_ v of the signature.
* @param r_ r of the signature.
* @param s_ s of the signature.
* @dev Revert if the signature is invalid.
* @param signer_ The signer of the signature.
* @param digest_ The digest that was signed.
* @param v_ v of the signature.
* @param r_ r of the signature.
* @param s_ s of the signature.
*/
function _revertIfInvalidSignature(
address signer_,
Expand All @@ -181,8 +179,8 @@ abstract contract ERC712Extended is IERC712Extended {
}

/**
* @notice Revert if error.
* @param error_ The SignatureChecker Error enum.
* @dev Revert if error.
* @param error_ The SignatureChecker Error enum.
*/
function _revertIfError(SignatureChecker.Error error_) private pure {
if (error_ == SignatureChecker.Error.NoError) return;
Expand Down

0 comments on commit 1d77e26

Please sign in to comment.