keymaster-connect 0.1.4 → 0.1.6
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 +29 -5
- package/dist/{chunk-VVOTW5ZT.js → chunk-M2YEI4PG.js} +92 -3
- package/dist/chunk-M2YEI4PG.js.map +1 -0
- package/dist/{chunk-2EYARFS3.js → chunk-THT6XHHY.js} +5 -5
- package/dist/chunk-THT6XHHY.js.map +1 -0
- package/dist/index.d.ts +109 -14
- package/dist/index.js +303 -28
- package/dist/index.js.map +1 -1
- package/dist/{protocol-NcRDmEKZ.d.ts → protocol-GXCfd2ya.d.ts} +57 -98
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.js +1 -1
- package/dist/script.d.ts +58 -0
- package/dist/script.js +253 -0
- package/dist/script.js.map +1 -0
- package/dist/server.d.ts +21 -12
- package/dist/server.js +144 -260
- package/dist/server.js.map +1 -1
- package/package.json +45 -10
- package/dist/chunk-2EYARFS3.js.map +0 -1
- package/dist/chunk-VVOTW5ZT.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, STORAGE_MAX_PAYLOAD_BYTES, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, isValidMsFileSourceId } from './chunk-
|
|
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, STORAGE_MAX_PAYLOAD_BYTES, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, isValidMsFileSourceId } from './chunk-THT6XHHY.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,87 +30,65 @@ 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) {
|
|
209
64
|
throw new ProtocolValidationError("invalid_origin", "aud does not match event origin");
|
|
210
65
|
}
|
|
211
66
|
}
|
|
212
|
-
function validatePaymentPayParams(raw
|
|
67
|
+
function validatePaymentPayParams(raw) {
|
|
213
68
|
const obj = expectObject(raw, "payment.pay params");
|
|
214
|
-
const allowed = ["
|
|
69
|
+
const allowed = ["clientRequestId", "outputs"];
|
|
215
70
|
for (const key of Object.keys(obj))
|
|
216
71
|
if (!allowed.includes(key))
|
|
217
72
|
throw new ProtocolValidationError("invalid_request", "payment.pay contains an unsupported field");
|
|
218
73
|
const clientRequestId = expectNonEmptyString(obj.clientRequestId, "clientRequestId");
|
|
219
74
|
if (clientRequestId.length > 128 || !/^[A-Za-z0-9._:-]+$/u.test(clientRequestId))
|
|
220
75
|
throw new ProtocolValidationError("invalid_request", "clientRequestId is invalid");
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (
|
|
230
|
-
throw new ProtocolValidationError("invalid_request",
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
} else {
|
|
238
|
-
if (recipient.type !== "p2pk")
|
|
239
|
-
throw new ProtocolValidationError("invalid_request", "recipient.type must match paymentType");
|
|
240
|
-
for (const key of Object.keys(recipient))
|
|
241
|
-
if (key !== "type" && key !== "publicKeyHex")
|
|
242
|
-
throw new ProtocolValidationError("invalid_request", "recipient contains an unsupported field");
|
|
243
|
-
assertCompressedPubkeyHex(expectString(recipient.publicKeyHex, "recipient.publicKeyHex"));
|
|
244
|
-
}
|
|
245
|
-
const amountSatoshis = expectPositiveInteger(obj.amountSatoshis, "amountSatoshis");
|
|
246
|
-
let feeRateSatoshisPerKb;
|
|
247
|
-
if (obj.feeRateSatoshisPerKb !== void 0)
|
|
248
|
-
feeRateSatoshisPerKb = expectPositiveInteger(obj.feeRateSatoshisPerKb, "feeRateSatoshisPerKb");
|
|
249
|
-
const channelId = expectPaymentChannelId(obj.channelId);
|
|
250
|
-
return {
|
|
251
|
-
channelId,
|
|
252
|
-
clientRequestId,
|
|
253
|
-
paymentType,
|
|
254
|
-
businessKind,
|
|
255
|
-
amountSatoshis,
|
|
256
|
-
...feeRateSatoshisPerKb === void 0 ? {} : { feeRateSatoshisPerKb },
|
|
257
|
-
recipient: paymentType === "p2pkh" ? { type: "p2pkh", address: expectString(recipient.address, "recipient.address") } : { type: "p2pk", publicKeyHex: expectString(recipient.publicKeyHex, "recipient.publicKeyHex").toLowerCase() }
|
|
258
|
-
};
|
|
76
|
+
if (!Array.isArray(obj.outputs) || obj.outputs.length === 0)
|
|
77
|
+
throw new ProtocolValidationError("invalid_request", "outputs must be a non-empty array");
|
|
78
|
+
const outputs = obj.outputs.map((entry, index) => {
|
|
79
|
+
const item = expectObject(entry, `outputs[${index}]`);
|
|
80
|
+
for (const key of Object.keys(item))
|
|
81
|
+
if (key !== "amount" && key !== "script")
|
|
82
|
+
throw new ProtocolValidationError("invalid_request", `outputs[${index}] contains an unsupported field`);
|
|
83
|
+
const amount = item.amount;
|
|
84
|
+
if (typeof amount !== "number" || !Number.isSafeInteger(amount) || amount < 0)
|
|
85
|
+
throw new ProtocolValidationError("invalid_request", `outputs[${index}].amount must be a non-negative safe integer`);
|
|
86
|
+
const script = expectString(item.script, `outputs[${index}].script`);
|
|
87
|
+
if (script.length % 2 !== 0 || !/^[0-9a-fA-F]*$/u.test(script))
|
|
88
|
+
throw new ProtocolValidationError("invalid_request", `outputs[${index}].script must be even-length hex`);
|
|
89
|
+
return { amount, script: script.toLowerCase() };
|
|
90
|
+
});
|
|
91
|
+
return { clientRequestId, outputs };
|
|
259
92
|
}
|
|
260
93
|
function validatePaymentStatusParams(raw) {
|
|
261
94
|
const obj = expectObject(raw, "payment.status params");
|
|
@@ -267,27 +100,12 @@ function validatePaymentStatusParams(raw) {
|
|
|
267
100
|
throw new ProtocolValidationError("invalid_request", "clientRequestId is invalid");
|
|
268
101
|
return { clientRequestId };
|
|
269
102
|
}
|
|
270
|
-
function expectPaymentChannelId(raw) {
|
|
271
|
-
const id = expectNonEmptyString(raw, "channelId");
|
|
272
|
-
if (id.length > 96 || !/^[a-z][a-z0-9.-]+$/.test(id))
|
|
273
|
-
throw new ProtocolValidationError("invalid_request", "Invalid payment channelId");
|
|
274
|
-
return id;
|
|
275
|
-
}
|
|
276
|
-
function validatePaymentChannelRegisterParams(raw) {
|
|
277
|
-
const obj = expectObject(raw, "payment.channel.register params");
|
|
278
|
-
const channelName = expectNonEmptyString(obj.channelName, "channelName");
|
|
279
|
-
if (!/^[a-z][a-z0-9.-]{0,31}$/.test(channelName))
|
|
280
|
-
throw new ProtocolValidationError("invalid_request", "Invalid channelName");
|
|
281
|
-
if (obj.paymentType !== "p2pkh" && obj.paymentType !== "p2pk")
|
|
282
|
-
throw new ProtocolValidationError("invalid_request", "Unsupported paymentType");
|
|
283
|
-
return { channelName, paymentType: obj.paymentType };
|
|
284
|
-
}
|
|
285
103
|
function validateConnectLoginParams(raw) {
|
|
286
104
|
const obj = expectObject(raw, "connect.login params");
|
|
287
105
|
for (const key of Object.keys(obj))
|
|
288
|
-
if (key !== "text" && key !== "claims" && key !== "
|
|
106
|
+
if (key !== "text" && key !== "claims" && key !== "publicationProof")
|
|
289
107
|
throw new ProtocolValidationError("invalid_request", "connect.login contains an unsupported field");
|
|
290
|
-
const
|
|
108
|
+
const text = expectString(obj.text, "text");
|
|
291
109
|
let claims;
|
|
292
110
|
if (obj.claims !== void 0) {
|
|
293
111
|
if (!Array.isArray(obj.claims)) {
|
|
@@ -297,32 +115,21 @@ function validateConnectLoginParams(raw) {
|
|
|
297
115
|
throw new ProtocolValidationError("invalid_request", "claims entries must be strings");
|
|
298
116
|
claims = [...obj.claims];
|
|
299
117
|
}
|
|
300
|
-
let
|
|
301
|
-
if (obj.
|
|
118
|
+
let publicationProof;
|
|
119
|
+
if (obj.publicationProof !== void 0) {
|
|
302
120
|
try {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
throw new ProtocolValidationError("invalid_request", error instanceof AppIdentityValidationError ? error.message : "appIdentity is invalid");
|
|
121
|
+
publicationProof = validatePublicationProof(obj.publicationProof);
|
|
122
|
+
} catch {
|
|
123
|
+
throw new ProtocolValidationError("invalid_request", "publicationProof is invalid");
|
|
307
124
|
}
|
|
308
125
|
}
|
|
309
|
-
return
|
|
126
|
+
return publicationProof === void 0 ? { text, claims } : { text, claims, publicationProof };
|
|
310
127
|
}
|
|
311
128
|
function validateConnectResumeParams(raw) {
|
|
312
129
|
const obj = expectObject(raw, "connect.resume params");
|
|
313
130
|
for (const field of Object.keys(obj))
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
let appIdentity;
|
|
317
|
-
if (obj.appIdentity !== void 0) {
|
|
318
|
-
try {
|
|
319
|
-
verifyAppIdentityProof(obj.appIdentity);
|
|
320
|
-
appIdentity = obj.appIdentity;
|
|
321
|
-
} catch (error) {
|
|
322
|
-
throw new ProtocolValidationError("invalid_request", error instanceof Error ? error.message : "appIdentity is invalid");
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return { ...appIdentity ? { appIdentity } : {} };
|
|
131
|
+
throw new ProtocolValidationError("invalid_request", "connect.resume contains an unsupported field");
|
|
132
|
+
return {};
|
|
326
133
|
}
|
|
327
134
|
function validateConnectLogoutParams(raw) {
|
|
328
135
|
expectObject(raw, "connect.logout params");
|
|
@@ -330,20 +137,17 @@ function validateConnectLogoutParams(raw) {
|
|
|
330
137
|
}
|
|
331
138
|
function validateConnectLaunchParams(raw) {
|
|
332
139
|
const obj = expectObject(raw, "connect.launch params");
|
|
333
|
-
if (Object.keys(obj).some((key) => key !== "launchToken" && key !== "
|
|
140
|
+
if (Object.keys(obj).some((key) => key !== "launchToken" && key !== "publicationProof")) {
|
|
334
141
|
throw new ProtocolValidationError("invalid_request", "connect.launch contains an unsupported field");
|
|
335
142
|
}
|
|
336
143
|
const launchToken = expectNonEmptyString(obj.launchToken, "launchToken");
|
|
337
|
-
if (obj.
|
|
338
|
-
throw new ProtocolValidationError("invalid_request", "appIdentity is required");
|
|
339
|
-
let appIdentity;
|
|
144
|
+
if (obj.publicationProof === void 0) return { launchToken };
|
|
340
145
|
try {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
} catch
|
|
344
|
-
throw new ProtocolValidationError("invalid_request",
|
|
146
|
+
const publicationProof = validatePublicationProof(obj.publicationProof);
|
|
147
|
+
return { launchToken, publicationProof };
|
|
148
|
+
} catch {
|
|
149
|
+
throw new ProtocolValidationError("invalid_request", "publicationProof is invalid");
|
|
345
150
|
}
|
|
346
|
-
return { launchToken, appIdentity };
|
|
347
151
|
}
|
|
348
152
|
function validateChannelPublishParams(raw) {
|
|
349
153
|
const obj = expectObject(raw, "channel.publish params");
|
|
@@ -413,6 +217,10 @@ function requireExactKeys(obj, keys, name) {
|
|
|
413
217
|
}
|
|
414
218
|
}
|
|
415
219
|
var STORAGE_FORBIDDEN_FIELDS = [
|
|
220
|
+
"appPublicKeyHex",
|
|
221
|
+
"appPublicKey",
|
|
222
|
+
"publicationProof",
|
|
223
|
+
"appProof",
|
|
416
224
|
"publisherPublicKeyHex",
|
|
417
225
|
"publisherPublicKey",
|
|
418
226
|
"appId",
|
|
@@ -510,6 +318,10 @@ function validateStorageDeleteParams(raw) {
|
|
|
510
318
|
return { path };
|
|
511
319
|
}
|
|
512
320
|
var MSFILE_FORBIDDEN_FIELDS = [
|
|
321
|
+
"appPublicKeyHex",
|
|
322
|
+
"appPublicKey",
|
|
323
|
+
"publicationProof",
|
|
324
|
+
"appProof",
|
|
513
325
|
"maxPriceSatoshis",
|
|
514
326
|
"contentKind",
|
|
515
327
|
"kind",
|
|
@@ -585,20 +397,14 @@ function assertP2pkhAddressForAsset(addr, assetId) {
|
|
|
585
397
|
throw new ProtocolValidationError("invalid_request", `recipientAddress must be a ${networkLabel} P2PKH address`);
|
|
586
398
|
}
|
|
587
399
|
}
|
|
588
|
-
function assertCompressedPubkeyHex(
|
|
589
|
-
if (
|
|
400
|
+
function assertCompressedPubkeyHex(hex) {
|
|
401
|
+
if (hex.length !== 66) {
|
|
590
402
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be a 33-byte compressed public key hex");
|
|
591
403
|
}
|
|
592
|
-
if (!/^[0-9a-fA-F]+$/.test(
|
|
404
|
+
if (!/^[0-9a-fA-F]+$/.test(hex)) {
|
|
593
405
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be hex");
|
|
594
406
|
}
|
|
595
407
|
}
|
|
596
|
-
function expectPositiveInteger(v, name) {
|
|
597
|
-
if (typeof v !== "number" || !Number.isInteger(v) || v <= 0) {
|
|
598
|
-
throw new ProtocolValidationError("invalid_request", `${name} must be a positive integer`);
|
|
599
|
-
}
|
|
600
|
-
return v;
|
|
601
|
-
}
|
|
602
408
|
function expectNonEmptyString(v, name) {
|
|
603
409
|
if (typeof v !== "string" || v.length === 0) {
|
|
604
410
|
throw new ProtocolValidationError("invalid_request", `${name} must be a non-empty string`);
|
|
@@ -708,15 +514,14 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
708
514
|
const id = expectNonEmptyString(value.id, "Request id");
|
|
709
515
|
if (["networkIdentity", "walletPublicKeyHex"].some((field) => field in value)) throw new ProtocolValidationError("invalid_request", "Wallet selectors have been removed");
|
|
710
516
|
if (typeof value.method !== "string") throw new ProtocolValidationError("invalid_request", "Unknown method");
|
|
711
|
-
if (value.method === "feepool.prepare" || value.method === "feepool.commit" || value.method === "p2pkh.transfer") throw new ProtocolValidationError("unsupported_scope", "Method is outside Connect v1 scope");
|
|
517
|
+
if (value.method === "feepool.prepare" || value.method === "feepool.commit" || value.method === "p2pkh.transfer" || value.method === "payment.channel.register") throw new ProtocolValidationError("unsupported_scope", "Method is outside Connect v1 scope");
|
|
712
518
|
if (!PROTOCOL_METHODS.includes(value.method)) throw new ProtocolValidationError("invalid_request", "Unknown method");
|
|
713
519
|
const params = expectObject(value.params, "Params");
|
|
714
520
|
for (const field of ["walletPublicKeyHex", "networkIdentity", "bsvNetwork", "assetId"]) {
|
|
715
521
|
if (field in params) throw new ProtocolValidationError("invalid_request", `Removed wallet selector: ${field}`);
|
|
716
522
|
}
|
|
717
523
|
const method = value.method;
|
|
718
|
-
|
|
719
|
-
const identity = validateNetworkIdentity(receiverIdentity);
|
|
524
|
+
validateNetworkIdentity(receiverIdentity);
|
|
720
525
|
let normalized;
|
|
721
526
|
switch (method) {
|
|
722
527
|
case "connect.login":
|
|
@@ -744,14 +549,11 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
744
549
|
normalized = validateCipherDecryptParams(params);
|
|
745
550
|
break;
|
|
746
551
|
case "payment.pay":
|
|
747
|
-
normalized = validatePaymentPayParams(params
|
|
552
|
+
normalized = validatePaymentPayParams(params);
|
|
748
553
|
break;
|
|
749
554
|
case "payment.status":
|
|
750
555
|
normalized = validatePaymentStatusParams(params);
|
|
751
556
|
break;
|
|
752
|
-
case "payment.channel.register":
|
|
753
|
-
normalized = validatePaymentChannelRegisterParams(params);
|
|
754
|
-
break;
|
|
755
557
|
case "channel.publish":
|
|
756
558
|
normalized = validateChannelPublishParams(params);
|
|
757
559
|
break;
|
|
@@ -767,9 +569,11 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
767
569
|
case "storage.directory.delete":
|
|
768
570
|
normalized = validateStorageDirectoryParams(params, method, false);
|
|
769
571
|
break;
|
|
572
|
+
case "storage.encrypted.put":
|
|
770
573
|
case "storage.put":
|
|
771
574
|
normalized = validateStoragePutParams(params);
|
|
772
575
|
break;
|
|
576
|
+
case "storage.encrypted.get":
|
|
773
577
|
case "storage.get":
|
|
774
578
|
normalized = validateStorageGetParams(params);
|
|
775
579
|
break;
|
|
@@ -791,6 +595,80 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
791
595
|
return { id, method, params: normalized };
|
|
792
596
|
}
|
|
793
597
|
|
|
598
|
+
// src/internal/origin.ts
|
|
599
|
+
var OriginValidationError = class extends Error {
|
|
600
|
+
constructor(message) {
|
|
601
|
+
super(message);
|
|
602
|
+
this.name = "OriginValidationError";
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
function canonicalizeJson(value) {
|
|
606
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") {
|
|
607
|
+
return JSON.stringify(value);
|
|
608
|
+
}
|
|
609
|
+
if (typeof value === "number") {
|
|
610
|
+
if (!Number.isFinite(value)) {
|
|
611
|
+
throw new OriginValidationError("identity payload contains a non-finite number");
|
|
612
|
+
}
|
|
613
|
+
return JSON.stringify(value);
|
|
614
|
+
}
|
|
615
|
+
if (Array.isArray(value)) {
|
|
616
|
+
return `[${value.map(canonicalizeJson).join(",")}]`;
|
|
617
|
+
}
|
|
618
|
+
if (typeof value === "object") {
|
|
619
|
+
const object = value;
|
|
620
|
+
const entries = Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${canonicalizeJson(object[key])}`);
|
|
621
|
+
return `{${entries.join(",")}}`;
|
|
622
|
+
}
|
|
623
|
+
throw new OriginValidationError("identity payload contains an unsupported value");
|
|
624
|
+
}
|
|
625
|
+
function parseHttpUrl(input, field) {
|
|
626
|
+
if (typeof input !== "string" || !input) {
|
|
627
|
+
throw new OriginValidationError(`${field} must be a non-empty string`);
|
|
628
|
+
}
|
|
629
|
+
let url;
|
|
630
|
+
try {
|
|
631
|
+
url = new URL(input);
|
|
632
|
+
} catch {
|
|
633
|
+
throw new OriginValidationError(`${field} is not a valid URL`);
|
|
634
|
+
}
|
|
635
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
636
|
+
throw new OriginValidationError(`${field} must use http or https`);
|
|
637
|
+
}
|
|
638
|
+
if (url.username || url.password) {
|
|
639
|
+
throw new OriginValidationError(`${field} must not contain credentials`);
|
|
640
|
+
}
|
|
641
|
+
if (url.origin === "null" || url.origin === "") {
|
|
642
|
+
throw new OriginValidationError(`${field} has an opaque origin`);
|
|
643
|
+
}
|
|
644
|
+
if (!url.host) {
|
|
645
|
+
throw new OriginValidationError(`${field} is invalid`);
|
|
646
|
+
}
|
|
647
|
+
return url;
|
|
648
|
+
}
|
|
649
|
+
function parseEntryUrl(input) {
|
|
650
|
+
const url = parseHttpUrl(input, "url");
|
|
651
|
+
return { origin: url.origin, url: url.href };
|
|
652
|
+
}
|
|
653
|
+
function canonicalOrigin(input) {
|
|
654
|
+
const url = parseHttpUrl(input, "origin");
|
|
655
|
+
if (url.pathname !== "/" && url.pathname !== "") {
|
|
656
|
+
throw new OriginValidationError("origin must not contain a path");
|
|
657
|
+
}
|
|
658
|
+
if (url.search || url.hash) {
|
|
659
|
+
throw new OriginValidationError("origin must not contain query or fragment");
|
|
660
|
+
}
|
|
661
|
+
return url.origin;
|
|
662
|
+
}
|
|
663
|
+
function assertCanonicalOrigin(input, field = "origin") {
|
|
664
|
+
try {
|
|
665
|
+
return canonicalOrigin(input);
|
|
666
|
+
} catch (error) {
|
|
667
|
+
if (error instanceof OriginValidationError) throw error;
|
|
668
|
+
throw new OriginValidationError(`${field} is invalid`);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
794
672
|
// src/toolReceiver.ts
|
|
795
673
|
var KeymasterAiToolReceiver = class {
|
|
796
674
|
constructor(options) {
|
|
@@ -1021,8 +899,14 @@ var KeymasterConnectReceiver = class {
|
|
|
1021
899
|
const connected = result;
|
|
1022
900
|
if (!sameNetworkIdentity(validateNetworkIdentity(connected.networkIdentity), context.networkIdentity) || "walletPublicKeyHex" in connected || "ownerPublicKeyHex" in connected) throw new ProtocolValidationError("invalid_request", "Invalid public connection result");
|
|
1023
901
|
publicResult = { ...result, networkIdentity: context.networkIdentity };
|
|
1024
|
-
|
|
1025
|
-
|
|
902
|
+
const existing = this.bindings.get(context.source);
|
|
903
|
+
const reusable = context.method === "connect.resume" && existing && existing.origin === context.origin && existing.walletGeneration === context.walletGeneration && existing.runGeneration === context.runGeneration && sameNetworkIdentity(existing.identity, context.networkIdentity);
|
|
904
|
+
if (reusable) {
|
|
905
|
+
bindingId = existing.id;
|
|
906
|
+
} else {
|
|
907
|
+
bindingId = this.options.randomUUID?.() || crypto.randomUUID();
|
|
908
|
+
this.bindings.set(context.source, { id: bindingId, origin: context.origin, identity: context.networkIdentity, walletGeneration: context.walletGeneration, runGeneration: context.runGeneration });
|
|
909
|
+
}
|
|
1026
910
|
}
|
|
1027
911
|
this.finish(context);
|
|
1028
912
|
if (context.method === "connect.logout") this.bindings.delete(context.source);
|
|
@@ -1099,6 +983,6 @@ var KeymasterConnectReceiver = class {
|
|
|
1099
983
|
}
|
|
1100
984
|
};
|
|
1101
985
|
|
|
1102
|
-
export {
|
|
986
|
+
export { KeymasterAiToolReceiver, KeymasterConnectReceiver, OriginValidationError, ProtocolValidationError, assertCanonicalOrigin, assertCompressedPubkeyHex, assertMainnetP2pkhAddress, assertOriginMatches, assertP2pkhAddressForAsset, canonicalOrigin, canonicalizeJson, isEstablishing, isStructuredCloneRequestCandidate, parseCancelMessage, parseEntryUrl, parseReconnectMessage, parseRequestMessage, validateExactChannel };
|
|
1103
987
|
//# sourceMappingURL=server.js.map
|
|
1104
988
|
//# sourceMappingURL=server.js.map
|