Skip to content

Commit 8faeb97

Browse files
Add multisig swap owner template (#284)
* Add multisig swap owner template * Code review comments
1 parent fe21b92 commit 8faeb97

File tree

8 files changed

+353
-0
lines changed

8 files changed

+353
-0
lines changed

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ DEPLOY_DIR = $(network)/$(shell date +'%Y-%m-%d')-deploy
33
INCIDENT_DIR = $(network)/$(shell date +'%Y-%m-%d')-$(incident)
44
GAS_INCREASE_DIR = $(network)/$(shell date +'%Y-%m-%d')-increase-gas-limit
55
FAULT_PROOF_UPGRADE_DIR = $(network)/$(shell date +'%Y-%m-%d')-upgrade-fault-proofs
6+
SAFE_MANAGEMENT_DIR = $(network)/$(shell date +'%Y-%m-%d')-safe-swap-owner
67
TEMPLATE_GENERIC = setup-templates/template-generic
78
TEMPLATE_DEPLOY = setup-templates/template-deploy
89
TEMPLATE_INCIDENT = setup-templates/template-incident
910
TEMPLATE_GAS_INCREASE = setup-templates/template-gas-increase
1011
TEMPLATE_UPGRADE_FAULT_PROOFS = setup-templates/template-upgrade-fault-proofs
12+
TEMPLATE_SAFE_MANAGEMENT = setup-templates/template-safe-management
1113

1214
ifndef $(GOPATH)
1315
GOPATH=$(shell go env GOPATH)
@@ -46,6 +48,11 @@ setup-gas-increase:
4648
setup-upgrade-fault-proofs:
4749
cp -r $(TEMPLATE_UPGRADE_FAULT_PROOFS) $(FAULT_PROOF_UPGRADE_DIR)
4850

51+
# Run `make setup-safe-management network=<network>`
52+
setup-safe-management:
53+
rm -rf $(TEMPLATE_SAFE_MANAGEMENT)/cache $(TEMPLATE_SAFE_MANAGEMENT)/lib $(TEMPLATE_SAFE_MANAGEMENT)/out
54+
cp -r $(TEMPLATE_SAFE_MANAGEMENT) $(SAFE_MANAGEMENT_DIR)
55+
4956
##
5057
# Solidity Setup
5158
##

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ This template is used to upgrade the fault proof contracts. This is commonly don
111111
1. Check in the task when it's ready to sign and collect signatures from signers
112112
1. Once executed, check in the records files and mark the task `DONE` in the README.
113113

114+
## Using the swap owner template
115+
116+
This template is used to perform ownership management on a Gnosis Safe multisig, specifically it can swap owners on the multisig.
117+
118+
1. Ensure you have followed the instructions above in `setup`.
119+
1. Run `make setup-safe-management network=<network>` and go to the folder that was created by this command.
120+
1. Specify the commit of [Optimism code](https://github.com/ethereum-optimism/optimism) and [Base contracts code](https://github.com/base-org/contracts) you intend to use in the `.env` file.
121+
1. Run `make deps`.
122+
1. Specify the `OWNER_SAFE`, which is the safe multisig where an owner will be replaced, the `OLD_SIGNER` (current owner) to remove, and the `NEW_SIGNER` (new owner) to be added in the `.env` file.
123+
1. Build the contracts with `forge build`.
124+
1. Simulate the task with `make sign` and update the generic validations in `VALIDATION.md` with the real values.
125+
1. Check in the task when it's ready to sign and request the facilitators to collect signatures from signers.
126+
1. Once executed, check in the records files and mark the task `DONE` in the README.
127+
114128
## Using the generic template
115129

116130
This template can be used to do contract calls, upgrades, or one-off deployments.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
OP_COMMIT=
2+
BASE_CONTRACTS_COMMIT=
3+
4+
OWNER_SAFE=
5+
OLD_SIGNER=
6+
NEW_SIGNER=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include ../../Makefile
2+
include ../.env
3+
include .env
4+
5+
ifndef LEDGER_ACCOUNT
6+
override LEDGER_ACCOUNT = 0
7+
endif
8+
9+
.PHONY: sign
10+
sign:
11+
$(GOPATH)/bin/eip712sign --ledger --hd-paths "m/44'/60'/$(LEDGER_ACCOUNT)'/0/0" -- \
12+
forge script --rpc-url $(L1_RPC_URL) SwapOwner \
13+
--sig "sign()"
14+
15+
.PHONY: execute
16+
execute:
17+
forge script --rpc-url $(L1_RPC_URL) SwapOwner \
18+
--sig "run(bytes)" $(SIGNATURES) --ledger --hd-paths "m/44'/60'/$(LEDGER_ACCOUNT)'/0/0" --broadcast
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Swap Owner on Gnosis Safe
2+
3+
Status: PENDING
4+
5+
## Description
6+
7+
This task contains a single script that can be used to swap an owner in a Gnosis Safe.
8+
9+
## Procedure
10+
11+
### 1. Update repo:
12+
13+
```bash
14+
cd contract-deployments
15+
git pull
16+
make setup-safe-management network=<network>
17+
cd <network>/<date>safe-swap-owner
18+
make deps
19+
```
20+
21+
### 2. Setup Ledger
22+
23+
Your Ledger needs to be connected and unlocked. The Ethereum
24+
application needs to be opened on Ledger with the message "Application
25+
is ready".
26+
27+
### 3. Simulate, Validate, and Sign
28+
29+
#### 3.1. Simulate and validate the transaction
30+
31+
Make sure your ledger is still unlocked and run the following.
32+
33+
```bash
34+
make sign
35+
```
36+
37+
You will see a "Simulation link" from the output.
38+
39+
Paste this URL in your browser. A prompt may ask you to choose a
40+
project, any project will do. You can create one if necessary.
41+
42+
Click "Simulate Transaction".
43+
44+
We will be performing 3 validations and extract the domain hash and
45+
message hash to approve on your Ledger:
46+
47+
1. Validate integrity of the simulation.
48+
2. Validate correctness of the state diff.
49+
3. Validate and extract domain hash and message hash to approve.
50+
51+
##### 3.1.1. Validate integrity of the simulation.
52+
53+
Make sure you are on the "Overview" tab of the tenderly simulation, to
54+
validate integrity of the simulation, we need to check the following:
55+
56+
1. "Network": Check the network is `<network>`.
57+
2. "Timestamp": Check the simulation is performed on a block with a
58+
recent timestamp (i.e. close to when you run the script).
59+
3. "Sender": Check the address shown is your signer account.
60+
61+
##### 3.1.2. Validate correctness of the state diff.
62+
63+
Now click on the "State" tab, and refer to the [State Validations](./VALIDATION.md) instructions for the transaction you are signing.
64+
Once complete return to this document to complete the signing.
65+
66+
### 4. Extract the domain hash and the message hash to approve.
67+
68+
Now that we have verified the transaction performs the right
69+
operation, we need to extract the domain hash and the message hash to
70+
approve.
71+
72+
Go back to the "Overview" tab, and find the
73+
`GnosisSafe.checkSignatures` call. This call's `data` parameter
74+
contains both the domain hash and the message hash that will show up
75+
in your Ledger.
76+
77+
It will be a concatenation of `0x1901`, the domain hash, and the
78+
message hash: `0x1901[domain hash][message hash]`.
79+
80+
Note down this value. You will need to compare it with the ones
81+
displayed on the Ledger screen at signing.
82+
83+
Once the validations are done, it's time to actually sign the
84+
transaction.
85+
86+
> [!WARNING]
87+
> This is the most security critical part of the playbook: make sure the
88+
> domain hash and message hash in the following three places match:
89+
>
90+
> 1. On your Ledger screen.
91+
> 2. In the terminal output.
92+
> 3. In the Tenderly simulation. You should use the same Tenderly
93+
> simulation as the one you used to verify the state diffs, instead
94+
> of opening the new one printed in the console.
95+
>
96+
97+
After verification, sign the transaction. You will see the `Data`,
98+
`Signer` and `Signature` printed in the console. Format should be
99+
something like this:
100+
101+
```shell
102+
Data: <DATA>
103+
Signer: <ADDRESS>
104+
Signature: <SIGNATURE>
105+
```
106+
107+
Double check the signer address is the right one.
108+
109+
#### 4.1. Send the output to Facilitator(s)
110+
111+
Nothing has occurred onchain - these are offchain signatures which
112+
will be collected by Facilitators for execution. Execution can occur
113+
by anyone once a threshold of signatures are collected, so a
114+
Facilitator will do the final execution for convenience.
115+
116+
Share the `Data`, `Signer` and `Signature` with the Facilitator, and
117+
congrats, you are done!
118+
119+
### [For Facilitator ONLY] How to execute
120+
121+
#### Execute the transaction
122+
123+
1. Collect outputs from all participating signers.
124+
1. Concatenate all signatures and export it as the `SIGNATURES`
125+
environment variable, i.e. `export
126+
SIGNATURES="[SIGNATURE1][SIGNATURE2]..."`.
127+
1. Run the `make execute` command as described below to execute the transaction.
128+
129+
For example, if the quorum is 2 and you get the following outputs:
130+
131+
```shell
132+
Data: 0xDEADBEEF
133+
Signer: 0xC0FFEE01
134+
Signature: AAAA
135+
```
136+
137+
```shell
138+
Data: 0xDEADBEEF
139+
Signer: 0xC0FFEE02
140+
Signature: BBBB
141+
```
142+
143+
Then you should run:
144+
145+
Coinbase facilitator:
146+
147+
```bash
148+
SIGNATURES=AAAABBBB make execute
149+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Validation
2+
3+
This document can be used to validate the state diff resulting from the execution of the swap owner transaction.
4+
5+
For each contract listed in the state diff, please verify that no contracts or state changes shown in the Tenderly diff are missing from this document. Additionally, please verify that for each contract:
6+
7+
- The following state changes (and no others) are made to that contract. This validates that no unexpected state changes occur.
8+
- All addresses (in section headers and storage values) match the provided name, using the Etherscan links provided. This validates the bytecode deployed at the addresses contains the correct logic.
9+
- All key values match the semantic meaning provided, which can be validated using the storage layout links provided.
10+
11+
## State Changes
12+
13+
### `<safe_address>` (Base `GnosisSafeProxy`)
14+
15+
- **Key**: `owners[newOwner]` <br/>
16+
**Before**: `0x0000000000000000000000000000000000000000000000000000000000000000` <br/>
17+
**After**: `Next address in the list returned by getOwners()` <br/>
18+
**Meaning**: Sets the address value at the mapping key `owners[newOwner]` to the next address in the list returned by `getOwners()`. This is the first step required to replace the `oldOwner` address in the linked list data structure of owners. <br/>
19+
**Verify**: You can verify the key derivation by running `cast index address <newOwner> 2` in your terminal. See the following section for an explanation of the storage and value calculations.
20+
- **Key**: `owners[prevOwner]` <br/>
21+
**Before**: `oldOwner address` <br/>
22+
**After**: `newOwner address` <br/>
23+
**Meaning**: Points the address value at mapping key `owners[prevOwner]` to the `newOwner` address. This is the second step required to replace the `oldOwner` address in the linked list data structure of owners. <br/>
24+
**Verify**: You can verify the key derivation by running `cast index address <prevOwner> 2` in your terminal. See the following section for an explanation of the storage and value calculations.
25+
- **Key**: `owners[oldOwner]` <br/>
26+
**Before**: `Next address in the list returned by getOwners()` <br/>
27+
**After**: `0x0000000000000000000000000000000000000000000000000000000000000000` <br/>
28+
**Meaning**: Clears the address value at the mapping key `owners[oldOwner]`. This removes the final reference to the `oldOwner` from the `owners` linked list. <br/>
29+
**Verify**: You can verify the key derivation by running `cast index address <oldOwner> 2` in your terminal. See the following section for an explanation of the storage and value calculations.
30+
- **Key**: `0x0000000000000000000000000000000000000000000000000000000000000005` <br/>
31+
**Before**: `Current nonce value (in hexadecimal)` <br/>
32+
**After**: `Current nonce value + 1 (in hexadecimal)` <br/>
33+
**Meaning**: Increments the `nonce` value of the Gnosis Safe. <br/>
34+
**Verify**: You can verify the value by running `cast storage <safe_address> 5 -r <rpc_url>` in your terminal. This value represents the _current_ nonce value.
35+
36+
### `SwapOwner` Storage Calculations
37+
38+
The [`swapOwner`](https://github.com/safe-global/safe-smart-account/blob/8823fa3e44936e2aecf23bb97662eb0ffeff2f93/contracts/base/OwnerManager.sol#L94) function in the Gnosis Safe implementation will perform [three storage changes](https://github.com/safe-global/safe-smart-account/blob/8823fa3e44936e2aecf23bb97662eb0ffeff2f93/contracts/base/OwnerManager.sol#L106-L108):
39+
40+
- Point the `newOwner` address to the owner address that was previously pointed to by the `oldOwner` that is being removed.
41+
- Point the `prevOwner` address to the `newOwner` address.
42+
- Remove the pointer value stored at the mapping of the `oldOwner` address.
43+
44+
These changes are needed on account of the data structure that holds the owners in the Gnosis Safe implementation being a singly-linked list. To calculate the expected storage locations of these mapping values, we can perform the following:
45+
46+
#### Calculating `prevOwner`
47+
48+
The `prevOwner` is identified by the script, but can be manually checked by running the cast command:
49+
50+
```sh
51+
cast call <safe_address> "getOwners()(address[])" -r <rpc_url>
52+
53+
[0x6CD3850756b7894774Ab715D136F9dD02837De50, 0x3cd692eCE8b6573A2220ae00d0dEb98f0DfFA9a1, 0x5FbEFA105bbd53b43bf537Cbc5cD30804Dd0c993, 0x3Dad2200849925Bb46d9bF05aFa5f7F213F4c18E, 0xB011a32ED8b4F70D9943A2199F539bbeCd7b62F7, 0xf9e320f3dA12E68af219d9E2A490Dd649f6B177c]
54+
```
55+
56+
The order that the owner addresses are returned in indicates who the `prevOwner` and next owner values of the address to remove is. If the owner to remove is the 0th value, its `prevOwner` would be the special sentinel node value [`SENTINEL_OWNERS`](https://github.com/safe-global/safe-smart-account/blob/f9cc387f72640eb2c1d6ae8abe9d6ff25ca1ed3b/contracts/base/OwnerManager.sol#L17). Otherwise, if the owner to remove is the last value in the array, its next owner address would be the special sentinel node value [`SENTINEL_OWNERS`](https://github.com/safe-global/safe-smart-account/blob/f9cc387f72640eb2c1d6ae8abe9d6ff25ca1ed3b/contracts/base/OwnerManager.sol#L17).
57+
58+
#### Calculating storage locations and values
59+
60+
With the order of the owners identified, we can calculate the expected storage mapping locations and their values. For the first change, the expected storage slot and value will be the following:
61+
62+
**Storage Slot**: `cast index address <newOwner> 2`
63+
64+
**Value**: The address immediately following `oldOwner` in the `getOwners` list, or the `SENTINEL_OWNERS` special value if the address is the last in the array.
65+
66+
For the second change, it will be:
67+
68+
**Storage Slot**: `cast index address <prevOwner> 2`
69+
70+
**Value**: `newOwner` address
71+
72+
For the final storage change, it will be:
73+
74+
**Storage Slot**: `cast index address <oldOwner> 2`
75+
76+
**Value**: `address(0)`
77+
78+
Note that for all the above storage calculations, we used storage slot 2 as that is the location of the `owners` mapping in the Gnosis Safe storage layout. This can be confirmed with the following command: `cast storage <safe_address> -r <rpc_url> -e <etherscan_api_key>`. Also note that while the storage changes may not appear in the same order in the Tenderly simulation, there should still be 3 storage changes related to the `owner` linked list and one change for the `nonce` value on the `GnosisSafeProxy`. There should be no additional changes to the proxy besides these ones!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[profile.default]
2+
src = 'src'
3+
out = 'out'
4+
libs = ['lib']
5+
broadcast = 'records'
6+
fs_permissions = [ {access = "read-write", path = "./"} ]
7+
optimizer = true
8+
optimizer_runs = 999999
9+
solc_version = "0.8.15"
10+
via-ir = false
11+
remappings = [
12+
'@eth-optimism-bedrock/=lib/optimism/packages/contracts-bedrock/',
13+
'@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts',
14+
'@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts',
15+
'@rari-capital/solmate/=lib/solmate/',
16+
'@base-contracts/=lib/base-contracts',
17+
'@solady/=lib/solady/src/'
18+
]
19+
20+
# See more config options https://github.com/foundry-rs/foundry/tree/master/config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.15;
3+
4+
import "@base-contracts/script/universal/MultisigBuilder.sol";
5+
import {IGnosisSafe} from "@base-contracts/script/universal/IGnosisSafe.sol";
6+
7+
contract SwapOwner is MultisigBuilder {
8+
address internal OWNER_SAFE = vm.envAddress("OWNER_SAFE");
9+
address internal OLD_SIGNER = vm.envAddress("OLD_SIGNER");
10+
address internal NEW_SIGNER = vm.envAddress("NEW_SIGNER");
11+
address internal constant SENTINEL_OWNERS = address(0x1);
12+
13+
address[] internal safeOwners; // The list of all owners of the safe
14+
address internal prevOwnerLinked; // The previous owner in Safe's linked list of owners
15+
16+
function setUp() public {
17+
safeOwners = IGnosisSafe(OWNER_SAFE).getOwners();
18+
_precheck();
19+
20+
// We need to locate the previous owner in the linked list of owners to properly swap out the target signer
21+
for (uint256 i = 0; i < safeOwners.length; i++) {
22+
if (safeOwners[i] == OLD_SIGNER) {
23+
// There is a sentinel node as the head of the linked list, so we need to handle the first owner separately
24+
if (i == 0) {
25+
prevOwnerLinked = SENTINEL_OWNERS;
26+
} else {
27+
prevOwnerLinked = safeOwners[i - 1];
28+
}
29+
break;
30+
}
31+
}
32+
}
33+
34+
function _precheck() internal view {
35+
// Sanity checks on the current owner state of the safe
36+
require(IGnosisSafe(OWNER_SAFE).isOwner(OLD_SIGNER), "Signer to swap is not an owner");
37+
require(!IGnosisSafe(OWNER_SAFE).isOwner(NEW_SIGNER), "New signer is already an owner");
38+
}
39+
40+
function _buildCalls() internal view override returns (IMulticall3.Call3[] memory) {
41+
IMulticall3.Call3[] memory calls = new IMulticall3.Call3[](1);
42+
43+
// While more roundabout then simply calling execTransaction with a swapOwner call, this still works and lets us use our existing tooling
44+
calls[0] = IMulticall3.Call3({
45+
target: OWNER_SAFE,
46+
allowFailure: false,
47+
callData: abi.encodeCall(IGnosisSafe.swapOwner, (prevOwnerLinked, OLD_SIGNER, NEW_SIGNER))
48+
});
49+
50+
return calls;
51+
}
52+
53+
function _postCheck(Vm.AccountAccess[] memory, Simulation.Payload memory) internal view override {
54+
require(IGnosisSafe(OWNER_SAFE).isOwner(NEW_SIGNER), "New signer was not added as an owner");
55+
require(!IGnosisSafe(OWNER_SAFE).isOwner(OLD_SIGNER), "Old signer was not removed as an owner");
56+
}
57+
58+
function _ownerSafe() internal view override returns (address) {
59+
return OWNER_SAFE;
60+
}
61+
}

0 commit comments

Comments
 (0)