@zanii/monitor 0.1.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 +159 -0
- package/README.md +61 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +72 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
- package/src/cli.ts +67 -0
- package/src/index.ts +131 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work; and
|
|
103
|
+
|
|
104
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
105
|
+
distribution, then any Derivative Works that You distribute must
|
|
106
|
+
include a readable copy of the attribution notices contained
|
|
107
|
+
within such NOTICE file.
|
|
108
|
+
|
|
109
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
110
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
111
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
112
|
+
this License, without any additional terms or conditions.
|
|
113
|
+
|
|
114
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
115
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
116
|
+
except as required for reasonable and customary use in describing the
|
|
117
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
118
|
+
|
|
119
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
120
|
+
to in writing, Licensor provides the Work (and each Contributor
|
|
121
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
122
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
123
|
+
without limitation, any warranties or conditions of TITLE,
|
|
124
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
125
|
+
|
|
126
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
127
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
128
|
+
unless required by applicable law (such as deliberate and grossly
|
|
129
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
130
|
+
liable to You for damages, including any direct, indirect, special,
|
|
131
|
+
incidental, or consequential damages of any character arising as a
|
|
132
|
+
result of this License or out of the use or inability to use the
|
|
133
|
+
Work.
|
|
134
|
+
|
|
135
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
136
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
137
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
138
|
+
or other liability obligations and/or rights consistent with this
|
|
139
|
+
License. However, in accepting such obligations, You may act only
|
|
140
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
141
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
142
|
+
defend, and hold each Contributor harmless for any liability
|
|
143
|
+
incurred by, or claims asserted against, such Contributor.
|
|
144
|
+
|
|
145
|
+
END OF TERMS AND CONDITIONS
|
|
146
|
+
|
|
147
|
+
Copyright 2026 Zanii
|
|
148
|
+
|
|
149
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
150
|
+
you may not use this file except in compliance with the License.
|
|
151
|
+
You may obtain a copy of the License at
|
|
152
|
+
|
|
153
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
154
|
+
|
|
155
|
+
Unless required by applicable law or agreed to in writing, software
|
|
156
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
157
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
158
|
+
See the License for the specific language governing permissions and
|
|
159
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# @zanii/monitor
|
|
2
|
+
|
|
3
|
+
An **independent watchdog** for a Zanii transparency log. "Trust no one, including
|
|
4
|
+
Zanii" is only a claim until someone continuously checks it — this is that someone.
|
|
5
|
+
It verifies each Signed Tree Head's signature, proves the log is **append-only**
|
|
6
|
+
(a consistency proof from the last size it saw — rewriting past history fails this),
|
|
7
|
+
and confirms every **anchored** checkpoint is still an append-only prefix of the
|
|
8
|
+
current tree. Any violation is surfaced loudly.
|
|
9
|
+
|
|
10
|
+
Run it as a library or the `zanii-monitor` CLI. Node 18+.
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @zanii/monitor
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## CLI
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
# watch continuously, persist state, POST alerts on violation
|
|
20
|
+
zanii-monitor --server https://ledger.zanii.agency --interval 60 \
|
|
21
|
+
--state ./monitor.state --alert https://your-app/alert
|
|
22
|
+
|
|
23
|
+
# one-shot for CI/cron — exits 1 on any violation
|
|
24
|
+
zanii-monitor --server https://ledger.zanii.agency --once
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Persisting `--state` across runs is what makes an *offline* rewrite impossible to
|
|
28
|
+
hide: the next check proves the log is consistent with the size you last verified.
|
|
29
|
+
|
|
30
|
+
## Library
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { checkOnce, monitor } from '@zanii/monitor';
|
|
34
|
+
|
|
35
|
+
const r = await checkOnce('https://ledger.zanii.agency', previousState);
|
|
36
|
+
if (!r.ok) console.error('VIOLATION:', r.violations);
|
|
37
|
+
// persist r.state and pass it back next time
|
|
38
|
+
|
|
39
|
+
await monitor('https://ledger.zanii.agency', {
|
|
40
|
+
intervalMs: 60_000,
|
|
41
|
+
onViolation: (r) => pageOncall(r.violations),
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## What it checks
|
|
46
|
+
|
|
47
|
+
| Check | Proves |
|
|
48
|
+
|---|---|
|
|
49
|
+
| STH signature | the tree head really came from the log operator |
|
|
50
|
+
| Consistency (last size → now) | the log is **append-only** — no past entry was altered, removed, or reordered |
|
|
51
|
+
| Anchor validity + extension | the current tree is an append-only extension of every on-chain checkpoint |
|
|
52
|
+
|
|
53
|
+
`MonitorResult = { ok, size, root, checks[], violations[], state }`. Persist `state`.
|
|
54
|
+
|
|
55
|
+
## Changelog
|
|
56
|
+
|
|
57
|
+
- **0.1.0** — initial release: `checkOnce`, `monitor`, and the `zanii-monitor` CLI.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
Apache-2.0.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* zanii-monitor — watch a Zanii log for any append-only or anchoring violation.
|
|
4
|
+
*
|
|
5
|
+
* zanii-monitor --server https://ledger.zanii.agency --interval 60 \
|
|
6
|
+
* --state ./monitor.state --alert https://you/webhook
|
|
7
|
+
* zanii-monitor --server https://ledger.zanii.agency --once # exit 1 on violation
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { checkOnce, monitor } from './index.js';
|
|
11
|
+
const argOf = (name) => {
|
|
12
|
+
const i = process.argv.indexOf(`--${name}`);
|
|
13
|
+
return i >= 0 ? process.argv[i + 1] : undefined;
|
|
14
|
+
};
|
|
15
|
+
const has = (name) => process.argv.includes(`--${name}`);
|
|
16
|
+
const server = argOf('server');
|
|
17
|
+
if (!server) {
|
|
18
|
+
console.error('usage: zanii-monitor --server <url> [--interval <sec>] [--once] [--state <file>] [--alert <webhook-url>]');
|
|
19
|
+
process.exit(2);
|
|
20
|
+
}
|
|
21
|
+
const stateFile = argOf('state');
|
|
22
|
+
const alertUrl = argOf('alert');
|
|
23
|
+
const loadState = () => {
|
|
24
|
+
if (stateFile && existsSync(stateFile)) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(readFileSync(stateFile, 'utf8'));
|
|
27
|
+
}
|
|
28
|
+
catch { /* ignore */ }
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
const saveState = (s) => { if (stateFile)
|
|
33
|
+
writeFileSync(stateFile, JSON.stringify(s)); };
|
|
34
|
+
const line = (r) => {
|
|
35
|
+
const ts = new Date().toISOString();
|
|
36
|
+
return r.ok
|
|
37
|
+
? `${ts} OK size=${r.size} root=${r.root.slice(0, 24)}… (${r.checks.length} checks passed)`
|
|
38
|
+
: `${ts} VIOLATION size=${r.size} :: ${r.violations.join(' ; ')}`;
|
|
39
|
+
};
|
|
40
|
+
const alert = async (r) => {
|
|
41
|
+
if (!alertUrl)
|
|
42
|
+
return;
|
|
43
|
+
try {
|
|
44
|
+
await fetch(alertUrl, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'content-type': 'application/json' },
|
|
47
|
+
body: JSON.stringify({ source: 'zanii-monitor', server, ...r }),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch { /* best-effort */ }
|
|
51
|
+
};
|
|
52
|
+
if (has('once')) {
|
|
53
|
+
const r = await checkOnce(server, loadState());
|
|
54
|
+
console.log(line(r));
|
|
55
|
+
saveState(r.state);
|
|
56
|
+
if (!r.ok) {
|
|
57
|
+
await alert(r);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const interval = Number(argOf('interval') ?? '60') * 1000;
|
|
64
|
+
console.error(`zanii-monitor: watching ${server} every ${interval / 1000}s${alertUrl ? ` (alerting ${alertUrl})` : ''}`);
|
|
65
|
+
await monitor(server, {
|
|
66
|
+
intervalMs: interval,
|
|
67
|
+
initialState: loadState(),
|
|
68
|
+
onResult: (r) => { console.log(line(r)); saveState(r.state); },
|
|
69
|
+
onViolation: alert,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;GAMG;AACH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAyC,MAAM,YAAY,CAAC;AAEvF,MAAM,KAAK,GAAG,CAAC,IAAY,EAAsB,EAAE;IACjD,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAC5C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,GAA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,GAAG,IAAI,IAAI,QAAQ,CAAC,CAAC,CAAC,cAAc,QAAQ,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACzH,MAAM,OAAO,CAAC,MAAM,EAAE;QACpB,UAAU,EAAE,QAAQ;QACpB,YAAY,EAAE,SAAS,EAAE;QACzB,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC9D,WAAW,EAAE,KAAK;KACnB,CAAC,CAAC;AACL,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
2
|
+
/** Persist this between runs so an offline rewrite can't slip past. */
|
|
3
|
+
export interface MonitorState {
|
|
4
|
+
size: number;
|
|
5
|
+
root: string;
|
|
6
|
+
}
|
|
7
|
+
export interface Check {
|
|
8
|
+
name: string;
|
|
9
|
+
ok: boolean;
|
|
10
|
+
detail?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface MonitorResult {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
size: number;
|
|
15
|
+
root: string;
|
|
16
|
+
checks: Check[];
|
|
17
|
+
violations: string[];
|
|
18
|
+
/** The verified head — persist this and pass as `prev` next time. */
|
|
19
|
+
state: MonitorState;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Run one integrity check. Pass the `state` returned last time as `prev` to prove
|
|
23
|
+
* the log only grew (never rewrote) since you last looked.
|
|
24
|
+
*/
|
|
25
|
+
export declare function checkOnce(server: string, prev?: MonitorState, fetchImpl?: FetchLike): Promise<MonitorResult>;
|
|
26
|
+
export interface MonitorOptions {
|
|
27
|
+
intervalMs?: number;
|
|
28
|
+
fetchImpl?: FetchLike;
|
|
29
|
+
initialState?: MonitorState;
|
|
30
|
+
onResult?: (r: MonitorResult) => void | Promise<void>;
|
|
31
|
+
onViolation?: (r: MonitorResult) => void | Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/** Run forever, checking every `intervalMs` (default 60s). Never resolves. */
|
|
34
|
+
export declare function monitor(server: string, opts?: MonitorOptions): Promise<never>;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEjF,uEAAuE;AACvE,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,OAAO,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,qEAAqE;IACrE,KAAK,EAAE,YAAY,CAAC;CACrB;AAQD;;;GAGG;AACH,wBAAsB,SAAS,CAC7B,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,YAAY,EACnB,SAAS,GAAE,SAAiC,GAC3C,OAAO,CAAC,aAAa,CAAC,CAoDxB;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD,WAAW,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAED,8EAA8E;AAC9E,wBAAsB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,cAAmB,GAAG,OAAO,CAAC,KAAK,CAAC,CAevF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/monitor — an independent watchdog for a Zanii transparency log.
|
|
3
|
+
*
|
|
4
|
+
* "Trust no one, including Zanii" is a claim until someone continuously checks it.
|
|
5
|
+
* This monitor does: it verifies each Signed Tree Head's signature, proves the log
|
|
6
|
+
* is **append-only** (a consistency proof from the last size it saw — a rewrite of
|
|
7
|
+
* past history fails this), and confirms every **anchored** checkpoint is still an
|
|
8
|
+
* append-only prefix of the current tree. Any violation is surfaced loudly.
|
|
9
|
+
*
|
|
10
|
+
* Run it as a library (`checkOnce`, `monitor`) or via the `zanii-monitor` CLI.
|
|
11
|
+
*/
|
|
12
|
+
import { hexToBytes } from '@noble/hashes/utils';
|
|
13
|
+
import { verifyConsistency, verifySTH } from '@zanii/core';
|
|
14
|
+
const toRoot = (s) => hexToBytes(s.replace('sha256:', ''));
|
|
15
|
+
const short = (s) => {
|
|
16
|
+
const t = String(s ?? '');
|
|
17
|
+
return t.length > 16 ? t.slice(0, 16) + '…' : t;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Run one integrity check. Pass the `state` returned last time as `prev` to prove
|
|
21
|
+
* the log only grew (never rewrote) since you last looked.
|
|
22
|
+
*/
|
|
23
|
+
export async function checkOnce(server, prev, fetchImpl = (u, i) => fetch(u, i)) {
|
|
24
|
+
const base = server.replace(/\/$/, '');
|
|
25
|
+
const get = async (p) => {
|
|
26
|
+
const r = await fetchImpl(`${base}${p}`);
|
|
27
|
+
if (!r.ok)
|
|
28
|
+
throw new Error(`${p} → ${r.status}`);
|
|
29
|
+
return r.json();
|
|
30
|
+
};
|
|
31
|
+
const checks = [];
|
|
32
|
+
const violations = [];
|
|
33
|
+
const push = (name, ok, detail) => {
|
|
34
|
+
checks.push({ name, ok, detail });
|
|
35
|
+
if (!ok)
|
|
36
|
+
violations.push(detail ?? name);
|
|
37
|
+
};
|
|
38
|
+
const sth = (await get('/v1/sth'));
|
|
39
|
+
push('signed tree head signature', verifySTH(sth) && !!sth.log_id, 'STH signature invalid');
|
|
40
|
+
if (prev) {
|
|
41
|
+
if (sth.size < prev.size) {
|
|
42
|
+
push(`append-only (size ${sth.size} ≥ ${prev.size})`, false, `log SHRANK: size ${sth.size} < previously seen ${prev.size}`);
|
|
43
|
+
}
|
|
44
|
+
else if (sth.size === prev.size) {
|
|
45
|
+
push('append-only (root unchanged at same size)', sth.root === prev.root, `root changed at unchanged size ${sth.size} — history was rewritten`);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const c = await get(`/v1/consistency?first=${prev.size}`);
|
|
49
|
+
const ok = verifyConsistency(prev.size, sth.size, toRoot(prev.root), toRoot(sth.root), c.proof.map(hexToBytes));
|
|
50
|
+
push(`append-only (consistency ${prev.size}→${sth.size})`, ok, `consistency proof ${prev.size}→${sth.size} FAILED — history was rewritten`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Every anchored checkpoint must be an append-only prefix of the current tree.
|
|
54
|
+
try {
|
|
55
|
+
const { anchors } = (await get('/v1/anchors'));
|
|
56
|
+
for (const a of anchors ?? []) {
|
|
57
|
+
let ok = verifySTH(a.sth) && a.sth.log_id === sth.log_id && a.sth.size <= sth.size;
|
|
58
|
+
if (ok && a.sth.size < sth.size) {
|
|
59
|
+
const c = await get(`/v1/consistency?first=${a.sth.size}`);
|
|
60
|
+
ok = verifyConsistency(a.sth.size, sth.size, toRoot(a.sth.root), toRoot(sth.root), c.proof.map(hexToBytes));
|
|
61
|
+
}
|
|
62
|
+
push(`anchor ${short(a.ref)} valid + extended`, ok, `anchor ${short(a.ref)}: invalid STH or current tree is not an append-only extension of it`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
/* anchors endpoint optional */
|
|
67
|
+
}
|
|
68
|
+
return { ok: violations.length === 0, size: sth.size, root: sth.root, checks, violations, state: { size: sth.size, root: sth.root } };
|
|
69
|
+
}
|
|
70
|
+
/** Run forever, checking every `intervalMs` (default 60s). Never resolves. */
|
|
71
|
+
export async function monitor(server, opts = {}) {
|
|
72
|
+
const interval = opts.intervalMs ?? 60_000;
|
|
73
|
+
const impl = opts.fetchImpl ?? ((u, i) => fetch(u, i));
|
|
74
|
+
let state = opts.initialState;
|
|
75
|
+
for (;;) {
|
|
76
|
+
try {
|
|
77
|
+
const r = await checkOnce(server, state, impl);
|
|
78
|
+
state = r.state;
|
|
79
|
+
await opts.onResult?.(r);
|
|
80
|
+
if (!r.ok)
|
|
81
|
+
await opts.onViolation?.(r);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
await opts.onResult?.({ ok: false, size: -1, root: '', checks: [], violations: [String(e)], state: state ?? { size: 0, root: '' } });
|
|
85
|
+
}
|
|
86
|
+
await new Promise((res) => setTimeout(res, interval));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAuB,MAAM,aAAa,CAAC;AA0BhF,MAAM,MAAM,GAAG,CAAC,CAAS,EAAc,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,C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or",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Independent Zanii log monitor: continuously verify the transparency log is append-only (consistency proofs) and that anchored checkpoints hold. Makes 'trust no one' operational.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://ledger.zanii.agency",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"zanii-monitor": "./dist/cli.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@noble/hashes": "^1.7.1",
|
|
33
|
+
"@zanii/core": "0.2.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^26.1.0",
|
|
37
|
+
"typescript": "^5.8.2",
|
|
38
|
+
"vitest": "^3.0.9",
|
|
39
|
+
"@zanii/testing": "0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc -p tsconfig.build.json",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"typecheck": "tsc --noEmit"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* zanii-monitor — watch a Zanii log for any append-only or anchoring violation.
|
|
4
|
+
*
|
|
5
|
+
* zanii-monitor --server https://ledger.zanii.agency --interval 60 \
|
|
6
|
+
* --state ./monitor.state --alert https://you/webhook
|
|
7
|
+
* zanii-monitor --server https://ledger.zanii.agency --once # exit 1 on violation
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { checkOnce, monitor, type MonitorResult, type MonitorState } from './index.js';
|
|
11
|
+
|
|
12
|
+
const argOf = (name: string): string | undefined => {
|
|
13
|
+
const i = process.argv.indexOf(`--${name}`);
|
|
14
|
+
return i >= 0 ? process.argv[i + 1] : undefined;
|
|
15
|
+
};
|
|
16
|
+
const has = (name: string): boolean => process.argv.includes(`--${name}`);
|
|
17
|
+
|
|
18
|
+
const server = argOf('server');
|
|
19
|
+
if (!server) {
|
|
20
|
+
console.error('usage: zanii-monitor --server <url> [--interval <sec>] [--once] [--state <file>] [--alert <webhook-url>]');
|
|
21
|
+
process.exit(2);
|
|
22
|
+
}
|
|
23
|
+
const stateFile = argOf('state');
|
|
24
|
+
const alertUrl = argOf('alert');
|
|
25
|
+
|
|
26
|
+
const loadState = (): MonitorState | undefined => {
|
|
27
|
+
if (stateFile && existsSync(stateFile)) {
|
|
28
|
+
try { return JSON.parse(readFileSync(stateFile, 'utf8')) as MonitorState; } catch { /* ignore */ }
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
const saveState = (s: MonitorState): void => { if (stateFile) writeFileSync(stateFile, JSON.stringify(s)); };
|
|
33
|
+
|
|
34
|
+
const line = (r: MonitorResult): string => {
|
|
35
|
+
const ts = new Date().toISOString();
|
|
36
|
+
return r.ok
|
|
37
|
+
? `${ts} OK size=${r.size} root=${r.root.slice(0, 24)}… (${r.checks.length} checks passed)`
|
|
38
|
+
: `${ts} VIOLATION size=${r.size} :: ${r.violations.join(' ; ')}`;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const alert = async (r: MonitorResult): Promise<void> => {
|
|
42
|
+
if (!alertUrl) return;
|
|
43
|
+
try {
|
|
44
|
+
await fetch(alertUrl, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'content-type': 'application/json' },
|
|
47
|
+
body: JSON.stringify({ source: 'zanii-monitor', server, ...r }),
|
|
48
|
+
});
|
|
49
|
+
} catch { /* best-effort */ }
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
if (has('once')) {
|
|
53
|
+
const r = await checkOnce(server, loadState());
|
|
54
|
+
console.log(line(r));
|
|
55
|
+
saveState(r.state);
|
|
56
|
+
if (!r.ok) { await alert(r); process.exit(1); }
|
|
57
|
+
process.exit(0);
|
|
58
|
+
} else {
|
|
59
|
+
const interval = Number(argOf('interval') ?? '60') * 1000;
|
|
60
|
+
console.error(`zanii-monitor: watching ${server} every ${interval / 1000}s${alertUrl ? ` (alerting ${alertUrl})` : ''}`);
|
|
61
|
+
await monitor(server, {
|
|
62
|
+
intervalMs: interval,
|
|
63
|
+
initialState: loadState(),
|
|
64
|
+
onResult: (r) => { console.log(line(r)); saveState(r.state); },
|
|
65
|
+
onViolation: alert,
|
|
66
|
+
});
|
|
67
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/monitor — an independent watchdog for a Zanii transparency log.
|
|
3
|
+
*
|
|
4
|
+
* "Trust no one, including Zanii" is a claim until someone continuously checks it.
|
|
5
|
+
* This monitor does: it verifies each Signed Tree Head's signature, proves the log
|
|
6
|
+
* is **append-only** (a consistency proof from the last size it saw — a rewrite of
|
|
7
|
+
* past history fails this), and confirms every **anchored** checkpoint is still an
|
|
8
|
+
* append-only prefix of the current tree. Any violation is surfaced loudly.
|
|
9
|
+
*
|
|
10
|
+
* Run it as a library (`checkOnce`, `monitor`) or via the `zanii-monitor` CLI.
|
|
11
|
+
*/
|
|
12
|
+
import { hexToBytes } from '@noble/hashes/utils';
|
|
13
|
+
import { verifyConsistency, verifySTH, type SignedTreeHead } from '@zanii/core';
|
|
14
|
+
|
|
15
|
+
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
16
|
+
|
|
17
|
+
/** Persist this between runs so an offline rewrite can't slip past. */
|
|
18
|
+
export interface MonitorState {
|
|
19
|
+
size: number;
|
|
20
|
+
root: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Check {
|
|
24
|
+
name: string;
|
|
25
|
+
ok: boolean;
|
|
26
|
+
detail?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MonitorResult {
|
|
30
|
+
ok: boolean;
|
|
31
|
+
size: number;
|
|
32
|
+
root: string;
|
|
33
|
+
checks: Check[];
|
|
34
|
+
violations: string[];
|
|
35
|
+
/** The verified head — persist this and pass as `prev` next time. */
|
|
36
|
+
state: MonitorState;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const toRoot = (s: string): Uint8Array => hexToBytes(s.replace('sha256:', ''));
|
|
40
|
+
const short = (s: unknown): string => {
|
|
41
|
+
const t = String(s ?? '');
|
|
42
|
+
return t.length > 16 ? t.slice(0, 16) + '…' : t;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Run one integrity check. Pass the `state` returned last time as `prev` to prove
|
|
47
|
+
* the log only grew (never rewrote) since you last looked.
|
|
48
|
+
*/
|
|
49
|
+
export async function checkOnce(
|
|
50
|
+
server: string,
|
|
51
|
+
prev?: MonitorState,
|
|
52
|
+
fetchImpl: FetchLike = (u, i) => fetch(u, i),
|
|
53
|
+
): Promise<MonitorResult> {
|
|
54
|
+
const base = server.replace(/\/$/, '');
|
|
55
|
+
const get = async (p: string): Promise<any> => {
|
|
56
|
+
const r = await fetchImpl(`${base}${p}`);
|
|
57
|
+
if (!r.ok) throw new Error(`${p} → ${r.status}`);
|
|
58
|
+
return r.json();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const checks: Check[] = [];
|
|
62
|
+
const violations: string[] = [];
|
|
63
|
+
const push = (name: string, ok: boolean, detail?: string) => {
|
|
64
|
+
checks.push({ name, ok, detail });
|
|
65
|
+
if (!ok) violations.push(detail ?? name);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const sth = (await get('/v1/sth')) as SignedTreeHead;
|
|
69
|
+
push('signed tree head signature', verifySTH(sth) && !!sth.log_id, 'STH signature invalid');
|
|
70
|
+
|
|
71
|
+
if (prev) {
|
|
72
|
+
if (sth.size < prev.size) {
|
|
73
|
+
push(`append-only (size ${sth.size} ≥ ${prev.size})`, false,
|
|
74
|
+
`log SHRANK: size ${sth.size} < previously seen ${prev.size}`);
|
|
75
|
+
} else if (sth.size === prev.size) {
|
|
76
|
+
push('append-only (root unchanged at same size)', sth.root === prev.root,
|
|
77
|
+
`root changed at unchanged size ${sth.size} — history was rewritten`);
|
|
78
|
+
} else {
|
|
79
|
+
const c = await get(`/v1/consistency?first=${prev.size}`);
|
|
80
|
+
const ok = verifyConsistency(prev.size, sth.size, toRoot(prev.root), toRoot(sth.root),
|
|
81
|
+
(c.proof as string[]).map(hexToBytes));
|
|
82
|
+
push(`append-only (consistency ${prev.size}→${sth.size})`, ok,
|
|
83
|
+
`consistency proof ${prev.size}→${sth.size} FAILED — history was rewritten`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Every anchored checkpoint must be an append-only prefix of the current tree.
|
|
88
|
+
try {
|
|
89
|
+
const { anchors } = (await get('/v1/anchors')) as { anchors?: Array<{ sth: SignedTreeHead; ref?: unknown }> };
|
|
90
|
+
for (const a of anchors ?? []) {
|
|
91
|
+
let ok = verifySTH(a.sth) && a.sth.log_id === sth.log_id && a.sth.size <= sth.size;
|
|
92
|
+
if (ok && a.sth.size < sth.size) {
|
|
93
|
+
const c = await get(`/v1/consistency?first=${a.sth.size}`);
|
|
94
|
+
ok = verifyConsistency(a.sth.size, sth.size, toRoot(a.sth.root), toRoot(sth.root),
|
|
95
|
+
(c.proof as string[]).map(hexToBytes));
|
|
96
|
+
}
|
|
97
|
+
push(`anchor ${short(a.ref)} valid + extended`, ok,
|
|
98
|
+
`anchor ${short(a.ref)}: invalid STH or current tree is not an append-only extension of it`);
|
|
99
|
+
}
|
|
100
|
+
} catch {
|
|
101
|
+
/* anchors endpoint optional */
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { ok: violations.length === 0, size: sth.size, root: sth.root, checks, violations, state: { size: sth.size, root: sth.root } };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface MonitorOptions {
|
|
108
|
+
intervalMs?: number;
|
|
109
|
+
fetchImpl?: FetchLike;
|
|
110
|
+
initialState?: MonitorState;
|
|
111
|
+
onResult?: (r: MonitorResult) => void | Promise<void>;
|
|
112
|
+
onViolation?: (r: MonitorResult) => void | Promise<void>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Run forever, checking every `intervalMs` (default 60s). Never resolves. */
|
|
116
|
+
export async function monitor(server: string, opts: MonitorOptions = {}): Promise<never> {
|
|
117
|
+
const interval = opts.intervalMs ?? 60_000;
|
|
118
|
+
const impl = opts.fetchImpl ?? ((u: string, i?: RequestInit) => fetch(u, i));
|
|
119
|
+
let state = opts.initialState;
|
|
120
|
+
for (;;) {
|
|
121
|
+
try {
|
|
122
|
+
const r = await checkOnce(server, state, impl);
|
|
123
|
+
state = r.state;
|
|
124
|
+
await opts.onResult?.(r);
|
|
125
|
+
if (!r.ok) await opts.onViolation?.(r);
|
|
126
|
+
} catch (e) {
|
|
127
|
+
await opts.onResult?.({ ok: false, size: -1, root: '', checks: [], violations: [String(e)], state: state ?? { size: 0, root: '' } });
|
|
128
|
+
}
|
|
129
|
+
await new Promise((res) => setTimeout(res, interval));
|
|
130
|
+
}
|
|
131
|
+
}
|