Skip to content

Move areValidNSignaturesNow to ERC7913Utils #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions contracts/mocks/ERC7913VerifierMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ contract ERC7913VerifierMock is IERC7913SignatureVerifier {
}

function verify(bytes calldata key, bytes32 /* hash */, bytes calldata signature) external pure returns (bytes4) {
// For testing purposes, we'll only accept a specific key and signature combination
if (
keccak256(key) == keccak256(abi.encodePacked("valid_key")) &&
keccak256(signature) == keccak256(abi.encodePacked("valid_signature"))
) {
// For testing purposes, we'll only accept specific key/signature combinations
if (_isKnownSigner1(key, signature) || _isKnownSigner2(key, signature)) {
return IERC7913SignatureVerifier.verify.selector;
}
return 0xffffffff;
}

function _isKnownSigner1(bytes calldata key, bytes calldata signature) internal pure returns (bool) {
return
keccak256(key) == keccak256(abi.encodePacked("valid_key_1")) &&
keccak256(signature) == keccak256(abi.encodePacked("valid_signature_1"));
}

function _isKnownSigner2(bytes calldata key, bytes calldata signature) internal pure returns (bool) {
return
keccak256(key) == keccak256(abi.encodePacked("valid_key_2")) &&
keccak256(signature) == keccak256(abi.encodePacked("valid_signature_2"));
}
}
50 changes: 50 additions & 0 deletions contracts/utils/cryptography/ERC7913Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,54 @@ library ERC7913Utils {
abi.decode(result, (bytes32)) == bytes32(IERC7913SignatureVerifier.verify.selector));
}
}

/**
* @dev Verifies multiple `signatures` for a given hash using a set of `signers`.
*
* The signers must be ordered by their `signerId` to ensure no duplicates and to optimize
* the verification process. The function will return `false` if the signers are not properly ordered.
*
* Requirements:
*
* * The `signatures` array must be at least the `signers` array's length.
*
* NOTE: The `signerId` function argument must be deterministic and should not manipulate
* memory state directly and should follow Solidity memory safety rules to avoid unexpected behavior.
*/
function areValidNSignaturesNow(
bytes32 hash,
bytes[] memory signers,
bytes[] memory signatures,
function(bytes memory) view returns (bytes32) signerId
) internal view returns (bool) {
bytes32 currentSignerId = bytes32(0);

uint256 signersLength = signers.length;
for (uint256 i = 0; i < signersLength; i++) {
bytes memory signer = signers[i];
// Signers must ordered by id to ensure no duplicates
bytes32 id = signerId(signer);
if (currentSignerId >= id || !isValidSignatureNow(signer, hash, signatures[i])) {
return false;
}

currentSignerId = id;
}

return true;
}

/// @dev Overload of {areValidNSignaturesNow} that uses the `keccak256` as the `signerId` function.
function areValidNSignaturesNow(
bytes32 hash,
bytes[] memory signers,
bytes[] memory signatures
) internal view returns (bool) {
return areValidNSignaturesNow(hash, signers, signatures, _keccak256);
}

/// @dev Computes the keccak256 hash of the given data.
function _keccak256(bytes memory data) private pure returns (bytes32) {
return keccak256(data);
}
}
14 changes: 3 additions & 11 deletions contracts/utils/cryptography/MultiSignerERC7913.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
*/
abstract contract MultiSignerERC7913 is AbstractSigner {
using EnumerableSetExtended for EnumerableSetExtended.BytesSet;
using ERC7913Utils for bytes;
using ERC7913Utils for *;
using SafeCast for uint256;

EnumerableSetExtended.BytesSet private _signersSet;
Expand Down Expand Up @@ -198,21 +198,13 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
bytes[] memory signingSigners,
bytes[] memory signatures
) internal view virtual returns (bool valid) {
bytes32 currentSignerId = bytes32(0);

uint256 signersLength = signingSigners.length;
for (uint256 i = 0; i < signersLength; i++) {
// Signers must ordered by id to ensure no duplicates
bytes memory signer = signingSigners[i];
bytes32 id = signerId(signer);
if (currentSignerId >= id || !isSigner(signer) || !signer.isValidSignatureNow(hash, signatures[i])) {
if (!isSigner(signingSigners[i])) {
return false;
}

currentSignerId = id;
}

return true;
return hash.areValidNSignaturesNow(signingSigners, signatures, signerId);
}

/**
Expand Down
Loading