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

feat: add Pausable to KeyRegistry #368

Merged
merged 1 commit into from
Aug 22, 2023
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
4 changes: 2 additions & 2 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BundleRegistryGasUsageTest:testGasRegisterWithSig() (gas: 834117)
BundleRegistryGasUsageTest:testGasTrustedBatchRegister() (gas: 7068101)
BundleRegistryGasUsageTest:testGasTrustedRegister() (gas: 914283)
BundleRegistryGasUsageTest:testGasTrustedBatchRegister() (gas: 7091701)
BundleRegistryGasUsageTest:testGasTrustedRegister() (gas: 918443)
IdRegistryGasUsageTest:testGasRegister() (gas: 736116)
IdRegistryGasUsageTest:testGasRegisterForAndRecover() (gas: 1743798)
IdRegistryGasUsageTest:testGasRegisterFromTrustedCaller() (gas: 809179)
Expand Down
25 changes: 21 additions & 4 deletions src/KeyRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.21;

import {EIP712} from "openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {Nonces} from "openzeppelin-latest/contracts/utils/Nonces.sol";
import {Pausable} from "openzeppelin/contracts/security/Pausable.sol";

import {IdRegistryLike} from "./interfaces/IdRegistryLike.sol";
import {IMetadataValidator} from "./interfaces/IMetadataValidator.sol";
Expand All @@ -16,7 +17,7 @@ import {TrustedCaller} from "./lib/TrustedCaller.sol";
* @notice See ../docs/docs.md for an overview.
*/

contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
contract KeyRegistry is TrustedCaller, Signatures, Pausable, EIP712, Nonces {
/**
* @notice State enumeration for a key in the registry. During migration, an admin can change
* the state of any fids key from NULL to ADDED or ADDED to NULL. After migration, an
Expand Down Expand Up @@ -492,6 +493,22 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
idRegistry = IdRegistryLike(_idRegistry);
}

/**
* @notice Pause add, remove, and reset.e registration, transfer, and recovery.
* Must be called by the owner.
*/
function pause() external onlyOwner {
_pause();
}

/**
* @notice Unpause add, remove, and reset.otice Unpause registration, transfer, and recovery.
* Must be called by the owner.
*/
function unpause() external onlyOwner {
_unpause();
}

/*//////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////*/
Expand All @@ -502,7 +519,7 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
bytes calldata key,
uint8 metadataType,
bytes calldata metadata
) internal {
) internal whenNotPaused {
KeyData storage keyData = keys[fid][key];
if (keyData.state != KeyState.NULL) revert InvalidState();

Expand All @@ -518,15 +535,15 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
emit Add(fid, keyType, key, key, metadataType, metadata);
}

function _remove(uint256 fid, bytes calldata key) internal {
function _remove(uint256 fid, bytes calldata key) internal whenNotPaused {
KeyData storage keyData = keys[fid][key];
if (keyData.state != KeyState.ADDED) revert InvalidState();

keyData.state = KeyState.REMOVED;
emit Remove(fid, key, key);
}

function _reset(uint256 fid, bytes calldata key) internal {
function _reset(uint256 fid, bytes calldata key) internal whenNotPaused {
KeyData storage keyData = keys[fid][key];
if (keyData.state != KeyState.ADDED) revert InvalidState();

Expand Down
194 changes: 194 additions & 0 deletions test/KeyRegistry/KeyRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
assertRemoved(fid, key, keyType);
}

function testFuzzAddRevertsPaused(
address to,
address recovery,
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes memory metadata
) public {
keyType = uint32(bound(keyType, 1, type(uint32).max));
metadataType = uint8(bound(metadataType, 1, type(uint8).max));

_registerFid(to, recovery);
_registerValidator(keyType, metadataType);

vm.prank(owner);
keyRegistry.pause();

vm.prank(to);
vm.expectRevert("Pausable: paused");
keyRegistry.add(keyType, key, metadataType, metadata);
}

function testFuzzAddFor(
address registrar,
uint256 ownerPk,
Expand Down Expand Up @@ -338,6 +360,35 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
assertNull(fid, key);
}

function testFuzzAddForRevertsPaused(
address registrar,
uint256 fidOwnerPk,
address recovery,
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes calldata metadata,
uint40 _deadline
) public {
keyType = uint32(bound(keyType, 1, type(uint32).max));

uint256 deadline = _boundDeadline(_deadline);
fidOwnerPk = _boundPk(fidOwnerPk);

address fidOwner = vm.addr(fidOwnerPk);
uint256 fid = _registerFid(fidOwner, recovery);
bytes memory sig = _signAdd(fidOwnerPk, fidOwner, keyType, key, metadataType, metadata, deadline);

vm.prank(owner);
keyRegistry.pause();

vm.prank(registrar);
vm.expectRevert("Pausable: paused");
keyRegistry.addFor(fidOwner, keyType, key, metadataType, metadata, deadline, sig);

assertNull(fid, key);
}

function testFuzzTrustedAdd(
address to,
address recovery,
Expand Down Expand Up @@ -433,6 +484,32 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
assertNull(fid, key);
}

function testFuzzTrustedAddRevertsPaused(
address to,
address recovery,
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes calldata metadata
) public {
keyType = uint32(bound(keyType, 1, type(uint32).max));
metadataType = uint8(bound(metadataType, 1, type(uint8).max));
_registerValidator(keyType, metadataType);

vm.startPrank(owner);
keyRegistry.setTrustedCaller(trustedCaller);
keyRegistry.pause();
vm.stopPrank();

uint256 fid = _registerFid(to, recovery);

vm.prank(trustedCaller);
vm.expectRevert("Pausable: paused");
keyRegistry.trustedAdd(to, keyType, key, metadataType, metadata);

assertNull(fid, key);
}

function testAddTypeHash() public {
assertEq(
keyRegistry.addTypehash(),
Expand Down Expand Up @@ -537,6 +614,35 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
assertRemoved(fid, key, keyType);
}

function testFuzzRemoveRevertsWhenPaused(
address to,
address recovery,
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes memory metadata
) public {
keyType = uint32(bound(keyType, 1, type(uint32).max));
metadataType = uint8(bound(metadataType, 1, type(uint8).max));

uint256 fid = _registerFid(to, recovery);
_registerValidator(keyType, metadataType);

vm.startPrank(to);
keyRegistry.add(keyType, key, metadataType, metadata);
keyRegistry.remove(key);
vm.stopPrank();

vm.prank(owner);
keyRegistry.pause();

vm.prank(to);
vm.expectRevert("Pausable: paused");
keyRegistry.remove(key);

assertRemoved(fid, key, keyType);
}

function testFuzzRemoveFor(
address registrar,
uint256 ownerPk,
Expand Down Expand Up @@ -691,6 +797,42 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
assertAdded(fid, key, keyType);
}

function testFuzzRemoveForRevertsWhenPaused(
address registrar,
uint256 fidOwnerPk,
address recovery,
uint32 keyType,
bytes calldata key,
uint8 metadataType,
bytes memory metadata,
uint40 _deadline
) public {
keyType = uint32(bound(keyType, 1, type(uint32).max));
metadataType = uint8(bound(metadataType, 1, type(uint8).max));

uint256 deadline = _boundDeadline(_deadline);
fidOwnerPk = _boundPk(fidOwnerPk);
_registerValidator(keyType, metadataType);

address fidOwner = vm.addr(fidOwnerPk);
uint256 fid = _registerFid(fidOwner, recovery);
bytes memory sig = _signRemove(fidOwnerPk, fidOwner, key, deadline);

vm.prank(fidOwner);
keyRegistry.add(keyType, key, metadataType, metadata);
assertEq(keyRegistry.keyDataOf(fid, key).state, KeyRegistry.KeyState.ADDED);
assertEq(keyRegistry.keyDataOf(fid, key).keyType, keyType);

vm.prank(owner);
keyRegistry.pause();

vm.prank(registrar);
vm.expectRevert("Pausable: paused");
keyRegistry.removeFor(fidOwner, key, deadline, sig);

assertAdded(fid, key, keyType);
}

function testRemoveTypeHash() public {
assertEq(
keyRegistry.removeTypehash(), keccak256("Remove(address owner,bytes key,uint256 nonce,uint256 deadline)")
Expand Down Expand Up @@ -845,6 +987,20 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
vm.stopPrank();
}

function testBulkAddRevertsWhenPaused() public {
_registerValidator(1, 1);

KeyRegistry.BulkAddData[] memory addItems =
BulkAddDataBuilder.empty().addFid(1).addKey(0, "key1", "metadata1").addFid(2).addKey(1, "key2", "metadata2");

vm.startPrank(owner);
keyRegistry.pause();

vm.expectRevert("Pausable: paused");
keyRegistry.bulkAddKeysForMigration(addItems);
vm.stopPrank();
}

/*//////////////////////////////////////////////////////////////
BULK REMOVE
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -988,6 +1144,44 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
vm.stopPrank();
}

function testFuzzBulkRemoveSignerForMigrationRevertsWhenPaused() public {
KeyRegistry.BulkResetData[] memory items = BulkResetDataBuilder.empty().addFid(1).addKey(0, "key");

vm.startPrank(owner);
keyRegistry.pause();

vm.expectRevert("Pausable: paused");
keyRegistry.bulkResetKeysForMigration(items);

vm.stopPrank();
}

/*//////////////////////////////////////////////////////////////
PAUSABILITY
//////////////////////////////////////////////////////////////*/

function testFuzzOnlyAdminCanPause(address caller) public {
vm.assume(caller != owner);

vm.prank(caller);
vm.expectRevert("Ownable: caller is not the owner");
keyRegistry.pause();
}

function testPauseUnpause() public {
assertEq(keyRegistry.paused(), false);

vm.prank(owner);
keyRegistry.pause();

assertEq(keyRegistry.paused(), true);

vm.prank(owner);
keyRegistry.unpause();

assertEq(keyRegistry.paused(), false);
}

/*//////////////////////////////////////////////////////////////
SET IDREGISTRY
//////////////////////////////////////////////////////////////*/
Expand Down
Loading