@serfab/cadre-core 0.9.0 → 0.10.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 +4 -2
- package/dist/bootstrap-peer-store-file.d.ts +13 -0
- package/dist/bootstrap-peer-store-file.js +42 -0
- package/dist/bootstrap-peer-store-file.js.map +1 -0
- package/dist/bootstrap-peer-store.d.ts +73 -0
- package/dist/bootstrap-peer-store.js +128 -0
- package/dist/bootstrap-peer-store.js.map +1 -0
- package/dist/cadre-node.d.ts +918 -53
- package/dist/cadre-node.js +1843 -190
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +93 -0
- package/dist/control-authorization.js +57 -0
- package/dist/control-authorization.js.map +1 -0
- package/dist/control-database.d.ts +990 -50
- package/dist/control-database.js +1459 -108
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +89 -10
- package/dist/control-formation-recorder.js +129 -14
- package/dist/control-formation-recorder.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +774 -210
- package/dist/control-schema.js.map +1 -1
- package/dist/control-stream.d.ts +38 -4
- package/dist/control-stream.js +70 -4
- package/dist/control-stream.js.map +1 -1
- package/dist/control-write-retry.d.ts +151 -0
- package/dist/control-write-retry.js +411 -0
- package/dist/control-write-retry.js.map +1 -0
- package/dist/delegate-admission.d.ts +134 -0
- package/dist/delegate-admission.js +244 -0
- package/dist/delegate-admission.js.map +1 -0
- package/dist/device-token.d.ts +11 -11
- package/dist/device-token.js +14 -13
- package/dist/device-token.js.map +1 -1
- package/dist/ed25519-key.d.ts +26 -0
- package/dist/ed25519-key.js +56 -1
- package/dist/ed25519-key.js.map +1 -1
- package/dist/file-durable-slot.d.ts +20 -0
- package/dist/file-durable-slot.js +53 -0
- package/dist/file-durable-slot.js.map +1 -0
- package/dist/formation-approval.d.ts +157 -0
- package/dist/formation-approval.js +375 -0
- package/dist/formation-approval.js.map +1 -0
- package/dist/fs-atomic.d.ts +38 -0
- package/dist/fs-atomic.js +97 -0
- package/dist/fs-atomic.js.map +1 -0
- package/dist/identity-key.d.ts +49 -0
- package/dist/identity-key.js +81 -0
- package/dist/identity-key.js.map +1 -0
- package/dist/index.d.ts +16 -6
- package/dist/index.js +39 -5
- package/dist/index.js.map +1 -1
- package/dist/key-store-file.d.ts +2 -24
- package/dist/key-store-file.js +10 -77
- package/dist/key-store-file.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +146 -0
- package/dist/membership-connection-gater.js +180 -0
- package/dist/membership-connection-gater.js.map +1 -0
- package/dist/node-local-snapshot.d.ts +109 -0
- package/dist/node-local-snapshot.js +182 -0
- package/dist/node-local-snapshot.js.map +1 -0
- package/dist/peer-authorization.d.ts +135 -18
- package/dist/peer-authorization.js +152 -21
- package/dist/peer-authorization.js.map +1 -1
- package/dist/peer-record.d.ts +21 -8
- package/dist/peer-record.js +26 -10
- package/dist/peer-record.js.map +1 -1
- package/dist/relay-addrs.d.ts +45 -0
- package/dist/relay-addrs.js +103 -0
- package/dist/relay-addrs.js.map +1 -0
- package/dist/relay-reservation.d.ts +266 -0
- package/dist/relay-reservation.js +613 -0
- package/dist/relay-reservation.js.map +1 -0
- package/dist/seed-bootstrap.d.ts +221 -56
- package/dist/seed-bootstrap.js +328 -188
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/seed-trust-policy.d.ts +47 -21
- package/dist/seed-trust-policy.js +24 -15
- package/dist/seed-trust-policy.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +42 -13
- package/dist/strand-addr-protocol.js +58 -52
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-backfill.d.ts +109 -0
- package/dist/strand-backfill.js +279 -0
- package/dist/strand-backfill.js.map +1 -0
- package/dist/strand-formation-manager.d.ts +38 -9
- package/dist/strand-formation-manager.js +120 -17
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +103 -4
- package/dist/strand-formation-protocol.js +232 -4
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +39 -1
- package/dist/strand-instance-manager.js +92 -14
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-registry.d.ts +5 -5
- package/dist/strand-member-registry.js +18 -10
- package/dist/strand-member-registry.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +482 -88
- package/dist/strand-membership-writer.js +757 -136
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-solicitation.d.ts +99 -33
- package/dist/strand-solicitation.js +100 -52
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-transport-key.d.ts +40 -0
- package/dist/strand-transport-key.js +56 -0
- package/dist/strand-transport-key.js.map +1 -0
- package/dist/strand-wake-protocol.d.ts +9 -7
- package/dist/strand-wake-protocol.js +22 -46
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.d.ts +17 -2
- package/dist/strand-watcher.js +47 -3
- package/dist/strand-watcher.js.map +1 -1
- package/dist/trusted-owner-store-file.d.ts +12 -0
- package/dist/trusted-owner-store-file.js +39 -0
- package/dist/trusted-owner-store-file.js.map +1 -0
- package/dist/trusted-owner-store.d.ts +64 -0
- package/dist/trusted-owner-store.js +126 -0
- package/dist/trusted-owner-store.js.map +1 -0
- package/dist/types.d.ts +275 -20
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -1
- package/package.json +19 -8
- package/src/bootstrap-peer-store-file.ts +44 -0
- package/src/bootstrap-peer-store.ts +172 -0
- package/src/cadre-node.ts +1992 -202
- package/src/control-authorization.ts +114 -0
- package/src/control-database.ts +1704 -146
- package/src/control-formation-recorder.ts +172 -19
- package/src/control-schema.ts +786 -222
- package/src/control-stream.ts +81 -4
- package/src/control-write-retry.ts +473 -0
- package/src/delegate-admission.ts +302 -0
- package/src/device-token.ts +18 -13
- package/src/ed25519-key.ts +134 -69
- package/src/file-durable-slot.ts +61 -0
- package/src/formation-approval.ts +604 -0
- package/src/fs-atomic.ts +107 -0
- package/src/identity-key.ts +109 -0
- package/src/index.ts +136 -5
- package/src/key-store-file.ts +10 -81
- package/src/membership-connection-gater.ts +204 -0
- package/src/node-local-snapshot.ts +260 -0
- package/src/peer-authorization.ts +199 -21
- package/src/peer-record.ts +28 -10
- package/src/relay-addrs.ts +111 -0
- package/src/relay-reservation.ts +823 -0
- package/src/seed-bootstrap.ts +397 -197
- package/src/seed-trust-policy.ts +58 -25
- package/src/strand-addr-protocol.ts +84 -53
- package/src/strand-backfill.ts +366 -0
- package/src/strand-formation-manager.ts +145 -19
- package/src/strand-formation-protocol.ts +309 -8
- package/src/strand-instance-manager.ts +111 -14
- package/src/strand-member-registry.ts +18 -13
- package/src/strand-membership-writer.ts +1430 -640
- package/src/strand-solicitation.ts +155 -90
- package/src/strand-transport-key.ts +58 -0
- package/src/strand-wake-protocol.ts +29 -46
- package/src/strand-watcher.ts +66 -3
- package/src/trusted-owner-store-file.ts +41 -0
- package/src/trusted-owner-store.ts +171 -0
- package/src/types.ts +292 -21
package/src/fs-atomic.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-only filesystem helpers: atomic file write, filename-safe encoding, and
|
|
3
|
+
* the ENOENT check, shared by `key-store-file.ts` and `file-durable-slot.ts`.
|
|
4
|
+
*
|
|
5
|
+
* This module imports `node:fs/promises`, so — like its consumers — it is kept
|
|
6
|
+
* OUT of the package's cross-platform default entry and is not itself an
|
|
7
|
+
* exported subpath; it is only reachable through the Node-only store modules.
|
|
8
|
+
*/
|
|
9
|
+
import debug from 'debug';
|
|
10
|
+
import { mkdir, rm, rename, open } from 'node:fs/promises';
|
|
11
|
+
import type { FileHandle } from 'node:fs/promises';
|
|
12
|
+
|
|
13
|
+
const log = debug('sereus:cadre:fs-atomic');
|
|
14
|
+
|
|
15
|
+
/** Best-effort POSIX permissions (ignored on Windows / unsupported FSes). */
|
|
16
|
+
export const DIR_MODE = 0o700;
|
|
17
|
+
export const FILE_MODE = 0o600;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Percent-encode a string into a filesystem-safe filename component. Beyond
|
|
21
|
+
* `encodeURIComponent` (which already escapes `/`, spaces, and unicode), this
|
|
22
|
+
* also escapes the unreserved characters `encodeURIComponent` leaves intact but
|
|
23
|
+
* that are unsafe in filenames on some platforms (notably `*` on Windows) — and
|
|
24
|
+
* `.`, so a file suffix appended by the caller is the only literal dot, making
|
|
25
|
+
* suffix-stripping unambiguous. Reversed by `decodeURIComponent`. The result
|
|
26
|
+
* contains only `A-Za-z0-9-_` and `%XX`.
|
|
27
|
+
*/
|
|
28
|
+
export function encodeFileSafeComponent(component: string): string {
|
|
29
|
+
return encodeURIComponent(component).replace(
|
|
30
|
+
/[!'()*~.]/g,
|
|
31
|
+
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Whether an unknown error is a Node "file/dir not found" (ENOENT). */
|
|
36
|
+
export function isNotFound(error: unknown): boolean {
|
|
37
|
+
return typeof error === 'object' && error !== null
|
|
38
|
+
&& (error as { code?: unknown }).code === 'ENOENT';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Crash-atomic file write: a concurrent reader sees either the complete
|
|
43
|
+
* previous bytes or the complete new bytes, never a torn file. `dir` is created
|
|
44
|
+
* (0700) if absent; the data is written to `tmpPath` (created exclusively,
|
|
45
|
+
* 0600), fsync'd, then atomically renamed onto `finalPath`. A failure at any
|
|
46
|
+
* point removes the temp file and leaves the previous file untouched.
|
|
47
|
+
*
|
|
48
|
+
* `tmpPath` must be a sibling of `finalPath` (same directory) so the rename
|
|
49
|
+
* stays within one filesystem and is therefore atomic, and should carry a
|
|
50
|
+
* random component so concurrent writers (and crash-orphaned leftovers) are
|
|
51
|
+
* collide-free.
|
|
52
|
+
*
|
|
53
|
+
* Durability bar: the temp file is fsync'd before the rename so a power loss
|
|
54
|
+
* cannot surface a file whose bytes never reached the platter, and `dir` is
|
|
55
|
+
* fsync'd afterwards (best-effort — see {@link syncDirBestEffort}) so the
|
|
56
|
+
* rename itself survives a crash. On POSIX `rename(2)` replaces atomically;
|
|
57
|
+
* Node maps to `MoveFileEx(MOVEFILE_REPLACE_EXISTING)` on Windows, which is
|
|
58
|
+
* also atomic — though it can fail with `EPERM`/`EBUSY` if another process
|
|
59
|
+
* holds the destination open. That cross-process case is out of scope for the
|
|
60
|
+
* single-flight in-process callers here; such a failure surfaces to the caller
|
|
61
|
+
* with the previous file left intact.
|
|
62
|
+
*/
|
|
63
|
+
export async function writeFileAtomically(
|
|
64
|
+
dir: string,
|
|
65
|
+
tmpPath: string,
|
|
66
|
+
finalPath: string,
|
|
67
|
+
data: Uint8Array
|
|
68
|
+
): Promise<void> {
|
|
69
|
+
await mkdir(dir, { recursive: true, mode: DIR_MODE });
|
|
70
|
+
// 'wx' creates the temp file exclusively; a name collision (astronomically
|
|
71
|
+
// unlikely given the caller's random component) fails here before we own anything.
|
|
72
|
+
const handle = await open(tmpPath, 'wx', FILE_MODE);
|
|
73
|
+
try {
|
|
74
|
+
try {
|
|
75
|
+
await handle.writeFile(data);
|
|
76
|
+
await handle.sync(); // land bytes on disk before the rename exposes them
|
|
77
|
+
} finally {
|
|
78
|
+
await handle.close().catch(() => {});
|
|
79
|
+
}
|
|
80
|
+
await rename(tmpPath, finalPath);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
// Any failure after creation (write, sync, or rename): drop the temp
|
|
83
|
+
// file so a retry leaves no `.tmp` debris. The destination is only ever
|
|
84
|
+
// swapped by the atomic rename, so the previous bytes stay intact.
|
|
85
|
+
await rm(tmpPath, { force: true }).catch(() => {});
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
await syncDirBestEffort(dir);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Best-effort fsync of a directory so a fresh file's directory entry is
|
|
93
|
+
* durable across power loss. Opening a directory for fsync is not portable
|
|
94
|
+
* (unsupported on Windows and some filesystems), and rename atomicity does not
|
|
95
|
+
* depend on it — so a failure here is logged and swallowed, never surfaced.
|
|
96
|
+
*/
|
|
97
|
+
async function syncDirBestEffort(dir: string): Promise<void> {
|
|
98
|
+
let dirHandle: FileHandle | undefined;
|
|
99
|
+
try {
|
|
100
|
+
dirHandle = await open(dir, 'r');
|
|
101
|
+
await dirHandle.sync();
|
|
102
|
+
} catch (error) {
|
|
103
|
+
log('directory fsync skipped for %s: %o', dir, error);
|
|
104
|
+
} finally {
|
|
105
|
+
await dirHandle?.close().catch(() => {});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* identity-key — the load-or-create rule for a {@link KeyStore}-backed node
|
|
3
|
+
* identity, plus the "prove you hold this node key" signer built on top of it.
|
|
4
|
+
*
|
|
5
|
+
* Both live here because both are needed *outside* `CadreNode`: an embedding app
|
|
6
|
+
* (the React Native reference app) has to resolve its identity key before the node
|
|
7
|
+
* is constructed, so it can sign an out-of-band HTTP request with the same key the
|
|
8
|
+
* node will then load. Keeping one copy of the load-or-create rule is load-bearing
|
|
9
|
+
* — a second copy that drifted could generate a fresh key and orphan the real
|
|
10
|
+
* identity.
|
|
11
|
+
*
|
|
12
|
+
* Dependency-free beyond `@libp2p/crypto` + `uint8arrays` (both already core
|
|
13
|
+
* dependencies), so this module is safe in every (RN / browser / Node) entry graph.
|
|
14
|
+
*/
|
|
15
|
+
import debug from 'debug';
|
|
16
|
+
import { generateKeyPair, privateKeyToProtobuf, privateKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys';
|
|
17
|
+
import { toString as uint8ArrayToString } from 'uint8arrays';
|
|
18
|
+
import type { PrivateKey } from '@libp2p/interface';
|
|
19
|
+
import { DEFAULT_IDENTITY_KEY_ID, type KeyId, type KeyStore } from './key-store.js';
|
|
20
|
+
|
|
21
|
+
const log = debug('sereus:cadre:identity-key');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Load the node identity key from `keyStore`, generating and persisting a fresh
|
|
25
|
+
* Ed25519 key when the slot is empty.
|
|
26
|
+
*
|
|
27
|
+
* A rejected `get` (e.g. {@link KeyStoreAccessError} from a cancelled biometric
|
|
28
|
+
* prompt) **propagates** — we never fall through to generation on a read error,
|
|
29
|
+
* because that would silently orphan an existing but momentarily unreadable
|
|
30
|
+
* identity. Corrupt bytes in the slot likewise throw rather than regenerate.
|
|
31
|
+
*
|
|
32
|
+
* Idempotent in effect: a second call on a populated store loads the stored key
|
|
33
|
+
* and writes nothing.
|
|
34
|
+
*
|
|
35
|
+
* @param keyStore - Backend the identity is read from / persisted to.
|
|
36
|
+
* @param keyId - Slot id; defaults to {@link DEFAULT_IDENTITY_KEY_ID}.
|
|
37
|
+
* @returns The resolved libp2p private key.
|
|
38
|
+
*/
|
|
39
|
+
export async function loadOrCreateIdentityKey(
|
|
40
|
+
keyStore: KeyStore,
|
|
41
|
+
keyId: KeyId = DEFAULT_IDENTITY_KEY_ID
|
|
42
|
+
): Promise<PrivateKey> {
|
|
43
|
+
// A rejection here (e.g. KeyStoreAccessError) must propagate — do NOT fall
|
|
44
|
+
// through to generation, which would orphan an existing but unreadable key.
|
|
45
|
+
const bytes = await keyStore.get(keyId);
|
|
46
|
+
if (bytes) {
|
|
47
|
+
// Corrupt/garbage bytes throw here; surface loudly rather than
|
|
48
|
+
// regenerating (which would orphan the real identity).
|
|
49
|
+
const loaded = privateKeyFromProtobuf(bytes);
|
|
50
|
+
log('Identity key loaded from key store (slot present)');
|
|
51
|
+
return loaded;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const generated = await generateKeyPair('Ed25519');
|
|
55
|
+
await keyStore.set(keyId, privateKeyToProtobuf(generated));
|
|
56
|
+
log('Identity key generated and persisted to key store (first run)');
|
|
57
|
+
return generated;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Proof-of-possession signer for a node key, for out-of-band HTTP services that
|
|
62
|
+
* want to attribute a request to a peer id.
|
|
63
|
+
*
|
|
64
|
+
* Deliberately generic — it carries no ICE/TURN vocabulary, and the message it
|
|
65
|
+
* signs is the caller's business. The one consumer today is the reference apps'
|
|
66
|
+
* `loadIceConfig`, which signs the TURN credential issuer's peer assertion; those
|
|
67
|
+
* files mirror this interface structurally rather than importing it, so they stay
|
|
68
|
+
* dependency-free.
|
|
69
|
+
*/
|
|
70
|
+
export interface PeerKeySigner {
|
|
71
|
+
/** base58btc peer id — the same string libp2p reports for this node. */
|
|
72
|
+
readonly peerId: string;
|
|
73
|
+
/** base64url of the libp2p protobuf-encoded public key. */
|
|
74
|
+
readonly publicKeyB64: string;
|
|
75
|
+
/** Sign the UTF-8 bytes of `message`; resolves to a base64url signature. */
|
|
76
|
+
sign(message: string): Promise<string>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Wrap a libp2p private key as a {@link PeerKeySigner}.
|
|
81
|
+
*
|
|
82
|
+
* Ed25519 only: the peer id is derived as an identity multihash (so
|
|
83
|
+
* `publicKey.toString()` already *is* the base58btc peer id — no `@libp2p/peer-id`
|
|
84
|
+
* round trip needed), and verifiers of this proof accept nothing else.
|
|
85
|
+
*
|
|
86
|
+
* @param privateKey - The node's libp2p Ed25519 private key.
|
|
87
|
+
* @returns A signer exposing the peer id, the encoded public key, and `sign`.
|
|
88
|
+
* @throws If the key is not Ed25519.
|
|
89
|
+
*/
|
|
90
|
+
export function peerKeySigner(privateKey: PrivateKey): PeerKeySigner {
|
|
91
|
+
if (privateKey.type !== 'Ed25519') {
|
|
92
|
+
throw new Error(`peerKeySigner requires an Ed25519 key, got ${privateKey.type}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const publicKey = privateKey.publicKey;
|
|
96
|
+
// For Ed25519 the public key's digest is an identity multihash, so its string
|
|
97
|
+
// form is the peer id itself — the same value `peerIdFromPrivateKey` produces.
|
|
98
|
+
const peerId = publicKey.toString();
|
|
99
|
+
const publicKeyB64 = uint8ArrayToString(publicKeyToProtobuf(publicKey), 'base64url');
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
peerId,
|
|
103
|
+
publicKeyB64,
|
|
104
|
+
async sign(message: string): Promise<string> {
|
|
105
|
+
const signature = await privateKey.sign(new TextEncoder().encode(message));
|
|
106
|
+
return uint8ArrayToString(signature, 'base64url');
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,10 +8,27 @@ export { canonicalJson } from './canonical-json.js';
|
|
|
8
8
|
export { CadreNode } from './cadre-node.js';
|
|
9
9
|
|
|
10
10
|
// Control database
|
|
11
|
-
export { ControlDatabase, buildAuthorizationMessage, type ControlDatabaseConfig, type ControlTable } from './control-database.js';
|
|
11
|
+
export { ControlDatabase, MissingHostStrandError, buildAuthorizationMessage, formationVouchMessage, formationConsentMessage, REAPABLE_TABLES, type ReapableTable, type ControlDatabaseConfig, type ControlTable, type RevocableTable, type FormationUsageResult, type MembershipChangeListener, type GuardedDeleteListener, type RevokedRowRef } from './control-database.js';
|
|
12
|
+
|
|
13
|
+
// Bounded retry for transient control-write failures (classifier + loop behind
|
|
14
|
+
// ControlDatabase.lockedWithRetry; exported so the integration package can drive the
|
|
15
|
+
// classifier against real engine errors)
|
|
16
|
+
export {
|
|
17
|
+
CONTROL_WRITE_ATTEMPTS,
|
|
18
|
+
CONTROL_WRITE_RETRY_BUDGET_MS,
|
|
19
|
+
SCHEMA_INIT_ATTEMPTS,
|
|
20
|
+
SCHEMA_INIT_RETRY_POLICY,
|
|
21
|
+
isRetriableControlWriteFailure,
|
|
22
|
+
isRetriableSchemaInitFailure,
|
|
23
|
+
retryControlWrite,
|
|
24
|
+
type ControlWriteRetryOptions
|
|
25
|
+
} from './control-write-retry.js';
|
|
26
|
+
|
|
27
|
+
// Control-plane authorization field vector (the domain/action tagging every signer shares)
|
|
28
|
+
export { controlAuthorizationFields, type ControlDomain, type ControlAction } from './control-authorization.js';
|
|
12
29
|
|
|
13
30
|
// Ed25519 key bridge (libp2p Ed25519 -> base64url keypair)
|
|
14
|
-
export { ed25519KeyPairFromLibp2p, ed25519PublicKeyFromPrivate, type Ed25519KeyPair } from './ed25519-key.js';
|
|
31
|
+
export { ed25519KeyPairFromLibp2p, ed25519PublicKeyFromPrivate, requireEd25519PublicKeyB64, type Ed25519KeyPair } from './ed25519-key.js';
|
|
15
32
|
|
|
16
33
|
// Pluggable key store (backend-agnostic identity/owner key material seam).
|
|
17
34
|
// Dependency-free: the interface, error, default slot id, and in-memory backend
|
|
@@ -26,6 +43,46 @@ export {
|
|
|
26
43
|
type KeyId
|
|
27
44
|
} from './key-store.js';
|
|
28
45
|
|
|
46
|
+
// The KeyStore-backed identity rule, shared with embedding apps that need the
|
|
47
|
+
// node key BEFORE `CadreNode` exists (RN resolves it to sign its ICE-manifest
|
|
48
|
+
// request), plus the generic "prove you hold this node key" signer built on it.
|
|
49
|
+
export {
|
|
50
|
+
loadOrCreateIdentityKey,
|
|
51
|
+
peerKeySigner,
|
|
52
|
+
type PeerKeySigner
|
|
53
|
+
} from './identity-key.js';
|
|
54
|
+
|
|
55
|
+
// Node-local trusted-owner anchor: the NON-replicated, per-party record of
|
|
56
|
+
// out-of-band-established owner keys (the trust anchor the replicated OwnerKey
|
|
57
|
+
// table cannot be). Cross-platform: interface + in-memory store only — the
|
|
58
|
+
// file-backed store is the Node-only subpath
|
|
59
|
+
// '@serfab/cadre-core/trusted-owner-store-file' (same isolation as
|
|
60
|
+
// key-store-file) so node:fs never lands in this graph.
|
|
61
|
+
export {
|
|
62
|
+
MemoryTrustedOwnerStore,
|
|
63
|
+
PersistentTrustedOwnerStore,
|
|
64
|
+
type TrustedOwnerStore,
|
|
65
|
+
type TrustSource
|
|
66
|
+
} from './trusted-owner-store.js';
|
|
67
|
+
|
|
68
|
+
// The one platform-specific piece of a persistent node-local store: a single
|
|
69
|
+
// durable text slot the embedding app supplies (IndexedDB / SecureStore /
|
|
70
|
+
// SQLite / a file). Everything else — envelope, load policy, snapshot write
|
|
71
|
+
// chain — is cross-platform in node-local-snapshot.ts.
|
|
72
|
+
export type { DurableSlot } from './node-local-snapshot.js';
|
|
73
|
+
|
|
74
|
+
// Node-local cold-start bootstrap-peer store: the dial targets retained from an
|
|
75
|
+
// applied seed so a node that could not connect on its first try keeps retrying
|
|
76
|
+
// across restarts. Same cross-platform split as the trusted-owner anchor above —
|
|
77
|
+
// interface + in-memory store here, Node-only file backend behind the subpath
|
|
78
|
+
// '@serfab/cadre-core/bootstrap-peer-store-file'.
|
|
79
|
+
export {
|
|
80
|
+
MemoryBootstrapPeerStore,
|
|
81
|
+
PersistentBootstrapPeerStore,
|
|
82
|
+
type BootstrapPeerStore,
|
|
83
|
+
type BootstrapPeerEntry
|
|
84
|
+
} from './bootstrap-peer-store.js';
|
|
85
|
+
|
|
29
86
|
// Strand database
|
|
30
87
|
export { StrandDatabase, type StrandDatabaseConfig } from './strand-database.js';
|
|
31
88
|
|
|
@@ -44,6 +101,18 @@ export {
|
|
|
44
101
|
getStrandStoragePath
|
|
45
102
|
} from './strand-instance-manager.js';
|
|
46
103
|
|
|
104
|
+
// Strand peer-join block catch-up (push this strand's own blocks to each newly
|
|
105
|
+
// connected strand peer, so a late joiner physically holds pre-join blocks).
|
|
106
|
+
export {
|
|
107
|
+
StrandBackfill,
|
|
108
|
+
DEFAULT_STRAND_BACKFILL,
|
|
109
|
+
MAX_BLOCK_MESSAGE_BYTES,
|
|
110
|
+
type StrandBackfillConfig,
|
|
111
|
+
type StrandBackfillDeps,
|
|
112
|
+
type StrandBackfillResult,
|
|
113
|
+
type StrandBackfillPushClient
|
|
114
|
+
} from './strand-backfill.js';
|
|
115
|
+
|
|
47
116
|
// Hibernation
|
|
48
117
|
export {
|
|
49
118
|
HibernationManager,
|
|
@@ -72,11 +141,24 @@ export {
|
|
|
72
141
|
type FormationUsageRecorder,
|
|
73
142
|
type ResolvedHostStrand,
|
|
74
143
|
type StrandProvisioner,
|
|
75
|
-
type FormationSigner,
|
|
76
144
|
type FormationResponseValidator,
|
|
77
145
|
type StrandSolicitationServiceOptions
|
|
78
146
|
} from './strand-solicitation.js';
|
|
79
147
|
|
|
148
|
+
// Formation approval client: asks an invite's ValidationUrl hook whether ONE redemption
|
|
149
|
+
// may proceed, and checks the answer against the bytes the control database will verify.
|
|
150
|
+
export {
|
|
151
|
+
createHttpFormationApprover,
|
|
152
|
+
signFormationApproval,
|
|
153
|
+
verifyFormationApproval,
|
|
154
|
+
FormationApprovalError,
|
|
155
|
+
type FormationApprovalRequest,
|
|
156
|
+
type FormationVouchFields,
|
|
157
|
+
type FormationApproval,
|
|
158
|
+
type FormationApprover,
|
|
159
|
+
type FormationApprovalFailure
|
|
160
|
+
} from './formation-approval.js';
|
|
161
|
+
|
|
80
162
|
// DB-backed FormationUsageRecorder (reads/writes the real CadreControl tables)
|
|
81
163
|
export { ControlFormationUsageRecorder } from './control-formation-recorder.js';
|
|
82
164
|
|
|
@@ -90,12 +172,21 @@ export { generateStrandMemberKey, strandMemberKeyPair } from './strand-member-ke
|
|
|
90
172
|
export {
|
|
91
173
|
signStrandPayload,
|
|
92
174
|
verifyStrandPayload,
|
|
175
|
+
signStrandApproval,
|
|
176
|
+
generateStrandStampId,
|
|
93
177
|
bootstrapFounderMembership,
|
|
94
178
|
issueInvite,
|
|
95
179
|
consumeInvite,
|
|
180
|
+
cancelInvite,
|
|
181
|
+
listOutstandingInvites,
|
|
96
182
|
addMemberByManager,
|
|
183
|
+
revokeMember,
|
|
184
|
+
leaveStrand,
|
|
97
185
|
registerMemberPeer,
|
|
186
|
+
listMemberPeers,
|
|
187
|
+
removeMemberPeer,
|
|
98
188
|
addManager,
|
|
189
|
+
admitManager,
|
|
99
190
|
removeManager,
|
|
100
191
|
STRAND_ENGINE,
|
|
101
192
|
STRAND_ENGINE_VERSION,
|
|
@@ -103,9 +194,17 @@ export {
|
|
|
103
194
|
type IssueInviteParams,
|
|
104
195
|
type IssuedInvite,
|
|
105
196
|
type ConsumeInviteParams,
|
|
197
|
+
type CancelInviteParams,
|
|
198
|
+
type OutstandingInvite,
|
|
106
199
|
type AddMemberByManagerParams,
|
|
200
|
+
type RevokeMemberParams,
|
|
201
|
+
type LeaveStrandParams,
|
|
107
202
|
type RegisterMemberPeerParams,
|
|
203
|
+
type RemoveOwnPeerParams,
|
|
204
|
+
type RemoveMemberPeerByManagerParams,
|
|
205
|
+
type RemoveMemberPeerParams,
|
|
108
206
|
type AddManagerParams,
|
|
207
|
+
type AdmitManagerParams,
|
|
109
208
|
type RemoveManagerParams
|
|
110
209
|
} from './strand-membership-writer.js';
|
|
111
210
|
|
|
@@ -136,7 +235,13 @@ export {
|
|
|
136
235
|
peerAuthorizationDigest,
|
|
137
236
|
cadrePeerVoucherDigest,
|
|
138
237
|
cadrePeerRemoveDigest,
|
|
139
|
-
|
|
238
|
+
deviceTokenAddDigest,
|
|
239
|
+
deviceTokenRemoveDigest,
|
|
240
|
+
formationConsentDigest,
|
|
241
|
+
verifyPeerAuthorization,
|
|
242
|
+
verifyCadrePeerVoucher,
|
|
243
|
+
verifyFormationConsent,
|
|
244
|
+
type DeviceTokenAuthorizedRow
|
|
140
245
|
} from './peer-authorization.js';
|
|
141
246
|
|
|
142
247
|
// Strand Wake (control-network push-wake protocol)
|
|
@@ -219,7 +324,7 @@ export {
|
|
|
219
324
|
|
|
220
325
|
// Seed trust policy (trust anchor for incoming seeds)
|
|
221
326
|
export {
|
|
222
|
-
|
|
327
|
+
anchoredTrustPolicy,
|
|
223
328
|
pinnedKeyTrustPolicy,
|
|
224
329
|
tofuTrustPolicy,
|
|
225
330
|
type SeedTrustPolicy,
|
|
@@ -260,6 +365,32 @@ export {
|
|
|
260
365
|
type StrandFormationManagerOptions
|
|
261
366
|
} from './strand-formation-manager.js';
|
|
262
367
|
|
|
368
|
+
// Control-network inbound connection gate (membership defense-in-depth)
|
|
369
|
+
export {
|
|
370
|
+
createMembershipConnectionGater,
|
|
371
|
+
STRANGER_OPEN_PROTOCOLS,
|
|
372
|
+
DEFAULT_ENROLLMENT_WINDOW_MS,
|
|
373
|
+
ADMISSION_DECISION_TIMEOUT_MS,
|
|
374
|
+
type InboundAdmissionPolicy
|
|
375
|
+
} from './membership-connection-gater.js';
|
|
376
|
+
|
|
377
|
+
// Relay reservation via the bare `/p2p-circuit` search listener (fail-soft;
|
|
378
|
+
// the fail-fast alternative is `network.relayAddrs`, see relay-reservation.ts)
|
|
379
|
+
export {
|
|
380
|
+
circuitMultiaddrs,
|
|
381
|
+
superviseRelayReservation,
|
|
382
|
+
DEFAULT_RELAY_RESERVE_TIMEOUT_MS,
|
|
383
|
+
DEFAULT_RELAY_RESERVE_POLL_MS,
|
|
384
|
+
DEFAULT_RELAY_CHECK_MS,
|
|
385
|
+
DEFAULT_RELAY_MIN_BACKOFF_MS,
|
|
386
|
+
DEFAULT_RELAY_MAX_BACKOFF_MS,
|
|
387
|
+
type RelayReservationStatus,
|
|
388
|
+
type RelayReservationState,
|
|
389
|
+
type RelayReserveOptions,
|
|
390
|
+
type RelayReservationSupervisor,
|
|
391
|
+
type RelayReservationSupervisorOptions
|
|
392
|
+
} from './relay-reservation.js';
|
|
393
|
+
|
|
263
394
|
// Connection-path diagnostics (relayed vs direct classification + summary)
|
|
264
395
|
export {
|
|
265
396
|
classifyTransport,
|
package/src/key-store-file.ts
CHANGED
|
@@ -14,12 +14,12 @@
|
|
|
14
14
|
* `node:fs` edge never reaches a bundler that cannot satisfy it.
|
|
15
15
|
*/
|
|
16
16
|
import debug from 'debug';
|
|
17
|
-
import {
|
|
18
|
-
import type { FileHandle } from 'node:fs/promises';
|
|
17
|
+
import { readFile, rm, readdir } from 'node:fs/promises';
|
|
19
18
|
import { join } from 'node:path';
|
|
20
19
|
import { randomBytes } from 'node:crypto';
|
|
21
20
|
import type { KeyId, KeyStore } from './key-store.js';
|
|
22
21
|
import { KeyStoreAccessError } from './key-store.js';
|
|
22
|
+
import { encodeFileSafeComponent, isNotFound, writeFileAtomically } from './fs-atomic.js';
|
|
23
23
|
|
|
24
24
|
const log = debug('sereus:cadre:key-store-file');
|
|
25
25
|
|
|
@@ -33,24 +33,14 @@ const SLOT_SUFFIX = '.key';
|
|
|
33
33
|
*/
|
|
34
34
|
const TEMP_SUFFIX = '.tmp';
|
|
35
35
|
|
|
36
|
-
/** Best-effort POSIX permissions (ignored on Windows / unsupported FSes). */
|
|
37
|
-
const DIR_MODE = 0o700;
|
|
38
|
-
const FILE_MODE = 0o600;
|
|
39
|
-
|
|
40
36
|
/**
|
|
41
|
-
* Percent-encode a keyId into a filesystem-safe filename component
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* `.` so the `.key` suffix is the only literal dot, making suffix-stripping in
|
|
46
|
-
* {@link decodeKeyId} unambiguous. {@link decodeKeyId} reverses every escape via
|
|
47
|
-
* `decodeURIComponent`. The result contains only `A-Za-z0-9-_` and `%XX`.
|
|
37
|
+
* Percent-encode a keyId into a filesystem-safe filename component (see
|
|
38
|
+
* {@link encodeFileSafeComponent} — `.` is escaped so the `.key` suffix is the
|
|
39
|
+
* only literal dot, making suffix-stripping in {@link decodeKeyId} unambiguous;
|
|
40
|
+
* {@link decodeKeyId} reverses every escape via `decodeURIComponent`).
|
|
48
41
|
*/
|
|
49
42
|
function encodeKeyId(keyId: KeyId): string {
|
|
50
|
-
return
|
|
51
|
-
/[!'()*~.]/g,
|
|
52
|
-
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
|
|
53
|
-
);
|
|
43
|
+
return encodeFileSafeComponent(keyId);
|
|
54
44
|
}
|
|
55
45
|
|
|
56
46
|
/**
|
|
@@ -70,12 +60,6 @@ function decodeKeyId(fileName: string): KeyId | undefined {
|
|
|
70
60
|
}
|
|
71
61
|
}
|
|
72
62
|
|
|
73
|
-
/** Whether an unknown error is a Node "file/dir not found" (ENOENT). */
|
|
74
|
-
function isNotFound(error: unknown): boolean {
|
|
75
|
-
return typeof error === 'object' && error !== null
|
|
76
|
-
&& (error as { code?: unknown }).code === 'ENOENT';
|
|
77
|
-
}
|
|
78
|
-
|
|
79
63
|
/**
|
|
80
64
|
* File-backed {@link KeyStore}. Each slot is `<dir>/<encoded keyId>.key`
|
|
81
65
|
* containing the raw material bytes. The directory is created lazily on first
|
|
@@ -119,66 +103,11 @@ export class FileKeyStore implements KeyStore {
|
|
|
119
103
|
* Crash-atomic write: a concurrent reader sees either the complete previous
|
|
120
104
|
* bytes or the complete new bytes, never a torn slot. The new material is
|
|
121
105
|
* written to a sibling temp file, fsync'd, then atomically renamed over the
|
|
122
|
-
* slot. A failure at any point removes the
|
|
123
|
-
* slot untouched.
|
|
106
|
+
* slot (see {@link writeFileAtomically}). A failure at any point removes the
|
|
107
|
+
* temp file and leaves the previous slot untouched.
|
|
124
108
|
*/
|
|
125
109
|
async set(keyId: KeyId, keyMaterial: Uint8Array): Promise<void> {
|
|
126
|
-
await
|
|
127
|
-
await this.writeSlotAtomically(this.tempPath(keyId), this.slotPath(keyId), keyMaterial);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Write material to `tmpPath`, durably land it, then atomically swap it onto
|
|
132
|
-
* `finalPath`.
|
|
133
|
-
*
|
|
134
|
-
* Durability bar: the temp file is fsync'd before the rename so a power loss
|
|
135
|
-
* cannot surface a slot whose bytes never reached the platter, and the slot
|
|
136
|
-
* directory is fsync'd afterwards (best-effort — see {@link syncDir}) so the
|
|
137
|
-
* rename itself survives a crash. On POSIX `rename(2)` replaces atomically;
|
|
138
|
-
* Node maps to `MoveFileEx(MOVEFILE_REPLACE_EXISTING)` on Windows, which is
|
|
139
|
-
* also atomic — though it can fail with `EPERM`/`EBUSY` if another process
|
|
140
|
-
* holds the destination open. That cross-process case is out of scope here
|
|
141
|
-
* (the in-process identity path is single-flight); such a failure surfaces
|
|
142
|
-
* to the caller with the previous slot left intact.
|
|
143
|
-
*/
|
|
144
|
-
private async writeSlotAtomically(tmpPath: string, finalPath: string, keyMaterial: Uint8Array): Promise<void> {
|
|
145
|
-
// 'wx' creates the temp file exclusively; a name collision (astronomically
|
|
146
|
-
// unlikely given the random component) fails here before we own anything.
|
|
147
|
-
const handle = await open(tmpPath, 'wx', FILE_MODE);
|
|
148
|
-
try {
|
|
149
|
-
try {
|
|
150
|
-
await handle.writeFile(keyMaterial);
|
|
151
|
-
await handle.sync(); // land bytes on disk before the rename exposes them
|
|
152
|
-
} finally {
|
|
153
|
-
await handle.close().catch(() => {});
|
|
154
|
-
}
|
|
155
|
-
await rename(tmpPath, finalPath);
|
|
156
|
-
} catch (error) {
|
|
157
|
-
// Any failure after creation (write, sync, or rename): drop the temp
|
|
158
|
-
// file so a retry leaves no `.tmp` debris. The slot is only ever
|
|
159
|
-
// swapped by the atomic rename, so the previous bytes stay intact.
|
|
160
|
-
await rm(tmpPath, { force: true }).catch(() => {});
|
|
161
|
-
throw error;
|
|
162
|
-
}
|
|
163
|
-
await this.syncDir();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Best-effort fsync of the slot directory so a fresh slot's directory entry is
|
|
168
|
-
* durable across power loss. Opening a directory for fsync is not portable
|
|
169
|
-
* (unsupported on Windows and some filesystems), and rename atomicity does not
|
|
170
|
-
* depend on it — so a failure here is logged and swallowed, never surfaced.
|
|
171
|
-
*/
|
|
172
|
-
private async syncDir(): Promise<void> {
|
|
173
|
-
let dirHandle: FileHandle | undefined;
|
|
174
|
-
try {
|
|
175
|
-
dirHandle = await open(this.dir, 'r');
|
|
176
|
-
await dirHandle.sync();
|
|
177
|
-
} catch (error) {
|
|
178
|
-
log('FileKeyStore: directory fsync skipped for %s: %o', this.dir, error);
|
|
179
|
-
} finally {
|
|
180
|
-
await dirHandle?.close().catch(() => {});
|
|
181
|
-
}
|
|
110
|
+
await writeFileAtomically(this.dir, this.tempPath(keyId), this.slotPath(keyId), keyMaterial);
|
|
182
111
|
}
|
|
183
112
|
|
|
184
113
|
async delete(keyId: KeyId): Promise<void> {
|