@mikeargento/bitgraph-player 0.2.1 → 0.4.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/README.md +40 -0
- package/SPEC.md +95 -6
- package/dist/__tests__/check.test.d.ts +2 -0
- package/dist/__tests__/check.test.d.ts.map +1 -0
- package/dist/__tests__/check.test.js +358 -0
- package/dist/__tests__/check.test.js.map +1 -0
- package/dist/__tests__/sig-v2.test.d.ts +2 -0
- package/dist/__tests__/sig-v2.test.d.ts.map +1 -0
- package/dist/__tests__/sig-v2.test.js +266 -0
- package/dist/__tests__/sig-v2.test.js.map +1 -0
- package/dist/check.d.ts +156 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +640 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.js +84 -15
- package/dist/cli.js.map +1 -1
- package/dist/evaluate.d.ts +8 -21
- package/dist/evaluate.d.ts.map +1 -1
- package/dist/evaluate.js +93 -2
- package/dist/evaluate.js.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/play.d.ts +12 -2
- package/dist/play.d.ts.map +1 -1
- package/dist/play.js +39 -4
- package/dist/play.js.map +1 -1
- package/dist/rule.d.ts +0 -13
- package/dist/rule.d.ts.map +1 -1
- package/dist/rule.js +84 -8
- package/dist/rule.js.map +1 -1
- package/dist/sig.d.ts +38 -0
- package/dist/sig.d.ts.map +1 -0
- package/dist/sig.js +154 -0
- package/dist/sig.js.map +1 -0
- package/dist/types.d.ts +21 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/verdict.d.ts +1 -1
- package/dist/verdict.d.ts.map +1 -1
- package/dist/verdict.js +17 -2
- package/dist/verdict.js.map +1 -1
- package/dist-web/verify.html +159 -0
- package/package.json +8 -3
- package/src/__tests__/check.test.ts +417 -0
- package/src/__tests__/fixtures/export-random-043/ethereum-anchors/anchor-after-witness.json +6 -0
- package/src/__tests__/fixtures/export-random-043/ethereum-anchors/anchor-after.json +43 -0
- package/src/__tests__/fixtures/export-random-043/ethereum-anchors/anchor-before-witness.json +6 -0
- package/src/__tests__/fixtures/export-random-043/ethereum-anchors/anchor-before.json +47 -0
- package/src/__tests__/fixtures/export-random-043/proof.json +38 -0
- package/src/__tests__/fixtures/export-random-043/random-043.txt +1 -0
- package/src/__tests__/sig-v2.test.ts +315 -0
- package/src/check.ts +873 -0
- package/src/cli.ts +82 -16
- package/src/evaluate.ts +107 -2
- package/src/index.ts +9 -2
- package/src/play.ts +43 -5
- package/src/rule.ts +93 -9
- package/src/sig.ts +170 -0
- package/src/types.ts +21 -4
- package/src/verdict.ts +18 -2
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Format 2: trustedKeys + signedBy.
|
|
5
|
+
*
|
|
6
|
+
* The invariants under test:
|
|
7
|
+
* - signedBy is TRUE or UNDETERMINED, never FALSE, so not(signedBy)
|
|
8
|
+
* is permanently UNDETERMINED: no open-world negative escapes.
|
|
9
|
+
* - Signature math is derived; the name-to-key binding is declared.
|
|
10
|
+
* - A /1 rule parses and evaluates exactly as before (verdict/1), and
|
|
11
|
+
* /2-only syntax in a /1 rule is a parse error.
|
|
12
|
+
* - Broken signature files are noise, never evidence and never errors.
|
|
13
|
+
*
|
|
14
|
+
* All evidence is synthetic and in-memory. No ledger writes, ever.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { strict as assert } from "node:assert";
|
|
18
|
+
import { createHash, generateKeyPairSync, sign as cryptoSign } from "node:crypto";
|
|
19
|
+
import { test } from "node:test";
|
|
20
|
+
import { evaluate } from "../evaluate.js";
|
|
21
|
+
import { playAudit, claimUsesSignatures } from "../play.js";
|
|
22
|
+
import { parseRule, RuleError } from "../rule.js";
|
|
23
|
+
import { parseSigFile, sigMessage } from "../sig.js";
|
|
24
|
+
import { resolveCast } from "../cast.js";
|
|
25
|
+
import { digestFor, makeAudit } from "./fixtures.js";
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Key + signature helpers (in-memory, per-run keys; verification is the
|
|
29
|
+
// deterministic part under test, not key generation)
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
function ed25519Pair(): { publicKeyB64: string; privateKey: import("node:crypto").KeyObject } {
|
|
33
|
+
const { publicKey, privateKey } = generateKeyPairSync("ed25519");
|
|
34
|
+
const spki = publicKey.export({ format: "der", type: "spki" }) as Buffer;
|
|
35
|
+
// Raw key = last 32 bytes of the SPKI encoding.
|
|
36
|
+
return { publicKeyB64: spki.subarray(spki.length - 32).toString("base64"), privateKey };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function es256Pair(): { publicKeyB64: string; privateKey: import("node:crypto").KeyObject } {
|
|
40
|
+
const { publicKey, privateKey } = generateKeyPairSync("ec", { namedCurve: "P-256" });
|
|
41
|
+
const spki = publicKey.export({ format: "der", type: "spki" }) as Buffer;
|
|
42
|
+
return { publicKeyB64: spki.toString("base64"), privateKey };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function hexOfB64(digestB64: string): string {
|
|
46
|
+
return Buffer.from(digestB64, "base64").toString("hex");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function makeSigBytes(
|
|
50
|
+
alg: "ed25519" | "es256",
|
|
51
|
+
publicKeyB64: string,
|
|
52
|
+
privateKey: import("node:crypto").KeyObject,
|
|
53
|
+
overHex: string
|
|
54
|
+
): Uint8Array {
|
|
55
|
+
const message = sigMessage(overHex);
|
|
56
|
+
const signature =
|
|
57
|
+
alg === "ed25519"
|
|
58
|
+
? cryptoSign(null, message, privateKey)
|
|
59
|
+
: cryptoSign("sha256", message, privateKey);
|
|
60
|
+
return Buffer.from(
|
|
61
|
+
JSON.stringify({
|
|
62
|
+
sig: "bitgraph-sig/1",
|
|
63
|
+
over: `sha256:${overHex}`,
|
|
64
|
+
alg,
|
|
65
|
+
publicKey: publicKeyB64,
|
|
66
|
+
signature: signature.toString("base64"),
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function evidenceOf(...items: Uint8Array[]): Map<string, Uint8Array> {
|
|
72
|
+
const map = new Map<string, Uint8Array>();
|
|
73
|
+
for (const bytes of items) {
|
|
74
|
+
map.set(createHash("sha256").update(bytes).digest("hex"), bytes);
|
|
75
|
+
}
|
|
76
|
+
return map;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const WORK = digestFor("work");
|
|
80
|
+
|
|
81
|
+
function v2Rule(publicKeyB64: string, alg: "ed25519" | "es256", claim: unknown): string {
|
|
82
|
+
return JSON.stringify({
|
|
83
|
+
rule: "bitgraph-player/2",
|
|
84
|
+
id: "test-v2",
|
|
85
|
+
cast: { work: { digest: WORK } },
|
|
86
|
+
world: "closed",
|
|
87
|
+
requires: { ordering: "assumption-dependent" },
|
|
88
|
+
trustedKeys: { mike: { alg, publicKey: publicKeyB64 } },
|
|
89
|
+
claim,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function auditWithWork() {
|
|
94
|
+
return makeAudit({
|
|
95
|
+
proofs: [{ name: "w", digestB64: WORK, epochId: "e1", counter: "1" }],
|
|
96
|
+
partitions: [{ epochId: "e1", members: ["w"] }],
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// Parsing
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
test("signedBy in a /1 rule is a parse error", () => {
|
|
105
|
+
const text = JSON.stringify({
|
|
106
|
+
rule: "bitgraph-player/1",
|
|
107
|
+
id: "r",
|
|
108
|
+
cast: { work: { digest: WORK } },
|
|
109
|
+
world: "closed",
|
|
110
|
+
requires: { ordering: "hash-linked" },
|
|
111
|
+
claim: { signedBy: ["work", "mike"] },
|
|
112
|
+
});
|
|
113
|
+
assert.throws(() => parseRule(text), RuleError);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("trustedKeys in a /1 rule is a parse error", () => {
|
|
117
|
+
const text = JSON.stringify({
|
|
118
|
+
rule: "bitgraph-player/1",
|
|
119
|
+
id: "r",
|
|
120
|
+
cast: { work: { digest: WORK } },
|
|
121
|
+
world: "closed",
|
|
122
|
+
requires: { ordering: "hash-linked" },
|
|
123
|
+
trustedKeys: { mike: { alg: "ed25519", publicKey: "A".repeat(43) + "=" } },
|
|
124
|
+
claim: { exists: "work" },
|
|
125
|
+
});
|
|
126
|
+
assert.throws(() => parseRule(text), RuleError);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("signedBy referencing an undeclared trusted key is a parse error", () => {
|
|
130
|
+
const { publicKeyB64 } = ed25519Pair();
|
|
131
|
+
const text = v2Rule(publicKeyB64, "ed25519", { signedBy: ["work", "ghost"] });
|
|
132
|
+
assert.throws(() => parseRule(text), /does not declare/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("trustedKeys entries are strictly validated", () => {
|
|
136
|
+
for (const bad of [
|
|
137
|
+
{ alg: "rsa", publicKey: "x" },
|
|
138
|
+
{ alg: "ed25519" },
|
|
139
|
+
{ publicKey: "x" },
|
|
140
|
+
{ alg: "ed25519", publicKey: "x", extra: 1 },
|
|
141
|
+
]) {
|
|
142
|
+
const text = JSON.stringify({
|
|
143
|
+
rule: "bitgraph-player/2",
|
|
144
|
+
id: "r",
|
|
145
|
+
cast: { work: { digest: WORK } },
|
|
146
|
+
world: "closed",
|
|
147
|
+
requires: { ordering: "hash-linked" },
|
|
148
|
+
trustedKeys: { mike: bad },
|
|
149
|
+
claim: { exists: "work" },
|
|
150
|
+
});
|
|
151
|
+
assert.throws(() => parseRule(text), RuleError, JSON.stringify(bad));
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("pure-integer trusted key names are rejected", () => {
|
|
156
|
+
const text = JSON.stringify({
|
|
157
|
+
rule: "bitgraph-player/2",
|
|
158
|
+
id: "r",
|
|
159
|
+
cast: { work: { digest: WORK } },
|
|
160
|
+
world: "closed",
|
|
161
|
+
requires: { ordering: "hash-linked" },
|
|
162
|
+
trustedKeys: { "42": { alg: "ed25519", publicKey: "A".repeat(43) + "=" } },
|
|
163
|
+
claim: { exists: "work" },
|
|
164
|
+
});
|
|
165
|
+
assert.throws(() => parseRule(text), /non-digit/);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("claimUsesSignatures walks the tree", () => {
|
|
169
|
+
assert.equal(claimUsesSignatures({ exists: "x" }), false);
|
|
170
|
+
assert.equal(
|
|
171
|
+
claimUsesSignatures({ all: [{ exists: "x" }, { not: { signedBy: ["x", "k"] } }] }),
|
|
172
|
+
true
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// ---------------------------------------------------------------------------
|
|
177
|
+
// Signature file parsing
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
test("parseSigFile accepts a well-formed file and rejects noise", () => {
|
|
181
|
+
const { publicKeyB64, privateKey } = ed25519Pair();
|
|
182
|
+
const good = makeSigBytes("ed25519", publicKeyB64, privateKey, hexOfB64(WORK));
|
|
183
|
+
assert.notEqual(parseSigFile(good), undefined);
|
|
184
|
+
assert.equal(parseSigFile(Buffer.from("not json")), undefined);
|
|
185
|
+
assert.equal(parseSigFile(Buffer.from(JSON.stringify({ sig: "other/1" }))), undefined);
|
|
186
|
+
assert.equal(
|
|
187
|
+
parseSigFile(Buffer.from(JSON.stringify({ sig: "bitgraph-sig/1", over: "x", alg: "rsa" }))),
|
|
188
|
+
undefined
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
// Evaluation: ed25519 and es256
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
for (const alg of ["ed25519", "es256"] as const) {
|
|
197
|
+
test(`signedBy(${alg}): valid signature in evidence evaluates TRUE with derived math`, () => {
|
|
198
|
+
const pair = alg === "ed25519" ? ed25519Pair() : es256Pair();
|
|
199
|
+
const rule = parseRule(v2Rule(pair.publicKeyB64, alg, { signedBy: ["work", "mike"] }));
|
|
200
|
+
const audit = auditWithWork();
|
|
201
|
+
const evidence = evidenceOf(makeSigBytes(alg, pair.publicKeyB64, pair.privateKey, hexOfB64(WORK)));
|
|
202
|
+
const result = playAudit(rule, "0".repeat(64), audit, evidence);
|
|
203
|
+
assert.equal(result.verdict.result, "TRUE");
|
|
204
|
+
assert.equal(result.verdict.verdict, "bitgraph-player-verdict/2");
|
|
205
|
+
const step = result.verdict.derived.find((s) => s.claim.startsWith("signedBy"));
|
|
206
|
+
assert.ok(step !== undefined);
|
|
207
|
+
assert.equal((step as NonNullable<typeof step>).result, "TRUE");
|
|
208
|
+
// The binding is declared, alongside the math being derived.
|
|
209
|
+
const trusted = result.verdict.declared.find((d) => d.assertion === "trusted-key");
|
|
210
|
+
assert.ok(trusted !== undefined);
|
|
211
|
+
assert.equal((trusted as NonNullable<typeof trusted>)["keyName"], "mike");
|
|
212
|
+
assert.equal((trusted as NonNullable<typeof trusted>).verifiedHere, false);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test(`signedBy(${alg}): tampered signature is noise, evaluates UNDETERMINED`, () => {
|
|
216
|
+
const pair = alg === "ed25519" ? ed25519Pair() : es256Pair();
|
|
217
|
+
const rule = parseRule(v2Rule(pair.publicKeyB64, alg, { signedBy: ["work", "mike"] }));
|
|
218
|
+
const bytes = makeSigBytes(alg, pair.publicKeyB64, pair.privateKey, hexOfB64(WORK));
|
|
219
|
+
const parsed = JSON.parse(Buffer.from(bytes).toString("utf8")) as Record<string, string>;
|
|
220
|
+
const sigBytes = Buffer.from(parsed["signature"] as string, "base64");
|
|
221
|
+
sigBytes[0] = (sigBytes[0] as number) ^ 0xff;
|
|
222
|
+
parsed["signature"] = sigBytes.toString("base64");
|
|
223
|
+
const tampered = Buffer.from(JSON.stringify(parsed));
|
|
224
|
+
const result = playAudit(rule, "0".repeat(64), auditWithWork(), evidenceOf(tampered));
|
|
225
|
+
assert.equal(result.verdict.result, "UNDETERMINED");
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
test("a non-P-256 key declared as es256 is undecodable key material: UNDETERMINED, never a verify", () => {
|
|
230
|
+
const { publicKey, privateKey } = generateKeyPairSync("ec", { namedCurve: "secp384r1" });
|
|
231
|
+
const spki = (publicKey.export({ format: "der", type: "spki" }) as Buffer).toString("base64");
|
|
232
|
+
const rule = parseRule(v2Rule(spki, "es256", { signedBy: ["work", "mike"] }));
|
|
233
|
+
const evidence = evidenceOf(makeSigBytes("es256", spki, privateKey, hexOfB64(WORK)));
|
|
234
|
+
const result = playAudit(rule, "0".repeat(64), auditWithWork(), evidence);
|
|
235
|
+
assert.equal(result.verdict.result, "UNDETERMINED");
|
|
236
|
+
const step = result.verdict.derived.find((s) => s.claim.startsWith("signedBy"));
|
|
237
|
+
assert.ok(step !== undefined);
|
|
238
|
+
assert.match(String((step as NonNullable<typeof step>).because["reason"]), /not decodable/);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("signedBy with empty evidence is UNDETERMINED, and not(signedBy) stays UNDETERMINED", () => {
|
|
242
|
+
const { publicKeyB64 } = ed25519Pair();
|
|
243
|
+
const rule = parseRule(v2Rule(publicKeyB64, "ed25519", { not: { signedBy: ["work", "mike"] } }));
|
|
244
|
+
const result = playAudit(rule, "0".repeat(64), auditWithWork());
|
|
245
|
+
// Never FALSE and never TRUE: the open world stays open.
|
|
246
|
+
assert.equal(result.verdict.result, "UNDETERMINED");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("signedBy by the wrong key is UNDETERMINED, never FALSE", () => {
|
|
250
|
+
const right = ed25519Pair();
|
|
251
|
+
const wrong = ed25519Pair();
|
|
252
|
+
const rule = parseRule(v2Rule(right.publicKeyB64, "ed25519", { signedBy: ["work", "mike"] }));
|
|
253
|
+
const evidence = evidenceOf(
|
|
254
|
+
makeSigBytes("ed25519", wrong.publicKeyB64, wrong.privateKey, hexOfB64(WORK))
|
|
255
|
+
);
|
|
256
|
+
const result = playAudit(rule, "0".repeat(64), auditWithWork(), evidence);
|
|
257
|
+
assert.equal(result.verdict.result, "UNDETERMINED");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("signedBy over a different digest does not satisfy the role's claim", () => {
|
|
261
|
+
const pair = ed25519Pair();
|
|
262
|
+
const rule = parseRule(v2Rule(pair.publicKeyB64, "ed25519", { signedBy: ["work", "mike"] }));
|
|
263
|
+
const other = digestFor("other");
|
|
264
|
+
const evidence = evidenceOf(
|
|
265
|
+
makeSigBytes("ed25519", pair.publicKeyB64, pair.privateKey, hexOfB64(other))
|
|
266
|
+
);
|
|
267
|
+
const result = playAudit(rule, "0".repeat(64), auditWithWork(), evidence);
|
|
268
|
+
assert.equal(result.verdict.result, "UNDETERMINED");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test("signedBy evaluates against the declared digest even when the role has no recording", () => {
|
|
272
|
+
const pair = ed25519Pair();
|
|
273
|
+
const text = JSON.stringify({
|
|
274
|
+
rule: "bitgraph-player/2",
|
|
275
|
+
id: "r",
|
|
276
|
+
cast: { work: { digest: WORK, optional: true } },
|
|
277
|
+
world: "closed",
|
|
278
|
+
requires: { ordering: "hash-linked" },
|
|
279
|
+
trustedKeys: { mike: { alg: "ed25519", publicKey: pair.publicKeyB64 } },
|
|
280
|
+
claim: { signedBy: ["work", "mike"] },
|
|
281
|
+
});
|
|
282
|
+
const rule = parseRule(text);
|
|
283
|
+
const audit = makeAudit({ proofs: [], partitions: [] });
|
|
284
|
+
const evidence = evidenceOf(
|
|
285
|
+
makeSigBytes("ed25519", pair.publicKeyB64, pair.privateKey, hexOfB64(WORK))
|
|
286
|
+
);
|
|
287
|
+
const resolutions = resolveCast(rule.cast, audit);
|
|
288
|
+
const evaluation = evaluate(rule, resolutions, audit, evidence);
|
|
289
|
+
assert.equal(evaluation.result, "TRUE");
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("a /1 rule still yields a /1 verdict and ignores supplied evidence", () => {
|
|
293
|
+
const text = JSON.stringify({
|
|
294
|
+
rule: "bitgraph-player/1",
|
|
295
|
+
id: "r",
|
|
296
|
+
cast: { work: { digest: WORK } },
|
|
297
|
+
world: "closed",
|
|
298
|
+
requires: { ordering: "assumption-dependent" },
|
|
299
|
+
claim: { exists: "work" },
|
|
300
|
+
});
|
|
301
|
+
const rule = parseRule(text);
|
|
302
|
+
const result = playAudit(rule, "0".repeat(64), auditWithWork(), evidenceOf(Buffer.from("junk")));
|
|
303
|
+
assert.equal(result.verdict.verdict, "bitgraph-player-verdict/1");
|
|
304
|
+
assert.equal(result.verdict.result, "TRUE");
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test("multiple candidates: the winner is deterministic and reported by content hash", () => {
|
|
308
|
+
const pair = ed25519Pair();
|
|
309
|
+
const rule = parseRule(v2Rule(pair.publicKeyB64, "ed25519", { signedBy: ["work", "mike"] }));
|
|
310
|
+
const sig = makeSigBytes("ed25519", pair.publicKeyB64, pair.privateKey, hexOfB64(WORK));
|
|
311
|
+
const junk = Buffer.from("{}");
|
|
312
|
+
const a = playAudit(rule, "0".repeat(64), auditWithWork(), evidenceOf(sig, junk));
|
|
313
|
+
const b = playAudit(rule, "0".repeat(64), auditWithWork(), evidenceOf(junk, sig));
|
|
314
|
+
assert.equal(a.bytes, b.bytes);
|
|
315
|
+
});
|