@serfab/cadre-core 0.7.1 → 0.9.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 +11 -10
- package/dist/cadre-node.d.ts +818 -19
- package/dist/cadre-node.js +1952 -99
- package/dist/cadre-node.js.map +1 -1
- package/dist/canonical-datetime.d.ts +33 -0
- package/dist/canonical-datetime.js +38 -0
- package/dist/canonical-datetime.js.map +1 -0
- package/dist/canonical-json.d.ts +12 -0
- package/dist/canonical-json.js +33 -0
- package/dist/canonical-json.js.map +1 -0
- package/dist/control-cohort.d.ts +63 -0
- package/dist/control-cohort.js +80 -0
- package/dist/control-cohort.js.map +1 -0
- package/dist/control-database.d.ts +279 -8
- package/dist/control-database.js +563 -134
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +89 -0
- package/dist/control-formation-recorder.js +139 -0
- package/dist/control-formation-recorder.js.map +1 -0
- package/dist/control-schema.d.ts +12 -0
- package/dist/control-schema.js +223 -0
- package/dist/control-schema.js.map +1 -0
- package/dist/control-stream.d.ts +54 -0
- package/dist/control-stream.js +104 -0
- package/dist/control-stream.js.map +1 -0
- package/dist/device-token.d.ts +50 -0
- package/dist/device-token.js +66 -0
- package/dist/device-token.js.map +1 -0
- package/dist/diagnostics/connection-path.d.ts +118 -0
- package/dist/diagnostics/connection-path.js +204 -0
- package/dist/diagnostics/connection-path.js.map +1 -0
- package/dist/diagnostics/webrtc-turn-tracker.d.ts +50 -0
- package/dist/diagnostics/webrtc-turn-tracker.js +196 -0
- package/dist/diagnostics/webrtc-turn-tracker.js.map +1 -0
- package/dist/ed25519-key.d.ts +42 -0
- package/dist/ed25519-key.js +51 -0
- package/dist/ed25519-key.js.map +1 -0
- package/dist/hibernation-manager.d.ts +79 -0
- package/dist/hibernation-manager.js +173 -23
- package/dist/hibernation-manager.js.map +1 -1
- package/dist/index.d.ts +25 -4
- package/dist/index.js +59 -4
- package/dist/index.js.map +1 -1
- package/dist/key-store-file.d.ts +52 -0
- package/dist/key-store-file.js +194 -0
- package/dist/key-store-file.js.map +1 -0
- package/dist/key-store.d.ts +75 -0
- package/dist/key-store.js +66 -0
- package/dist/key-store.js.map +1 -0
- package/dist/peer-authorization.d.ts +48 -0
- package/dist/peer-authorization.js +66 -0
- package/dist/peer-authorization.js.map +1 -0
- package/dist/peer-record.d.ts +89 -0
- package/dist/peer-record.js +117 -0
- package/dist/peer-record.js.map +1 -0
- package/dist/push-credentials.d.ts +34 -0
- package/dist/push-credentials.js +81 -0
- package/dist/push-credentials.js.map +1 -0
- package/dist/push-fanout.d.ts +137 -0
- package/dist/push-fanout.js +197 -0
- package/dist/push-fanout.js.map +1 -0
- package/dist/push-node.d.ts +38 -0
- package/dist/push-node.js +48 -0
- package/dist/push-node.js.map +1 -0
- package/dist/push-notifier-apns.d.ts +61 -0
- package/dist/push-notifier-apns.js +199 -0
- package/dist/push-notifier-apns.js.map +1 -0
- package/dist/push-notifier-fcm.d.ts +44 -0
- package/dist/push-notifier-fcm.js +182 -0
- package/dist/push-notifier-fcm.js.map +1 -0
- package/dist/push-notifier-shared.d.ts +18 -0
- package/dist/push-notifier-shared.js +25 -0
- package/dist/push-notifier-shared.js.map +1 -0
- package/dist/push-notifier.d.ts +52 -0
- package/dist/push-notifier.js +22 -0
- package/dist/push-notifier.js.map +1 -0
- package/dist/schema-verification.d.ts +11 -2
- package/dist/schema-verification.js +15 -4
- package/dist/schema-verification.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +228 -21
- package/dist/seed-bootstrap.js +489 -168
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/seed-trust-policy.d.ts +63 -0
- package/dist/seed-trust-policy.js +56 -0
- package/dist/seed-trust-policy.js.map +1 -0
- package/dist/strand-addr-protocol.d.ts +132 -0
- package/dist/strand-addr-protocol.js +313 -0
- package/dist/strand-addr-protocol.js.map +1 -0
- package/dist/strand-cohort.d.ts +54 -0
- package/dist/strand-cohort.js +38 -0
- package/dist/strand-cohort.js.map +1 -0
- package/dist/strand-database.d.ts +54 -6
- package/dist/strand-database.js +88 -100
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-formation-manager.d.ts +79 -19
- package/dist/strand-formation-manager.js +196 -104
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +162 -0
- package/dist/strand-formation-protocol.js +279 -0
- package/dist/strand-formation-protocol.js.map +1 -0
- package/dist/strand-instance-manager.d.ts +73 -4
- package/dist/strand-instance-manager.js +167 -41
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-key.d.ts +29 -0
- package/dist/strand-member-key.js +38 -0
- package/dist/strand-member-key.js.map +1 -0
- package/dist/strand-member-registry.d.ts +98 -0
- package/dist/strand-member-registry.js +129 -0
- package/dist/strand-member-registry.js.map +1 -0
- package/dist/strand-membership-writer.d.ts +361 -0
- package/dist/strand-membership-writer.js +449 -0
- package/dist/strand-membership-writer.js.map +1 -0
- package/dist/strand-solicitation.d.ts +77 -2
- package/dist/strand-solicitation.js +25 -4
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-payload.d.ts +30 -0
- package/dist/strand-wake-payload.js +20 -0
- package/dist/strand-wake-payload.js.map +1 -0
- package/dist/strand-wake-protocol.d.ts +114 -0
- package/dist/strand-wake-protocol.js +257 -0
- package/dist/strand-wake-protocol.js.map +1 -0
- package/dist/strand-watcher.d.ts +6 -2
- package/dist/strand-watcher.js +57 -17
- package/dist/strand-watcher.js.map +1 -1
- package/dist/types.d.ts +436 -33
- package/dist/types.js +22 -5
- package/dist/types.js.map +1 -1
- package/package.json +18 -8
- package/src/cadre-node.ts +2981 -948
- package/src/canonical-datetime.ts +39 -0
- package/src/canonical-json.ts +30 -0
- package/src/control-cohort.ts +103 -0
- package/src/control-database.ts +948 -383
- package/src/control-formation-recorder.ts +151 -0
- package/src/control-schema.ts +222 -0
- package/src/control-stream.ts +128 -0
- package/src/device-token.ts +89 -0
- package/src/diagnostics/connection-path.ts +277 -0
- package/src/diagnostics/webrtc-turn-tracker.ts +222 -0
- package/src/ed25519-key.ts +69 -0
- package/src/hibernation-manager.ts +197 -28
- package/src/index.ts +275 -79
- package/src/key-store-file.ts +203 -0
- package/src/key-store.ts +99 -0
- package/src/peer-authorization.ts +81 -0
- package/src/peer-record.ts +146 -0
- package/src/push-credentials.ts +84 -0
- package/src/push-fanout.ts +272 -0
- package/src/push-node.ts +64 -0
- package/src/push-notifier-apns.ts +247 -0
- package/src/push-notifier-fcm.ts +220 -0
- package/src/push-notifier-shared.ts +28 -0
- package/src/push-notifier.ts +52 -0
- package/src/schema-verification.ts +18 -4
- package/src/seed-bootstrap.ts +563 -223
- package/src/seed-trust-policy.ts +107 -0
- package/src/strand-addr-protocol.ts +410 -0
- package/src/strand-cohort.ts +77 -0
- package/src/strand-database.ts +113 -141
- package/src/strand-formation-manager.ts +233 -142
- package/src/strand-formation-protocol.ts +391 -0
- package/src/strand-instance-manager.ts +207 -56
- package/src/strand-member-key.ts +39 -0
- package/src/strand-member-registry.ts +177 -0
- package/src/strand-membership-writer.ts +640 -0
- package/src/strand-solicitation.ts +93 -4
- package/src/strand-wake-payload.ts +32 -0
- package/src/strand-wake-protocol.ts +331 -0
- package/src/strand-watcher.ts +61 -17
- package/src/types.ts +1032 -582
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-only file-backed {@link KeyStore}: one file per slot under a configured
|
|
3
|
+
* directory, holding the raw key material bytes.
|
|
4
|
+
*
|
|
5
|
+
* This module imports `node:fs/promises`, `node:path`, and `node:crypto`, so it
|
|
6
|
+
* is deliberately kept OUT of the package's cross-platform default entry
|
|
7
|
+
* (`./index.js`). Import it from the dedicated subpath instead:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { FileKeyStore } from '@serfab/cadre-core/key-store-file';
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* A React Native / browser entry graph never resolves this path, so the
|
|
14
|
+
* `node:fs` edge never reaches a bundler that cannot satisfy it.
|
|
15
|
+
*/
|
|
16
|
+
import debug from 'debug';
|
|
17
|
+
import { mkdir, readFile, rm, readdir, rename, open } from 'node:fs/promises';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { randomBytes } from 'node:crypto';
|
|
20
|
+
import { KeyStoreAccessError } from './key-store.js';
|
|
21
|
+
const log = debug('sereus:cadre:key-store-file');
|
|
22
|
+
/** Suffix appended to the encoded keyId to form a slot filename. */
|
|
23
|
+
const SLOT_SUFFIX = '.key';
|
|
24
|
+
/**
|
|
25
|
+
* Suffix for the transient file written then atomically renamed over the slot.
|
|
26
|
+
* Distinct from {@link SLOT_SUFFIX}, so {@link FileKeyStore.list} (which matches
|
|
27
|
+
* only `.key`) never surfaces an in-flight or crash-orphaned temp file.
|
|
28
|
+
*/
|
|
29
|
+
const TEMP_SUFFIX = '.tmp';
|
|
30
|
+
/** Best-effort POSIX permissions (ignored on Windows / unsupported FSes). */
|
|
31
|
+
const DIR_MODE = 0o700;
|
|
32
|
+
const FILE_MODE = 0o600;
|
|
33
|
+
/**
|
|
34
|
+
* Percent-encode a keyId into a filesystem-safe filename component. Beyond
|
|
35
|
+
* `encodeURIComponent` (which already escapes `/`, spaces, and unicode), this
|
|
36
|
+
* also escapes the unreserved characters `encodeURIComponent` leaves intact but
|
|
37
|
+
* that are unsafe in filenames on some platforms (notably `*` on Windows) — and
|
|
38
|
+
* `.` so the `.key` suffix is the only literal dot, making suffix-stripping in
|
|
39
|
+
* {@link decodeKeyId} unambiguous. {@link decodeKeyId} reverses every escape via
|
|
40
|
+
* `decodeURIComponent`. The result contains only `A-Za-z0-9-_` and `%XX`.
|
|
41
|
+
*/
|
|
42
|
+
function encodeKeyId(keyId) {
|
|
43
|
+
return encodeURIComponent(keyId).replace(/[!'()*~.]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Reverse {@link encodeKeyId}: strip the slot suffix and percent-decode. Returns
|
|
47
|
+
* `undefined` for a `.key` filename this store did not write (a foreign file
|
|
48
|
+
* with an invalid percent-sequence), so {@link FileKeyStore.list} can skip it
|
|
49
|
+
* rather than letting one undecodable entry throw and break enumeration of every
|
|
50
|
+
* real slot.
|
|
51
|
+
*/
|
|
52
|
+
function decodeKeyId(fileName) {
|
|
53
|
+
const encoded = fileName.slice(0, -SLOT_SUFFIX.length);
|
|
54
|
+
try {
|
|
55
|
+
return decodeURIComponent(encoded);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
log('FileKeyStore: skipping undecodable slot filename %s: %o', fileName, error);
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Whether an unknown error is a Node "file/dir not found" (ENOENT). */
|
|
63
|
+
function isNotFound(error) {
|
|
64
|
+
return typeof error === 'object' && error !== null
|
|
65
|
+
&& error.code === 'ENOENT';
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* File-backed {@link KeyStore}. Each slot is `<dir>/<encoded keyId>.key`
|
|
69
|
+
* containing the raw material bytes. The directory is created lazily on first
|
|
70
|
+
* {@link set}. Suitable for headless Node cadre nodes and tests; for mobile use
|
|
71
|
+
* a platform secure-enclave backend instead.
|
|
72
|
+
*/
|
|
73
|
+
export class FileKeyStore {
|
|
74
|
+
constructor(dir) {
|
|
75
|
+
this.dir = dir;
|
|
76
|
+
}
|
|
77
|
+
slotPath(keyId) {
|
|
78
|
+
return join(this.dir, `${encodeKeyId(keyId)}${SLOT_SUFFIX}`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Sibling path for the temp file written before the atomic rename. The random
|
|
82
|
+
* component makes concurrent {@link set}s to the same slot (and crash-orphaned
|
|
83
|
+
* leftovers) collide-free; it lives in the same directory as the slot so the
|
|
84
|
+
* final {@link rename} stays within one filesystem and is therefore atomic.
|
|
85
|
+
*/
|
|
86
|
+
tempPath(keyId) {
|
|
87
|
+
const unique = randomBytes(6).toString('hex');
|
|
88
|
+
return join(this.dir, `${encodeKeyId(keyId)}.${unique}${TEMP_SUFFIX}`);
|
|
89
|
+
}
|
|
90
|
+
async get(keyId) {
|
|
91
|
+
try {
|
|
92
|
+
const buf = await readFile(this.slotPath(keyId));
|
|
93
|
+
// Copy into a plain Uint8Array decoupled from Node's Buffer pool.
|
|
94
|
+
return new Uint8Array(buf);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
// A missing slot is "empty" (undefined), NOT an error — so a
|
|
98
|
+
// load-or-create caller generates a key rather than mistaking a
|
|
99
|
+
// permission failure for absence. Any other failure (e.g. EACCES) is
|
|
100
|
+
// surfaced as access-denied so callers do not clobber an existing key.
|
|
101
|
+
if (isNotFound(error))
|
|
102
|
+
return undefined;
|
|
103
|
+
throw new KeyStoreAccessError(keyId, `FileKeyStore: failed to read key slot ${keyId}`, { cause: error });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Crash-atomic write: a concurrent reader sees either the complete previous
|
|
108
|
+
* bytes or the complete new bytes, never a torn slot. The new material is
|
|
109
|
+
* written to a sibling temp file, fsync'd, then atomically renamed over the
|
|
110
|
+
* slot. A failure at any point removes the temp file and leaves the previous
|
|
111
|
+
* slot untouched.
|
|
112
|
+
*/
|
|
113
|
+
async set(keyId, keyMaterial) {
|
|
114
|
+
await mkdir(this.dir, { recursive: true, mode: DIR_MODE });
|
|
115
|
+
await this.writeSlotAtomically(this.tempPath(keyId), this.slotPath(keyId), keyMaterial);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Write material to `tmpPath`, durably land it, then atomically swap it onto
|
|
119
|
+
* `finalPath`.
|
|
120
|
+
*
|
|
121
|
+
* Durability bar: the temp file is fsync'd before the rename so a power loss
|
|
122
|
+
* cannot surface a slot whose bytes never reached the platter, and the slot
|
|
123
|
+
* directory is fsync'd afterwards (best-effort — see {@link syncDir}) so the
|
|
124
|
+
* rename itself survives a crash. On POSIX `rename(2)` replaces atomically;
|
|
125
|
+
* Node maps to `MoveFileEx(MOVEFILE_REPLACE_EXISTING)` on Windows, which is
|
|
126
|
+
* also atomic — though it can fail with `EPERM`/`EBUSY` if another process
|
|
127
|
+
* holds the destination open. That cross-process case is out of scope here
|
|
128
|
+
* (the in-process identity path is single-flight); such a failure surfaces
|
|
129
|
+
* to the caller with the previous slot left intact.
|
|
130
|
+
*/
|
|
131
|
+
async writeSlotAtomically(tmpPath, finalPath, keyMaterial) {
|
|
132
|
+
// 'wx' creates the temp file exclusively; a name collision (astronomically
|
|
133
|
+
// unlikely given the random component) fails here before we own anything.
|
|
134
|
+
const handle = await open(tmpPath, 'wx', FILE_MODE);
|
|
135
|
+
try {
|
|
136
|
+
try {
|
|
137
|
+
await handle.writeFile(keyMaterial);
|
|
138
|
+
await handle.sync(); // land bytes on disk before the rename exposes them
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
await handle.close().catch(() => { });
|
|
142
|
+
}
|
|
143
|
+
await rename(tmpPath, finalPath);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
// Any failure after creation (write, sync, or rename): drop the temp
|
|
147
|
+
// file so a retry leaves no `.tmp` debris. The slot is only ever
|
|
148
|
+
// swapped by the atomic rename, so the previous bytes stay intact.
|
|
149
|
+
await rm(tmpPath, { force: true }).catch(() => { });
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
await this.syncDir();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Best-effort fsync of the slot directory so a fresh slot's directory entry is
|
|
156
|
+
* durable across power loss. Opening a directory for fsync is not portable
|
|
157
|
+
* (unsupported on Windows and some filesystems), and rename atomicity does not
|
|
158
|
+
* depend on it — so a failure here is logged and swallowed, never surfaced.
|
|
159
|
+
*/
|
|
160
|
+
async syncDir() {
|
|
161
|
+
let dirHandle;
|
|
162
|
+
try {
|
|
163
|
+
dirHandle = await open(this.dir, 'r');
|
|
164
|
+
await dirHandle.sync();
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
log('FileKeyStore: directory fsync skipped for %s: %o', this.dir, error);
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
await dirHandle?.close().catch(() => { });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async delete(keyId) {
|
|
174
|
+
// force:true makes removing an absent slot a no-op (idempotent).
|
|
175
|
+
await rm(this.slotPath(keyId), { force: true });
|
|
176
|
+
}
|
|
177
|
+
async list() {
|
|
178
|
+
let entries;
|
|
179
|
+
try {
|
|
180
|
+
entries = await readdir(this.dir);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
// An absent directory means no slots yet — not a failure.
|
|
184
|
+
if (isNotFound(error))
|
|
185
|
+
return [];
|
|
186
|
+
throw new Error('FileKeyStore: failed to list key directory', { cause: error });
|
|
187
|
+
}
|
|
188
|
+
return entries
|
|
189
|
+
.filter((name) => name.endsWith(SLOT_SUFFIX))
|
|
190
|
+
.map(decodeKeyId)
|
|
191
|
+
.filter((id) => id !== undefined);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=key-store-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-store-file.js","sourceRoot":"","sources":["../src/key-store-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE9E,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAErD,MAAM,GAAG,GAAG,KAAK,CAA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xB,iEAAiE;QACjE,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,IAAI;QACT,IAAI,OAAiB,CAAC;QACtB,IAAI,CAAC;YACJ,OAAO,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,0DAA0D;YAC1D,IAAI,UAAU,CAAC,KAAK,CAAC;gBAAE,OAAO,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,4CAA4C,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QACjF,CAAC;QACD,OAAO,OAAO;aACZ,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;aAC5C,GAAG,CAAC,WAAW,CAAC;aAChB,MAAM,CAAC,CAAC,EAAE,EAAe,EAAE,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC;IACjD,CAAC;CACD"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend-agnostic store for raw private key material.
|
|
3
|
+
*
|
|
4
|
+
* Mobile cadre nodes hold sensitive key material at rest: the libp2p
|
|
5
|
+
* peer/node identity key and (in the single-key reference model) the owner
|
|
6
|
+
* signing key derived from it. This module defines the seam those keys flow
|
|
7
|
+
* through so a platform-secure backend (iOS Keychain / Android Keystore) can be
|
|
8
|
+
* plugged in without `@serfab/cadre-core` taking any platform dependency.
|
|
9
|
+
*
|
|
10
|
+
* This file is dependency-free (no node:/RN imports) so it is safe in every
|
|
11
|
+
* entry graph. The Node reference {@link FileKeyStore} lives in a separate
|
|
12
|
+
* subpath module (`./key-store-file.js`) so its `node:fs` import never lands in
|
|
13
|
+
* the cross-platform default entry.
|
|
14
|
+
*/
|
|
15
|
+
/** Opaque identifier for a stored key slot (e.g. 'cadre/identity'). */
|
|
16
|
+
export type KeyId = string;
|
|
17
|
+
/**
|
|
18
|
+
* Default slot id for the node identity key. `CadreNode` reads/persists its
|
|
19
|
+
* libp2p identity here when a {@link KeyStore} is configured without an explicit
|
|
20
|
+
* `identityKeyId`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const DEFAULT_IDENTITY_KEY_ID: KeyId;
|
|
23
|
+
/**
|
|
24
|
+
* Backend-agnostic store for raw private key material. Implementations may be
|
|
25
|
+
* file-based, OS-keyring, or platform secure-enclave backed (iOS Keychain /
|
|
26
|
+
* Android Keystore). The interface makes no assumption about the backend and
|
|
27
|
+
* never logs, returns, or throws key material.
|
|
28
|
+
*
|
|
29
|
+
* Key material is raw bytes (e.g. libp2p protobuf-serialized private keys).
|
|
30
|
+
* Text-only backends encode/decode (base64) internally; byte-native backends
|
|
31
|
+
* (LevelDB) store as-is.
|
|
32
|
+
*/
|
|
33
|
+
export interface KeyStore {
|
|
34
|
+
/** Resolves to the stored material, or undefined if the slot is empty. */
|
|
35
|
+
get(keyId: KeyId): Promise<Uint8Array | undefined>;
|
|
36
|
+
/** Writes/overwrites the slot. */
|
|
37
|
+
set(keyId: KeyId, keyMaterial: Uint8Array): Promise<void>;
|
|
38
|
+
/** Removes the slot. Idempotent — succeeds even if absent. */
|
|
39
|
+
delete(keyId: KeyId): Promise<void>;
|
|
40
|
+
/** Enumerates the keyIds this store knows about. */
|
|
41
|
+
list(): Promise<KeyId[]>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Thrown by {@link KeyStore.get} when access was denied or could not be
|
|
45
|
+
* satisfied (e.g. a biometric/device-unlock prompt was cancelled or failed).
|
|
46
|
+
* Distinguishes "access refused" from "slot empty" (which is a plain `undefined`
|
|
47
|
+
* return). Biometric gating itself is out of scope here, but `get` rejecting
|
|
48
|
+
* with this error is the contract a gated backend uses, so callers must not
|
|
49
|
+
* treat it as "no key" (which would trigger key regeneration and silent
|
|
50
|
+
* identity loss).
|
|
51
|
+
*
|
|
52
|
+
* Carries only the {@link keyId} — never key material — so it is safe to log.
|
|
53
|
+
*/
|
|
54
|
+
export declare class KeyStoreAccessError extends Error {
|
|
55
|
+
readonly keyId: KeyId;
|
|
56
|
+
constructor(keyId: KeyId, message: string, options?: {
|
|
57
|
+
cause?: unknown;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* In-memory {@link KeyStore} backed by a `Map`. For tests and ephemeral nodes;
|
|
62
|
+
* nothing is persisted across process restarts. Dependency-free, so it is safe
|
|
63
|
+
* in every entry graph.
|
|
64
|
+
*
|
|
65
|
+
* Material is defensively copied on both {@link set} and {@link get} so a caller
|
|
66
|
+
* mutating its own buffer cannot alter stored state (and vice versa), matching
|
|
67
|
+
* the fresh-buffer semantics a file/keyring backend produces.
|
|
68
|
+
*/
|
|
69
|
+
export declare class InMemoryKeyStore implements KeyStore {
|
|
70
|
+
private readonly slots;
|
|
71
|
+
get(keyId: KeyId): Promise<Uint8Array | undefined>;
|
|
72
|
+
set(keyId: KeyId, keyMaterial: Uint8Array): Promise<void>;
|
|
73
|
+
delete(keyId: KeyId): Promise<void>;
|
|
74
|
+
list(): Promise<KeyId[]>;
|
|
75
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend-agnostic store for raw private key material.
|
|
3
|
+
*
|
|
4
|
+
* Mobile cadre nodes hold sensitive key material at rest: the libp2p
|
|
5
|
+
* peer/node identity key and (in the single-key reference model) the owner
|
|
6
|
+
* signing key derived from it. This module defines the seam those keys flow
|
|
7
|
+
* through so a platform-secure backend (iOS Keychain / Android Keystore) can be
|
|
8
|
+
* plugged in without `@serfab/cadre-core` taking any platform dependency.
|
|
9
|
+
*
|
|
10
|
+
* This file is dependency-free (no node:/RN imports) so it is safe in every
|
|
11
|
+
* entry graph. The Node reference {@link FileKeyStore} lives in a separate
|
|
12
|
+
* subpath module (`./key-store-file.js`) so its `node:fs` import never lands in
|
|
13
|
+
* the cross-platform default entry.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Default slot id for the node identity key. `CadreNode` reads/persists its
|
|
17
|
+
* libp2p identity here when a {@link KeyStore} is configured without an explicit
|
|
18
|
+
* `identityKeyId`.
|
|
19
|
+
*/
|
|
20
|
+
export const DEFAULT_IDENTITY_KEY_ID = 'cadre/identity';
|
|
21
|
+
/**
|
|
22
|
+
* Thrown by {@link KeyStore.get} when access was denied or could not be
|
|
23
|
+
* satisfied (e.g. a biometric/device-unlock prompt was cancelled or failed).
|
|
24
|
+
* Distinguishes "access refused" from "slot empty" (which is a plain `undefined`
|
|
25
|
+
* return). Biometric gating itself is out of scope here, but `get` rejecting
|
|
26
|
+
* with this error is the contract a gated backend uses, so callers must not
|
|
27
|
+
* treat it as "no key" (which would trigger key regeneration and silent
|
|
28
|
+
* identity loss).
|
|
29
|
+
*
|
|
30
|
+
* Carries only the {@link keyId} — never key material — so it is safe to log.
|
|
31
|
+
*/
|
|
32
|
+
export class KeyStoreAccessError extends Error {
|
|
33
|
+
constructor(keyId, message, options) {
|
|
34
|
+
super(message, options);
|
|
35
|
+
this.name = 'KeyStoreAccessError';
|
|
36
|
+
this.keyId = keyId;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* In-memory {@link KeyStore} backed by a `Map`. For tests and ephemeral nodes;
|
|
41
|
+
* nothing is persisted across process restarts. Dependency-free, so it is safe
|
|
42
|
+
* in every entry graph.
|
|
43
|
+
*
|
|
44
|
+
* Material is defensively copied on both {@link set} and {@link get} so a caller
|
|
45
|
+
* mutating its own buffer cannot alter stored state (and vice versa), matching
|
|
46
|
+
* the fresh-buffer semantics a file/keyring backend produces.
|
|
47
|
+
*/
|
|
48
|
+
export class InMemoryKeyStore {
|
|
49
|
+
constructor() {
|
|
50
|
+
this.slots = new Map();
|
|
51
|
+
}
|
|
52
|
+
async get(keyId) {
|
|
53
|
+
const stored = this.slots.get(keyId);
|
|
54
|
+
return stored ? stored.slice() : undefined;
|
|
55
|
+
}
|
|
56
|
+
async set(keyId, keyMaterial) {
|
|
57
|
+
this.slots.set(keyId, keyMaterial.slice());
|
|
58
|
+
}
|
|
59
|
+
async delete(keyId) {
|
|
60
|
+
this.slots.delete(keyId);
|
|
61
|
+
}
|
|
62
|
+
async list() {
|
|
63
|
+
return [...this.slots.keys()];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=key-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-store.js","sourceRoot":"","sources":["../src/key-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAKH;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAU,gBAAgB,CAAC;AA0B/D;;;;;;;;;;GAUG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAG7C,YAAY,KAAY,EAAE,OAAe,EAAE,OAA6B;QACvE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;CACD;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,gBAAgB;IAA7B;QACkB,UAAK,GAAG,IAAI,GAAG,EAAqB,CAAC;IAkBvD,CAAC;IAhBA,KAAK,CAAC,GAAG,CAAC,KAAY;QACrB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACrC,OAAO,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,KAAY,EAAE,WAAuB;QAC9C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAAY;QACxB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/B,CAAC;CACD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical digest an owner signs to authorize a peer.
|
|
3
|
+
*
|
|
4
|
+
* This is the exact byte construction {@link SeedBootstrapService.authorizePeer}
|
|
5
|
+
* signs over when it inserts an authorized `CadrePeer` row:
|
|
6
|
+
* `digest([peerId], 'sha256', 'base64url')`. Factored into one place so the
|
|
7
|
+
* producer (owner signing) and the verifier (the offline `cadre enroll
|
|
8
|
+
* register` check) can never drift apart — change the digest here and both move
|
|
9
|
+
* together. The SQL mirror is the bare single-field `digest(coalesce(new.PeerId, old.PeerId))`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function peerAuthorizationDigest(peerId: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Canonical digest an owner signs to VOUCH a `CadrePeer` membership row (insert
|
|
14
|
+
* and the owner re-touch update). Binds the peer id to the row's single-use
|
|
15
|
+
* `StampId` nonce, so a captured signed insert cannot be replayed (the `StampId` is
|
|
16
|
+
* unique) and — because {@link cadrePeerRemoveDigest} scopes a DIFFERENT payload — the
|
|
17
|
+
* stored voucher (`VouchSig`) cannot be replayed to authorize a delete.
|
|
18
|
+
*
|
|
19
|
+
* The SQL mirror is `digest(new.PeerId, new.StampId)` (two TEXT fields), matching the
|
|
20
|
+
* canonical multi-field framing in control-database.ts:buildAuthorizationMessage.
|
|
21
|
+
*/
|
|
22
|
+
export declare function cadrePeerVoucherDigest(peerId: string, stampId: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Canonical digest an owner signs to REMOVE a `CadrePeer` row. Deliberately a
|
|
25
|
+
* distinct payload from {@link cadrePeerVoucherDigest} (adds the `'remove'` action
|
|
26
|
+
* tag) so the row's stored voucher — a signature over the voucher digest — can never
|
|
27
|
+
* satisfy this delete check. The signature is supplied in write context and never
|
|
28
|
+
* stored, so no reader can replay it; binding the live row's `StampId` also invalidates
|
|
29
|
+
* any captured remove after a delete+reinsert (the nonce rotates).
|
|
30
|
+
*
|
|
31
|
+
* SQL mirror: `digest(old.PeerId, old.StampId, 'remove')`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function cadrePeerRemoveDigest(peerId: string, stampId: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Verify that `signature` is a valid owner ed25519 signature over `peerId`'s
|
|
36
|
+
* authorization digest, using `ownerPublicKey` (base64url).
|
|
37
|
+
*
|
|
38
|
+
* This is the mirror of the signing done in
|
|
39
|
+
* {@link SeedBootstrapService.authorizePeer}: it checks the signature against
|
|
40
|
+
* {@link peerAuthorizationDigest}. A `true` result means the holder of the
|
|
41
|
+
* owner private key vouched for this peer ID — it does NOT mean the peer is
|
|
42
|
+
* registered anywhere.
|
|
43
|
+
*
|
|
44
|
+
* Returns a boolean and never throws: malformed base64url, a bad/garbage key, or
|
|
45
|
+
* any crypto failure resolves to `false` (callers want a verdict, not an
|
|
46
|
+
* exception). The catch is logged at debug.
|
|
47
|
+
*/
|
|
48
|
+
export declare function verifyPeerAuthorization(peerId: string, ownerPublicKey: string, signature: string): boolean;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import { digest, verify } from '@optimystic/quereus-plugin-crypto';
|
|
3
|
+
const log = debug('sereus:cadre:peer-authorization');
|
|
4
|
+
/**
|
|
5
|
+
* Canonical digest an owner signs to authorize a peer.
|
|
6
|
+
*
|
|
7
|
+
* This is the exact byte construction {@link SeedBootstrapService.authorizePeer}
|
|
8
|
+
* signs over when it inserts an authorized `CadrePeer` row:
|
|
9
|
+
* `digest([peerId], 'sha256', 'base64url')`. Factored into one place so the
|
|
10
|
+
* producer (owner signing) and the verifier (the offline `cadre enroll
|
|
11
|
+
* register` check) can never drift apart — change the digest here and both move
|
|
12
|
+
* together. The SQL mirror is the bare single-field `digest(coalesce(new.PeerId, old.PeerId))`.
|
|
13
|
+
*/
|
|
14
|
+
export function peerAuthorizationDigest(peerId) {
|
|
15
|
+
return digest([peerId], 'sha256', 'base64url');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Canonical digest an owner signs to VOUCH a `CadrePeer` membership row (insert
|
|
19
|
+
* and the owner re-touch update). Binds the peer id to the row's single-use
|
|
20
|
+
* `StampId` nonce, so a captured signed insert cannot be replayed (the `StampId` is
|
|
21
|
+
* unique) and — because {@link cadrePeerRemoveDigest} scopes a DIFFERENT payload — the
|
|
22
|
+
* stored voucher (`VouchSig`) cannot be replayed to authorize a delete.
|
|
23
|
+
*
|
|
24
|
+
* The SQL mirror is `digest(new.PeerId, new.StampId)` (two TEXT fields), matching the
|
|
25
|
+
* canonical multi-field framing in control-database.ts:buildAuthorizationMessage.
|
|
26
|
+
*/
|
|
27
|
+
export function cadrePeerVoucherDigest(peerId, stampId) {
|
|
28
|
+
return digest([peerId, stampId], 'sha256', 'base64url');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Canonical digest an owner signs to REMOVE a `CadrePeer` row. Deliberately a
|
|
32
|
+
* distinct payload from {@link cadrePeerVoucherDigest} (adds the `'remove'` action
|
|
33
|
+
* tag) so the row's stored voucher — a signature over the voucher digest — can never
|
|
34
|
+
* satisfy this delete check. The signature is supplied in write context and never
|
|
35
|
+
* stored, so no reader can replay it; binding the live row's `StampId` also invalidates
|
|
36
|
+
* any captured remove after a delete+reinsert (the nonce rotates).
|
|
37
|
+
*
|
|
38
|
+
* SQL mirror: `digest(old.PeerId, old.StampId, 'remove')`.
|
|
39
|
+
*/
|
|
40
|
+
export function cadrePeerRemoveDigest(peerId, stampId) {
|
|
41
|
+
return digest([peerId, stampId, 'remove'], 'sha256', 'base64url');
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Verify that `signature` is a valid owner ed25519 signature over `peerId`'s
|
|
45
|
+
* authorization digest, using `ownerPublicKey` (base64url).
|
|
46
|
+
*
|
|
47
|
+
* This is the mirror of the signing done in
|
|
48
|
+
* {@link SeedBootstrapService.authorizePeer}: it checks the signature against
|
|
49
|
+
* {@link peerAuthorizationDigest}. A `true` result means the holder of the
|
|
50
|
+
* owner private key vouched for this peer ID — it does NOT mean the peer is
|
|
51
|
+
* registered anywhere.
|
|
52
|
+
*
|
|
53
|
+
* Returns a boolean and never throws: malformed base64url, a bad/garbage key, or
|
|
54
|
+
* any crypto failure resolves to `false` (callers want a verdict, not an
|
|
55
|
+
* exception). The catch is logged at debug.
|
|
56
|
+
*/
|
|
57
|
+
export function verifyPeerAuthorization(peerId, ownerPublicKey, signature) {
|
|
58
|
+
try {
|
|
59
|
+
return verify(peerAuthorizationDigest(peerId), signature, ownerPublicKey, 'ed25519', 'base64url', 'base64url', 'base64url');
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
log('verifyPeerAuthorization failed: %o', error);
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=peer-authorization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peer-authorization.js","sourceRoot":"","sources":["../src/peer-authorization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAEnE,MAAM,GAAG,GAAG,KAAK,CAAC,iCAAiC,CAAC,CAAC;AAErD;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAAc;IACpD,OAAO,MAAM,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,WAAW,CAAW,CAAC;AAC3D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAc,EAAE,OAAe;IACpE,OAAO,MAAM,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,WAAW,CAAW,CAAC;AACpE,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAc,EAAE,OAAe;IACnE,OAAO,MAAM,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,QAAQ,EAAE,WAAW,CAAW,CAAC;AAC9E,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,uBAAuB,CACrC,MAAc,EACd,cAAsB,EACtB,SAAiB;IAEjB,IAAI,CAAC;QACH,OAAO,MAAM,CACX,uBAAuB,CAAC,MAAM,CAAC,EAC/B,SAAS,EACT,cAAc,EACd,SAAS,EACT,WAAW,EACX,WAAW,EACX,WAAW,CACZ,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;QACjD,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Peer-address record helpers: the single source of truth for the bytes a node
|
|
3
|
+
* signs when publishing its own `CadrePeer` row and re-verifies when resolving
|
|
4
|
+
* another peer's row.
|
|
5
|
+
*
|
|
6
|
+
* The signed payload is a single SHA-256 digest of a delimited string of the
|
|
7
|
+
* authenticated fields. It is intentionally NOT JSON-canonicalized: a delimited
|
|
8
|
+
* digest is trivially deterministic across node/browser/RN (plain string concat
|
|
9
|
+
* + sha256, no key ordering) and — crucially — is reconstructable inside the
|
|
10
|
+
* `CadrePeer.AuthorizedUpdate` SQL constraint from the row's own columns:
|
|
11
|
+
*
|
|
12
|
+
* digest(new.PeerId || '|' || new.Multiaddr || '|' || cast(new.UpdatedAt as text))
|
|
13
|
+
*
|
|
14
|
+
* Keep {@link peerRecordSignedPayload} and that constraint byte-for-byte in
|
|
15
|
+
* sync. The `'|'` delimiter is safe: a base58btc PeerId, a multiaddr (which uses
|
|
16
|
+
* `/`), and a base-10 integer never contain it.
|
|
17
|
+
*
|
|
18
|
+
* `publicKey` is deliberately excluded from the payload — it is the key the
|
|
19
|
+
* signature is verified *with* (so a signature already commits to exactly one
|
|
20
|
+
* key), and its binding to `peerId` is checked separately at resolve time.
|
|
21
|
+
*/
|
|
22
|
+
import type { PeerAddressRecord, PeerResolveTrustPolicy } from './types.js';
|
|
23
|
+
/**
|
|
24
|
+
* Default freshness ceiling for resolved peer-address records (15 minutes).
|
|
25
|
+
* A record older than this is treated as stale and filtered out, so a resolver
|
|
26
|
+
* never hands back a dead relay reservation. The publish path re-stamps at
|
|
27
|
+
* roughly half this interval (see {@link DEFAULT_PEER_RECORD_HEARTBEAT_MS}).
|
|
28
|
+
*/
|
|
29
|
+
export declare const DEFAULT_PEER_RECORD_MAX_AGE_MS: number;
|
|
30
|
+
/**
|
|
31
|
+
* TTL heartbeat interval for re-publishing the self record — half the freshness
|
|
32
|
+
* ceiling, so a record is refreshed well before it can go stale.
|
|
33
|
+
*/
|
|
34
|
+
export declare const DEFAULT_PEER_RECORD_HEARTBEAT_MS: number;
|
|
35
|
+
/**
|
|
36
|
+
* Build the base64url SHA-256 digest that the self-signature covers. Mirrors the
|
|
37
|
+
* `CadrePeer.AuthorizedUpdate` constraint exactly; both sides take the default
|
|
38
|
+
* base64url output of a single `digest(...)`, which round-trips cleanly (unlike
|
|
39
|
+
* concatenating several base64url digests).
|
|
40
|
+
*
|
|
41
|
+
* @param peerId - base58btc libp2p peer id (the row key)
|
|
42
|
+
* @param multiaddr - the comma-joined `Multiaddr` string EXACTLY as stored
|
|
43
|
+
* @param updatedAt - epoch-ms freshness stamp
|
|
44
|
+
*/
|
|
45
|
+
export declare function peerRecordSignedPayload(peerId: string, multiaddr: string, updatedAt: number): string;
|
|
46
|
+
/**
|
|
47
|
+
* Sign a peer-address record with the ed25519 private key behind its `peerId`.
|
|
48
|
+
* Returns a fully-populated {@link PeerAddressRecord} (the `addrs` order is
|
|
49
|
+
* preserved and is the order signed over).
|
|
50
|
+
*
|
|
51
|
+
* @param fields - the record fields to sign (signaling addr first by convention)
|
|
52
|
+
* @param privateKeyB64 - base64url ed25519 seed (see `ed25519KeyPairFromLibp2p`)
|
|
53
|
+
*/
|
|
54
|
+
export declare function signPeerRecord(fields: {
|
|
55
|
+
peerId: string;
|
|
56
|
+
publicKey: string;
|
|
57
|
+
addrs: string[];
|
|
58
|
+
updatedAt: number;
|
|
59
|
+
}, privateKeyB64: string): PeerAddressRecord;
|
|
60
|
+
/**
|
|
61
|
+
* Verify a record's self-signature against its own `publicKey`. Reconstructs the
|
|
62
|
+
* signed bytes from the record exactly as {@link signPeerRecord} produced them.
|
|
63
|
+
* Returns false on a missing key/sig or any verification failure.
|
|
64
|
+
*/
|
|
65
|
+
export declare function verifyPeerRecordSignature(record: PeerAddressRecord): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Freshness predicate: true when `updatedAt` is a positive stamp within
|
|
68
|
+
* `maxAgeMs` of `now`. A non-positive `updatedAt` (never self-published) is
|
|
69
|
+
* never fresh.
|
|
70
|
+
*/
|
|
71
|
+
export declare function isPeerRecordFresh(updatedAt: number, maxAgeMs: number, now: number): boolean;
|
|
72
|
+
/** True if `addr` is a `/p2p-circuit` signaling/relay multiaddr. */
|
|
73
|
+
export declare function isSignalingAddr(addr: string): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Return `addrs` with signaling (`/p2p-circuit`) addrs first, otherwise stable.
|
|
76
|
+
* Used to present the WebRTC dial input ahead of direct addrs. Does NOT mutate
|
|
77
|
+
* the input and must not be used before signature verification (verification
|
|
78
|
+
* uses the original on-record order).
|
|
79
|
+
*/
|
|
80
|
+
export declare function orderSignalingFirst(addrs: string[]): string[];
|
|
81
|
+
/**
|
|
82
|
+
* Default resolve trust gate: trust any peer that has a `CadrePeer` row.
|
|
83
|
+
*
|
|
84
|
+
* A row only exists because an owner signed its `AuthorizedInsert`, so row
|
|
85
|
+
* presence already means "owner-vouched member". This is the seam where a
|
|
86
|
+
* stricter policy (trust circle, pinned keys) plugs in — see
|
|
87
|
+
* {@link PeerResolveTrustPolicy}.
|
|
88
|
+
*/
|
|
89
|
+
export declare function currentMemberTrustPolicy(): PeerResolveTrustPolicy;
|