stellar-specguard 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +163 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +135 -0
- package/dist/compare/allow.d.ts +6 -0
- package/dist/compare/allow.js +40 -0
- package/dist/compare/diff.d.ts +2 -0
- package/dist/compare/diff.js +388 -0
- package/dist/extract/source.d.ts +15 -0
- package/dist/extract/source.js +53 -0
- package/dist/extract/spec.d.ts +2 -0
- package/dist/extract/spec.js +96 -0
- package/dist/extract/types.d.ts +7 -0
- package/dist/extract/types.js +53 -0
- package/dist/extract/wasm.d.ts +4 -0
- package/dist/extract/wasm.js +21 -0
- package/dist/model.d.ts +109 -0
- package/dist/model.js +5 -0
- package/dist/report/markdown.d.ts +2 -0
- package/dist/report/markdown.js +46 -0
- package/dist/report/text.d.ts +2 -0
- package/dist/report/text.js +49 -0
- package/dist/web.d.ts +11 -0
- package/dist/web.js +32 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SpecGuard contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,164 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SpecGuard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Interface compatibility checker for Soroban contracts.
|
|
4
|
+
|
|
5
|
+
A Soroban contract can replace its Wasm and keep the same address. That is
|
|
6
|
+
convenient, but the new code can quietly change the public interface: a
|
|
7
|
+
removed function, a different argument type, a changed struct. Apps, generated
|
|
8
|
+
bindings and other contracts that call it then break after the upgrade.
|
|
9
|
+
|
|
10
|
+
SpecGuard compares the contract spec embedded in two Wasm builds (or two
|
|
11
|
+
deployed contracts) and reports every interface change as **breaking**,
|
|
12
|
+
**risky** or **compatible**. It exits non-zero on breaking changes, so it can
|
|
13
|
+
gate a release in CI.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install -g stellar-specguard
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This installs two commands: `specguard`, and `stellar-specguard`, which the
|
|
22
|
+
[Stellar CLI](https://developers.stellar.org/docs/tools/cli/plugins) picks up
|
|
23
|
+
as a plugin, so `stellar specguard diff ...` works next to the commands you
|
|
24
|
+
already use. Node.js 20 or newer.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
# two local builds
|
|
30
|
+
specguard diff old.wasm new.wasm
|
|
31
|
+
|
|
32
|
+
# a deployed contract against a new build
|
|
33
|
+
specguard diff CBQ...XYZ target/wasm32v1-none/release/app.wasm --network testnet
|
|
34
|
+
|
|
35
|
+
# machine-readable report
|
|
36
|
+
specguard diff old.wasm new.wasm --format json > report.json
|
|
37
|
+
|
|
38
|
+
# print a saved report as markdown
|
|
39
|
+
specguard render report.json --format markdown
|
|
40
|
+
|
|
41
|
+
# save the interface to a file you commit, then compare builds against it
|
|
42
|
+
specguard snapshot target/wasm32v1-none/release/app.wasm --out specguard.snapshot.json
|
|
43
|
+
specguard diff specguard.snapshot.json target/wasm32v1-none/release/app.wasm
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Intentional breaks can be listed, with a reason, in `specguard.json`; see
|
|
47
|
+
[Allowed changes](docs/RULES.md#allowed-changes).
|
|
48
|
+
|
|
49
|
+
Each side can be a `.wasm` file, a contract id, a Wasm hash or a snapshot file. Contract ids and
|
|
50
|
+
hashes are fetched read-only over RPC; `--network testnet` and
|
|
51
|
+
`--network futurenet` have defaults, mainnet needs `--rpc-url`.
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
$ specguard diff vault_v1.wasm vault_v2_breaking.wasm
|
|
55
|
+
old vault_v1.wasm 6be7f445..0528
|
|
56
|
+
new vault_v2_breaking.wasm 7cc398bc..2adf
|
|
57
|
+
|
|
58
|
+
BREAKING fn deposit(amount) argument type changed
|
|
59
|
+
i128 -> u64
|
|
60
|
+
BREAKING fn position return type changed
|
|
61
|
+
Option<Position> -> Position
|
|
62
|
+
BREAKING fn last_action function removed
|
|
63
|
+
- last_action(owner: Address) -> Option<Action>
|
|
64
|
+
BREAKING struct Position.fee field added
|
|
65
|
+
+ i128
|
|
66
|
+
used by position
|
|
67
|
+
BREAKING error VaultError::NotFound error code changed
|
|
68
|
+
1 -> 10
|
|
69
|
+
used by deposit, withdraw
|
|
70
|
+
|
|
71
|
+
5 breaking, 0 risky, 0 compatible, 0 info
|
|
72
|
+
result: breaking
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
| Exit code | Meaning |
|
|
76
|
+
|---|---|
|
|
77
|
+
| 0 | Passed |
|
|
78
|
+
| 1 | Breaking changes found (or risky ones, with `--fail-on risky`) |
|
|
79
|
+
| 2 | Invalid input or runtime error |
|
|
80
|
+
|
|
81
|
+
## GitHub Actions
|
|
82
|
+
|
|
83
|
+
```yaml
|
|
84
|
+
- uses: devgoestobar/SpecGuard@v1
|
|
85
|
+
with:
|
|
86
|
+
old: ${{ vars.CONTRACT_ID }} # deployed contract
|
|
87
|
+
new: target/wasm32v1-none/release/my_contract.wasm
|
|
88
|
+
network: testnet
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The step fails when the new build breaks the interface (`fail-on: risky` to be
|
|
92
|
+
stricter), writes the report to the job summary, and on pull requests posts it
|
|
93
|
+
as a comment that is updated on every push (give the job
|
|
94
|
+
`pull-requests: write`, or set `comment: false`). Outputs: `verdict`,
|
|
95
|
+
`breaking`, `risky`, `report` (path to the JSON report). A complete workflow
|
|
96
|
+
that builds the contract first is in
|
|
97
|
+
[examples/github-actions/specguard.yml](examples/github-actions/specguard.yml).
|
|
98
|
+
|
|
99
|
+
## In the browser
|
|
100
|
+
|
|
101
|
+
[devgoestobar.github.io/SpecGuard/viewer](https://devgoestobar.github.io/SpecGuard/viewer/)
|
|
102
|
+
compares two contract versions with nothing to install: enter two contract
|
|
103
|
+
ids or Wasm hashes (testnet, futurenet, or mainnet through an RPC URL you
|
|
104
|
+
provide) or pick two `.wasm` files. It runs the same engine as the CLI inside
|
|
105
|
+
the page; files are never uploaded. Results can be downloaded as JSON, and a
|
|
106
|
+
comparison of two ids can be shared as a link.
|
|
107
|
+
|
|
108
|
+
The same page opens saved reports (`?report=<url-of-report.json>`, or drop a
|
|
109
|
+
file on it). The engine bundle in `viewer/engine.js` is built with
|
|
110
|
+
`npm run build:web`. Sample reports from the example contracts are in
|
|
111
|
+
[examples/reports](examples/reports).
|
|
112
|
+
|
|
113
|
+

|
|
114
|
+
|
|
115
|
+
## What it checks
|
|
116
|
+
|
|
117
|
+
Functions, arguments, return types, custom types (structs, unions, enums),
|
|
118
|
+
error enums, contract events and contract metadata, all read from the `contractspecv0`,
|
|
119
|
+
`contractmetav0` and `contractenvmetav0` sections of the Wasm. Each change is
|
|
120
|
+
classified as breaking, risky, compatible or info.
|
|
121
|
+
|
|
122
|
+
The full rule list, and the reasoning behind each one, is in
|
|
123
|
+
[docs/RULES.md](docs/RULES.md).
|
|
124
|
+
|
|
125
|
+
## What it does not check
|
|
126
|
+
|
|
127
|
+
- Storage layout or data migrations
|
|
128
|
+
- Business logic or security issues
|
|
129
|
+
- It never signs, submits or executes an upgrade
|
|
130
|
+
|
|
131
|
+
A clean report means the callable interface did not break. It does not mean
|
|
132
|
+
the upgrade is safe in every other respect.
|
|
133
|
+
|
|
134
|
+
## Development
|
|
135
|
+
|
|
136
|
+
Requires Node.js 22.18 or newer.
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
npm install
|
|
140
|
+
npm test
|
|
141
|
+
node src/cli.ts diff fixtures/wasm/vault_v1.wasm fixtures/wasm/vault_v2_breaking.wasm
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`fixtures/contracts` holds a small vault contract and three upgraded versions
|
|
145
|
+
of it (compatible, risky, breaking), plus a pair of contracts whose events
|
|
146
|
+
change between versions. The built Wasm is committed in
|
|
147
|
+
`fixtures/wasm`, so tests do not need Rust. To rebuild it, install the
|
|
148
|
+
`stellar` CLI and run `scripts/build-fixtures.sh`.
|
|
149
|
+
|
|
150
|
+
## Evidence
|
|
151
|
+
|
|
152
|
+
| | |
|
|
153
|
+
|---|---|
|
|
154
|
+
| Comparison engine | [src/compare/diff.ts](src/compare/diff.ts), [src/extract/spec.ts](src/extract/spec.ts), rules in [docs/RULES.md](docs/RULES.md) |
|
|
155
|
+
| Example contracts | [fixtures/contracts](fixtures/contracts): a vault contract and three upgrades (compatible, risky, breaking) |
|
|
156
|
+
| Tests | 51 tests, see [docs/evidence/test-log.txt](docs/evidence/test-log.txt) and the [ci workflow runs](https://github.com/devgoestobar/SpecGuard/actions/workflows/ci.yml) |
|
|
157
|
+
| CI gate | [action.yml](action.yml), [example workflow](examples/github-actions/specguard.yml), and the [action self-test](https://github.com/devgoestobar/SpecGuard/actions/workflows/action.yml), which checks the step fails on the breaking upgrade |
|
|
158
|
+
| Reports | [examples/reports](examples/reports) in text, JSON and markdown |
|
|
159
|
+
| Deployed contract | v1 on testnet, compared against the upgrades: [docs/evidence/testnet](docs/evidence/testnet) |
|
|
160
|
+
| Screenshots | [docs/evidence/screenshots](docs/evidence/screenshots) |
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
MIT
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { basename } from "node:path";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import { applyAllowList, parseAllowList } from "./compare/allow.js";
|
|
7
|
+
import { compare } from "./compare/diff.js";
|
|
8
|
+
import { loadInterface, toSnapshot } from "./extract/source.js";
|
|
9
|
+
import { renderMarkdown } from "./report/markdown.js";
|
|
10
|
+
import { renderText } from "./report/text.js";
|
|
11
|
+
// Installed as `stellar-specguard` too, which the Stellar CLI runs as `stellar specguard`.
|
|
12
|
+
const PROG = basename(process.argv[1] ?? "").startsWith("stellar-specguard") ? "stellar specguard" : "specguard";
|
|
13
|
+
const USAGE = `Usage: ${PROG} diff <old> <new> [options]
|
|
14
|
+
${PROG} snapshot <source> [--out specguard.snapshot.json]
|
|
15
|
+
${PROG} render <report.json> [--format text|markdown]
|
|
16
|
+
|
|
17
|
+
diff compares the public interface of two Soroban contract versions.
|
|
18
|
+
Each side can be a .wasm file, a contract id (C...), a Wasm hash, or a
|
|
19
|
+
snapshot .json written by snapshot.
|
|
20
|
+
|
|
21
|
+
snapshot saves a contract's interface to a file you can commit, so later
|
|
22
|
+
builds can be compared against it without the network.
|
|
23
|
+
|
|
24
|
+
render prints a saved JSON report in another format.
|
|
25
|
+
|
|
26
|
+
Options:
|
|
27
|
+
--format <text|json|markdown>
|
|
28
|
+
Output format (default: text)
|
|
29
|
+
--out <file> Also write the JSON report to a file
|
|
30
|
+
--fail-on <breaking|risky>
|
|
31
|
+
Lowest severity that fails the run (default: breaking)
|
|
32
|
+
--network <name> testnet or futurenet, for contract ids and hashes
|
|
33
|
+
--rpc-url <url> RPC endpoint, overrides --network (needed for mainnet)
|
|
34
|
+
--config <file> Allow list of intentional changes (default: specguard.json if present)
|
|
35
|
+
--no-color Plain text output
|
|
36
|
+
-h, --help Show this help
|
|
37
|
+
|
|
38
|
+
Exit codes:
|
|
39
|
+
0 passed
|
|
40
|
+
1 changes at or above --fail-on found
|
|
41
|
+
2 invalid input or runtime error
|
|
42
|
+
`;
|
|
43
|
+
async function main(argv) {
|
|
44
|
+
const { values, positionals } = parseArgs({
|
|
45
|
+
args: argv,
|
|
46
|
+
allowPositionals: true,
|
|
47
|
+
options: {
|
|
48
|
+
format: { type: "string", default: "text" },
|
|
49
|
+
out: { type: "string" },
|
|
50
|
+
"fail-on": { type: "string", default: "breaking" },
|
|
51
|
+
network: { type: "string" },
|
|
52
|
+
"rpc-url": { type: "string" },
|
|
53
|
+
config: { type: "string" },
|
|
54
|
+
"no-color": { type: "boolean" },
|
|
55
|
+
help: { type: "boolean", short: "h" },
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
if (values.help || positionals.length === 0) {
|
|
59
|
+
process.stdout.write(USAGE);
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
const [command, oldArg, newArg, ...rest] = positionals;
|
|
63
|
+
const format = values.format;
|
|
64
|
+
if (format !== "text" && format !== "json" && format !== "markdown")
|
|
65
|
+
return usageError(`unknown format: ${format}`);
|
|
66
|
+
if (command === "render") {
|
|
67
|
+
if (!oldArg || newArg)
|
|
68
|
+
return usageError("render takes one report file");
|
|
69
|
+
const report = JSON.parse(await readFile(oldArg, "utf8"));
|
|
70
|
+
if (report.schema !== 1)
|
|
71
|
+
throw new Error(`${oldArg}: not a SpecGuard report`);
|
|
72
|
+
process.stdout.write(output(report, format, values["no-color"]));
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
const opts = { network: values.network, rpcUrl: values["rpc-url"] };
|
|
76
|
+
if (command === "snapshot") {
|
|
77
|
+
if (!oldArg || newArg)
|
|
78
|
+
return usageError("snapshot takes one source");
|
|
79
|
+
const out = values.out ?? "specguard.snapshot.json";
|
|
80
|
+
const iface = await loadInterface(oldArg, opts);
|
|
81
|
+
await writeFile(out, JSON.stringify(toSnapshot(iface), null, 2) + "\n");
|
|
82
|
+
process.stdout.write(`wrote ${out}: ${iface.functions.length} functions, ${iface.types.length} types, ${iface.events.length} events\n`);
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
if (command !== "diff")
|
|
86
|
+
return usageError(`unknown command: ${command}`);
|
|
87
|
+
if (!oldArg || !newArg || rest.length)
|
|
88
|
+
return usageError("diff takes exactly two arguments");
|
|
89
|
+
if (values["fail-on"] !== "breaking" && values["fail-on"] !== "risky")
|
|
90
|
+
return usageError(`--fail-on must be breaking or risky`);
|
|
91
|
+
const [a, b] = await Promise.all([loadInterface(oldArg, opts), loadInterface(newArg, opts)]);
|
|
92
|
+
let report = compare(a, b);
|
|
93
|
+
const configFile = values.config ?? (existsSync("specguard.json") ? "specguard.json" : undefined);
|
|
94
|
+
if (configFile) {
|
|
95
|
+
const text = await readFile(configFile, "utf8").catch(() => {
|
|
96
|
+
throw new Error(`cannot read ${configFile}`);
|
|
97
|
+
});
|
|
98
|
+
const result = applyAllowList(report, parseAllowList(JSON.parse(text), configFile));
|
|
99
|
+
report = result.report;
|
|
100
|
+
for (const u of result.unused) {
|
|
101
|
+
process.stderr.write(`warning: ${configFile}: allow entry "${u.path}" did not match any change\n`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (values.out)
|
|
105
|
+
await writeFile(values.out, JSON.stringify(report, null, 2) + "\n");
|
|
106
|
+
process.stdout.write(output(report, format, values["no-color"]));
|
|
107
|
+
const failing = values["fail-on"] === "risky" ? report.verdict !== "compatible" : report.verdict === "breaking";
|
|
108
|
+
return failing ? 1 : 0;
|
|
109
|
+
}
|
|
110
|
+
function output(report, format, noColor) {
|
|
111
|
+
if (format === "json")
|
|
112
|
+
return JSON.stringify(report, null, 2) + "\n";
|
|
113
|
+
if (format === "markdown")
|
|
114
|
+
return renderMarkdown(report);
|
|
115
|
+
const color = !noColor && !process.env.NO_COLOR && process.stdout.isTTY === true;
|
|
116
|
+
return renderText(report, color);
|
|
117
|
+
}
|
|
118
|
+
// RPC failures can arrive as plain objects ({ code, message }) rather than Errors.
|
|
119
|
+
function describe(err) {
|
|
120
|
+
if (err instanceof Error)
|
|
121
|
+
return err.message;
|
|
122
|
+
if (err && typeof err === "object" && "message" in err)
|
|
123
|
+
return String(err.message);
|
|
124
|
+
return typeof err === "object" ? JSON.stringify(err) : String(err);
|
|
125
|
+
}
|
|
126
|
+
function usageError(message) {
|
|
127
|
+
process.stderr.write(`error: ${message}\n\n${USAGE}`);
|
|
128
|
+
return 2;
|
|
129
|
+
}
|
|
130
|
+
main(process.argv.slice(2)).then((code) => {
|
|
131
|
+
process.exitCode = code;
|
|
132
|
+
}, (err) => {
|
|
133
|
+
process.stderr.write(`error: ${describe(err)}\n`);
|
|
134
|
+
process.exitCode = 2;
|
|
135
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Marks intentional changes listed in the allow list. Allowed changes stay in
|
|
2
|
+
// the report, with their reason, but no longer count toward the verdict.
|
|
3
|
+
export function applyAllowList(report, allow) {
|
|
4
|
+
const used = new Set();
|
|
5
|
+
const changes = report.changes.map((c) => {
|
|
6
|
+
const entry = allow.find((a) => a.path === c.path && (!a.rule || a.rule === c.rule));
|
|
7
|
+
if (!entry || c.severity === "info")
|
|
8
|
+
return c;
|
|
9
|
+
used.add(entry);
|
|
10
|
+
return { ...c, allowed: entry.reason };
|
|
11
|
+
});
|
|
12
|
+
const summary = { breaking: 0, risky: 0, compatible: 0, info: 0 };
|
|
13
|
+
let allowed = 0;
|
|
14
|
+
for (const c of changes) {
|
|
15
|
+
if (c.allowed)
|
|
16
|
+
allowed++;
|
|
17
|
+
else
|
|
18
|
+
summary[c.severity]++;
|
|
19
|
+
}
|
|
20
|
+
const verdict = summary.breaking ? "breaking" : summary.risky ? "risky" : "compatible";
|
|
21
|
+
return {
|
|
22
|
+
report: { ...report, changes, summary, allowed, verdict },
|
|
23
|
+
unused: allow.filter((a) => !used.has(a)),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function parseAllowList(json, file) {
|
|
27
|
+
const allow = json?.allow;
|
|
28
|
+
if (allow === undefined)
|
|
29
|
+
return [];
|
|
30
|
+
if (!Array.isArray(allow))
|
|
31
|
+
throw new Error(`${file}: "allow" must be a list`);
|
|
32
|
+
return allow.map((a, i) => {
|
|
33
|
+
if (typeof a?.path !== "string" || typeof a?.reason !== "string" || !a.reason.trim()) {
|
|
34
|
+
throw new Error(`${file}: allow[${i}] needs a "path" and a non-empty "reason"`);
|
|
35
|
+
}
|
|
36
|
+
if (a.rule !== undefined && typeof a.rule !== "string")
|
|
37
|
+
throw new Error(`${file}: allow[${i}].rule must be a string`);
|
|
38
|
+
return { path: a.path, rule: a.rule, reason: a.reason };
|
|
39
|
+
});
|
|
40
|
+
}
|