@333eco/origin-verify 0.1.2 → 0.1.4

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 CHANGED
@@ -26,7 +26,7 @@ Four states. Each stands alone and none implies another.
26
26
  | state | it says | it rests on |
27
27
  |---|---|---|
28
28
  | `included` | this record is in the tree this checkpoint signs, and the log cannot later remove it unnoticed | the operator's keys and the Merkle proof |
29
- | `witnessed` | enough outside witnesses cosigned the checkpoint | their signatures. ⚠️ False today: no witness has admitted this log yet, and the result says so |
29
+ | `witnessed` | enough outside witnesses cosigned the checkpoint | their signatures. ⚠️ False today: the log is on the Witness Network's testing list, not yet staging. Testing witnesses may cosign and `witnesses` names them, but they are not counted, because they may reset or disappear |
30
30
  | `timestamped` | a checkpoint holding this record existed by this time | each authority's word, one per token |
31
31
  | `bitcoin` | a checkpoint holding this record existed by this block | Bitcoin's proof of work |
32
32
 
@@ -44,6 +44,22 @@ Four states. Each stands alone and none implies another.
44
44
 
45
45
  ⚠️ The DCT hash most people reach for fails on pixel art: half the coefficients of a mirror-symmetric sprite are near zero, so their bits are noise. Measured on 150 pictures × 5 re-encodings, 40 of them symmetric sprites: this hash finds 99.7% of the same picture within distance 8, with 0.8% of different pictures within 8.
46
46
 
47
+ ## A proof inside the file
48
+
49
+ A pack can travel inside the PNG or JPEG it describes, so the file can be checked with nothing else and no network:
50
+
51
+ ```js
52
+ import { embedPack, verifyFile } from "@333eco/origin-verify";
53
+ const carried = embedPack(fileBytes, pack); // a copy with the proof inside; refuses a pack for any other file
54
+ const result = await verifyFile(carried, trust);
55
+ ```
56
+
57
+ The proof sits in a private PNG chunk or in JPEG APP9 segments. The picture isn't touched, and taking the proof out gives back exactly the bytes that were signed, which is how a reader knows it belongs to this file. Embedding again replaces the proof, so a pack can be renewed as it gains its timestamps.
58
+
59
+ - A pack with an `opening` carries the passkey's signature. The file then proves a passkey signed it, and it shows that passkey's public key: anyone can link every file carrying the same key. Include one only if the signer chooses to.
60
+ - Most platforms strip or re-encode what they receive, and the proof goes with it. Then look the file up by what it still is, below.
61
+ - A carried proof is a snapshot. It can't say whether the signer withdrew the record later, so `verifyFile` reports `withdrawalChecked: false`. Only the log can answer that.
62
+
47
63
  ## Honest limits
48
64
 
49
65
  - `trust.mjs` pins the log's keys and the timestamp authorities' roots. If you got this package from somewhere you don't trust, nothing here helps. npm provenance links each version to this repository's source.
package/embed.mjs ADDED
@@ -0,0 +1,119 @@
1
+ // Carries a record's pack inside the file it describes, so the file can be checked with nothing else (founder 2026-11-08).
2
+ // The pack goes in one removable place: a private PNG chunk, or JPEG APP9 segments. Taking it out gives back the signed bytes exactly, and that is
3
+ // how a reader knows the proof belongs to this file. The picture is never touched, so `pixels` and `phash` are the same with or without it.
4
+ // ⚠️ A pack is a snapshot: it cannot say the signer did not withdraw the record later. Only the log can (`withdrawalChecked: false` in verifyFile).
5
+ import { concat, hex, sha256, text, u32, utf8 } from "./primitives.mjs";
6
+
7
+ const MAX_PACK = 8 * 1024 * 1024;
8
+ const LABEL = utf8("b-origin-pack\0");
9
+ const PNG_SIG = [137, 80, 78, 71, 13, 10, 26, 10];
10
+ // Ancillary, private, and NOT safe to copy (the last letter is upper case): an editor that changes the image and does not know this chunk must drop it.
11
+ const PNG_TYPE = utf8("orGN");
12
+ const IEND = utf8("IEND");
13
+ const JPEG_DATA = 65533 - LABEL.length - 4; // an APP segment holds 65533 bytes after its length field
14
+
15
+ const CRC = Uint32Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
16
+ const crc32 = (...parts) => { let c = 0xffffffff; for (const p of parts) for (let i = 0; i < p.length; i++) c = CRC[(c ^ p[i]) & 255] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };
17
+ const same = (b, at, want) => at + want.length <= b.length && want.every((x, i) => b[at + i] === x);
18
+ const u32At = (b, at) => new DataView(b.buffer, b.byteOffset + at, 4).getUint32(0);
19
+ const u16 = (n) => new Uint8Array([n >> 8, n & 255]);
20
+ const u16At = (b, at) => (b[at] << 8) | b[at + 1];
21
+ const isPng = (b) => same(b, 0, PNG_SIG);
22
+ const isJpeg = (b) => b.length > 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff;
23
+
24
+ /** Where a PNG's chunks are, or why it can't be read. */
25
+ function pngChunks(b) {
26
+ const out = [];
27
+ for (let i = 8; i < b.length;) {
28
+ if (i + 12 > b.length) return { why: "the PNG is cut short" };
29
+ const n = u32At(b, i), end = i + 12 + n;
30
+ if (end > b.length) return { why: "the PNG is cut short" };
31
+ out.push({ at: i, end, type: b.subarray(i + 4, i + 8), data: b.subarray(i + 8, i + 8 + n), crc: u32At(b, i + 8 + n) });
32
+ if (same(b, i + 4, IEND)) return { chunks: out };
33
+ i = end;
34
+ }
35
+ return { why: "the PNG has no end" };
36
+ }
37
+ /** Our segments in a JPEG's header, in file order. Reads only up to the image data. */
38
+ function jpegOurs(b) {
39
+ const ours = [];
40
+ for (let i = 2; i + 2 <= b.length;) {
41
+ if (b[i] !== 0xff) return { why: "the JPEG's header is damaged" };
42
+ const m = b[i + 1];
43
+ if (m === 0xff) { i++; continue; }
44
+ if (m === 0xda || m === 0xd9) return { ours };
45
+ if (m === 0x01 || (m >= 0xd0 && m <= 0xd7)) { i += 2; continue; }
46
+ if (i + 4 > b.length) return { why: "the JPEG's header is damaged" };
47
+ const n = u16At(b, i + 2);
48
+ if (n < 2 || i + 2 + n > b.length) return { why: "the JPEG's header is damaged" };
49
+ if (m === 0xe9 && same(b, i + 4, LABEL)) ours.push({ at: i, end: i + 2 + n, seq: u16At(b, i + 4 + LABEL.length), total: u16At(b, i + 6 + LABEL.length), data: b.subarray(i + 8 + LABEL.length, i + 2 + n) });
50
+ i += 2 + n;
51
+ }
52
+ return { why: "the JPEG's header is damaged" };
53
+ }
54
+ const without = (b, ranges) => { const parts = []; let at = 0; for (const r of ranges) { parts.push(b.subarray(at, r.at)); at = r.end; } parts.push(b.subarray(at)); return concat(...parts); };
55
+ function parsePack(raw) {
56
+ if (raw.length > MAX_PACK) return { damaged: "the proof is too large" };
57
+ try { return { pack: JSON.parse(text(raw)) }; } catch { return { damaged: "the proof can't be read" }; }
58
+ }
59
+
60
+ /**
61
+ * The proof a file carries: null if none, { format, damaged } if it is broken, or { format, pack, original } where `original` is the file without it.
62
+ * ⛔ This only reads. Nothing here says the proof is good, or that it belongs to this file: verifyFile does.
63
+ */
64
+ export function readEmbedded(bytes) {
65
+ const b = new Uint8Array(bytes);
66
+ if (isPng(b)) {
67
+ const { chunks, why } = pngChunks(b);
68
+ if (why) return null;
69
+ const ours = chunks.filter((c) => same(c.type, 0, PNG_TYPE));
70
+ if (!ours.length) return null;
71
+ if (ours.length > 1) return { format: "png", damaged: "the file carries more than one proof" };
72
+ const [c] = ours;
73
+ if (crc32(c.type, c.data) !== c.crc) return { format: "png", damaged: "the proof was changed" };
74
+ if (!same(c.data, 0, LABEL)) return { format: "png", damaged: "the proof can't be read" };
75
+ const p = parsePack(c.data.subarray(LABEL.length));
76
+ return p.damaged ? { format: "png", ...p } : { format: "png", pack: p.pack, original: without(b, [c]) };
77
+ }
78
+ if (isJpeg(b)) {
79
+ const { ours, why } = jpegOurs(b);
80
+ if (why || !ours.length) return null;
81
+ const total = ours[0].total;
82
+ if (ours.length !== total || ours.some((s, i) => s.seq !== i || s.total !== total)) return { format: "jpeg", damaged: "part of the proof is missing" };
83
+ const p = parsePack(concat(...ours.map((s) => s.data)));
84
+ return p.damaged ? { format: "jpeg", ...p } : { format: "jpeg", pack: p.pack, original: without(b, ours) };
85
+ }
86
+ return null;
87
+ }
88
+
89
+ /**
90
+ * A copy of the file with this pack inside. Refuses a pack for any other file. A proof already there is replaced, so a pack can be renewed as it gains its clocks.
91
+ * A pack that holds an `opening` shows the passkey's public key to anyone with the file: every file that carries that opening's key can be linked to the others. That is the signer's choice to make.
92
+ */
93
+ export function embedPack(bytes, pack) {
94
+ if (pack?.kind !== "b-origin-pack" || pack.entry?.t !== "record") throw new TypeError("not a B-Origin pack for a record");
95
+ const b = new Uint8Array(bytes), had = readEmbedded(b);
96
+ if (had?.damaged) throw new Error(`can't renew: ${had.damaged}`);
97
+ const base = had ? had.original : b;
98
+ if (hex(sha256(base)) !== pack.entry.claim?.sha256) throw new Error("this pack describes a different file");
99
+ const raw = utf8(JSON.stringify(pack));
100
+ if (raw.length > MAX_PACK) throw new Error("the pack is too large to carry");
101
+ if (isPng(base)) {
102
+ const { chunks, why } = pngChunks(base);
103
+ if (why) throw new Error(why);
104
+ const end = chunks[chunks.length - 1], data = concat(LABEL, raw);
105
+ return concat(base.subarray(0, end.at), u32(data.length), PNG_TYPE, data, u32(crc32(PNG_TYPE, data)), base.subarray(end.at));
106
+ }
107
+ if (isJpeg(base)) {
108
+ const total = Math.ceil(raw.length / JPEG_DATA);
109
+ if (total > 0xffff) throw new Error("the pack is too large to carry");
110
+ const parts = [base.subarray(0, 2)];
111
+ for (let i = 0; i < total; i++) {
112
+ const data = raw.subarray(i * JPEG_DATA, (i + 1) * JPEG_DATA);
113
+ parts.push(new Uint8Array([0xff, 0xe9]), u16(2 + LABEL.length + 4 + data.length), LABEL, u16(i), u16(total), data);
114
+ }
115
+ parts.push(base.subarray(2));
116
+ return concat(...parts);
117
+ }
118
+ throw new Error("a proof can be carried only in a PNG or a JPEG: keep the pack beside any other file");
119
+ }
package/index.mjs CHANGED
@@ -1,10 +1,12 @@
1
1
  // @333eco/origin-verify. CC0. Checks a B-Origin record with no help from its operator: no runtime dependencies, in a browser or in Node.
2
2
  // ⚠️ Written apart from the service on purpose: it shares no code with services/b, and it checks proofs with RFC 9162's
3
3
  // iterative algorithms where the service builds them with RFC 6962's recursion. One author's blind spots still apply.
4
- import { b64, b64u, concat, der, derSigToRaw, ed25519, eq, hmacSha256, sha256, text, toB64, toB64u, u32, u64, utf8 } from "./primitives.mjs";
4
+ import { b64, b64u, concat, der, derSigToRaw, ed25519, eq, hex, hmacSha256, sha256, text, toB64, toB64u, u32, u64, utf8 } from "./primitives.mjs";
5
5
  import { checkOts, checkTimestamp } from "./clocks.mjs";
6
6
  export { checkOts, checkTimestamp } from "./clocks.mjs";
7
7
  export { hashFile, decodePng, phashOf, phashDistance } from "./media.mjs";
8
+ import { readEmbedded } from "./embed.mjs";
9
+ export { embedPack, readEmbedded } from "./embed.mjs";
8
10
 
9
11
  const NODE = new Uint8Array([1]), LEAF = new Uint8Array([0]);
10
12
  const odd = (n) => n % 2 === 1;
@@ -374,3 +376,19 @@ export async function verifyPack(pack, trust, { header } = {}) {
374
376
  }
375
377
  return out;
376
378
  }
379
+
380
+ /**
381
+ * Checks a file by the proof inside it, with no network. The proof counts only if taking it out gives back exactly the bytes the record names.
382
+ * ⚠️ `withdrawalChecked` is always false here: a pack is a snapshot, and whether the signer has since withdrawn the record is a question for the log (findRecords).
383
+ */
384
+ export async function verifyFile(bytes, trust, opts = {}) {
385
+ const found = readEmbedded(bytes);
386
+ if (!found) return { ok: false, embedded: false, why: "this file carries no proof" };
387
+ if (found.damaged) return { ok: false, embedded: true, why: found.damaged };
388
+ const claim = found.pack?.entry?.claim;
389
+ if (found.pack?.entry?.t !== "record" || !claim) return { ok: false, embedded: true, why: "the proof is not for a record" };
390
+ if (hex(sha256(found.original)) !== claim.sha256) return { ok: false, embedded: true, why: "this proof describes a different file" };
391
+ const out = await verifyPack(found.pack, trust, opts);
392
+ if (!out.ok) return { ...out, embedded: true };
393
+ return { ...out, embedded: true, format: found.format, claim, withdrawalChecked: false };
394
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@333eco/origin-verify",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Checks a B-Origin record with no help from its operator: signatures, its place in the public log, witness cosignatures, RFC 3161 tokens, OpenTimestamps proofs against Bitcoin, and a signer's reveal. Browser or Node, no runtime dependencies.",
5
5
  "license": "CC0-1.0",
6
6
  "author": "Thon Ly",
@@ -32,6 +32,7 @@
32
32
  "primitives.mjs",
33
33
  "clocks.mjs",
34
34
  "media.mjs",
35
+ "embed.mjs",
35
36
  "trust.mjs",
36
37
  "cli.mjs",
37
38
  "README.md"
package/trust.mjs CHANGED
@@ -7,15 +7,18 @@ export default {
7
7
  },
8
8
  "rpId": "sign.333.eco",
9
9
  "webOrigin": "https://sign.333.eco",
10
- "witnessQuorum": 4,
11
- "witnessLabel": "staging witnesses",
10
+ // The Witness Network's TESTING list (2026-11-04). These witnesses may reset or disappear, so they are named and NOT counted: no witnessQuorum, so `witnessed` stays false.
11
+ // A move to staging puts back that list's keys and a quorum. Read from each operator's own page, 2026-11-04.
12
+ "witnessLabel": "testing witnesses",
12
13
  "witnesses": [
13
14
  "witness.navigli.sunlight.geomys.org+a3e00fe2+BNy/co4C1Hn1p+INwJrfUlgz7W55dSZReusH/GhUhJ/G",
14
- "krabbel.ct.ipng.ch+4b10dbd8+BO4sLOtnhktlqMCejDx9C5LDd+/xZxbmBb6BP9ZD2Def",
15
- "witness.gdbsecurity.com+a1398f73+BAbIijGonVIk3ikDac+KKlSm1ELHOzcddhjjB6zB4BNf",
16
15
  "markovianprotocol.com/witness+41b8827f+BJKLl8rBYM0oUxSaWh9rjTldTprEpYj/SWVIGgacA9XC",
17
16
  "witness.stagemole.eu+67f7aea0+BEqSG3yu9YrmcM3BHvQYTxwFj3uSWakQepafafpUqklv",
18
- "staging.witness.transparency.goog/ring-any-bells+2e1a8dc9+BG5JTpLc3FJtwzgh1Uv+Qelz9qeOH2bfWjS1s0s+y4rL"
17
+ "transparency.dev/DEV:witness-little-garden+d8042a87+BCtusOxINQNUTN5Oj8HObRkh2yHf/MwYaGX4CPdiVEPM",
18
+ "kladje.ct.ipng.ch+cdfb671b+BH6mAtKyb9gR+D1sL6TSGLbGoRqcX5AXpBgluXmaimp7",
19
+ "rgdd.se/poc-witness+db03732e+BCjJKlo6BU0xfIb8LutqerIFTWIXEA0L5n3tW3QyPFgG",
20
+ "witness1.smartit.nu/witness1+a48c820f+BPSFWg9G6KPiO7QPryYO5Xq4oYJJ+kAvLKLSimDhoxMO",
21
+ "remora.n621.de+da77ade7+BOvN63jn/bLvkieywe8R6UYAtVtNbZpXh34x7onlmtw2"
19
22
  ],
20
23
  "tsaRoots": {
21
24
  "accv-es": [