Skip to content

Commit

Permalink
Validate prefix on external controller additions (#22)
Browse files Browse the repository at this point in the history
Validate prefix on external controller additions


---------

Authored-by: Robert Leonard 
Reviewed-by: Martin Riedel
  • Loading branch information
Robert-H-Leonard authored Oct 3, 2023
1 parent 5f4c770 commit a52b6ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/DidRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ contract DIDRegistry is IDidRegistry, Initializable, UUPSUpgradeable, OwnableUpg
}

function addExternalController(address didIdentifier, string calldata controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public {
_doesExternalControllerHaveCorrectPrefix(controller);
require(_doesExternalControllerExist(didIdentifier,controller) == -1, "External controller already exist");
didStates[didIdentifier].externalControllers.push(controller);

Expand Down Expand Up @@ -347,6 +348,16 @@ contract DIDRegistry is IDidRegistry, Initializable, UUPSUpgradeable, OwnableUpg
return false;
}

function _doesExternalControllerHaveCorrectPrefix(string memory str) internal pure {
bytes memory correctPrefix = bytes("did:");
bytes memory bytesString = bytes(str);

// Get first 4 charecters in string
for(uint i = 0; i < 4; i++) {
require(correctPrefix[i] == bytesString[i], "Invalid prefix for external controller. External controls must start with did:");
}
}

function _stringCompare(string memory str1, string memory str2) internal pure returns (bool) {
if (bytes(str1).length != bytes(str2).length) {
return false;
Expand Down
17 changes: 14 additions & 3 deletions test/DidRegistryControllerTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract DidRegistryControllerTest is DidRegistryTest {

function test_should_add_new_external_controller() public {
address user = vm.addr(1);
string memory newController = "testExternalController";
string memory newController = "did:testExternalController";

vm.startPrank(user); // Make transactions using users EOA
didRegistry.initializeDidState(user);
Expand All @@ -49,7 +49,7 @@ contract DidRegistryControllerTest is DidRegistryTest {

function test_should_remove_new_external_controller() public {
address user = vm.addr(1);
string memory newController = "testExternalController";
string memory newController = "did:testExternalController";

vm.startPrank(user); // Make transactions using users EOA
didRegistry.initializeDidState(user);
Expand All @@ -62,6 +62,17 @@ contract DidRegistryControllerTest is DidRegistryTest {
assertEq(controllers.length, 0);
}

function test_revert_should_fail_to_add_new_external_controller() public {
address user = vm.addr(1);
string memory newController = "testExternalController";

vm.startPrank(user); // Make transactions using users EOA
didRegistry.initializeDidState(user);

vm.expectRevert("Invalid prefix for external controller. External controls must start with did:");
didRegistry.addExternalController(user, newController);
}

function test_revert_should_fail_to_add_duplicate_native_controller() public {
address user = vm.addr(1);
address newController = vm.addr(2);
Expand Down Expand Up @@ -101,7 +112,7 @@ contract DidRegistryControllerTest is DidRegistryTest {

function test_revert_should_fail_to_add_duplicate_external_controller() public {
address user = vm.addr(1);
string memory newController = "testExternalController";
string memory newController = "did:testExternalController";

vm.startPrank(user); // Make transactions using users EOA
didRegistry.initializeDidState(user);
Expand Down

0 comments on commit a52b6ad

Please sign in to comment.