Skip to content

Commit

Permalink
Add changeMainAuthorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Oct 30, 2024
1 parent 3b3c808 commit 08f91b8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/contracts/UserOverrideableDKIMRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ contract UserOverrideableDKIMRegistry is
address indexed authorizer
);

/// @notice Emitted when the main authorizer address is changed.
event MainAuthorizerChanged(address indexed newMainAuthorizer);

/// @notice Main authorizer address.
address public mainAuthorizer;

Expand Down Expand Up @@ -356,6 +359,27 @@ contract UserOverrideableDKIMRegistry is
emit DKIMPublicKeyHashReactivated(publicKeyHash, authorizer);
}

/**
* @notice Changes the main authorizer address.
* @param newMainAuthorizer The address of the new main authorizer.
* @custom:require Only the owner can change the main authorizer address.
* @custom:require The new main authorizer address cannot be zero.
* @custom:require The new main authorizer address cannot be the same as the current main authorizer.
* @custom:event MainAuthorizerChanged Emitted when the main authorizer address changes.
*/
function changeMainAuthorizer(address newMainAuthorizer) public onlyOwner {
require(
newMainAuthorizer != address(0),
"newMainAuthorizer address cannot be zero"
);
require(
newMainAuthorizer != mainAuthorizer,
"newMainAuthorizer address cannot be the same as the current mainAuthorizer"
);
mainAuthorizer = newMainAuthorizer;
emit MainAuthorizerChanged(newMainAuthorizer);
}

/**
* @notice Computes a signed message string for setting or revoking a DKIM public key hash.
* @param prefix The operation prefix (SET: or REVOKE:).
Expand Down
44 changes: 44 additions & 0 deletions packages/contracts/test/UserOverrideableDKIMRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,22 @@ contract UserOverrideableDKIMRegistryTest is Test {
vm.stopPrank();
}

function testChangeMainAuthorizer() public {
vm.startPrank(deployer);
vm.expectEmit();
emit UserOverrideableDKIMRegistry.MainAuthorizerChanged(user1);
registry.changeMainAuthorizer(user1);
vm.stopPrank();
}

function testChangeMainAuthorizerContract() public {
vm.startPrank(deployer);
vm.expectEmit();
emit UserOverrideableDKIMRegistry.MainAuthorizerChanged(user1);
registryWithContract.changeMainAuthorizer(address(user1));
vm.stopPrank();
}

function testFailIsDKIMPublicKeyHashValidByUser2() public {
testSetDKIMPublicKeyHashByUser1();

Expand Down Expand Up @@ -1099,4 +1115,32 @@ contract UserOverrideableDKIMRegistryTest is Test {
);
console.log(signedMsg);
}

function testExpectRevertChangeMainAuthorizerByNonOwner() public {
vm.startPrank(mainAuthorizer);
vm.expectRevert(
abi.encodeWithSelector(
OwnableUpgradeable.OwnableUnauthorizedAccount.selector,
mainAuthorizer
)
);
registry.changeMainAuthorizer(user1);
vm.stopPrank();
}

function testExpectRevertChangeMainAuthorizerIsZero() public {
vm.startPrank(deployer);
vm.expectRevert("newMainAuthorizer address cannot be zero");
registry.changeMainAuthorizer(address(0));
vm.stopPrank();
}

function testExpectRevertChangeMainAuthorizerIsSame() public {
vm.startPrank(deployer);
vm.expectRevert(
"newMainAuthorizer address cannot be the same as the current mainAuthorizer"
);
registry.changeMainAuthorizer(mainAuthorizer);
vm.stopPrank();
}
}

0 comments on commit 08f91b8

Please sign in to comment.