nervur 0.19.2 → 0.20.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 +66 -101
- package/dist/being/being.d.ts +7 -15
- package/dist/being/being.js +16 -67
- package/dist/being/digest.d.ts +1 -1
- package/dist/being/digest.js +7 -27
- package/dist/being/faculty.d.ts +9 -0
- package/dist/being/faculty.js +68 -0
- package/dist/being/index.d.ts +4 -5
- package/dist/being/index.js +5 -5
- package/dist/being/types.d.ts +35 -62
- package/dist/being/types.js +3 -57
- package/dist/being/words.d.ts +13 -0
- package/dist/being/words.js +14 -0
- package/dist/contract/index.d.ts +26 -0
- package/dist/contract/index.js +19 -0
- package/dist/crypto/aes.d.ts +3 -0
- package/dist/crypto/aes.js +24 -0
- package/dist/crypto/bytes.d.ts +6 -0
- package/dist/crypto/bytes.js +33 -0
- package/dist/crypto/ed25519.d.ts +3 -0
- package/dist/crypto/ed25519.js +85 -0
- package/dist/crypto/hash.d.ts +2 -0
- package/dist/crypto/hash.js +11 -0
- package/dist/crypto/index.d.ts +7 -0
- package/dist/crypto/index.js +10 -0
- package/dist/crypto/json.d.ts +8 -0
- package/dist/crypto/json.js +250 -0
- package/dist/crypto/mlkem.d.ts +12 -0
- package/dist/crypto/mlkem.js +36 -0
- package/dist/crypto/subtle.d.ts +10 -0
- package/dist/crypto/subtle.js +29 -0
- package/dist/crypto/x25519.d.ts +2 -0
- package/dist/crypto/x25519.js +21 -0
- package/dist/folder/index.d.ts +13 -0
- package/dist/folder/index.js +125 -0
- package/dist/harbor/dock.d.ts +19 -0
- package/dist/harbor/dock.js +92 -0
- package/dist/harbor/harbor.d.ts +22 -0
- package/dist/harbor/harbor.js +84 -0
- package/dist/harbor/index.d.ts +4 -10
- package/dist/harbor/index.js +5 -18
- package/dist/harbor/registry.d.ts +7 -0
- package/dist/harbor/registry.js +13 -0
- package/dist/harbor/terrain.d.ts +8 -0
- package/dist/harbor/terrain.js +2 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +14 -0
- package/dist/pointer/bodies.d.ts +20 -0
- package/dist/pointer/bodies.js +45 -0
- package/dist/pointer/index.d.ts +2 -0
- package/dist/pointer/index.js +4 -0
- package/dist/pointer/world.d.ts +34 -0
- package/dist/pointer/world.js +76 -0
- package/dist/quo/door.d.ts +34 -0
- package/dist/quo/door.js +172 -0
- package/dist/quo/index.d.ts +8 -0
- package/dist/quo/index.js +11 -0
- package/dist/quo/invitation.d.ts +7 -0
- package/dist/quo/invitation.js +15 -0
- package/dist/quo/keys.d.ts +40 -0
- package/dist/quo/keys.js +79 -0
- package/dist/quo/payload.d.ts +15 -0
- package/dist/quo/payload.js +55 -0
- package/dist/quo/relations.d.ts +40 -0
- package/dist/quo/relations.js +33 -0
- package/dist/quo/reply.d.ts +13 -0
- package/dist/quo/reply.js +35 -0
- package/dist/quo/seal.d.ts +42 -0
- package/dist/quo/seal.js +78 -0
- package/dist/quo/standing.d.ts +39 -0
- package/dist/quo/standing.js +90 -0
- package/dist/ward/allowance.d.ts +13 -9
- package/dist/ward/allowance.js +28 -67
- package/dist/ward/cells.d.ts +6 -6
- package/dist/ward/cells.js +67 -179
- package/dist/ward/index.d.ts +5 -10
- package/dist/ward/index.js +6 -13
- package/dist/ward/partition.d.ts +44 -60
- package/dist/ward/partition.js +56 -285
- package/dist/ward/stance.d.ts +29 -20
- package/dist/ward/stance.js +173 -406
- package/dist/ward/ward-being.d.ts +33 -0
- package/dist/ward/ward-being.js +76 -0
- package/dist/ward/ward.d.ts +43 -11
- package/dist/ward/ward.js +215 -363
- package/package.json +19 -33
- package/src/being/being.ts +31 -78
- package/src/being/digest.ts +7 -35
- package/src/being/faculty.ts +66 -0
- package/src/being/index.ts +5 -6
- package/src/being/types.ts +47 -145
- package/src/being/words.ts +31 -0
- package/src/contract/index.ts +44 -0
- package/src/crypto/aes.ts +26 -0
- package/src/crypto/bytes.ts +37 -0
- package/src/crypto/ed25519.ts +84 -0
- package/src/crypto/hash.ts +14 -0
- package/src/crypto/index.ts +10 -0
- package/src/crypto/json.ts +241 -0
- package/src/crypto/mlkem.ts +38 -0
- package/src/crypto/subtle.ts +33 -0
- package/src/crypto/x25519.ts +21 -0
- package/src/folder/index.ts +134 -0
- package/src/harbor/dock.ts +101 -0
- package/src/harbor/harbor.ts +105 -0
- package/src/harbor/index.ts +5 -19
- package/src/harbor/registry.ts +20 -0
- package/src/harbor/terrain.ts +13 -0
- package/src/index.ts +20 -0
- package/src/pointer/bodies.ts +47 -0
- package/src/pointer/index.ts +4 -0
- package/src/pointer/world.ts +91 -0
- package/src/quo/door.ts +178 -0
- package/src/quo/index.ts +11 -0
- package/src/quo/invitation.ts +15 -0
- package/src/quo/keys.ts +91 -0
- package/src/quo/payload.ts +61 -0
- package/src/quo/relations.ts +62 -0
- package/src/quo/reply.ts +38 -0
- package/src/quo/seal.ts +101 -0
- package/src/quo/standing.ts +111 -0
- package/src/stand/main.ts +19 -0
- package/src/stand/stand.ts +178 -0
- package/src/ward/allowance.ts +37 -75
- package/src/ward/cells.ts +63 -176
- package/src/ward/index.ts +6 -17
- package/src/ward/partition.ts +86 -326
- package/src/ward/stance.ts +185 -420
- package/src/ward/ward-being.ts +97 -0
- package/src/ward/ward.ts +229 -363
- package/dist/being/lent.d.ts +0 -32
- package/dist/being/lent.js +0 -72
- package/dist/being/silence.d.ts +0 -12
- package/dist/being/silence.js +0 -41
- package/dist/conformance/assert.d.ts +0 -11
- package/dist/conformance/assert.js +0 -106
- package/dist/conformance/beings.d.ts +0 -199
- package/dist/conformance/beings.js +0 -188
- package/dist/conformance/estate.d.ts +0 -5
- package/dist/conformance/estate.js +0 -388
- package/dist/conformance/index.d.ts +0 -80
- package/dist/conformance/index.js +0 -819
- package/dist/conformance/reach.d.ts +0 -10
- package/dist/conformance/reach.js +0 -72
- package/dist/conformance/store.d.ts +0 -5
- package/dist/conformance/store.js +0 -113
- package/dist/harbor/box.d.ts +0 -121
- package/dist/harbor/box.js +0 -121
- package/dist/harbor/core.d.ts +0 -55
- package/dist/harbor/core.js +0 -662
- package/dist/harbor/dial.d.ts +0 -9
- package/dist/harbor/dial.js +0 -81
- package/dist/harbor/memory.d.ts +0 -29
- package/dist/harbor/memory.js +0 -104
- package/dist/harbor/reach.d.ts +0 -36
- package/dist/harbor/reach.js +0 -199
- package/dist/harbor/store.d.ts +0 -35
- package/dist/harbor/store.js +0 -62
- package/dist/vector/cases.d.ts +0 -42
- package/dist/vector/cases.js +0 -223
- package/dist/vector/index.d.ts +0 -6
- package/dist/vector/index.js +0 -8
- package/dist/vector/stand.d.ts +0 -9
- package/dist/vector/stand.js +0 -77
- package/dist/vector/world.d.ts +0 -144
- package/dist/vector/world.js +0 -209
- package/dist/ward/arithmetic.d.ts +0 -31
- package/dist/ward/arithmetic.js +0 -249
- package/dist/ward/door.d.ts +0 -18
- package/dist/ward/door.js +0 -186
- package/dist/ward/ground.d.ts +0 -29
- package/dist/ward/ground.js +0 -56
- package/dist/ward/heirs.d.ts +0 -13
- package/dist/ward/heirs.js +0 -117
- package/dist/ward/json.d.ts +0 -2
- package/dist/ward/json.js +0 -163
- package/dist/ward/owner.d.ts +0 -13
- package/dist/ward/owner.js +0 -220
- package/dist/ward/seal.d.ts +0 -52
- package/dist/ward/seal.js +0 -150
- package/src/being/lent.ts +0 -72
- package/src/being/silence.ts +0 -46
- package/src/conformance/assert.ts +0 -100
- package/src/conformance/beings.ts +0 -188
- package/src/conformance/estate.ts +0 -412
- package/src/conformance/index.ts +0 -965
- package/src/conformance/reach.ts +0 -83
- package/src/conformance/store.ts +0 -125
- package/src/harbor/box.ts +0 -131
- package/src/harbor/core.ts +0 -699
- package/src/harbor/dial.ts +0 -112
- package/src/harbor/memory.ts +0 -123
- package/src/harbor/reach.ts +0 -221
- package/src/harbor/store.ts +0 -91
- package/src/vector/cases.ts +0 -257
- package/src/vector/index.ts +0 -11
- package/src/vector/stand.ts +0 -76
- package/src/vector/world.ts +0 -232
- package/src/ward/arithmetic.ts +0 -251
- package/src/ward/door.ts +0 -186
- package/src/ward/ground.ts +0 -142
- package/src/ward/heirs.ts +0 -116
- package/src/ward/json.ts +0 -144
- package/src/ward/owner.ts +0 -214
- package/src/ward/seal.ts +0 -178
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Carrier } from '../contract/index.js';
|
|
2
|
+
import { Harbor, Terrain } from '../harbor/index.js';
|
|
3
|
+
import { CryptoEntropy, HeldCustody, SystemClock, VolatileMemory } from './bodies.js';
|
|
4
|
+
// A carrier that reaches no one: a harbor alone.
|
|
5
|
+
export class NoCarrier extends Carrier {
|
|
6
|
+
carry() {
|
|
7
|
+
return Promise.resolve(null);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
// Bytes to whichever harbor of a world holds the ward pk.
|
|
11
|
+
export class PointerCarrier extends Carrier {
|
|
12
|
+
#world;
|
|
13
|
+
constructor(world) {
|
|
14
|
+
super();
|
|
15
|
+
this.#world = world;
|
|
16
|
+
}
|
|
17
|
+
carry(pk, bytes) {
|
|
18
|
+
return this.#world.carry(pk, bytes);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class PointerTerrain extends Terrain {
|
|
22
|
+
entropy;
|
|
23
|
+
clock;
|
|
24
|
+
custody;
|
|
25
|
+
memory;
|
|
26
|
+
carrier;
|
|
27
|
+
constructor(parts = {}) {
|
|
28
|
+
super();
|
|
29
|
+
this.entropy = parts.entropy ?? new CryptoEntropy();
|
|
30
|
+
this.clock = parts.clock ?? new SystemClock();
|
|
31
|
+
this.custody = parts.custody ?? new HeldCustody(this.entropy);
|
|
32
|
+
this.memory = parts.memory ?? new VolatileMemory();
|
|
33
|
+
this.carrier = parts.carrier ?? new NoCarrier();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export class World {
|
|
37
|
+
#places = new Map();
|
|
38
|
+
// Harbors the world reaches no one in, nor anyone out of.
|
|
39
|
+
cut = new Set();
|
|
40
|
+
// A harbor of this world. `parts` replaces the pointer bodies, a folder's
|
|
41
|
+
// memory and custody among them.
|
|
42
|
+
async harbor(name, classes, parts = {}) {
|
|
43
|
+
if (this.#places.has(name))
|
|
44
|
+
throw new Error(`a harbor named ${name} stands`);
|
|
45
|
+
const terrain = new PointerTerrain({ ...parts, carrier: new PointerCarrier(this) });
|
|
46
|
+
const harbor = await Harbor.open(terrain, classes);
|
|
47
|
+
this.#places.set(name, { harbor, terrain, classes });
|
|
48
|
+
return harbor;
|
|
49
|
+
}
|
|
50
|
+
get(name) {
|
|
51
|
+
const place = this.#places.get(name);
|
|
52
|
+
if (!place)
|
|
53
|
+
throw new Error(`no harbor named ${name}`);
|
|
54
|
+
return place.harbor;
|
|
55
|
+
}
|
|
56
|
+
// The harbor opened again on its own terrain, with the classes it was
|
|
57
|
+
// made with unless others are named. `parts` replaces bodies of the
|
|
58
|
+
// terrain, as a new process would build them over the same disk.
|
|
59
|
+
async restart(name, classes, parts) {
|
|
60
|
+
const place = this.#places.get(name);
|
|
61
|
+
if (!place)
|
|
62
|
+
throw new Error(`no harbor named ${name}`);
|
|
63
|
+
const t = place.terrain;
|
|
64
|
+
const terrain = parts ? new PointerTerrain({ entropy: t.entropy, clock: t.clock, custody: t.custody, memory: t.memory, ...parts, carrier: t.carrier }) : t;
|
|
65
|
+
const harbor = await Harbor.open(terrain, classes ?? place.classes);
|
|
66
|
+
this.#places.set(name, { ...place, harbor, terrain });
|
|
67
|
+
return harbor;
|
|
68
|
+
}
|
|
69
|
+
carry(pk, bytes) {
|
|
70
|
+
for (const [name, { harbor }] of this.#places) {
|
|
71
|
+
if (harbor.holds(pk))
|
|
72
|
+
return this.cut.has(name) ? Promise.resolve(null) : harbor.arrive(pk, bytes);
|
|
73
|
+
}
|
|
74
|
+
return Promise.resolve(null);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Draw, type WardKey } from './keys.ts';
|
|
2
|
+
import type { Invitation } from './invitation.ts';
|
|
3
|
+
import { type Payload } from './payload.ts';
|
|
4
|
+
import type { Relations } from './relations.ts';
|
|
5
|
+
export declare const SPAN = 64;
|
|
6
|
+
export type Choice = {
|
|
7
|
+
readonly object: string;
|
|
8
|
+
readonly seen: string | null;
|
|
9
|
+
} | {
|
|
10
|
+
readonly silence: true;
|
|
11
|
+
};
|
|
12
|
+
export interface Behind {
|
|
13
|
+
readonly zero: boolean;
|
|
14
|
+
answer(heir: string | null, payload: Payload): Promise<Choice>;
|
|
15
|
+
}
|
|
16
|
+
export type Judged = {
|
|
17
|
+
bytes: Uint8Array;
|
|
18
|
+
case: number;
|
|
19
|
+
heard: boolean;
|
|
20
|
+
};
|
|
21
|
+
export declare class Door {
|
|
22
|
+
#private;
|
|
23
|
+
readonly key: WardKey;
|
|
24
|
+
readonly relations: Relations;
|
|
25
|
+
readonly behind: Behind;
|
|
26
|
+
readonly draw: Draw;
|
|
27
|
+
constructor(key: WardKey, relations: Relations, behind: Behind, draw: Draw);
|
|
28
|
+
invite(): Promise<{
|
|
29
|
+
heir: string;
|
|
30
|
+
invitation: Invitation;
|
|
31
|
+
}>;
|
|
32
|
+
remove(heir: string): boolean;
|
|
33
|
+
arrive(bytes: Uint8Array): Promise<Judged>;
|
|
34
|
+
}
|
package/dist/quo/door.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The door: an ask's box in, a reply's box out, judged by the thirteen cases
|
|
3
|
+
// of the spec in their order. A stranger hears one silence whatever the
|
|
4
|
+
// case. The case is handed back beside the bytes, for the kit's own eyes,
|
|
5
|
+
// with `heard`: whether a key the door holds or keeps spoke.
|
|
6
|
+
import { decapsulate, hex, unhex, utf8, x25519Public, zero } from '../crypto/index.js';
|
|
7
|
+
import { drawLock, SigningKey } from './keys.js';
|
|
8
|
+
import { readPayload } from './payload.js';
|
|
9
|
+
import { writeReply } from './reply.js';
|
|
10
|
+
import { checkBody, follow, HEAD, knockEdge, openBody, openHead, replyKey, sealReply, signBody, SIZE, splitBody, splitKnock, ZERO_EDGE } from './seal.js';
|
|
11
|
+
// The count numbers below the highest a door still honours: those within
|
|
12
|
+
// this span that it has not seen.
|
|
13
|
+
export const SPAN = 64;
|
|
14
|
+
export class Door {
|
|
15
|
+
key;
|
|
16
|
+
relations;
|
|
17
|
+
behind;
|
|
18
|
+
draw;
|
|
19
|
+
constructor(key, relations, behind, draw) {
|
|
20
|
+
this.key = key;
|
|
21
|
+
this.relations = relations;
|
|
22
|
+
this.behind = behind;
|
|
23
|
+
this.draw = draw;
|
|
24
|
+
}
|
|
25
|
+
// A new heir, held fresh, and its invitation. The lock is drawn at the
|
|
26
|
+
// first invitation that names a heir, before the heir's secret.
|
|
27
|
+
async invite() {
|
|
28
|
+
let lock = this.relations.lock();
|
|
29
|
+
if (!lock)
|
|
30
|
+
this.relations.setLock((lock = drawLock(this.draw)));
|
|
31
|
+
const heir = await SigningKey.draw(this.draw);
|
|
32
|
+
this.relations.set(heir.id, { state: 'fresh' });
|
|
33
|
+
return { heir: heir.id, invitation: { ward: this.key.pk, heir: heir.id, secret: hex(heir.secret), lock: hex(lock.ek) } };
|
|
34
|
+
}
|
|
35
|
+
// The door stops holding a heir. A fresh one leaves nothing, a spent one
|
|
36
|
+
// leaves the keys kept at removal.
|
|
37
|
+
remove(heir) {
|
|
38
|
+
const record = this.relations.get(heir);
|
|
39
|
+
if (record === undefined || record.state === 'kept')
|
|
40
|
+
return false;
|
|
41
|
+
if (record.state === 'fresh')
|
|
42
|
+
this.relations.delete(heir);
|
|
43
|
+
else
|
|
44
|
+
this.relations.set(heir, { state: 'kept', held: record.held, vouched: record.vouched, open: record.open, offered: record.offered });
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
async arrive(bytes) {
|
|
48
|
+
const stranger = async (n) => ({ bytes: await this.#silence(bytes), case: n, heard: false });
|
|
49
|
+
if (bytes.length > SIZE || bytes.length < HEAD)
|
|
50
|
+
return stranger(1);
|
|
51
|
+
const agreement = await this.key.open(bytes.subarray(0, HEAD));
|
|
52
|
+
const opened = agreement && (await openHead(bytes, agreement));
|
|
53
|
+
if (!opened)
|
|
54
|
+
return stranger(1);
|
|
55
|
+
const isZero = zero(opened.head);
|
|
56
|
+
const heir = hex(opened.head);
|
|
57
|
+
const record = isZero ? undefined : this.relations.get(heir);
|
|
58
|
+
const body = await this.#body(opened, record);
|
|
59
|
+
if (!body)
|
|
60
|
+
return stranger(1);
|
|
61
|
+
const read = readPayload(body.text);
|
|
62
|
+
if ('fault' in read)
|
|
63
|
+
return stranger(read.fault === 'not an object' ? 2 : 3);
|
|
64
|
+
const p = read.payload;
|
|
65
|
+
if (isZero ? p.to !== null : p.to !== heir)
|
|
66
|
+
return stranger(3);
|
|
67
|
+
if (isZero) {
|
|
68
|
+
if (!this.behind.zero)
|
|
69
|
+
return stranger(4);
|
|
70
|
+
if (!(await checkBody(unhex(p.by), body)))
|
|
71
|
+
return stranger(5);
|
|
72
|
+
const choice = await this.#choose(null, p);
|
|
73
|
+
return { bytes: await this.#sealed(opened, choice), case: 'object' in choice ? 12 : 13, heard: false };
|
|
74
|
+
}
|
|
75
|
+
if (record === undefined)
|
|
76
|
+
return stranger(6);
|
|
77
|
+
if (!admits(record, heir, p.by))
|
|
78
|
+
return stranger(7);
|
|
79
|
+
if (!(await checkBody(unhex(p.by), body)))
|
|
80
|
+
return stranger(8);
|
|
81
|
+
const reply = await replyKey(this.draw, opened.lid);
|
|
82
|
+
if (!reply)
|
|
83
|
+
return stranger(1);
|
|
84
|
+
const offered = await follow(body.edge, reply.agreement);
|
|
85
|
+
// Checked again, since another arrival may have moved the keys while
|
|
86
|
+
// this one was checking its signature.
|
|
87
|
+
const now = this.relations.get(heir);
|
|
88
|
+
if (now === undefined || !admits(now, heir, p.by) || !opensUnder(now, body.edge))
|
|
89
|
+
return stranger(7);
|
|
90
|
+
if (now.state === 'kept')
|
|
91
|
+
return this.#heard(reply, { quo: 'removed' }, 9);
|
|
92
|
+
if (now.state === 'fresh' && (p.next === null || p.next === heir))
|
|
93
|
+
return this.#heard(reply, { quo: 'unannounced' }, 10);
|
|
94
|
+
if (now.state === 'spent' && !honours(now, p.seq))
|
|
95
|
+
return this.#heard(reply, { quo: 'repeated' }, 11);
|
|
96
|
+
this.relations.set(heir, move(now, heir, p, hex(body.edge), hex(offered)));
|
|
97
|
+
const choice = await this.#choose(heir, p);
|
|
98
|
+
const kept = await this.relations.kept().catch(() => false);
|
|
99
|
+
return this.#heard(reply, kept ? choice : { silence: true }, kept && 'object' in choice ? 12 : 13);
|
|
100
|
+
}
|
|
101
|
+
async #heard(key, reply, n) {
|
|
102
|
+
return { bytes: await sealReply(key, await this.#signed(reply)), case: n, heard: true };
|
|
103
|
+
}
|
|
104
|
+
#signed(reply) {
|
|
105
|
+
return signBody(utf8(writeReply(reply)), this.key.signing);
|
|
106
|
+
}
|
|
107
|
+
async #choose(heir, p) {
|
|
108
|
+
try {
|
|
109
|
+
return await this.behind.answer(heir, p);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return { silence: true };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async #sealed(opened, reply) {
|
|
116
|
+
const key = await replyKey(this.draw, opened.lid);
|
|
117
|
+
return key ? sealReply(key, await this.#signed(reply)) : this.#silence(opened.lid);
|
|
118
|
+
}
|
|
119
|
+
// The body, opened under the edge keys the door takes for this head.
|
|
120
|
+
async #body(opened, record) {
|
|
121
|
+
const tries = [];
|
|
122
|
+
if (record?.state === 'fresh') {
|
|
123
|
+
const knock = splitKnock(opened.rest);
|
|
124
|
+
const lock = this.relations.lock();
|
|
125
|
+
const shared = knock && lock ? decapsulate(lock.dk, knock.ciphertext) : null;
|
|
126
|
+
if (!knock || !shared)
|
|
127
|
+
return null;
|
|
128
|
+
tries.push({ edge: await knockEdge(shared), sealed: knock.sealedBody });
|
|
129
|
+
}
|
|
130
|
+
else if (record === undefined)
|
|
131
|
+
tries.push({ edge: ZERO_EDGE, sealed: opened.rest });
|
|
132
|
+
else
|
|
133
|
+
for (const edge of [record.open, record.offered])
|
|
134
|
+
tries.push({ edge: unhex(edge), sealed: opened.rest });
|
|
135
|
+
for (const { edge, sealed } of tries) {
|
|
136
|
+
const plain = await openBody(opened, edge, sealed);
|
|
137
|
+
if (plain === null)
|
|
138
|
+
continue;
|
|
139
|
+
const body = splitBody(plain);
|
|
140
|
+
return body && { edge, ...body };
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
// A stranger's silence: sealed to the first thirty-two bytes that arrived
|
|
145
|
+
// when they take a seal, and otherwise to a lid nobody holds.
|
|
146
|
+
async #silence(arrived) {
|
|
147
|
+
const text = await this.#signed({ silence: true });
|
|
148
|
+
const candidates = [arrived.length >= HEAD ? arrived.subarray(0, HEAD) : null, this.draw(HEAD)];
|
|
149
|
+
for (const lid of candidates) {
|
|
150
|
+
const key = lid && (await replyKey(this.draw, lid));
|
|
151
|
+
if (key)
|
|
152
|
+
return sealReply(key, text);
|
|
153
|
+
}
|
|
154
|
+
return sealReply((await replyKey(this.draw, await x25519Public(this.draw(HEAD)))), text);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const admits = (record, heir, by) => (record.state === 'fresh' ? by === heir : by === record.held || by === record.vouched);
|
|
158
|
+
const opensUnder = (record, edge) => record.state === 'fresh' || hex(edge) === record.open || hex(edge) === record.offered;
|
|
159
|
+
const honours = (record, seq) => seq > record.highest || (seq > record.highest - SPAN && seq !== record.highest && !record.honoured.includes(seq));
|
|
160
|
+
// The move tables: the keys and edge keys after a choice, and the count.
|
|
161
|
+
const move = (record, heir, p, edge, offered) => {
|
|
162
|
+
const announced = p.next !== null && p.next !== heir && p.next !== p.by ? p.next : null;
|
|
163
|
+
if (record.state === 'fresh')
|
|
164
|
+
return { state: 'spent', held: announced, vouched: null, open: edge, offered, highest: p.seq, honoured: [] };
|
|
165
|
+
if (record.state !== 'spent')
|
|
166
|
+
return record;
|
|
167
|
+
if (p.seq < record.highest)
|
|
168
|
+
return { ...record, honoured: [...record.honoured, p.seq] };
|
|
169
|
+
const keys = p.by === record.held ? { held: record.held, vouched: announced ?? record.vouched, open: edge, offered } : { held: p.by, vouched: announced, open: edge, offered };
|
|
170
|
+
const honoured = [...record.honoured, record.highest].filter((n) => n > p.seq - SPAN);
|
|
171
|
+
return { state: 'spent', ...keys, highest: p.seq, honoured };
|
|
172
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { Door, SPAN, type Behind, type Choice, type Judged } from './door.ts';
|
|
2
|
+
export { readInvitation, type Invitation } from './invitation.ts';
|
|
3
|
+
export { Ephemeral, SigningKey, WardKey, type Draw } from './keys.ts';
|
|
4
|
+
export { readPayload, writePayload, type Payload } from './payload.ts';
|
|
5
|
+
export { GONE, keptLock, lockOf, MemoryRelations, type HeirRecord, type KeptLock, type Relations } from './relations.ts';
|
|
6
|
+
export { readReply, SILENCE, WORDS, writeReply, type Reply, type Word } from './reply.ts';
|
|
7
|
+
export { SIZE } from './seal.ts';
|
|
8
|
+
export { freshStanding, Standing, type Read, type Sent, type StandingState } from './standing.ts';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Quo's wire: keys, the invitation, the payload, the reply, the boxes, the
|
|
3
|
+
// door and the standing. It knows no being, no ward and no terrain.
|
|
4
|
+
export { Door, SPAN } from './door.js';
|
|
5
|
+
export { readInvitation } from './invitation.js';
|
|
6
|
+
export { Ephemeral, SigningKey, WardKey } from './keys.js';
|
|
7
|
+
export { readPayload, writePayload } from './payload.js';
|
|
8
|
+
export { GONE, keptLock, lockOf, MemoryRelations } from './relations.js';
|
|
9
|
+
export { readReply, SILENCE, WORDS, writeReply } from './reply.js';
|
|
10
|
+
export { SIZE } from './seal.js';
|
|
11
|
+
export { freshStanding, Standing } from './standing.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The invitation: `{ ward, heir, secret, lock }`, a value with no route.
|
|
3
|
+
import { EK, encapsulate, isHex, unhex } from '../crypto/index.js';
|
|
4
|
+
// An invitation, or null for any other shape. A field beside the four is
|
|
5
|
+
// ignored, and the lock is held to the modulus check.
|
|
6
|
+
export const readInvitation = (value) => {
|
|
7
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
8
|
+
return null;
|
|
9
|
+
const { ward, heir, secret, lock } = value;
|
|
10
|
+
if (!isHex(ward, 64) || !isHex(heir, 32) || !isHex(secret, 32) || !isHex(lock, EK))
|
|
11
|
+
return null;
|
|
12
|
+
if (encapsulate(unhex(lock), new Uint8Array(32)) === null)
|
|
13
|
+
return null;
|
|
14
|
+
return { ward, heir, secret, lock };
|
|
15
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Lock } from '../crypto/index.ts';
|
|
2
|
+
export type Draw = (length: number) => Uint8Array;
|
|
3
|
+
export declare const LABEL: {
|
|
4
|
+
readonly seal: Uint8Array<ArrayBufferLike>;
|
|
5
|
+
readonly edgeSeal: Uint8Array<ArrayBufferLike>;
|
|
6
|
+
readonly lock: Uint8Array<ArrayBufferLike>;
|
|
7
|
+
readonly edge: Uint8Array<ArrayBufferLike>;
|
|
8
|
+
readonly wardSign: Uint8Array<ArrayBufferLike>;
|
|
9
|
+
readonly wardSeal: Uint8Array<ArrayBufferLike>;
|
|
10
|
+
};
|
|
11
|
+
export declare class SigningKey {
|
|
12
|
+
readonly secret: Uint8Array;
|
|
13
|
+
readonly pk: Uint8Array;
|
|
14
|
+
private constructor();
|
|
15
|
+
static from(secret: Uint8Array): Promise<SigningKey>;
|
|
16
|
+
static draw(draw: Draw): Promise<SigningKey>;
|
|
17
|
+
get id(): string;
|
|
18
|
+
}
|
|
19
|
+
export declare class Ephemeral {
|
|
20
|
+
readonly secret: Uint8Array;
|
|
21
|
+
readonly pk: Uint8Array;
|
|
22
|
+
private constructor();
|
|
23
|
+
static from(secret: Uint8Array): Promise<Ephemeral>;
|
|
24
|
+
static draw(draw: Draw): Promise<Ephemeral>;
|
|
25
|
+
agree(pk: Uint8Array): Promise<Uint8Array | null>;
|
|
26
|
+
}
|
|
27
|
+
export declare class WardKey {
|
|
28
|
+
readonly signing: SigningKey;
|
|
29
|
+
readonly sealSecret: Uint8Array;
|
|
30
|
+
readonly padlock: Uint8Array;
|
|
31
|
+
private constructor();
|
|
32
|
+
static from(seed: Uint8Array | string): Promise<WardKey>;
|
|
33
|
+
get pk(): string;
|
|
34
|
+
open(lid: Uint8Array): Promise<Uint8Array | null>;
|
|
35
|
+
}
|
|
36
|
+
export declare const wardHalves: (pk: string) => {
|
|
37
|
+
signing: string;
|
|
38
|
+
padlock: string;
|
|
39
|
+
};
|
|
40
|
+
export declare const drawLock: (draw: Draw) => Lock;
|
package/dist/quo/keys.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The keys of Quo: the ward key derived from a seed, and every other key
|
|
3
|
+
// drawn. A draw is the caller's, so this layer names no source of bytes.
|
|
4
|
+
import { agree, ed25519Public, hex, hkdf, lockFrom, sha256, utf8, x25519Public } from '../crypto/index.js';
|
|
5
|
+
export const LABEL = {
|
|
6
|
+
seal: utf8('quo-seal'),
|
|
7
|
+
edgeSeal: utf8('quo-edge-seal'),
|
|
8
|
+
lock: utf8('quo-lock'),
|
|
9
|
+
edge: utf8('quo-edge'),
|
|
10
|
+
wardSign: utf8('quo-ward-sign'),
|
|
11
|
+
wardSeal: utf8('quo-ward-seal'),
|
|
12
|
+
};
|
|
13
|
+
// An Ed25519 pair: a heir, or a standing's own key.
|
|
14
|
+
export class SigningKey {
|
|
15
|
+
secret;
|
|
16
|
+
pk;
|
|
17
|
+
constructor(secret, pk) {
|
|
18
|
+
this.secret = secret;
|
|
19
|
+
this.pk = pk;
|
|
20
|
+
}
|
|
21
|
+
static async from(secret) {
|
|
22
|
+
return new SigningKey(secret, await ed25519Public(secret));
|
|
23
|
+
}
|
|
24
|
+
static draw(draw) {
|
|
25
|
+
return SigningKey.from(draw(32));
|
|
26
|
+
}
|
|
27
|
+
get id() {
|
|
28
|
+
return hex(this.pk);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// An X25519 pair used for one box.
|
|
32
|
+
export class Ephemeral {
|
|
33
|
+
secret;
|
|
34
|
+
pk;
|
|
35
|
+
constructor(secret, pk) {
|
|
36
|
+
this.secret = secret;
|
|
37
|
+
this.pk = pk;
|
|
38
|
+
}
|
|
39
|
+
static async from(secret) {
|
|
40
|
+
return new Ephemeral(secret, await x25519Public(secret));
|
|
41
|
+
}
|
|
42
|
+
static draw(draw) {
|
|
43
|
+
return Ephemeral.from(draw(32));
|
|
44
|
+
}
|
|
45
|
+
agree(pk) {
|
|
46
|
+
return agree(this.secret, pk);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// The ward key: a signing pair and a padlock, both from the seed.
|
|
50
|
+
export class WardKey {
|
|
51
|
+
signing;
|
|
52
|
+
sealSecret;
|
|
53
|
+
padlock;
|
|
54
|
+
constructor(signing, sealSecret, padlock) {
|
|
55
|
+
this.signing = signing;
|
|
56
|
+
this.sealSecret = sealSecret;
|
|
57
|
+
this.padlock = padlock;
|
|
58
|
+
}
|
|
59
|
+
// Thirty-two bytes are the seed. Text, and bytes of any other length, are
|
|
60
|
+
// hashed to it first.
|
|
61
|
+
static async from(seed) {
|
|
62
|
+
const bytes = typeof seed === 'string' ? await sha256(utf8(seed)) : seed.length === 32 ? seed : await sha256(seed);
|
|
63
|
+
const sealSecret = await hkdf(bytes, LABEL.wardSeal, 32);
|
|
64
|
+
return new WardKey(await SigningKey.from(await hkdf(bytes, LABEL.wardSign, 32)), sealSecret, await x25519Public(sealSecret));
|
|
65
|
+
}
|
|
66
|
+
get pk() {
|
|
67
|
+
return hex(this.signing.pk) + hex(this.padlock);
|
|
68
|
+
}
|
|
69
|
+
open(lid) {
|
|
70
|
+
return agree(this.sealSecret, lid);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// A ward pk's two halves.
|
|
74
|
+
export const wardHalves = (pk) => ({ signing: pk.slice(0, 64), padlock: pk.slice(64) });
|
|
75
|
+
// A lock from sixty-four drawn bytes.
|
|
76
|
+
export const drawLock = (draw) => {
|
|
77
|
+
const bytes = draw(64);
|
|
78
|
+
return lockFrom(bytes.subarray(0, 32), bytes.subarray(32));
|
|
79
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type Payload = {
|
|
2
|
+
readonly to: string | null;
|
|
3
|
+
readonly by: string;
|
|
4
|
+
readonly next: string | null;
|
|
5
|
+
readonly seq: number;
|
|
6
|
+
readonly method?: string;
|
|
7
|
+
readonly args?: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const writePayload: (p: Payload) => string;
|
|
10
|
+
export type PayloadRead = {
|
|
11
|
+
payload: Payload;
|
|
12
|
+
} | {
|
|
13
|
+
fault: 'not an object' | 'not well formed';
|
|
14
|
+
};
|
|
15
|
+
export declare const readPayload: (bytes: Uint8Array) => PayloadRead;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The payload: `{ to, by, next, seq, method?, args? }`. `args` is carried as
|
|
3
|
+
// the text it was written in, since Quo reads nothing inside it.
|
|
4
|
+
import { isCount, isHex, readObject } from '../crypto/index.js';
|
|
5
|
+
const OWED = ['to', 'by', 'next', 'seq'];
|
|
6
|
+
const ZERO = '0'.repeat(64);
|
|
7
|
+
const isPk = (text) => isHex(text, 32) && text !== ZERO;
|
|
8
|
+
// A pk written as a JSON string, or null written as null.
|
|
9
|
+
const pkOrNull = (text, nullable) => {
|
|
10
|
+
if (nullable && text === 'null')
|
|
11
|
+
return null;
|
|
12
|
+
if (!/^"[0-9a-f]{64}"$/.test(text))
|
|
13
|
+
return undefined;
|
|
14
|
+
const pk = text.slice(1, -1);
|
|
15
|
+
return isPk(pk) ? pk : undefined;
|
|
16
|
+
};
|
|
17
|
+
export const writePayload = (p) => {
|
|
18
|
+
const parts = [`"to":${JSON.stringify(p.to)}`, `"by":"${p.by}"`, `"next":${JSON.stringify(p.next)}`, `"seq":${p.seq}`];
|
|
19
|
+
if (p.method !== undefined)
|
|
20
|
+
parts.push(`"method":${JSON.stringify(p.method)}`);
|
|
21
|
+
if (p.args !== undefined)
|
|
22
|
+
parts.push(`"args":${p.args}`);
|
|
23
|
+
return `{${parts.join(',')}}`;
|
|
24
|
+
};
|
|
25
|
+
// Cases 2 and 3 of the door, before the head is compared.
|
|
26
|
+
export const readPayload = (bytes) => {
|
|
27
|
+
const fields = readObject(bytes);
|
|
28
|
+
if (fields === null)
|
|
29
|
+
return { fault: 'not an object' };
|
|
30
|
+
const bad = { fault: 'not well formed' };
|
|
31
|
+
if (!OWED.every((f) => fields.has(f)))
|
|
32
|
+
return bad;
|
|
33
|
+
const to = pkOrNull(fields.get('to'), true);
|
|
34
|
+
const by = pkOrNull(fields.get('by'), false);
|
|
35
|
+
const next = pkOrNull(fields.get('next'), true);
|
|
36
|
+
const seq = fields.get('seq');
|
|
37
|
+
if (to === undefined || !by || next === undefined || !isCount(seq))
|
|
38
|
+
return bad;
|
|
39
|
+
const method = fields.get('method');
|
|
40
|
+
if (method !== undefined && !method.startsWith('"'))
|
|
41
|
+
return bad;
|
|
42
|
+
const args = fields.get('args');
|
|
43
|
+
if (args !== undefined && !args.startsWith('{'))
|
|
44
|
+
return bad;
|
|
45
|
+
return {
|
|
46
|
+
payload: {
|
|
47
|
+
to,
|
|
48
|
+
by,
|
|
49
|
+
next,
|
|
50
|
+
seq: Number(seq),
|
|
51
|
+
...(method === undefined ? {} : { method: JSON.parse(method) }),
|
|
52
|
+
...(args === undefined ? {} : { args }),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Lock } from '../crypto/index.ts';
|
|
2
|
+
export type Keys = {
|
|
3
|
+
readonly held: string;
|
|
4
|
+
readonly vouched: string | null;
|
|
5
|
+
readonly open: string;
|
|
6
|
+
readonly offered: string;
|
|
7
|
+
};
|
|
8
|
+
export type HeirRecord = {
|
|
9
|
+
readonly state: 'fresh';
|
|
10
|
+
} | ({
|
|
11
|
+
readonly state: 'spent';
|
|
12
|
+
readonly highest: number;
|
|
13
|
+
readonly honoured: readonly number[];
|
|
14
|
+
} & Keys) | ({
|
|
15
|
+
readonly state: 'kept';
|
|
16
|
+
} & Keys);
|
|
17
|
+
export type KeptLock = {
|
|
18
|
+
readonly ek: string;
|
|
19
|
+
readonly dk: string;
|
|
20
|
+
};
|
|
21
|
+
export interface Relations {
|
|
22
|
+
lock(): Lock | undefined;
|
|
23
|
+
setLock(lock: Lock): void;
|
|
24
|
+
get(heir: string): HeirRecord | undefined;
|
|
25
|
+
set(heir: string, record: HeirRecord): void;
|
|
26
|
+
delete(heir: string): void;
|
|
27
|
+
kept(): Promise<boolean>;
|
|
28
|
+
}
|
|
29
|
+
export declare const lockOf: (kept: KeptLock | undefined) => Lock | undefined;
|
|
30
|
+
export declare const keptLock: (lock: Lock) => KeptLock;
|
|
31
|
+
export declare const GONE = 256;
|
|
32
|
+
export declare class MemoryRelations implements Relations {
|
|
33
|
+
#private;
|
|
34
|
+
lock(): Lock | undefined;
|
|
35
|
+
setLock(lock: Lock): void;
|
|
36
|
+
get(heir: string): HeirRecord | undefined;
|
|
37
|
+
set(heir: string, record: HeirRecord): void;
|
|
38
|
+
delete(heir: string): void;
|
|
39
|
+
kept(): Promise<boolean>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { hex, unhex } from '../crypto/index.js';
|
|
2
|
+
export const lockOf = (kept) => (kept ? { ek: unhex(kept.ek), dk: unhex(kept.dk) } : undefined);
|
|
3
|
+
export const keptLock = (lock) => ({ ek: hex(lock.ek), dk: hex(lock.dk) });
|
|
4
|
+
// A bound on the kept records of removed relations, oldest out.
|
|
5
|
+
export const GONE = 256;
|
|
6
|
+
export class MemoryRelations {
|
|
7
|
+
#lock;
|
|
8
|
+
#heirs = new Map();
|
|
9
|
+
#gone = [];
|
|
10
|
+
lock() {
|
|
11
|
+
return lockOf(this.#lock);
|
|
12
|
+
}
|
|
13
|
+
setLock(lock) {
|
|
14
|
+
this.#lock = keptLock(lock);
|
|
15
|
+
}
|
|
16
|
+
get(heir) {
|
|
17
|
+
return this.#heirs.get(heir);
|
|
18
|
+
}
|
|
19
|
+
set(heir, record) {
|
|
20
|
+
this.#heirs.set(heir, record);
|
|
21
|
+
if (record.state !== 'kept')
|
|
22
|
+
return;
|
|
23
|
+
this.#gone.push(heir);
|
|
24
|
+
while (this.#gone.length > GONE)
|
|
25
|
+
this.#heirs.delete(this.#gone.shift());
|
|
26
|
+
}
|
|
27
|
+
delete(heir) {
|
|
28
|
+
this.#heirs.delete(heir);
|
|
29
|
+
}
|
|
30
|
+
kept() {
|
|
31
|
+
return Promise.resolve(true);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const WORDS: readonly ['removed', 'unannounced', 'repeated'];
|
|
2
|
+
export type Word = (typeof WORDS)[number];
|
|
3
|
+
export type Reply = {
|
|
4
|
+
readonly object: string;
|
|
5
|
+
readonly seen: string | null;
|
|
6
|
+
} | {
|
|
7
|
+
readonly silence: true;
|
|
8
|
+
} | {
|
|
9
|
+
readonly quo: Word;
|
|
10
|
+
};
|
|
11
|
+
export declare const SILENCE = "{\"silence\":true}";
|
|
12
|
+
export declare const writeReply: (reply: Reply) => string;
|
|
13
|
+
export declare const readReply: (bytes: Uint8Array) => Reply | null;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The reply text: `{ object, seen }`, `{ silence: true }` or `{ quo: word }`.
|
|
3
|
+
// `object` is carried as the text it was written in.
|
|
4
|
+
import { readObject } from '../crypto/index.js';
|
|
5
|
+
export const WORDS = ['removed', 'unannounced', 'repeated'];
|
|
6
|
+
export const SILENCE = '{"silence":true}';
|
|
7
|
+
export const writeReply = (reply) => {
|
|
8
|
+
if ('silence' in reply)
|
|
9
|
+
return SILENCE;
|
|
10
|
+
if ('quo' in reply)
|
|
11
|
+
return `{"quo":"${reply.quo}"}`;
|
|
12
|
+
return `{"object":${reply.object},"seen":${JSON.stringify(reply.seen)}}`;
|
|
13
|
+
};
|
|
14
|
+
const exactly = (fields, ...keys) => fields.size === keys.length && keys.every((k) => fields.has(k));
|
|
15
|
+
// A reply text of one of the three shapes, or null for anything else, which
|
|
16
|
+
// the reader takes as silence.
|
|
17
|
+
export const readReply = (bytes) => {
|
|
18
|
+
const fields = readObject(bytes);
|
|
19
|
+
if (fields === null)
|
|
20
|
+
return null;
|
|
21
|
+
if (exactly(fields, 'silence'))
|
|
22
|
+
return fields.get('silence') === 'true' ? { silence: true } : null;
|
|
23
|
+
if (exactly(fields, 'quo')) {
|
|
24
|
+
const text = fields.get('quo');
|
|
25
|
+
const word = text.startsWith('"') ? JSON.parse(text) : null;
|
|
26
|
+
return WORDS.find((w) => w === word) ? { quo: word } : null;
|
|
27
|
+
}
|
|
28
|
+
if (exactly(fields, 'object', 'seen')) {
|
|
29
|
+
const seen = fields.get('seen');
|
|
30
|
+
if (seen === 'null')
|
|
31
|
+
return { object: fields.get('object'), seen: null };
|
|
32
|
+
return seen.startsWith('"') ? { object: fields.get('object'), seen: JSON.parse(seen) } : null;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
};
|