@nervur.org/installer-node 0.0.0-stage → 0.37.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +105 -0
- package/README.md +158 -2
- package/SECURITY.md +8 -0
- package/bin/nervur.js +5 -0
- package/cargo/emscripten-module.wasm +0 -0
- package/cargo/manifest.json +1 -0
- package/cargo/sqlite3.wasm +0 -0
- package/cargo/table-i-i.wasm +0 -0
- package/cargo/table-i-ii.wasm +0 -0
- package/cargo/table-i-iii.wasm +0 -0
- package/cargo/table-i-iiii.wasm +0 -0
- package/cargo/table-i-iiiii.wasm +0 -0
- package/cargo/table-i-iiij.wasm +0 -0
- package/cargo/table-i-ij.wasm +0 -0
- package/cargo/table-v-ii.wasm +0 -0
- package/cargo/table-v-iii.wasm +0 -0
- package/cargo/worker.js +30756 -0
- package/organs/console/node/guard.d.ts +10 -0
- package/organs/console/node/guard.js +52 -0
- package/organs/console/node/index.d.ts +4 -0
- package/organs/console/node/index.js +5 -0
- package/organs/console/node/line.d.ts +16 -0
- package/organs/console/node/line.js +150 -0
- package/organs/console/node/main.js +51 -0
- package/organs/console/node/program.d.ts +1 -0
- package/organs/console/node/program.js +5 -0
- package/organs/console/node/proof.d.ts +15 -0
- package/organs/console/node/proof.js +84 -0
- package/organs/console/node/socket.d.ts +15 -0
- package/organs/console/node/socket.js +99 -0
- package/organs/disk/index.d.ts +1 -0
- package/organs/disk/index.js +2 -0
- package/organs/disk/node/files.js +162 -0
- package/organs/disk/node/main.js +20 -0
- package/organs/disk/node/program.d.ts +1 -0
- package/organs/disk/node/program.js +5 -0
- package/organs/disk/organ.d.ts +5 -0
- package/organs/disk/organ.js +69 -0
- package/organs/network/node/fill.d.ts +21 -0
- package/organs/network/node/fill.js +195 -0
- package/organs/network/node/flows.js +66 -0
- package/organs/network/node/half.js +260 -0
- package/organs/network/node/lines.d.ts +29 -0
- package/organs/network/node/lines.js +236 -0
- package/organs/network/node/link.d.ts +24 -0
- package/organs/network/node/link.js +215 -0
- package/organs/network/node/main.js +41 -0
- package/organs/network/node/moved.d.ts +2 -0
- package/organs/network/node/moved.js +20 -0
- package/organs/network/node/near.d.ts +63 -0
- package/organs/network/node/near.js +263 -0
- package/organs/network/node/program.d.ts +1 -0
- package/organs/network/node/program.js +5 -0
- package/organs/network/node/schemes.d.ts +1 -0
- package/organs/network/node/schemes.js +2 -0
- package/organs/network/node/sent.d.ts +13 -0
- package/organs/network/node/sent.js +101 -0
- package/organs/network/node/served.d.ts +19 -0
- package/organs/network/node/served.js +237 -0
- package/organs/screen/node/client.js +3 -0
- package/organs/screen/node/follow.js +266 -0
- package/organs/screen/node/frames.js +26 -0
- package/organs/screen/node/held.js +27 -0
- package/organs/screen/node/http.js +179 -0
- package/organs/screen/node/lay.js +116 -0
- package/organs/screen/node/leave.js +27 -0
- package/organs/screen/node/linked.js +69 -0
- package/organs/screen/node/listen.js +61 -0
- package/organs/screen/node/look.js +121 -0
- package/organs/screen/node/loop.js +154 -0
- package/organs/screen/node/main.js +74 -0
- package/organs/screen/node/mirror.js +112 -0
- package/organs/screen/node/notice.js +44 -0
- package/organs/screen/node/presser.js +11 -0
- package/organs/screen/node/presses.js +108 -0
- package/organs/screen/node/program.d.ts +1 -0
- package/organs/screen/node/program.js +5 -0
- package/organs/screen/node/shown.js +205 -0
- package/organs/screen/node/standing.js +29 -0
- package/organs/screen/node/state.js +80 -0
- package/organs/screen/node/tell.js +47 -0
- package/organs/screen/node/uid.js +65 -0
- package/organs/screen/node/worn.js +23 -0
- package/organs/shell/index.d.ts +1 -0
- package/organs/shell/index.js +2 -0
- package/organs/shell/node/main.js +26 -0
- package/organs/shell/node/output.js +70 -0
- package/organs/shell/node/pressure.js +14 -0
- package/organs/shell/node/program.d.ts +1 -0
- package/organs/shell/node/program.js +5 -0
- package/organs/shell/node/runs.js +220 -0
- package/organs/shell/node/told.js +70 -0
- package/organs/shell/organ.d.ts +5 -0
- package/organs/shell/organ.js +59 -0
- package/package.json +43 -4
- package/src/command/act.d.ts +31 -0
- package/src/command/act.js +256 -0
- package/src/command/answers.d.ts +3 -0
- package/src/command/answers.js +232 -0
- package/src/command/args.d.ts +14 -0
- package/src/command/args.js +220 -0
- package/src/command/arrive.d.ts +2 -0
- package/src/command/arrive.js +21 -0
- package/src/command/asked.d.ts +18 -0
- package/src/command/asked.js +49 -0
- package/src/command/backup.d.ts +18 -0
- package/src/command/backup.js +261 -0
- package/src/command/before.d.ts +41 -0
- package/src/command/before.js +237 -0
- package/src/command/born.d.ts +27 -0
- package/src/command/born.js +103 -0
- package/src/command/bridge.d.ts +19 -0
- package/src/command/bridge.js +152 -0
- package/src/command/chain.d.ts +9 -0
- package/src/command/chain.js +48 -0
- package/src/command/channel.d.ts +29 -0
- package/src/command/channel.js +263 -0
- package/src/command/choices.d.ts +35 -0
- package/src/command/choices.js +173 -0
- package/src/command/claim.d.ts +12 -0
- package/src/command/claim.js +46 -0
- package/src/command/commands.d.ts +16 -0
- package/src/command/commands.js +257 -0
- package/src/command/ends.d.ts +16 -0
- package/src/command/ends.js +78 -0
- package/src/command/follow.d.ts +12 -0
- package/src/command/follow.js +66 -0
- package/src/command/given.d.ts +39 -0
- package/src/command/given.js +53 -0
- package/src/command/held.d.ts +15 -0
- package/src/command/held.js +56 -0
- package/src/command/index.d.ts +1 -0
- package/src/command/index.js +203 -0
- package/src/command/jailer.d.ts +28 -0
- package/src/command/jailer.js +135 -0
- package/src/command/laid.d.ts +6 -0
- package/src/command/laid.js +31 -0
- package/src/command/left.d.ts +9 -0
- package/src/command/left.js +34 -0
- package/src/command/line.d.ts +7 -0
- package/src/command/line.js +39 -0
- package/src/command/link.d.ts +30 -0
- package/src/command/link.js +123 -0
- package/src/command/lobes.d.ts +1 -0
- package/src/command/lobes.js +10 -0
- package/src/command/locks.d.ts +17 -0
- package/src/command/locks.js +76 -0
- package/src/command/mcp/calls.d.ts +27 -0
- package/src/command/mcp/calls.js +155 -0
- package/src/command/mcp/notices.d.ts +22 -0
- package/src/command/mcp/notices.js +203 -0
- package/src/command/mcp/resources.d.ts +31 -0
- package/src/command/mcp/resources.js +108 -0
- package/src/command/mcp/server.d.ts +19 -0
- package/src/command/mcp/server.js +149 -0
- package/src/command/mcp/tools.d.ts +39 -0
- package/src/command/mcp/tools.js +131 -0
- package/src/command/mcp.d.ts +8 -0
- package/src/command/mcp.js +39 -0
- package/src/command/noticed.d.ts +11 -0
- package/src/command/noticed.js +55 -0
- package/src/command/older.d.ts +11 -0
- package/src/command/older.js +45 -0
- package/src/command/open.d.ts +24 -0
- package/src/command/open.js +107 -0
- package/src/command/passphrase.d.ts +6 -0
- package/src/command/passphrase.js +82 -0
- package/src/command/reclaim.d.ts +7 -0
- package/src/command/reclaim.js +47 -0
- package/src/command/refused.d.ts +4 -0
- package/src/command/refused.js +27 -0
- package/src/command/release.d.ts +27 -0
- package/src/command/release.js +153 -0
- package/src/command/repo.d.ts +31 -0
- package/src/command/repo.js +133 -0
- package/src/command/rewrap.d.ts +16 -0
- package/src/command/rewrap.js +175 -0
- package/src/command/runtime.d.ts +25 -0
- package/src/command/runtime.js +175 -0
- package/src/command/salvage.d.ts +4 -0
- package/src/command/salvage.js +42 -0
- package/src/command/screen.d.ts +14 -0
- package/src/command/screen.js +62 -0
- package/src/command/spawn.d.ts +23 -0
- package/src/command/spawn.js +213 -0
- package/src/command/start.d.ts +34 -0
- package/src/command/start.js +246 -0
- package/src/command/state.d.ts +5 -0
- package/src/command/state.js +39 -0
- package/src/command/token.d.ts +1 -0
- package/src/command/token.js +9 -0
- package/src/command/trial.d.ts +17 -0
- package/src/command/trial.js +92 -0
- package/src/command/vouch.d.ts +1 -0
- package/src/command/vouch.js +15 -0
- package/src/command/windows-processes.d.ts +2 -0
- package/src/command/windows-processes.js +22 -0
- package/src/command/wipe.d.ts +9 -0
- package/src/command/wipe.js +105 -0
- package/src/command/words.d.ts +5 -0
- package/src/command/words.js +53 -0
- package/src/door/claim.d.ts +5 -0
- package/src/door/claim.js +21 -0
- package/src/door/claimed.d.ts +7 -0
- package/src/door/claimed.js +110 -0
- package/src/door/configuration.d.ts +40 -0
- package/src/door/configuration.js +206 -0
- package/src/door/design.d.ts +13 -0
- package/src/door/design.js +71 -0
- package/src/door/digests.d.ts +7 -0
- package/src/door/digests.js +36 -0
- package/src/door/distribution.d.ts +25 -0
- package/src/door/distribution.js +88 -0
- package/src/door/door.d.ts +41 -0
- package/src/door/door.js +219 -0
- package/src/door/dressed.d.ts +34 -0
- package/src/door/dressed.js +182 -0
- package/src/door/faculties.d.ts +29 -0
- package/src/door/faculties.js +88 -0
- package/src/door/longest.d.ts +6 -0
- package/src/door/longest.js +25 -0
- package/src/fills/account-only.d.ts +3 -0
- package/src/fills/account-only.js +24 -0
- package/src/fills/boot-key.d.ts +21 -0
- package/src/fills/boot-key.js +70 -0
- package/src/fills/boot-move.d.ts +25 -0
- package/src/fills/boot-move.js +86 -0
- package/src/fills/bridge.d.ts +13 -0
- package/src/fills/bridge.js +86 -0
- package/src/fills/chance.d.ts +2 -0
- package/src/fills/chance.js +7 -0
- package/src/fills/crypto.d.ts +2 -0
- package/src/fills/crypto.js +94 -0
- package/src/fills/flushed.d.ts +9 -0
- package/src/fills/flushed.js +80 -0
- package/src/fills/hardware.d.ts +18 -0
- package/src/fills/hardware.js +70 -0
- package/src/fills/held.d.ts +31 -0
- package/src/fills/held.js +171 -0
- package/src/fills/keepers.d.ts +23 -0
- package/src/fills/keepers.js +151 -0
- package/src/fills/keyring.d.ts +19 -0
- package/src/fills/keyring.js +131 -0
- package/src/fills/pages.d.ts +3 -0
- package/src/fills/pages.js +260 -0
- package/src/fills/reserve.d.ts +11 -0
- package/src/fills/reserve.js +72 -0
- package/src/fills/time.d.ts +2 -0
- package/src/fills/time.js +29 -0
- package/src/fills/unlocker.d.ts +6 -0
- package/src/fills/unlocker.js +26 -0
- package/src/fills/vitals.d.ts +8 -0
- package/src/fills/vitals.js +100 -0
- package/src/fills/windows-start.d.ts +2 -0
- package/src/fills/windows-start.js +21 -0
- package/src/halves/build.d.ts +10 -0
- package/src/halves/build.js +219 -0
- package/src/halves/end.d.ts +17 -0
- package/src/halves/end.js +157 -0
- package/src/halves/genome.d.ts +3 -0
- package/src/halves/genome.js +21 -0
- package/src/halves/handler.d.ts +8 -0
- package/src/halves/handler.js +40 -0
- package/src/halves/index.d.ts +5 -0
- package/src/halves/index.js +6 -0
- package/src/halves/laid.d.ts +12 -0
- package/src/halves/laid.js +103 -0
- package/src/halves/lent.d.ts +6 -0
- package/src/halves/lent.js +147 -0
- package/src/halves/native.d.ts +12 -0
- package/src/halves/native.js +53 -0
- package/src/halves/statements.d.ts +23 -0
- package/src/halves/statements.js +165 -0
- package/src/halves/tokens.d.ts +22 -0
- package/src/halves/tokens.js +135 -0
- package/src/link/link.d.ts +4 -0
- package/src/link/link.js +4 -0
- package/src/link/placeholder.d.ts +1 -0
- package/src/link/placeholder.js +21 -0
- package/src/link/steps.d.ts +52 -0
- package/src/link/steps.js +118 -0
- package/src/lobes/calls.d.ts +76 -0
- package/src/lobes/calls.js +61 -0
- package/src/lobes/channel.d.ts +76 -0
- package/src/lobes/channel.js +202 -0
- package/src/lobes/index.d.ts +1 -0
- package/src/lobes/index.js +2 -0
- package/src/lobes/lobes.d.ts +15 -0
- package/src/lobes/lobes.js +244 -0
- package/src/lobes/main.js +107 -0
- package/src/lobes/thread.d.ts +22 -0
- package/src/lobes/thread.js +270 -0
- package/src/organism/act.d.ts +8 -0
- package/src/organism/act.js +63 -0
- package/src/organism/beat.d.ts +5 -0
- package/src/organism/beat.js +45 -0
- package/src/organism/compiled.d.ts +2 -0
- package/src/organism/compiled.js +24 -0
- package/src/organism/folder.d.ts +88 -0
- package/src/organism/folder.js +259 -0
- package/src/organism/main.d.ts +1 -0
- package/src/organism/main.js +11 -0
- package/src/organism/organism.d.ts +1 -0
- package/src/organism/organism.js +219 -0
- package/src/organism/said.d.ts +12 -0
- package/src/organism/said.js +30 -0
- package/src/organism/told.d.ts +5 -0
- package/src/organism/told.js +110 -0
- package/src/organism/unlock.d.ts +8 -0
- package/src/organism/unlock.js +95 -0
- package/src/organism/upgrade.d.ts +24 -0
- package/src/organism/upgrade.js +145 -0
- package/src/organism/via.d.ts +7 -0
- package/src/organism/via.js +20 -0
- package/src/service/body.d.ts +8 -0
- package/src/service/body.js +34 -0
- package/src/service/handler.d.ts +20 -0
- package/src/service/handler.js +128 -0
- package/src/service/index.d.ts +4 -0
- package/src/service/index.js +16 -0
- package/src/service/manager.d.ts +18 -0
- package/src/service/manager.js +185 -0
- package/src/service/service.d.ts +24 -0
- package/src/service/service.js +200 -0
- package/src/service/swap.d.ts +12 -0
- package/src/service/swap.js +74 -0
- package/src/service/watchdog.d.ts +6 -0
- package/src/service/watchdog.js +31 -0
- package/src/tcp/crowd.d.ts +18 -0
- package/src/tcp/crowd.js +85 -0
- package/src/tcp/dial.d.ts +23 -0
- package/src/tcp/dial.js +102 -0
- package/src/tcp/frame.d.ts +23 -0
- package/src/tcp/frame.js +159 -0
- package/src/tcp/listen.d.ts +50 -0
- package/src/tcp/listen.js +207 -0
- package/src/tcp/source.d.ts +2 -0
- package/src/tcp/source.js +26 -0
- package/src/version/index.d.ts +1 -0
- package/src/version/index.js +2 -0
- package/src/version/version.d.ts +1 -0
- package/src/version/version.js +2 -0
- package/src/work/first.d.ts +11 -0
- package/src/work/first.js +91 -0
- package/src/work/index.d.ts +2 -0
- package/src/work/index.js +15 -0
- package/src/work/work.d.ts +20 -0
- package/src/work/work.js +136 -0
- package/src/ws/dial.d.ts +7 -0
- package/src/ws/dial.js +252 -0
- package/src/ws/dialing.d.ts +50 -0
- package/src/ws/dialing.js +72 -0
- package/src/ws/frame.d.ts +32 -0
- package/src/ws/frame.js +124 -0
- package/src/ws/handshake.d.ts +4 -0
- package/src/ws/handshake.js +27 -0
- package/src/ws/line.d.ts +45 -0
- package/src/ws/line.js +250 -0
- package/src/ws/listen.d.ts +42 -0
- package/src/ws/listen.js +272 -0
- package/src/ws/rfc6455.d.ts +18 -0
- package/src/ws/rfc6455.js +187 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import {
|
|
3
|
+
createCipheriv,
|
|
4
|
+
createDecipheriv,
|
|
5
|
+
createHash,
|
|
6
|
+
createPrivateKey,
|
|
7
|
+
createPublicKey,
|
|
8
|
+
diffieHellman,
|
|
9
|
+
sign,
|
|
10
|
+
verify,
|
|
11
|
+
|
|
12
|
+
} from 'node:crypto';
|
|
13
|
+
import { nobleCrypto } from '@nervur.org/genome/quo';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const KEY = 32;
|
|
17
|
+
const SIGNATURE = 64;
|
|
18
|
+
const TAG = 16;
|
|
19
|
+
|
|
20
|
+
const ED_SECRET = Buffer.from('302e020100300506032b657004220420', 'hex');
|
|
21
|
+
const ED_PUBLIC = Buffer.from('302a300506032b6570032100', 'hex');
|
|
22
|
+
const X_SECRET = Buffer.from('302e020100300506032b656e04220420', 'hex');
|
|
23
|
+
const X_PUBLIC = Buffer.from('302a300506032b656e032100', 'hex');
|
|
24
|
+
|
|
25
|
+
const secretOf = (prefix , secret ) =>
|
|
26
|
+
createPrivateKey({ key: Buffer.concat([prefix, secret]), format: 'der', type: 'pkcs8' });
|
|
27
|
+
const publicOf = (prefix , key ) =>
|
|
28
|
+
createPublicKey({ key: Buffer.concat([prefix, key]), format: 'der', type: 'spki' });
|
|
29
|
+
const publicBytes = (prefix , secret ) =>
|
|
30
|
+
new Uint8Array(createPublicKey(secret).export({ format: 'der', type: 'spki' }).subarray(prefix.length));
|
|
31
|
+
|
|
32
|
+
const SMALL = [
|
|
33
|
+
'0100000000000000000000000000000000000000000000000000000000000000',
|
|
34
|
+
'ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f',
|
|
35
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
36
|
+
'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a',
|
|
37
|
+
'26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const refusedKey = (publicKey ) => {
|
|
41
|
+
const y = Buffer.from(publicKey);
|
|
42
|
+
y[31] = (y[31] ?? 0) & 0x7f;
|
|
43
|
+
if (y[31] === 0x7f && y.subarray(1, 31).every((byte) => byte === 0xff) && (y[0] ?? 0) >= 0xed) return true;
|
|
44
|
+
return SMALL.includes(y.toString('hex'));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const ed25519Verify = (publicKey, message, signature) => {
|
|
48
|
+
if (publicKey.length !== KEY || signature.length !== SIGNATURE || refusedKey(publicKey)) return false;
|
|
49
|
+
try {
|
|
50
|
+
if (verify(null, message, publicOf(ED_PUBLIC, publicKey), signature)) return true;
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
return nobleCrypto.ed25519Verify(publicKey, message, signature);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const nodeCrypto = {
|
|
57
|
+
...nobleCrypto,
|
|
58
|
+
sha256: (bytes) => new Uint8Array(createHash('sha256').update(bytes).digest()),
|
|
59
|
+
ed25519PublicKey: (secret) => publicBytes(ED_PUBLIC, secretOf(ED_SECRET, secret)),
|
|
60
|
+
ed25519Sign: (secret, message) => new Uint8Array(sign(null, message, secretOf(ED_SECRET, secret))),
|
|
61
|
+
ed25519Verify,
|
|
62
|
+
x25519PublicKey: (secret) => publicBytes(X_PUBLIC, secretOf(X_SECRET, secret)),
|
|
63
|
+
x25519Agree: (secret, publicKey) => {
|
|
64
|
+
if (publicKey.length !== KEY) return null;
|
|
65
|
+
try {
|
|
66
|
+
const agreed = diffieHellman({
|
|
67
|
+
privateKey: secretOf(X_SECRET, secret),
|
|
68
|
+
publicKey: publicOf(X_PUBLIC, publicKey),
|
|
69
|
+
});
|
|
70
|
+
return agreed.every((byte) => byte === 0) ? null : new Uint8Array(agreed);
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
aesSeal: (key, nonce, plaintext, additional) => {
|
|
76
|
+
const cipher = createCipheriv('aes-256-gcm', key, nonce, { authTagLength: TAG });
|
|
77
|
+
cipher.setAAD(additional);
|
|
78
|
+
return new Uint8Array(Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]));
|
|
79
|
+
},
|
|
80
|
+
aesOpen: (key, nonce, ciphertext, additional) => {
|
|
81
|
+
if (ciphertext.length < TAG) return null;
|
|
82
|
+
const cut = ciphertext.length - TAG;
|
|
83
|
+
try {
|
|
84
|
+
const decipher = createDecipheriv('aes-256-gcm', key, nonce, { authTagLength: TAG });
|
|
85
|
+
decipher.setAAD(additional);
|
|
86
|
+
decipher.setAuthTag(ciphertext.subarray(cut));
|
|
87
|
+
const opened = decipher.update(ciphertext.subarray(0, cut));
|
|
88
|
+
decipher.final();
|
|
89
|
+
return new Uint8Array(opened);
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const synced: (path: string) => void;
|
|
2
|
+
export declare const folderFlushed: typeof synced;
|
|
3
|
+
export declare const laidWhole: (path: string, content: string | Uint8Array, mode: number) => void;
|
|
4
|
+
export declare const movedWhole: (from: string, to: string) => void;
|
|
5
|
+
export declare const removedWhole: (path: string) => void;
|
|
6
|
+
export declare const foldersLaid: (path: string, mode: number) => void;
|
|
7
|
+
export declare const treeFlushed: (root: string) => void;
|
|
8
|
+
export declare const linkedWhole: (target: string, at: string) => void;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
fsyncSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
mkdirSync,
|
|
8
|
+
openSync,
|
|
9
|
+
readdirSync,
|
|
10
|
+
renameSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
symlinkSync,
|
|
13
|
+
writeFileSync,
|
|
14
|
+
} from 'node:fs';
|
|
15
|
+
import { dirname, join, resolve } from 'node:path';
|
|
16
|
+
import { accountOnlyLaid } from './account-only.js';
|
|
17
|
+
|
|
18
|
+
const synced = (path ) => {
|
|
19
|
+
const windows = process.platform === 'win32';
|
|
20
|
+
if (windows && lstatSync(path).isDirectory()) return;
|
|
21
|
+
let open;
|
|
22
|
+
try {
|
|
23
|
+
open = openSync(path, windows ? 'r+' : 'r');
|
|
24
|
+
} catch (thrown) {
|
|
25
|
+
if (windows && thrown instanceof Error && 'code' in thrown && thrown.code === 'EBUSY') return;
|
|
26
|
+
throw thrown;
|
|
27
|
+
}
|
|
28
|
+
fsyncSync(open);
|
|
29
|
+
closeSync(open);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const folderFlushed = synced;
|
|
33
|
+
|
|
34
|
+
export const laidWhole = (path , content , mode ) => {
|
|
35
|
+
const writing = `${path}.writing`;
|
|
36
|
+
const open = openSync(writing, 'w', mode);
|
|
37
|
+
writeFileSync(open, content);
|
|
38
|
+
fsyncSync(open);
|
|
39
|
+
closeSync(open);
|
|
40
|
+
renameSync(writing, path);
|
|
41
|
+
synced(dirname(path));
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const movedWhole = (from , to ) => {
|
|
45
|
+
renameSync(from, to);
|
|
46
|
+
synced(dirname(to));
|
|
47
|
+
if (dirname(from) !== dirname(to)) synced(dirname(from));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const removedWhole = (path ) => {
|
|
51
|
+
rmSync(path, { force: true });
|
|
52
|
+
synced(dirname(path));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const foldersLaid = (path , mode ) => {
|
|
56
|
+
const missing = [];
|
|
57
|
+
for (let at = resolve(path); !existsSync(at); at = dirname(at)) missing.push(at);
|
|
58
|
+
mkdirSync(path, { recursive: true, mode });
|
|
59
|
+
if (process.platform === 'win32' && (mode & 0o077) === 0 && missing.length > 0) accountOnlyLaid(resolve(path));
|
|
60
|
+
for (const at of missing) synced(dirname(at));
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const treeFlushed = (root ) => {
|
|
64
|
+
const under = (path ) => {
|
|
65
|
+
const stat = lstatSync(path);
|
|
66
|
+
if (stat.isSymbolicLink()) return;
|
|
67
|
+
if (stat.isDirectory()) for (const name of readdirSync(path)) under(join(path, name));
|
|
68
|
+
if (stat.isFile() || stat.isDirectory()) synced(path);
|
|
69
|
+
};
|
|
70
|
+
under(root);
|
|
71
|
+
synced(dirname(root));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const linkedWhole = (target , at ) => {
|
|
75
|
+
const writing = `${at}.writing`;
|
|
76
|
+
rmSync(writing, { force: true });
|
|
77
|
+
symlinkSync(target, writing);
|
|
78
|
+
renameSync(writing, at);
|
|
79
|
+
synced(dirname(at));
|
|
80
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Ran } from './keyring.js';
|
|
2
|
+
type Why = {
|
|
3
|
+
readonly why: string;
|
|
4
|
+
};
|
|
5
|
+
export type Board = {
|
|
6
|
+
seal(key: Uint8Array): Promise<{
|
|
7
|
+
readonly sealed: string;
|
|
8
|
+
} | Why>;
|
|
9
|
+
open(sealed: string): Promise<{
|
|
10
|
+
readonly key: Uint8Array;
|
|
11
|
+
} | Why>;
|
|
12
|
+
};
|
|
13
|
+
export declare const boardOf: (platform: NodeJS.Platform, ran?: Ran) => Board | Why;
|
|
14
|
+
export declare const sealedOf: (home: string) => string;
|
|
15
|
+
export declare const sealedKey: (home: string) => string | null;
|
|
16
|
+
export declare const laySealed: (home: string, sealed: string) => void;
|
|
17
|
+
export declare const hardwarePrice: () => string;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { unhex } from '@nervur.org/genome/standard';
|
|
5
|
+
import { nodeFiles } from './boot-move.js';
|
|
6
|
+
import { ranHere, } from './keyring.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const SEAL = String.raw`import CryptoKit
|
|
11
|
+
import Foundation
|
|
12
|
+
let lines = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!.split(separator: "\n").map(String.init)
|
|
13
|
+
func fail(_ why: String) -> Never { FileHandle.standardError.write(Data(why.utf8)); exit(1) }
|
|
14
|
+
guard SecureEnclave.isAvailable else { fail("no Secure Enclave") }
|
|
15
|
+
func derived(_ s: SharedSecret) -> SymmetricKey { s.hkdfDerivedSymmetricKey(using: SHA256.self, salt: Data(), sharedInfo: Data("nervur boot key".utf8), outputByteCount: 32) }
|
|
16
|
+
func bytes(_ b: String) -> Data { guard let d = Data(base64Encoded: b) else { fail("not base64") }; return d }
|
|
17
|
+
do {
|
|
18
|
+
switch lines[0] {
|
|
19
|
+
case "seal":
|
|
20
|
+
let enclave = try SecureEnclave.P256.KeyAgreement.PrivateKey()
|
|
21
|
+
let ephemeral = P256.KeyAgreement.PrivateKey()
|
|
22
|
+
let box = try AES.GCM.seal(bytes(lines[1]), using: derived(try ephemeral.sharedSecretFromKeyAgreement(with: enclave.publicKey)))
|
|
23
|
+
print(enclave.dataRepresentation.base64EncodedString(), ephemeral.publicKey.rawRepresentation.base64EncodedString(), box.combined!.base64EncodedString())
|
|
24
|
+
default:
|
|
25
|
+
let enclave = try SecureEnclave.P256.KeyAgreement.PrivateKey(dataRepresentation: bytes(lines[1]))
|
|
26
|
+
let key = try AES.GCM.open(AES.GCM.SealedBox(combined: bytes(lines[3])), using: derived(try enclave.sharedSecretFromKeyAgreement(with: try P256.KeyAgreement.PublicKey(rawRepresentation: bytes(lines[2])))))
|
|
27
|
+
print(key.base64EncodedString())
|
|
28
|
+
}
|
|
29
|
+
} catch { fail("\(error)") }`;
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const b64 = (bytes ) => Buffer.from(bytes).toString('base64');
|
|
37
|
+
|
|
38
|
+
export const boardOf = (platform , ran = ranHere) => {
|
|
39
|
+
if (platform !== 'darwin')
|
|
40
|
+
return {
|
|
41
|
+
why: `no hardware the installer can drive on ${platform}: a TPM is not built, and a Mac's Secure Enclave stands only on a Mac; choose another --boot`,
|
|
42
|
+
};
|
|
43
|
+
const swift = (input ) => ran('/usr/bin/swift', ['-e', SEAL], input);
|
|
44
|
+
return {
|
|
45
|
+
seal: async (key) => {
|
|
46
|
+
const done = await swift(`seal\n${b64(key)}\n`);
|
|
47
|
+
if ('why' in done) return done;
|
|
48
|
+
return done.code === 0
|
|
49
|
+
? { sealed: done.out.trim() }
|
|
50
|
+
: { why: `the Secure Enclave did not seal: ${done.err.trim()}` };
|
|
51
|
+
},
|
|
52
|
+
open: async (sealed) => {
|
|
53
|
+
const done = await swift(`open\n${sealed.split(' ').join('\n')}\n`);
|
|
54
|
+
if ('why' in done) return done;
|
|
55
|
+
if (done.code !== 0) return { why: `the Secure Enclave did not open: ${done.err.trim()}` };
|
|
56
|
+
const read = unhex(Buffer.from(done.out.trim(), 'base64').toString('hex'));
|
|
57
|
+
return 'why' in read ? read : { key: read.bytes };
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const sealedOf = (home ) => join(home, 'hardware.sealed');
|
|
63
|
+
|
|
64
|
+
export const sealedKey = (home ) =>
|
|
65
|
+
existsSync(sealedOf(home)) ? readFileSync(sealedOf(home), 'utf8').trim() : null;
|
|
66
|
+
|
|
67
|
+
export const laySealed = (home , sealed ) => nodeFiles.lay(sealedOf(home), `${sealed}\n`);
|
|
68
|
+
|
|
69
|
+
export const hardwarePrice = () =>
|
|
70
|
+
`The boot key is sealed to this Mac's Secure Enclave and opens only on this board: it needs Xcode's swift at every boot, opens when the Mac is unlocked, and dies with the board, so keep a backup named to a key of yours. The Enclave measures nothing, so it does not enforce a fingerprint.`;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { UnlockerSeam } from '@nervur.org/genome/seams';
|
|
2
|
+
import type { Laid } from '../organism/folder.js';
|
|
3
|
+
import { type Board } from './hardware.js';
|
|
4
|
+
import { type Keyring } from './keyring.js';
|
|
5
|
+
import { type Keepers } from './keepers.js';
|
|
6
|
+
type Why = {
|
|
7
|
+
readonly why: string;
|
|
8
|
+
};
|
|
9
|
+
export type Hands = {
|
|
10
|
+
readonly keyring: Keyring | Why;
|
|
11
|
+
readonly board: Board | Why;
|
|
12
|
+
readonly keepers: (home: string, span: number) => Keepers;
|
|
13
|
+
};
|
|
14
|
+
export declare const hostHands: (platform?: NodeJS.Platform) => Hands;
|
|
15
|
+
export declare const terminalOf: (laid: Laid) => Exclude<Laid['bootMode'], 'remote'>;
|
|
16
|
+
export declare const humanEnds: (laid: Laid) => boolean;
|
|
17
|
+
export declare const fingerprintHere: (laid: Laid, home: string) => string;
|
|
18
|
+
export type Custody = {
|
|
19
|
+
open(): Promise<{
|
|
20
|
+
readonly key: Uint8Array;
|
|
21
|
+
} | null | Why>;
|
|
22
|
+
keep(next: Uint8Array): Promise<null | Why>;
|
|
23
|
+
};
|
|
24
|
+
export declare const endCustodyOf: (home: string, laid: Laid, hands: Hands) => Custody;
|
|
25
|
+
export declare const custodyOf: (home: string, laid: Laid, hands: Hands) => Custody;
|
|
26
|
+
export declare const admitHere: (home: string, laid: Laid, hands: Hands) => Promise<null | Why>;
|
|
27
|
+
export declare const lastFingerprintOf: (home: string) => string | null;
|
|
28
|
+
export declare const layFingerprint: (home: string, fingerprint: string) => void;
|
|
29
|
+
export declare const priceOf: (home: string, laid: Laid) => string;
|
|
30
|
+
export declare const heldUnlocker: (home: string, laid: Laid, hands: Hands, refused: (why: string) => void) => UnlockerSeam;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
import { hex } from '@nervur.org/genome/standard';
|
|
6
|
+
|
|
7
|
+
import { bootKeyFileOf, readBootKey } from './boot-key.js';
|
|
8
|
+
import { keyText, movedBoot, nextKeyOf, nodeFiles } from './boot-move.js';
|
|
9
|
+
import { boardOf, hardwarePrice, laySealed, sealedKey, } from './hardware.js';
|
|
10
|
+
import { keyringOf, keyringPrice, } from './keyring.js';
|
|
11
|
+
import { KEEPER_SPAN, keepersAt, keepersPrice, } from './keepers.js';
|
|
12
|
+
import { configurationOf } from '../door/configuration.js';
|
|
13
|
+
import { digestsRunning } from '../door/digests.js';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export const hostHands = (platform = process.platform) => ({
|
|
24
|
+
keyring: keyringOf(platform),
|
|
25
|
+
board: boardOf(platform),
|
|
26
|
+
keepers: keepersAt,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const terminalOf = (laid ) =>
|
|
30
|
+
laid.bootMode === 'remote' ? laid.end : laid.bootMode;
|
|
31
|
+
|
|
32
|
+
export const humanEnds = (laid ) => terminalOf(laid) === 'present';
|
|
33
|
+
|
|
34
|
+
const nameOf = (home ) => `organism:${home}`;
|
|
35
|
+
|
|
36
|
+
export const fingerprintHere = (laid , home ) =>
|
|
37
|
+
createHash('sha256')
|
|
38
|
+
.update(JSON.stringify({ configuration: configurationOf(laid, home), ...digestsRunning() }))
|
|
39
|
+
.digest('hex');
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
const keyringCustody = (home , keyring ) => ({
|
|
47
|
+
open: async () => ('why' in keyring ? keyring : keyring.kept(nameOf(home))),
|
|
48
|
+
keep: async (next) => ('why' in keyring ? keyring : keyring.keep(nameOf(home), next)),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const boardCustody = (home , board ) => ({
|
|
52
|
+
open: async () => {
|
|
53
|
+
if ('why' in board) return board;
|
|
54
|
+
const sealed = sealedKey(home);
|
|
55
|
+
return sealed === null ? { why: `no sealed key stands in ${home}` } : board.open(sealed);
|
|
56
|
+
},
|
|
57
|
+
keep: async (next) => {
|
|
58
|
+
if ('why' in board) return board;
|
|
59
|
+
const sealed = await board.seal(next);
|
|
60
|
+
if ('why' in sealed) return sealed;
|
|
61
|
+
laySealed(home, sealed.sealed);
|
|
62
|
+
return null;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const fileCustody = (home ) => ({
|
|
67
|
+
open: () => Promise.resolve(existsSync(bootKeyFileOf(home)) ? readBootKey(home) : null),
|
|
68
|
+
keep: (next) => {
|
|
69
|
+
nodeFiles.lay(bootKeyFileOf(home), keyText(next));
|
|
70
|
+
return Promise.resolve(null);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const humanCustody = { open: () => Promise.resolve(null), keep: () => Promise.resolve(null) };
|
|
75
|
+
|
|
76
|
+
const terminalCustody = (home , mode , hands ) => {
|
|
77
|
+
if (mode === 'keyring') return keyringCustody(home, hands.keyring);
|
|
78
|
+
if (mode === 'hardware') return boardCustody(home, hands.board);
|
|
79
|
+
if (mode === 'file') return fileCustody(home);
|
|
80
|
+
return humanCustody;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const endCustodyOf = (home , laid , hands ) =>
|
|
84
|
+
terminalCustody(home, terminalOf(laid), hands);
|
|
85
|
+
|
|
86
|
+
export const custodyOf = (home , laid , hands ) => {
|
|
87
|
+
const end = terminalCustody(home, terminalOf(laid), hands);
|
|
88
|
+
if (laid.bootMode !== 'remote') return end;
|
|
89
|
+
const keepers = hands.keepers(home, laid.keeperSpan ?? KEEPER_SPAN);
|
|
90
|
+
const fingerprint = () => fingerprintHere(laid, home);
|
|
91
|
+
return {
|
|
92
|
+
open: async () => {
|
|
93
|
+
const heard = await keepers.opened(fingerprint());
|
|
94
|
+
if (!('why' in heard)) return heard;
|
|
95
|
+
const ended = await end.open();
|
|
96
|
+
return ended === null || 'key' in ended ? ended : { why: `${heard.why}; and ${ended.why}` };
|
|
97
|
+
},
|
|
98
|
+
keep: async (next) => {
|
|
99
|
+
const old = await keepers.opened(fingerprint());
|
|
100
|
+
if ('why' in old) {
|
|
101
|
+
const ended = await end.open();
|
|
102
|
+
if (ended === null || 'why' in ended) return { why: `the keepers cannot be told the new key: ${old.why}` };
|
|
103
|
+
const moved = await keepers.rewrapped(ended.key, next);
|
|
104
|
+
return moved ?? end.keep(next);
|
|
105
|
+
}
|
|
106
|
+
if (hex(old.key) !== hex(next)) {
|
|
107
|
+
const moved = await keepers.rewrapped(old.key, next);
|
|
108
|
+
if (moved !== null) return moved;
|
|
109
|
+
}
|
|
110
|
+
return end.keep(next);
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const admitHere = async (home , laid , hands ) => {
|
|
116
|
+
if (laid.bootMode !== 'remote') return null;
|
|
117
|
+
const keepers = hands.keepers(home, laid.keeperSpan ?? KEEPER_SPAN);
|
|
118
|
+
const stored = lastFingerprintOf(home);
|
|
119
|
+
const fingerprint = fingerprintHere(laid, home);
|
|
120
|
+
if (stored === fingerprint) return null;
|
|
121
|
+
const opened = await keepers.opened(stored ?? fingerprint);
|
|
122
|
+
if ('why' in opened) return opened;
|
|
123
|
+
const moved = await keepers.admitted(fingerprint, opened.key);
|
|
124
|
+
if (moved !== null) return moved;
|
|
125
|
+
layFingerprint(home, fingerprint);
|
|
126
|
+
return null;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const fingerprintFile = (home ) => `${home}/keepers.fingerprint`;
|
|
130
|
+
export const lastFingerprintOf = (home ) =>
|
|
131
|
+
existsSync(fingerprintFile(home)) ? readFileSync(fingerprintFile(home), 'utf8').trim() : null;
|
|
132
|
+
export const layFingerprint = (home , fingerprint ) => {
|
|
133
|
+
nodeFiles.lay(fingerprintFile(home), `${fingerprint}\n`);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const priceOf = (home , laid ) => {
|
|
137
|
+
const end = terminalOf(laid);
|
|
138
|
+
const ending = {
|
|
139
|
+
keyring: keyringPrice(process.platform),
|
|
140
|
+
hardware: hardwarePrice(),
|
|
141
|
+
file: `The boot key stands in ${bootKeyFileOf(home)}, read by your account alone: whoever reads that file owns the organism.`,
|
|
142
|
+
present:
|
|
143
|
+
'The boot key is your passphrase, stretched by its salt: no one at this device can start the organism without it.',
|
|
144
|
+
}[end];
|
|
145
|
+
return laid.bootMode === 'remote' ? `${keepersPrice}\n${ending}` : ending;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export const heldUnlocker = (home , laid , hands , refused ) => {
|
|
149
|
+
const custody = custodyOf(home, laid, hands);
|
|
150
|
+
let held = null;
|
|
151
|
+
return {
|
|
152
|
+
open: async () => {
|
|
153
|
+
const opened = await custody.open();
|
|
154
|
+
if (opened === null) return null;
|
|
155
|
+
if ('why' in opened) {
|
|
156
|
+
refused(opened.why);
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
held = opened.key;
|
|
160
|
+
return { key: opened.key };
|
|
161
|
+
},
|
|
162
|
+
admit: async (fingerprint) => {
|
|
163
|
+
if (laid.bootMode !== 'remote' || held === null) return null;
|
|
164
|
+
return hands.keepers(home, laid.keeperSpan ?? KEEPER_SPAN).admitted(hex(fingerprint), held);
|
|
165
|
+
},
|
|
166
|
+
rewrap: async (next, change) => {
|
|
167
|
+
if (nodeFiles.stands(nextKeyOf(home))) return { why: `a new boot key stands at ${nextKeyOf(home)} already` };
|
|
168
|
+
return movedBoot(nodeFiles, home, { key: next, mode: 'present', keep: (key) => custody.keep(key) }, change);
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type Why = {
|
|
2
|
+
readonly why: string;
|
|
3
|
+
};
|
|
4
|
+
export declare const KEEPER_SPAN = 10000;
|
|
5
|
+
export type Reach = {
|
|
6
|
+
send: (address: string, bytes: Uint8Array, wait: number, ward: string) => Promise<{
|
|
7
|
+
readonly reply: Uint8Array | null;
|
|
8
|
+
} | Why>;
|
|
9
|
+
close: () => void;
|
|
10
|
+
};
|
|
11
|
+
export declare const keepersOf: (home: string) => string;
|
|
12
|
+
export type Keepers = {
|
|
13
|
+
added(invitation: string, key: Uint8Array, fingerprint: string): Promise<null | Why>;
|
|
14
|
+
opened(fingerprint: string): Promise<{
|
|
15
|
+
readonly key: Uint8Array;
|
|
16
|
+
} | Why>;
|
|
17
|
+
admitted(fingerprint: string, key: Uint8Array): Promise<null | Why>;
|
|
18
|
+
rewrapped(old: Uint8Array, next: Uint8Array): Promise<null | Why>;
|
|
19
|
+
readonly count: () => number;
|
|
20
|
+
};
|
|
21
|
+
export declare const keepersAt: (home: string, span?: number, reach?: Reach) => Keepers;
|
|
22
|
+
export declare const keepersPrice = "The boot key is kept by your keepers: each answers this device over Quo at a boot, so a hub waits only while every keeper is out, and the mode after them takes over. A copy of keepers.json asks as this device does until your keeper ends its relation, and the keeper checks the fingerprint the device reports, which stops a changed device, not a thief.";
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { nobleCrypto, read as readQuo, readInvitation, seal, standingOn, } from '@nervur.org/genome/quo';
|
|
6
|
+
import { dataText, hex, isFields, readText, unhex, } from '@nervur.org/genome/standard';
|
|
7
|
+
import { nodeFiles } from './boot-move.js';
|
|
8
|
+
import { sendOf, dialingOf, } from '../tcp/dial.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const crypto = nobleCrypto;
|
|
13
|
+
const SCHEMES = ['tcp'];
|
|
14
|
+
const draw = (n ) => new Uint8Array(randomBytes(n));
|
|
15
|
+
|
|
16
|
+
export const KEEPER_SPAN = 10_000;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const reached = (dialing = dialingOf()) => ({
|
|
29
|
+
send: sendOf(dialing),
|
|
30
|
+
close: () => {
|
|
31
|
+
for (const line of dialing.lines.values()) line.socket.destroy();
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const keepersOf = (home ) => join(home, 'keepers.json');
|
|
36
|
+
|
|
37
|
+
const isStanding = (one ) =>
|
|
38
|
+
typeof one === 'object' && one !== null && 'invitation' in one && 'phase' in one;
|
|
39
|
+
const readStore = (home ) => {
|
|
40
|
+
if (!existsSync(keepersOf(home))) return [];
|
|
41
|
+
const read = JSON.parse(readFileSync(keepersOf(home), 'utf8'));
|
|
42
|
+
return Array.isArray(read) ? read.filter(isStanding) : [];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const layStore = (home , standings ) => {
|
|
46
|
+
nodeFiles.lay(`${keepersOf(home)}.next`, `${JSON.stringify(standings, null, 2)}\n`);
|
|
47
|
+
nodeFiles.rename(`${keepersOf(home)}.next`, keepersOf(home));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const asked = async (standing , method , args , span , reach ) => {
|
|
51
|
+
const sealed = seal(crypto, draw, standing, { method, args: dataText(args) });
|
|
52
|
+
if ('why' in sealed) return { why: sealed.why, standing };
|
|
53
|
+
const [address] = (standing.invitation.at ?? []).filter((one) => one.startsWith('tcp:'));
|
|
54
|
+
if (address === undefined) return { why: 'this keeper is reached at no tcp address', standing };
|
|
55
|
+
const sent = await reach.send(address, sealed.box, span, standing.invitation.ward);
|
|
56
|
+
if ('why' in sent) return { why: sent.why, standing: sealed.standing };
|
|
57
|
+
if (sent.reply === null) return { why: 'the keeper said nothing', standing: sealed.standing };
|
|
58
|
+
const heard = readQuo(crypto, sealed.standing, sealed.pending, sent.reply, SCHEMES);
|
|
59
|
+
if (!('object' in heard.read))
|
|
60
|
+
return { why: `the keeper answered ${JSON.stringify(heard.read)}`, standing: heard.standing };
|
|
61
|
+
const answer = readText(heard.read.object);
|
|
62
|
+
if ('why' in answer || !isFields(answer.data) || !('result' in answer.data)) {
|
|
63
|
+
const message = 'why' in answer ? answer.why : JSON.stringify(answer.data);
|
|
64
|
+
return { why: `the keeper refused: ${message}`, standing: heard.standing };
|
|
65
|
+
}
|
|
66
|
+
return { result: answer.data['result'], standing: heard.standing };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
const firstRefusal = (answers ) => {
|
|
84
|
+
const failed = answers.find((one) => 'why' in one);
|
|
85
|
+
return failed !== undefined && 'why' in failed ? { why: failed.why } : null;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const firstOpen = async (
|
|
89
|
+
home ,
|
|
90
|
+
fingerprint ,
|
|
91
|
+
span ,
|
|
92
|
+
reach ,
|
|
93
|
+
) => {
|
|
94
|
+
const standings = readStore(home);
|
|
95
|
+
let why = { why: 'no keeper is laid' };
|
|
96
|
+
for (const [at, standing] of standings.entries()) {
|
|
97
|
+
const answer = await asked(standing, 'open', { fingerprint }, span, reach);
|
|
98
|
+
standings[at] = answer.standing;
|
|
99
|
+
layStore(home, standings);
|
|
100
|
+
if ('why' in answer) {
|
|
101
|
+
why = { why: answer.why };
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const key = typeof answer.result === 'string' ? unhex(answer.result) : null;
|
|
105
|
+
reach.close();
|
|
106
|
+
if (key === null || 'why' in key || key.bytes.length !== 32) return { why: 'a keeper answered no boot key' };
|
|
107
|
+
return { key: key.bytes };
|
|
108
|
+
}
|
|
109
|
+
reach.close();
|
|
110
|
+
return why;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const keepersAt = (home , span = KEEPER_SPAN, reach = reached()) => {
|
|
114
|
+
const each = async (
|
|
115
|
+
step ,
|
|
116
|
+
) => {
|
|
117
|
+
const standings = readStore(home);
|
|
118
|
+
const answers = [];
|
|
119
|
+
for (const [at, standing] of standings.entries()) {
|
|
120
|
+
const answer = await step(standing);
|
|
121
|
+
standings[at] = answer.standing;
|
|
122
|
+
layStore(home, standings);
|
|
123
|
+
answers.push(answer);
|
|
124
|
+
}
|
|
125
|
+
reach.close();
|
|
126
|
+
return answers;
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
added: async (text, key, fingerprint) => {
|
|
130
|
+
const invitation = readInvitation(text.trim(), SCHEMES);
|
|
131
|
+
if (invitation === null) return { why: 'the keeper invitation is no invitation reached over tcp' };
|
|
132
|
+
const standing = standingOn(invitation);
|
|
133
|
+
const answer = await asked(standing, 'keep', { key: hex(key), fingerprint }, span, reach);
|
|
134
|
+
reach.close();
|
|
135
|
+
if ('why' in answer) return { why: answer.why };
|
|
136
|
+
layStore(home, [...readStore(home), answer.standing]);
|
|
137
|
+
return null;
|
|
138
|
+
},
|
|
139
|
+
opened: (fingerprint) => firstOpen(home, fingerprint, span, reach),
|
|
140
|
+
admitted: async (fingerprint, key) =>
|
|
141
|
+
firstRefusal(await each((standing) => asked(standing, 'admit', { fingerprint, key: hex(key) }, span, reach))),
|
|
142
|
+
rewrapped: async (old, next) =>
|
|
143
|
+
firstRefusal(
|
|
144
|
+
await each((standing) => asked(standing, 'rewrap', { key: hex(old), next: hex(next) }, span, reach)),
|
|
145
|
+
),
|
|
146
|
+
count: () => readStore(home).length,
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export const keepersPrice =
|
|
151
|
+
'The boot key is kept by your keepers: each answers this device over Quo at a boot, so a hub waits only while every keeper is out, and the mode after them takes over. A copy of keepers.json asks as this device does until your keeper ends its relation, and the keeper checks the fingerprint the device reports, which stops a changed device, not a thief.';
|