Skip to content

Commit fbb1e8f

Browse files
authored
Add cross-chain delegation (#169)
* add delegation * clean up * rm script * rm empty script
1 parent e2cef6c commit fbb1e8f

15 files changed

+655
-332
lines changed

README.md

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -94,84 +94,44 @@ The following functions are `onlyOwner`, and since the NFT contract ownership is
9494

9595
### Crosschain
9696

97-
Make sure the main account has sufficient balance on OP Sepolia and Arbitrum Sepolia:
97+
Make sure the main account, Bob and Alice have sufficient balance on OP Sepolia and Arbitrum Sepolia:
9898

9999
```
100-
pnpm bal
101-
```
102-
103-
Deploy:
104-
105-
```
106-
pnpm deploy:all
107-
```
108-
109-
It will:
110-
111-
- Deploy to OP Sepolia
112-
- Deploy to Arbitrum Sepolia
113-
114-
Add a member (mint):
100+
# Deploy to OP Sepolia and Arbitrum Sepolia
101+
pnpm deploy:op-sepolia
102+
pnpm deploy:arbitrum-sepolia
115103
116-
```
117-
./scripts/mint.sh
118-
```
119-
120-
It will:
121-
122-
- Submit a proposal and add a member on OP Sepolia
123-
- Generate a membership proof on OP Sepolia
124-
- Claim that proof on Arbitrum Sepolia
104+
# Add a new member
105+
npx hardhat run scripts/propose.ts --network op-sepolia
106+
npx hardhat run scripts/verify-proof.ts --network op-sepolia
107+
npx hardhat run scripts/claim-membership.ts --network arbitrum-sepolia
125108
126-
Ban a member (burn):
109+
# Ban a member
110+
npx hardhat run scripts/propose-burn.ts --network op-sepolia
111+
npx hardhat run scripts/verify-burn-proof.ts --network op-sepolia
112+
npx hardhat run scripts/claim-burn.ts --network arbitrum-sepolia
127113
128-
```
129-
./scripts/burn.sh
130-
```
114+
# Edit 1 membership NFT metadata
115+
npx hardhat run scripts/propose-metadata.ts --network op-sepolia
116+
npx hardhat run scripts/verify-metadata-proof.ts --network op-sepolia
117+
npx hardhat run scripts/claim-metadata.ts --network arbitrum-sepolia
131118
132-
It will:
119+
# Edit the manifesto
120+
npx hardhat run scripts/propose-manifesto.ts --network op-sepolia
121+
npx hardhat run scripts/verify-manifesto-proof.ts --network op-sepolia
122+
npx hardhat run scripts/claim-manifesto.ts --network arbitrum-sepolia
133123
134-
- Submit a proposal and ban a member on OP Sepolia
135-
- Generate a burn proof on OP Sepolia
136-
- Claim that proof on Arbitrum Sepolia
124+
# Change 1 voting parameter
125+
npx hardhat run scripts/propose-voting-delay.ts --network op-sepolia
126+
npx hardhat run scripts/verify-voting-delay-proof.ts --network op-sepolia
127+
npx hardhat run scripts/claim-voting-delay.ts --network arbitrum-sepolia
137128
138-
Edit membership NFT metadata:
139-
140-
```
141-
./scripts/metadata.sh
129+
# Change delegation
130+
npx hardhat run scripts/propose-delegation.ts --network op-sepolia
131+
npx hardhat run scripts/verify-delegation-proof.ts --network op-sepolia
132+
npx hardhat run scripts/claim-delegation.ts --network arbitrum-sepolia
142133
```
143134

144-
It will:
145-
146-
- Submit a proposal edit the NFT metadata of tokenId 1 on OP Sepolia
147-
- Generate a metadata proof on OP Sepolia
148-
- Claim that proof on Arbitrum Sepolia
149-
150-
Edit the manifesto:
151-
152-
```
153-
./scripts/manifesto.sh
154-
```
155-
156-
It will:
157-
158-
- Submit a proposal to edit the manifesto on OP Sepolia
159-
- Generate a manifesto proof on OP Sepolia
160-
- Claim that proof on Arbitrum Sepolia
161-
162-
Change the voting delay:
163-
164-
```
165-
./scripts/voting-delay.sh
166-
```
167-
168-
It will:
169-
170-
- Submit a proposal to change the voting delay on OP Sepolia
171-
- Generate a voting delay proof on OP Sepolia
172-
- Claim that proof on Arbitrum Sepolia
173-
174-
175135
## Core Dependencies
176136

177137
- Node [v20.9.0](https://nodejs.org/uk/blog/release/v20.9.0/)

contracts/variants/crosschain/NFT.sol

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ contract NFT is
3434
/// @notice Tracks token existence on each chain
3535
mapping(uint256 => bool) public existsOnChain;
3636

37+
mapping(address => address) public crosschainDelegates;
38+
3739
/// @notice Operation types for cross-chain message verification
3840
/// @dev Used to differentiate between different types of cross-chain operations
3941
enum OperationType {
40-
MINT, // Mint new token
41-
BURN, // Burn existing token
42-
SET_METADATA // Update token metadata
42+
MINT,
43+
BURN,
44+
SET_METADATA,
45+
SET_DELEGATION
4346
}
4447

4548
/**
@@ -68,6 +71,13 @@ contract NFT is
6871
*/
6972
event MetadataUpdated(uint256 indexed tokenId, string newUri);
7073

74+
event DelegationUpdated(address indexed delegator, address indexed delegate);
75+
event CrosschainDelegationClaimed(
76+
address indexed delegator,
77+
address indexed delegate,
78+
address indexed claimer
79+
);
80+
7181
/**
7282
* @notice Restricts operations to the home chain
7383
* @dev Used to ensure certain operations only occur on the chain where the contract was originally deployed
@@ -189,6 +199,32 @@ contract NFT is
189199
return abi.encode(tokenId, uri, digest);
190200
}
191201

202+
function delegate(address delegatee) public virtual override onlyHomeChain {
203+
super.delegate(delegatee);
204+
crosschainDelegates[msg.sender] = delegatee;
205+
emit DelegationUpdated(msg.sender, delegatee);
206+
}
207+
208+
function generateDelegationProof(
209+
address delegator,
210+
address delegatee
211+
) external view returns (bytes memory) {
212+
require(block.chainid == home, "Proofs can only be generated on home chain");
213+
require(crosschainDelegates[delegator] == delegatee, "Invalid delegation state");
214+
215+
bytes32 message = keccak256(
216+
abi.encodePacked(
217+
address(this),
218+
uint8(OperationType.SET_DELEGATION),
219+
delegator,
220+
delegatee
221+
)
222+
);
223+
bytes32 digest = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", message));
224+
225+
return abi.encode(delegator, delegatee, digest);
226+
}
227+
192228
/**
193229
* @notice Claims a membership on a foreign chain
194230
* @dev Verifies proof and mints token on foreign chain
@@ -260,6 +296,30 @@ contract NFT is
260296
emit MetadataUpdated(tokenId, uri);
261297
}
262298

299+
function claimDelegation(bytes memory proof) external {
300+
(address delegator, address delegatee, bytes32 digest) = abi.decode(
301+
proof,
302+
(address, address, bytes32)
303+
);
304+
305+
bytes32 message = keccak256(
306+
abi.encodePacked(
307+
address(this),
308+
uint8(OperationType.SET_DELEGATION),
309+
delegator,
310+
delegatee
311+
)
312+
);
313+
bytes32 expectedDigest = keccak256(
314+
abi.encodePacked("\x19Ethereum Signed Message:\n32", message)
315+
);
316+
require(digest == expectedDigest, "Invalid delegation proof");
317+
318+
_delegate(delegator, delegatee);
319+
crosschainDelegates[delegator] = delegatee;
320+
emit CrosschainDelegationClaimed(delegator, delegatee, msg.sender);
321+
}
322+
263323
// Internal Functions
264324

265325
/**

deploy/deploy-crosschain-gov.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
1919
const { deployments, getNamedAccounts } = hre
2020
const { deterministic } = deployments
2121
const { deployer } = await getNamedAccounts()
22-
const salt = hre.ethers.id("Dec-12-v2")
22+
const salt = hre.ethers.id("Dec-17-v1")
2323
const homeChainId = 11155420
2424

2525
function wait(ms: number): Promise<void> {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"scripts": {
77
"compile": "hardhat compile",
88
"test": "hardhat test",
9-
"test:all": "./scripts/deploy.sh && ./scripts/mint.sh && ./scripts/burn.sh && ./scripts/metadata.sh && ./scripts/manifesto.sh && ./scripts/voting-delay.sh",
109
"test:crosschain": "hardhat test test/Gov-crosschain.ts",
1110
"deploy:optimism": "hardhat deploy --network optimism --reset",
1211
"deploy:base": "hardhat deploy --network base --reset",
@@ -16,6 +15,7 @@
1615
"deploy:base-sepolia": "hardhat deploy --network base-sepolia --reset",
1716
"crosschain:sepolia": "hardhat deploy --network sepolia --tags CrosschainGov --reset",
1817
"crosschain:op-sepolia": "hardhat deploy --network op-sepolia --tags CrosschainGov --reset",
18+
"crosschain:arbitrum-sepolia": "hardhat deploy --network arbitrum-sepolia --tags CrosschainGov --reset",
1919
"deploy:all": "./scripts/deploy.sh",
2020
"bal": "npx hardhat run scripts/check-my-balance.ts",
2121
"verify:setup": "hardhat run scripts/verify-crosschain-setup.ts",

scripts/burn.sh

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)