keymaster-connect 0.2.0 → 0.4.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/README.md +47 -4
- package/dist/chunk-7ZOCLB2S.js +68 -0
- package/dist/chunk-7ZOCLB2S.js.map +1 -0
- package/dist/{chunk-FUIH6ZOG.js → chunk-QUZZ2OXO.js} +25 -3
- package/dist/chunk-QUZZ2OXO.js.map +1 -0
- package/dist/index.d.ts +82 -27
- package/dist/index.js +104 -32
- package/dist/index.js.map +1 -1
- package/dist/{protocol-BIUxkT-w.d.ts → protocol-Czc3NgOD.d.ts} +278 -5
- 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 +2 -2
- package/dist/server.js +232 -10
- package/dist/server.js.map +1 -1
- package/dist/signing.d.ts +12 -0
- package/dist/signing.js +3 -0
- package/dist/signing.js.map +1 -0
- package/package.json +17 -15
- package/dist/chunk-FUIH6ZOG.js.map +0 -1
package/dist/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isAiToolMessage, parseToolDefinitions, validateToolArguments, assertToolPayload } from './chunk-VVOTW5ZT.js';
|
|
2
2
|
export { AI_TOOLS_MAX_JSON_CHARS, assertToolJson, assertToolPayload, isAiToolMessage, parseToolDefinitions, validateToolArguments } from './chunk-VVOTW5ZT.js';
|
|
3
|
-
import { PROTOCOL_VERSION, PROTOCOL_METHODS, validateNetworkIdentity, sameNetworkIdentity, isValidMsFileSourceId } from './chunk-
|
|
3
|
+
import { PROTOCOL_VERSION, PROTOCOL_METHODS, validateNetworkIdentity, sameNetworkIdentity, DOMAIN_MAX_PROFILES, isValidMsFileSourceId } from './chunk-QUZZ2OXO.js';
|
|
4
|
+
import { validateApplicationPurpose, assertPaymentSignatureSlot } from './chunk-7ZOCLB2S.js';
|
|
4
5
|
|
|
5
6
|
// src/validation.ts
|
|
6
7
|
var ProtocolValidationError = class extends Error {
|
|
@@ -64,6 +65,28 @@ function assertOriginMatches(aud, eventOrigin) {
|
|
|
64
65
|
throw new ProtocolValidationError("invalid_origin", "aud does not match event origin");
|
|
65
66
|
}
|
|
66
67
|
}
|
|
68
|
+
function domainServiceID(params, input) {
|
|
69
|
+
if (Object.keys(params).some((key) => key !== input && key !== "serviceId")) {
|
|
70
|
+
throw new ProtocolValidationError("invalid_request", "Unexpected domain query parameter");
|
|
71
|
+
}
|
|
72
|
+
const id = params.serviceId;
|
|
73
|
+
if (id !== void 0 && (typeof id !== "string" || !/^[A-Za-z0-9_-]{1,64}$/u.test(id))) {
|
|
74
|
+
throw new ProtocolValidationError("invalid_request", "Invalid domain service ID");
|
|
75
|
+
}
|
|
76
|
+
return id;
|
|
77
|
+
}
|
|
78
|
+
function validateDomainProfilesParams(params) {
|
|
79
|
+
const serviceId = domainServiceID(params, "publicKeys");
|
|
80
|
+
const keys2 = params.publicKeys;
|
|
81
|
+
if (!Array.isArray(keys2) || keys2.length > DOMAIN_MAX_PROFILES) {
|
|
82
|
+
throw new ProtocolValidationError("invalid_request", "Expected up to 100 public keys");
|
|
83
|
+
}
|
|
84
|
+
const publicKeys = Array.from(keys2, (key) => {
|
|
85
|
+
if (typeof key !== "string") throw new ProtocolValidationError("invalid_request", "Public keys must be strings");
|
|
86
|
+
return key;
|
|
87
|
+
});
|
|
88
|
+
return { publicKeys, serviceId };
|
|
89
|
+
}
|
|
67
90
|
function validatePaymentPayParams(raw) {
|
|
68
91
|
const obj = expectObject(raw, "payment.pay params");
|
|
69
92
|
const allowed = ["clientRequestId", "outputs"];
|
|
@@ -100,10 +123,94 @@ function validatePaymentStatusParams(raw) {
|
|
|
100
123
|
throw new ProtocolValidationError("invalid_request", "clientRequestId is invalid");
|
|
101
124
|
return { clientRequestId };
|
|
102
125
|
}
|
|
126
|
+
function keys(obj, allowed) {
|
|
127
|
+
if (Object.keys(obj).some((key) => !allowed.includes(key))) {
|
|
128
|
+
throw new ProtocolValidationError("invalid_request", "Unexpected request field");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function msfileBlockPrice(value) {
|
|
132
|
+
if (value === void 0) return void 0;
|
|
133
|
+
if (typeof value !== "string" || !/^[1-9][0-9]{0,15}$/u.test(value))
|
|
134
|
+
throw new ProtocolValidationError("invalid_request", "Invalid MSFile full block price limit");
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
function hex(value, field, pattern = /^(?:[0-9a-f]{2})+$/iu) {
|
|
138
|
+
const str = expectString(value, field);
|
|
139
|
+
if (!pattern.test(str)) throw new ProtocolValidationError("invalid_request", `Invalid ${field}`);
|
|
140
|
+
return str.toLowerCase();
|
|
141
|
+
}
|
|
142
|
+
function natural(value, field, max = Number.MAX_SAFE_INTEGER) {
|
|
143
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > max) {
|
|
144
|
+
throw new ProtocolValidationError("invalid_request", `Invalid ${field}`);
|
|
145
|
+
}
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
function applicationPurpose(value) {
|
|
149
|
+
try {
|
|
150
|
+
return validateApplicationPurpose(expectString(value, "purpose"));
|
|
151
|
+
} catch {
|
|
152
|
+
throw new ProtocolValidationError("invalid_request", "Invalid application purpose");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function validatePrepare(obj) {
|
|
156
|
+
keys(obj, ["clientRequestId", "outputs", "dataSignature"]);
|
|
157
|
+
const { clientRequestId, outputs } = validatePaymentPayParams({ clientRequestId: obj.clientRequestId, outputs: obj.outputs });
|
|
158
|
+
const raw = expectObject(obj.dataSignature, "dataSignature");
|
|
159
|
+
keys(raw, ["outputIndex", "offset", "prefixHex", "suffixHex"]);
|
|
160
|
+
const dataSignature = {
|
|
161
|
+
outputIndex: natural(raw.outputIndex, "outputIndex"),
|
|
162
|
+
offset: natural(raw.offset, "offset"),
|
|
163
|
+
prefixHex: hex(raw.prefixHex, "prefixHex", /^(?:[0-9a-f]{2})*$/iu),
|
|
164
|
+
suffixHex: hex(raw.suffixHex, "suffixHex", /^(?:[0-9a-f]{2})*$/iu)
|
|
165
|
+
};
|
|
166
|
+
try {
|
|
167
|
+
assertPaymentSignatureSlot(outputs, dataSignature);
|
|
168
|
+
} catch {
|
|
169
|
+
throw new ProtocolValidationError("invalid_request", "Invalid payment signature slot");
|
|
170
|
+
}
|
|
171
|
+
return { clientRequestId, outputs, dataSignature };
|
|
172
|
+
}
|
|
173
|
+
function validateReservation(obj, commit) {
|
|
174
|
+
keys(obj, commit ? ["clientRequestId", "reservationId", "signingObject"] : ["clientRequestId", "reservationId"]);
|
|
175
|
+
const params = validatePaymentStatusParams({ clientRequestId: obj.clientRequestId });
|
|
176
|
+
const reservationId = hex(obj.reservationId, "reservationId", /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/iu);
|
|
177
|
+
return { ...params, reservationId, ...commit ? { signingObject: parseBinaryField(obj.signingObject, "signingObject") } : {} };
|
|
178
|
+
}
|
|
179
|
+
function validateScriptHistory(obj) {
|
|
180
|
+
const { state } = obj;
|
|
181
|
+
const script = hex(obj.script, "script");
|
|
182
|
+
const cursor = obj.cursor === void 0 ? void 0 : expectNonEmptyString(obj.cursor, "cursor");
|
|
183
|
+
if (state === "unconfirmed") {
|
|
184
|
+
keys(obj, ["state", "script", "cursor"]);
|
|
185
|
+
return { state, script, cursor };
|
|
186
|
+
}
|
|
187
|
+
if (state !== "confirmed") throw new ProtocolValidationError("invalid_request", "Invalid script history state");
|
|
188
|
+
keys(obj, ["state", "script", "cursor", "fromHeight", "toHeight"]);
|
|
189
|
+
const fromHeight = natural(obj.fromHeight, "fromHeight");
|
|
190
|
+
const toHeight = natural(obj.toHeight, "toHeight");
|
|
191
|
+
if (toHeight < fromHeight) throw new ProtocolValidationError("invalid_request", "Invalid height range");
|
|
192
|
+
return { state, script, cursor, fromHeight, toHeight };
|
|
193
|
+
}
|
|
194
|
+
function validateApplicationPayment(obj) {
|
|
195
|
+
keys(obj, ["purpose", "inputs", "address", "feeSatoshis", "rawTxHex"]);
|
|
196
|
+
if (!Array.isArray(obj.inputs) || obj.inputs.length === 0) throw new ProtocolValidationError("invalid_request", "Expected recovery inputs");
|
|
197
|
+
const inputs = Array.from(obj.inputs, (raw) => {
|
|
198
|
+
const row = expectObject(raw, "outpoint");
|
|
199
|
+
keys(row, ["txid", "vout"]);
|
|
200
|
+
return { txid: hex(row.txid, "txid", /^[0-9a-f]{64}$/iu), vout: natural(row.vout, "vout", 4294967295) };
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
purpose: applicationPurpose(obj.purpose),
|
|
204
|
+
inputs,
|
|
205
|
+
address: expectNonEmptyString(obj.address, "address"),
|
|
206
|
+
feeSatoshis: natural(obj.feeSatoshis, "feeSatoshis"),
|
|
207
|
+
rawTxHex: hex(obj.rawTxHex, "rawTxHex")
|
|
208
|
+
};
|
|
209
|
+
}
|
|
103
210
|
function validateConnectLoginParams(raw) {
|
|
104
211
|
const obj = expectObject(raw, "connect.login params");
|
|
105
212
|
for (const key of Object.keys(obj))
|
|
106
|
-
if (key !== "text" && key !== "claims" && key !== "publicationProof")
|
|
213
|
+
if (key !== "text" && key !== "claims" && key !== "publicationProof" && key !== "loginChallenge")
|
|
107
214
|
throw new ProtocolValidationError("invalid_request", "connect.login contains an unsupported field");
|
|
108
215
|
const text = expectString(obj.text, "text");
|
|
109
216
|
let claims;
|
|
@@ -116,7 +223,12 @@ function validateConnectLoginParams(raw) {
|
|
|
116
223
|
claims = [...obj.claims];
|
|
117
224
|
}
|
|
118
225
|
const publicationProof = expectPublicationRecordString(obj.publicationProof);
|
|
119
|
-
return
|
|
226
|
+
return {
|
|
227
|
+
text,
|
|
228
|
+
claims,
|
|
229
|
+
...publicationProof === void 0 ? {} : { publicationProof },
|
|
230
|
+
...validateLoginChallenge(obj.loginChallenge)
|
|
231
|
+
};
|
|
120
232
|
}
|
|
121
233
|
function validateConnectLogoutParams(raw) {
|
|
122
234
|
expectObject(raw, "connect.logout params");
|
|
@@ -124,12 +236,23 @@ function validateConnectLogoutParams(raw) {
|
|
|
124
236
|
}
|
|
125
237
|
function validateConnectLaunchParams(raw) {
|
|
126
238
|
const obj = expectObject(raw, "connect.launch params");
|
|
127
|
-
if (Object.keys(obj).some((key) => key !== "launchToken" && key !== "publicationProof")) {
|
|
239
|
+
if (Object.keys(obj).some((key) => key !== "launchToken" && key !== "publicationProof" && key !== "loginChallenge")) {
|
|
128
240
|
throw new ProtocolValidationError("invalid_request", "connect.launch contains an unsupported field");
|
|
129
241
|
}
|
|
130
242
|
const launchToken = expectNonEmptyString(obj.launchToken, "launchToken");
|
|
131
243
|
const publicationProof = expectPublicationRecordString(obj.publicationProof);
|
|
132
|
-
return
|
|
244
|
+
return {
|
|
245
|
+
launchToken,
|
|
246
|
+
...publicationProof === void 0 ? {} : { publicationProof },
|
|
247
|
+
...validateLoginChallenge(obj.loginChallenge)
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function validateLoginChallenge(value) {
|
|
251
|
+
if (value === void 0) return {};
|
|
252
|
+
if (typeof value !== "string" || !/^[0-9a-f]{64}$/u.test(value)) {
|
|
253
|
+
throw new ProtocolValidationError("invalid_request", "loginChallenge must be 32 bytes in lowercase hex");
|
|
254
|
+
}
|
|
255
|
+
return { loginChallenge: value };
|
|
133
256
|
}
|
|
134
257
|
function expectPublicationRecordString(value) {
|
|
135
258
|
if (value === void 0) return void 0;
|
|
@@ -198,9 +321,9 @@ function validateJsonValue(value, field, depth = 0) {
|
|
|
198
321
|
}
|
|
199
322
|
throw new ProtocolValidationError("invalid_request", `${field} must be a JSON value`);
|
|
200
323
|
}
|
|
201
|
-
function requireExactKeys(obj,
|
|
324
|
+
function requireExactKeys(obj, keys2, name) {
|
|
202
325
|
const actual = Object.keys(obj).sort();
|
|
203
|
-
const expected = [...
|
|
326
|
+
const expected = [...keys2].sort();
|
|
204
327
|
if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
|
|
205
328
|
throw new ProtocolValidationError("invalid_request", `${name} contains unsupported fields`);
|
|
206
329
|
}
|
|
@@ -285,11 +408,11 @@ function assertP2pkhAddressForAsset(addr, assetId) {
|
|
|
285
408
|
throw new ProtocolValidationError("invalid_request", `recipientAddress must be a ${networkLabel} P2PKH address`);
|
|
286
409
|
}
|
|
287
410
|
}
|
|
288
|
-
function assertCompressedPubkeyHex(
|
|
289
|
-
if (
|
|
411
|
+
function assertCompressedPubkeyHex(hex2) {
|
|
412
|
+
if (hex2.length !== 66) {
|
|
290
413
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be a 33-byte compressed public key hex");
|
|
291
414
|
}
|
|
292
|
-
if (!/^[0-9a-fA-F]+$/.test(
|
|
415
|
+
if (!/^[0-9a-fA-F]+$/.test(hex2)) {
|
|
293
416
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be hex");
|
|
294
417
|
}
|
|
295
418
|
}
|
|
@@ -439,6 +562,105 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
439
562
|
case "payment.status":
|
|
440
563
|
normalized = validatePaymentStatusParams(params);
|
|
441
564
|
break;
|
|
565
|
+
case "payment.prepare":
|
|
566
|
+
normalized = validatePrepare(params);
|
|
567
|
+
break;
|
|
568
|
+
case "payment.commit":
|
|
569
|
+
normalized = validateReservation(params, true);
|
|
570
|
+
break;
|
|
571
|
+
case "payment.cancel":
|
|
572
|
+
normalized = validateReservation(params, false);
|
|
573
|
+
break;
|
|
574
|
+
case "chain.get_height":
|
|
575
|
+
case "msfile.seller.status":
|
|
576
|
+
keys(params, []);
|
|
577
|
+
normalized = {};
|
|
578
|
+
break;
|
|
579
|
+
case "chain.list_script_history":
|
|
580
|
+
normalized = validateScriptHistory(params);
|
|
581
|
+
break;
|
|
582
|
+
case "chain.get_transaction":
|
|
583
|
+
keys(params, ["txid", "observedRawTxHex"]);
|
|
584
|
+
normalized = {
|
|
585
|
+
txid: hex(params.txid, "txid", /^[0-9a-f]{64}$/iu),
|
|
586
|
+
observedRawTxHex: params.observedRawTxHex === void 0 ? void 0 : hex(params.observedRawTxHex, "observedRawTxHex")
|
|
587
|
+
};
|
|
588
|
+
break;
|
|
589
|
+
case "chain.get_transactions": {
|
|
590
|
+
keys(params, ["txids", "mode"]);
|
|
591
|
+
const { txids, mode } = params;
|
|
592
|
+
if (!Array.isArray(txids) || !txids.length || txids.length > 20 || mode !== "raw" && mode !== "status") {
|
|
593
|
+
throw new ProtocolValidationError("invalid_request", "Expected 1 to 20 transaction IDs and a valid query mode");
|
|
594
|
+
}
|
|
595
|
+
const ids = Array.from(txids, (txid) => hex(txid, "txid", /^[0-9a-f]{64}$/iu));
|
|
596
|
+
if (new Set(ids).size !== ids.length) throw new ProtocolValidationError("invalid_request", "Duplicate transaction ID");
|
|
597
|
+
normalized = { txids: ids, mode };
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
case "chain.list_script_unspent":
|
|
601
|
+
keys(params, ["script"]);
|
|
602
|
+
normalized = { script: hex(params.script, "script") };
|
|
603
|
+
break;
|
|
604
|
+
case "chain.broadcast":
|
|
605
|
+
keys(params, ["rawTxHex"]);
|
|
606
|
+
normalized = { rawTxHex: hex(params.rawTxHex, "rawTxHex") };
|
|
607
|
+
break;
|
|
608
|
+
case "application.key":
|
|
609
|
+
keys(params, ["purpose"]);
|
|
610
|
+
normalized = { purpose: applicationPurpose(params.purpose) };
|
|
611
|
+
break;
|
|
612
|
+
case "application.sign_message":
|
|
613
|
+
keys(params, ["purpose", "message"]);
|
|
614
|
+
normalized = { purpose: applicationPurpose(params.purpose), message: parseBinaryField(params.message, "message") };
|
|
615
|
+
break;
|
|
616
|
+
case "application.sign_payment":
|
|
617
|
+
normalized = validateApplicationPayment(params);
|
|
618
|
+
break;
|
|
619
|
+
case "msfile.import":
|
|
620
|
+
keys(params, ["fileName", "content"]);
|
|
621
|
+
normalized = { fileName: expectNonEmptyString(params.fileName, "fileName"), content: parseBinaryField(params.content, "content") };
|
|
622
|
+
break;
|
|
623
|
+
case "msfile.content.read":
|
|
624
|
+
case "msfile.content.open": {
|
|
625
|
+
keys(params, ["seedHashHex", "clientRequestId", "maxFullBlockPriceSatoshis"]);
|
|
626
|
+
const price = msfileBlockPrice(params.maxFullBlockPriceSatoshis);
|
|
627
|
+
const needsId = method === "msfile.content.open" || price !== void 0 || params.clientRequestId !== void 0;
|
|
628
|
+
normalized = {
|
|
629
|
+
...validateMsFileStatParams(params),
|
|
630
|
+
...needsId ? validatePaymentStatusParams({ clientRequestId: params.clientRequestId }) : {},
|
|
631
|
+
...price === void 0 ? {} : { maxFullBlockPriceSatoshis: price }
|
|
632
|
+
};
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
case "msfile.content.status":
|
|
636
|
+
normalized = validatePaymentStatusParams(params);
|
|
637
|
+
break;
|
|
638
|
+
case "msfile.content.price": {
|
|
639
|
+
keys(params, ["clientRequestId", "maxFullBlockPriceSatoshis"]);
|
|
640
|
+
const price = msfileBlockPrice(params.maxFullBlockPriceSatoshis);
|
|
641
|
+
normalized = price === void 0 && params.clientRequestId === void 0 ? {} : {
|
|
642
|
+
...validatePaymentStatusParams({ clientRequestId: params.clientRequestId }),
|
|
643
|
+
...price === void 0 ? {} : { maxFullBlockPriceSatoshis: price }
|
|
644
|
+
};
|
|
645
|
+
if (price === void 0 && params.clientRequestId !== void 0)
|
|
646
|
+
throw new ProtocolValidationError("invalid_request", "Expected a full block price limit");
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
case "msfile.content.range": {
|
|
650
|
+
keys(params, ["clientRequestId", "offset", "length"]);
|
|
651
|
+
const { offset, length } = params;
|
|
652
|
+
if (typeof offset !== "number" || !Number.isSafeInteger(offset) || offset < 0 || typeof length !== "number" || !Number.isSafeInteger(length) || length < 0 || length > 256 * 1024 || !Number.isSafeInteger(offset + length)) {
|
|
653
|
+
throw new ProtocolValidationError("invalid_request", "Invalid MSFile byte range");
|
|
654
|
+
}
|
|
655
|
+
normalized = { ...validatePaymentStatusParams({ clientRequestId: params.clientRequestId }), offset, length };
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
case "domain.profiles":
|
|
659
|
+
normalized = validateDomainProfilesParams(params);
|
|
660
|
+
break;
|
|
661
|
+
case "domain.get_public_key":
|
|
662
|
+
normalized = { domain: expectString(params.domain, "domain"), serviceId: domainServiceID(params, "domain") };
|
|
663
|
+
break;
|
|
442
664
|
case "channel.publish":
|
|
443
665
|
normalized = validateChannelPublishParams(params);
|
|
444
666
|
break;
|