@guardefi/cli 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +31 -2
- package/assurance/cli-check.js +446 -0
- package/assurance/cli-project-paths.js +56 -0
- package/assurance/cli-storage.js +289 -0
- package/assurance/cli-ui.js +617 -0
- package/assurance/guardefi-public.js +66 -0
- package/assurance/release-policy.js +212 -0
- package/core/behavioral-diff.js +555 -0
- package/core/canonical-json.js +9 -0
- package/core/engine/abi.js +99 -0
- package/core/engine/assumptions.json +86 -0
- package/core/engine/bytecode.js +119 -0
- package/core/engine/callee-binding.js +140 -0
- package/core/engine/deployment.js +175 -0
- package/core/engine/external-effects.js +40 -0
- package/core/engine/harness.js +503 -0
- package/core/engine/index.js +638 -0
- package/core/engine/integrity.js +124 -0
- package/core/engine/model.js +369 -0
- package/core/engine/policy.js +59 -0
- package/core/engine/proof-certificate.js +69 -0
- package/core/engine/provenance.js +354 -0
- package/core/engine/screen.js +117 -0
- package/core/engine/upgrade-authority.js +327 -0
- package/core/generic.js +334 -0
- package/core/hardhat.js +114 -0
- package/core/isl/behavior-profile.js +169 -0
- package/core/isl/behavior-solidity.js +267 -0
- package/core/isl/behavior-storage.js +151 -0
- package/core/isl/behavior-verify.js +223 -0
- package/core/isl/getter-profile.js +149 -0
- package/core/isl/vendor/NOTICE.md +38 -0
- package/core/isl/vendor/isl-parser.cjs +5872 -0
- package/core/isl-compiler.js +19 -0
- package/core/isl-firewall.js +1396 -0
- package/core/keccak.js +89 -0
- package/core/prove.js +376 -0
- package/core/release-receipt.js +793 -0
- package/core/rpc.js +74 -0
- package/core/tools.js +129 -0
- package/core/verdict-policy.js +195 -0
- package/docs/public-cli.md +32 -0
- package/package.json +29 -4
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// `guardefi storage`: 32-byte visual storage slot inspector & upgrade layout diff.
|
|
4
|
+
// Replaces manual `forge inspect <Contract> storage-layout` eye-balling with exact
|
|
5
|
+
// byte-range packing bars, ERC-7201 namespace discovery, and collision detection.
|
|
6
|
+
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
const { isName, cast } = require('../core/tools');
|
|
10
|
+
const { isHardhat, hardhatWorkspace } = require('../core/hardhat');
|
|
11
|
+
const { namespaces } = require('../core/engine/model');
|
|
12
|
+
const { normalizeProjectPos } = require('./cli-project-paths');
|
|
13
|
+
const { ensureBuilt, loadBuiltArtifact } = require('./cli-check');
|
|
14
|
+
const { suggestClosest } = require('./cli-ui');
|
|
15
|
+
|
|
16
|
+
const ALLOWED_FLAGS = new Set(['json', 'pretty', 'no-color', 'quiet']);
|
|
17
|
+
|
|
18
|
+
function normalizeTypeShape(typeEntry, types) {
|
|
19
|
+
if (!typeEntry) return 'unknown';
|
|
20
|
+
if (/^contract\s+/.test(typeEntry.label || '')) return 'address';
|
|
21
|
+
if (typeEntry.label === 'address payable') return 'address';
|
|
22
|
+
if (typeEntry.encoding === 'mapping') {
|
|
23
|
+
const k = normalizeTypeShape(types[typeEntry.key], types);
|
|
24
|
+
const v = normalizeTypeShape(types[typeEntry.value], types);
|
|
25
|
+
return `mapping(${k} => ${v})`;
|
|
26
|
+
}
|
|
27
|
+
return typeEntry.label || 'unknown';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function extractSlotMap(artifact) {
|
|
31
|
+
const layout = artifact?.storageLayout || { storage: [], types: {} };
|
|
32
|
+
const types = layout.types || {};
|
|
33
|
+
const entries = (layout.storage || []).map((item) => {
|
|
34
|
+
const t = types[item.type] || {};
|
|
35
|
+
const slot = BigInt(item.slot);
|
|
36
|
+
const offset = Number(item.offset || 0);
|
|
37
|
+
const bytes = Number(t.numberOfBytes || 32);
|
|
38
|
+
const spanSlots = bytes > 32 ? Math.ceil(bytes / 32) : 1;
|
|
39
|
+
return {
|
|
40
|
+
label: item.label,
|
|
41
|
+
slot: slot.toString(10),
|
|
42
|
+
slotNum: slot <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(slot) : null,
|
|
43
|
+
offset,
|
|
44
|
+
bytes: Math.min(bytes, 32),
|
|
45
|
+
totalBytes: bytes,
|
|
46
|
+
spanSlots,
|
|
47
|
+
encoding: t.encoding || 'inplace',
|
|
48
|
+
typeLabel: t.label || item.type,
|
|
49
|
+
shape: normalizeTypeShape(t, types),
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Group by starting slot for visual 32-byte packing bars
|
|
54
|
+
const bySlot = new Map();
|
|
55
|
+
for (const e of entries) {
|
|
56
|
+
if (!bySlot.has(e.slot)) bySlot.set(e.slot, []);
|
|
57
|
+
bySlot.get(e.slot).push(e);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const slots = [];
|
|
61
|
+
for (const [slotKey, vars] of bySlot.entries()) {
|
|
62
|
+
vars.sort((a, b) => a.offset - b.offset);
|
|
63
|
+
const usedBytes = vars.reduce((acc, v) => Math.max(acc, v.offset + v.bytes), 0);
|
|
64
|
+
const freeBytes = Math.max(0, 32 - usedBytes);
|
|
65
|
+
slots.push({
|
|
66
|
+
slot: slotKey,
|
|
67
|
+
usedBytes: Math.min(32, usedBytes),
|
|
68
|
+
freeBytes,
|
|
69
|
+
packed: vars.length > 1,
|
|
70
|
+
packable: freeBytes > 0 && vars.every((v) => v.encoding === 'inplace' && v.totalBytes < 32),
|
|
71
|
+
variables: vars,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const wastedPackableBytes = slots.filter((s) => s.packable).reduce((sum, s) => sum + s.freeBytes, 0);
|
|
76
|
+
return {
|
|
77
|
+
totalVariables: entries.length,
|
|
78
|
+
totalSlots: slots.reduce((sum, s) => sum + Math.max(...s.variables.map((v) => v.spanSlots)), 0),
|
|
79
|
+
packedSlots: slots.filter((s) => s.packed).length,
|
|
80
|
+
freePackableBytes: wastedPackableBytes,
|
|
81
|
+
entries,
|
|
82
|
+
slots,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function diffStorageMaps(baseName, baseMap, candName, candMap) {
|
|
87
|
+
const deltas = [];
|
|
88
|
+
const collisions = [];
|
|
89
|
+
|
|
90
|
+
const maxBasePos = baseMap.entries.reduce((acc, e) => {
|
|
91
|
+
const s = BigInt(e.slot);
|
|
92
|
+
const end = e.offset + e.totalBytes;
|
|
93
|
+
if (!acc || s > acc.slot || (s === acc.slot && end > acc.end)) return { slot: s, end };
|
|
94
|
+
return acc;
|
|
95
|
+
}, null);
|
|
96
|
+
|
|
97
|
+
const baseByName = new Map(baseMap.entries.map((e) => [e.label, e]));
|
|
98
|
+
const candByName = new Map(candMap.entries.map((e) => [e.label, e]));
|
|
99
|
+
const candByCoord = new Map(candMap.entries.map((e) => [`${e.slot}:${e.offset}`, e]));
|
|
100
|
+
const matchedCandCoords = new Set();
|
|
101
|
+
|
|
102
|
+
for (const b of baseMap.entries) {
|
|
103
|
+
const coord = `${b.slot}:${b.offset}`;
|
|
104
|
+
const c = candByCoord.get(coord);
|
|
105
|
+
if (!c) {
|
|
106
|
+
const movedTo = candByName.get(b.label);
|
|
107
|
+
const detail = movedTo
|
|
108
|
+
? `Variable \`${b.label}\` moved from slot ${b.slot} (offset ${b.offset}) to slot ${movedTo.slot} (offset ${movedTo.offset}).`
|
|
109
|
+
: `Variable \`${b.label}\` (${b.typeLabel}) at slot ${b.slot} (offset ${b.offset}) was removed or overwritten.`;
|
|
110
|
+
const row = { status: 'COLLISION', slot: b.slot, offset: b.offset, variable: b.label, before: b.typeLabel, after: movedTo ? `${movedTo.typeLabel} @ slot ${movedTo.slot}` : 'removed', detail };
|
|
111
|
+
deltas.push(row);
|
|
112
|
+
collisions.push(row);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
matchedCandCoords.add(coord);
|
|
116
|
+
const shiftedBaseTarget = b.label !== c.label ? candByName.get(b.label) : null;
|
|
117
|
+
const displacedFromBase = b.label !== c.label ? baseByName.get(c.label) : null;
|
|
118
|
+
if (b.shape !== c.shape || b.totalBytes !== c.totalBytes) {
|
|
119
|
+
const detail = `Slot ${b.slot} (offset ${b.offset}) changed type from \`${b.label}: ${b.typeLabel}\` (${b.totalBytes}B) to \`${c.label}: ${c.typeLabel}\` (${c.totalBytes}B).`;
|
|
120
|
+
const row = { status: 'COLLISION', slot: b.slot, offset: b.offset, variable: c.label, before: `${b.label}: ${b.typeLabel}`, after: `${c.label}: ${c.typeLabel}`, detail };
|
|
121
|
+
deltas.push(row);
|
|
122
|
+
collisions.push(row);
|
|
123
|
+
} else if (shiftedBaseTarget || displacedFromBase) {
|
|
124
|
+
const detail = shiftedBaseTarget
|
|
125
|
+
? `Slot ${b.slot} (offset ${b.offset}) replaced \`${b.label}\` with \`${c.label}\`, shifting \`${b.label}\` to slot ${shiftedBaseTarget.slot} (offset ${shiftedBaseTarget.offset}).`
|
|
126
|
+
: `Slot ${b.slot} (offset ${b.offset}) overwrote \`${b.label}\` with \`${c.label}\` (moved from slot ${displacedFromBase.slot}).`;
|
|
127
|
+
const row = { status: 'COLLISION', slot: b.slot, offset: b.offset, variable: c.label, before: `${b.label}: ${b.typeLabel}`, after: `${c.label}: ${c.typeLabel}`, detail };
|
|
128
|
+
deltas.push(row);
|
|
129
|
+
collisions.push(row);
|
|
130
|
+
} else if (b.label !== c.label) {
|
|
131
|
+
deltas.push({
|
|
132
|
+
status: 'RENAMED',
|
|
133
|
+
slot: b.slot,
|
|
134
|
+
offset: b.offset,
|
|
135
|
+
variable: c.label,
|
|
136
|
+
before: `${b.label}: ${b.typeLabel}`,
|
|
137
|
+
after: `${c.label}: ${c.typeLabel}`,
|
|
138
|
+
detail: `Slot ${b.slot} (offset ${b.offset}) renamed \`${b.label}\` -> \`${c.label}\` (same storage shape \`${b.shape}\`).`,
|
|
139
|
+
});
|
|
140
|
+
} else {
|
|
141
|
+
deltas.push({
|
|
142
|
+
status: 'UNCHANGED',
|
|
143
|
+
slot: b.slot,
|
|
144
|
+
offset: b.offset,
|
|
145
|
+
variable: b.label,
|
|
146
|
+
before: b.typeLabel,
|
|
147
|
+
after: c.typeLabel,
|
|
148
|
+
detail: `Preserved \`${b.label}\` (${b.typeLabel}) at slot ${b.slot}${b.offset ? ` +${b.offset}B` : ''}.`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const c of candMap.entries) {
|
|
154
|
+
const coord = `${c.slot}:${c.offset}`;
|
|
155
|
+
if (matchedCandCoords.has(coord)) continue;
|
|
156
|
+
const cSlot = BigInt(c.slot);
|
|
157
|
+
const isSafeAppend = !maxBasePos ||
|
|
158
|
+
cSlot > maxBasePos.slot ||
|
|
159
|
+
(cSlot === maxBasePos.slot && c.offset >= maxBasePos.end);
|
|
160
|
+
if (isSafeAppend) {
|
|
161
|
+
const packedTail = maxBasePos && cSlot === maxBasePos.slot;
|
|
162
|
+
deltas.push({
|
|
163
|
+
status: packedTail ? 'PACKED_APPEND' : 'APPENDED',
|
|
164
|
+
slot: c.slot,
|
|
165
|
+
offset: c.offset,
|
|
166
|
+
variable: c.label,
|
|
167
|
+
before: null,
|
|
168
|
+
after: c.typeLabel,
|
|
169
|
+
detail: packedTail
|
|
170
|
+
? `Safely packed new \`${c.label}\` (${c.typeLabel}, ${c.bytes}B) into unused tail of slot ${c.slot} at offset ${c.offset}.`
|
|
171
|
+
: `Safely appended \`${c.label}\` (${c.typeLabel}) at new slot ${c.slot}.`,
|
|
172
|
+
});
|
|
173
|
+
} else {
|
|
174
|
+
const row = {
|
|
175
|
+
status: 'COLLISION',
|
|
176
|
+
slot: c.slot,
|
|
177
|
+
offset: c.offset,
|
|
178
|
+
variable: c.label,
|
|
179
|
+
before: 'existing layout region',
|
|
180
|
+
after: `${c.label}: ${c.typeLabel}`,
|
|
181
|
+
detail: `Inserted \`${c.label}\` (${c.typeLabel}) inside ${baseName}'s active storage range at slot ${c.slot} (offset ${c.offset}).`,
|
|
182
|
+
};
|
|
183
|
+
deltas.push(row);
|
|
184
|
+
collisions.push(row);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
compatible: collisions.length === 0,
|
|
190
|
+
verdict: collisions.length === 0 ? 'SHIP' : 'NO_SHIP',
|
|
191
|
+
collisions,
|
|
192
|
+
deltas,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function runStorage({ pos: rawPos = [], opt = {} }) {
|
|
197
|
+
const validFlags = [...ALLOWED_FLAGS].map((f) => `--${f}`);
|
|
198
|
+
for (const k of Object.keys(opt)) {
|
|
199
|
+
if (!ALLOWED_FLAGS.has(k)) {
|
|
200
|
+
const hint = suggestClosest(`--${k}`, validFlags);
|
|
201
|
+
throw new Error(`Unsupported option --${k} on \`guardefi storage\`${hint ? ` (did you mean ${hint}?)` : ''}.`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const normalized = normalizeProjectPos('storage', rawPos, process.cwd());
|
|
206
|
+
if (normalized.length < 2 || normalized.length > 3) {
|
|
207
|
+
throw new Error('Usage: guardefi storage [projectDir] <Contract> [CandidateContract]');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const [projectArg, firstContract, secondContract] = normalized;
|
|
211
|
+
if (!isName(firstContract) || (secondContract && !isName(secondContract))) {
|
|
212
|
+
throw new Error('Contract names must be valid Solidity identifiers.');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const projectRoot = path.resolve(projectArg);
|
|
216
|
+
if (!fs.existsSync(projectRoot) || !fs.statSync(projectRoot).isDirectory()) {
|
|
217
|
+
throw new Error(`Project directory not found: ${projectArg}`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const names = secondContract ? [firstContract, secondContract] : [firstContract];
|
|
221
|
+
const hhWorkspace = isHardhat(projectRoot) && !fs.existsSync(path.join(projectRoot, 'foundry.toml'))
|
|
222
|
+
? hardhatWorkspace(projectRoot, names)
|
|
223
|
+
: null;
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
const buildRoot = hhWorkspace?.root || projectRoot;
|
|
227
|
+
await ensureBuilt(buildRoot, names, opt, projectRoot);
|
|
228
|
+
|
|
229
|
+
const firstArt = loadBuiltArtifact(buildRoot, firstContract);
|
|
230
|
+
const firstMap = extractSlotMap(firstArt);
|
|
231
|
+
|
|
232
|
+
let erc7201 = [];
|
|
233
|
+
try {
|
|
234
|
+
const ns = await namespaces(buildRoot, cast);
|
|
235
|
+
erc7201 = ns.map((n) => ({
|
|
236
|
+
id: n.id,
|
|
237
|
+
struct: n.name,
|
|
238
|
+
baseSlot: '0x' + n.base.toString(16).padStart(64, '0'),
|
|
239
|
+
members: (n.type?.members || []).map((m) => ({ label: m.label, relSlot: m.slot, offset: m.offset, kind: m.type?.label || m.type?.kind })),
|
|
240
|
+
}));
|
|
241
|
+
} catch {
|
|
242
|
+
erc7201 = [];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (!secondContract) {
|
|
246
|
+
return {
|
|
247
|
+
report: {
|
|
248
|
+
schema: 'guardefi.storage.v1',
|
|
249
|
+
mode: 'inspect',
|
|
250
|
+
project: projectRoot,
|
|
251
|
+
contract: firstContract,
|
|
252
|
+
layout: firstMap,
|
|
253
|
+
erc7201,
|
|
254
|
+
},
|
|
255
|
+
exitCode: 0,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const secondArt = loadBuiltArtifact(buildRoot, secondContract);
|
|
260
|
+
const secondMap = extractSlotMap(secondArt);
|
|
261
|
+
const comparison = diffStorageMaps(firstContract, firstMap, secondContract, secondMap);
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
report: {
|
|
265
|
+
schema: 'guardefi.storage.v1',
|
|
266
|
+
mode: 'diff',
|
|
267
|
+
project: projectRoot,
|
|
268
|
+
baseline: firstContract,
|
|
269
|
+
candidate: secondContract,
|
|
270
|
+
compatible: comparison.compatible,
|
|
271
|
+
verdict: comparison.verdict,
|
|
272
|
+
baselineLayout: firstMap,
|
|
273
|
+
candidateLayout: secondMap,
|
|
274
|
+
erc7201,
|
|
275
|
+
collisions: comparison.collisions,
|
|
276
|
+
deltas: comparison.deltas,
|
|
277
|
+
},
|
|
278
|
+
exitCode: comparison.compatible ? 0 : 1,
|
|
279
|
+
};
|
|
280
|
+
} finally {
|
|
281
|
+
hhWorkspace?.cleanup();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
module.exports = {
|
|
286
|
+
runStorage,
|
|
287
|
+
extractSlotMap,
|
|
288
|
+
diffStorageMaps,
|
|
289
|
+
};
|