diff --git a/src/DidRegistry.sol b/src/DidRegistry.sol index 27eed29..2e14d17 100644 --- a/src/DidRegistry.sol +++ b/src/DidRegistry.sol @@ -203,54 +203,49 @@ contract DIDRegistry is IDidRegistry, Initializable, UUPSUpgradeable, OwnableUpg } function addNativeController(address didIdentifier, address controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public { - require(!_doesNativeControllerExist(didIdentifier,controller), "Native controller already exist"); + require(_doesNativeControllerExist(didIdentifier,controller) == -1, "Native controller already exist"); didStates[didIdentifier].nativeControllers.push(controller); emit ControllerAdded(didIdentifier, abi.encodePacked(controller), true); } - function removeNativeController(address didIdentifier, address controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public returns(bool) { + function removeNativeController(address didIdentifier, address controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public { require(didIdentifier != controller, "Cannot remove default authority key"); - require(_doesNativeControllerExist(didIdentifier,controller), "Native controller does not exist"); + + // If an index is returned the controller exits + int index = _doesNativeControllerExist(didIdentifier,controller); + require(index >= 0, "Native controller does not exist"); DidState storage didState = didStates[didIdentifier]; - for(uint i=0; i < didState.nativeControllers.length; i++) { - if(didState.nativeControllers[i] == controller) { - // Remove native controller from array (not built into solidity so manipulating array to remove) - didState.nativeControllers[i] = didState.nativeControllers[didState.nativeControllers.length - 1]; - didState.nativeControllers.pop(); + // Remove native controller from array (not built into solidity so manipulating array to remove) + didState.nativeControllers[uint(index)] = didState.nativeControllers[didState.nativeControllers.length - 1]; + didState.nativeControllers.pop(); - emit ControllerRemoved(didIdentifier, abi.encodePacked(controller), true); - return true; - } - } - return false; + emit ControllerRemoved(didIdentifier, abi.encodePacked(controller), true); } function addExternalController(address didIdentifier, string calldata controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public { - require(!_doesExternalControllerExist(didIdentifier,controller), "External controller already exist"); + require(_doesExternalControllerExist(didIdentifier,controller) == -1, "External controller already exist"); didStates[didIdentifier].externalControllers.push(controller); emit ControllerAdded(didIdentifier, abi.encodePacked(controller), false); } - function removeExternalController(address didIdentifier, string calldata controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public returns(bool) { - require(_doesExternalControllerExist(didIdentifier,controller), "External controller does not exist"); + function removeExternalController(address didIdentifier, string calldata controller) onlyNonGenerativeDid(didIdentifier) onlyAuthorizedKeys(didIdentifier) public { + + // If an index is returned the controller exits + int index = _doesExternalControllerExist(didIdentifier,controller); + require(index >= 0, "External controller does not exist"); DidState storage didState = didStates[didIdentifier]; - for(uint i=0; i < didState.externalControllers.length; i++) { - if(_stringCompare(didState.externalControllers[i], controller)) { - // Remove native controller from array (not built into solidity so manipulating array to remove) - didState.externalControllers[i] = didState.externalControllers[didState.externalControllers.length - 1]; - didState.externalControllers.pop(); - emit ControllerRemoved(didIdentifier, abi.encodePacked(controller), false); - return true; - } - } - return false; + // Remove native controller from array (not built into solidity so manipulating array to remove) + didState.externalControllers[uint(index)] = didState.externalControllers[didState.externalControllers.length - 1]; + didState.externalControllers.pop(); + + emit ControllerRemoved(didIdentifier, abi.encodePacked(controller), false); } function _isKeyAuthority(address didIdentifier, address authority) internal view returns(bool) { @@ -308,24 +303,25 @@ contract DIDRegistry is IDidRegistry, Initializable, UUPSUpgradeable, OwnableUpg return false; } - function _doesNativeControllerExist(address didIdentifier, address controller) internal view returns(bool) { + function _doesNativeControllerExist(address didIdentifier, address controller) internal view returns(int index) { DidState storage didState = didStates[didIdentifier]; for(uint i=0; i < didState.nativeControllers.length; i++) { if(didState.nativeControllers[i] == controller) { - return true; + // Return index if controller found + return int(i); } } - return false; + return -1; } - function _doesExternalControllerExist(address didIdentifier, string calldata controller) internal view returns(bool) { + function _doesExternalControllerExist(address didIdentifier, string calldata controller) internal view returns(int index) { DidState storage didState = didStates[didIdentifier]; for(uint i=0; i < didState.externalControllers.length; i++) { if(_stringCompare(didState.externalControllers[i], controller)) { - return true; + return int(i); } } - return false; + return -1; } function _hasAuthorityVerificationMethod(address didIdentifier) internal view returns(bool) {