@tinycloud/cli 0.7.7-beta.2 → 0.7.7-beta.3

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
@@ -43446,6 +43446,43 @@ async function createSDKInstance(ctx, options) {
43446
43446
  await replayAdditionalDelegations(node, ctx.profile);
43447
43447
  return node;
43448
43448
  }
43449
+ async function bootstrapDelegatedSession(ctx, delegation) {
43450
+ const profile = await ProfileManager.getProfile(ctx.profile);
43451
+ if (resolveProfilePosture(profile) !== "delegate-session") {
43452
+ throw new CLIError(
43453
+ "AUTH_REQUIRED",
43454
+ `Profile "${ctx.profile}" is not a delegate-session profile.`,
43455
+ ExitCode.AUTH_REQUIRED
43456
+ );
43457
+ }
43458
+ const sessionDid = profile.sessionDid ?? profile.did;
43459
+ if (delegation.delegateDID.split("#", 1)[0] !== sessionDid.split("#", 1)[0]) {
43460
+ throw new CLIError(
43461
+ "DELEGATION_AUDIENCE_MISMATCH",
43462
+ `Delegation targets ${delegation.delegateDID}, but profile "${ctx.profile}" uses ${sessionDid}.`,
43463
+ ExitCode.PERMISSION_DENIED
43464
+ );
43465
+ }
43466
+ const key2 = await ProfileManager.getKey(ctx.profile);
43467
+ const jwk = signerJwkForProfile(ctx.profile, void 0, key2);
43468
+ await ProfileManager.setSession(ctx.profile, {
43469
+ delegationHeader: delegation.delegationHeader,
43470
+ delegationCid: delegation.cid,
43471
+ spaceId: delegation.spaceId,
43472
+ jwk,
43473
+ verificationMethod: sessionDid,
43474
+ address: delegation.ownerAddress,
43475
+ chainId: delegation.chainId,
43476
+ siwe: "",
43477
+ signature: ""
43478
+ });
43479
+ await ProfileManager.setProfile(ctx.profile, {
43480
+ ...profile,
43481
+ sessionDid,
43482
+ spaceId: delegation.spaceId
43483
+ });
43484
+ return createSDKInstance(ctx);
43485
+ }
43449
43486
  async function ensureAuthenticated(ctx, options) {
43450
43487
  if (options?.privateKey) {
43451
43488
  return createSDKInstance(ctx, options);
@@ -43745,7 +43782,15 @@ function registerAuthCommand(program2) {
43745
43782
  return;
43746
43783
  }
43747
43784
  const imported = normalizeDelegationImport(parsed);
43748
- const node = await ensureAuthenticated(ctx);
43785
+ let node;
43786
+ try {
43787
+ node = await ensureAuthenticated(ctx);
43788
+ } catch (error) {
43789
+ const profile = await ProfileManager.getProfile(ctx.profile);
43790
+ const session = await ProfileManager.getSession(ctx.profile);
43791
+ if (session || resolveProfilePosture(profile) !== "delegate-session") throw error;
43792
+ node = await bootstrapDelegatedSession(ctx, imported.delegation);
43793
+ }
43749
43794
  await appendAdditionalDelegation(ctx.profile, storedAdditionalDelegation(
43750
43795
  imported.delegation,
43751
43796
  imported.permissions