keymaster-connect 0.1.5 → 0.1.7
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 +11 -21
- package/dist/{chunk-ZDIFZJYD.js → chunk-D2RL3QGR.js} +5 -17
- package/dist/chunk-D2RL3QGR.js.map +1 -0
- package/dist/{chunk-VVOTW5ZT.js → chunk-M2YEI4PG.js} +92 -3
- package/dist/chunk-M2YEI4PG.js.map +1 -0
- package/dist/index.d.ts +14 -173
- package/dist/index.js +127 -548
- package/dist/index.js.map +1 -1
- package/dist/{protocol-C8z30CKK.d.ts → protocol-B7bpi5ro.d.ts} +43 -191
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.js +1 -1
- package/dist/script.d.ts +1 -1
- package/dist/server.d.ts +24 -17
- package/dist/server.js +112 -340
- package/dist/server.js.map +1 -1
- package/package.json +45 -11
- package/dist/chunk-VVOTW5ZT.js.map +0 -1
- package/dist/chunk-ZDIFZJYD.js.map +0 -1
package/dist/server.js
CHANGED
|
@@ -1,151 +1,6 @@
|
|
|
1
|
-
import { isAiToolMessage, parseToolDefinitions, validateToolArguments, assertToolPayload } from './chunk-
|
|
2
|
-
export { AI_TOOLS_MAX_JSON_CHARS, assertToolJson, assertToolPayload, isAiToolMessage, parseToolDefinitions, validateToolArguments } from './chunk-
|
|
3
|
-
import { PROTOCOL_VERSION, PROTOCOL_METHODS, validateNetworkIdentity, sameNetworkIdentity,
|
|
4
|
-
import { secp256k1 } from '@noble/curves/secp256k1.js';
|
|
5
|
-
import { sha256 } from '@noble/hashes/sha2.js';
|
|
6
|
-
|
|
7
|
-
var APP_IDENTITY_DOMAIN_V1 = "keymaster-app-identity:v1";
|
|
8
|
-
var AppIdentityValidationError = class extends Error {
|
|
9
|
-
constructor(message) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.name = "AppIdentityValidationError";
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
function exact(value, keys, field) {
|
|
15
|
-
const actual = Object.keys(value).sort();
|
|
16
|
-
const expected = [...keys].sort();
|
|
17
|
-
if (actual.length !== expected.length || actual.some((key, i) => key !== expected[i])) {
|
|
18
|
-
throw new AppIdentityValidationError(`${field} has unexpected fields`);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function text(value, field, max) {
|
|
22
|
-
const invalidControl = (c) => {
|
|
23
|
-
const code = c.charCodeAt(0);
|
|
24
|
-
return code <= 31 || code >= 127 && code <= 159 || code === 65533;
|
|
25
|
-
};
|
|
26
|
-
if (typeof value !== "string" || !value.trim() || value !== value.trim() || [...value].length > max || [...value].some(invalidControl)) {
|
|
27
|
-
throw new AppIdentityValidationError(`${field} is invalid`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
function hex(value, field, length) {
|
|
31
|
-
if (typeof value !== "string" || value.length !== length || !/^[0-9a-f]+$/u.test(value)) {
|
|
32
|
-
throw new AppIdentityValidationError(`${field} must be hex`);
|
|
33
|
-
}
|
|
34
|
-
const out = new Uint8Array(length / 2);
|
|
35
|
-
for (let i = 0; i < out.length; i++) out[i] = Number.parseInt(value.slice(i * 2, i * 2 + 2), 16);
|
|
36
|
-
return out;
|
|
37
|
-
}
|
|
38
|
-
function canonicalizeJson(value) {
|
|
39
|
-
if (value === null || typeof value === "boolean" || typeof value === "string") {
|
|
40
|
-
return JSON.stringify(value);
|
|
41
|
-
}
|
|
42
|
-
if (typeof value === "number") {
|
|
43
|
-
if (!Number.isFinite(value)) {
|
|
44
|
-
throw new AppIdentityValidationError("identity payload contains a non-finite number");
|
|
45
|
-
}
|
|
46
|
-
return JSON.stringify(value);
|
|
47
|
-
}
|
|
48
|
-
if (Array.isArray(value)) {
|
|
49
|
-
return `[${value.map(canonicalizeJson).join(",")}]`;
|
|
50
|
-
}
|
|
51
|
-
if (typeof value === "object") {
|
|
52
|
-
const object = value;
|
|
53
|
-
const entries = Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${canonicalizeJson(object[key])}`);
|
|
54
|
-
return `{${entries.join(",")}}`;
|
|
55
|
-
}
|
|
56
|
-
throw new AppIdentityValidationError("identity payload contains an unsupported value");
|
|
57
|
-
}
|
|
58
|
-
function identityPayload(proof) {
|
|
59
|
-
return {
|
|
60
|
-
version: 1,
|
|
61
|
-
publisherPublicKey: proof.publisherPublicKey,
|
|
62
|
-
app: { ...proof.app },
|
|
63
|
-
requirements: [...proof.requirements]
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
function identityDigestBytes(proof) {
|
|
67
|
-
const payload = new TextEncoder().encode(canonicalizeJson(identityPayload(proof)));
|
|
68
|
-
const domain = new TextEncoder().encode(APP_IDENTITY_DOMAIN_V1);
|
|
69
|
-
const signed = new Uint8Array(domain.length + 1 + payload.length);
|
|
70
|
-
signed.set(domain);
|
|
71
|
-
signed[domain.length] = 0;
|
|
72
|
-
signed.set(payload, domain.length + 1);
|
|
73
|
-
return sha256(signed);
|
|
74
|
-
}
|
|
75
|
-
function verifyAppIdentityProof(raw) {
|
|
76
|
-
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
77
|
-
throw new AppIdentityValidationError("appIdentity must be an object");
|
|
78
|
-
}
|
|
79
|
-
const proof = raw;
|
|
80
|
-
exact(proof, ["version", "publisherPublicKey", "app", "requirements", "signature"], "appIdentity");
|
|
81
|
-
if (proof.version !== 1 || typeof proof.publisherPublicKey !== "string" || !/^(02|03)[0-9a-f]{64}$/u.test(proof.publisherPublicKey)) {
|
|
82
|
-
throw new AppIdentityValidationError("publisherPublicKey is invalid");
|
|
83
|
-
}
|
|
84
|
-
try {
|
|
85
|
-
if (!secp256k1.utils.isValidPublicKey(hex(proof.publisherPublicKey, "publisherPublicKey", 66))) throw new Error();
|
|
86
|
-
} catch {
|
|
87
|
-
throw new AppIdentityValidationError("publisherPublicKey is invalid");
|
|
88
|
-
}
|
|
89
|
-
if (!proof.app || typeof proof.app !== "object" || Array.isArray(proof.app)) {
|
|
90
|
-
throw new AppIdentityValidationError("app must be an object");
|
|
91
|
-
}
|
|
92
|
-
const app = proof.app;
|
|
93
|
-
exact(app, ["id", "name", "description"], "app");
|
|
94
|
-
text(app.id, "app.id", 63);
|
|
95
|
-
text(app.name, "app.name", 120);
|
|
96
|
-
text(app.description, "app.description", 500);
|
|
97
|
-
if (!/^[a-z0-9](?:[a-z0-9._-]{0,61}[a-z0-9])?$/u.test(app.id)) {
|
|
98
|
-
throw new AppIdentityValidationError("app.id is invalid");
|
|
99
|
-
}
|
|
100
|
-
if (!Array.isArray(proof.requirements)) {
|
|
101
|
-
throw new AppIdentityValidationError("requirements must be an array");
|
|
102
|
-
}
|
|
103
|
-
const requirements = proof.requirements;
|
|
104
|
-
const allowed = /* @__PURE__ */ new Set(["private-key", "storage"]);
|
|
105
|
-
if (requirements.some((r) => typeof r !== "string" || !allowed.has(r)) || new Set(requirements).size !== requirements.length || requirements.some((r, i) => i > 0 && String(requirements[i - 1]) >= String(r))) {
|
|
106
|
-
throw new AppIdentityValidationError("requirements must be sorted and unique");
|
|
107
|
-
}
|
|
108
|
-
const signature = hex(proof.signature, "signature", 128);
|
|
109
|
-
const typed = {
|
|
110
|
-
version: 1,
|
|
111
|
-
publisherPublicKey: proof.publisherPublicKey,
|
|
112
|
-
app: { id: app.id, name: app.name, description: app.description },
|
|
113
|
-
requirements,
|
|
114
|
-
signature: proof.signature
|
|
115
|
-
};
|
|
116
|
-
try {
|
|
117
|
-
if (!secp256k1.verify(
|
|
118
|
-
signature,
|
|
119
|
-
identityDigestBytes(typed),
|
|
120
|
-
hex(proof.publisherPublicKey, "publisherPublicKey", 66),
|
|
121
|
-
{ prehash: false, format: "compact" }
|
|
122
|
-
)) throw new Error();
|
|
123
|
-
} catch {
|
|
124
|
-
throw new AppIdentityValidationError("appIdentity signature is invalid");
|
|
125
|
-
}
|
|
126
|
-
const digest = identityDigestBytes(typed);
|
|
127
|
-
return {
|
|
128
|
-
version: 1,
|
|
129
|
-
publisherPublicKeyHex: typed.publisherPublicKey,
|
|
130
|
-
appId: typed.app.id,
|
|
131
|
-
appName: typed.app.name,
|
|
132
|
-
identityDigestHex: Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("")
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
function isVerifiedAppIdentitySnapshot(value) {
|
|
136
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
137
|
-
const snapshot = value;
|
|
138
|
-
if (snapshot.version !== 1 || typeof snapshot.appId !== "string" || !/^[a-z0-9](?:[a-z0-9._-]{0,61}[a-z0-9])?$/u.test(snapshot.appId) || typeof snapshot.appName !== "string" || !snapshot.appName.trim() || snapshot.appName !== snapshot.appName.trim() || [...snapshot.appName].length > 120 || [...snapshot.appName].some((c) => /[\u0000-\u001f\u007f-\u009f\ufffd]/u.test(c)) || typeof snapshot.publisherPublicKeyHex !== "string" || !/^(02|03)[0-9a-f]{64}$/u.test(snapshot.publisherPublicKeyHex) || typeof snapshot.identityDigestHex !== "string" || !/^[0-9a-f]{64}$/u.test(snapshot.identityDigestHex)) {
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
try {
|
|
142
|
-
return secp256k1.utils.isValidPublicKey(
|
|
143
|
-
hex(snapshot.publisherPublicKeyHex, "publisherPublicKeyHex", 66)
|
|
144
|
-
);
|
|
145
|
-
} catch {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
1
|
+
import { isAiToolMessage, parseToolDefinitions, validateToolArguments, assertToolPayload, validatePublicationProof } from './chunk-M2YEI4PG.js';
|
|
2
|
+
export { AI_TOOLS_MAX_JSON_CHARS, assertToolJson, assertToolPayload, isAiToolMessage, parseToolDefinitions, validateToolArguments } from './chunk-M2YEI4PG.js';
|
|
3
|
+
import { PROTOCOL_VERSION, PROTOCOL_METHODS, validateNetworkIdentity, sameNetworkIdentity, isValidMsFileSourceId } from './chunk-D2RL3QGR.js';
|
|
149
4
|
|
|
150
5
|
// src/validation.ts
|
|
151
6
|
var ProtocolValidationError = class extends Error {
|
|
@@ -161,7 +16,7 @@ function validateIdentityGetParams(raw) {
|
|
|
161
16
|
const aud = expectString(obj.aud, "aud");
|
|
162
17
|
const iat = expectInteger(obj.iat, "iat");
|
|
163
18
|
const exp = expectInteger(obj.exp, "exp");
|
|
164
|
-
const
|
|
19
|
+
const text = expectString(obj.text, "text");
|
|
165
20
|
if (exp <= iat) {
|
|
166
21
|
throw new ProtocolValidationError("invalid_request", "exp must be greater than iat");
|
|
167
22
|
}
|
|
@@ -175,34 +30,34 @@ function validateIdentityGetParams(raw) {
|
|
|
175
30
|
}
|
|
176
31
|
}
|
|
177
32
|
}
|
|
178
|
-
return { aud, iat, exp, text
|
|
33
|
+
return { aud, iat, exp, text, claims: obj.claims };
|
|
179
34
|
}
|
|
180
35
|
function validateIntentSignParams(raw) {
|
|
181
36
|
const obj = expectObject(raw, "intent.sign params");
|
|
182
37
|
const aud = expectString(obj.aud, "aud");
|
|
183
38
|
const iat = expectInteger(obj.iat, "iat");
|
|
184
39
|
const exp = expectInteger(obj.exp, "exp");
|
|
185
|
-
const
|
|
40
|
+
const text = expectString(obj.text, "text");
|
|
186
41
|
const contentType = expectString(obj.contentType, "contentType");
|
|
187
42
|
if (exp <= iat) {
|
|
188
43
|
throw new ProtocolValidationError("invalid_request", "exp must be greater than iat");
|
|
189
44
|
}
|
|
190
45
|
const content = parseBinaryField(obj.content, "content");
|
|
191
|
-
return { aud, iat, exp, text
|
|
46
|
+
return { aud, iat, exp, text, contentType, content };
|
|
192
47
|
}
|
|
193
48
|
function validateCipherEncryptParams(raw) {
|
|
194
49
|
const obj = expectObject(raw, "cipher.encrypt params");
|
|
195
|
-
const
|
|
50
|
+
const text = expectString(obj.text, "text");
|
|
196
51
|
const contentType = expectString(obj.contentType, "contentType");
|
|
197
52
|
const content = parseBinaryField(obj.content, "content");
|
|
198
|
-
return { text
|
|
53
|
+
return { text, contentType, content };
|
|
199
54
|
}
|
|
200
55
|
function validateCipherDecryptParams(raw) {
|
|
201
56
|
const obj = expectObject(raw, "cipher.decrypt params");
|
|
202
|
-
const
|
|
57
|
+
const text = expectString(obj.text, "text");
|
|
203
58
|
const nonce = parseBinaryField(obj.nonce, "nonce");
|
|
204
59
|
const cipherbytes = parseBinaryField(obj.cipherbytes, "cipherbytes");
|
|
205
|
-
return { text
|
|
60
|
+
return { text, nonce, cipherbytes };
|
|
206
61
|
}
|
|
207
62
|
function assertOriginMatches(aud, eventOrigin) {
|
|
208
63
|
if (aud !== eventOrigin) {
|
|
@@ -248,9 +103,9 @@ function validatePaymentStatusParams(raw) {
|
|
|
248
103
|
function validateConnectLoginParams(raw) {
|
|
249
104
|
const obj = expectObject(raw, "connect.login params");
|
|
250
105
|
for (const key of Object.keys(obj))
|
|
251
|
-
if (key !== "text" && key !== "claims" && key !== "
|
|
106
|
+
if (key !== "text" && key !== "claims" && key !== "publicationProof")
|
|
252
107
|
throw new ProtocolValidationError("invalid_request", "connect.login contains an unsupported field");
|
|
253
|
-
const
|
|
108
|
+
const text = expectString(obj.text, "text");
|
|
254
109
|
let claims;
|
|
255
110
|
if (obj.claims !== void 0) {
|
|
256
111
|
if (!Array.isArray(obj.claims)) {
|
|
@@ -260,32 +115,15 @@ function validateConnectLoginParams(raw) {
|
|
|
260
115
|
throw new ProtocolValidationError("invalid_request", "claims entries must be strings");
|
|
261
116
|
claims = [...obj.claims];
|
|
262
117
|
}
|
|
263
|
-
let
|
|
264
|
-
if (obj.
|
|
118
|
+
let publicationProof;
|
|
119
|
+
if (obj.publicationProof !== void 0) {
|
|
265
120
|
try {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
throw new ProtocolValidationError("invalid_request", error instanceof AppIdentityValidationError ? error.message : "appIdentity is invalid");
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return { text: text2, claims, ...appIdentity ? { appIdentity } : {} };
|
|
273
|
-
}
|
|
274
|
-
function validateConnectResumeParams(raw) {
|
|
275
|
-
const obj = expectObject(raw, "connect.resume params");
|
|
276
|
-
for (const field of Object.keys(obj))
|
|
277
|
-
if (field !== "appIdentity")
|
|
278
|
-
throw new ProtocolValidationError("invalid_request", "connect.resume contains an unsupported field");
|
|
279
|
-
let appIdentity;
|
|
280
|
-
if (obj.appIdentity !== void 0) {
|
|
281
|
-
try {
|
|
282
|
-
verifyAppIdentityProof(obj.appIdentity);
|
|
283
|
-
appIdentity = obj.appIdentity;
|
|
284
|
-
} catch (error) {
|
|
285
|
-
throw new ProtocolValidationError("invalid_request", error instanceof Error ? error.message : "appIdentity is invalid");
|
|
121
|
+
publicationProof = validatePublicationProof(obj.publicationProof);
|
|
122
|
+
} catch {
|
|
123
|
+
throw new ProtocolValidationError("invalid_request", "publicationProof is invalid");
|
|
286
124
|
}
|
|
287
125
|
}
|
|
288
|
-
return
|
|
126
|
+
return publicationProof === void 0 ? { text, claims } : { text, claims, publicationProof };
|
|
289
127
|
}
|
|
290
128
|
function validateConnectLogoutParams(raw) {
|
|
291
129
|
expectObject(raw, "connect.logout params");
|
|
@@ -293,20 +131,17 @@ function validateConnectLogoutParams(raw) {
|
|
|
293
131
|
}
|
|
294
132
|
function validateConnectLaunchParams(raw) {
|
|
295
133
|
const obj = expectObject(raw, "connect.launch params");
|
|
296
|
-
if (Object.keys(obj).some((key) => key !== "launchToken" && key !== "
|
|
134
|
+
if (Object.keys(obj).some((key) => key !== "launchToken" && key !== "publicationProof")) {
|
|
297
135
|
throw new ProtocolValidationError("invalid_request", "connect.launch contains an unsupported field");
|
|
298
136
|
}
|
|
299
137
|
const launchToken = expectNonEmptyString(obj.launchToken, "launchToken");
|
|
300
|
-
if (obj.
|
|
301
|
-
throw new ProtocolValidationError("invalid_request", "appIdentity is required");
|
|
302
|
-
let appIdentity;
|
|
138
|
+
if (obj.publicationProof === void 0) return { launchToken };
|
|
303
139
|
try {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
} catch
|
|
307
|
-
throw new ProtocolValidationError("invalid_request",
|
|
140
|
+
const publicationProof = validatePublicationProof(obj.publicationProof);
|
|
141
|
+
return { launchToken, publicationProof };
|
|
142
|
+
} catch {
|
|
143
|
+
throw new ProtocolValidationError("invalid_request", "publicationProof is invalid");
|
|
308
144
|
}
|
|
309
|
-
return { launchToken, appIdentity };
|
|
310
145
|
}
|
|
311
146
|
function validateChannelPublishParams(raw) {
|
|
312
147
|
const obj = expectObject(raw, "channel.publish params");
|
|
@@ -375,104 +210,11 @@ function requireExactKeys(obj, keys, name) {
|
|
|
375
210
|
throw new ProtocolValidationError("invalid_request", `${name} contains unsupported fields`);
|
|
376
211
|
}
|
|
377
212
|
}
|
|
378
|
-
var STORAGE_FORBIDDEN_FIELDS = [
|
|
379
|
-
"publisherPublicKeyHex",
|
|
380
|
-
"publisherPublicKey",
|
|
381
|
-
"appId",
|
|
382
|
-
"appName",
|
|
383
|
-
"appIdentity",
|
|
384
|
-
"namespaceRoot",
|
|
385
|
-
"bucket",
|
|
386
|
-
"endpoint",
|
|
387
|
-
"providerId",
|
|
388
|
-
"s3Key",
|
|
389
|
-
"s3UploadId",
|
|
390
|
-
"accessKeyId",
|
|
391
|
-
"secretAccessKey"
|
|
392
|
-
];
|
|
393
|
-
function storageObject(raw, name) {
|
|
394
|
-
const obj = expectObject(raw, name);
|
|
395
|
-
for (const field of STORAGE_FORBIDDEN_FIELDS) {
|
|
396
|
-
if (Object.prototype.hasOwnProperty.call(obj, field))
|
|
397
|
-
throw new ProtocolValidationError("invalid_request", `${name} contains a forbidden field`);
|
|
398
|
-
}
|
|
399
|
-
return obj;
|
|
400
|
-
}
|
|
401
|
-
function storagePath(value, field, allowEmpty = false) {
|
|
402
|
-
const path = expectString(value, field);
|
|
403
|
-
if (!allowEmpty && path.length === 0)
|
|
404
|
-
throw new ProtocolValidationError("invalid_request", `${field} must not be empty`);
|
|
405
|
-
const withoutTrailingSlash = path.endsWith("/") ? path.slice(0, -1) : path;
|
|
406
|
-
if (path.length > 1024 || path.startsWith("/") || path.includes("\\") || path.includes("\0") || /[\u0000-\u001f\u007f]/u.test(path) || /[\u2044\u2215\u29f8\uff0f]/u.test(path)) {
|
|
407
|
-
throw new ProtocolValidationError("invalid_request", `${field} is not a valid relative path`);
|
|
408
|
-
}
|
|
409
|
-
const segments = withoutTrailingSlash.split("/");
|
|
410
|
-
if (!withoutTrailingSlash && !allowEmpty || withoutTrailingSlash && segments.some((segment) => !segment || segment === "." || segment === ".." || segment.length > 255)) {
|
|
411
|
-
throw new ProtocolValidationError("invalid_request", `${field} is not a valid relative path`);
|
|
412
|
-
}
|
|
413
|
-
return path;
|
|
414
|
-
}
|
|
415
|
-
function storageBinary(value, field) {
|
|
416
|
-
const binary = parseBinaryField(value, field);
|
|
417
|
-
if (binary.bytes.byteLength > STORAGE_MAX_PAYLOAD_BYTES) {
|
|
418
|
-
throw new ProtocolValidationError("storage_limit_exceeded", `${field} exceeds the maximum payload size`);
|
|
419
|
-
}
|
|
420
|
-
return binary;
|
|
421
|
-
}
|
|
422
|
-
function validateStorageListParams(raw) {
|
|
423
|
-
const obj = storageObject(raw, "storage.list params");
|
|
424
|
-
const prefix = obj.prefix === void 0 ? "" : storagePath(obj.prefix, "prefix", true);
|
|
425
|
-
const cursor = obj.cursor === void 0 ? void 0 : expectNonEmptyString(obj.cursor, "cursor");
|
|
426
|
-
const limit = obj.limit === void 0 ? STORAGE_DEFAULT_LIST_LIMIT : obj.limit;
|
|
427
|
-
if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > STORAGE_MAX_LIST_LIMIT) {
|
|
428
|
-
throw new ProtocolValidationError("invalid_request", "limit must be between 1 and 1000");
|
|
429
|
-
}
|
|
430
|
-
return { prefix, cursor, limit };
|
|
431
|
-
}
|
|
432
|
-
function validateStorageDirectoryParams(raw, name, allowOverwrite) {
|
|
433
|
-
const obj = storageObject(raw, `${name} params`);
|
|
434
|
-
const path = storagePath(obj.path, "path");
|
|
435
|
-
const overwrite = obj.overwrite === void 0 ? void 0 : obj.overwrite;
|
|
436
|
-
if (overwrite !== void 0 && typeof overwrite !== "boolean")
|
|
437
|
-
throw new ProtocolValidationError("invalid_request", "overwrite must be boolean");
|
|
438
|
-
return allowOverwrite ? { path, overwrite } : { path };
|
|
439
|
-
}
|
|
440
|
-
function validateStoragePutParams(raw) {
|
|
441
|
-
const obj = storageObject(raw, "storage.put params");
|
|
442
|
-
const path = storagePath(obj.path, "path");
|
|
443
|
-
if (path.endsWith("/"))
|
|
444
|
-
throw new ProtocolValidationError("invalid_request", "object path must not end with '/'");
|
|
445
|
-
const content = storageBinary(obj.content, "content");
|
|
446
|
-
const contentType = obj.contentType === void 0 ? void 0 : expectString(obj.contentType, "contentType");
|
|
447
|
-
const overwrite = obj.overwrite === void 0 ? void 0 : obj.overwrite;
|
|
448
|
-
if (overwrite !== void 0 && typeof overwrite !== "boolean")
|
|
449
|
-
throw new ProtocolValidationError("invalid_request", "overwrite must be boolean");
|
|
450
|
-
return { path, content, contentType, overwrite };
|
|
451
|
-
}
|
|
452
|
-
function validateStorageGetParams(raw) {
|
|
453
|
-
const obj = storageObject(raw, "storage.get params");
|
|
454
|
-
const path = storagePath(obj.path, "path");
|
|
455
|
-
if (path.endsWith("/"))
|
|
456
|
-
throw new ProtocolValidationError("invalid_request", "object path must not end with '/'");
|
|
457
|
-
const offset = obj.offset === void 0 ? 0 : obj.offset;
|
|
458
|
-
const length = obj.length === void 0 ? STORAGE_MAX_PAYLOAD_BYTES : obj.length;
|
|
459
|
-
if (typeof offset !== "number" || !Number.isSafeInteger(offset) || offset < 0)
|
|
460
|
-
throw new ProtocolValidationError("invalid_request", "offset must be a non-negative safe integer");
|
|
461
|
-
if (typeof length !== "number" || !Number.isSafeInteger(length) || length < 1 || length > STORAGE_MAX_PAYLOAD_BYTES)
|
|
462
|
-
throw new ProtocolValidationError("storage_limit_exceeded", "length exceeds the maximum payload size");
|
|
463
|
-
if (offset > Number.MAX_SAFE_INTEGER - (length - 1))
|
|
464
|
-
throw new ProtocolValidationError("storage_limit_exceeded", "requested range exceeds safe integer bounds");
|
|
465
|
-
const ifMatch = obj.ifMatch === void 0 ? void 0 : expectString(obj.ifMatch, "ifMatch");
|
|
466
|
-
return { path, offset, length, ifMatch };
|
|
467
|
-
}
|
|
468
|
-
function validateStorageDeleteParams(raw) {
|
|
469
|
-
const obj = storageObject(raw, "storage.delete params");
|
|
470
|
-
const path = storagePath(obj.path, "path");
|
|
471
|
-
if (path.endsWith("/"))
|
|
472
|
-
throw new ProtocolValidationError("invalid_request", "object path must not end with '/'");
|
|
473
|
-
return { path };
|
|
474
|
-
}
|
|
475
213
|
var MSFILE_FORBIDDEN_FIELDS = [
|
|
214
|
+
"appPublicKeyHex",
|
|
215
|
+
"appPublicKey",
|
|
216
|
+
"publicationProof",
|
|
217
|
+
"appProof",
|
|
476
218
|
"maxPriceSatoshis",
|
|
477
219
|
"contentKind",
|
|
478
220
|
"kind",
|
|
@@ -548,11 +290,11 @@ function assertP2pkhAddressForAsset(addr, assetId) {
|
|
|
548
290
|
throw new ProtocolValidationError("invalid_request", `recipientAddress must be a ${networkLabel} P2PKH address`);
|
|
549
291
|
}
|
|
550
292
|
}
|
|
551
|
-
function assertCompressedPubkeyHex(
|
|
552
|
-
if (
|
|
293
|
+
function assertCompressedPubkeyHex(hex) {
|
|
294
|
+
if (hex.length !== 66) {
|
|
553
295
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be a 33-byte compressed public key hex");
|
|
554
296
|
}
|
|
555
|
-
if (!/^[0-9a-fA-F]+$/.test(
|
|
297
|
+
if (!/^[0-9a-fA-F]+$/.test(hex)) {
|
|
556
298
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be hex");
|
|
557
299
|
}
|
|
558
300
|
}
|
|
@@ -678,9 +420,6 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
678
420
|
case "connect.login":
|
|
679
421
|
normalized = validateConnectLoginParams(params);
|
|
680
422
|
break;
|
|
681
|
-
case "connect.resume":
|
|
682
|
-
normalized = validateConnectResumeParams(params);
|
|
683
|
-
break;
|
|
684
423
|
case "connect.logout":
|
|
685
424
|
normalized = validateConnectLogoutParams(params);
|
|
686
425
|
break;
|
|
@@ -711,26 +450,6 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
711
450
|
case "channel.subscription_set":
|
|
712
451
|
normalized = validateChannelSubscriptionSetParams(params);
|
|
713
452
|
break;
|
|
714
|
-
case "storage.list":
|
|
715
|
-
normalized = validateStorageListParams(params);
|
|
716
|
-
break;
|
|
717
|
-
case "storage.directory.create":
|
|
718
|
-
normalized = validateStorageDirectoryParams(params, method, true);
|
|
719
|
-
break;
|
|
720
|
-
case "storage.directory.delete":
|
|
721
|
-
normalized = validateStorageDirectoryParams(params, method, false);
|
|
722
|
-
break;
|
|
723
|
-
case "storage.encrypted.put":
|
|
724
|
-
case "storage.put":
|
|
725
|
-
normalized = validateStoragePutParams(params);
|
|
726
|
-
break;
|
|
727
|
-
case "storage.encrypted.get":
|
|
728
|
-
case "storage.get":
|
|
729
|
-
normalized = validateStorageGetParams(params);
|
|
730
|
-
break;
|
|
731
|
-
case "storage.delete":
|
|
732
|
-
normalized = validateStorageDeleteParams(params);
|
|
733
|
-
break;
|
|
734
453
|
case "msfile.stat":
|
|
735
454
|
normalized = validateMsFileStatParams(params);
|
|
736
455
|
break;
|
|
@@ -746,6 +465,80 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
746
465
|
return { id, method, params: normalized };
|
|
747
466
|
}
|
|
748
467
|
|
|
468
|
+
// src/internal/origin.ts
|
|
469
|
+
var OriginValidationError = class extends Error {
|
|
470
|
+
constructor(message) {
|
|
471
|
+
super(message);
|
|
472
|
+
this.name = "OriginValidationError";
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
function canonicalizeJson(value) {
|
|
476
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") {
|
|
477
|
+
return JSON.stringify(value);
|
|
478
|
+
}
|
|
479
|
+
if (typeof value === "number") {
|
|
480
|
+
if (!Number.isFinite(value)) {
|
|
481
|
+
throw new OriginValidationError("identity payload contains a non-finite number");
|
|
482
|
+
}
|
|
483
|
+
return JSON.stringify(value);
|
|
484
|
+
}
|
|
485
|
+
if (Array.isArray(value)) {
|
|
486
|
+
return `[${value.map(canonicalizeJson).join(",")}]`;
|
|
487
|
+
}
|
|
488
|
+
if (typeof value === "object") {
|
|
489
|
+
const object = value;
|
|
490
|
+
const entries = Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${canonicalizeJson(object[key])}`);
|
|
491
|
+
return `{${entries.join(",")}}`;
|
|
492
|
+
}
|
|
493
|
+
throw new OriginValidationError("identity payload contains an unsupported value");
|
|
494
|
+
}
|
|
495
|
+
function parseHttpUrl(input, field) {
|
|
496
|
+
if (typeof input !== "string" || !input) {
|
|
497
|
+
throw new OriginValidationError(`${field} must be a non-empty string`);
|
|
498
|
+
}
|
|
499
|
+
let url;
|
|
500
|
+
try {
|
|
501
|
+
url = new URL(input);
|
|
502
|
+
} catch {
|
|
503
|
+
throw new OriginValidationError(`${field} is not a valid URL`);
|
|
504
|
+
}
|
|
505
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
506
|
+
throw new OriginValidationError(`${field} must use http or https`);
|
|
507
|
+
}
|
|
508
|
+
if (url.username || url.password) {
|
|
509
|
+
throw new OriginValidationError(`${field} must not contain credentials`);
|
|
510
|
+
}
|
|
511
|
+
if (url.origin === "null" || url.origin === "") {
|
|
512
|
+
throw new OriginValidationError(`${field} has an opaque origin`);
|
|
513
|
+
}
|
|
514
|
+
if (!url.host) {
|
|
515
|
+
throw new OriginValidationError(`${field} is invalid`);
|
|
516
|
+
}
|
|
517
|
+
return url;
|
|
518
|
+
}
|
|
519
|
+
function parseEntryUrl(input) {
|
|
520
|
+
const url = parseHttpUrl(input, "url");
|
|
521
|
+
return { origin: url.origin, url: url.href };
|
|
522
|
+
}
|
|
523
|
+
function canonicalOrigin(input) {
|
|
524
|
+
const url = parseHttpUrl(input, "origin");
|
|
525
|
+
if (url.pathname !== "/" && url.pathname !== "") {
|
|
526
|
+
throw new OriginValidationError("origin must not contain a path");
|
|
527
|
+
}
|
|
528
|
+
if (url.search || url.hash) {
|
|
529
|
+
throw new OriginValidationError("origin must not contain query or fragment");
|
|
530
|
+
}
|
|
531
|
+
return url.origin;
|
|
532
|
+
}
|
|
533
|
+
function assertCanonicalOrigin(input, field = "origin") {
|
|
534
|
+
try {
|
|
535
|
+
return canonicalOrigin(input);
|
|
536
|
+
} catch (error) {
|
|
537
|
+
if (error instanceof OriginValidationError) throw error;
|
|
538
|
+
throw new OriginValidationError(`${field} is invalid`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
749
542
|
// src/toolReceiver.ts
|
|
750
543
|
var KeymasterAiToolReceiver = class {
|
|
751
544
|
constructor(options) {
|
|
@@ -852,20 +645,7 @@ var KeymasterAiToolReceiver = class {
|
|
|
852
645
|
|
|
853
646
|
// src/server.ts
|
|
854
647
|
function isEstablishing(method) {
|
|
855
|
-
return ["connect.login", "connect.
|
|
856
|
-
}
|
|
857
|
-
function parseReconnectMessage(data) {
|
|
858
|
-
if (!data || typeof data !== "object") return null;
|
|
859
|
-
const value = data;
|
|
860
|
-
if (value.v !== PROTOCOL_VERSION || value.type !== "reconnect" || typeof value.id !== "string" || !value.id || value.id.length > 128 || typeof value.authorizationId !== "string" || !value.authorizationId || value.authorizationId.length > 128 || value.bindingId !== void 0 && typeof value.bindingId !== "string" || Object.keys(value).some((key) => !["v", "type", "id", "authorizationId", "bindingId", "appIdentity"].includes(key))) return null;
|
|
861
|
-
if (value.appIdentity !== void 0) {
|
|
862
|
-
try {
|
|
863
|
-
verifyAppIdentityProof(value.appIdentity);
|
|
864
|
-
} catch {
|
|
865
|
-
return null;
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
return value;
|
|
648
|
+
return ["connect.login", "connect.launch"].includes(method);
|
|
869
649
|
}
|
|
870
650
|
var KeymasterConnectReceiver = class {
|
|
871
651
|
constructor(options) {
|
|
@@ -902,8 +682,7 @@ var KeymasterConnectReceiver = class {
|
|
|
902
682
|
/** 校验并签发请求上下文。重复在途 ID 被忽略;非法可信请求返回标准错误。 */
|
|
903
683
|
async accept(event) {
|
|
904
684
|
if (!this.isAllowedPeer(event)) return null;
|
|
905
|
-
const
|
|
906
|
-
const raw = reconnect ? { v: PROTOCOL_VERSION, type: "request", id: reconnect.id, method: "connect.resume", params: reconnect.appIdentity ? { appIdentity: reconnect.appIdentity } : {} } : event.data;
|
|
685
|
+
const raw = event.data;
|
|
907
686
|
if (!raw || typeof raw !== "object" || raw.type !== "request") return null;
|
|
908
687
|
if (typeof raw.id === "string" && this.pending.get(event.source)?.has(raw.id)) return null;
|
|
909
688
|
try {
|
|
@@ -937,7 +716,6 @@ var KeymasterConnectReceiver = class {
|
|
|
937
716
|
walletGeneration: before.walletGeneration,
|
|
938
717
|
runGeneration: before.runGeneration,
|
|
939
718
|
bindingId: establishing ? void 0 : binding.id,
|
|
940
|
-
...reconnect ? { authorizationId: reconnect.authorizationId } : {},
|
|
941
719
|
signal: abort.signal
|
|
942
720
|
});
|
|
943
721
|
this.isAllowedPeer(event, true);
|
|
@@ -983,14 +761,8 @@ var KeymasterConnectReceiver = class {
|
|
|
983
761
|
const connected = result;
|
|
984
762
|
if (!sameNetworkIdentity(validateNetworkIdentity(connected.networkIdentity), context.networkIdentity) || "walletPublicKeyHex" in connected || "ownerPublicKeyHex" in connected) throw new ProtocolValidationError("invalid_request", "Invalid public connection result");
|
|
985
763
|
publicResult = { ...result, networkIdentity: context.networkIdentity };
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
if (reusable) {
|
|
989
|
-
bindingId = existing.id;
|
|
990
|
-
} else {
|
|
991
|
-
bindingId = this.options.randomUUID?.() || crypto.randomUUID();
|
|
992
|
-
this.bindings.set(context.source, { id: bindingId, origin: context.origin, identity: context.networkIdentity, walletGeneration: context.walletGeneration, runGeneration: context.runGeneration });
|
|
993
|
-
}
|
|
764
|
+
bindingId = this.options.randomUUID?.() || crypto.randomUUID();
|
|
765
|
+
this.bindings.set(context.source, { id: bindingId, origin: context.origin, identity: context.networkIdentity, walletGeneration: context.walletGeneration, runGeneration: context.runGeneration });
|
|
994
766
|
}
|
|
995
767
|
this.finish(context);
|
|
996
768
|
if (context.method === "connect.logout") this.bindings.delete(context.source);
|
|
@@ -1012,7 +784,7 @@ var KeymasterConnectReceiver = class {
|
|
|
1012
784
|
} catch {
|
|
1013
785
|
}
|
|
1014
786
|
}
|
|
1015
|
-
/**
|
|
787
|
+
/** 只读核对现存瞬时绑定,不创建新连接。 */
|
|
1016
788
|
matchesBinding(source, origin, id) {
|
|
1017
789
|
const binding = this.bindings.get(source), lifecycle = this.options.lifecycle();
|
|
1018
790
|
return !!binding && binding.id === id && binding.origin === origin && binding.walletGeneration === lifecycle.walletGeneration && binding.runGeneration === lifecycle.runGeneration && this.isAllowedPeer({ source, origin, data: null });
|
|
@@ -1067,6 +839,6 @@ var KeymasterConnectReceiver = class {
|
|
|
1067
839
|
}
|
|
1068
840
|
};
|
|
1069
841
|
|
|
1070
|
-
export {
|
|
842
|
+
export { KeymasterAiToolReceiver, KeymasterConnectReceiver, OriginValidationError, ProtocolValidationError, assertCanonicalOrigin, assertCompressedPubkeyHex, assertMainnetP2pkhAddress, assertOriginMatches, assertP2pkhAddressForAsset, canonicalOrigin, canonicalizeJson, isEstablishing, isStructuredCloneRequestCandidate, parseCancelMessage, parseEntryUrl, parseRequestMessage, validateExactChannel };
|
|
1071
843
|
//# sourceMappingURL=server.js.map
|
|
1072
844
|
//# sourceMappingURL=server.js.map
|