Skip to content

Commit

Permalink
Merge pull request #21 from semiotic-ai/11-add-a-function-to-revoke-a…
Browse files Browse the repository at this point in the history
…uthorized-signers

feat(collateral-contract): adds ability to thaw and revoke authorized…
  • Loading branch information
ColePBryan authored Jul 12, 2023
2 parents 5818d32 + a3e0a48 commit f5c5122
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 49 deletions.
95 changes: 83 additions & 12 deletions src/Collateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ contract Collateral {
struct CollateralAccount {
uint256 balance; // Total collateral balance for a sender-receiver pair
uint256 amountThawing; // Amount of collateral currently being thawed
uint256 thawEndTimestamp; // Block number at which thawing period ends
uint256 thawEndTimestamp; // Block number at which thawing period ends (zero if not thawing)
}

struct SenderAuthorization {
address sender; // Sender the signer is authorized to sign for
uint256 thawEndTimestamp; // Block number at which thawing period ends (zero if not thawing)
}

// Stores how much collateral each sender has deposited for each receiver, as well as thawing information
mapping(address sender => mapping(address reciever => CollateralAccount collateralAccount))
private collateralAccounts;
// Map of authorized signers to which sender they are authorized to sign for
mapping(address signer => address sender) private authorizedSigners;
// Map of signer to authorized signer information
mapping(address signer => SenderAuthorization authorizedSigner)
private authorizedSigners;

// The ERC20 token used for collateral
IERC20 public immutable collateralToken;
Expand All @@ -48,7 +54,10 @@ contract Collateral {
AllocationIDTracker public immutable allocationIDTracker;

// The duration (in seconds) in which collateral funds are thawing before they can be withdrawn
uint256 public immutable thawingPeriod;
uint256 public immutable withdrawCollateralThawingPeriod;

// The duration (in seconds) in which a signer is thawing before they can be revoked
uint256 public immutable revokeSignerThawingPeriod;

/**
* @dev Emitted when collateral is deposited for a receiver.
Expand Down Expand Up @@ -80,6 +89,23 @@ contract Collateral {
uint256 thawEndTimestamp
);

/**
* @dev Emitted when a thaw request is made for authorized signer
*/
event ThawSigner(
address indexed sender,
address indexed authorizedSigner,
uint256 thawEndTimestamp
);

/**
* @dev Emitted when a authorized signer has been revoked
*/
event RevokeAuthorizedSigner(
address indexed sender,
address indexed authorizedSigner
);

/**
* @dev Emitted when thawed collateral is withdrawn by the sender.
*/
Expand All @@ -99,13 +125,15 @@ contract Collateral {
address staking_,
address tapVerifier_,
address allocationIDTracker_,
uint256 thawingPeriod_
uint256 withdrawCollateralThawingPeriod_,
uint256 revokeSignerThawingPeriod_
) {
collateralToken = IERC20(collateralToken_);
staking = IStaking(staking_);
tapVerifier = TAPVerifier(tapVerifier_);
allocationIDTracker = AllocationIDTracker(allocationIDTracker_);
thawingPeriod = thawingPeriod_;
withdrawCollateralThawingPeriod = withdrawCollateralThawingPeriod_;
revokeSignerThawingPeriod = revokeSignerThawingPeriod_;
}

/**
Expand Down Expand Up @@ -149,7 +177,9 @@ contract Collateral {
// Increase the amount being thawed
account.amountThawing = totalThawingRequested;
// Set when the thaw is complete (thawing period number of seconds after current timestamp)
account.thawEndTimestamp = block.timestamp + thawingPeriod;
account.thawEndTimestamp =
block.timestamp +
withdrawCollateralThawingPeriod;

emit Thaw(
msg.sender,
Expand Down Expand Up @@ -197,17 +227,58 @@ contract Collateral {
*/
function authorizeSigner(address signer, bytes calldata proof) external {
require(
authorizedSigners[signer] == address(0),
authorizedSigners[signer].sender == address(0),
"Signer already authorized"
);
require(
verifyAuthorizedSignerProof(proof, signer),
"Invalid signer proof"
);
authorizedSigners[signer] = msg.sender;
authorizedSigners[signer].sender = msg.sender;
authorizedSigners[signer].thawEndTimestamp = 0;
emit AuthorizeSigner(signer, msg.sender);
}

/**
* @dev Starts thawing a signer to be removed from the authorized signers list.
* @param signer Address of the signer to remove.
*/
function thawSigner(address signer) external {
require(
authorizedSigners[signer].sender == msg.sender,
"Signer not authorized for sender"
);
authorizedSigners[signer].thawEndTimestamp =
block.timestamp +
revokeSignerThawingPeriod;
emit ThawSigner(
authorizedSigners[signer].sender,
signer,
authorizedSigners[signer].thawEndTimestamp
);
}

/**
* @dev Revokes a signer from the authorized signers list if thawed.
* @param signer Address of the signer to remove.
*/
function revokeAuthorizedSigner(address signer) external {
require(
authorizedSigners[signer].sender == msg.sender,
"Signer not authorized for sender"
);
require(
authorizedSigners[signer].thawEndTimestamp != 0,
"Signer not thawing"
);
require(
authorizedSigners[signer].thawEndTimestamp <= block.timestamp,
"Signer still thawing"
);
delete authorizedSigners[signer];
emit RevokeAuthorizedSigner(authorizedSigners[signer].sender, signer);
}

/**
* @dev Redeems collateral for a receiver using a signed RAV.
* @param signedRAV Signed RAV containing the receiver and collateral amount.
Expand All @@ -224,11 +295,11 @@ contract Collateral {
) external {
address signer = tapVerifier.recoverRAVSigner(signedRAV);
require(
authorizedSigners[signer] != address(0),
authorizedSigners[signer].sender != address(0),
"Signer not authorized"
);

address sender = authorizedSigners[signer];
address sender = authorizedSigners[signer].sender;
address receiver = msg.sender;
uint256 amount = signedRAV.rav.valueAggregate;
address allocationId = signedRAV.rav.allocationId;
Expand Down Expand Up @@ -285,7 +356,7 @@ contract Collateral {
address signer,
address receiver
) external view returns (CollateralAccount memory) {
return collateralAccounts[authorizedSigners[signer]][receiver];
return collateralAccounts[authorizedSigners[signer].sender][receiver];
}

/**
Expand Down
Loading

0 comments on commit f5c5122

Please sign in to comment.