slotdiff 0.0.0-stage → 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.
Files changed (79) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +10 -0
  3. package/README.md +304 -2
  4. package/dist/bin.d.ts +3 -0
  5. package/dist/bin.d.ts.map +1 -0
  6. package/dist/bin.js +22 -0
  7. package/dist/bin.js.map +1 -0
  8. package/dist/build-info.d.ts +45 -0
  9. package/dist/build-info.d.ts.map +1 -0
  10. package/dist/build-info.js +319 -0
  11. package/dist/build-info.js.map +1 -0
  12. package/dist/bytecode.d.ts +46 -0
  13. package/dist/bytecode.d.ts.map +1 -0
  14. package/dist/bytecode.js +101 -0
  15. package/dist/bytecode.js.map +1 -0
  16. package/dist/chain.d.ts +20 -0
  17. package/dist/chain.d.ts.map +1 -0
  18. package/dist/chain.js +52 -0
  19. package/dist/chain.js.map +1 -0
  20. package/dist/cli.d.ts +25 -0
  21. package/dist/cli.d.ts.map +1 -0
  22. package/dist/cli.js +363 -0
  23. package/dist/cli.js.map +1 -0
  24. package/dist/diff.d.ts +15 -0
  25. package/dist/diff.d.ts.map +1 -0
  26. package/dist/diff.js +11 -0
  27. package/dist/diff.js.map +1 -0
  28. package/dist/erc7201.d.ts +72 -0
  29. package/dist/erc7201.d.ts.map +1 -0
  30. package/dist/erc7201.js +340 -0
  31. package/dist/erc7201.js.map +1 -0
  32. package/dist/errors.d.ts +23 -0
  33. package/dist/errors.d.ts.map +1 -0
  34. package/dist/errors.js +32 -0
  35. package/dist/errors.js.map +1 -0
  36. package/dist/explorer.d.ts +42 -0
  37. package/dist/explorer.d.ts.map +1 -0
  38. package/dist/explorer.js +131 -0
  39. package/dist/explorer.js.map +1 -0
  40. package/dist/index.d.ts +13 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +12 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/reconstruct.d.ts +75 -0
  45. package/dist/reconstruct.d.ts.map +1 -0
  46. package/dist/reconstruct.js +169 -0
  47. package/dist/reconstruct.js.map +1 -0
  48. package/dist/record.d.ts +48 -0
  49. package/dist/record.d.ts.map +1 -0
  50. package/dist/record.js +91 -0
  51. package/dist/record.js.map +1 -0
  52. package/dist/report.d.ts +26 -0
  53. package/dist/report.d.ts.map +1 -0
  54. package/dist/report.js +109 -0
  55. package/dist/report.js.map +1 -0
  56. package/dist/resolve.d.ts +65 -0
  57. package/dist/resolve.d.ts.map +1 -0
  58. package/dist/resolve.js +90 -0
  59. package/dist/resolve.js.map +1 -0
  60. package/dist/solc.d.ts +38 -0
  61. package/dist/solc.d.ts.map +1 -0
  62. package/dist/solc.js +194 -0
  63. package/dist/solc.js.map +1 -0
  64. package/package.json +73 -4
  65. package/src/bin.ts +25 -0
  66. package/src/build-info.ts +405 -0
  67. package/src/bytecode.ts +141 -0
  68. package/src/chain.ts +75 -0
  69. package/src/cli.ts +423 -0
  70. package/src/diff.ts +31 -0
  71. package/src/erc7201.ts +412 -0
  72. package/src/errors.ts +33 -0
  73. package/src/explorer.ts +190 -0
  74. package/src/index.ts +53 -0
  75. package/src/reconstruct.ts +329 -0
  76. package/src/record.ts +127 -0
  77. package/src/report.ts +155 -0
  78. package/src/resolve.ts +153 -0
  79. package/src/solc.ts +268 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # Changelog
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
+
15
+ ## 0.1.0-alpha.1
16
+
17
+ First release.
18
+
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.
20
+ - `proveLocalBuild`: a layout record from your own build, only when it is the deployed build.
21
+ - `diffLayouts`: OpenZeppelin's storage comparison, including ERC-7201 namespaced storage.
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.
25
+ - `layoutFromSource` accepts build-info input from Foundry and Hardhat.
26
+ - Loads from ESM and CommonJS. Node 22.12+.
package/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rohit Solia
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
10
+
package/README.md CHANGED
@@ -1,3 +1,305 @@
1
- # Temporary Holding Version
1
+ # slotdiff
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/slotdiff)](https://www.npmjs.com/package/slotdiff) [![CI](https://github.com/rokso/slotdiff/actions/workflows/ci.yml/badge.svg)](https://github.com/rokso/slotdiff/actions/workflows/ci.yml) [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/rokso/slotdiff/badge)](https://scorecard.dev/viewer/?uri=github.com/rokso/slotdiff) [![License: MIT](https://img.shields.io/npm/l/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:
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.
26
+ ```
27
+
28
+ ## Why
29
+
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:
31
+
32
+ - OpenZeppelin's `validate` compares two local builds. It has no way to know what is live.
33
+ - `cast storage <address>` prints a live layout but does not compare it with anything.
34
+ - Deployment artifacts describe the last build you deployed, not necessarily what the proxy runs.
35
+
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.
37
+
38
+ ## Install
39
+
40
+ ```sh
41
+ npm install slotdiff
42
+ ```
43
+
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.
118
+
119
+ ## Usage
120
+
121
+ ### Live proxy vs a local build
122
+
123
+ ```ts
124
+ import { JsonRpcProvider } from "ethers";
125
+ import {
126
+ diffLayouts,
127
+ findBuildInfo,
128
+ getSolc,
129
+ layoutFromSource,
130
+ resolveProxyLayout,
131
+ } from "slotdiff";
132
+
133
+ // Any object with send(method, params): an ethers JsonRpcProvider or Hardhat's network provider.
134
+ const provider = new JsonRpcProvider(process.env.RPC_URL);
135
+ const apiKey = process.env.ETHERSCAN_API_KEY;
136
+
137
+ // 1. The layout of whatever the proxy runs now, proven against its deployed code.
138
+ const live = await resolveProxyLayout(provider, "0xProxy", { explorer: { apiKey } });
139
+
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);
144
+
145
+ // 3. Compare.
146
+ const report = diffLayouts(live.record.layout, next);
147
+ if (!report.ok) {
148
+ console.error(report.explain());
149
+ process.exit(1);
150
+ }
151
+ ```
152
+
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:
154
+
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` |
160
+
161
+ With `findBuildInfo`, Hardhat 3's `project/` prefix is optional.
162
+
163
+ `explain()` gives OpenZeppelin's report, for example:
164
+
165
+ ```
166
+ src/Vault.sol:7: Inserted `inserted`
167
+ > New variables should be placed after all existing inherited variables
168
+ ```
169
+
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.
196
+
197
+ ### Keep records, skip the rebuild next time
198
+
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.
200
+
201
+ ```ts
202
+ import { readFileSync, writeFileSync } from "node:fs";
203
+ import { readImplementation, resolveLayout, verifyRecord } from "slotdiff";
204
+
205
+ const impl = await readImplementation(provider, "0xProxy"); // what the proxy runs now
206
+ const path = `layouts/${impl}.json`;
207
+
208
+ let record;
209
+ try {
210
+ record = await verifyRecord(provider, impl, JSON.parse(readFileSync(path, "utf8")));
211
+ } catch {
212
+ record = await resolveLayout(provider, impl, { explorer: { apiKey } });
213
+ writeFileSync(path, JSON.stringify(record, null, 2));
214
+ }
215
+ ```
216
+
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.
218
+
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.
220
+
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.
222
+
223
+ ### No explorer: prove your own build
224
+
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.
226
+
227
+ ```ts
228
+ import { findBuildInfo, getSolc, layoutFromSource, proveLocalBuild } from "slotdiff";
229
+
230
+ const build = await findBuildInfo(process.cwd(), "src/Vault.sol:Vault");
231
+ const solc = await getSolc(build.solcVersion);
232
+ const { bytecodeMatch, record } = await proveLocalBuild(provider, impl, {
233
+ contract: build.contract,
234
+ compiler: solc.longVersion,
235
+ layout: await layoutFromSource(build.input, solc, build.contract),
236
+ deployedBytecode: build.deployedBytecode!,
237
+ immutableReferences: build.immutableReferences,
238
+ });
239
+ if (record === undefined) throw new Error(`Not the deployed build (${bytecodeMatch})`);
240
+ ```
241
+
242
+ ## What counts as proof
243
+
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.
245
+
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:
247
+
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 |
255
+
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.
257
+
258
+ ## Errors
259
+
260
+ slotdiff fails closed: it never treats "could not check" as "safe".
261
+
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.
264
+
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.
266
+
267
+ ## API
268
+
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`.
289
+
290
+ ## Limitations
291
+
292
+ - Explorer source must be verified as standard-json. Flattened and multi-file verifications do not record every compiler setting, so they are refused.
293
+ - Solidity only.
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).
302
+
303
+ ## License
304
+
305
+ MIT
package/dist/bin.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=bin.d.ts.map
@@ -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
@@ -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"}