@mikeargento/bitgraph-mcp 0.6.0 → 0.7.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.
- package/dist/__tests__/carrier.test.js +2 -2
- package/dist/__tests__/carrier.test.js.map +1 -1
- package/dist/__tests__/format.test.js +1 -1
- package/dist/__tests__/format.test.js.map +1 -1
- package/dist/__tests__/proof-beside.test.js +1 -1
- package/dist/__tests__/proof-beside.test.js.map +1 -1
- package/dist/__tests__/tools.test.js +1 -1
- package/dist/__tests__/tools.test.js.map +1 -1
- package/dist/format.d.ts +11 -2
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +3 -4
- package/dist/format.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/instructions.d.ts +2 -0
- package/dist/instructions.d.ts.map +1 -0
- package/dist/instructions.js +9 -0
- package/dist/instructions.js.map +1 -0
- package/dist/server.d.ts +3 -38
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +15 -106
- package/dist/server.js.map +1 -1
- package/package.json +4 -3
- package/src/__tests__/carrier.test.ts +2 -2
- package/src/__tests__/format.test.ts +2 -2
- package/src/__tests__/proof-beside.test.ts +1 -1
- package/src/__tests__/tools.test.ts +1 -1
- package/src/format.ts +3 -5
- package/src/index.ts +1 -1
- package/src/instructions.ts +11 -0
- package/src/server.ts +25 -186
- package/dist/__tests__/encoding.test.d.ts +0 -2
- package/dist/__tests__/encoding.test.d.ts.map +0 -1
- package/dist/__tests__/encoding.test.js +0 -51
- package/dist/__tests__/encoding.test.js.map +0 -1
- package/dist/__tests__/scan.test.d.ts +0 -2
- package/dist/__tests__/scan.test.d.ts.map +0 -1
- package/dist/__tests__/scan.test.js +0 -73
- package/dist/__tests__/scan.test.js.map +0 -1
- package/dist/api.d.ts +0 -42
- package/dist/api.d.ts.map +0 -1
- package/dist/api.js +0 -121
- package/dist/api.js.map +0 -1
- package/dist/carrier-io.d.ts +0 -52
- package/dist/carrier-io.d.ts.map +0 -1
- package/dist/carrier-io.js +0 -91
- package/dist/carrier-io.js.map +0 -1
- package/dist/encoding.d.ts +0 -19
- package/dist/encoding.d.ts.map +0 -1
- package/dist/encoding.js +0 -64
- package/dist/encoding.js.map +0 -1
- package/dist/scan.d.ts +0 -60
- package/dist/scan.d.ts.map +0 -1
- package/dist/scan.js +0 -201
- package/dist/scan.js.map +0 -1
- package/dist/task.d.ts +0 -55
- package/dist/task.d.ts.map +0 -1
- package/dist/task.js +0 -182
- package/dist/task.js.map +0 -1
- package/dist/types.d.ts +0 -108
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -3
- package/dist/types.js.map +0 -1
- package/src/__tests__/encoding.test.ts +0 -64
- package/src/__tests__/scan.test.ts +0 -80
- package/src/api.ts +0 -170
- package/src/carrier-io.ts +0 -120
- package/src/encoding.ts +0 -72
- package/src/scan.ts +0 -220
- package/src/task.ts +0 -187
- package/src/types.ts +0 -96
package/src/carrier-io.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @mikeargento/bitgraph-mcp: BitGraphed files (bitgraph-carrier/1).
|
|
5
|
-
*
|
|
6
|
-
* A BitGraphed file is committed bytes followed by one structural block that
|
|
7
|
-
* carries the proof and the anchor evidence. The block is found from the END
|
|
8
|
-
* of the file, by structure, never by scanning. Everything here is local:
|
|
9
|
-
* an 8-byte tail read decides whether a file is a carrier at all, and the
|
|
10
|
-
* full judgment runs offline through @mikeargento/bitgraph-verify.
|
|
11
|
-
*
|
|
12
|
-
* The one rule that cannot bend: the envelope is never the recorded thing,
|
|
13
|
-
* the bytes inside it are. So the tools look up the INNER digest, and
|
|
14
|
-
* bitgraph_record never mints carrier bytes, the same structural guarantee
|
|
15
|
-
* the site's drop box gives.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { open } from "node:fs/promises";
|
|
19
|
-
import {
|
|
20
|
-
CARRIER_MAGIC, CARRIER_OVERHEAD, parseCarrier, verifyCarrier,
|
|
21
|
-
type CarrierVerifyResult,
|
|
22
|
-
} from "@mikeargento/bitgraph-verify";
|
|
23
|
-
|
|
24
|
-
/** ISO string for a witness timestamp (seconds), or null when the header was unreadable. */
|
|
25
|
-
const iso = (ts: number | null): string | null => (ts === null ? null : new Date(ts * 1000).toISOString());
|
|
26
|
-
|
|
27
|
-
/** The window as the tools state it. not_after null = NOT FETCHED, in those words. */
|
|
28
|
-
export interface CarrierWindowView {
|
|
29
|
-
verdict: "TRUE" | "FALSE" | "UNDETERMINED";
|
|
30
|
-
not_before: { block: number; hash: string; time: string | null } | null;
|
|
31
|
-
/** null when the ceiling has not been fetched into the file yet. */
|
|
32
|
-
not_after: { block: number; hash: string; time: string | null } | null;
|
|
33
|
-
ceiling: "present" | "unfetched" | null;
|
|
34
|
-
reasons: string[];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function carrierWindowView(r: CarrierVerifyResult): CarrierWindowView {
|
|
38
|
-
return {
|
|
39
|
-
verdict: r.verdict,
|
|
40
|
-
not_before: r.bounds ? { block: r.bounds.notBefore.blockNumber, hash: r.bounds.notBefore.blockHash, time: iso(r.bounds.notBefore.timestamp) } : null,
|
|
41
|
-
not_after: r.bounds?.notAfter ? { block: r.bounds.notAfter.blockNumber, hash: r.bounds.notAfter.blockHash, time: iso(r.bounds.notAfter.timestamp) } : null,
|
|
42
|
-
ceiling: r.ceiling,
|
|
43
|
-
reasons: r.reasons,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* One line for a markdown row: the verdict and the window, in the stated
|
|
49
|
-
* words. The floor is temporal (after the block's own time, cryptographically);
|
|
50
|
-
* the ceiling is POSITIONAL: committed before the ANCHORING of the later
|
|
51
|
-
* block, never before that block's mine time, because an anchor is built
|
|
52
|
-
* after the block it carries. Canon §3.5/§3.6: floor in time, ceiling in
|
|
53
|
-
* position; converting the positional ceiling into a clock reading is the
|
|
54
|
-
* one error.
|
|
55
|
-
*/
|
|
56
|
-
export function carrierLine(view: CarrierWindowView): string {
|
|
57
|
-
const parts: string[] = [`carried proof ${view.verdict}`];
|
|
58
|
-
if (view.not_before) parts.push(`after block ${view.not_before.block}${view.not_before.time ? ` (mined ${view.not_before.time})` : ""}`);
|
|
59
|
-
parts.push(
|
|
60
|
-
view.not_after
|
|
61
|
-
? `before the anchoring of block ${view.not_after.block}${view.not_after.time ? ` (block mined ${view.not_after.time})` : ""}`
|
|
62
|
-
: "closing anchor NOT FETCHED (drop the file on bitgraph.ing to complete it)"
|
|
63
|
-
);
|
|
64
|
-
return parts.join(" · ");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* True when the file's last 8 bytes are the carrier magic. One positioned
|
|
69
|
-
* read; no file that is not a carrier pays more than that.
|
|
70
|
-
*/
|
|
71
|
-
export async function sniffCarrierTail(path: string, size: number): Promise<boolean> {
|
|
72
|
-
if (size < CARRIER_OVERHEAD) return false;
|
|
73
|
-
const fd = await open(path, "r");
|
|
74
|
-
try {
|
|
75
|
-
const tail = Buffer.alloc(CARRIER_MAGIC.length);
|
|
76
|
-
await fd.read(tail, 0, tail.length, size - tail.length);
|
|
77
|
-
return tail.equals(Buffer.from(CARRIER_MAGIC));
|
|
78
|
-
} finally {
|
|
79
|
-
await fd.close();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface CarrierFileResult {
|
|
84
|
-
/** "ok": readable block, judged. "corrupt": block found but unreadable. */
|
|
85
|
-
status: "ok" | "corrupt";
|
|
86
|
-
/** Standard base64 SHA-256 of the COMMITTED bytes inside; the digest every lookup uses. */
|
|
87
|
-
innerDigestB64: string | null;
|
|
88
|
-
/** The committed bytes, for callers that go on to sniff or serve them. */
|
|
89
|
-
inner: Uint8Array | null;
|
|
90
|
-
result: CarrierVerifyResult | null;
|
|
91
|
-
view: CarrierWindowView | null;
|
|
92
|
-
corruptReason: string | null;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Read a tail-confirmed carrier whole and judge it offline: the carried
|
|
97
|
-
* proof, the floor by identity, the witnesses, the ceiling when present.
|
|
98
|
-
* Proof-level judgment (attestation depth stays the audit CLI's job).
|
|
99
|
-
*/
|
|
100
|
-
export async function readCarrierFile(path: string): Promise<CarrierFileResult> {
|
|
101
|
-
const fd = await open(path, "r");
|
|
102
|
-
let bytes: Uint8Array;
|
|
103
|
-
try {
|
|
104
|
-
const info = await fd.stat();
|
|
105
|
-
const buf = Buffer.alloc(info.size);
|
|
106
|
-
await fd.read(buf, 0, info.size, 0);
|
|
107
|
-
bytes = new Uint8Array(buf);
|
|
108
|
-
} finally {
|
|
109
|
-
await fd.close();
|
|
110
|
-
}
|
|
111
|
-
const parsed = parseCarrier(bytes);
|
|
112
|
-
if (parsed.kind !== "carrier") {
|
|
113
|
-
const reason = parsed.kind === "corrupt" ? parsed.reason : "the tail magic matched but no block parsed";
|
|
114
|
-
return { status: "corrupt", innerDigestB64: null, inner: null, result: null, view: null, corruptReason: reason };
|
|
115
|
-
}
|
|
116
|
-
const result = await verifyCarrier(bytes);
|
|
117
|
-
const { createHash } = await import("node:crypto");
|
|
118
|
-
const innerDigestB64 = createHash("sha256").update(parsed.inner).digest("base64");
|
|
119
|
-
return { status: "ok", innerDigestB64, inner: parsed.inner, result, view: carrierWindowView(result), corruptReason: null };
|
|
120
|
-
}
|
package/src/encoding.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @mikeargento/bitgraph-mcp: digest encoding and file hashing.
|
|
5
|
-
*
|
|
6
|
-
* BitGraph uses two base64 forms for the same SHA-256 digest:
|
|
7
|
-
* standard base64 with padding inside proof objects and commit requests,
|
|
8
|
-
* URL-safe unpadded base64 in API lookups, S3 keys, and proof page URLs.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { createHash } from "node:crypto";
|
|
12
|
-
import { createReadStream } from "node:fs";
|
|
13
|
-
import { stat } from "node:fs/promises";
|
|
14
|
-
|
|
15
|
-
/** Standard base64 → URL-safe unpadded base64. */
|
|
16
|
-
export function toUrlSafeB64(b64: string): string {
|
|
17
|
-
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* URL-safe base64 → standard base64 with padding.
|
|
22
|
-
* A no-op (apart from re-padding) when the input is already standard base64,
|
|
23
|
-
* so it is safe to call on either form.
|
|
24
|
-
*/
|
|
25
|
-
export function fromUrlSafeB64(urlSafe: string): string {
|
|
26
|
-
let b64 = urlSafe.replace(/-/g, "+").replace(/_/g, "/");
|
|
27
|
-
while (b64.length % 4 !== 0) b64 += "=";
|
|
28
|
-
return b64;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** True if the string plausibly encodes 32 bytes of SHA-256 in either base64 form. */
|
|
32
|
-
export function looksLikeDigest(s: string): boolean {
|
|
33
|
-
const normalized = fromUrlSafeB64(s.trim());
|
|
34
|
-
if (!/^[A-Za-z0-9+/]{43}=$/.test(normalized)) return false;
|
|
35
|
-
return Buffer.from(normalized, "base64").length === 32;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* SHA-256 of a file's bytes as standard base64.
|
|
40
|
-
* Streams from disk, so file size is not a memory concern. Only this digest
|
|
41
|
-
* ever leaves the machine; file contents are never uploaded.
|
|
42
|
-
*/
|
|
43
|
-
export async function sha256FileB64(path: string): Promise<string> {
|
|
44
|
-
const info = await stat(path);
|
|
45
|
-
if (!info.isFile()) {
|
|
46
|
-
throw new Error(`Not a regular file: ${path}`);
|
|
47
|
-
}
|
|
48
|
-
const hash = createHash("sha256");
|
|
49
|
-
for await (const chunk of createReadStream(path)) {
|
|
50
|
-
hash.update(chunk as Buffer);
|
|
51
|
-
}
|
|
52
|
-
return hash.digest("base64");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Run an async mapper over items with bounded concurrency, preserving order. */
|
|
56
|
-
export async function mapConcurrent<T, R>(
|
|
57
|
-
items: readonly T[],
|
|
58
|
-
concurrency: number,
|
|
59
|
-
fn: (item: T, index: number) => Promise<R>
|
|
60
|
-
): Promise<R[]> {
|
|
61
|
-
const results: R[] = new Array(items.length);
|
|
62
|
-
let next = 0;
|
|
63
|
-
async function worker(): Promise<void> {
|
|
64
|
-
while (next < items.length) {
|
|
65
|
-
const index = next++;
|
|
66
|
-
results[index] = await fn(items[index] as T, index);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
|
|
70
|
-
await Promise.all(workers);
|
|
71
|
-
return results;
|
|
72
|
-
}
|
package/src/scan.ts
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @mikeargento/bitgraph-mcp: the scan.
|
|
5
|
-
*
|
|
6
|
-
* One pass over each file yields its SHA-256 (the member's origin) and a
|
|
7
|
-
* hasher left open after the placement's prefix and the file's last byte, so
|
|
8
|
-
* the member's fused digest can be finished later, for whatever slot the set
|
|
9
|
-
* is made under, without reading the file again. A placement is prefix,
|
|
10
|
-
* original, suffix (its frame): trailer/1 has no prefix and container/2's is
|
|
11
|
-
* the original's tar header, which depends on the size alone; the suffix
|
|
12
|
-
* carries the commitment and is hashed at BitGraph time. Node's SHA-256 is
|
|
13
|
-
* the platform's own, and a Hash can be copied mid-stream, which is the whole
|
|
14
|
-
* trick: the copy takes the suffix, so no file is read twice or held in
|
|
15
|
-
* memory, and the original is never touched.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { createHash, type Hash } from "node:crypto";
|
|
19
|
-
import { createReadStream } from "node:fs";
|
|
20
|
-
import { readdir, stat } from "node:fs/promises";
|
|
21
|
-
import { basename, join } from "node:path";
|
|
22
|
-
import { placementForBytes } from "@mikeargento/bitgraph";
|
|
23
|
-
import { getPlacement } from "@mikeargento/bitgraph-verify";
|
|
24
|
-
|
|
25
|
-
/** The placements the scan makes: chosen from the bytes by the core, never from the name. */
|
|
26
|
-
export type ScanPlacement = "trailer/1" | "container/2";
|
|
27
|
-
|
|
28
|
-
export interface ScannedFile {
|
|
29
|
-
path: string;
|
|
30
|
-
name: string;
|
|
31
|
-
size: number;
|
|
32
|
-
/** SHA-256 of the file, standard base64: the member's origin. */
|
|
33
|
-
digestB64: string;
|
|
34
|
-
originDigest: Uint8Array;
|
|
35
|
-
placement: ScanPlacement;
|
|
36
|
-
/**
|
|
37
|
-
* The hasher after the placement's prefix and the file's bytes, still open.
|
|
38
|
-
* Null when the file's length changed while it was read: its fused digest
|
|
39
|
-
* then needs the bytes again.
|
|
40
|
-
*/
|
|
41
|
-
state: Hash | null;
|
|
42
|
-
/** True when the bytes carry what looks like a C2PA manifest (Content Credentials). */
|
|
43
|
-
c2pa: boolean;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Content Credentials detection, structural and cheap: a C2PA manifest lives
|
|
48
|
-
* in a JUMBF box whose description ("jumd") is followed within a few bytes by
|
|
49
|
-
* the store label "c2pa", in every container (JPEG APP11, PNG caBX, ISO BMFF).
|
|
50
|
-
* Requiring the pair in order and near each other keeps a stray "c2pa" in
|
|
51
|
-
* prose from counting. Detection, not judgment: the proof page parses and
|
|
52
|
-
* displays the manifest itself.
|
|
53
|
-
*/
|
|
54
|
-
const JUMD = Buffer.from("jumd", "ascii");
|
|
55
|
-
const C2PA_LABEL = Buffer.from("c2pa", "ascii");
|
|
56
|
-
const C2PA_NEAR = 96;
|
|
57
|
-
|
|
58
|
-
function windowHasC2pa(buf: Buffer): boolean {
|
|
59
|
-
for (let i = buf.indexOf(JUMD); i !== -1; i = buf.indexOf(JUMD, i + 1)) {
|
|
60
|
-
const from = i + JUMD.length;
|
|
61
|
-
const to = Math.min(buf.length, i + C2PA_NEAR);
|
|
62
|
-
if (buf.subarray(from, to).indexOf(C2PA_LABEL) !== -1) return true;
|
|
63
|
-
}
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
class C2paSniffer {
|
|
68
|
-
found = false;
|
|
69
|
-
/** The last C2PA_NEAR + 4 bytes of the previous chunk, so a pair split across chunks is seen whole. */
|
|
70
|
-
private carry: Buffer = Buffer.alloc(0);
|
|
71
|
-
update(chunk: Buffer): void {
|
|
72
|
-
if (this.found) return;
|
|
73
|
-
const buf = this.carry.length > 0 ? Buffer.concat([this.carry, chunk]) : chunk;
|
|
74
|
-
if (windowHasC2pa(buf)) {
|
|
75
|
-
this.found = true;
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const keep = Math.min(buf.length, C2PA_NEAR + JUMD.length);
|
|
79
|
-
this.carry = Buffer.from(buf.subarray(buf.length - keep));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** One-shot form for bytes already in memory (a carrier's committed bytes). */
|
|
84
|
-
export function sniffC2paBytes(bytes: Uint8Array): boolean {
|
|
85
|
-
return windowHasC2pa(Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Bytes the placement decision reads: every magic number sits in the first 16. */
|
|
89
|
-
const SNIFF = 64;
|
|
90
|
-
|
|
91
|
-
/** Hash one file in a single pass. Throws, before any network call, when the path is not a regular file. */
|
|
92
|
-
export async function scanFile(path: string): Promise<ScannedFile> {
|
|
93
|
-
const info = await stat(path);
|
|
94
|
-
if (!info.isFile()) throw new Error(`Not a regular file: ${path}`);
|
|
95
|
-
const size = info.size;
|
|
96
|
-
const origin = createHash("sha256");
|
|
97
|
-
// Held in one object: the closures below assign to it.
|
|
98
|
-
const run: { fused: Hash | null; placement: ScanPlacement | null } = { fused: null, placement: null };
|
|
99
|
-
let head: Buffer | null = null;
|
|
100
|
-
let bytes = 0;
|
|
101
|
-
const start = (p: ScanPlacement) => {
|
|
102
|
-
run.placement = p;
|
|
103
|
-
// A second hasher runs over prefix and bytes when the prefix is not
|
|
104
|
-
// empty; with an empty prefix the origin hasher's own state is the fused state.
|
|
105
|
-
const prefix = getPlacement(p)?.scanPrefix?.(size) ?? null;
|
|
106
|
-
if (prefix !== null && prefix.length > 0) run.fused = createHash("sha256").update(prefix);
|
|
107
|
-
};
|
|
108
|
-
const sniffer = new C2paSniffer();
|
|
109
|
-
const feed = (chunk: Buffer) => {
|
|
110
|
-
origin.update(chunk);
|
|
111
|
-
if (run.fused !== null) run.fused.update(chunk);
|
|
112
|
-
sniffer.update(chunk);
|
|
113
|
-
};
|
|
114
|
-
for await (const raw of createReadStream(path)) {
|
|
115
|
-
const chunk = raw as Buffer;
|
|
116
|
-
if (run.placement === null) {
|
|
117
|
-
head = head === null ? chunk : Buffer.concat([head, chunk]);
|
|
118
|
-
if (head.length >= SNIFF) {
|
|
119
|
-
start(placementForBytes(new Uint8Array(head.subarray(0, SNIFF))));
|
|
120
|
-
feed(head);
|
|
121
|
-
head = null;
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
feed(chunk);
|
|
125
|
-
}
|
|
126
|
-
bytes += chunk.length;
|
|
127
|
-
}
|
|
128
|
-
if (run.placement === null) {
|
|
129
|
-
// A short file: decide from what there is, then hash it.
|
|
130
|
-
start(placementForBytes(new Uint8Array(head ?? Buffer.alloc(0))));
|
|
131
|
-
if (head !== null) feed(head);
|
|
132
|
-
}
|
|
133
|
-
const placement = run.placement as ScanPlacement;
|
|
134
|
-
const state = bytes !== size ? null : (run.fused ?? origin);
|
|
135
|
-
// When the origin hasher is the state its digest is taken from a copy, so it stays open.
|
|
136
|
-
const digest = state === origin ? origin.copy().digest() : origin.digest();
|
|
137
|
-
return {
|
|
138
|
-
path,
|
|
139
|
-
name: basename(path),
|
|
140
|
-
size,
|
|
141
|
-
digestB64: digest.toString("base64"),
|
|
142
|
-
originDigest: Uint8Array.from(digest),
|
|
143
|
-
placement,
|
|
144
|
-
state,
|
|
145
|
-
c2pa: sniffer.found,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* A member's fused digest for a slot: the open hasher, copied, finished with
|
|
151
|
-
* the placement's suffix for that slot's commitment. The hash of prefix,
|
|
152
|
-
* original, suffix is exactly the hash of the placement's own build, which
|
|
153
|
-
* a test pins.
|
|
154
|
-
*/
|
|
155
|
-
export function fusedDigestFor(file: ScannedFile, commitment: Uint8Array): Uint8Array {
|
|
156
|
-
if (file.state === null) throw new Error(`${file.path}: the file changed while it was read`);
|
|
157
|
-
const placement = getPlacement(file.placement);
|
|
158
|
-
if (placement?.frame === undefined) throw new Error(`placement ${file.placement} has no frame`);
|
|
159
|
-
const { suffix } = placement.frame({ originalSize: file.size, originDigest: file.originDigest, commitment });
|
|
160
|
-
return Uint8Array.from(file.state.copy().update(suffix).digest());
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export interface Expansion {
|
|
164
|
-
/** Regular files in the order given; a directory contributes its files sorted by name, depth first. */
|
|
165
|
-
files: string[];
|
|
166
|
-
/** How many of the inputs were directories. */
|
|
167
|
-
directories: number;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Paths to files: a regular file is itself; a directory is every regular
|
|
172
|
-
* file under it, recursively, with hidden entries (names starting with a
|
|
173
|
-
* dot) and symbolic links left out. The same file twice is one file. A path
|
|
174
|
-
* that is missing or neither kind, or more than `limit` files in all, throws
|
|
175
|
-
* before any network call; the message names every path that failed.
|
|
176
|
-
*/
|
|
177
|
-
export async function expandPaths(paths: readonly string[], limit: number): Promise<Expansion> {
|
|
178
|
-
const files: string[] = [];
|
|
179
|
-
const seen = new Set<string>();
|
|
180
|
-
const failures: string[] = [];
|
|
181
|
-
let directories = 0;
|
|
182
|
-
const push = (p: string) => {
|
|
183
|
-
if (seen.has(p)) return;
|
|
184
|
-
seen.add(p);
|
|
185
|
-
files.push(p);
|
|
186
|
-
if (files.length > limit) throw new Error(`more than ${limit} files; BitGraph fewer at a time`);
|
|
187
|
-
};
|
|
188
|
-
const walk = async (dir: string): Promise<void> => {
|
|
189
|
-
const entries = (await readdir(dir, { withFileTypes: true }))
|
|
190
|
-
.filter((e) => !e.name.startsWith("."))
|
|
191
|
-
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
192
|
-
for (const e of entries) {
|
|
193
|
-
const full = join(dir, e.name);
|
|
194
|
-
if (e.isSymbolicLink()) continue;
|
|
195
|
-
if (e.isDirectory()) await walk(full);
|
|
196
|
-
else if (e.isFile()) push(full);
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
for (const p of paths) {
|
|
200
|
-
try {
|
|
201
|
-
const info = await stat(p);
|
|
202
|
-
if (info.isDirectory()) {
|
|
203
|
-
directories += 1;
|
|
204
|
-
await walk(p);
|
|
205
|
-
} else if (info.isFile()) {
|
|
206
|
-
push(p);
|
|
207
|
-
} else {
|
|
208
|
-
throw new Error("not a regular file or a directory");
|
|
209
|
-
}
|
|
210
|
-
} catch (err) {
|
|
211
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
212
|
-
if (message.startsWith("more than ")) throw err;
|
|
213
|
-
failures.push(`${p}: ${message}`);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
if (failures.length > 0) {
|
|
217
|
-
throw new Error(`Could not read ${failures.length} path(s); nothing was BitGraphed.\n${failures.join("\n")}\nUse absolute paths to existing regular files or directories.`);
|
|
218
|
-
}
|
|
219
|
-
return { files, directories };
|
|
220
|
-
}
|
package/src/task.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A position BEFORE the work: the task form of the stdio server.
|
|
5
|
-
*
|
|
6
|
-
* bitgraph_open with no files asks the site's own /api/fuse/allocate for a
|
|
7
|
-
* slot and returns its commitment, and nothing else. The caller puts the
|
|
8
|
-
* commitment into the task it is about to run (the prompt, the request, a
|
|
9
|
-
* seed, a line in the document) and, within the slot's life, commits the
|
|
10
|
-
* digest of those task bytes under the slot with the inline marker
|
|
11
|
-
* (bitgraph-fuse/1, title base64url): a verifier recomputes the commitment
|
|
12
|
-
* from the slot record and finds its base64url text inside the bytes.
|
|
13
|
-
* Outputs are recorded afterwards with bitgraph_record and sit later.
|
|
14
|
-
*
|
|
15
|
-
* The slot allocation and the commit go through the same two site routes
|
|
16
|
-
* the core package's fuse() uses; the recovery rule is the core's (read back
|
|
17
|
-
* by digest, refuse a proof under any other slot).
|
|
18
|
-
*/
|
|
19
|
-
import { readFile } from "node:fs/promises";
|
|
20
|
-
import { createHash } from "node:crypto";
|
|
21
|
-
import { bytesToBase64, computeSlotCommitment, findCommitment, inlineAttribution, verifyProofIntegrity, ENCODING_BASE64URL } from "@mikeargento/bitgraph-verify";
|
|
22
|
-
import type { BitGraphProof, SlotAllocation } from "@mikeargento/bitgraph-verify";
|
|
23
|
-
import { ApiError, type ApiConfig } from "./api.js";
|
|
24
|
-
import { toUrlSafeB64 } from "./encoding.js";
|
|
25
|
-
|
|
26
|
-
export const SLOT_TTL_SECONDS = 120;
|
|
27
|
-
const CHAIN = "bitgraph:main";
|
|
28
|
-
const B64_32 = /^[A-Za-z0-9+/]{43}=$/;
|
|
29
|
-
const B64_64 = /^[A-Za-z0-9+/]{86}==$/;
|
|
30
|
-
|
|
31
|
-
export interface TaskState {
|
|
32
|
-
v: 1;
|
|
33
|
-
task: true;
|
|
34
|
-
slot: SlotAllocation;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function isSlotRecord(x: unknown): x is SlotAllocation {
|
|
38
|
-
if (x === null || typeof x !== "object" || Array.isArray(x)) return false;
|
|
39
|
-
const s = x as Record<string, unknown>;
|
|
40
|
-
return s.version === "bitgraph/slot/1" && typeof s.nonceB64 === "string" && B64_32.test(s.nonceB64) && typeof s.counter === "string" && /^(0|[1-9][0-9]*)$/.test(s.counter)
|
|
41
|
-
&& typeof s.epochId === "string" && typeof s.publicKeyB64 === "string" && typeof s.signatureB64 === "string" && B64_64.test(s.signatureB64) && s.chainId === CHAIN;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function encodeTaskToken(state: TaskState): string {
|
|
45
|
-
return Buffer.from(JSON.stringify(state), "utf8").toString("base64url");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function decodeTaskToken(token: string): TaskState | null {
|
|
49
|
-
let parsed: unknown;
|
|
50
|
-
try { parsed = JSON.parse(Buffer.from(token, "base64url").toString("utf8")); } catch { return null; }
|
|
51
|
-
if (typeof parsed !== "object" || parsed === null) return null;
|
|
52
|
-
const s = parsed as Record<string, unknown>;
|
|
53
|
-
if (s.v !== 1 || s.task !== true || !isSlotRecord(s.slot)) return null;
|
|
54
|
-
return { v: 1, task: true, slot: s.slot };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async function post(config: ApiConfig, path: string, body: unknown, timeoutMs: number): Promise<{ status: number; json: unknown; retryAfterSec: number | null }> {
|
|
58
|
-
const headers: Record<string, string> = { "content-type": "application/json", accept: "application/json" };
|
|
59
|
-
if (config.apiKey) headers["authorization"] = `Bearer ${config.apiKey}`;
|
|
60
|
-
const res = await fetch(`${config.baseUrl}${path}`, { method: "POST", headers, body: JSON.stringify(body), signal: AbortSignal.timeout(timeoutMs) });
|
|
61
|
-
let json: unknown = null;
|
|
62
|
-
try { json = await res.json(); } catch { /* non-JSON */ }
|
|
63
|
-
const raw = res.headers.get("retry-after");
|
|
64
|
-
const retry = raw !== null ? Number.parseInt(raw, 10) : NaN;
|
|
65
|
-
return { status: res.status, json, retryAfterSec: Number.isFinite(retry) ? retry : null };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const messageOf = (json: unknown, fallback: string): string => {
|
|
69
|
-
const e = (json as { error?: unknown } | null)?.error;
|
|
70
|
-
return typeof e === "string" ? e : fallback;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export interface Begun {
|
|
74
|
-
token: string;
|
|
75
|
-
/** Unpadded base64url: the string to put into the task. */
|
|
76
|
-
commitment: string;
|
|
77
|
-
commitmentB64: string;
|
|
78
|
-
slotCounter: string;
|
|
79
|
-
epoch: string;
|
|
80
|
-
floor: { block: number } | null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Step one of the task form: a held slot and its commitment, before any work exists. */
|
|
84
|
-
export async function beginTask(config: ApiConfig): Promise<Begun> {
|
|
85
|
-
const r = await post(config, "/api/fuse/allocate", {}, 20_000);
|
|
86
|
-
if (r.status !== 200) throw new ApiError(r.status, messageOf(r.json, `allocation refused (${r.status})`), r.retryAfterSec);
|
|
87
|
-
const slot = (r.json as { slot?: unknown } | null)?.slot;
|
|
88
|
-
if (!isSlotRecord(slot)) throw new ApiError(502, "the allocation response is not a slot record on bitgraph:main");
|
|
89
|
-
const commitmentB64 = bytesToBase64(computeSlotCommitment(slot));
|
|
90
|
-
let floor: Begun["floor"] = null;
|
|
91
|
-
try {
|
|
92
|
-
const res = await fetch(`${config.baseUrl}/api/proofs/anchors?counter=${encodeURIComponent(slot.counter)}&epoch=${encodeURIComponent(slot.epochId)}&before=1`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(10_000) });
|
|
93
|
-
const data = res.status === 200 ? ((await res.json()) as { anchors?: Array<{ commit?: { anchor?: { blockNumber?: number } } }> }) : null;
|
|
94
|
-
const b = data?.anchors?.[0]?.commit?.anchor?.blockNumber;
|
|
95
|
-
if (typeof b === "number") floor = { block: b };
|
|
96
|
-
} catch { floor = null; }
|
|
97
|
-
return { token: encodeTaskToken({ v: 1, task: true, slot }), commitment: toUrlSafeB64(commitmentB64), commitmentB64, slotCounter: slot.counter, epoch: toUrlSafeB64(slot.epochId), floor };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export interface SealedTask {
|
|
101
|
-
proof: BitGraphProof;
|
|
102
|
-
artifactDigestB64: string;
|
|
103
|
-
/** Byte offsets of the commitment string inside the task bytes, when the bytes were given. */
|
|
104
|
-
offsets: number[] | null;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Step two: the task bytes (a path, or the bytes themselves) sealed under
|
|
109
|
-
* the held slot with the inline marker. When the bytes are in hand the
|
|
110
|
-
* commitment is looked for BEFORE the commit, so a task that does not carry
|
|
111
|
-
* it is refused without spending the slot.
|
|
112
|
-
*/
|
|
113
|
-
export async function sealTask(config: ApiConfig, state: TaskState, task: { path: string } | { bytes: Uint8Array } | { digestB64: string }): Promise<SealedTask> {
|
|
114
|
-
const { slot } = state;
|
|
115
|
-
const commitment = computeSlotCommitment(slot);
|
|
116
|
-
let bytes: Uint8Array | null = null;
|
|
117
|
-
let artifactDigestB64: string;
|
|
118
|
-
if ("digestB64" in task) {
|
|
119
|
-
if (!B64_32.test(task.digestB64)) throw new ApiError(400, "artifact digest must be a base64 SHA-256");
|
|
120
|
-
artifactDigestB64 = task.digestB64;
|
|
121
|
-
} else {
|
|
122
|
-
bytes = "path" in task ? new Uint8Array(await readFile(task.path)) : task.bytes;
|
|
123
|
-
artifactDigestB64 = createHash("sha256").update(bytes).digest("base64");
|
|
124
|
-
}
|
|
125
|
-
const offsets = bytes === null ? null : findCommitment(bytes, commitment, ENCODING_BASE64URL);
|
|
126
|
-
if (offsets !== null && offsets.length === 0) {
|
|
127
|
-
throw new ApiError(400, `the task bytes do not contain the commitment string ${toUrlSafeB64(bytesToBase64(commitment))}; put it in before sealing. Nothing was committed and the slot is still held.`);
|
|
128
|
-
}
|
|
129
|
-
const attribution = inlineAttribution();
|
|
130
|
-
const r = await post(config, "/api/fuse/commit", { digests: [{ digestB64: artifactDigestB64, hashAlg: "sha256" }], slotId: slot.nonceB64, slot, chainId: CHAIN, attribution }, 40_000);
|
|
131
|
-
let proof: BitGraphProof | null = null;
|
|
132
|
-
if (r.status === 200) proof = ((r.json as { proof?: BitGraphProof } | null)?.proof ?? null);
|
|
133
|
-
else if (r.status === 409 || r.status === 503) proof = await recover(config, artifactDigestB64, slot);
|
|
134
|
-
if (proof === null) throw new ApiError(r.status, messageOf(r.json, `commit refused (${r.status})`), r.retryAfterSec);
|
|
135
|
-
if (proof.artifact?.digestB64 !== artifactDigestB64 || proof.commit?.nonceB64 !== slot.nonceB64 || proof.slotAllocation?.nonceB64 !== slot.nonceB64) throw new ApiError(502, "the boundary returned a proof under a different slot; nothing is labelled sealed");
|
|
136
|
-
const a = proof.attribution;
|
|
137
|
-
if (a?.name !== attribution.name || a?.title !== attribution.title || a?.message !== undefined) throw new ApiError(502, "the returned proof does not carry the inline marker that was sent; nothing is labelled sealed");
|
|
138
|
-
const integrity = await verifyProofIntegrity({ proof });
|
|
139
|
-
if (!integrity.valid) throw new ApiError(502, `the returned proof does not verify: ${integrity.reason ?? "unknown reason"}`);
|
|
140
|
-
return { proof, artifactDigestB64, offsets };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async function recover(config: ApiConfig, artifactDigestB64: string, slot: SlotAllocation): Promise<BitGraphProof | null> {
|
|
144
|
-
for (let attempt = 0; attempt < 4; attempt++) {
|
|
145
|
-
if (attempt > 0) await new Promise((r) => setTimeout(r, 1500));
|
|
146
|
-
try {
|
|
147
|
-
const res = await fetch(`${config.baseUrl}/api/proofs/${encodeURIComponent(toUrlSafeB64(artifactDigestB64))}`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(15_000) });
|
|
148
|
-
if (res.status !== 200) continue;
|
|
149
|
-
const j = (await res.json()) as { proofs?: Array<{ proof?: BitGraphProof }> } | null;
|
|
150
|
-
for (const e of j?.proofs ?? []) {
|
|
151
|
-
const p = e.proof;
|
|
152
|
-
if (p && p.artifact?.digestB64 === artifactDigestB64 && p.commit?.nonceB64 === slot.nonceB64) return p;
|
|
153
|
-
}
|
|
154
|
-
} catch { /* try again */ }
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export const TASK_INSTRUCTIONS =
|
|
160
|
-
"You hold a position and its commitment, and no work exists yet. Put the commitment string INSIDE THE OUTPUT you are about to make whenever its format can hold text (SVG, HTML, code, JSON, Markdown, CSV, a document): as a comment, a field, a line that stays in the file. " +
|
|
161
|
-
`When the output exists, call bitgraph_commit with this fuse_token and the output file's path, within ${SLOT_TTL_SECONDS} seconds of opening: the output itself is sealed under the position, and the proof is written beside it. ` +
|
|
162
|
-
"If the output cannot hold text (a PNG, audio, video) or will take longer than that to make, put the commitment into a task file instead (the exact prompt or request you send), seal that file the same way, and when the output exists record it with bitgraph_record: the task is sealed before the output existed, and the output is recorded after. " +
|
|
163
|
-
"Keep the exact bytes that were sealed unchanged: a verifier recomputes the commitment from the proof and looks for the string inside them. " +
|
|
164
|
-
"Leave the proof file as written, whole, every field, environment.attestation.reportB64 included: a proof missing slotAllocation, environment, or the attestation cannot be verified.";
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Write a sealed task's proof beside the file it is about, whole, and say
|
|
168
|
-
* where. `make_task.txt` gets `make_task.proof.json`; a name already taken
|
|
169
|
-
* gets the position's counter in it rather than being overwritten.
|
|
170
|
-
*
|
|
171
|
-
* ⚠️ THE SERVER WRITES IT, NOT THE AGENT. Handed the JSON to save, an agent
|
|
172
|
-
* tidied it and dropped slotAllocation and environment, and the signature
|
|
173
|
-
* had no body left to be checked against (Grok, 2026-09-13). A file this
|
|
174
|
-
* process writes is the proof exactly as the boundary returned it.
|
|
175
|
-
*/
|
|
176
|
-
export async function writeProofBeside(sealedPath: string, proof: BitGraphProof): Promise<string> {
|
|
177
|
-
const { writeFile, access } = await import("node:fs/promises");
|
|
178
|
-
const { basename, dirname, extname, join } = await import("node:path");
|
|
179
|
-
const dir = dirname(sealedPath);
|
|
180
|
-
const base = basename(sealedPath);
|
|
181
|
-
const stem = extname(base) === "" ? base : base.slice(0, -extname(base).length);
|
|
182
|
-
const counter = proof.commit?.counter ?? "0";
|
|
183
|
-
let target = join(dir, `${stem}.proof.json`);
|
|
184
|
-
if (await access(target).then(() => true).catch(() => false)) target = join(dir, `${stem}.proof.${counter}.json`);
|
|
185
|
-
await writeFile(target, JSON.stringify(proof, null, 2) + "\n", { flag: "wx" });
|
|
186
|
-
return target;
|
|
187
|
-
}
|