Skip to content

Commit

Permalink
Fyeo did bnb 01 Fix (#19)
Browse files Browse the repository at this point in the history
Update native controller check to return index of controller if found. This approach saves gas due to only having to use 1 loop instead of 2 when searching for existing controllers

---------

Authored-by: Robert Leonard 
Reviewd-by: Martin Riedel
  • Loading branch information
Robert-H-Leonard authored Oct 3, 2023
1 parent 4fee341 commit c37fb27
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/DidRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c37fb27

Please sign in to comment.