Skip to content
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

chore: clean up unused code #207

Merged
merged 17 commits into from
Dec 9, 2024
Merged
27 changes: 18 additions & 9 deletions src/validators/SessionKeyValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract SessionKeyValidator is IValidationHook, IModuleValidator {

function createSession(SessionLib.SessionSpec memory sessionSpec) public {
bytes32 sessionHash = keccak256(abi.encode(sessionSpec));
require(_isInitialized(msg.sender), "Account not initialized");
require(_isHookInitialized(msg.sender), "Account not initialized");
require(sessionSpec.signer != address(0), "Invalid signer");
require(sessions[sessionHash].status[msg.sender] == SessionLib.Status.NotInitialized, "Session already exists");
require(sessionSpec.feeLimit.limitType != SessionLib.LimitType.Unlimited, "Unlimited fee allowance is not safe");
Expand All @@ -68,30 +68,31 @@ contract SessionKeyValidator is IValidationHook, IModuleValidator {

function init(bytes calldata data) external {
// to prevent duplicate inits, since this can be hook plus a validator
if (!_isInitialized(msg.sender) && data.length != 0) {
if (!_isHookAndModuleInitialized(msg.sender) && data.length != 0) {
require(_addValidationKey(data), "init failed");
}
}

function _addValidationKey(bytes memory sessionData) internal returns (bool) {
if (sessionData.length == 0) {
return false;
}
SessionLib.SessionSpec memory sessionSpec = abi.decode(sessionData, (SessionLib.SessionSpec));
createSession(sessionSpec);
return true;
}

function disable() external {
if (_isInitialized(msg.sender)) {
if (_isHookInitialized(msg.sender)) {
// Here we have to revoke all keys, so that if the module
// is installed again later, there will be no active sessions from the past.
// Problem: if there are too many keys, this will run out of gas.
// Solution: before uninstalling, require that all keys are revoked manually.
require(sessionCounter[msg.sender] == 0, "Revoke all keys first");
ly0va marked this conversation as resolved.
Show resolved Hide resolved
IValidatorManager(msg.sender).removeModuleValidator(address(this));
IHookManager(msg.sender).removeHook(address(this), true);
}

// Check module and hook independently so it's not stuck in a bad state
if (_isModuleInitialized(msg.sender)) {
IValidatorManager(msg.sender).removeModuleValidator(address(this));
}
}

function supportsInterface(bytes4 interfaceId) external pure override returns (bool) {
Expand Down Expand Up @@ -122,13 +123,21 @@ contract SessionKeyValidator is IValidationHook, IModuleValidator {
* @return true if validator is registered for the account, false otherwise
*/
function isInitialized(address smartAccount) external view returns (bool) {
return _isInitialized(smartAccount);
return _isHookAndModuleInitialized(smartAccount);
}

function _isInitialized(address smartAccount) internal view returns (bool) {
function _isHookAndModuleInitialized(address smartAccount) internal view returns (bool) {
return _isHookInitialized(smartAccount) && _isModuleInitialized(smartAccount);
}

function _isHookInitialized(address smartAccount) internal view returns (bool) {
return IHookManager(smartAccount).isHook(address(this));
}

function _isModuleInitialized(address smartAccount) internal view returns (bool) {
return IValidatorManager(smartAccount).isModuleValidator(address(this));
}

function validationHook(bytes32 signedHash, Transaction calldata transaction, bytes calldata hookData) external {
(bytes memory signature, address validator, ) = abi.decode(transaction.signature, (bytes, address, bytes[]));
if (validator != address(this)) {
Expand Down
2 changes: 1 addition & 1 deletion src/validators/WebAuthValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract WebAuthValidator is VerifierCaller, IModuleValidator {
// We can only disconnect the module from the account,
// re-linking it will allow any previous keys
function disable() external {
require(false, "Cannot disable module without removing it from account");
revert("Cannot disable module without removing it from account");
}

function _addValidationKey(bytes memory key) internal returns (bool) {
Expand Down
2 changes: 1 addition & 1 deletion test/SessionKeyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class SessionTester {
};

const signedTransaction = await this.sessionAccount.signTransaction(this.aaTransaction);
await expect (provider.broadcastTransaction(signedTransaction)).to.be.reverted;
await expect(provider.broadcastTransaction(signedTransaction)).to.be.reverted;
};

getLimit(limit?: PartialLimit): SessionLib.UsageLimitStruct {
Expand Down
Loading