bridle-core 0.1.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.
Files changed (48) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +14 -0
  3. package/dist/src/envelope.d.ts +101 -0
  4. package/dist/src/envelope.d.ts.map +1 -0
  5. package/dist/src/envelope.js +137 -0
  6. package/dist/src/envelope.js.map +1 -0
  7. package/dist/src/identity.d.ts +31 -0
  8. package/dist/src/identity.d.ts.map +1 -0
  9. package/dist/src/identity.js +63 -0
  10. package/dist/src/identity.js.map +1 -0
  11. package/dist/src/index.d.ts +8 -0
  12. package/dist/src/index.d.ts.map +1 -0
  13. package/dist/src/index.js +8 -0
  14. package/dist/src/index.js.map +1 -0
  15. package/dist/src/policy.d.ts +59 -0
  16. package/dist/src/policy.d.ts.map +1 -0
  17. package/dist/src/policy.js +190 -0
  18. package/dist/src/policy.js.map +1 -0
  19. package/dist/src/redact.d.ts +8 -0
  20. package/dist/src/redact.d.ts.map +1 -0
  21. package/dist/src/redact.js +54 -0
  22. package/dist/src/redact.js.map +1 -0
  23. package/dist/src/render.d.ts +15 -0
  24. package/dist/src/render.d.ts.map +1 -0
  25. package/dist/src/render.js +44 -0
  26. package/dist/src/render.js.map +1 -0
  27. package/dist/src/seal.d.ts +32 -0
  28. package/dist/src/seal.d.ts.map +1 -0
  29. package/dist/src/seal.js +60 -0
  30. package/dist/src/seal.js.map +1 -0
  31. package/dist/src/transport.d.ts +14 -0
  32. package/dist/src/transport.d.ts.map +1 -0
  33. package/dist/src/transport.js +34 -0
  34. package/dist/src/transport.js.map +1 -0
  35. package/dist/test/core.test.d.ts +2 -0
  36. package/dist/test/core.test.d.ts.map +1 -0
  37. package/dist/test/core.test.js +310 -0
  38. package/dist/test/core.test.js.map +1 -0
  39. package/dist/tsconfig.tsbuildinfo +1 -0
  40. package/package.json +46 -0
  41. package/src/envelope.ts +196 -0
  42. package/src/identity.ts +81 -0
  43. package/src/index.ts +7 -0
  44. package/src/policy.ts +243 -0
  45. package/src/redact.ts +59 -0
  46. package/src/render.ts +50 -0
  47. package/src/seal.ts +100 -0
  48. package/src/transport.ts +31 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,14 @@
1
+ Bridle
2
+ Copyright 2026 Shape (shape-labs.com)
3
+
4
+ This product includes software developed at Shape.
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -0,0 +1,101 @@
1
+ import type { SealedBox } from "./seal.js";
2
+ /**
3
+ * The four verbs. This list is closed on purpose: a protocol that can express
4
+ * anything is a protocol you cannot write a policy for.
5
+ */
6
+ export declare const VERBS: readonly ["context.push", "task.queue", "state.read", "run.request"];
7
+ export type Verb = (typeof VERBS)[number];
8
+ export type Verdict = "allow" | "ask" | "deny";
9
+ /** Where a piece of handed-off work has got to. */
10
+ export declare const TASK_STATUSES: readonly ["accepted", "working", "done", "blocked"];
11
+ export type TaskStatus = (typeof TASK_STATUSES)[number];
12
+ export interface ContextPush {
13
+ note?: string;
14
+ decision?: string;
15
+ /** Present when this push reports on work someone handed you. */
16
+ status?: TaskStatus;
17
+ files?: {
18
+ path: string;
19
+ sha256: string;
20
+ bytes: number;
21
+ content?: string;
22
+ }[];
23
+ links?: string[];
24
+ }
25
+ export interface TaskQueue {
26
+ title: string;
27
+ detail?: string;
28
+ repo?: string;
29
+ }
30
+ export interface StateRead {
31
+ fields: ("branch" | "diffstat" | "openFiles" | "task")[];
32
+ }
33
+ export interface RunRequest {
34
+ command: string;
35
+ cwd?: string;
36
+ reason?: string;
37
+ }
38
+ export type Payload = ContextPush | TaskQueue | StateRead | RunRequest;
39
+ export interface Envelope {
40
+ /** Protocol version. Bumped only for breaking envelope changes. */
41
+ v: 1;
42
+ id: string;
43
+ ts: string;
44
+ verb: Verb;
45
+ /** Sending node name, e.g. "marko.dev". */
46
+ from: string;
47
+ /** Sender's Ed25519 public key, base64. Carried so a receiver can verify offline. */
48
+ fromKey: string;
49
+ /** Receiving node name. */
50
+ to: string;
51
+ scope: {
52
+ repo?: string;
53
+ tools?: string[];
54
+ };
55
+ /**
56
+ * The envelope this one answers. Set when reporting back on work someone
57
+ * handed you, so the sender can correlate a reply with what they asked for.
58
+ */
59
+ ref?: string;
60
+ /** Present before sealing and after opening. Never on the wire. */
61
+ payload?: Payload;
62
+ /** What actually crosses the relay. The relay cannot open this. */
63
+ sealed?: SealedBox;
64
+ /** Base64 Ed25519 signature over the canonical form of every field above. */
65
+ sig?: string;
66
+ }
67
+ /**
68
+ * Deterministic serialisation for signing. Keys are sorted at every level so
69
+ * two implementations in two languages agree byte-for-byte.
70
+ */
71
+ export declare function canonical(value: unknown): string;
72
+ /** The exact bytes that get signed: the envelope minus its own signature. */
73
+ export declare function signingBytes(env: Envelope): Buffer;
74
+ export declare function envelopeDigest(env: Envelope): string;
75
+ export interface DraftEnvelope {
76
+ ref?: string;
77
+ verb: Verb;
78
+ from: string;
79
+ fromKey: string;
80
+ to: string;
81
+ payload: Payload;
82
+ scope?: {
83
+ repo?: string;
84
+ tools?: string[];
85
+ };
86
+ /** Injectable for tests; defaults to now. */
87
+ ts?: string;
88
+ id?: string;
89
+ }
90
+ export declare function createEnvelope(draft: DraftEnvelope): Envelope;
91
+ export declare class EnvelopeError extends Error {
92
+ }
93
+ /**
94
+ * Structural validation. This runs before anything else touches an envelope —
95
+ * a malformed envelope is rejected, never "best-effort" interpreted.
96
+ */
97
+ export declare function validateEnvelope(input: unknown): Envelope;
98
+ export declare function validatePayload(verb: Verb, p: Record<string, unknown>): void;
99
+ /** Size of what is actually carried — ciphertext while sealed, plaintext once open. */
100
+ export declare function payloadBytes(env: Envelope): number;
101
+ //# sourceMappingURL=envelope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../../src/envelope.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C;;;GAGG;AACH,eAAO,MAAM,KAAK,sEAAuE,CAAC;AAC1F,MAAM,MAAM,IAAI,GAAG,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1C,MAAM,MAAM,OAAO,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;AAE/C,mDAAmD;AACnD,eAAO,MAAM,aAAa,qDAAsD,CAAC;AACjF,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAExD,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,KAAK,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC5E,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AACD,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AACD,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,CAAC,QAAQ,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,CAAC,EAAE,CAAC;CAC1D;AACD,MAAM,WAAW,UAAU;IACzB,OAAO,EA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AI,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAC5C,6CAA6C;IAC7C,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,GAAG,QAAQ,CAa7D;AAED,qBAAa,aAAc,SAAQ,KAAK;CAAG;AAI3C;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,QAAQ,CAgCzD;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CA+B5E;AAED,uFAAuF;AACvF,wBAAgB,YAAY,CAAC,GAAG,EAAE,QAAQ,GAAG,MAAM,CAGlD"}
@@ -0,0 +1,137 @@
1
+ import { randomUUID, createHash } from "node:crypto";
2
+ /**
3
+ * The four verbs. This list is closed on purpose: a protocol that can express
4
+ * anything is a protocol you cannot write a policy for.
5
+ */
6
+ export const VERBS = ["context.push", "task.queue", "state.read", "run.request"];
7
+ /** Where a piece of handed-off work has got to. */
8
+ export const TASK_STATUSES = ["accepted", "working", "done", "blocked"];
9
+ /**
10
+ * Deterministic serialisation for signing. Keys are sorted at every level so
11
+ * two implementations in two languages agree byte-for-byte.
12
+ */
13
+ export function canonical(value) {
14
+ if (value === null || typeof value !== "object")
15
+ return JSON.stringify(value) ?? "null";
16
+ if (Array.isArray(value))
17
+ return "[" + value.map(canonical).join(",") + "]";
18
+ const obj = value;
19
+ const keys = Object.keys(obj).filter((k) => obj[k] !== undefined).sort();
20
+ return "{" + keys.map((k) => JSON.stringify(k) + ":" + canonical(obj[k])).join(",") + "}";
21
+ }
22
+ /** The exact bytes that get signed: the envelope minus its own signature. */
23
+ export function signingBytes(env) {
24
+ const { sig: _drop, ...rest } = env;
25
+ return Buffer.from(canonical(rest), "utf8");
26
+ }
27
+ export function envelopeDigest(env) {
28
+ return createHash("sha256").update(signingBytes(env)).digest("hex");
29
+ }
30
+ export function createEnvelope(draft) {
31
+ return {
32
+ v: 1,
33
+ id: draft.id ?? randomUUID(),
34
+ ts: draft.ts ?? new Date().toISOString(),
35
+ verb: draft.verb,
36
+ from: draft.from,
37
+ fromKey: draft.fromKey,
38
+ to: draft.to,
39
+ scope: draft.scope ?? {},
40
+ ...(draft.ref ? { ref: draft.ref } : {}),
41
+ payload: draft.payload,
42
+ };
43
+ }
44
+ export class EnvelopeError extends Error {
45
+ }
46
+ const NAME_RE = /^[a-z0-9][a-z0-9._-]{1,62}$/i;
47
+ /**
48
+ * Structural validation. This runs before anything else touches an envelope —
49
+ * a malformed envelope is rejected, never "best-effort" interpreted.
50
+ */
51
+ export function validateEnvelope(input) {
52
+ if (typeof input !== "object" || input === null)
53
+ throw new EnvelopeError("envelope must be an object");
54
+ const e = input;
55
+ if (e.v !== 1)
56
+ throw new EnvelopeError(`unsupported envelope version: ${String(e.v)}`);
57
+ for (const field of ["id", "ts", "from", "fromKey", "to"]) {
58
+ if (typeof e[field] !== "string" || !e[field].length) {
59
+ throw new EnvelopeError(`missing or invalid field: ${field}`);
60
+ }
61
+ }
62
+ if (!VERBS.includes(e.verb))
63
+ throw new EnvelopeError(`unknown verb: ${String(e.verb)}`);
64
+ if (!NAME_RE.test(e.from))
65
+ throw new EnvelopeError("invalid sender name");
66
+ if (!NAME_RE.test(e.to))
67
+ throw new EnvelopeError("invalid recipient name");
68
+ if (Number.isNaN(Date.parse(e.ts)))
69
+ throw new EnvelopeError("invalid timestamp");
70
+ if (e.ref !== undefined && (typeof e.ref !== "string" || !e.ref.length)) {
71
+ throw new EnvelopeError("ref must be an envelope id");
72
+ }
73
+ if (typeof e.scope !== "object" || e.scope === null)
74
+ throw new EnvelopeError("scope must be an object");
75
+ const hasPayload = typeof e.payload === "object" && e.payload !== null;
76
+ const hasSealed = typeof e.sealed === "object" && e.sealed !== null;
77
+ if (!hasPayload && !hasSealed)
78
+ throw new EnvelopeError("envelope carries neither a payload nor a sealed box");
79
+ if (hasPayload && hasSealed)
80
+ throw new EnvelopeError("envelope carries both a payload and a sealed box");
81
+ if (hasSealed) {
82
+ for (const f of ["epk", "iv", "ct", "tag"]) {
83
+ if (typeof e.sealed[f] !== "string") {
84
+ throw new EnvelopeError(`sealed box is missing ${f}`);
85
+ }
86
+ }
87
+ }
88
+ if (hasPayload)
89
+ validatePayload(e.verb, e.payload);
90
+ return e;
91
+ }
92
+ export function validatePayload(verb, p) {
93
+ const str = (v) => typeof v === "string";
94
+ switch (verb) {
95
+ case "context.push":
96
+ if (p.note !== undefined && !str(p.note))
97
+ throw new EnvelopeError("context.push.note must be a string");
98
+ if (p.decision !== undefined && !str(p.decision))
99
+ throw new EnvelopeError("context.push.decision must be a string");
100
+ if (p.files !== undefined && !Array.isArray(p.files))
101
+ throw new EnvelopeError("context.push.files must be an array");
102
+ if (p.links !== undefined && !Array.isArray(p.links))
103
+ throw new EnvelopeError("context.push.links must be an array");
104
+ if (p.status !== undefined && !TASK_STATUSES.includes(p.status)) {
105
+ throw new EnvelopeError(`unknown status: ${String(p.status)}`);
106
+ }
107
+ if (p.note === undefined && p.decision === undefined && p.files === undefined &&
108
+ p.links === undefined && p.status === undefined) {
109
+ throw new EnvelopeError("context.push needs at least one of: note, decision, files, links, status");
110
+ }
111
+ return;
112
+ case "task.queue":
113
+ if (!str(p.title) || !p.title.trim())
114
+ throw new EnvelopeError("task.queue.title is required");
115
+ return;
116
+ case "state.read": {
117
+ if (!Array.isArray(p.fields) || p.fields.length === 0)
118
+ throw new EnvelopeError("state.read.fields is required");
119
+ const allowed = new Set(["branch", "diffstat", "openFiles", "task"]);
120
+ for (const f of p.fields)
121
+ if (!allowed.has(f))
122
+ throw new EnvelopeError(`unknown state field: ${String(f)}`);
123
+ return;
124
+ }
125
+ case "run.request":
126
+ if (!str(p.command) || !p.command.trim())
127
+ throw new EnvelopeError("run.request.command is required");
128
+ return;
129
+ }
130
+ }
131
+ /** Size of what is actually carried — ciphertext while sealed, plaintext once open. */
132
+ export function payloadBytes(env) {
133
+ if (env.sealed)
134
+ return Buffer.from(env.sealed.ct, "base64").byteLength;
135
+ return Buffer.byteLength(canonical(env.payload), "utf8");
136
+ }
137
+ //# sourceMappingURL=envelope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.js","sourceRoot":"","sources":["../../src/envelope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGrD;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,CAAU,CAAC;AAK1F,mDAAmD;AACnD,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,CAAU,CAAC;AAqDjF;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC;IACxF,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IAC5E,MAAM,GAAG,GAAG,KAAgC,CAAC;IAC7C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC;IACzE,OAAO,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CA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I,EAAE;gBAAE,MAAM,IAAI,aAAa,CAAC,8BAA8B,CAAC,CAAC;YAC1G,OAAO;QACT,KAAK,YAAY,CAAC,CAAC,CAAC;YAClB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC;gBAAE,MAAM,IAAI,aAAa,CAAC,+BAA+B,CAAC,CAAC;YAChH,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC;YACrE,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM;gBAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAW,CAAC;oBAAE,MAAM,IAAI,aAAa,CAAC,wBAAwB,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACtH,OAAO;QACT,CAAC;QACD,KAAK,aAAa;YAChB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAE,CAAC,CAAC,OAAkB,CAAC,IAAI,EAAE;gBAAE,MAAM,IAAI,aAAa,CAAC,iCAAiC,CAAC,CAAC;YACjH,OAAO;IACX,CAAC;AACH,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,YAAY,CAAC,GAAa;IACxC,IAAI,GAAG,CAAC,MAAM;QAAE,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC,UAAU,CAAC;IACvE,OAAO,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,CAAC;AAC3D,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { type Envelope } from "./envelope.js";
2
+ export interface Identity {
3
+ /** Node name inside the bridlenet, e.g. "marko.dev". */
4
+ name: string;
5
+ /** Ed25519 — proves who sent an envelope. */
6
+ publicKey: string;
7
+ privateKey: string;
8
+ /** X25519 — decides who can read one. */
9
+ sealPublicKey: string;
10
+ sealPrivateKey: string;
11
+ }
12
+ export declare function generateIdentity(name: string): Identity;
13
+ /** Short, human-comparable fingerprint — the thing two people read aloud when pairing. */
14
+ export declare function fingerprint(publicKeyB64: string): string;
15
+ export declare function signEnvelope(env: Envelope, identity: Identity): Envelope;
16
+ /**
17
+ * Verifies the signature against the key carried *in the envelope*. The caller
18
+ * must separately confirm that key is the one they granted a scope to —
19
+ * see `verifyFrom`.
20
+ */
21
+ export declare function verifyEnvelope(env: Envelope): boolean;
22
+ /**
23
+ * The check that actually matters: the signature is valid AND the key belongs to
24
+ * the peer we think we are talking to. Verifying the signature alone would let
25
+ * anyone claim to be anyone by bringing their own key.
26
+ */
27
+ export declare function verifyFrom(env: Envelope, expectedKeyB64: string | undefined): {
28
+ ok: boolean;
29
+ reason?: string;
30
+ };
31
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../src/identity.ts"],"names":[],"mappings":"AAQA,OAAO,EAAgB,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AAG5D,MAAM,WAAW,QAAQ;IACvB,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,yCAAyC;IACzC,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAUvD;AASD,0FAA0F;AAC1F,wBAAgB,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAIxD;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,GAAG,QAAQ,CAMxE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,QAAQ,GAAG,OAAO,CAOrD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,GAAG,SAAS,GAAG;IAAE,EAAE,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAK9G"}
@@ -0,0 +1,63 @@
1
+ import { generateKeyPairSync, sign as nodeSign, verify as nodeVerify, createPublicKey, createPrivateKey, } from "node:crypto";
2
+ import { signingBytes } from "./envelope.js";
3
+ import { generateSealingKeys } from "./seal.js";
4
+ export function generateIdentity(name) {
5
+ const { publicKey, privateKey } = generateKeyPairSync("ed25519");
6
+ const sealing = generateSealingKeys();
7
+ return {
8
+ name,
9
+ publicKey: publicKey.export({ type: "spki", format: "der" }).toString("base64"),
10
+ privateKey: privateKey.export({ type: "pkcs8", format: "der" }).toString("base64"),
11
+ sealPublicKey: sealing.publicKey,
12
+ sealPrivateKey: sealing.privateKey,
13
+ };
14
+ }
15
+ function toPublic(b64) {
16
+ return createPublicKey({ key: Buffer.from(b64, "base64"), format: "der", type: "spki" });
17
+ }
18
+ function toPrivate(b64) {
19
+ return createPrivateKey({ key: Buffer.from(b64, "base64"), format: "der", type: "pkcs8" });
20
+ }
21
+ /** Short, human-comparable fingerprint — the thing two people read aloud when pairing. */
22
+ export function fingerprint(publicKeyB64) {
23
+ const raw = Buffer.from(publicKeyB64, "base64").subarray(-32);
24
+ const hex = raw.toString("hex").slice(0, 16).toUpperCase();
25
+ return (hex.match(/.{4}/g) ?? []).join("-");
26
+ }
27
+ export function signEnvelope(env, identity) {
28
+ if (env.fromKey !== identity.publicKey) {
29
+ throw new Error("refusing to sign: envelope.fromKey does not match this identity");
30
+ }
31
+ const sig = nodeSign(null, signingBytes(env), toPrivate(identity.privateKey));
32
+ return { ...env, sig: sig.toString("base64") };
33
+ }
34
+ /**
35
+ * Verifies the signature against the key carried *in the envelope*. The caller
36
+ * must separately confirm that key is the one they granted a scope to —
37
+ * see `verifyFrom`.
38
+ */
39
+ export function verifyEnvelope(env) {
40
+ if (!env.sig)
41
+ return false;
42
+ try {
43
+ return nodeVerify(null, signingBytes(env), toPublic(env.fromKey), Buffer.from(env.sig, "base64"));
44
+ }
45
+ catch {
46
+ return false;
47
+ }
48
+ }
49
+ /**
50
+ * The check that actually matters: the signature is valid AND the key belongs to
51
+ * the peer we think we are talking to. Verifying the signature alone would let
52
+ * anyone claim to be anyone by bringing their own key.
53
+ */
54
+ export function verifyFrom(env, expectedKeyB64) {
55
+ if (!env.sig)
56
+ return { ok: false, reason: "envelope is unsigned" };
57
+ if (!expectedKeyB64)
58
+ return { ok: false, reason: `no known key for peer "${env.from}"` };
59
+ if (env.fromKey !== expectedKeyB64)
60
+ return { ok: false, reason: `key mismatch for peer "${env.from}"` };
61
+ return verifyEnvelope(env) ? { ok: true } : { ok: false, reason: "bad signature" };
62
+ }
63
+ //# sourceMappingURL=identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.js","sourceRoot":"","sources":["../../src/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,IAAI,IAAI,QAAQ,EAChB,MAAM,IAAI,UAAU,EACpB,eAAe,EACf,gBAAgB,GAEjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAiB,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAahD,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,mBAAmB,CAAC,SAAS,CAAC,CAAC;IACjE,MAAM,OAAO,GAAG,mBAAmB,EAAE,CAAC;IACtC,OAAO;QACL,IAAI;QACJ,SAAS,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ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E,CAAC;AACrF,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from "./envelope.js";
2
+ export * from "./identity.js";
3
+ export * from "./policy.js";
4
+ export * from "./redact.js";
5
+ export * from "./seal.js";
6
+ export * from "./transport.js";
7
+ export * from "./render.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from "./envelope.js";
2
+ export * from "./identity.js";
3
+ export * from "./policy.js";
4
+ export * from "./redact.js";
5
+ export * from "./seal.js";
6
+ export * from "./transport.js";
7
+ export * from "./render.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}