@tinycloud/sdk-services 2.2.1-beta.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/encryption/index.cjs +25 -25
- package/dist/encryption/index.cjs.map +1 -1
- package/dist/encryption/index.d.cts +25 -25
- package/dist/encryption/index.d.ts +25 -25
- package/dist/encryption/index.js +25 -25
- package/dist/encryption/index.js.map +1 -1
- package/dist/index.cjs +25 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +25 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -471,20 +471,20 @@ function parseNetworkId(networkId) {
|
|
|
471
471
|
const lastColon = body.lastIndexOf(":");
|
|
472
472
|
if (lastColon <= 0 || lastColon === body.length - 1) {
|
|
473
473
|
throw new NetworkIdError(
|
|
474
|
-
`networkId missing
|
|
474
|
+
`networkId missing ownerDid or name segment (got ${JSON.stringify(networkId)})`
|
|
475
475
|
);
|
|
476
476
|
}
|
|
477
|
-
const
|
|
477
|
+
const ownerDid = body.slice(0, lastColon);
|
|
478
478
|
const name = body.slice(lastColon + 1);
|
|
479
|
-
if (!
|
|
479
|
+
if (!ownerDid.startsWith("did:")) {
|
|
480
480
|
throw new NetworkIdError(
|
|
481
|
-
`networkId
|
|
481
|
+
`networkId ownerDid must be a DID (got ${JSON.stringify(ownerDid)})`
|
|
482
482
|
);
|
|
483
483
|
}
|
|
484
|
-
const didParts =
|
|
484
|
+
const didParts = ownerDid.split(":");
|
|
485
485
|
if (didParts.length < 3 || didParts.some((p) => p.length === 0)) {
|
|
486
486
|
throw new NetworkIdError(
|
|
487
|
-
`networkId
|
|
487
|
+
`networkId ownerDid is not a well-formed DID (got ${JSON.stringify(ownerDid)})`
|
|
488
488
|
);
|
|
489
489
|
}
|
|
490
490
|
if (!NETWORK_NAME_RE.test(name)) {
|
|
@@ -492,18 +492,18 @@ function parseNetworkId(networkId) {
|
|
|
492
492
|
`networkId name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
|
|
493
493
|
);
|
|
494
494
|
}
|
|
495
|
-
return { networkId,
|
|
495
|
+
return { networkId, ownerDid, name };
|
|
496
496
|
}
|
|
497
|
-
function buildNetworkId(
|
|
498
|
-
if (typeof
|
|
499
|
-
throw new NetworkIdError("
|
|
497
|
+
function buildNetworkId(ownerDid, name) {
|
|
498
|
+
if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
|
|
499
|
+
throw new NetworkIdError("ownerDid must be a DID");
|
|
500
500
|
}
|
|
501
501
|
if (typeof name !== "string" || !NETWORK_NAME_RE.test(name)) {
|
|
502
502
|
throw new NetworkIdError(
|
|
503
503
|
`network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
|
|
504
504
|
);
|
|
505
505
|
}
|
|
506
|
-
const networkId = `${URN_PREFIX}${
|
|
506
|
+
const networkId = `${URN_PREFIX}${ownerDid}:${name}`;
|
|
507
507
|
parseNetworkId(networkId);
|
|
508
508
|
return networkId;
|
|
509
509
|
}
|
|
@@ -580,27 +580,27 @@ function toError(error) {
|
|
|
580
580
|
// src/encryption/discovery.ts
|
|
581
581
|
async function discoverNetwork(input) {
|
|
582
582
|
let networkId;
|
|
583
|
-
let
|
|
583
|
+
let ownerDid;
|
|
584
584
|
let name;
|
|
585
585
|
try {
|
|
586
586
|
if (input.identifier.startsWith("urn:tinycloud:encryption:")) {
|
|
587
587
|
const parsed = parseNetworkId(input.identifier);
|
|
588
588
|
networkId = parsed.networkId;
|
|
589
|
-
|
|
589
|
+
ownerDid = parsed.ownerDid;
|
|
590
590
|
name = parsed.name;
|
|
591
591
|
} else {
|
|
592
|
-
if (input.
|
|
592
|
+
if (input.ownerDid === void 0) {
|
|
593
593
|
return {
|
|
594
594
|
ok: false,
|
|
595
595
|
error: encryptionError({
|
|
596
596
|
code: "INVALID_INPUT",
|
|
597
|
-
message: "discoverNetwork requires `
|
|
597
|
+
message: "discoverNetwork requires `ownerDid` when identifier is a bare network name"
|
|
598
598
|
})
|
|
599
599
|
};
|
|
600
600
|
}
|
|
601
|
-
networkId = `urn:tinycloud:encryption:${input.
|
|
601
|
+
networkId = `urn:tinycloud:encryption:${input.ownerDid}:${input.identifier}`;
|
|
602
602
|
const parsed = parseNetworkId(networkId);
|
|
603
|
-
|
|
603
|
+
ownerDid = parsed.ownerDid;
|
|
604
604
|
name = parsed.name;
|
|
605
605
|
}
|
|
606
606
|
} catch (err2) {
|
|
@@ -619,7 +619,7 @@ async function discoverNetwork(input) {
|
|
|
619
619
|
try {
|
|
620
620
|
const descriptor = await input.node.fetchByNetworkId(networkId);
|
|
621
621
|
if (descriptor !== null) {
|
|
622
|
-
const validated = validateDescriptor(descriptor, networkId,
|
|
622
|
+
const validated = validateDescriptor(descriptor, networkId, ownerDid, name);
|
|
623
623
|
if (!validated.ok) return validated;
|
|
624
624
|
return { ok: true, data: { descriptor: validated.data, source: "node" } };
|
|
625
625
|
}
|
|
@@ -629,11 +629,11 @@ async function discoverNetwork(input) {
|
|
|
629
629
|
if (input.wellKnown !== void 0) {
|
|
630
630
|
try {
|
|
631
631
|
const descriptor = await input.wellKnown.fetchWellKnown(
|
|
632
|
-
|
|
632
|
+
ownerDid,
|
|
633
633
|
networkDiscoveryKey(name)
|
|
634
634
|
);
|
|
635
635
|
if (descriptor !== null) {
|
|
636
|
-
const validated = validateDescriptor(descriptor, networkId,
|
|
636
|
+
const validated = validateDescriptor(descriptor, networkId, ownerDid, name);
|
|
637
637
|
if (!validated.ok) return validated;
|
|
638
638
|
return {
|
|
639
639
|
ok: true,
|
|
@@ -652,7 +652,7 @@ async function discoverNetwork(input) {
|
|
|
652
652
|
})
|
|
653
653
|
};
|
|
654
654
|
}
|
|
655
|
-
function validateDescriptor(descriptor, networkId,
|
|
655
|
+
function validateDescriptor(descriptor, networkId, ownerDid, name) {
|
|
656
656
|
if (descriptor.networkId !== networkId) {
|
|
657
657
|
return {
|
|
658
658
|
ok: false,
|
|
@@ -662,12 +662,12 @@ function validateDescriptor(descriptor, networkId, principal, name) {
|
|
|
662
662
|
})
|
|
663
663
|
};
|
|
664
664
|
}
|
|
665
|
-
if (descriptor.
|
|
665
|
+
if (descriptor.ownerDid !== ownerDid) {
|
|
666
666
|
return {
|
|
667
667
|
ok: false,
|
|
668
668
|
error: encryptionError({
|
|
669
669
|
code: "INVALID_NETWORK_ID",
|
|
670
|
-
message: "descriptor
|
|
670
|
+
message: "descriptor ownerDid does not match networkId ownerDid"
|
|
671
671
|
})
|
|
672
672
|
};
|
|
673
673
|
}
|
|
@@ -1159,10 +1159,10 @@ var EncryptionService = class extends BaseService {
|
|
|
1159
1159
|
get crypto() {
|
|
1160
1160
|
return this._config.crypto;
|
|
1161
1161
|
}
|
|
1162
|
-
async discoverNetwork(identifier,
|
|
1162
|
+
async discoverNetwork(identifier, ownerDid) {
|
|
1163
1163
|
const result = await discoverNetwork({
|
|
1164
1164
|
identifier,
|
|
1165
|
-
...
|
|
1165
|
+
...ownerDid !== void 0 ? { ownerDid } : {},
|
|
1166
1166
|
...this._config.node !== void 0 ? { node: this._config.node } : {},
|
|
1167
1167
|
...this._config.wellKnown !== void 0 ? { wellKnown: this._config.wellKnown } : {}
|
|
1168
1168
|
});
|