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.
@@ -0,0 +1,109 @@
1
+ export type Severity = "breaking" | "risky" | "compatible" | "info";
2
+ export declare const SEVERITIES: readonly Severity[];
3
+ export interface Param {
4
+ name: string;
5
+ type: string;
6
+ }
7
+ export interface Fn {
8
+ name: string;
9
+ doc: string;
10
+ inputs: Param[];
11
+ output: string;
12
+ /** Custom types referenced directly by the signature. */
13
+ refs: string[];
14
+ }
15
+ export interface StructType {
16
+ kind: "struct";
17
+ name: string;
18
+ doc: string;
19
+ fields: Param[];
20
+ refs: string[];
21
+ }
22
+ export interface UnionType {
23
+ kind: "union";
24
+ name: string;
25
+ doc: string;
26
+ cases: {
27
+ name: string;
28
+ types: string[];
29
+ }[];
30
+ refs: string[];
31
+ }
32
+ export interface EnumType {
33
+ kind: "enum" | "error";
34
+ name: string;
35
+ doc: string;
36
+ cases: {
37
+ name: string;
38
+ value: number;
39
+ }[];
40
+ refs: string[];
41
+ }
42
+ export type CustomType = StructType | UnionType | EnumType;
43
+ export interface EventParam {
44
+ name: string;
45
+ type: string;
46
+ location: "topic" | "data";
47
+ }
48
+ export interface EventSpec {
49
+ name: string;
50
+ doc: string;
51
+ /** Fixed symbols published before the topic params, e.g. ["deposited"]. */
52
+ topics: string[];
53
+ params: EventParam[];
54
+ /** How data params are encoded: a map keyed by name, a vector, or one value. */
55
+ format: "map" | "vec" | "single_value";
56
+ refs: string[];
57
+ }
58
+ export interface ContractInterface {
59
+ source: string;
60
+ wasmHash: string;
61
+ functions: Fn[];
62
+ types: CustomType[];
63
+ events: EventSpec[];
64
+ meta: Record<string, string>;
65
+ envMeta: {
66
+ protocol?: number;
67
+ preRelease?: number;
68
+ };
69
+ }
70
+ export interface Change {
71
+ severity: Severity;
72
+ rule: string;
73
+ /** Where the change is, e.g. `fn deposit(amount)` or `struct Position.fee`. */
74
+ path: string;
75
+ message: string;
76
+ before?: string;
77
+ after?: string;
78
+ /** Functions affected through a custom type. */
79
+ usedBy?: string[];
80
+ /** Set when the change is listed in the allow list; holds the stated reason. */
81
+ allowed?: string;
82
+ }
83
+ export interface Report {
84
+ schema: 1;
85
+ old: {
86
+ source: string;
87
+ wasmHash: string;
88
+ };
89
+ new: {
90
+ source: string;
91
+ wasmHash: string;
92
+ };
93
+ verdict: Exclude<Severity, "info">;
94
+ /** Counts exclude allowed changes. */
95
+ summary: Record<Severity, number>;
96
+ allowed?: number;
97
+ changes: Change[];
98
+ }
99
+ /** A contract interface saved to a file, so it can be compared without the Wasm or the network. */
100
+ export interface Snapshot extends ContractInterface {
101
+ specguardSnapshot: 1;
102
+ }
103
+ export interface AllowEntry {
104
+ /** Exact change path as printed in a report, e.g. `fn last_action`. */
105
+ path: string;
106
+ /** Optional rule id; when set, only that rule at that path is allowed. */
107
+ rule?: string;
108
+ reason: string;
109
+ }
package/dist/model.js ADDED
@@ -0,0 +1,5 @@
1
+ // Normalized view of a contract interface, independent of the XDR shapes.
2
+ // Both sides of a comparison are reduced to this before any rule runs.
3
+ // Types are kept as their rendered form (`Option<Position>`), which is what
4
+ // rules compare and what reports show.
5
+ export const SEVERITIES = ["breaking", "risky", "compatible", "info"];
@@ -0,0 +1,2 @@
1
+ import type { Report } from "../model.ts";
2
+ export declare function renderMarkdown(r: Report): string;
@@ -0,0 +1,46 @@
1
+ // GitHub-flavoured markdown, used for job summaries and PR comments.
2
+ export function renderMarkdown(r) {
3
+ const s = r.summary;
4
+ const lines = [
5
+ `### SpecGuard: ${r.verdict}`,
6
+ "",
7
+ `| | Source | Wasm hash |`,
8
+ `|---|---|---|`,
9
+ `| old | ${code(r.old.source)} | ${code(r.old.wasmHash.slice(0, 16))} |`,
10
+ `| new | ${code(r.new.source)} | ${code(r.new.wasmHash.slice(0, 16))} |`,
11
+ "",
12
+ `${s.breaking} breaking, ${s.risky} risky, ${s.compatible} compatible, ${s.info} info${r.allowed ? `, ${r.allowed} allowed` : ""}`,
13
+ "",
14
+ ];
15
+ if (r.changes.length === 0) {
16
+ lines.push("No interface changes.");
17
+ }
18
+ else {
19
+ lines.push("| Severity | Where | Change | Detail |", "|---|---|---|---|");
20
+ for (const c of r.changes) {
21
+ const sev = c.allowed ? `${c.severity} (allowed)` : c.severity;
22
+ lines.push(`| ${sev} | ${code(c.path)} | ${esc(c.message)} | ${detail(c)} |`);
23
+ }
24
+ }
25
+ return lines.join("\n") + "\n";
26
+ }
27
+ function detail(c) {
28
+ const parts = [];
29
+ if (c.before !== undefined && c.after !== undefined)
30
+ parts.push(`${code(c.before)} → ${code(c.after)}`);
31
+ else if (c.before !== undefined)
32
+ parts.push(`was ${code(c.before)}`);
33
+ else if (c.after !== undefined)
34
+ parts.push(`+ ${code(c.after)}`);
35
+ if (c.usedBy?.length)
36
+ parts.push(`used by ${c.usedBy.map(code).join(", ")}`);
37
+ if (c.allowed)
38
+ parts.push(`allowed: ${esc(c.allowed)}`);
39
+ return parts.join("<br>");
40
+ }
41
+ function code(s) {
42
+ return "`" + s.replace(/`/g, "'").replace(/\|/g, "\\|") + "`";
43
+ }
44
+ function esc(s) {
45
+ return s.replace(/\|/g, "\\|");
46
+ }
@@ -0,0 +1,2 @@
1
+ import type { Report } from "../model.ts";
2
+ export declare function renderText(r: Report, color: boolean): string;
@@ -0,0 +1,49 @@
1
+ const LABEL = {
2
+ breaking: "BREAKING",
3
+ risky: "RISKY",
4
+ compatible: "OK",
5
+ info: "info",
6
+ };
7
+ const COLOR = {
8
+ breaking: "\x1b[31m",
9
+ risky: "\x1b[33m",
10
+ compatible: "\x1b[32m",
11
+ info: "\x1b[2m",
12
+ };
13
+ export function renderText(r, color) {
14
+ const paint = (s, text) => (color ? `${COLOR[s]}${text}\x1b[0m` : text);
15
+ const dim = (text) => (color ? `\x1b[2m${text}\x1b[0m` : text);
16
+ const lines = [];
17
+ const width = Math.max(r.old.source.length, r.new.source.length);
18
+ lines.push(`old ${r.old.source.padEnd(width)} ${dim(short(r.old.wasmHash))}`);
19
+ lines.push(`new ${r.new.source.padEnd(width)} ${dim(short(r.new.wasmHash))}`);
20
+ lines.push("");
21
+ if (r.changes.length === 0) {
22
+ lines.push("no interface changes");
23
+ }
24
+ else {
25
+ const pathWidth = Math.min(36, Math.max(...r.changes.map((c) => c.path.length)));
26
+ for (const c of r.changes) {
27
+ const label = c.allowed ? dim("ALLOWED".padEnd(9)) : paint(c.severity, LABEL[c.severity].padEnd(9));
28
+ lines.push(`${label} ${c.path.padEnd(pathWidth)} ${c.message}`);
29
+ const detail = c.before !== undefined && c.after !== undefined ? `${c.before} -> ${c.after}` : c.before !== undefined ? `- ${c.before}` : c.after !== undefined ? `+ ${c.after}` : undefined;
30
+ if (detail)
31
+ lines.push(dim(`${" ".repeat(10)}${detail}`));
32
+ if (c.usedBy?.length) {
33
+ lines.push(dim(`${" ".repeat(10)}used by ${c.usedBy.join(", ")}`));
34
+ }
35
+ if (c.allowed) {
36
+ lines.push(dim(`${" ".repeat(10)}allowed (${c.severity}): ${c.allowed}`));
37
+ }
38
+ }
39
+ }
40
+ const s = r.summary;
41
+ lines.push("");
42
+ const allowed = r.allowed ? `, ${r.allowed} allowed` : "";
43
+ lines.push(`${s.breaking} breaking, ${s.risky} risky, ${s.compatible} compatible, ${s.info} info${allowed}`);
44
+ lines.push(`result: ${paint(r.verdict, r.verdict)}`);
45
+ return lines.join("\n") + "\n";
46
+ }
47
+ function short(hash) {
48
+ return `${hash.slice(0, 8)}..${hash.slice(-4)}`;
49
+ }
package/dist/web.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { ContractInterface, Report } from "./model.ts";
2
+ export declare const RPC_URLS: Record<string, string>;
3
+ export type Side = {
4
+ file: File;
5
+ } | {
6
+ id: string;
7
+ rpcUrl: string;
8
+ network: string;
9
+ };
10
+ export declare function loadSide(side: Side): Promise<ContractInterface>;
11
+ export declare function compareSides(oldSide: Side, newSide: Side, allowJson?: string): Promise<Report>;
package/dist/web.js ADDED
@@ -0,0 +1,32 @@
1
+ // Browser entry for the viewer: the same engine as the CLI, bundled by
2
+ // `npm run build:web` into viewer/engine.js. Nothing here touches the file
3
+ // system, and the only network calls are read-only RPC requests.
4
+ import { Server } from "@stellar/stellar-sdk/rpc";
5
+ import { compare } from "./compare/diff.js";
6
+ import { applyAllowList, parseAllowList } from "./compare/allow.js";
7
+ import { interfaceFromWasm } from "./extract/spec.js";
8
+ export const RPC_URLS = {
9
+ testnet: "https://soroban-testnet.stellar.org",
10
+ futurenet: "https://rpc-futurenet.stellar.org",
11
+ };
12
+ const CONTRACT_ID = /^C[A-Z2-7]{55}$/;
13
+ const WASM_HASH = /^[0-9a-f]{64}$/i;
14
+ export async function loadSide(side) {
15
+ if ("file" in side) {
16
+ const bytes = new Uint8Array(await side.file.arrayBuffer());
17
+ return interfaceFromWasm(bytes, side.file.name);
18
+ }
19
+ const id = side.id.trim();
20
+ if (!CONTRACT_ID.test(id) && !WASM_HASH.test(id))
21
+ throw new Error(`"${id}" is not a contract id (C...) or a Wasm hash`);
22
+ const server = new Server(side.rpcUrl, { allowHttp: side.rpcUrl.startsWith("http://") });
23
+ const bytes = CONTRACT_ID.test(id) ? await server.getContractWasmByContractId(id) : await server.getContractWasmByHash(id, "hex");
24
+ return interfaceFromWasm(new Uint8Array(bytes), `${side.network}:${id}`);
25
+ }
26
+ export async function compareSides(oldSide, newSide, allowJson) {
27
+ const [a, b] = await Promise.all([loadSide(oldSide), loadSide(newSide)]);
28
+ const report = compare(a, b);
29
+ if (!allowJson?.trim())
30
+ return report;
31
+ return applyAllowList(report, parseAllowList(JSON.parse(allowJson), "allow list")).report;
32
+ }
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "stellar-specguard",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Interface compatibility checker for Soroban contracts. Runs as `specguard` or `stellar specguard`.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "bin": {
8
+ "specguard": "dist/cli.js",
9
+ "stellar-specguard": "dist/cli.js"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "engines": {
17
+ "node": ">=20"
18
+ },
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.build.json",
21
+ "typecheck": "tsc --noEmit",
22
+ "test": "node --test \"test/**/*.test.ts\"",
23
+ "specguard": "node src/cli.ts",
24
+ "prepublishOnly": "npm run typecheck && npm test && npm run build",
25
+ "build:web": "esbuild src/web.ts --bundle --format=esm --platform=browser --target=es2022 --minify --legal-comments=external --outfile=viewer/engine.js"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/devgoestobar/SpecGuard.git"
30
+ },
31
+ "homepage": "https://github.com/devgoestobar/SpecGuard#readme",
32
+ "bugs": {
33
+ "url": "https://github.com/devgoestobar/SpecGuard/issues"
34
+ },
35
+ "keywords": [
36
+ "stellar",
37
+ "soroban",
38
+ "smart-contracts",
39
+ "compatibility",
40
+ "ci",
41
+ "stellar-cli-plugin"
42
+ ],
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "dependencies": {
47
+ "@noble/hashes": "^2.4.0",
48
+ "@stellar/stellar-sdk": "^17.4.0"
49
+ },
50
+ "devDependencies": {
51
+ "@types/node": "^22.20.5",
52
+ "esbuild": "^0.28.2",
53
+ "typescript": "^7.0.2"
54
+ }
55
+ }