@tinycloud/sdk-services 3.1.0-beta.2 → 3.1.0-beta.6

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.
@@ -124,6 +124,106 @@ function err(error) {
124
124
  return { ok: false, error };
125
125
  }
126
126
 
127
+ // src/errors.ts
128
+ var import_bootstrap = require("@tinycloud/bootstrap");
129
+
130
+ // src/encryption/networkId.ts
131
+ var URN_PREFIX = "urn:tinycloud:encryption:";
132
+ var NETWORK_NAME_RE = /^[a-z0-9][a-z0-9-]*$/;
133
+ var PKH_EIP155_DID_RE = /^did:pkh:eip155:(\d+):(0x[a-fA-F0-9]{40})$/;
134
+ var NetworkIdError = class extends Error {
135
+ constructor(message) {
136
+ super(message);
137
+ this.name = "NetworkIdError";
138
+ }
139
+ };
140
+ function parseNetworkId(networkId) {
141
+ if (typeof networkId !== "string" || networkId.length === 0) {
142
+ throw new NetworkIdError("networkId must be a non-empty string");
143
+ }
144
+ if (!networkId.startsWith(URN_PREFIX)) {
145
+ throw new NetworkIdError(
146
+ `networkId must start with ${URN_PREFIX} (got ${JSON.stringify(networkId)})`
147
+ );
148
+ }
149
+ const body = networkId.slice(URN_PREFIX.length);
150
+ const lastColon = body.lastIndexOf(":");
151
+ if (lastColon <= 0 || lastColon === body.length - 1) {
152
+ throw new NetworkIdError(
153
+ `networkId missing ownerDid or name segment (got ${JSON.stringify(networkId)})`
154
+ );
155
+ }
156
+ const ownerDid = body.slice(0, lastColon);
157
+ const name = body.slice(lastColon + 1);
158
+ if (!ownerDid.startsWith("did:")) {
159
+ throw new NetworkIdError(
160
+ `networkId ownerDid must be a DID (got ${JSON.stringify(ownerDid)})`
161
+ );
162
+ }
163
+ const didParts = ownerDid.split(":");
164
+ if (didParts.length < 3 || didParts.some((p) => p.length === 0)) {
165
+ throw new NetworkIdError(
166
+ `networkId ownerDid is not a well-formed DID (got ${JSON.stringify(ownerDid)})`
167
+ );
168
+ }
169
+ if (!NETWORK_NAME_RE.test(name)) {
170
+ throw new NetworkIdError(
171
+ `networkId name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
172
+ );
173
+ }
174
+ return { networkId, ownerDid, name };
175
+ }
176
+ function buildNetworkId(ownerDid, name) {
177
+ if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
178
+ throw new NetworkIdError("ownerDid must be a DID");
179
+ }
180
+ if (typeof name !== "string" || !NETWORK_NAME_RE.test(name)) {
181
+ throw new NetworkIdError(
182
+ `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
183
+ );
184
+ }
185
+ const networkId = `${URN_PREFIX}${ownerDid}:${name}`;
186
+ parseNetworkId(networkId);
187
+ return networkId;
188
+ }
189
+ function isNetworkId(networkId) {
190
+ if (typeof networkId !== "string") {
191
+ return false;
192
+ }
193
+ try {
194
+ parseNetworkId(networkId);
195
+ return true;
196
+ } catch {
197
+ return false;
198
+ }
199
+ }
200
+ function parsePkhOwnerDid(ownerDid) {
201
+ const match = ownerDid.match(PKH_EIP155_DID_RE);
202
+ if (!match) return null;
203
+ return {
204
+ chainId: match[1],
205
+ address: match[2].toLowerCase()
206
+ };
207
+ }
208
+ function ownerDidMatches(a, b) {
209
+ const aPkh = parsePkhOwnerDid(a);
210
+ const bPkh = parsePkhOwnerDid(b);
211
+ if (aPkh && bPkh) {
212
+ return aPkh.chainId === bPkh.chainId && aPkh.address === bPkh.address;
213
+ }
214
+ return a === b;
215
+ }
216
+ function networkDiscoveryKey(name) {
217
+ if (!NETWORK_NAME_RE.test(name)) {
218
+ throw new NetworkIdError(
219
+ `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
220
+ );
221
+ }
222
+ return `.well-known/encryption/network/${name}`;
223
+ }
224
+ var ENCRYPTION_NETWORK_URN_PREFIX = URN_PREFIX;
225
+ var NETWORK_NAME_PATTERN = NETWORK_NAME_RE;
226
+
127
227
  // src/errors.ts
128
228
  function timeoutError(service) {
129
229
  return {
@@ -139,6 +239,12 @@ function abortedError(service) {
139
239
  service
140
240
  };
141
241
  }
242
+ var grantableActions = new Set(
243
+ import_bootstrap.CAPABILITY_REGISTRY.map(({ urn }) => urn).filter((urn) => !urn.endsWith("/*"))
244
+ );
245
+ var capabilityServices = new Set(
246
+ import_bootstrap.CAPABILITY_REGISTRY.map(({ service }) => service.slice("tinycloud.".length))
247
+ );
142
248
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
143
249
  if (error instanceof Error) {
144
250
  if (error.name === "AbortError") {
@@ -481,103 +587,6 @@ function canonicalHashHex(sha256, value) {
481
587
  return hexEncode(sha256(utf8Encode(canonical)));
482
588
  }
483
589
 
484
- // src/encryption/networkId.ts
485
- var URN_PREFIX = "urn:tinycloud:encryption:";
486
- var NETWORK_NAME_RE = /^[a-z0-9][a-z0-9-]*$/;
487
- var PKH_EIP155_DID_RE = /^did:pkh:eip155:(\d+):(0x[a-fA-F0-9]{40})$/;
488
- var NetworkIdError = class extends Error {
489
- constructor(message) {
490
- super(message);
491
- this.name = "NetworkIdError";
492
- }
493
- };
494
- function parseNetworkId(networkId) {
495
- if (typeof networkId !== "string" || networkId.length === 0) {
496
- throw new NetworkIdError("networkId must be a non-empty string");
497
- }
498
- if (!networkId.startsWith(URN_PREFIX)) {
499
- throw new NetworkIdError(
500
- `networkId must start with ${URN_PREFIX} (got ${JSON.stringify(networkId)})`
501
- );
502
- }
503
- const body = networkId.slice(URN_PREFIX.length);
504
- const lastColon = body.lastIndexOf(":");
505
- if (lastColon <= 0 || lastColon === body.length - 1) {
506
- throw new NetworkIdError(
507
- `networkId missing ownerDid or name segment (got ${JSON.stringify(networkId)})`
508
- );
509
- }
510
- const ownerDid = body.slice(0, lastColon);
511
- const name = body.slice(lastColon + 1);
512
- if (!ownerDid.startsWith("did:")) {
513
- throw new NetworkIdError(
514
- `networkId ownerDid must be a DID (got ${JSON.stringify(ownerDid)})`
515
- );
516
- }
517
- const didParts = ownerDid.split(":");
518
- if (didParts.length < 3 || didParts.some((p) => p.length === 0)) {
519
- throw new NetworkIdError(
520
- `networkId ownerDid is not a well-formed DID (got ${JSON.stringify(ownerDid)})`
521
- );
522
- }
523
- if (!NETWORK_NAME_RE.test(name)) {
524
- throw new NetworkIdError(
525
- `networkId name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
526
- );
527
- }
528
- return { networkId, ownerDid, name };
529
- }
530
- function buildNetworkId(ownerDid, name) {
531
- if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
532
- throw new NetworkIdError("ownerDid must be a DID");
533
- }
534
- if (typeof name !== "string" || !NETWORK_NAME_RE.test(name)) {
535
- throw new NetworkIdError(
536
- `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
537
- );
538
- }
539
- const networkId = `${URN_PREFIX}${ownerDid}:${name}`;
540
- parseNetworkId(networkId);
541
- return networkId;
542
- }
543
- function isNetworkId(networkId) {
544
- if (typeof networkId !== "string") {
545
- return false;
546
- }
547
- try {
548
- parseNetworkId(networkId);
549
- return true;
550
- } catch {
551
- return false;
552
- }
553
- }
554
- function parsePkhOwnerDid(ownerDid) {
555
- const match = ownerDid.match(PKH_EIP155_DID_RE);
556
- if (!match) return null;
557
- return {
558
- chainId: match[1],
559
- address: match[2].toLowerCase()
560
- };
561
- }
562
- function ownerDidMatches(a, b) {
563
- const aPkh = parsePkhOwnerDid(a);
564
- const bPkh = parsePkhOwnerDid(b);
565
- if (aPkh && bPkh) {
566
- return aPkh.chainId === bPkh.chainId && aPkh.address === bPkh.address;
567
- }
568
- return a === b;
569
- }
570
- function networkDiscoveryKey(name) {
571
- if (!NETWORK_NAME_RE.test(name)) {
572
- throw new NetworkIdError(
573
- `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
574
- );
575
- }
576
- return `.well-known/encryption/network/${name}`;
577
- }
578
- var ENCRYPTION_NETWORK_URN_PREFIX = URN_PREFIX;
579
- var NETWORK_NAME_PATTERN = NETWORK_NAME_RE;
580
-
581
590
  // src/encryption/types.ts
582
591
  var DEFAULT_ENCRYPTION_ALG = "x25519-aes256gcm/v1";
583
592
  var ENVELOPE_VERSION = 1;