Skip to content

Commit

Permalink
coupon update delegate key (#604)
Browse files Browse the repository at this point in the history
* coupon update delegate key

* update date

* update comments
  • Loading branch information
adridadou authored Oct 17, 2024
1 parent e8336eb commit 338fba2
Show file tree
Hide file tree
Showing 5 changed files with 539 additions and 0 deletions.
20 changes: 20 additions & 0 deletions configs/contracts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,26 @@ export const contracts: Array<ContractConfig> = [
],
],
},
{
id: adaptersIdsMap.COUPON_UPDATE_DELEGATE_KEY_ADAPTER,
name: "CouponUpdateDelegateKeyContract",
alias: "couponUpdateDelegateKey",
path: "../../contracts/adapters/CouponUpdateDelegateKeyContract",
enabled: true,
version: "1.0.0",
type: ContractType.Adapter,
acls: {
dao: [daoAccessFlagsMap.UPDATE_DELEGATE_KEY],
extensions: {},
},
daoConfigs: [
//config to mint coupons
[
"daoAddress",
"couponCreatorAddress"
],
],
},
{
id: adaptersIdsMap.KYC_ONBOARDING_ADAPTER,
name: "KycOnboardingContract",
Expand Down
151 changes: 151 additions & 0 deletions contracts/adapters/CouponUpdateDelegateKey.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
pragma solidity ^0.8.0;

// SPDX-License-Identifier: MIT

import "../core/DaoRegistry.sol";
import "../extensions/bank/Bank.sol";
import "../guards/AdapterGuard.sol";
import "./modifiers/Reimbursable.sol";
import "../utils/Signatures.sol";
import "../helpers/DaoHelper.sol";

/**
MIT License
Copyright (c) 2024 Openlaw
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

contract CouponUpdateDelegateKeyContract is
Reimbursable,
AdapterGuard,
Signatures
{
struct Coupon {
address authorizedMember;
address newDelegateKey;
uint256 nonce;
}

using SafeERC20 for IERC20;

string public constant COUPON_MESSAGE_TYPE =
"Message(address authorizedMember,address newDelegateKey,uint256 nonce)";

bytes32 public constant COUPON_MESSAGE_TYPEHASH =
keccak256(abi.encodePacked(COUPON_MESSAGE_TYPE));

bytes32 constant SignerAddressConfig =
keccak256("coupon-update-delegate-key.signerAddress");

mapping(address => mapping(uint256 => uint256)) private _flags;

event CouponRedeemed(
address daoAddress,
uint256 nonce,
address authorizedMember,
address newDelegateKey
);

/**
* @notice Configures the Adapter with the coupon signer address.
* @param signerAddress the address of the coupon signer
*/
function configureDao(
DaoRegistry dao,
address signerAddress
) external onlyAdapter(dao) {
dao.setAddressConfiguration(SignerAddressConfig, signerAddress);
}

/**
* @notice Hashes the provided coupon as an ERC712 hash.
* @param dao is the DAO instance to be configured
* @param coupon is the coupon to hash
*/
function hashCouponMessage(
DaoRegistry dao,
Coupon memory coupon
) public view returns (bytes32) {
bytes32 message = keccak256(
abi.encode(
COUPON_MESSAGE_TYPEHASH,
coupon.authorizedMember,
coupon.newDelegateKey,
coupon.nonce
)
);

return hashMessage(dao, address(this), message);
}

/**
* @notice Redeems a coupon to update the delegate key
* @param dao is the DAO instance to be configured
* @param authorizedMember is the member that this coupon authorized to update the delegate key
* @param newDelegateKey is the new delegate key for the member
* @param nonce is a unique identifier for this coupon request
* @param signature is message signature for verification
*/
// function is protected against reentrancy attack with the reentrancyGuard(dao)
// slither-disable-next-line reentrancy-benign
function redeemCoupon(
DaoRegistry dao,
address authorizedMember,
address newDelegateKey,
uint256 nonce,
bytes memory signature
) external reimbursable(dao) {
{
uint256 currentFlag = _flags[address(dao)][nonce / 256];
_flags[address(dao)][nonce / 256] = DaoHelper.setFlag(
currentFlag,
nonce % 256,
true
);

require(
DaoHelper.getFlag(currentFlag, nonce % 256) == false,
"coupon already redeemed"
);
}

Coupon memory coupon = Coupon(authorizedMember, newDelegateKey, nonce);
bytes32 hash = hashCouponMessage(dao, coupon);

require(
SignatureChecker.isValidSignatureNow(
dao.getAddressConfiguration(SignerAddressConfig),
hash,
signature
),
"invalid sig"
);
dao.updateDelegateKey(authorizedMember, newDelegateKey);

//slither-disable-next-line reentrancy-events
emit CouponRedeemed(
address(dao),
nonce,
authorizedMember,
newDelegateKey
);
}
}
Loading

0 comments on commit 338fba2

Please sign in to comment.