@tinycloud/sdk-core 2.2.0-beta.3 → 2.2.0-beta.4

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.cjs CHANGED
@@ -41,6 +41,8 @@ __export(index_exports, {
41
41
  DEFAULT_EXPIRY: () => DEFAULT_EXPIRY,
42
42
  DEFAULT_MANIFEST_SPACE: () => DEFAULT_MANIFEST_SPACE,
43
43
  DEFAULT_MANIFEST_VERSION: () => DEFAULT_MANIFEST_VERSION,
44
+ DEFAULT_TINYCLOUD_FALLBACK_HOST: () => DEFAULT_TINYCLOUD_FALLBACK_HOST,
45
+ DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL: () => DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL,
44
46
  DataVaultService: () => import_sdk_services4.DataVaultService,
45
47
  DatabaseHandle: () => import_sdk_services4.DatabaseHandle,
46
48
  DelegationErrorCodes: () => DelegationErrorCodes,
@@ -106,6 +108,7 @@ __export(index_exports, {
106
108
  parseSpaceUri: () => parseSpaceUri,
107
109
  resolveCloudLocation: () => resolveCloudLocation,
108
110
  resolveManifest: () => resolveManifest,
111
+ resolveTinyCloudHosts: () => resolveTinyCloudHosts,
109
112
  resourceCapabilitiesToAbilitiesMap: () => resourceCapabilitiesToAbilitiesMap,
110
113
  resourceCapabilitiesToSpaceAbilitiesMap: () => resourceCapabilitiesToSpaceAbilitiesMap,
111
114
  serviceError: () => import_sdk_services4.serviceError,
@@ -4349,6 +4352,8 @@ var import_uri_to_multiaddr = require("@multiformats/uri-to-multiaddr");
4349
4352
  var import_ed25519 = require("@noble/curves/ed25519");
4350
4353
  var import_basics = require("multiformats/basics");
4351
4354
  var import_viem = require("viem");
4355
+ var DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL = "https://registry.tinycloud.xyz";
4356
+ var DEFAULT_TINYCLOUD_FALLBACK_HOST = "https://node.tinycloud.xyz";
4352
4357
  var LocationRecordValidationError = class extends Error {
4353
4358
  constructor(message) {
4354
4359
  super(`Location record validation failed: ${message}`);
@@ -4383,7 +4388,9 @@ function canonicalLocationPayload(payload) {
4383
4388
  async function signLocationRecord(payload, signer) {
4384
4389
  validateLocationRecordPayload(payload);
4385
4390
  const message = canonicalLocationPayload(payload);
4386
- const signature = signer.type === "did:pkh" ? await signer.signMessage(message) : base64UrlEncode2(await signer.signBytes(new TextEncoder().encode(message)));
4391
+ const signature = signer.type === "did:pkh" ? await signer.signMessage(message) : base64UrlEncode2(
4392
+ await signer.signBytes(new TextEncoder().encode(message))
4393
+ );
4387
4394
  return { ...payload, signature };
4388
4395
  }
4389
4396
  function validateLocationRecordPayload(input) {
@@ -4397,7 +4404,9 @@ function validateLocationRecordPayload(input) {
4397
4404
  validateSubject(payload.subject);
4398
4405
  validateMultiaddrs(payload.multiaddrs);
4399
4406
  if (typeof payload.updated_at !== "string" || Number.isNaN(Date.parse(payload.updated_at))) {
4400
- throw new LocationRecordValidationError("updated_at must be an ISO timestamp");
4407
+ throw new LocationRecordValidationError(
4408
+ "updated_at must be an ISO timestamp"
4409
+ );
4401
4410
  }
4402
4411
  if (typeof payload.sequence !== "number" || !Number.isSafeInteger(payload.sequence) || payload.sequence < 0) {
4403
4412
  throw new LocationRecordValidationError(
@@ -4416,7 +4425,9 @@ function validateLocationRecord(input) {
4416
4425
  const payload = validateLocationRecordPayload(input);
4417
4426
  const signature = input.signature;
4418
4427
  if (typeof signature !== "string" || signature.length === 0) {
4419
- throw new LocationRecordValidationError("signature must be a non-empty string");
4428
+ throw new LocationRecordValidationError(
4429
+ "signature must be a non-empty string"
4430
+ );
4420
4431
  }
4421
4432
  return { ...payload, signature };
4422
4433
  }
@@ -4468,6 +4479,22 @@ async function resolveCloudLocation(subject, options = {}) {
4468
4479
  resolvedAt: (/* @__PURE__ */ new Date()).toISOString()
4469
4480
  };
4470
4481
  }
4482
+ async function resolveTinyCloudHosts(subject, options = {}) {
4483
+ const location = await resolveCloudLocation(subject, {
4484
+ explicitMultiaddrs: hostsToMultiaddrs(options.explicitHosts),
4485
+ blockchain: options.blockchain,
4486
+ centralizedRegistryUrl: options.registryUrl === null ? void 0 : options.registryUrl ?? DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL,
4487
+ fallbackMultiaddrs: hostsToMultiaddrs(
4488
+ options.fallbackHosts === null ? void 0 : options.fallbackHosts ?? [DEFAULT_TINYCLOUD_FALLBACK_HOST]
4489
+ ),
4490
+ fetch: options.fetch,
4491
+ verifyRecords: options.verifyRecords
4492
+ });
4493
+ return {
4494
+ hosts: location.multiaddrs.map((addr) => multiaddrToHttpUrl(addr)),
4495
+ location
4496
+ };
4497
+ }
4471
4498
  function multiaddrToHttpUrl(input) {
4472
4499
  const uri = (0, import_multiaddr_to_uri.multiaddrToUri)((0, import_multiaddr.multiaddr)(input));
4473
4500
  if (!uri.startsWith("http://") && !uri.startsWith("https://")) {
@@ -4484,6 +4511,14 @@ function httpUrlToMultiaddr(input) {
4484
4511
  }
4485
4512
  return (0, import_uri_to_multiaddr.uriToMultiaddr)(url.toString()).toString();
4486
4513
  }
4514
+ function hostsToMultiaddrs(hosts) {
4515
+ if (hosts === void 0 || hosts.length === 0) {
4516
+ return void 0;
4517
+ }
4518
+ return hosts.map(
4519
+ (host) => host.startsWith("/") ? host : httpUrlToMultiaddr(host)
4520
+ );
4521
+ }
4487
4522
  async function resolveExplicit(subject, multiaddrs) {
4488
4523
  return resolveAttempt("explicit", async () => {
4489
4524
  if (multiaddrs === void 0 || multiaddrs.length === 0) {
@@ -4497,7 +4532,12 @@ async function resolveBlockchain(subject, resolver, verifyRecords) {
4497
4532
  if (!resolver) {
4498
4533
  return null;
4499
4534
  }
4500
- return toCandidate(subject, "blockchain", await resolver(subject), verifyRecords);
4535
+ return toCandidate(
4536
+ subject,
4537
+ "blockchain",
4538
+ await resolver(subject),
4539
+ verifyRecords
4540
+ );
4501
4541
  });
4502
4542
  }
4503
4543
  async function resolveCentralized(subject, options, verifyRecords) {
@@ -4549,13 +4589,17 @@ async function toCandidate(subject, source, input, verifyRecord) {
4549
4589
  );
4550
4590
  }
4551
4591
  if (verifyRecord && !await verifyLocationRecord(record)) {
4552
- throw new LocationRecordValidationError("location record signature is invalid");
4592
+ throw new LocationRecordValidationError(
4593
+ "location record signature is invalid"
4594
+ );
4553
4595
  }
4554
4596
  return { source, multiaddrs: [...record.multiaddrs], record };
4555
4597
  }
4556
4598
  const candidateInput = input;
4557
4599
  if (!Array.isArray(candidateInput.multiaddrs)) {
4558
- throw new LocationRecordValidationError("candidate multiaddrs must be an array");
4600
+ throw new LocationRecordValidationError(
4601
+ "candidate multiaddrs must be an array"
4602
+ );
4559
4603
  }
4560
4604
  validateMultiaddrs(candidateInput.multiaddrs);
4561
4605
  if (candidateInput.record !== void 0) {
@@ -4566,7 +4610,9 @@ async function toCandidate(subject, source, input, verifyRecord) {
4566
4610
  );
4567
4611
  }
4568
4612
  if (verifyRecord && !await verifyLocationRecord(record)) {
4569
- throw new LocationRecordValidationError("location record signature is invalid");
4613
+ throw new LocationRecordValidationError(
4614
+ "location record signature is invalid"
4615
+ );
4570
4616
  }
4571
4617
  return { source, multiaddrs: [...candidateInput.multiaddrs], record };
4572
4618
  }
@@ -4574,10 +4620,14 @@ async function toCandidate(subject, source, input, verifyRecord) {
4574
4620
  }
4575
4621
  function validateSubject(subject) {
4576
4622
  if (typeof subject !== "string" || subject.length === 0) {
4577
- throw new LocationRecordValidationError("subject must be a non-empty string");
4623
+ throw new LocationRecordValidationError(
4624
+ "subject must be a non-empty string"
4625
+ );
4578
4626
  }
4579
4627
  if (!subject.startsWith("did:pkh:") && !subject.startsWith("did:key:")) {
4580
- throw new LocationRecordValidationError("subject must be did:pkh or did:key");
4628
+ throw new LocationRecordValidationError(
4629
+ "subject must be did:pkh or did:key"
4630
+ );
4581
4631
  }
4582
4632
  }
4583
4633
  function validateMultiaddrs(input) {
@@ -4621,16 +4671,24 @@ function verifyDidKeySignature(did, payload, signature) {
4621
4671
  "did:key signature must be a base64url Ed25519 signature"
4622
4672
  );
4623
4673
  }
4624
- return import_ed25519.ed25519.verify(signatureBytes, new TextEncoder().encode(payload), publicKey);
4674
+ return import_ed25519.ed25519.verify(
4675
+ signatureBytes,
4676
+ new TextEncoder().encode(payload),
4677
+ publicKey
4678
+ );
4625
4679
  }
4626
4680
  function ed25519PublicKeyFromDidKey(did) {
4627
4681
  const identifier = did.slice("did:key:".length);
4628
4682
  if (!identifier.startsWith("z")) {
4629
- throw new LocationRecordValidationError("did:key must use base58btc multibase");
4683
+ throw new LocationRecordValidationError(
4684
+ "did:key must use base58btc multibase"
4685
+ );
4630
4686
  }
4631
4687
  const bytes = import_basics.bases.base58btc.decode(identifier);
4632
4688
  if (bytes.length !== 34 || bytes[0] !== 237 || bytes[1] !== 1) {
4633
- throw new LocationRecordValidationError("did:key must be an Ed25519 public key");
4689
+ throw new LocationRecordValidationError(
4690
+ "did:key must be an Ed25519 public key"
4691
+ );
4634
4692
  }
4635
4693
  return bytes.slice(2);
4636
4694
  }
@@ -4801,6 +4859,8 @@ function parseRecapCapabilities(parseWasm, siwe) {
4801
4859
  DEFAULT_EXPIRY,
4802
4860
  DEFAULT_MANIFEST_SPACE,
4803
4861
  DEFAULT_MANIFEST_VERSION,
4862
+ DEFAULT_TINYCLOUD_FALLBACK_HOST,
4863
+ DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL,
4804
4864
  DataVaultService,
4805
4865
  DatabaseHandle,
4806
4866
  DelegationErrorCodes,
@@ -4866,6 +4926,7 @@ function parseRecapCapabilities(parseWasm, siwe) {
4866
4926
  parseSpaceUri,
4867
4927
  resolveCloudLocation,
4868
4928
  resolveManifest,
4929
+ resolveTinyCloudHosts,
4869
4930
  resourceCapabilitiesToAbilitiesMap,
4870
4931
  resourceCapabilitiesToSpaceAbilitiesMap,
4871
4932
  serviceError,