@motebit/crypto 1.1.0 → 1.2.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/dist/skills.js ADDED
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Skill manifest + envelope signing and verification — spec/skills-v1.md.
3
+ *
4
+ * Skills are motebit-internal protocol artifacts. They install locally,
5
+ * verify on motebit runtimes, and are signed by their author's motebit
6
+ * identity using `motebit-jcs-ed25519-b64-v1` — the same suite used for
7
+ * execution receipts, tool invocation receipts, settlement anchors, and
8
+ * migration artifacts. NOT W3C `eddsa-jcs-2022` (that suite is reserved for
9
+ * credentials, identity files, and presentations needing third-party W3C
10
+ * interop).
11
+ *
12
+ * Signature recipe (spec §5.1):
13
+ *
14
+ * manifest_bytes = JCS(manifest_with_signature_value_removed) || 0x0A || lf_body
15
+ * envelope_bytes = JCS(envelope_with_signature_value_removed)
16
+ * signature = Ed25519.sign(bytes, privateKey) -- routed through verifyBySuite
17
+ *
18
+ * Verification is offline: no relay, no registry, no external service. Per
19
+ * @motebit/crypto/CLAUDE.md rule 4, third-party verifiers using only this
20
+ * package and the signer's public key can validate any motebit-signed skill.
21
+ */
22
+ import { canonicalJson, fromBase64Url, hexToBytes, signBySuite, toBase64Url, verifyBySuite, } from "./signing.js";
23
+ /** The suite skills sign under in v1. Mirrors EXECUTION_RECEIPT_SUITE. */
24
+ export const SKILL_SIGNATURE_SUITE = "motebit-jcs-ed25519-b64-v1";
25
+ // ---------------------------------------------------------------------------
26
+ // Canonicalization — pure, deterministic, dependency-free
27
+ // ---------------------------------------------------------------------------
28
+ /**
29
+ * Strip `signature.value` from a SkillSignature, preserving `suite` and
30
+ * `public_key` (those ARE part of the signed canonical form per spec §5.1).
31
+ */
32
+ function signatureWithoutValue(sig) {
33
+ return { suite: sig.suite, public_key: sig.public_key };
34
+ }
35
+ /**
36
+ * Compute the canonical bytes that a SkillManifest signature is computed over.
37
+ * `body` is the LF-normalized SKILL.md body bytes (everything after the
38
+ * closing `---` delimiter), with CRLF/CR converted to LF, no BOM, UTF-8.
39
+ *
40
+ * Caller is responsible for body normalization. The reference parser in
41
+ * @motebit/skills (BSL) does the normalization at file-read time.
42
+ *
43
+ * The manifest's `motebit.signature.value` is removed before canonicalization;
44
+ * `suite` and `public_key` are preserved (they are signature-bound).
45
+ */
46
+ export function canonicalizeSkillManifestBytes(manifest, body) {
47
+ const motebitForCanonical = manifest.motebit.signature
48
+ ? {
49
+ ...manifest.motebit,
50
+ signature: signatureWithoutValue(manifest.motebit.signature),
51
+ }
52
+ : manifest.motebit;
53
+ const manifestForCanonical = { ...manifest, motebit: motebitForCanonical };
54
+ const canonical = canonicalJson(manifestForCanonical);
55
+ const manifestBytes = new TextEncoder().encode(canonical);
56
+ const out = new Uint8Array(manifestBytes.length + 1 + body.length);
57
+ out.set(manifestBytes, 0);
58
+ out[manifestBytes.length] = 0x0a; // LF separator
59
+ out.set(body, manifestBytes.length + 1);
60
+ return out;
61
+ }
62
+ /**
63
+ * Compute the canonical bytes that a SkillEnvelope signature is computed
64
+ * over. Sibling to manifest canonicalization; envelope is pure JSON so no
65
+ * body concatenation.
66
+ */
67
+ export function canonicalizeSkillEnvelopeBytes(envelope) {
68
+ const envelopeForCanonical = {
69
+ ...envelope,
70
+ signature: signatureWithoutValue(envelope.signature),
71
+ };
72
+ const canonical = canonicalJson(envelopeForCanonical);
73
+ return new TextEncoder().encode(canonical);
74
+ }
75
+ // ---------------------------------------------------------------------------
76
+ // Signing — for skill authors
77
+ // ---------------------------------------------------------------------------
78
+ /**
79
+ * Sign a SkillManifest, returning the signed manifest with `motebit.signature`
80
+ * populated. Caller provides the LF-normalized body bytes; signing recipe is
81
+ * spec §5.1.
82
+ *
83
+ * The returned manifest's `motebit.signature.public_key` is hex-encoded and
84
+ * `motebit.signature.value` is base64url-encoded, matching the suite contract.
85
+ */
86
+ export async function signSkillManifest(unsigned, privateKey, publicKey, body) {
87
+ const publicKeyHex = bytesToLowerHex(publicKey);
88
+ // Stamp the unsigned signature block (suite + public_key) so it participates
89
+ // in canonicalization at sign time.
90
+ const unsignedWithSig = {
91
+ ...unsigned,
92
+ motebit: {
93
+ ...unsigned.motebit,
94
+ signature: {
95
+ suite: SKILL_SIGNATURE_SUITE,
96
+ public_key: publicKeyHex,
97
+ value: "", // placeholder; stripped by canonicalize
98
+ },
99
+ },
100
+ };
101
+ const message = canonicalizeSkillManifestBytes(unsignedWithSig, body);
102
+ const sig = await signBySuite(SKILL_SIGNATURE_SUITE, message, privateKey);
103
+ return {
104
+ ...unsigned,
105
+ motebit: {
106
+ ...unsigned.motebit,
107
+ signature: {
108
+ suite: SKILL_SIGNATURE_SUITE,
109
+ public_key: publicKeyHex,
110
+ value: toBase64Url(sig),
111
+ },
112
+ },
113
+ };
114
+ }
115
+ /**
116
+ * Sign a SkillEnvelope, returning the signed envelope. Sibling to
117
+ * `signSkillManifest`. Envelope canonicalization is pure JSON (§5.1).
118
+ */
119
+ export async function signSkillEnvelope(unsigned, privateKey, publicKey) {
120
+ const publicKeyHex = bytesToLowerHex(publicKey);
121
+ const unsignedWithSig = {
122
+ ...unsigned,
123
+ signature: {
124
+ suite: SKILL_SIGNATURE_SUITE,
125
+ public_key: publicKeyHex,
126
+ value: "", // placeholder; stripped by canonicalize
127
+ },
128
+ };
129
+ const message = canonicalizeSkillEnvelopeBytes(unsignedWithSig);
130
+ const sig = await signBySuite(SKILL_SIGNATURE_SUITE, message, privateKey);
131
+ return {
132
+ ...unsigned,
133
+ signature: {
134
+ suite: SKILL_SIGNATURE_SUITE,
135
+ public_key: publicKeyHex,
136
+ value: toBase64Url(sig),
137
+ },
138
+ };
139
+ }
140
+ // ---------------------------------------------------------------------------
141
+ // Verification — fail-closed on every error path
142
+ // ---------------------------------------------------------------------------
143
+ /**
144
+ * Verify a SkillManifest's signature against the provided public key and
145
+ * LF-normalized body bytes. Returns `false` on any failure path:
146
+ *
147
+ * - manifest is unsigned (`motebit.signature` absent)
148
+ * - suite mismatch (only `motebit-jcs-ed25519-b64-v1` accepted in v1)
149
+ * - public_key in the signature doesn't match the supplied public key
150
+ * - signature value fails base64url decode
151
+ * - Ed25519 verification fails
152
+ *
153
+ * Per CLAUDE.md rule 4, this is the third-party self-verification entry
154
+ * point. Only this package + the signer's public key are required.
155
+ */
156
+ export async function verifySkillManifest(manifest, body, publicKey) {
157
+ return (await verifySkillManifestDetailed(manifest, body, publicKey)).valid;
158
+ }
159
+ /**
160
+ * Companion to `verifySkillManifest` that returns a categorized failure
161
+ * reason for observability. Same recipe; same fail-closed behavior.
162
+ */
163
+ export async function verifySkillManifestDetailed(manifest, body, publicKey) {
164
+ const sig = manifest.motebit.signature;
165
+ if (!sig)
166
+ return { valid: false, reason: "no_signature" };
167
+ if (sig.suite !== SKILL_SIGNATURE_SUITE)
168
+ return { valid: false, reason: "wrong_suite" };
169
+ const publicKeyHex = bytesToLowerHex(publicKey);
170
+ if (sig.public_key.toLowerCase() !== publicKeyHex) {
171
+ return { valid: false, reason: "bad_public_key" };
172
+ }
173
+ let sigBytes;
174
+ try {
175
+ sigBytes = fromBase64Url(sig.value);
176
+ }
177
+ catch {
178
+ return { valid: false, reason: "bad_signature_value" };
179
+ }
180
+ const message = canonicalizeSkillManifestBytes(manifest, body);
181
+ const valid = await verifyBySuite(SKILL_SIGNATURE_SUITE, message, sigBytes, publicKey);
182
+ return { valid, reason: valid ? "ok" : "ed25519_mismatch" };
183
+ }
184
+ /**
185
+ * Verify a SkillEnvelope's signature. Sibling to `verifySkillManifest` —
186
+ * same suite, same fail-closed semantics. Note: envelope verification does
187
+ * NOT cross-check `body_hash` or `files[].hash` against on-disk bytes; the
188
+ * caller (install-time logic in the registry) is responsible for that pass
189
+ * after signature verification succeeds.
190
+ */
191
+ export async function verifySkillEnvelope(envelope, publicKey) {
192
+ return (await verifySkillEnvelopeDetailed(envelope, publicKey)).valid;
193
+ }
194
+ export async function verifySkillEnvelopeDetailed(envelope, publicKey) {
195
+ const sig = envelope.signature;
196
+ if (sig.suite !== SKILL_SIGNATURE_SUITE)
197
+ return { valid: false, reason: "wrong_suite" };
198
+ const publicKeyHex = bytesToLowerHex(publicKey);
199
+ if (sig.public_key.toLowerCase() !== publicKeyHex) {
200
+ return { valid: false, reason: "bad_public_key" };
201
+ }
202
+ let sigBytes;
203
+ try {
204
+ sigBytes = fromBase64Url(sig.value);
205
+ }
206
+ catch {
207
+ return { valid: false, reason: "bad_signature_value" };
208
+ }
209
+ const message = canonicalizeSkillEnvelopeBytes(envelope);
210
+ const valid = await verifyBySuite(SKILL_SIGNATURE_SUITE, message, sigBytes, publicKey);
211
+ return { valid, reason: valid ? "ok" : "ed25519_mismatch" };
212
+ }
213
+ // ---------------------------------------------------------------------------
214
+ // Helpers
215
+ // ---------------------------------------------------------------------------
216
+ /** Hex-encode a 32-byte public key as 64 lowercase hex chars. */
217
+ function bytesToLowerHex(bytes) {
218
+ let hex = "";
219
+ for (const b of bytes) {
220
+ hex += b.toString(16).padStart(2, "0");
221
+ }
222
+ return hex;
223
+ }
224
+ /** Decode a hex public key from a SkillSignature for caller convenience. */
225
+ export function decodeSkillSignaturePublicKey(sig) {
226
+ return hexToBytes(sig.public_key);
227
+ }
228
+ //# sourceMappingURL=skills.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skills.js","sourceRoot":"","sources":["../src/skills.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,WAAW,EACX,WAAW,EACX,aAAa,GACd,MAAM,cAAc,CAAC;AAEtB,0EAA0E;AAC1E,MAAM,CAAC,MAAM,qBAAqB,GAAG,4BAAqC,CAAC;AAgB3E,8EAA8E;AAC9E,0DAA0D;AAC1D,8EAA8E;AAE9E;;;GAGG;AACH,SAAS,qBAAqB,CAAC,GAAmB;IAChD,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,CAAC,UAAU,EAA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