keymaster-connect 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -1,6 +1,6 @@
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, STORAGE_MAX_PAYLOAD_BYTES, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, isValidMsFileSourceId } from './chunk-2EYARFS3.js';
3
+ import { PROTOCOL_VERSION, PROTOCOL_METHODS, validateNetworkIdentity, sameNetworkIdentity, STORAGE_MAX_PAYLOAD_BYTES, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, isValidMsFileSourceId } from './chunk-ZDIFZJYD.js';
4
4
  import { secp256k1 } from '@noble/curves/secp256k1.js';
5
5
  import { sha256 } from '@noble/hashes/sha2.js';
6
6
 
@@ -209,53 +209,31 @@ function assertOriginMatches(aud, eventOrigin) {
209
209
  throw new ProtocolValidationError("invalid_origin", "aud does not match event origin");
210
210
  }
211
211
  }
212
- function validatePaymentPayParams(raw, network) {
212
+ function validatePaymentPayParams(raw) {
213
213
  const obj = expectObject(raw, "payment.pay params");
214
- const allowed = ["channelId", "clientRequestId", "paymentType", "businessKind", "amountSatoshis", "feeRateSatoshisPerKb", "recipient"];
214
+ const allowed = ["clientRequestId", "outputs"];
215
215
  for (const key of Object.keys(obj))
216
216
  if (!allowed.includes(key))
217
217
  throw new ProtocolValidationError("invalid_request", "payment.pay contains an unsupported field");
218
218
  const clientRequestId = expectNonEmptyString(obj.clientRequestId, "clientRequestId");
219
219
  if (clientRequestId.length > 128 || !/^[A-Za-z0-9._:-]+$/u.test(clientRequestId))
220
220
  throw new ProtocolValidationError("invalid_request", "clientRequestId is invalid");
221
- const paymentType = obj.paymentType;
222
- if (paymentType !== "p2pkh" && paymentType !== "p2pk")
223
- throw new ProtocolValidationError("invalid_request", "paymentType is unsupported");
224
- const businessKind = expectNonEmptyString(obj.businessKind, "businessKind");
225
- if (businessKind.length > 64 || !/^[A-Za-z0-9][A-Za-z0-9._:-]*$/u.test(businessKind))
226
- throw new ProtocolValidationError("invalid_request", "businessKind is invalid");
227
- const recipient = expectObject(obj.recipient, "recipient");
228
- if (paymentType === "p2pkh") {
229
- if (recipient.type !== "p2pkh")
230
- throw new ProtocolValidationError("invalid_request", "recipient.type must match paymentType");
231
- for (const key of Object.keys(recipient))
232
- if (key !== "type" && key !== "address")
233
- throw new ProtocolValidationError("invalid_request", "recipient contains an unsupported field");
234
- const address = expectString(recipient.address, "recipient.address");
235
- const assetId = network === "test" ? "bsv-testnet" : "bsv-mainnet";
236
- assertP2pkhAddressForAsset(address, assetId ?? "bsv-mainnet");
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
- };
221
+ if (!Array.isArray(obj.outputs) || obj.outputs.length === 0)
222
+ throw new ProtocolValidationError("invalid_request", "outputs must be a non-empty array");
223
+ const outputs = obj.outputs.map((entry, index) => {
224
+ const item = expectObject(entry, `outputs[${index}]`);
225
+ for (const key of Object.keys(item))
226
+ if (key !== "amount" && key !== "script")
227
+ throw new ProtocolValidationError("invalid_request", `outputs[${index}] contains an unsupported field`);
228
+ const amount = item.amount;
229
+ if (typeof amount !== "number" || !Number.isSafeInteger(amount) || amount < 0)
230
+ throw new ProtocolValidationError("invalid_request", `outputs[${index}].amount must be a non-negative safe integer`);
231
+ const script = expectString(item.script, `outputs[${index}].script`);
232
+ if (script.length % 2 !== 0 || !/^[0-9a-fA-F]*$/u.test(script))
233
+ throw new ProtocolValidationError("invalid_request", `outputs[${index}].script must be even-length hex`);
234
+ return { amount, script: script.toLowerCase() };
235
+ });
236
+ return { clientRequestId, outputs };
259
237
  }
260
238
  function validatePaymentStatusParams(raw) {
261
239
  const obj = expectObject(raw, "payment.status params");
@@ -267,21 +245,6 @@ function validatePaymentStatusParams(raw) {
267
245
  throw new ProtocolValidationError("invalid_request", "clientRequestId is invalid");
268
246
  return { clientRequestId };
269
247
  }
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
248
  function validateConnectLoginParams(raw) {
286
249
  const obj = expectObject(raw, "connect.login params");
287
250
  for (const key of Object.keys(obj))
@@ -593,12 +556,6 @@ function assertCompressedPubkeyHex(hex2) {
593
556
  throw new ProtocolValidationError("invalid_request", "counterpartyPublicKeyHex must be hex");
594
557
  }
595
558
  }
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
559
  function expectNonEmptyString(v, name) {
603
560
  if (typeof v !== "string" || v.length === 0) {
604
561
  throw new ProtocolValidationError("invalid_request", `${name} must be a non-empty string`);
@@ -708,15 +665,14 @@ function parseRequestMessage(raw, receiverIdentity) {
708
665
  const id = expectNonEmptyString(value.id, "Request id");
709
666
  if (["networkIdentity", "walletPublicKeyHex"].some((field) => field in value)) throw new ProtocolValidationError("invalid_request", "Wallet selectors have been removed");
710
667
  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");
668
+ 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
669
  if (!PROTOCOL_METHODS.includes(value.method)) throw new ProtocolValidationError("invalid_request", "Unknown method");
713
670
  const params = expectObject(value.params, "Params");
714
671
  for (const field of ["walletPublicKeyHex", "networkIdentity", "bsvNetwork", "assetId"]) {
715
672
  if (field in params) throw new ProtocolValidationError("invalid_request", `Removed wallet selector: ${field}`);
716
673
  }
717
674
  const method = value.method;
718
- if (method === "payment.channel.register" && params.paymentType === "feepool.connect") throw new ProtocolValidationError("unsupported_scope", "Connect v1 does not create fee-pool channels");
719
- const identity = validateNetworkIdentity(receiverIdentity);
675
+ validateNetworkIdentity(receiverIdentity);
720
676
  let normalized;
721
677
  switch (method) {
722
678
  case "connect.login":
@@ -744,14 +700,11 @@ function parseRequestMessage(raw, receiverIdentity) {
744
700
  normalized = validateCipherDecryptParams(params);
745
701
  break;
746
702
  case "payment.pay":
747
- normalized = validatePaymentPayParams(params, identity.bsvNetwork);
703
+ normalized = validatePaymentPayParams(params);
748
704
  break;
749
705
  case "payment.status":
750
706
  normalized = validatePaymentStatusParams(params);
751
707
  break;
752
- case "payment.channel.register":
753
- normalized = validatePaymentChannelRegisterParams(params);
754
- break;
755
708
  case "channel.publish":
756
709
  normalized = validateChannelPublishParams(params);
757
710
  break;
@@ -767,9 +720,11 @@ function parseRequestMessage(raw, receiverIdentity) {
767
720
  case "storage.directory.delete":
768
721
  normalized = validateStorageDirectoryParams(params, method, false);
769
722
  break;
723
+ case "storage.encrypted.put":
770
724
  case "storage.put":
771
725
  normalized = validateStoragePutParams(params);
772
726
  break;
727
+ case "storage.encrypted.get":
773
728
  case "storage.get":
774
729
  normalized = validateStorageGetParams(params);
775
730
  break;
@@ -902,7 +857,14 @@ function isEstablishing(method) {
902
857
  function parseReconnectMessage(data) {
903
858
  if (!data || typeof data !== "object") return null;
904
859
  const value = data;
905
- 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"].includes(key))) return null;
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
+ }
906
868
  return value;
907
869
  }
908
870
  var KeymasterConnectReceiver = class {
@@ -941,7 +903,7 @@ var KeymasterConnectReceiver = class {
941
903
  async accept(event) {
942
904
  if (!this.isAllowedPeer(event)) return null;
943
905
  const reconnect = parseReconnectMessage(event.data);
944
- const raw = reconnect ? { v: PROTOCOL_VERSION, type: "request", id: reconnect.id, method: "connect.resume", params: {} } : event.data;
906
+ const raw = reconnect ? { v: PROTOCOL_VERSION, type: "request", id: reconnect.id, method: "connect.resume", params: reconnect.appIdentity ? { appIdentity: reconnect.appIdentity } : {} } : event.data;
945
907
  if (!raw || typeof raw !== "object" || raw.type !== "request") return null;
946
908
  if (typeof raw.id === "string" && this.pending.get(event.source)?.has(raw.id)) return null;
947
909
  try {
@@ -1021,8 +983,14 @@ var KeymasterConnectReceiver = class {
1021
983
  const connected = result;
1022
984
  if (!sameNetworkIdentity(validateNetworkIdentity(connected.networkIdentity), context.networkIdentity) || "walletPublicKeyHex" in connected || "ownerPublicKeyHex" in connected) throw new ProtocolValidationError("invalid_request", "Invalid public connection result");
1023
985
  publicResult = { ...result, networkIdentity: context.networkIdentity };
1024
- bindingId = this.options.randomUUID?.() || crypto.randomUUID();
1025
- this.bindings.set(context.source, { id: bindingId, origin: context.origin, identity: context.networkIdentity, walletGeneration: context.walletGeneration, runGeneration: context.runGeneration });
986
+ const existing = this.bindings.get(context.source);
987
+ const reusable = context.method === "connect.resume" && existing && existing.origin === context.origin && existing.walletGeneration === context.walletGeneration && existing.runGeneration === context.runGeneration && sameNetworkIdentity(existing.identity, context.networkIdentity);
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
+ }
1026
994
  }
1027
995
  this.finish(context);
1028
996
  if (context.method === "connect.logout") this.bindings.delete(context.source);