@tinycloud/sdk-services 2.2.0 → 2.3.0-beta.2

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/index.js CHANGED
@@ -4429,20 +4429,20 @@ function parseNetworkId(networkId) {
4429
4429
  const lastColon = body.lastIndexOf(":");
4430
4430
  if (lastColon <= 0 || lastColon === body.length - 1) {
4431
4431
  throw new NetworkIdError(
4432
- `networkId missing principal or name segment (got ${JSON.stringify(networkId)})`
4432
+ `networkId missing ownerDid or name segment (got ${JSON.stringify(networkId)})`
4433
4433
  );
4434
4434
  }
4435
- const principal = body.slice(0, lastColon);
4435
+ const ownerDid = body.slice(0, lastColon);
4436
4436
  const name = body.slice(lastColon + 1);
4437
- if (!principal.startsWith("did:")) {
4437
+ if (!ownerDid.startsWith("did:")) {
4438
4438
  throw new NetworkIdError(
4439
- `networkId principal must be a DID (got ${JSON.stringify(principal)})`
4439
+ `networkId ownerDid must be a DID (got ${JSON.stringify(ownerDid)})`
4440
4440
  );
4441
4441
  }
4442
- const didParts = principal.split(":");
4442
+ const didParts = ownerDid.split(":");
4443
4443
  if (didParts.length < 3 || didParts.some((p) => p.length === 0)) {
4444
4444
  throw new NetworkIdError(
4445
- `networkId principal is not a well-formed DID (got ${JSON.stringify(principal)})`
4445
+ `networkId ownerDid is not a well-formed DID (got ${JSON.stringify(ownerDid)})`
4446
4446
  );
4447
4447
  }
4448
4448
  if (!NETWORK_NAME_RE.test(name)) {
@@ -4450,18 +4450,18 @@ function parseNetworkId(networkId) {
4450
4450
  `networkId name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
4451
4451
  );
4452
4452
  }
4453
- return { networkId, principal, name };
4453
+ return { networkId, ownerDid, name };
4454
4454
  }
4455
- function buildNetworkId(principal, name) {
4456
- if (typeof principal !== "string" || !principal.startsWith("did:")) {
4457
- throw new NetworkIdError("principal must be a DID");
4455
+ function buildNetworkId(ownerDid, name) {
4456
+ if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
4457
+ throw new NetworkIdError("ownerDid must be a DID");
4458
4458
  }
4459
4459
  if (typeof name !== "string" || !NETWORK_NAME_RE.test(name)) {
4460
4460
  throw new NetworkIdError(
4461
4461
  `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
4462
4462
  );
4463
4463
  }
4464
- const networkId = `${URN_PREFIX}${principal}:${name}`;
4464
+ const networkId = `${URN_PREFIX}${ownerDid}:${name}`;
4465
4465
  parseNetworkId(networkId);
4466
4466
  return networkId;
4467
4467
  }
@@ -4538,27 +4538,27 @@ function toError2(error) {
4538
4538
  // src/encryption/discovery.ts
4539
4539
  async function discoverNetwork(input) {
4540
4540
  let networkId;
4541
- let principal;
4541
+ let ownerDid;
4542
4542
  let name;
4543
4543
  try {
4544
4544
  if (input.identifier.startsWith("urn:tinycloud:encryption:")) {
4545
4545
  const parsed = parseNetworkId(input.identifier);
4546
4546
  networkId = parsed.networkId;
4547
- principal = parsed.principal;
4547
+ ownerDid = parsed.ownerDid;
4548
4548
  name = parsed.name;
4549
4549
  } else {
4550
- if (input.principal === void 0) {
4550
+ if (input.ownerDid === void 0) {
4551
4551
  return {
4552
4552
  ok: false,
4553
4553
  error: encryptionError({
4554
4554
  code: "INVALID_INPUT",
4555
- message: "discoverNetwork requires `principal` when identifier is a bare network name"
4555
+ message: "discoverNetwork requires `ownerDid` when identifier is a bare network name"
4556
4556
  })
4557
4557
  };
4558
4558
  }
4559
- networkId = `urn:tinycloud:encryption:${input.principal}:${input.identifier}`;
4559
+ networkId = `urn:tinycloud:encryption:${input.ownerDid}:${input.identifier}`;
4560
4560
  const parsed = parseNetworkId(networkId);
4561
- principal = parsed.principal;
4561
+ ownerDid = parsed.ownerDid;
4562
4562
  name = parsed.name;
4563
4563
  }
4564
4564
  } catch (err3) {
@@ -4577,7 +4577,7 @@ async function discoverNetwork(input) {
4577
4577
  try {
4578
4578
  const descriptor = await input.node.fetchByNetworkId(networkId);
4579
4579
  if (descriptor !== null) {
4580
- const validated = validateDescriptor(descriptor, networkId, principal, name);
4580
+ const validated = validateDescriptor(descriptor, networkId, ownerDid, name);
4581
4581
  if (!validated.ok) return validated;
4582
4582
  return { ok: true, data: { descriptor: validated.data, source: "node" } };
4583
4583
  }
@@ -4587,11 +4587,11 @@ async function discoverNetwork(input) {
4587
4587
  if (input.wellKnown !== void 0) {
4588
4588
  try {
4589
4589
  const descriptor = await input.wellKnown.fetchWellKnown(
4590
- principal,
4590
+ ownerDid,
4591
4591
  networkDiscoveryKey(name)
4592
4592
  );
4593
4593
  if (descriptor !== null) {
4594
- const validated = validateDescriptor(descriptor, networkId, principal, name);
4594
+ const validated = validateDescriptor(descriptor, networkId, ownerDid, name);
4595
4595
  if (!validated.ok) return validated;
4596
4596
  return {
4597
4597
  ok: true,
@@ -4610,7 +4610,7 @@ async function discoverNetwork(input) {
4610
4610
  })
4611
4611
  };
4612
4612
  }
4613
- function validateDescriptor(descriptor, networkId, principal, name) {
4613
+ function validateDescriptor(descriptor, networkId, ownerDid, name) {
4614
4614
  if (descriptor.networkId !== networkId) {
4615
4615
  return {
4616
4616
  ok: false,
@@ -4620,12 +4620,12 @@ function validateDescriptor(descriptor, networkId, principal, name) {
4620
4620
  })
4621
4621
  };
4622
4622
  }
4623
- if (descriptor.principal !== principal) {
4623
+ if (descriptor.ownerDid !== ownerDid) {
4624
4624
  return {
4625
4625
  ok: false,
4626
4626
  error: encryptionError({
4627
4627
  code: "INVALID_NETWORK_ID",
4628
- message: "descriptor principal does not match networkId principal"
4628
+ message: "descriptor ownerDid does not match networkId ownerDid"
4629
4629
  })
4630
4630
  };
4631
4631
  }
@@ -5005,6 +5005,15 @@ function verifyDecryptResponse(input) {
5005
5005
  })
5006
5006
  };
5007
5007
  }
5008
+ if (response.nodeId !== request.targetNode) {
5009
+ return {
5010
+ ok: false,
5011
+ error: encryptionError({
5012
+ code: "RESPONSE_BINDING_MISMATCH",
5013
+ field: "nodeId"
5014
+ })
5015
+ };
5016
+ }
5008
5017
  if (response.alg !== request.alg) {
5009
5018
  return {
5010
5019
  ok: false,
@@ -5108,10 +5117,10 @@ var EncryptionService = class extends BaseService {
5108
5117
  get crypto() {
5109
5118
  return this._config.crypto;
5110
5119
  }
5111
- async discoverNetwork(identifier, principal) {
5120
+ async discoverNetwork(identifier, ownerDid) {
5112
5121
  const result = await discoverNetwork({
5113
5122
  identifier,
5114
- ...principal !== void 0 ? { principal } : {},
5123
+ ...ownerDid !== void 0 ? { ownerDid } : {},
5115
5124
  ...this._config.node !== void 0 ? { node: this._config.node } : {},
5116
5125
  ...this._config.wellKnown !== void 0 ? { wellKnown: this._config.wellKnown } : {}
5117
5126
  });
@@ -5149,6 +5158,14 @@ var EncryptionService = class extends BaseService {
5149
5158
  try {
5150
5159
  const validated = validateEnvelope(this.crypto, envelope);
5151
5160
  if (!validated.ok) return validated;
5161
+ if (options?.aad !== void 0 && validated.data.aad !== base64Encode2(options.aad)) {
5162
+ return encErr(
5163
+ encryptionError({
5164
+ code: "INVALID_INPUT",
5165
+ message: "decryptEnvelope aad does not match the envelope"
5166
+ })
5167
+ );
5168
+ }
5152
5169
  let descriptor;
5153
5170
  if (options?.descriptor !== void 0) {
5154
5171
  descriptor = options.descriptor;