|
| 1 | +// As per ERC-1271 |
| 2 | +interface IERC1271Wallet { |
| 3 | + function isValidSignature(bytes32 hash, bytes calldata signature) external view returns (bytes4 magicValue); |
| 4 | +} |
| 5 | + |
| 6 | +error ERC1271Revert(bytes error); |
| 7 | +error ERC6492DeployFailed(bytes error); |
| 8 | + |
| 9 | +contract UniversalSigValidator { |
| 10 | + bytes32 private constant ERC6492_DETECTION_SUFFIX = 0x6492649264926492649264926492649264926492649264926492649264926492; |
| 11 | + bytes4 private constant ERC1271_SUCCESS = 0x1626ba7e; |
| 12 | + |
| 13 | + function isValidSigImpl( |
| 14 | + address _signer, |
| 15 | + bytes32 _hash, |
| 16 | + bytes calldata _signature, |
| 17 | + bool allowSideEffects, |
| 18 | + bool tryPrepare |
| 19 | + ) public returns (bool) { |
| 20 | + uint contractCodeLen = address(_signer).code.length; |
| 21 | + bytes memory sigToValidate; |
| 22 | + // The order here is strictly defined in https://eips.ethereum.org/EIPS/eip-6492 |
| 23 | + // - ERC-6492 suffix check and verification first, while being permissive in case the contract is already deployed; if the contract is deployed we will check the sig against the deployed version, this allows 6492 signatures to still be validated while taking into account potential key rotation |
| 24 | + // - ERC-1271 verification if there's contract code |
| 25 | + // - finally, ecrecover |
| 26 | + bool isCounterfactual = bytes32(_signature[_signature.length-32:_signature.length]) == ERC6492_DETECTION_SUFFIX; |
| 27 | + if (isCounterfactual) { |
| 28 | + address create2Factory; |
| 29 | + bytes memory factoryCalldata; |
| 30 | + (create2Factory, factoryCalldata, sigToValidate) = abi.decode(_signature[0:_signature.length-32], (address, bytes, bytes)); |
| 31 | + |
| 32 | + if (contractCodeLen == 0 || tryPrepare) { |
| 33 | + (bool success, bytes memory err) = create2Factory.call(factoryCalldata); |
| 34 | + if (!success) revert ERC6492DeployFailed(err); |
| 35 | + } |
| 36 | + } else { |
| 37 | + sigToValidate = _signature; |
| 38 | + } |
| 39 | + |
| 40 | + // Try ERC-1271 verification |
| 41 | + if (isCounterfactual || contractCodeLen > 0) { |
| 42 | + try IERC1271Wallet(_signer).isValidSignature(_hash, sigToValidate) returns (bytes4 magicValue) { |
| 43 | + bool isValid = magicValue == ERC1271_SUCCESS; |
| 44 | + |
| 45 | + // retry, but this time assume the prefix is a prepare call |
| 46 | + if (!isValid && !tryPrepare && contractCodeLen > 0) { |
| 47 | + return isValidSigImpl(_signer, _hash, _signature, allowSideEffects, true); |
| 48 | + } |
| 49 | + |
| 50 | + if (contractCodeLen == 0 && isCounterfactual && !allowSideEffects) { |
| 51 | + // if the call had side effects we need to return the |
| 52 | + // result using a `revert` (to undo the state changes) |
| 53 | + assembly { |
| 54 | + mstore(0, isValid) |
| 55 | + revert(31, 1) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return isValid; |
| 60 | + } catch (bytes memory err) { |
| 61 | + // retry, but this time assume the prefix is a prepare call |
| 62 | + if (!tryPrepare && contractCodeLen > 0) { |
| 63 | + return isValidSigImpl(_signer, _hash, _signature, allowSideEffects, true); |
| 64 | + } |
| 65 | + |
| 66 | + revert ERC1271Revert(err); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // ecrecover verification |
| 71 | + require(_signature.length == 65, 'SignatureValidator#recoverSigner: invalid signature length'); |
| 72 | + bytes32 r = bytes32(_signature[0:32]); |
| 73 | + bytes32 s = bytes32(_signature[32:64]); |
| 74 | + uint8 v = uint8(_signature[64]); |
| 75 | + if (v != 27 && v != 28) { |
| 76 | + revert('SignatureValidator: invalid signature v value'); |
| 77 | + } |
| 78 | + return ecrecover(_hash, v, r, s) == _signer; |
| 79 | + } |
| 80 | + |
| 81 | + function isValidSigWithSideEffects(address _signer, bytes32 _hash, bytes calldata _signature) |
| 82 | + external returns (bool) |
| 83 | + { |
| 84 | + return this.isValidSigImpl(_signer, _hash, _signature, true, false); |
| 85 | + } |
| 86 | + |
| 87 | + function isValidSig(address _signer, bytes32 _hash, bytes calldata _signature) |
| 88 | + external returns (bool) |
| 89 | + { |
| 90 | + try this.isValidSigImpl(_signer, _hash, _signature, false, false) returns (bool isValid) { return isValid; } |
| 91 | + catch (bytes memory error) { |
| 92 | + // in order to avoid side effects from the contract getting deployed, the entire call will revert with a single byte result |
| 93 | + uint len = error.length; |
| 94 | + if (len == 1) return error[0] == 0x01; |
| 95 | + // all other errors are simply forwarded, but in custom formats so that nothing else can revert with a single byte in the call |
| 96 | + else assembly { revert(error, len) } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// this is a helper so we can perform validation in a single eth_call without pre-deploying a singleton |
| 102 | +contract ValidateSigOffchain { |
| 103 | + constructor (address _signer, bytes32 _hash, bytes memory _signature) { |
| 104 | + UniversalSigValidator validator = new UniversalSigValidator(); |
| 105 | + bool isValidSig = validator.isValidSigWithSideEffects(_signer, _hash, _signature); |
| 106 | + assembly { |
| 107 | + mstore(0, isValidSig) |
| 108 | + return(31, 1) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments