@quo-systems/quo 0.2.9 → 0.2.11
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 +19 -10
- package/SPEC.md +301 -110
- package/dist/being/being.d.ts +2 -2
- package/dist/being/being.js +34 -12
- package/dist/being/digest.js +26 -8
- package/dist/being/index.d.ts +2 -2
- package/dist/being/index.js +2 -2
- package/dist/being/silence.d.ts +2 -0
- package/dist/being/silence.js +12 -0
- package/dist/being/types.d.ts +4 -2
- package/dist/being/types.js +25 -0
- package/dist/conformance/assert.js +40 -6
- package/dist/conformance/beings.d.ts +48 -5
- package/dist/conformance/beings.js +39 -8
- package/dist/conformance/estate.js +110 -21
- package/dist/conformance/index.d.ts +5 -2
- package/dist/conformance/index.js +165 -7
- package/dist/harbor/core.d.ts +5 -2
- package/dist/harbor/core.js +195 -45
- package/dist/harbor/dial.js +32 -15
- package/dist/harbor/index.d.ts +1 -0
- package/dist/harbor/index.js +3 -0
- package/dist/harbor/memory.d.ts +3 -3
- package/dist/harbor/memory.js +7 -12
- package/dist/harbor/reach.js +42 -17
- package/dist/ward/allowance.js +15 -4
- package/dist/ward/arithmetic.d.ts +1 -0
- package/dist/ward/arithmetic.js +22 -6
- package/dist/ward/cells.d.ts +3 -1
- package/dist/ward/cells.js +79 -21
- package/dist/ward/door.d.ts +3 -2
- package/dist/ward/door.js +38 -10
- package/dist/ward/ground.d.ts +5 -1
- package/dist/ward/ground.js +38 -1
- package/dist/ward/heirs.d.ts +2 -3
- package/dist/ward/heirs.js +19 -13
- package/dist/ward/index.d.ts +1 -0
- package/dist/ward/index.js +3 -0
- package/dist/ward/owner.d.ts +6 -27
- package/dist/ward/owner.js +59 -33
- package/dist/ward/partition.d.ts +3 -0
- package/dist/ward/partition.js +109 -4
- package/dist/ward/seal.d.ts +1 -0
- package/dist/ward/seal.js +41 -13
- package/dist/ward/stance.d.ts +7 -3
- package/dist/ward/stance.js +156 -66
- package/dist/ward/ward.d.ts +10 -0
- package/dist/ward/ward.js +123 -51
- package/package.json +4 -2
- package/src/being/being.ts +33 -11
- package/src/being/digest.ts +28 -13
- package/src/being/index.ts +2 -2
- package/src/being/silence.ts +14 -0
- package/src/being/types.ts +39 -5
- package/src/conformance/assert.ts +37 -4
- package/src/conformance/beings.ts +41 -10
- package/src/conformance/estate.ts +107 -20
- package/src/conformance/index.ts +188 -13
- package/src/harbor/core.ts +203 -46
- package/src/harbor/dial.ts +46 -17
- package/src/harbor/index.ts +3 -0
- package/src/harbor/memory.ts +8 -13
- package/src/harbor/reach.ts +47 -21
- package/src/ward/allowance.ts +15 -4
- package/src/ward/arithmetic.ts +25 -8
- package/src/ward/cells.ts +76 -25
- package/src/ward/door.ts +38 -12
- package/src/ward/ground.ts +52 -3
- package/src/ward/heirs.ts +19 -13
- package/src/ward/index.ts +3 -0
- package/src/ward/owner.ts +65 -46
- package/src/ward/partition.ts +109 -5
- package/src/ward/seal.ts +41 -12
- package/src/ward/stance.ts +163 -64
- package/src/ward/ward.ts +124 -52
- package/vectors/arithmetic.json +7 -0
- package/vectors/framing.json +30 -15
- package/vectors/wire.json +4 -4
package/src/ward/heirs.ts
CHANGED
|
@@ -13,11 +13,14 @@ const SPAN = 64;
|
|
|
13
13
|
|
|
14
14
|
export class Heirs {
|
|
15
15
|
#p: Partition;
|
|
16
|
-
|
|
16
|
+
#wrote: () => void;
|
|
17
|
+
constructor(p: Partition, wrote: () => void = () => {}) {
|
|
17
18
|
this.#p = p;
|
|
19
|
+
this.#wrote = wrote;
|
|
18
20
|
}
|
|
19
21
|
open(heir: string, being: string, id: string): void {
|
|
20
22
|
this.#p.heirs[heir] = { being, id, current: heir, announced: null, fresh: true, mark: 0, spent: [] };
|
|
23
|
+
this.#wrote();
|
|
21
24
|
}
|
|
22
25
|
// The id was removed. The heir is forgotten, and its last keys are kept
|
|
23
26
|
// apart, bounded, so that whoever still holds them hears `removed` at the
|
|
@@ -29,6 +32,7 @@ export class Heirs {
|
|
|
29
32
|
this.#p.gone[heir] = { current: h.current, announced: h.announced };
|
|
30
33
|
const keys = Object.keys(this.#p.gone);
|
|
31
34
|
for (const old of keys.slice(0, Math.max(0, keys.length - GONE))) delete this.#p.gone[old];
|
|
35
|
+
this.#wrote();
|
|
32
36
|
}
|
|
33
37
|
// May `by` speak for a relation she removed? True only for the keys the
|
|
34
38
|
// door held when the id went, which nobody but their holder has.
|
|
@@ -36,9 +40,6 @@ export class Heirs {
|
|
|
36
40
|
const g = this.#p.gone[heir];
|
|
37
41
|
return !!g && (by === g.current || by === g.announced);
|
|
38
42
|
}
|
|
39
|
-
get(heir: string): Heir | undefined {
|
|
40
|
-
return this.#p.heirs[heir];
|
|
41
|
-
}
|
|
42
43
|
// May `by` speak for this heir? Returns the record if so, null if not.
|
|
43
44
|
// Does not write: the caller verifies the signature first, then settles.
|
|
44
45
|
admits(heir: string, by: string): Heir | null {
|
|
@@ -78,25 +79,30 @@ export class Heirs {
|
|
|
78
79
|
// neither: a call that binds nothing must not burn a number on its way to
|
|
79
80
|
// being refused, or a stranger who cannot be heard would still leave a mark
|
|
80
81
|
// behind her. Every write below this line is one that is going to hold.
|
|
82
|
+
// This is where a knock is judged and where `unannounced` is said. settle
|
|
83
|
+
// below is the writing alone, and it judges nothing: one owner for the
|
|
84
|
+
// question, one for the answer.
|
|
81
85
|
honour(h: Heir, by: string, next: string | null, seq: number): true | DoorWord {
|
|
82
|
-
if (h.fresh && next === null) return 'unannounced'; // a knock without a key of her own binds nothing
|
|
86
|
+
if (h.fresh && (next === null || next === h.current)) return 'unannounced'; // a knock without a key of her own binds nothing, and the heir is not a key of her own
|
|
83
87
|
if (!this.spend(h, seq)) return 'repeated';
|
|
84
|
-
|
|
88
|
+
this.settle(h, by, next);
|
|
89
|
+
this.#wrote(); // the number and the keys moved together
|
|
90
|
+
return true;
|
|
85
91
|
}
|
|
86
92
|
|
|
87
|
-
// The signature checked out. Settle the keys: a fresh
|
|
88
|
-
// to what it announced
|
|
89
|
-
//
|
|
90
|
-
|
|
93
|
+
// The signature checked out and honour has judged. Settle the keys: a fresh
|
|
94
|
+
// heir rotates at once to what it announced, which honour has already held
|
|
95
|
+
// to being something other than itself, so that the heir dies as it speaks;
|
|
96
|
+
// a current key replaces its announcement; an announced key becomes current.
|
|
97
|
+
settle(h: Heir, by: string, next: string | null): void {
|
|
91
98
|
if (h.fresh) {
|
|
92
|
-
if (next === null) return
|
|
99
|
+
if (next === null) return; // honour judged this. the compiler has not read it, so the narrowing stays.
|
|
93
100
|
h.current = next;
|
|
94
101
|
h.announced = null;
|
|
95
102
|
h.fresh = false;
|
|
96
|
-
return
|
|
103
|
+
return;
|
|
97
104
|
}
|
|
98
105
|
if (by === h.announced) h.current = by;
|
|
99
106
|
h.announced = next;
|
|
100
|
-
return true;
|
|
101
107
|
}
|
|
102
108
|
}
|
package/src/ward/index.ts
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
export { Ward } from './ward.ts';
|
|
4
4
|
export type { Ground, WardPointers } from './ground.ts';
|
|
5
5
|
export type { Partition, Heir, Bind, StandingKeys } from './partition.ts';
|
|
6
|
+
// The two bounds on what a partition may hold, for a kit that must keep the
|
|
7
|
+
// same ones and for a test that pins them.
|
|
8
|
+
export { GONE, MINTED } from './partition.ts';
|
|
6
9
|
export type { AskPayload, ReplyPayload } from './seal.ts';
|
|
7
10
|
// The seal and the arithmetic, for a kit in another language to check its
|
|
8
11
|
// bytes against, and for tests that speak to a door directly.
|
package/src/ward/owner.ts
CHANGED
|
@@ -9,32 +9,29 @@
|
|
|
9
9
|
// her cells under the id the owner gave; remove takes a relation out of her
|
|
10
10
|
// by id, the mirror of it.
|
|
11
11
|
import { isSilence, isWord, wordOf } from '../being/silence.ts';
|
|
12
|
-
import { digest } from '../being/digest.ts';
|
|
13
12
|
import { OWNER } from '../being/types.ts';
|
|
14
13
|
import type { Ask, Asker, Invitation, Json, JsonObject, Wanted } from '../being/types.ts';
|
|
14
|
+
import { put } from './partition.ts';
|
|
15
|
+
import { seen } from './door.ts';
|
|
16
|
+
import { within, DEFAULT, LATE } from './allowance.ts';
|
|
17
|
+
import type { Resident } from './ward.ts';
|
|
15
18
|
|
|
16
19
|
export type OwnerSide = {
|
|
17
20
|
pk: string;
|
|
18
|
-
doors: Map<
|
|
19
|
-
|
|
20
|
-
{
|
|
21
|
-
key: string;
|
|
22
|
-
being: { answer: (...a: never[]) => unknown };
|
|
23
|
-
cells: { class?: string; occupants: Record<string, unknown>; standings: Record<string, unknown> };
|
|
24
|
-
stance: { occupants: { invite(id: string): Promise<Invitation | null>; remove(id: string): void }; standings: { knock(inv: Invitation, m?: string, a?: JsonObject, w?: Wanted): Promise<unknown>; take(id: string, inv: Invitation): Promise<string | null>; remove(id: string): void } };
|
|
25
|
-
}
|
|
26
|
-
>;
|
|
21
|
+
doors: Map<string, Resident>;
|
|
22
|
+
absent(): Record<string, string | null>; // the rows no door holds this run: key to class
|
|
27
23
|
publicKey(): string | null;
|
|
28
|
-
|
|
24
|
+
// Class first, then key, as the ground and a being's own boot both say it.
|
|
25
|
+
instantiate(className: string, key: string): Resident | 'threw' | null;
|
|
29
26
|
unboot(key: string): string[] | null;
|
|
30
|
-
setPublic(key: string): void;
|
|
27
|
+
setPublic(key: string | null): void; // null takes the mark off, and the being stays
|
|
31
28
|
};
|
|
32
29
|
|
|
33
30
|
const str = { type: 'string' };
|
|
34
31
|
export const OWNER_ASKS: Ask[] = [
|
|
35
32
|
{ name: 'boot', description: 'boot a being by class name, under a key the owner chooses', input: { type: 'object', properties: { key: str, class: str }, required: ['key', 'class'] } },
|
|
36
|
-
{ name: 'public', description: 'mark a booted being as the one public being of the ward, reached by anyone at the bare pk', input: { type: 'object', properties: { key: str }, required: ['key'] } },
|
|
37
|
-
{ name: 'invite', description: 'mint an invitation on a being of the ward, under the id she will know the occupant by; on the ward pk it mints an owner, and only the root may', input: { type: 'object', properties: { being: str, id: str }, required: ['being', 'id'] } },
|
|
33
|
+
{ name: 'public', description: 'mark a booted being as the one public being of the ward, reached by anyone at the bare pk; null takes the mark off and leaves her booted', input: { type: 'object', properties: { key: str }, required: ['key'] } },
|
|
34
|
+
{ name: 'invite', description: 'mint an invitation on a being of the ward, under the id she will know the occupant by, with the notes she will read on it; on the ward pk it mints an owner, and only the root may', input: { type: 'object', properties: { being: str, id: str, notes: { type: 'object' } }, required: ['being', 'id'] } },
|
|
38
35
|
{
|
|
39
36
|
name: 'knock',
|
|
40
37
|
description: 'knock for a being of the ward with an invitation, and take the standing under id if answered; being is a key booted, or { boot: class, key } to boot her first',
|
|
@@ -45,26 +42,38 @@ export const OWNER_ASKS: Ask[] = [
|
|
|
45
42
|
];
|
|
46
43
|
|
|
47
44
|
export async function ownerAnswer(w: OwnerSide, asker: Asker, method: string | undefined, args: JsonObject): Promise<Json> {
|
|
48
|
-
// Every name the owner gives is a
|
|
49
|
-
// object passed where a name belongs becomes the string
|
|
50
|
-
// and two owners who each got the shape wrong would be
|
|
51
|
-
|
|
45
|
+
// Every name the owner gives is a string, or it is nothing. Coerced instead
|
|
46
|
+
// of checked, an object passed where a name belongs becomes the string
|
|
47
|
+
// "[object Object]", and two owners who each got the shape wrong would be
|
|
48
|
+
// handed one being. Not `word`: a word in this tree is what a ward says when
|
|
49
|
+
// no object came back, and this file imports those.
|
|
50
|
+
const named = (v: unknown): string | null => (typeof v === 'string' ? v : null);
|
|
52
51
|
|
|
53
52
|
if (method === undefined) return describe(w);
|
|
54
53
|
if (method === 'boot') {
|
|
55
|
-
const key =
|
|
56
|
-
className =
|
|
54
|
+
const key = named(args.key),
|
|
55
|
+
className = named(args.class);
|
|
57
56
|
if (key === null || className === null) return { error: 'no such class, or key taken' };
|
|
58
|
-
const door = w.instantiate(
|
|
57
|
+
const door = w.instantiate(className, key);
|
|
58
|
+
if (door === 'threw') return { error: 'threw at birth' };
|
|
59
59
|
return door ? { booted: door.key } : { error: 'no such class, or key taken' };
|
|
60
60
|
}
|
|
61
61
|
if (method === 'public') {
|
|
62
62
|
// A ward may have one public being, and no more. Marking a second would
|
|
63
63
|
// leave the first holding every relation she had, reachable by nobody at
|
|
64
|
-
// the bare pk, and told by nobody that she had been replaced.
|
|
65
|
-
|
|
64
|
+
// the bare pk, and told by nobody that she had been replaced. One absent
|
|
65
|
+
// this run is reachable by nobody already, so the mark may move off her.
|
|
66
|
+
// Null takes the mark off: the being stays and keeps every relation she
|
|
67
|
+
// holds, and the ward answers arrivals for no heir with silence, as one
|
|
68
|
+
// that never had a public being does. Retiring her is not destroying her.
|
|
69
|
+
if (args.key === null) {
|
|
70
|
+
w.setPublic(null);
|
|
71
|
+
return { public: null };
|
|
72
|
+
}
|
|
73
|
+
const key = named(args.key);
|
|
66
74
|
if (key === null || key === w.pk || !w.doors.has(key)) return { error: 'no such being' };
|
|
67
|
-
|
|
75
|
+
const standing = w.publicKey();
|
|
76
|
+
if (standing !== null && standing !== key && w.doors.has(standing)) return { error: 'a ward has one public being' };
|
|
68
77
|
w.setPublic(key);
|
|
69
78
|
return { public: key };
|
|
70
79
|
}
|
|
@@ -73,12 +82,17 @@ export async function ownerAnswer(w: OwnerSide, asker: Asker, method: string | u
|
|
|
73
82
|
// is an owner, and ownership is minted by the ask pointer alone. An owner
|
|
74
83
|
// at the door asking to invite on the ward is refused, so that a carried
|
|
75
84
|
// key can pilot but never hand piloting on.
|
|
76
|
-
const being =
|
|
77
|
-
id =
|
|
85
|
+
const being = named(args.being),
|
|
86
|
+
id = named(args.id);
|
|
78
87
|
if (being === null || id === null) return { error: 'no such being' };
|
|
79
88
|
if (being === w.pk && asker.id !== OWNER) return { error: 'no such being' };
|
|
89
|
+
// The notes are the terms the owner mints under, and the being reads them
|
|
90
|
+
// on her gate. Placing an occupant is the owner's, and saying what it may
|
|
91
|
+
// do is part of placing it; the work behind the gate is still hers alone.
|
|
92
|
+
const notes = args.notes;
|
|
80
93
|
const door = w.doors.get(being);
|
|
81
|
-
|
|
94
|
+
if (!door) return { error: 'no such being' };
|
|
95
|
+
return await door.stance.occupants.invite(id, notes !== null && typeof notes === 'object' && !Array.isArray(notes) ? (notes as JsonObject) : undefined);
|
|
82
96
|
}
|
|
83
97
|
if (method === 'knock') {
|
|
84
98
|
const b = args.being as string | { boot: unknown; key: unknown };
|
|
@@ -86,13 +100,16 @@ export async function ownerAnswer(w: OwnerSide, asker: Asker, method: string | u
|
|
|
86
100
|
if (!door && b && typeof b === 'object') {
|
|
87
101
|
// new or existing: the owner names a being of theirs, and a key already
|
|
88
102
|
// booted is a being of theirs, not a class that failed to instantiate.
|
|
89
|
-
const key =
|
|
90
|
-
className =
|
|
103
|
+
const key = named(b.key),
|
|
104
|
+
className = named(b.boot);
|
|
91
105
|
if (key === null || className === null) return { error: 'no such being' };
|
|
92
|
-
const made = w.instantiate(
|
|
93
|
-
|
|
106
|
+
const made = w.instantiate(className, key);
|
|
107
|
+
if (made === 'threw') return { error: 'threw at birth' };
|
|
108
|
+
// Made or already there, the door is the one under the key she named:
|
|
109
|
+
// instantiate boots under that key or refuses, and never another.
|
|
110
|
+
door = w.doors.get(key);
|
|
94
111
|
}
|
|
95
|
-
const id =
|
|
112
|
+
const id = named(args.id);
|
|
96
113
|
// Never for the ward itself, for the reason invite refuses it: the ward
|
|
97
114
|
// is a being to nobody outside, and holds no relation of its own. A
|
|
98
115
|
// standing written into its cells would be the owner's reach dressed as
|
|
@@ -111,8 +128,8 @@ export async function ownerAnswer(w: OwnerSide, asker: Asker, method: string | u
|
|
|
111
128
|
// standing, since the two share one namespace. On the ward itself the
|
|
112
129
|
// id is an owner, and only the root may unseat one, for the reason only
|
|
113
130
|
// the root may invite one: ownership moves by the root alone.
|
|
114
|
-
const being =
|
|
115
|
-
id =
|
|
131
|
+
const being = named(args.being),
|
|
132
|
+
id = named(args.id);
|
|
116
133
|
if (being === null || id === null) return { error: 'no such being' };
|
|
117
134
|
if (being === w.pk && asker.id !== OWNER) return { error: 'no such being' };
|
|
118
135
|
const door = w.doors.get(being);
|
|
@@ -130,7 +147,7 @@ export async function ownerAnswer(w: OwnerSide, asker: Asker, method: string | u
|
|
|
130
147
|
// refused, for the reason knock is: it is a being to nobody outside, and
|
|
131
148
|
// unbooting it would be a ward deleting itself from inside its own map,
|
|
132
149
|
// leaving its owners bound to a door that is gone.
|
|
133
|
-
const being =
|
|
150
|
+
const being = named(args.being);
|
|
134
151
|
if (being === null || being === w.pk) return { error: 'no such being' };
|
|
135
152
|
const removed = w.unboot(being);
|
|
136
153
|
return removed === null ? { error: 'no such being' } : { unbooted: being, removed };
|
|
@@ -139,20 +156,22 @@ export async function ownerAnswer(w: OwnerSide, asker: Asker, method: string | u
|
|
|
139
156
|
}
|
|
140
157
|
|
|
141
158
|
// The ward's describe for its owner: its beings, their classes, a digest each,
|
|
142
|
-
// as she would describe herself to the owner.
|
|
159
|
+
// as she would describe herself to the owner. A being absent this run is
|
|
160
|
+
// listed too, with a null digest and absent true, so the owner can see her
|
|
161
|
+
// and take her out.
|
|
143
162
|
async function describe(w: OwnerSide): Promise<Json> {
|
|
144
163
|
const beings: Record<string, Json> = {};
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
beings[key] = { class: door.cells.class ?? null, public: w.publicKey() === key, digest: d };
|
|
164
|
+
// Every being is asked at once and each is bounded on her own. One being
|
|
165
|
+
// whose describe never settles is one null digest, not a ward that never
|
|
166
|
+
// answers its owner: the harbor learns the pk by this very ask at boot, so
|
|
167
|
+
// an unbounded wait here is a ward no restart can bring back.
|
|
168
|
+
const asked = [...w.doors].filter(([key]) => key !== w.pk);
|
|
169
|
+
const digests = await Promise.all(asked.map(async ([, door]) => (await within(DEFAULT.time, seen(door, { id: OWNER }))) as string | null | typeof LATE));
|
|
170
|
+
for (const [i, [key, door]] of asked.entries()) {
|
|
171
|
+
const d = digests[i];
|
|
172
|
+
put(beings, key, { class: door.cells.class ?? null, public: w.publicKey() === key, digest: d === LATE ? null : d });
|
|
155
173
|
}
|
|
174
|
+
for (const [key, cls] of Object.entries(w.absent())) put(beings, key, { class: cls, public: w.publicKey() === key, digest: null, absent: true });
|
|
156
175
|
return {
|
|
157
176
|
asks: OWNER_ASKS,
|
|
158
177
|
notes: { pk: w.pk, beings },
|
package/src/ward/partition.ts
CHANGED
|
@@ -75,7 +75,7 @@ export type Bind = {
|
|
|
75
75
|
occupants: Record<string, string>; // id -> heir pk
|
|
76
76
|
knocks: Record<string, { current: string; next: string | null; spoke: boolean; sent: boolean; seq: number }>; // <ward pk>:<heir pk>, or public:<ward pk> -> her keys for a knock, before take. spoke: bytes came back once. sent: bytes went out once, answered or not
|
|
77
77
|
answered: Record<string, true>; // <ward pk>:<heir pk>, or public:<ward pk>: knocked and answered
|
|
78
|
-
minted: string[]; // the last eight pks her side minted.
|
|
78
|
+
minted: string[]; // the last eight pks her side minted. nothing in the ward reads it: it is there to be looked at, and a kit that keeps it empty holds the same ward
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
// A relation she removed, as the door remembers it: the keys that may still
|
|
@@ -86,6 +86,30 @@ export type Bind = {
|
|
|
86
86
|
export type Gone = { current: string; announced: string | null };
|
|
87
87
|
export const GONE = 256;
|
|
88
88
|
|
|
89
|
+
// The last few pks a relation minted, and no more. A relation rotates on
|
|
90
|
+
// every ask, so a list of all of them is a partition that grows for as long
|
|
91
|
+
// as she keeps talking and can never be written down. It lives here, beside
|
|
92
|
+
// the other bound on the durable shape, because it is a fact about what a
|
|
93
|
+
// partition may hold and not about how a ward mints.
|
|
94
|
+
export const MINTED = 8;
|
|
95
|
+
|
|
96
|
+
// A knock record is a key she minted for one invitation, kept until she takes
|
|
97
|
+
// it. Only take deletes one, so a being who knocks and never takes would grow
|
|
98
|
+
// her bind table for the life of the ward, one secret seed at a time. The
|
|
99
|
+
// list is bounded like `gone`: past the count the oldest go, and a relation
|
|
100
|
+
// that answered outlives one that never did, since an answered knock is one
|
|
101
|
+
// she may still take.
|
|
102
|
+
export const KNOCKS = 64;
|
|
103
|
+
export const prune = (knocks: Record<string, unknown>, answered: Record<string, true>): void => {
|
|
104
|
+
const names = Object.keys(knocks);
|
|
105
|
+
if (names.length <= KNOCKS) return;
|
|
106
|
+
const order = [...names.filter((n) => !answered[n]), ...names.filter((n) => answered[n])];
|
|
107
|
+
for (const old of order.slice(0, names.length - KNOCKS)) {
|
|
108
|
+
delete knocks[old];
|
|
109
|
+
delete answered[old];
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
89
113
|
export type Partition = {
|
|
90
114
|
version: string;
|
|
91
115
|
beings: Record<string, Cells & { class?: string }>; // key -> her cells
|
|
@@ -95,11 +119,91 @@ export type Partition = {
|
|
|
95
119
|
public: string | null; // the one public being's key
|
|
96
120
|
};
|
|
97
121
|
|
|
122
|
+
// The shape, read before anything acts on it. A partition this ward wrote is
|
|
123
|
+
// this shape by construction; one it adopted was written by a hand, another
|
|
124
|
+
// kit, or a file that was cut short, and every field below is one the ward,
|
|
125
|
+
// the door or the heirs then act on without looking again. A `spent` that is
|
|
126
|
+
// not an array rejects the door's promise on the first bound ask, a `mark`
|
|
127
|
+
// that is not a number honours every number ever sent and grows without
|
|
128
|
+
// bound, a being row that is null refuses the whole ward at birth by
|
|
129
|
+
// accident and says nothing about why. So the shape is read once, here, and
|
|
130
|
+
// what is not it throws with the path that failed, which names the trouble
|
|
131
|
+
// to whoever is holding the file.
|
|
132
|
+
//
|
|
133
|
+
// The rule, so the reader does not grow into a schema: read every field the
|
|
134
|
+
// ward acts on, and refuse only what would make it act wrongly. So `minted`
|
|
135
|
+
// must be a list, because she pushes to it, and the truth in `answered` is
|
|
136
|
+
// not read, because anything there says answered and nothing else follows.
|
|
137
|
+
// `last` is reserved and nobody reads it, so nobody reads its shape either;
|
|
138
|
+
// the run that gives it a reader gives it a line here. A being's own cells
|
|
139
|
+
// are hers, of any shape JSON carries, and only the two records the ward
|
|
140
|
+
// keeps inside them are read.
|
|
141
|
+
const fault = (path: string, want: string): never => {
|
|
142
|
+
throw new Error(`partition ${path} is not ${want}`);
|
|
143
|
+
};
|
|
144
|
+
const table = (v: unknown, path: string): Record<string, unknown> => (typeof v === 'object' && v !== null && !Array.isArray(v) ? (v as Record<string, unknown>) : fault(path, 'an object'));
|
|
145
|
+
const list = (v: unknown, path: string): unknown[] => (Array.isArray(v) ? (v as unknown[]) : fault(path, 'an array'));
|
|
146
|
+
const text = (v: unknown, path: string): void => void (typeof v === 'string' || fault(path, 'a string'));
|
|
147
|
+
const orNull = (v: unknown, path: string): void => void (v === null || typeof v === 'string' || fault(path, 'a string or null'));
|
|
148
|
+
const whole = (v: unknown, path: string): void => void (Number.isSafeInteger(v) || fault(path, 'a whole number'));
|
|
149
|
+
const flag = (v: unknown, path: string): void => void (typeof v === 'boolean' || fault(path, 'true or false'));
|
|
150
|
+
|
|
151
|
+
function read(p: Partition): Partition {
|
|
152
|
+
for (const [key, cells] of Object.entries(table(p.beings, 'beings'))) {
|
|
153
|
+
const c = table(cells, `beings.${key}`);
|
|
154
|
+
table(c.standings, `beings.${key}.standings`);
|
|
155
|
+
table(c.occupants, `beings.${key}.occupants`);
|
|
156
|
+
if (c.class !== undefined) text(c.class, `beings.${key}.class`);
|
|
157
|
+
}
|
|
158
|
+
for (const [key, bind] of Object.entries(table(p.bind, 'bind'))) {
|
|
159
|
+
const b = table(bind, `bind.${key}`);
|
|
160
|
+
for (const [id, keys] of Object.entries(table(b.standings, `bind.${key}.standings`))) {
|
|
161
|
+
const w = `bind.${key}.standings.${id}`;
|
|
162
|
+
const s = table(keys, w);
|
|
163
|
+
text(s.ward, `${w}.ward`);
|
|
164
|
+
orNull(s.heir, `${w}.heir`);
|
|
165
|
+
text(s.current, `${w}.current`);
|
|
166
|
+
orNull(s.next, `${w}.next`);
|
|
167
|
+
whole(s.seq, `${w}.seq`);
|
|
168
|
+
}
|
|
169
|
+
for (const [id, pk] of Object.entries(table(b.occupants, `bind.${key}.occupants`))) text(pk, `bind.${key}.occupants.${id}`);
|
|
170
|
+
for (const [name, keys] of Object.entries(table(b.knocks, `bind.${key}.knocks`))) {
|
|
171
|
+
const w = `bind.${key}.knocks.${name}`;
|
|
172
|
+
const k = table(keys, w);
|
|
173
|
+
text(k.current, `${w}.current`);
|
|
174
|
+
orNull(k.next, `${w}.next`);
|
|
175
|
+
flag(k.spoke, `${w}.spoke`);
|
|
176
|
+
flag(k.sent, `${w}.sent`);
|
|
177
|
+
whole(k.seq, `${w}.seq`);
|
|
178
|
+
}
|
|
179
|
+
table(b.answered, `bind.${key}.answered`);
|
|
180
|
+
for (const [i, pk] of list(b.minted, `bind.${key}.minted`).entries()) text(pk, `bind.${key}.minted[${i}]`);
|
|
181
|
+
}
|
|
182
|
+
for (const [pk, heir] of Object.entries(table(p.heirs, 'heirs'))) {
|
|
183
|
+
const h = table(heir, `heirs.${pk}`);
|
|
184
|
+
text(h.being, `heirs.${pk}.being`);
|
|
185
|
+
text(h.id, `heirs.${pk}.id`);
|
|
186
|
+
text(h.current, `heirs.${pk}.current`);
|
|
187
|
+
orNull(h.announced, `heirs.${pk}.announced`);
|
|
188
|
+
flag(h.fresh, `heirs.${pk}.fresh`);
|
|
189
|
+
whole(h.mark, `heirs.${pk}.mark`);
|
|
190
|
+
for (const [i, n] of list(h.spent, `heirs.${pk}.spent`).entries()) whole(n, `heirs.${pk}.spent[${i}]`);
|
|
191
|
+
}
|
|
192
|
+
for (const [pk, gone] of Object.entries(table(p.gone, 'gone'))) {
|
|
193
|
+
const g = table(gone, `gone.${pk}`);
|
|
194
|
+
text(g.current, `gone.${pk}.current`);
|
|
195
|
+
orNull(g.announced, `gone.${pk}.announced`);
|
|
196
|
+
}
|
|
197
|
+
orNull(p.public, 'public');
|
|
198
|
+
return p;
|
|
199
|
+
}
|
|
200
|
+
|
|
98
201
|
// The one place a ward reads state that another ward wrote. A fresh
|
|
99
202
|
// memory becomes a partition of this version; anything already written must
|
|
100
|
-
// say it is this version
|
|
101
|
-
// where a ward is allowed to be loud:
|
|
102
|
-
// that were made, and no ask has been
|
|
203
|
+
// say it is this version and be the shape this version names, and a ward that
|
|
204
|
+
// cannot read it does not boot. Birth is where a ward is allowed to be loud:
|
|
205
|
+
// silence is the door's word, for asks that were made, and no ask has been
|
|
206
|
+
// made yet.
|
|
103
207
|
export function open(memory: Record<string, unknown>): Partition {
|
|
104
208
|
const p = memory as Partition;
|
|
105
209
|
if (Object.keys(memory).length > 0 && p.version !== VERSION) {
|
|
@@ -112,7 +216,7 @@ export function open(memory: Record<string, unknown>): Partition {
|
|
|
112
216
|
p.heirs ??= {};
|
|
113
217
|
p.gone ??= {};
|
|
114
218
|
p.public ??= null;
|
|
115
|
-
return p;
|
|
219
|
+
return read(p);
|
|
116
220
|
}
|
|
117
221
|
|
|
118
222
|
// One record, reached by a key a being or an owner chose. A bare lookup finds
|
package/src/ward/seal.ts
CHANGED
|
@@ -21,24 +21,52 @@
|
|
|
21
21
|
// reply = JSON { object, seen } | { silence: true } | { quo: word } signed by the ward key
|
|
22
22
|
import type { DoorWord, Json, JsonObject } from '../being/types.ts';
|
|
23
23
|
import { isDoorWord } from '../being/silence.ts';
|
|
24
|
-
import { KEY, SIGNATURE, box, concat, hex, sha256, sign, signingPair, sealingPair, unbox, unhex, verify } from './arithmetic.ts';
|
|
24
|
+
import { KEY, SIGNATURE, box, concat, derive, hex, sha256, sign, signingPair, sealingPair, unbox, unhex, verify } from './arithmetic.ts';
|
|
25
|
+
import { cellFault } from './cells.ts';
|
|
25
26
|
|
|
26
27
|
const utf8 = new TextEncoder();
|
|
27
28
|
const text = new TextDecoder();
|
|
28
29
|
|
|
29
30
|
export type WardKey = { sign: Uint8Array; padlock: Uint8Array; signPk: Uint8Array; padlockPk: Uint8Array; pk: string };
|
|
30
31
|
|
|
32
|
+
// The two labels a ward's keys are derived under. They are here and not in
|
|
33
|
+
// arithmetic.ts because a label is a decision and that file is arithmetic.
|
|
34
|
+
// `quo-seal` is taken: it is the label the message cipher derives its key and
|
|
35
|
+
// nonce under, from an agreement rather than from a seed, and two derivations
|
|
36
|
+
// answering to one name is how a kit is read wrong.
|
|
37
|
+
const WARD_SIGN = new TextEncoder().encode('quo-ward-sign');
|
|
38
|
+
const WARD_SEAL = new TextEncoder().encode('quo-ward-seal');
|
|
39
|
+
|
|
31
40
|
// The ward's key from its seed. Its pk on the wire is the signing pk then the padlock, 128 hex.
|
|
41
|
+
//
|
|
42
|
+
// One seed, two curves, and each secret derived from it under its own label.
|
|
43
|
+
// Fed the seed straight, the two scalars differ only because Ed25519 hashes
|
|
44
|
+
// what it is given and X25519 clamps it raw, which is an accident of the two
|
|
45
|
+
// designs and no separation at all: one secret would be doing two jobs with
|
|
46
|
+
// nothing said about it, and a second kit would have to reproduce a
|
|
47
|
+
// construction nobody named. HKDF-SHA-256 under a label is the separation
|
|
48
|
+
// said out loud, and it is what `vectors/framing.json` pins.
|
|
49
|
+
//
|
|
50
|
+
// Bytes are key material and text is not. A seed handed in as bytes of the
|
|
51
|
+
// key length is taken as it stands, which is what a harbor mints; anything
|
|
52
|
+
// else, of any length, is hashed to that length first. So a thirty-two
|
|
53
|
+
// character name is a name like any other, and not a key because it happened
|
|
54
|
+
// to be the right size.
|
|
32
55
|
export async function wardKey(seed: string | Uint8Array): Promise<WardKey> {
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const p = await sealingPair(seed32);
|
|
56
|
+
const seed32 = typeof seed === 'string' ? await sha256(utf8.encode(seed)) : seed.length === KEY ? seed : await sha256(seed);
|
|
57
|
+
const s = await signingPair(await derive(seed32, WARD_SIGN, KEY));
|
|
58
|
+
const p = await sealingPair(await derive(seed32, WARD_SEAL, KEY));
|
|
37
59
|
return { sign: s.secret, padlock: p.secret, signPk: s.pk, padlockPk: p.pk, pk: hex(s.pk) + hex(p.pk) };
|
|
38
60
|
}
|
|
39
61
|
export const wardSignPk = (pk: string): Uint8Array => unhex(pk.slice(0, KEY * 2));
|
|
40
62
|
export const wardPadlock = (pk: string): Uint8Array => unhex(pk.slice(KEY * 2));
|
|
41
63
|
export const isWardPk = (pk: unknown): pk is string => typeof pk === 'string' && /^[0-9a-f]{128}$/.test(pk);
|
|
64
|
+
// Thirty-two bytes as hex, which is how this kit writes every single key: a
|
|
65
|
+
// heir pk, a heir secret, a being's own key, and a digest, which is the same
|
|
66
|
+
// thirty-two bytes of SHA-256. A ward pk is two of these and has its own
|
|
67
|
+
// shape above. The two are spelled here and nowhere else, so a door, a
|
|
68
|
+
// stance, a harbor and a conformance suite all refuse the same string.
|
|
69
|
+
export const isHex = (v: unknown): v is string => typeof v === 'string' && /^[0-9a-f]{64}$/.test(v);
|
|
42
70
|
|
|
43
71
|
// A being's relation key: a seed, and the pk it signs as.
|
|
44
72
|
export async function beingKey(seed: Uint8Array): Promise<{ seed: string; pk: string }> {
|
|
@@ -82,15 +110,16 @@ export async function openAsk(bytes: Uint8Array, padlockSecret: Uint8Array): Pro
|
|
|
82
110
|
// The heir, or null for the public being. Absent is not null: a payload
|
|
83
111
|
// that never named one is malformed, like any other missing field.
|
|
84
112
|
const to = payload.to;
|
|
85
|
-
if (to !== null && !(
|
|
86
|
-
if (
|
|
87
|
-
if (payload.next !== null && !(
|
|
113
|
+
if (to !== null && !isHex(to)) return null;
|
|
114
|
+
if (!isHex(payload.by)) return null;
|
|
115
|
+
if (payload.next !== null && !isHex(payload.next)) return null;
|
|
88
116
|
if (payload.method !== undefined && typeof payload.method !== 'string') return null; // a name, or the empty ask. never a number, never an object.
|
|
89
117
|
if (payload.args !== undefined && (payload.args === null || typeof payload.args !== 'object' || Array.isArray(payload.args))) return null; // args are one object, or absent. a string or a list is not an ask.
|
|
118
|
+
if (payload.args !== undefined && cellFault(payload.args, 'args') !== null) return null; // and values all the way down: no key named __proto__, no nesting past the bound
|
|
90
119
|
if (!Number.isSafeInteger(payload.seq) || payload.seq < 1) return null; // her count for this relation. one and up, and a whole number.
|
|
91
|
-
// The allowance
|
|
92
|
-
//
|
|
93
|
-
|
|
120
|
+
// The allowance is not read here. It is the door's, D2, and the door is
|
|
121
|
+
// its one reader: `spent` in allowance.ts says what a budget is and what
|
|
122
|
+
// is left of it, in one sentence, for a payload that opened like any other.
|
|
94
123
|
// The hop count, if this ask carries one: a whole number, never below
|
|
95
124
|
// zero. Absent is the ordinary ask and always will be.
|
|
96
125
|
if (payload.hops !== undefined && (!Number.isSafeInteger(payload.hops) || payload.hops < 0)) return null;
|
|
@@ -128,7 +157,7 @@ export async function openReply(bytes: unknown, ephemeralSecret: Uint8Array, sig
|
|
|
128
157
|
if (r.silence === true) return { silence: true };
|
|
129
158
|
if (Object.hasOwn(r, 'quo')) return isDoorWord(r.quo) ? { quo: r.quo } : null;
|
|
130
159
|
if (!Object.hasOwn(r, 'object') || r.object === undefined) return null;
|
|
131
|
-
if (r.seen !== null && !(
|
|
160
|
+
if (r.seen !== null && !isHex(r.seen)) return null;
|
|
132
161
|
return { object: r.object as Json, seen: r.seen };
|
|
133
162
|
} catch {
|
|
134
163
|
return null;
|