keymaster-connect 0.2.0 → 0.3.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 +30 -0
- package/dist/chunk-7ZOCLB2S.js +68 -0
- package/dist/chunk-7ZOCLB2S.js.map +1 -0
- package/dist/{chunk-FUIH6ZOG.js → chunk-A4VCQSB5.js} +20 -3
- package/dist/chunk-A4VCQSB5.js.map +1 -0
- package/dist/index.d.ts +67 -26
- package/dist/index.js +50 -31
- package/dist/index.js.map +1 -1
- package/dist/{protocol-BIUxkT-w.d.ts → protocol-Dfo305R9.d.ts} +175 -3
- 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 +164 -6
- 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 +6 -1
- package/dist/chunk-FUIH6ZOG.js.map +0 -1
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { Q as ProtocolErrorCode, at as P2pkhTransferAssetId, aJ as ProtocolRequestMessage, N as NetworkIdentity, c as ProtocolMethod, M as MethodParams, T as AiToolMessage, S as AiToolDefinition, a5 as JSONValue, aK as ProtocolResultMessage, aI as ProtocolReadyMessage, aE as ProtocolClosingMessage, d as MethodResult, aF as ProtocolError, aH as ProtocolEventPayload } from './protocol-Dfo305R9.js';
|
|
2
|
+
export { R as AI_TOOLS_MAX_JSON_CHARS, A as AiTool, U as AiToolSchema, b as AiToolsRegistered, aM as assertToolJson, aN as assertToolPayload, aO as isAiToolMessage, aQ as parseToolDefinitions, aU as validateToolArguments } from './protocol-Dfo305R9.js';
|
|
3
3
|
|
|
4
4
|
/** 校验失败:抛出的对象。service 会捕获并组装 ProtocolError。 */
|
|
5
5
|
declare class ProtocolValidationError extends Error {
|
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-A4VCQSB5.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,6 +123,84 @@ 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 hex(value, field, pattern = /^(?:[0-9a-f]{2})+$/iu) {
|
|
132
|
+
const str = expectString(value, field);
|
|
133
|
+
if (!pattern.test(str)) throw new ProtocolValidationError("invalid_request", `Invalid ${field}`);
|
|
134
|
+
return str.toLowerCase();
|
|
135
|
+
}
|
|
136
|
+
function natural(value, field, max = Number.MAX_SAFE_INTEGER) {
|
|
137
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > max) {
|
|
138
|
+
throw new ProtocolValidationError("invalid_request", `Invalid ${field}`);
|
|
139
|
+
}
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
function applicationPurpose(value) {
|
|
143
|
+
try {
|
|
144
|
+
return validateApplicationPurpose(expectString(value, "purpose"));
|
|
145
|
+
} catch {
|
|
146
|
+
throw new ProtocolValidationError("invalid_request", "Invalid application purpose");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function validatePrepare(obj) {
|
|
150
|
+
keys(obj, ["clientRequestId", "outputs", "dataSignature"]);
|
|
151
|
+
const { clientRequestId, outputs } = validatePaymentPayParams({ clientRequestId: obj.clientRequestId, outputs: obj.outputs });
|
|
152
|
+
const raw = expectObject(obj.dataSignature, "dataSignature");
|
|
153
|
+
keys(raw, ["outputIndex", "offset", "prefixHex", "suffixHex"]);
|
|
154
|
+
const dataSignature = {
|
|
155
|
+
outputIndex: natural(raw.outputIndex, "outputIndex"),
|
|
156
|
+
offset: natural(raw.offset, "offset"),
|
|
157
|
+
prefixHex: hex(raw.prefixHex, "prefixHex", /^(?:[0-9a-f]{2})*$/iu),
|
|
158
|
+
suffixHex: hex(raw.suffixHex, "suffixHex", /^(?:[0-9a-f]{2})*$/iu)
|
|
159
|
+
};
|
|
160
|
+
try {
|
|
161
|
+
assertPaymentSignatureSlot(outputs, dataSignature);
|
|
162
|
+
} catch {
|
|
163
|
+
throw new ProtocolValidationError("invalid_request", "Invalid payment signature slot");
|
|
164
|
+
}
|
|
165
|
+
return { clientRequestId, outputs, dataSignature };
|
|
166
|
+
}
|
|
167
|
+
function validateReservation(obj, commit) {
|
|
168
|
+
keys(obj, commit ? ["clientRequestId", "reservationId", "signingObject"] : ["clientRequestId", "reservationId"]);
|
|
169
|
+
const params = validatePaymentStatusParams({ clientRequestId: obj.clientRequestId });
|
|
170
|
+
const reservationId = hex(obj.reservationId, "reservationId", /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/iu);
|
|
171
|
+
return { ...params, reservationId, ...commit ? { signingObject: parseBinaryField(obj.signingObject, "signingObject") } : {} };
|
|
172
|
+
}
|
|
173
|
+
function validateScriptHistory(obj) {
|
|
174
|
+
const { state } = obj;
|
|
175
|
+
const script = hex(obj.script, "script");
|
|
176
|
+
const cursor = obj.cursor === void 0 ? void 0 : expectNonEmptyString(obj.cursor, "cursor");
|
|
177
|
+
if (state === "unconfirmed") {
|
|
178
|
+
keys(obj, ["state", "script", "cursor"]);
|
|
179
|
+
return { state, script, cursor };
|
|
180
|
+
}
|
|
181
|
+
if (state !== "confirmed") throw new ProtocolValidationError("invalid_request", "Invalid script history state");
|
|
182
|
+
keys(obj, ["state", "script", "cursor", "fromHeight", "toHeight"]);
|
|
183
|
+
const fromHeight = natural(obj.fromHeight, "fromHeight");
|
|
184
|
+
const toHeight = natural(obj.toHeight, "toHeight");
|
|
185
|
+
if (toHeight < fromHeight) throw new ProtocolValidationError("invalid_request", "Invalid height range");
|
|
186
|
+
return { state, script, cursor, fromHeight, toHeight };
|
|
187
|
+
}
|
|
188
|
+
function validateApplicationPayment(obj) {
|
|
189
|
+
keys(obj, ["purpose", "inputs", "address", "feeSatoshis", "rawTxHex"]);
|
|
190
|
+
if (!Array.isArray(obj.inputs) || obj.inputs.length === 0) throw new ProtocolValidationError("invalid_request", "Expected recovery inputs");
|
|
191
|
+
const inputs = Array.from(obj.inputs, (raw) => {
|
|
192
|
+
const row = expectObject(raw, "outpoint");
|
|
193
|
+
keys(row, ["txid", "vout"]);
|
|
194
|
+
return { txid: hex(row.txid, "txid", /^[0-9a-f]{64}$/iu), vout: natural(row.vout, "vout", 4294967295) };
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
purpose: applicationPurpose(obj.purpose),
|
|
198
|
+
inputs,
|
|
199
|
+
address: expectNonEmptyString(obj.address, "address"),
|
|
200
|
+
feeSatoshis: natural(obj.feeSatoshis, "feeSatoshis"),
|
|
201
|
+
rawTxHex: hex(obj.rawTxHex, "rawTxHex")
|
|
202
|
+
};
|
|
203
|
+
}
|
|
103
204
|
function validateConnectLoginParams(raw) {
|
|
104
205
|
const obj = expectObject(raw, "connect.login params");
|
|
105
206
|
for (const key of Object.keys(obj))
|
|
@@ -198,9 +299,9 @@ function validateJsonValue(value, field, depth = 0) {
|
|
|
198
299
|
}
|
|
199
300
|
throw new ProtocolValidationError("invalid_request", `${field} must be a JSON value`);
|
|
200
301
|
}
|
|
201
|
-
function requireExactKeys(obj,
|
|
302
|
+
function requireExactKeys(obj, keys2, name) {
|
|
202
303
|
const actual = Object.keys(obj).sort();
|
|
203
|
-
const expected = [...
|
|
304
|
+
const expected = [...keys2].sort();
|
|
204
305
|
if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
|
|
205
306
|
throw new ProtocolValidationError("invalid_request", `${name} contains unsupported fields`);
|
|
206
307
|
}
|
|
@@ -285,11 +386,11 @@ function assertP2pkhAddressForAsset(addr, assetId) {
|
|
|
285
386
|
throw new ProtocolValidationError("invalid_request", `recipientAddress must be a ${networkLabel} P2PKH address`);
|
|
286
387
|
}
|
|
287
388
|
}
|
|
288
|
-
function assertCompressedPubkeyHex(
|
|
289
|
-
if (
|
|
389
|
+
function assertCompressedPubkeyHex(hex2) {
|
|
390
|
+
if (hex2.length !== 66) {
|
|
290
391
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be a 33-byte compressed public key hex");
|
|
291
392
|
}
|
|
292
|
-
if (!/^[0-9a-fA-F]+$/.test(
|
|
393
|
+
if (!/^[0-9a-fA-F]+$/.test(hex2)) {
|
|
293
394
|
throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be hex");
|
|
294
395
|
}
|
|
295
396
|
}
|
|
@@ -439,6 +540,63 @@ function parseRequestMessage(raw, receiverIdentity) {
|
|
|
439
540
|
case "payment.status":
|
|
440
541
|
normalized = validatePaymentStatusParams(params);
|
|
441
542
|
break;
|
|
543
|
+
case "payment.prepare":
|
|
544
|
+
normalized = validatePrepare(params);
|
|
545
|
+
break;
|
|
546
|
+
case "payment.commit":
|
|
547
|
+
normalized = validateReservation(params, true);
|
|
548
|
+
break;
|
|
549
|
+
case "payment.cancel":
|
|
550
|
+
normalized = validateReservation(params, false);
|
|
551
|
+
break;
|
|
552
|
+
case "chain.get_height":
|
|
553
|
+
case "msfile.seller.status":
|
|
554
|
+
keys(params, []);
|
|
555
|
+
normalized = {};
|
|
556
|
+
break;
|
|
557
|
+
case "chain.list_script_history":
|
|
558
|
+
normalized = validateScriptHistory(params);
|
|
559
|
+
break;
|
|
560
|
+
case "chain.get_transaction":
|
|
561
|
+
keys(params, ["txid", "observedRawTxHex"]);
|
|
562
|
+
normalized = {
|
|
563
|
+
txid: hex(params.txid, "txid", /^[0-9a-f]{64}$/iu),
|
|
564
|
+
observedRawTxHex: params.observedRawTxHex === void 0 ? void 0 : hex(params.observedRawTxHex, "observedRawTxHex")
|
|
565
|
+
};
|
|
566
|
+
break;
|
|
567
|
+
case "chain.list_script_unspent":
|
|
568
|
+
keys(params, ["script"]);
|
|
569
|
+
normalized = { script: hex(params.script, "script") };
|
|
570
|
+
break;
|
|
571
|
+
case "chain.broadcast":
|
|
572
|
+
keys(params, ["rawTxHex"]);
|
|
573
|
+
normalized = { rawTxHex: hex(params.rawTxHex, "rawTxHex") };
|
|
574
|
+
break;
|
|
575
|
+
case "application.key":
|
|
576
|
+
keys(params, ["purpose"]);
|
|
577
|
+
normalized = { purpose: applicationPurpose(params.purpose) };
|
|
578
|
+
break;
|
|
579
|
+
case "application.sign_message":
|
|
580
|
+
keys(params, ["purpose", "message"]);
|
|
581
|
+
normalized = { purpose: applicationPurpose(params.purpose), message: parseBinaryField(params.message, "message") };
|
|
582
|
+
break;
|
|
583
|
+
case "application.sign_payment":
|
|
584
|
+
normalized = validateApplicationPayment(params);
|
|
585
|
+
break;
|
|
586
|
+
case "msfile.import":
|
|
587
|
+
keys(params, ["fileName", "content"]);
|
|
588
|
+
normalized = { fileName: expectNonEmptyString(params.fileName, "fileName"), content: parseBinaryField(params.content, "content") };
|
|
589
|
+
break;
|
|
590
|
+
case "msfile.content.read":
|
|
591
|
+
keys(params, ["seedHashHex"]);
|
|
592
|
+
normalized = validateMsFileStatParams(params);
|
|
593
|
+
break;
|
|
594
|
+
case "domain.profiles":
|
|
595
|
+
normalized = validateDomainProfilesParams(params);
|
|
596
|
+
break;
|
|
597
|
+
case "domain.get_public_key":
|
|
598
|
+
normalized = { domain: expectString(params.domain, "domain"), serviceId: domainServiceID(params, "domain") };
|
|
599
|
+
break;
|
|
442
600
|
case "channel.publish":
|
|
443
601
|
normalized = validateChannelPublishParams(params);
|
|
444
602
|
break;
|