Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed Sep 26, 2024
1 parent e88d222 commit 025f311
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 149 deletions.
3 changes: 1 addition & 2 deletions contracts/Nexus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ contract Nexus is INexus, BaseAccount, ExecutionHelper, ModuleManager, UUPSUpgra
address next = validators.entries[SENTINEL];
while (next != ZERO_ADDRESS && next != SENTINEL) {
try IERC7739(next).supportsNestedTypedDataSign() returns (bytes32 res) {
if (res == SUPPORTS_NESTED_TYPED_DATA_SIGN)
return SUPPORTS_NESTED_TYPED_DATA_SIGN;
if (res == SUPPORTS_NESTED_TYPED_DATA_SIGN) return SUPPORTS_NESTED_TYPED_DATA_SIGN;
} catch {}
next = validators.entries[next];
}
Expand Down
77 changes: 28 additions & 49 deletions contracts/base/ERC7739Validator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ abstract contract ERC7739Validator is IERC7739 {
bytes32 internal constant _PERSONAL_SIGN_TYPEHASH = 0x983e65e5148e570cd828ead231ee759a8d7958721a768f93bc4483ba005c32de;
bytes32 internal constant _DOMAIN_TYPEHASH = 0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f;


/// @dev For automatic detection that the smart account supports the nested EIP-712 workflow.
/// By default, it returns `bytes32(bytes4(keccak256("supportsNestedTypedDataSign()")))`,
/// denoting support for the default behavior, as implemented in
Expand All @@ -22,23 +21,17 @@ abstract contract ERC7739Validator is IERC7739 {
result = bytes4(0xd620c85a);
}


/*//////////////////////////////////////////////////////////////////////////
INTERNAL
//////////////////////////////////////////////////////////////////////////*/


/// @dev Returns whether the `signature` is valid for the `hash.
/// Use this in your validator's `isValidSignatureWithSender` implementation.
function _erc1271IsValidSignatureWithSender(address sender, bytes32 hash, bytes calldata signature)
internal
view
virtual
returns (bool)
{
return _erc1271IsValidSignatureViaSafeCaller(sender, hash, signature)
|| _erc1271IsValidSignatureViaNestedEIP712(hash, signature)
|| _erc1271IsValidSignatureViaRPC(hash, signature);
function _erc1271IsValidSignatureWithSender(address sender, bytes32 hash, bytes calldata signature) internal view virtual returns (bool) {
return
_erc1271IsValidSignatureViaSafeCaller(sender, hash, signature) ||
_erc1271IsValidSignatureViaNestedEIP712(hash, signature) ||
_erc1271IsValidSignatureViaRPC(hash, signature);
}

/// @dev Returns whether the `msg.sender` is considered safe, such
Expand All @@ -56,19 +49,10 @@ abstract contract ERC7739Validator is IERC7739 {
/// module's specific internal function to validate the signature
/// against credentials.
/// Override for your module's custom logic.
function _erc1271IsValidSignatureNowCalldata(bytes32 hash, bytes calldata signature)
internal
view
virtual
returns (bool);
function _erc1271IsValidSignatureNowCalldata(bytes32 hash, bytes calldata signature) internal view virtual returns (bool);

/// @dev Unwraps and returns the signature.
function _erc1271UnwrapSignature(bytes calldata signature)
internal
view
virtual
returns (bytes calldata result)
{
function _erc1271UnwrapSignature(bytes calldata signature) internal view virtual returns (bytes calldata result) {
result = signature;
/// @solidity memory-safe-assembly
assembly {
Expand All @@ -87,12 +71,11 @@ abstract contract ERC7739Validator is IERC7739 {

/// @dev Performs the signature validation without nested EIP-712 if the caller is
/// a safe caller. A safe caller must include the address of this account in the hash.
function _erc1271IsValidSignatureViaSafeCaller(address sender, bytes32 hash, bytes calldata signature)
internal
view
virtual
returns (bool result)
{
function _erc1271IsValidSignatureViaSafeCaller(
address sender,
bytes32 hash,
bytes calldata signature
) internal view virtual returns (bool result) {
if (_erc1271CallerIsSafe(sender)) result = _erc1271IsValidSignatureNowCalldata(hash, signature);
}

Expand Down Expand Up @@ -167,12 +150,7 @@ abstract contract ERC7739Validator is IERC7739 {
/// All these are just for widespread out-of-the-box compatibility with other wallet clients.
/// We want to create bazaars, not walled castles.
/// And we'll use push the Turing Completeness of the EVM to the limits to do so.
function _erc1271IsValidSignatureViaNestedEIP712(bytes32 hash, bytes calldata signature)
internal
view
virtual
returns (bool result)
{
function _erc1271IsValidSignatureViaNestedEIP712(bytes32 hash, bytes calldata signature) internal view virtual returns (bool result) {
bytes32 t = _typedDataSignFieldsForAccount(msg.sender);
/// @solidity memory-safe-assembly
assembly {
Expand Down Expand Up @@ -202,7 +180,11 @@ abstract contract ERC7739Validator is IERC7739 {
// `d & 1 == 1` means that `contentsName` is invalid.
let d := shr(byte(0, mload(p)), 0x7fffffe000000000000010000000000) // Starts with `[a-z(]`.
// Store the end sentinel '(', and advance `p` until we encounter a '(' byte.
for { mstore(add(p, c), 40) } iszero(eq(byte(0, mload(p)), 40)) { p := add(p, 1) } {
for {
mstore(add(p, c), 40)
} iszero(eq(byte(0, mload(p)), 40)) {
p := add(p, 1)
} {
d := or(shr(byte(0, mload(p)), 0x120100000001), d) // Has a byte in ", )\x00".
}
mstore(p, " contents,bytes1 fields,string n") // Store the rest of the encoding.
Expand Down Expand Up @@ -230,12 +212,7 @@ abstract contract ERC7739Validator is IERC7739 {

/// @dev Performs the signature validation without nested EIP-712 to allow for easy sign ins.
/// This function must always return false or revert if called on-chain.
function _erc1271IsValidSignatureViaRPC(bytes32 hash, bytes calldata signature)
internal
view
virtual
returns (bool result)
{
function _erc1271IsValidSignatureViaRPC(bytes32 hash, bytes calldata signature) internal view virtual returns (bool result) {
// Non-zero gasprice is a heuristic to check if a call is on-chain,
// but we can't fully depend on it because it can be manipulated.
// See: https://x.com/NoahCitron/status/1580359718341484544
Expand All @@ -254,7 +231,9 @@ abstract contract ERC7739Validator is IERC7739 {
mstore(0x40, 0x40)
let gasToBurn := or(add(0xffff, gaslimit()), gaslimit())
// Burns gas computationally efficiently. Also, requires that `gas > gasToBurn`.
if or(eq(hash, b), lt(gas(), gasToBurn)) { invalid() }
if or(eq(hash, b), lt(gas(), gasToBurn)) {
invalid()
}
// Make a call to this with `b`, efficiently burning the gas provided.
// No valid transaction can consume more than the gaslimit.
// See: https://ethereum.github.io/yellowpaper/paper.pdf
Expand Down Expand Up @@ -299,14 +278,15 @@ abstract contract ERC7739Validator is IERC7739 {
/// @param structHash the typed data struct hash
function _hashTypedDataForAccount(address account, bytes32 structHash) private view returns (bytes32 digest) {
(
/*bytes1 fields*/,
string memory name,
,
/*bytes1 fields*/ string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
/*bytes32 salt*/,
/*uint256[] memory extensions*/
) = EIP712(account).eip712Domain();
,

) = /*bytes32 salt*/ /*uint256[] memory extensions*/
EIP712(account).eip712Domain();

/// @solidity memory-safe-assembly
assembly {
Expand All @@ -328,5 +308,4 @@ abstract contract ERC7739Validator is IERC7739 {
mstore(0x3a, 0)
}
}

}
4 changes: 2 additions & 2 deletions contracts/base/ModuleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ abstract contract ModuleManager is Storage, EIP712, IModuleManagerEventsAndError
revert ValidatorNotInstalled(enableModeSigValidator);
}
bytes32 eip712Digest = _hashTypedData(structHash);

// Use standard IERC-1271/ERC-7739 interface.
// Even if the validator doesn't support 7739 under the hood, it is still secure,
// Even if the validator doesn't support 7739 under the hood, it is still secure,
// as eip712digest is already built based on 712Domain of this Smart Account
// This interface should always be exposed by validators as per ERC-7579
try IValidator(enableModeSigValidator).isValidSignatureWithSender(address(this), eip712Digest, sig[20:]) returns (bytes4 res) {
Expand Down
1 change: 0 additions & 1 deletion contracts/lib/NonceLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ library NonceLib {
}
}
}

22 changes: 5 additions & 17 deletions contracts/mocks/MockSafe1271Caller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@ contract MockSafe1271Caller is IModule {
mapping(address smartAccount => uint256) balances;

function validateUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash) external returns (uint256) {

address account = userOp.sender;

// do something based on additional erc1271 sig
(bytes memory data, bytes memory erc1271sig, bytes memory userOpSig) = abi.decode(userOp.signature, (bytes, bytes, bytes));
bytes32 secureHash = keccak256(
abi.encode(
address(account),
block.chainid,
keccak256(data)
)
);
if(IERC7579Account(account).isValidSignature(secureHash, erc1271sig) == ERC1271_MAGICVALUE) {
bytes32 secureHash = keccak256(abi.encode(address(account), block.chainid, keccak256(data)));
if (IERC7579Account(account).isValidSignature(secureHash, erc1271sig) == ERC1271_MAGICVALUE) {
balances[account]++;
}
return VALIDATION_SUCCESS;
Expand All @@ -36,17 +29,12 @@ contract MockSafe1271Caller is IModule {
return balances[smartAccount];
}

function onInstall(bytes calldata data) external override {

}
function onInstall(bytes calldata data) external override {}

function onUninstall(bytes calldata data) external override {

}
function onUninstall(bytes calldata data) external override {}

function isModuleType(uint256 moduleTypeId) external pure returns (bool) {
return
moduleTypeId == MODULE_TYPE_VALIDATOR;
return moduleTypeId == MODULE_TYPE_VALIDATOR;
}

function isInitialized(address smartAccount) external view returns (bool) {
Expand Down
28 changes: 8 additions & 20 deletions contracts/mocks/MockValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@ contract MockValidator is ERC7739Validator {
return _validateSignatureForOwner(owner, userOpHash, userOp.signature) ? VALIDATION_SUCCESS : VALIDATION_FAILED;
}

function isValidSignatureWithSender(
function isValidSignatureWithSender(
address sender,
bytes32 hash,
bytes calldata signature
)
external
view
virtual
returns (bytes4 sigValidationResult)
{
) external view virtual returns (bytes4 sigValidationResult) {
// can put additional checks based on sender here

// check if sig is valid
bool success = _erc1271IsValidSignatureWithSender(sender, hash, _erc1271UnwrapSignature(signature));
/// @solidity memory-safe-assembly
assembly {
// `success ? bytes4(keccak256("isValidSignature(bytes32,bytes)")) : 0xffffffff`.
// We use `0xffffffff` for invalid, in convention with the reference implementation.
sigValidationResult := shl(224, or(0x1626ba7e, sub(0, iszero(success))))
}
}
}

// ISessionValidator interface for smart session
Expand All @@ -60,12 +55,7 @@ function isValidSignatureWithSender(
/// Obtains the authorized signer's credentials and calls some
/// module's specific internal function to validate the signature
/// against credentials.
function _erc1271IsValidSignatureNowCalldata(bytes32 hash, bytes calldata signature)
internal
view
override
returns (bool)
{
function _erc1271IsValidSignatureNowCalldata(bytes32 hash, bytes calldata signature) internal view override returns (bool) {
// obtain credentials
address owner = smartAccountOwners[msg.sender];

Expand All @@ -79,12 +69,10 @@ function isValidSignatureWithSender(
// The canonical `MulticallerWithSigner` at 0x000000000000D9ECebf3C23529de49815Dac1c4c
// is known to include the account in the hash to be signed.
// msg.sender = Smart Account
// sender = 1271 og request sender
// sender = 1271 og request sender
function _erc1271CallerIsSafe(address sender) internal view virtual override returns (bool) {
return (
sender == 0x000000000000D9ECebf3C23529de49815Dac1c4c || // MulticallerWithSigner
sender == msg.sender
);
return (sender == 0x000000000000D9ECebf3C23529de49815Dac1c4c || // MulticallerWithSigner
sender == msg.sender);
}

function onInstall(bytes calldata data) external {
Expand Down
Loading

0 comments on commit 025f311

Please sign in to comment.