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

fix: [spearbit-34] Session key rotation registration check #19

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ contract SessionKeyPermissionsPlugin is ISessionKeyPermissionsPlugin, SessionKey

/// @inheritdoc ISessionKeyPermissionsPlugin
function rotateKey(address oldSessionKey, address newSessionKey) external override {
// Assert that the new key is not already registered
SessionKeyId newKeyId = _sessionKeyIdOf(msg.sender, newSessionKey);
if (SessionKeyId.unwrap(newKeyId) != 0) {
adam-alchemy marked this conversation as resolved.
Show resolved Hide resolved
revert KeyAlreadyRegistered(newSessionKey);
}
// Assert that the old key is registered
SessionKeyId keyId = _sessionKeyIdOf(msg.sender, oldSessionKey);
_assertRegistered(keyId, oldSessionKey);
// Update the key ID of the old key to zero, and the new key to the old key's ID
_updateSessionKeyId(msg.sender, oldSessionKey, SessionKeyId.wrap(bytes32(0)));
_updateSessionKeyId(msg.sender, newSessionKey, keyId);

Expand Down
29 changes: 29 additions & 0 deletions test/plugin/session/permissions/SessionKeyPermissionsPlugin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,35 @@ contract SessionKeyPermissionsPluginTest is Test {
assertEq(returnedEndTime, endTime);
}

function test_rotateKey_invalid_alreadyRegistered() public {
// Attempt to rotate the key to itself
vm.expectRevert(
abi.encodeWithSelector(ISessionKeyPermissionsPlugin.KeyAlreadyRegistered.selector, sessionKey1)
);
vm.prank(owner1);
SessionKeyPermissionsPlugin(address(account1)).rotateKey(sessionKey1, sessionKey1);

// Add a second session key
address sessionKey2 = makeAddr("sessionKey2");
address[] memory keysToAdd = new address[](1);
keysToAdd[0] = sessionKey2;
vm.prank(owner1);
SessionKeyPlugin(address(account1)).updateSessionKeys(
keysToAdd, new SessionKeyPlugin.SessionKeyToRemove[](0)
);

// Register the second session key
vm.prank(owner1);
SessionKeyPermissionsPlugin(address(account1)).registerKey(sessionKey2, 0);

// Attempt to rotate the key to the second session key
vm.expectRevert(
abi.encodeWithSelector(ISessionKeyPermissionsPlugin.KeyAlreadyRegistered.selector, sessionKey2)
);
vm.prank(owner1);
SessionKeyPermissionsPlugin(address(account1)).rotateKey(sessionKey1, sessionKey2);
}

function testFuzz_sessionKeyPermissions_setRequiredPaymaster(address requiredPaymaster) public {
bytes[] memory updates = new bytes[](1);
updates[0] = abi.encodeCall(ISessionKeyPermissionsUpdates.setRequiredPaymaster, (requiredPaymaster));
Expand Down