slotdiff 0.1.0-alpha.1 → 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -9
- package/README.md +196 -105
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +22 -0
- package/dist/bin.js.map +1 -0
- package/dist/build-info.d.ts +45 -0
- package/dist/build-info.d.ts.map +1 -0
- package/dist/build-info.js +319 -0
- package/dist/build-info.js.map +1 -0
- package/dist/cli.d.ts +25 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +363 -0
- package/dist/cli.js.map +1 -0
- package/dist/erc7201.d.ts +72 -0
- package/dist/erc7201.d.ts.map +1 -0
- package/dist/erc7201.js +340 -0
- package/dist/erc7201.js.map +1 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +8 -0
- package/dist/errors.js.map +1 -1
- package/dist/explorer.js +2 -2
- package/dist/explorer.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/reconstruct.d.ts +51 -3
- package/dist/reconstruct.d.ts.map +1 -1
- package/dist/reconstruct.js +37 -8
- package/dist/reconstruct.js.map +1 -1
- package/dist/report.d.ts +26 -0
- package/dist/report.d.ts.map +1 -0
- package/dist/report.js +109 -0
- package/dist/report.js.map +1 -0
- package/dist/resolve.d.ts +11 -0
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +11 -1
- package/dist/resolve.js.map +1 -1
- package/package.json +6 -3
- package/src/bin.ts +25 -0
- package/src/build-info.ts +405 -0
- package/src/cli.ts +423 -0
- package/src/erc7201.ts +412 -0
- package/src/errors.ts +9 -0
- package/src/explorer.ts +2 -2
- package/src/index.ts +18 -1
- package/src/reconstruct.ts +111 -10
- package/src/report.ts +155 -0
- package/src/resolve.ts +17 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0-alpha.1
|
|
4
|
+
|
|
5
|
+
- CLI: `slotdiff <old> <new>`, where each side is a contract in your build info or an address (a proxy, read as what it runs now, or an implementation; needs `--rpc` and `ETHERSCAN_API_KEY`). Checks the storage layout, the ERC-7201 slots and OpenZeppelin's safety rules for `<new>`, with the proxy kind taken from `<old>`'s implementation, so a UUPS implementation missing `upgradeToAndCall` fails. Exits 0 when every check passed, 1 when one failed, 2 when something could not be checked. `ETHERSCAN_API_KEY` is sent only to Etherscan, never to `--explorer-url`.
|
|
6
|
+
- `findBuildInfo`: reads Foundry, Hardhat 2 and Hardhat 3 build info. The build is chosen by content: only one whose target and imports still match the files on disk, so the stale files Foundry keeps after each edit are never used. Its `input` holds only what the target imports.
|
|
7
|
+
- ERC-7201 namespaces are compared by the slot the code really uses, not only the annotation: a namespace whose slot moves fails, whatever the annotations say, and a corrected annotation over an unchanged slot passes (`SourceAnalysis.namespaces`, `checkNamespaces`).
|
|
8
|
+
- `analyzeSource`: `layoutFromSource` plus OpenZeppelin's safety check from one compile, as the CLI runs it; `safety({ kind })` takes the proxy kind being upgraded.
|
|
9
|
+
- Explorer errors no longer repeat fetch's error text, which can quote the request URL and with it the API key.
|
|
10
|
+
- CLI colors and a banner on an interactive terminal only (on runs, `--help` and `--version`); in a pipe or CI the output stays plain and `--version` prints just the version. `NO_COLOR` and `FORCE_COLOR` are honored. Built on Node's `util.styleText`, so no new dependency.
|
|
11
|
+
- **Breaking:** Node 22.13+ (was 22.12+), for `util.styleText`'s terminal detection.
|
|
12
|
+
- Releases are published from GitHub Actions through npm trusted publishing, staged for a maintainer's 2FA approval, with a provenance attestation linking each version to its commit.
|
|
13
|
+
- `SECURITY.md`: how to report a vulnerability privately, and what slotdiff touches (network, processes, files, environment). The README and `--help` now say plainly that a pass is no guarantee and that slotdiff comes without warranty.
|
|
14
|
+
|
|
3
15
|
## 0.1.0-alpha.1
|
|
4
16
|
|
|
5
17
|
First release.
|
|
6
18
|
|
|
7
|
-
- `resolveProxyLayout` / `resolveLayout`: the storage layout of the code a proxy (ERC-1967 or
|
|
8
|
-
beacon) runs now, rebuilt from verified standard-json source on any Etherscan-compatible explorer
|
|
9
|
-
and proven against the deployed code.
|
|
19
|
+
- `resolveProxyLayout` / `resolveLayout`: the storage layout of the code a proxy (ERC-1967 or beacon) runs now, rebuilt from verified standard-json source on any Etherscan-compatible explorer and proven against the deployed code.
|
|
10
20
|
- `proveLocalBuild`: a layout record from your own build, only when it is the deployed build.
|
|
11
21
|
- `diffLayouts`: OpenZeppelin's storage comparison, including ERC-7201 namespaced storage.
|
|
12
|
-
- `LayoutRecord`s are plain JSON that slotdiff never writes. Load them back with `verifyRecord`
|
|
13
|
-
|
|
14
|
-
-
|
|
15
|
-
`bytecodeHash: "none"` or `appendCBOR: false` are reported as `code-only` and refused.
|
|
16
|
-
- `getSolc`: native solc from a cache (Hardhat's included) or a checksum-verified download.
|
|
17
|
-
Accepts release versions such as `0.8.24`, as Foundry's build info records them.
|
|
22
|
+
- `LayoutRecord`s are plain JSON that slotdiff never writes. Load them back with `verifyRecord` (checked against the chain id and deployed code) or `parseRecord` (offline).
|
|
23
|
+
- A match proves a layout only when the build embeds solc's metadata hash. Builds with `bytecodeHash: "none"` or `appendCBOR: false` are reported as `code-only` and refused.
|
|
24
|
+
- `getSolc`: native solc from a cache (Hardhat's included) or a checksum-verified download. Accepts release versions such as `0.8.24`, as Foundry's build info records them.
|
|
18
25
|
- `layoutFromSource` accepts build-info input from Foundry and Hardhat.
|
|
19
26
|
- Loads from ESM and CommonJS. Node 22.12+.
|
package/README.md
CHANGED
|
@@ -1,30 +1,39 @@
|
|
|
1
1
|
# slotdiff
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/slotdiff) [](https://github.com/rokso/slotdiff/actions/workflows/ci.yml) [](https://scorecard.dev/viewer/?uri=github.com/rokso/slotdiff) [](LICENSE)
|
|
4
|
+
|
|
5
|
+
Storage layout diff for upgradeable Solidity contracts, where either side can be a local build or the code a proxy actually runs on chain.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npx slotdiff 0xProxy src/Vault.sol:Vault --rpc $RPC_URL # what the proxy runs vs your build
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
For example, two local versions:
|
|
5
12
|
|
|
6
|
-
```
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
```
|
|
14
|
+
$ npx slotdiff src/Vault.sol:Vault src/VaultBad.sol:VaultBad
|
|
15
|
+
old src/Vault.sol:Vault
|
|
16
|
+
out/build-info/90f317b6c0a81633.json
|
|
17
|
+
new src/VaultBad.sol:VaultBad
|
|
18
|
+
out/build-info/90f317b6c0a81633.json
|
|
19
|
+
|
|
20
|
+
✗ Storage layout incompatible
|
|
21
|
+
src/VaultBad.sol:4: Inserted `inserted`
|
|
22
|
+
> New variables should be placed after all existing inherited variables
|
|
23
|
+
✓ Safety rules no problems found (transparent proxy)
|
|
24
|
+
|
|
25
|
+
Not safe to upgrade: the storage layout is incompatible.
|
|
13
26
|
```
|
|
14
27
|
|
|
15
28
|
## Why
|
|
16
29
|
|
|
17
|
-
An upgrade is only safe relative to what the proxy runs **now**. Under lazy or multisig-queued
|
|
18
|
-
upgrades that is often not your latest build, and the tools you have compare the wrong thing:
|
|
30
|
+
An upgrade is only safe relative to what the proxy runs **now**. Under lazy or multisig-queued upgrades that is often not your latest build, and the tools you have compare the wrong thing:
|
|
19
31
|
|
|
20
32
|
- OpenZeppelin's `validate` compares two local builds. It has no way to know what is live.
|
|
21
33
|
- `cast storage <address>` prints a live layout but does not compare it with anything.
|
|
22
34
|
- Deployment artifacts describe the last build you deployed, not necessarily what the proxy runs.
|
|
23
35
|
|
|
24
|
-
slotdiff reads the implementation behind the proxy, rebuilds its layout from verified source, and
|
|
25
|
-
**proves** that source compiles to the deployed code before trusting it. It then compares with
|
|
26
|
-
OpenZeppelin's own rules, including ERC-7201 namespaced storage. Framework-agnostic: Foundry,
|
|
27
|
-
Hardhat 2 and 3, or a plain script.
|
|
36
|
+
slotdiff reads the implementation behind the proxy, rebuilds its layout from verified source, and **proves** that source compiles to the deployed code before trusting it. It then compares with OpenZeppelin's own rules, including ERC-7201 namespaced storage. Framework-agnostic: Foundry, Hardhat 2 and 3, or a plain script.
|
|
28
37
|
|
|
29
38
|
## Install
|
|
30
39
|
|
|
@@ -32,16 +41,94 @@ Hardhat 2 and 3, or a plain script.
|
|
|
32
41
|
npm install slotdiff
|
|
33
42
|
```
|
|
34
43
|
|
|
35
|
-
Node 22.
|
|
44
|
+
Node 22.13+. Works from ESM and CommonJS.
|
|
45
|
+
|
|
46
|
+
## Command line
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
slotdiff <old> <new> [--rpc <url>]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Each side is a contract in your build info (`src/Vault.sol:Vault`, or just `Vault` when only one contract has that name) or an address: a proxy, read as the implementation it runs now, or an implementation, such as one waiting in a multisig queue. Any mix works. An address needs `--rpc` and `ETHERSCAN_API_KEY` (or `--explorer-url` for another explorer), since its source comes from the explorer. The RPC is a flag because it picks the chain for each run; the key lives in the environment because one Etherscan key covers every chain. It is sent only to Etherscan: with `--explorer-url`, put that explorer's own key in the URL's query if it needs one.
|
|
53
|
+
|
|
54
|
+
A local side is read from the build info your tool already wrote; slotdiff does not compile your project. Build first, from the project root or with `--root`. Foundry writes build info only with `build_info = true` in `foundry.toml` or `forge build --build-info`. A build older than the sources on disk is refused, never used. `--build-info <file>` names one build-info file to use as is, such as a CI artifact from another checkout; the output then says it was not compared with the sources on disk.
|
|
55
|
+
|
|
56
|
+
Three checks run:
|
|
57
|
+
|
|
58
|
+
- **Storage layout**, by OpenZeppelin's rules, as above, with ERC-7201 namespaces matched by the slot the code really uses.
|
|
59
|
+
- **ERC-7201 slots**: each namespace keeps its data in place and `<new>`'s annotations match its code (see below).
|
|
60
|
+
- **Safety rules** for `<new>`, OpenZeppelin's checks that it can run behind a proxy: constructors, `selfdestruct`, `delegatecall`, initial values, immutables and initializers (an initializer order problem is only a warning, as in OpenZeppelin). Source exceptions (`@custom:oz-upgrades-unsafe-allow`) are honored, and `--unsafe-allow <kinds>` accepts more. For an address, the verified source is checked. `--no-safety` skips it.
|
|
61
|
+
|
|
62
|
+
OpenZeppelin matches ERC-7201 namespaces by their `@custom:storage-location erc7201:<id>` annotation alone. slotdiff also reads the slot the code assigns (`assembly { $.slot := LOCATION }`, with `LOCATION` a hex constant, the ERC-7201 formula, or `erc7201("id")`) and compares namespaces by that slot. The check fails when `<new>` would read a namespace from another slot than the deployed code wrote it to, whatever the annotations say, and when an annotation in `<new>` disagrees with its code. In `<old>`, already deployed, a disagreement is reported and the namespace is matched by its real slot, so correcting a wrong annotation is not reported as a deleted namespace. A slot slotdiff cannot read (computed at run time, say) means the upgrade could not be checked: exit code 2.
|
|
63
|
+
|
|
64
|
+
For example, after correcting an annotation that never matched the deployed code:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
ERC-7201 namespaces
|
|
68
|
+
annotation slot from annotation slot in code
|
|
69
|
+
old example.vault 0xd192…a100 0x0d90…d100 ✗ annotation does not match code
|
|
70
|
+
new example.vault.real 0x0d90…d100 0x0d90…d100 ✓
|
|
71
|
+
Same slot in code on both sides: the storage did not move. Compared as one namespace.
|
|
72
|
+
|
|
73
|
+
✓ Storage layout compatible
|
|
74
|
+
✓ ERC-7201 slots compared by the slot in code (see above)
|
|
75
|
+
✓ Safety rules no problems found (transparent proxy)
|
|
76
|
+
|
|
77
|
+
Safe to upgrade.
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Changing the constant to match the wrong annotation instead would move the data:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
ERC-7201 namespaces
|
|
84
|
+
annotation slot from annotation slot in code
|
|
85
|
+
old example.vault 0xd192…a100 0x0d90…d100 ✗ annotation does not match code
|
|
86
|
+
new example.vault 0xd192…a100 0xd192…a100 ✓
|
|
87
|
+
The new code moves this namespace from 0x0d90…d100 to 0xd192…a100: its data would be left behind. Keep the slot the deployed code uses, under an annotation that matches it.
|
|
88
|
+
|
|
89
|
+
✓ Storage layout compatible
|
|
90
|
+
✗ ERC-7201 slots problems found in <new> (see above)
|
|
91
|
+
✓ Safety rules no problems found (transparent proxy)
|
|
92
|
+
|
|
93
|
+
Not safe to upgrade: an ERC-7201 namespace fails the slot check.
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The proxy kind comes from `<old>`'s implementation: UUPS when it has `upgradeTo` or `upgradeToAndCall`, beacon for a beacon proxy, transparent otherwise. So a UUPS implementation that lost its upgrade function, which would leave the proxy unable to upgrade again, is caught:
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
$ npx slotdiff src/UupsV1.sol:UupsV1 src/UupsV2Lost.sol:UupsV2Lost
|
|
100
|
+
old src/UupsV1.sol:UupsV1
|
|
101
|
+
out/build-info/fb5d5362dc531baa.json
|
|
102
|
+
new src/UupsV2Lost.sol:UupsV2Lost
|
|
103
|
+
out/build-info/fb5d5362dc531baa.json
|
|
104
|
+
|
|
105
|
+
✓ Storage layout compatible
|
|
106
|
+
✗ Safety rules problems found (uups proxy)
|
|
107
|
+
src/UupsV2Lost.sol:3: Implementation is missing a public `upgradeTo(address)` or `upgradeToAndCall(address,bytes)` function
|
|
108
|
+
Inherit UUPSUpgradeable to include one or both of these functions in your contract
|
|
109
|
+
@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol
|
|
110
|
+
https://zpl.in/upgrades/error-008
|
|
111
|
+
|
|
112
|
+
Not safe to upgrade: <new> breaks the safety rules.
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Exit codes: `0` every check ran and passed (or was skipped with `--no-safety`), `1` a check failed, `2` something could not be checked (never reported as compatible). `slotdiff --help` lists every option.
|
|
116
|
+
|
|
117
|
+
Colors and the banner appear only on an interactive terminal, never in a pipe, a log or CI. `NO_COLOR` turns colors off; `FORCE_COLOR` turns them on, for CI logs that render them.
|
|
36
118
|
|
|
37
119
|
## Usage
|
|
38
120
|
|
|
39
121
|
### Live proxy vs a local build
|
|
40
122
|
|
|
41
123
|
```ts
|
|
42
|
-
import { readFileSync } from "node:fs";
|
|
43
124
|
import { JsonRpcProvider } from "ethers";
|
|
44
|
-
import {
|
|
125
|
+
import {
|
|
126
|
+
diffLayouts,
|
|
127
|
+
findBuildInfo,
|
|
128
|
+
getSolc,
|
|
129
|
+
layoutFromSource,
|
|
130
|
+
resolveProxyLayout,
|
|
131
|
+
} from "slotdiff";
|
|
45
132
|
|
|
46
133
|
// Any object with send(method, params): an ethers JsonRpcProvider or Hardhat's network provider.
|
|
47
134
|
const provider = new JsonRpcProvider(process.env.RPC_URL);
|
|
@@ -50,10 +137,10 @@ const apiKey = process.env.ETHERSCAN_API_KEY;
|
|
|
50
137
|
// 1. The layout of whatever the proxy runs now, proven against its deployed code.
|
|
51
138
|
const live = await resolveProxyLayout(provider, "0xProxy", { explorer: { apiKey } });
|
|
52
139
|
|
|
53
|
-
// 2. The layout of the new version, from your build info (
|
|
54
|
-
const
|
|
55
|
-
const solc = await getSolc(
|
|
56
|
-
const next = await layoutFromSource(
|
|
140
|
+
// 2. The layout of the new version, from your build info (Foundry, Hardhat 2 or 3).
|
|
141
|
+
const build = await findBuildInfo(process.cwd(), "src/Vault.sol:Vault");
|
|
142
|
+
const solc = await getSolc(build.solcVersion);
|
|
143
|
+
const next = await layoutFromSource(build.input, solc, build.contract);
|
|
57
144
|
|
|
58
145
|
// 3. Compare.
|
|
59
146
|
const report = diffLayouts(live.record.layout, next);
|
|
@@ -63,15 +150,15 @@ if (!report.ok) {
|
|
|
63
150
|
}
|
|
64
151
|
```
|
|
65
152
|
|
|
66
|
-
Where the build info lives:
|
|
153
|
+
`findBuildInfo` picks the build whose sources still match the files on disk, so the stale build info Foundry keeps after each edit is never used, and its `input` holds only the target and what it imports. Pass `{ file }` to use one file as is (for example a CI artifact). Where the build info lives:
|
|
67
154
|
|
|
68
|
-
| Tool
|
|
69
|
-
|
|
|
70
|
-
| Foundry
|
|
71
|
-
| Hardhat 2 | `artifacts/build-info/*.json`
|
|
72
|
-
| Hardhat 3 | `artifacts/build-info/<id>.json`; the compiler output is in `<id>.output.json` next to it
|
|
155
|
+
| Tool | Build info | Contract name |
|
|
156
|
+
| --- | --- | --- |
|
|
157
|
+
| Foundry | `out/build-info/*.json`, only with `build_info = true` in `foundry.toml` or `forge build --build-info` | `src/Vault.sol:Vault` |
|
|
158
|
+
| Hardhat 2 | `artifacts/build-info/*.json` | `contracts/Vault.sol:Vault` |
|
|
159
|
+
| Hardhat 3 | `artifacts/build-info/<id>.json`; the compiler output is in `<id>.output.json` next to it | `project/contracts/Vault.sol:Vault` |
|
|
73
160
|
|
|
74
|
-
|
|
161
|
+
With `findBuildInfo`, Hardhat 3's `project/` prefix is optional.
|
|
75
162
|
|
|
76
163
|
`explain()` gives OpenZeppelin's report, for example:
|
|
77
164
|
|
|
@@ -80,14 +167,36 @@ src/Vault.sol:7: Inserted `inserted`
|
|
|
80
167
|
> New variables should be placed after all existing inherited variables
|
|
81
168
|
```
|
|
82
169
|
|
|
83
|
-
The explorer defaults to Etherscan's v2 API, where one key covers every chain it indexes. For another
|
|
84
|
-
|
|
170
|
+
The explorer defaults to Etherscan's v2 API, where one key covers every chain it indexes. For another Etherscan-compatible explorer, such as Blockscout, pass `{ apiUrl }` (a key is optional there).
|
|
171
|
+
|
|
172
|
+
### Safety rules and ERC-7201 slots too
|
|
173
|
+
|
|
174
|
+
`analyzeSource` is `layoutFromSource` plus OpenZeppelin's safety check and the slot each ERC-7201 namespace really uses, from one compile, as the command line runs them. Pass the kind of the proxy being upgraded, not the new contract's own: a UUPS implementation that lost its upgrade function would otherwise look transparent and pass.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { analyzeSource, checkNamespaces, diffLayouts, findBuildInfo, getSolc } from "slotdiff";
|
|
178
|
+
|
|
179
|
+
async function analyze(contract: string) {
|
|
180
|
+
const build = await findBuildInfo(process.cwd(), contract);
|
|
181
|
+
return analyzeSource(build.input, await getSolc(build.solcVersion), build.contract);
|
|
182
|
+
}
|
|
183
|
+
const old = await analyze("src/Vault.sol:Vault");
|
|
184
|
+
const next = await analyze("src/VaultV2.sol:VaultV2");
|
|
185
|
+
|
|
186
|
+
// Pair ERC-7201 namespaces by the slot the code uses, as the command line does.
|
|
187
|
+
const slots = checkNamespaces(old, next); // result: "pass", "fail" or "unread"
|
|
188
|
+
const layout = diffLayouts(slots.layout, next.layout);
|
|
189
|
+
const safety = next.safety({ kind: old.kind }); // the kind of the proxy being upgraded
|
|
190
|
+
if (!layout.ok || slots.result !== "pass" || !safety.ok) {
|
|
191
|
+
console.error(layout.explain(), slots.pairs, safety.explain());
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
`safety` also takes `unsafeAllow` (error kinds to accept, listed in `SAFETY_ERROR_KINDS`), on top of `@custom:oz-upgrades-unsafe-allow` in the source.
|
|
85
196
|
|
|
86
197
|
### Keep records, skip the rebuild next time
|
|
87
198
|
|
|
88
|
-
Rebuilding means fetching source and compiling. A `LayoutRecord` is plain JSON: store it wherever
|
|
89
|
-
suits you (committed next to your deployments, a CI cache) and load it back with `verifyRecord`,
|
|
90
|
-
which refuses it unless this chain still runs the exact code it was proven against.
|
|
199
|
+
Rebuilding means fetching source and compiling. A `LayoutRecord` is plain JSON: store it wherever suits you (committed next to your deployments, a CI cache) and load it back with `verifyRecord`, which refuses it unless this chain still runs the exact code it was proven against.
|
|
91
200
|
|
|
92
201
|
```ts
|
|
93
202
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
@@ -105,109 +214,91 @@ try {
|
|
|
105
214
|
}
|
|
106
215
|
```
|
|
107
216
|
|
|
108
|
-
Key records by **implementation**, and read the implementation from the proxy every time. A record
|
|
109
|
-
for an old implementation still verifies after the proxy is upgraded, because that code is still
|
|
110
|
-
deployed at its own address.
|
|
217
|
+
Key records by **implementation**, and read the implementation from the proxy every time. A record for an old implementation still verifies after the proxy is upgraded, because that code is still deployed at its own address.
|
|
111
218
|
|
|
112
|
-
Without an RPC, `parseRecord(value, address)` checks a record's shape and proof offline. It cannot
|
|
113
|
-
tell whether the proxy has moved on, so prefer `verifyRecord` whenever you can reach the chain.
|
|
219
|
+
Without an RPC, `parseRecord(value, address)` checks a record's shape and proof offline. It cannot tell whether the proxy has moved on, so prefer `verifyRecord` whenever you can reach the chain.
|
|
114
220
|
|
|
115
|
-
A record is bound to the deployed code, not to its own `layout` field: an edited layout in a saved
|
|
116
|
-
record still verifies. Treat saved records as trusted input and review changes to them like code.
|
|
221
|
+
A record is bound to the deployed code, not to its own `layout` field: an edited layout in a saved record still verifies. Treat saved records as trusted input and review changes to them like code.
|
|
117
222
|
|
|
118
223
|
### No explorer: prove your own build
|
|
119
224
|
|
|
120
|
-
If you deployed it yourself, your build can serve as the source. `proveLocalBuild` compares the
|
|
121
|
-
deployed code with your compiler output and returns a record only when they prove to be the same
|
|
122
|
-
build.
|
|
225
|
+
If you deployed it yourself, your build can serve as the source. `proveLocalBuild` compares the deployed code with your compiler output and returns a record only when they prove to be the same build.
|
|
123
226
|
|
|
124
227
|
```ts
|
|
125
|
-
import { proveLocalBuild } from "slotdiff";
|
|
228
|
+
import { findBuildInfo, getSolc, layoutFromSource, proveLocalBuild } from "slotdiff";
|
|
126
229
|
|
|
127
|
-
|
|
128
|
-
const
|
|
230
|
+
const build = await findBuildInfo(process.cwd(), "src/Vault.sol:Vault");
|
|
231
|
+
const solc = await getSolc(build.solcVersion);
|
|
129
232
|
const { bytecodeMatch, record } = await proveLocalBuild(provider, impl, {
|
|
130
|
-
contract:
|
|
233
|
+
contract: build.contract,
|
|
131
234
|
compiler: solc.longVersion,
|
|
132
|
-
layout: await layoutFromSource(
|
|
133
|
-
deployedBytecode:
|
|
134
|
-
immutableReferences:
|
|
235
|
+
layout: await layoutFromSource(build.input, solc, build.contract),
|
|
236
|
+
deployedBytecode: build.deployedBytecode!,
|
|
237
|
+
immutableReferences: build.immutableReferences,
|
|
135
238
|
});
|
|
136
239
|
if (record === undefined) throw new Error(`Not the deployed build (${bytecodeMatch})`);
|
|
137
240
|
```
|
|
138
241
|
|
|
139
242
|
## What counts as proof
|
|
140
243
|
|
|
141
|
-
A layout is only trusted when the compiled code matches the deployed code as `exact` or
|
|
142
|
-
`immutables-only` (identical once immutables and library addresses are masked), **and** the build
|
|
143
|
-
embeds solc's metadata hash. That hash covers every source file and setting, so matching code then
|
|
144
|
-
means matching source.
|
|
244
|
+
A layout is only trusted when the compiled code matches the deployed code as `exact` or `immutables-only` (identical once immutables and library addresses are masked), **and** the build embeds solc's metadata hash. That hash covers every source file and setting, so matching code then means matching source.
|
|
145
245
|
|
|
146
|
-
Anything weaker is refused, because variables no code reads, retyped fields and gap sizes never
|
|
147
|
-
reach the bytecode, so a different layout can compile to the same code:
|
|
246
|
+
Anything weaker is refused, because variables no code reads, retyped fields and gap sizes never reach the bytecode, so a different layout can compile to the same code:
|
|
148
247
|
|
|
149
|
-
| Match
|
|
150
|
-
|
|
|
151
|
-
| `exact`
|
|
152
|
-
| `immutables-only` | Identical once immutables and library links are masked
|
|
153
|
-
| `metadata-only`
|
|
154
|
-
| `code-only`
|
|
155
|
-
| `none`
|
|
248
|
+
| Match | Meaning | Proof? |
|
|
249
|
+
| --- | --- | --- |
|
|
250
|
+
| `exact` | Byte-identical | Yes |
|
|
251
|
+
| `immutables-only` | Identical once immutables and library links are masked | Yes |
|
|
252
|
+
| `metadata-only` | Identical only after stripping metadata: the source differed | No |
|
|
253
|
+
| `code-only` | Identical, but built with `bytecodeHash: "none"` or `appendCBOR: false` | No |
|
|
254
|
+
| `none` | Different code | No |
|
|
156
255
|
|
|
157
|
-
Contracts built without a metadata hash (common for deterministic CREATE2 deployments) therefore
|
|
158
|
-
cannot be proven. For explorer source, the verified compiler settings are checked as well, so a
|
|
159
|
-
build without a hash is refused even if its code happens to end in bytes shaped like one. With
|
|
160
|
-
`proveLocalBuild`, the hash is read from your own compiler output.
|
|
256
|
+
Contracts built without a metadata hash (common for deterministic CREATE2 deployments) therefore cannot be proven. For explorer source, the verified compiler settings are checked as well, so a build without a hash is refused even if its code happens to end in bytes shaped like one. With `proveLocalBuild`, the hash is read from your own compiler output.
|
|
161
257
|
|
|
162
258
|
## Errors
|
|
163
259
|
|
|
164
260
|
slotdiff fails closed: it never treats "could not check" as "safe".
|
|
165
261
|
|
|
166
|
-
- `LayoutUnavailableError`: no answer. Not a proxy, unverified source, explorer outage or rate
|
|
167
|
-
|
|
168
|
-
- `LayoutIntegrityError`: an answer that cannot be trusted. Source that does not compile to the
|
|
169
|
-
deployed code, a weak match, a record from another chain or for other code. Never fall back on
|
|
170
|
-
this one.
|
|
262
|
+
- `LayoutUnavailableError`: no answer. Not a proxy, unverified source, explorer outage or rate limit, no compiler for the platform. Falling back to another source is reasonable.
|
|
263
|
+
- `LayoutIntegrityError`: an answer that cannot be trusted. Source that does not compile to the deployed code, a weak match, a record from another chain or for other code. Never fall back on this one.
|
|
171
264
|
|
|
172
|
-
A few problems that are yours to fix throw a plain `Error`: a rejected explorer API key, a compiler
|
|
173
|
-
binary that fails to run (for example the wrong architecture), and a record in a format from a
|
|
174
|
-
newer slotdiff.
|
|
265
|
+
A few problems that are yours to fix throw a plain `Error`: a rejected explorer API key, a compiler binary that fails to run (for example the wrong architecture), and a record in a format from a newer slotdiff.
|
|
175
266
|
|
|
176
267
|
## API
|
|
177
268
|
|
|
178
|
-
| Export
|
|
179
|
-
|
|
|
180
|
-
| `diffLayouts(before, after, { unsafeAllowRenames? })`
|
|
181
|
-
| `resolveProxyLayout(provider, proxy, options)`
|
|
182
|
-
| `resolveLayout(provider, address, options)`
|
|
183
|
-
| `proveLocalBuild(provider, address, build)`
|
|
184
|
-
| `verifyRecord(provider, address, value)`
|
|
185
|
-
| `parseRecord(value, address?)`
|
|
186
|
-
| `layoutFromSource(input, solc, "file.sol:Name")`
|
|
187
|
-
| `
|
|
188
|
-
| `
|
|
189
|
-
| `
|
|
190
|
-
| `
|
|
191
|
-
|
|
192
|
-
`
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
`@custom:oz-retyped-from`.
|
|
269
|
+
| Export | Purpose |
|
|
270
|
+
| --- | --- |
|
|
271
|
+
| `diffLayouts(before, after, { unsafeAllowRenames? })` | OpenZeppelin's storage comparison. `ok` and `explain()` |
|
|
272
|
+
| `resolveProxyLayout(provider, proxy, options)` | Proven layout of what a proxy (ERC-1967 or beacon) runs now |
|
|
273
|
+
| `resolveLayout(provider, address, options)` | Proven layout of the code at an implementation address |
|
|
274
|
+
| `proveLocalBuild(provider, address, build)` | A record from your own build, only when it is the deployed build |
|
|
275
|
+
| `verifyRecord(provider, address, value)` | Load a saved record, checked against the chain |
|
|
276
|
+
| `parseRecord(value, address?)` | Load a saved record, checked offline |
|
|
277
|
+
| `layoutFromSource(input, solc, "file.sol:Name")` | Layout from standard-json input, including ERC-7201 namespaces. No proof |
|
|
278
|
+
| `analyzeSource(input, solc, "file.sol:Name")` | The layout, proxy `kind`, ERC-7201 `namespaces` and `safety({ kind })` |
|
|
279
|
+
| `checkNamespaces(old, next)` | ERC-7201 namespaces paired by real slot; a moved one fails |
|
|
280
|
+
| `findBuildInfo(root, contract, { file? })` | Your build info for a contract: input, compiler version, deployed code |
|
|
281
|
+
| `getSolc(version)` | Native solc, from a cache or downloaded and checksum-verified |
|
|
282
|
+
| `readProxy`, `readImplementation`, `readCode`, `readChainId` | Chain reads |
|
|
283
|
+
| `fetchVerifiedSource(chainId, address, explorer)` | Verified standard-json source from an Etherscan-compatible explorer |
|
|
284
|
+
| `compareDeployedBytecode`, `isProvingMatch` | The match classification above |
|
|
285
|
+
|
|
286
|
+
`getSolc` takes a long (`0.8.24+commit.e11b9ed9`) or release (`0.8.24`) version. Compilers are cached under your OS cache directory (`slotdiff/compilers`, or `cacheDir`), and Hardhat's compiler cache is reused when present.
|
|
287
|
+
|
|
288
|
+
Renames and retypes follow OpenZeppelin's NatSpec tags, `@custom:oz-renamed-from` and `@custom:oz-retyped-from`.
|
|
198
289
|
|
|
199
290
|
## Limitations
|
|
200
291
|
|
|
201
|
-
- Explorer source must be verified as standard-json. Flattened and multi-file verifications do not
|
|
202
|
-
record every compiler setting, so they are refused.
|
|
292
|
+
- Explorer source must be verified as standard-json. Flattened and multi-file verifications do not record every compiler setting, so they are refused.
|
|
203
293
|
- Solidity only.
|
|
204
|
-
- Struct members cannot be renamed or retyped: Solidity has no NatSpec on struct members, so
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
294
|
+
- Struct members cannot be renamed or retyped: Solidity has no NatSpec on struct members, so OpenZeppelin's tags cannot reach them.
|
|
295
|
+
- An ERC-7201 slot is read only from a `$.slot := ...` assignment whose value is a constant (a hex literal, the ERC-7201 formula, or `erc7201("id")`), directly or through a local variable set once or a function with no arguments that returns one. Any other slot (computed at run time, a library's constant) is not read, and the upgrade is reported as not checked.
|
|
296
|
+
|
|
297
|
+
## No guarantee
|
|
298
|
+
|
|
299
|
+
slotdiff is provided as is, without warranty of any kind (see [LICENSE](LICENSE)). It checks what its rules and OpenZeppelin's can check. "Safe to upgrade" means those checks found no problem, not that the upgrade is safe in every way. It does not replace review, a test upgrade on a fork, or an audit. The authors accept no liability for any loss, including lost or locked funds, arising from using slotdiff or relying on its results.
|
|
300
|
+
|
|
301
|
+
To report a security problem, or to see what slotdiff touches on your machine (network, processes, files, environment), read [SECURITY.md](SECURITY.md).
|
|
211
302
|
|
|
212
303
|
## License
|
|
213
304
|
|
package/dist/bin.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":""}
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { styleText } from "node:util";
|
|
3
|
+
import { run } from "./cli.js";
|
|
4
|
+
// A reader that stops early, such as `| head`, is not an error.
|
|
5
|
+
process.stdout.on("error", (e) => {
|
|
6
|
+
if (e.code !== "EPIPE")
|
|
7
|
+
throw e;
|
|
8
|
+
process.exit();
|
|
9
|
+
});
|
|
10
|
+
run(process.argv.slice(2), {
|
|
11
|
+
stdout: (line) => console.log(line),
|
|
12
|
+
stderr: (line) => console.error(line),
|
|
13
|
+
tty: process.stdout.isTTY === true,
|
|
14
|
+
// Node's own rules for color: a terminal that has them, NO_COLOR, FORCE_COLOR.
|
|
15
|
+
color: styleText("red", "x", { stream: process.stdout }) !== "x",
|
|
16
|
+
}).then((code) => {
|
|
17
|
+
process.exitCode = code;
|
|
18
|
+
}, (e) => {
|
|
19
|
+
console.error(e);
|
|
20
|
+
process.exitCode = 2;
|
|
21
|
+
});
|
|
22
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,gEAAgE;AAChE,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,CAAwB,EAAE,EAAE;IACtD,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO;QAAE,MAAM,CAAC,CAAC;IAChC,OAAO,CAAC,IAAI,EAAE,CAAC;AACjB,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;IACzB,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;IACnC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC;IACrC,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,KAAK,IAAI;IAClC,+EAA+E;IAC/E,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,GAAG;CACjE,CAAC,CAAC,IAAI,CACL,CAAC,IAAI,EAAE,EAAE;IACP,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;AAC1B,CAAC,EACD,CAAC,CAAU,EAAE,EAAE;IACb,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CACF,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads a project's own build info: Foundry, Hardhat 2 and Hardhat 3.
|
|
3
|
+
*
|
|
4
|
+
* The build is chosen by content, never by modification time or file count.
|
|
5
|
+
* Foundry keeps every build-info file it ever wrote, so after a few edits
|
|
6
|
+
* several hold the target, most of them describing older sources. Only a build
|
|
7
|
+
* whose sources still match the files on disk is used.
|
|
8
|
+
*/
|
|
9
|
+
import type { SolcInput } from "@openzeppelin/upgrades-core";
|
|
10
|
+
import type { ImmutableReferences } from "./bytecode.js";
|
|
11
|
+
export interface BuildInfoOptions {
|
|
12
|
+
/**
|
|
13
|
+
* One build-info file to use as is, relative to the project root. Its
|
|
14
|
+
* sources are not compared with the files on disk, so it may come from
|
|
15
|
+
* another checkout, such as a CI artifact.
|
|
16
|
+
*/
|
|
17
|
+
file?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface BuildInfoContract {
|
|
20
|
+
/** The build-info file it was read from. */
|
|
21
|
+
file: string;
|
|
22
|
+
/**
|
|
23
|
+
* Fully qualified name as solc saw it, `source.sol:Name`. Hardhat 3 prefixes
|
|
24
|
+
* project sources with `project/`.
|
|
25
|
+
*/
|
|
26
|
+
contract: string;
|
|
27
|
+
/** As recorded: a long version, or a release version for Foundry. Both work with `getSolc`. */
|
|
28
|
+
solcVersion: string;
|
|
29
|
+
/**
|
|
30
|
+
* The compiler input, reduced to the target and what it imports, so that a
|
|
31
|
+
* layout compiles only those sources rather than the whole project.
|
|
32
|
+
*/
|
|
33
|
+
input: SolcInput;
|
|
34
|
+
/** Absent when the build info holds no compiler output. */
|
|
35
|
+
deployedBytecode?: string;
|
|
36
|
+
immutableReferences?: ImmutableReferences;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Finds `contract` in the build info under `root` and returns what a layout
|
|
40
|
+
* or a bytecode proof needs. `contract` is `path/File.sol:Name`, with the path
|
|
41
|
+
* as written in the project (Hardhat 3's `project/` prefix is optional), or a
|
|
42
|
+
* bare `Name` when only one contract has it.
|
|
43
|
+
*/
|
|
44
|
+
export declare function findBuildInfo(root: string, contract: string, options?: BuildInfoOptions): Promise<BuildInfoContract>;
|
|
45
|
+
//# sourceMappingURL=build-info.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-info.d.ts","sourceRoot":"","sources":["../src/build-info.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAG7D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEzD,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,iBAAiB;IAChC,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB,+FAA+F;IAC/F,WAAW,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,KAAK,EAAE,SAAS,CAAC;IACjB,2DAA2D;IAC3D,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,mBAAmB,CAAC,EAAE,mBAAmB,CAAC;CAC3C;AAkCD;;;;;GAKG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,iBAAiB,CAAC,CAgH5B"}
|