Skip to content

Commit

Permalink
Optimize lookup mapping in GlobalAllowList
Browse files Browse the repository at this point in the history
Closes #97
  • Loading branch information
cygnusv committed Aug 22, 2023
1 parent 7753fda commit 89eaf3d
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions contracts/contracts/coordination/GlobalAllowList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract GlobalAllowList is AccessControlDefaultAdminRules, IEncryptionAuthorize
using ECDSA for bytes32;

Coordinator public coordinator;
mapping(uint256 => mapping(address => bool)) public authorizations;
mapping(bytes32 => bool) authorizations;

constructor(
Coordinator _coordinator,
Expand All @@ -31,29 +31,39 @@ contract GlobalAllowList is AccessControlDefaultAdminRules, IEncryptionAuthorize
coordinator = _coordinator;
}

function lookupKey(uint32 ritualId, address encryptor) internal pure returns(bytes32) {
return keccak256(abi.encodePacked(ritualId, encryptor));
}

function isAddressAuthorized(uint32 ritualId, address encryptor) public view returns(bool) {
return authorizations[lookupKey(ritualId, encryptor)];
}

function isAuthorized(
uint32 ritualId,
bytes memory evidence,
bytes memory ciphertextHeader
) public view override returns(bool) {
) external view override returns(bool) {
bytes32 digest = keccak256(ciphertextHeader);
address recovered_address = digest.toEthSignedMessageHash().recover(evidence);
return authorizations[ritualId][recovered_address];
return isAddressAuthorized(ritualId, recovered_address);
}

function authorize(uint32 ritualId, address[] calldata addresses) public onlyAuthority(ritualId) {
require(coordinator.isRitualFinalized(ritualId),
"Only active rituals can add authorizations");
for (uint256 i=0; i < addresses.length; i++) {
authorizations[ritualId][addresses[i]] = true;
}
function authorize(uint32 ritualId, address[] calldata addresses) external onlyAuthority(ritualId) {
setAuthorizations(ritualId, addresses, true);
}

function deauthorize(uint32 ritualId, address[] calldata addresses) external onlyAuthority(ritualId) {
setAuthorizations(ritualId, addresses, false);
}

function deauthorize(uint32 ritualId, address[] calldata addresses) public onlyAuthority(ritualId) {
require(coordinator.isRitualFinalized(ritualId),
"Only active rituals can add authorizations");
function setAuthorizations(uint32 ritualId, address[] calldata addresses, bool value) internal {
require(
coordinator.isRitualFinalized(ritualId),
"Only active rituals can add authorizations"
);
for (uint256 i=0; i < addresses.length; i++) {
authorizations[ritualId][addresses[i]] = false;
authorizations[lookupKey(ritualId, addresses[i])] = value;
}
}
}

0 comments on commit 89eaf3d

Please sign in to comment.