@calimero-network/mero-js 19.9.0 → 19.11.0

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.
@@ -1,5 +1,5 @@
1
1
  import { HttpClient } from '../http-client/index.js';
2
- import type { HealthStatus, AdminAuthStatus, InstallApplicationRequest, InstallApplicationResponseData, InstallDevApplicationRequest, UninstallApplicationResponseData, ListApplicationsResponseData, GetApplicationResponseData, ApplicationVersionEntry, GetApplicationAbiResponseData, GetLatestVersionResponseData, ListPackagesResponseData, ListVersionsResponseData, CreateContextRequest, CreateContextResponseData, DeleteContextResponseData, GetContextsResponseData, Context, GenerateContextIdentityResponseData, GetContextIdentitiesResponseData, JoinContextResponseData, JoinSubgroupInheritanceResponseData, ContextGroupResponseData, ContextStorageResponseData, InviteSpecializedNodeRequest, InviteSpecializedNodeResponseData, UpdateContextApplicationRequest, ResyncContextRequest, ResyncContextResponseData, ContextsWithExecutorsResponseData, UploadBlobRequest, UploadBlobResponseData, DeleteBlobResponseData, ListBlobsResponseData, GetBlobInfoResponseData, BlobReadOptions, CreateContextAliasRequest, CreateApplicationAliasRequest, CreateDeviceAliasRequest, CreateAliasResponseData, LookupAliasResponseData, DeleteAliasResponseData, ListAliasesResponseData, ListNamespacesResponseData, CreateNamespaceRequest, CreateNamespaceResponseData, DeleteNamespaceResponseData, CreateNamespaceInvitationRequest, CreateNamespaceInvitationResponseData, CreateRecursiveInvitationResponseData, JoinNamespaceRequest, JoinNamespaceResponseData, CreateGroupInNamespaceRequest, CreateGroupInNamespaceResponseData, SubgroupEntry, Namespace, NamespaceIdentity, NodeIdentity, AccountPairInitRequest, AccountPairInitResponseData, AccountPairCompleteRequest, AccountPairCompleteResponseData, RelinkDeviceRequest, RelinkDeviceResponseData, AccountDeviceEntry, AccountApplicationEntry, RevokeAccountDeviceRequest, RevokeAccountDeviceResponseData, GroupInfoResponseData, CreateGroupRequest, CreateGroupResponseData, DeleteGroupResponseData, ListGroupMembersResponseData, ListGroupContextsResponseData, AddGroupMembersRequest, RemoveGroupMembersRequest, UpdateMemberRoleRequest, MemberCapabilities, SetMemberCapabilitiesRequest, SetDefaultCapabilitiesRequest, SetMemberAutoFollowRequest, SetSubgroupVisibilityRequest, SetTeeAdmissionPolicyRequest, GetTeeAdmissionPolicyResponseData, SetGroupMetadataRequest, SetMemberMetadataRequest, SetContextMetadataRequest, MetadataRecord, SyncGroupResponseData, UpgradeGroupRequest, UpgradeGroupResponseData, GroupUpgradeStatusResponseData, MigrationStatus, CascadeStatusEntry, RetryGroupUpgradeResponseData, ReparentGroupRequest, ReparentGroupResponseData, IssueOwnershipProofRequest, IssueNamespaceOwnershipProofRequest, IssueOwnershipProofResponseData, CreateGroupInvitationRequest, CreateGroupInvitationResponseData, CreateRecursiveGroupInvitationResponseData, JoinGroupRequest, JoinGroupResponseData, TeeInfoResponseData, TeeAttestRequest, TeeAttestResponseData, TeeVerifyQuoteRequest, TeeVerifyQuoteResponseData, PerformIntentRequest, PerformIntentResponseData, IntentRelayInfo, AdmitJoinRequest, AdmitJoinResponseData } from './admin-types.js';
2
+ import type { HealthStatus, AdminAuthStatus, InstallApplicationRequest, InstallApplicationResponseData, InstallDevApplicationRequest, UninstallApplicationResponseData, ListApplicationsResponseData, GetApplicationResponseData, ApplicationVersionEntry, GetApplicationAbiResponseData, GetLatestVersionResponseData, ListPackagesResponseData, ListVersionsResponseData, CreateContextRequest, CreateContextResponseData, DeleteContextResponseData, GetContextsResponseData, Context, GenerateContextIdentityResponseData, GetContextIdentitiesResponseData, JoinContextResponseData, JoinSubgroupInheritanceResponseData, ContextGroupResponseData, ContextStorageResponseData, InviteSpecializedNodeRequest, InviteSpecializedNodeResponseData, UpdateContextApplicationRequest, ResyncContextRequest, ResyncContextResponseData, ContextsWithExecutorsResponseData, UploadBlobRequest, UploadBlobResponseData, DeleteBlobResponseData, ListBlobsResponseData, GetBlobInfoResponseData, BlobReadOptions, CreateContextAliasRequest, CreateApplicationAliasRequest, CreateDeviceAliasRequest, CreateAliasResponseData, LookupAliasResponseData, DeleteAliasResponseData, ListAliasesResponseData, ListNamespacesResponseData, CreateNamespaceRequest, CreateNamespaceResponseData, DeleteNamespaceResponseData, CreateNamespaceInvitationRequest, CreateNamespaceInvitationResponseData, CreateRecursiveInvitationResponseData, JoinNamespaceRequest, JoinNamespaceResponseData, CreateGroupInNamespaceRequest, CreateGroupInNamespaceResponseData, SubgroupEntry, Namespace, NamespaceIdentity, NodeIdentity, AccountPairInitRequest, AccountPairInitResponseData, AccountPairCompleteRequest, AccountPairCompleteResponseData, RelinkDeviceRequest, RelinkDeviceResponseData, RescopeDeviceRequest, RescopeDeviceResponseData, AccountDeviceEntry, AccountApplicationEntry, RevokeAccountDeviceRequest, RevokeAccountDeviceResponseData, GroupInfoResponseData, CreateGroupRequest, CreateGroupResponseData, DeleteGroupResponseData, ListGroupMembersResponseData, ListGroupContextsResponseData, AddGroupMembersRequest, RemoveGroupMembersRequest, UpdateMemberRoleRequest, MemberCapabilities, SetMemberCapabilitiesRequest, SetDefaultCapabilitiesRequest, SetMemberAutoFollowRequest, SetSubgroupVisibilityRequest, SetTeeAdmissionPolicyRequest, GetTeeAdmissionPolicyResponseData, SetGroupMetadataRequest, SetMemberMetadataRequest, SetContextMetadataRequest, MetadataRecord, SyncGroupResponseData, UpgradeGroupRequest, UpgradeGroupResponseData, GroupUpgradeStatusResponseData, MigrationStatus, CascadeStatusEntry, RetryGroupUpgradeResponseData, ReparentGroupRequest, ReparentGroupResponseData, IssueOwnershipProofRequest, IssueNamespaceOwnershipProofRequest, IssueOwnershipProofResponseData, CreateGroupInvitationRequest, CreateGroupInvitationResponseData, CreateRecursiveGroupInvitationResponseData, JoinGroupRequest, JoinGroupResponseData, TeeInfoResponseData, TeeAttestRequest, TeeAttestResponseData, TeeVerifyQuoteRequest, TeeVerifyQuoteResponseData, PerformIntentRequest, PerformIntentResponseData, IntentRelayInfo, AdmitJoinRequest, AdmitJoinResponseData } from './admin-types.js';
3
3
  /**
4
4
  * Compare two version strings by SemVer 2.0 precedence, ascending: negative if
5
5
  * `a < b`, positive if `a > b`, `0` if equal. Build metadata (`+...`) is stripped
@@ -272,6 +272,14 @@ export declare class AdminApiClient {
272
272
  * would make the accidental request the widest one.
273
273
  */
274
274
  relinkAccountDevice(deviceId: string, request?: RelinkDeviceRequest): Promise<RelinkDeviceResponseData>;
275
+ /**
276
+ * Replace a device's scope outright, in one root-signed op.
277
+ *
278
+ * {@link relinkAccountDevice} only ever adds applications; this is how a
279
+ * scope is narrowed - or an `all` device is given a fixed list instead. The
280
+ * device need not be online.
281
+ */
282
+ rescopeAccountDevice(deviceId: string, request: RescopeDeviceRequest): Promise<RescopeDeviceResponseData>;
275
283
  /**
276
284
  * Every device of this account, as this node sees them - its own included.
277
285
  *
@@ -587,6 +587,16 @@ export class AdminApiClient {
587
587
  async relinkAccountDevice(deviceId, request) {
588
588
  return unwrap(await this.httpClient.post(`/admin-api/account/devices/${deviceId}/relink`, request ?? {}));
589
589
  }
590
+ /**
591
+ * Replace a device's scope outright, in one root-signed op.
592
+ *
593
+ * {@link relinkAccountDevice} only ever adds applications; this is how a
594
+ * scope is narrowed - or an `all` device is given a fixed list instead. The
595
+ * device need not be online.
596
+ */
597
+ async rescopeAccountDevice(deviceId, request) {
598
+ return unwrap(await this.httpClient.put(`/admin-api/account/devices/${encodeURIComponent(deviceId)}/scope`, request));
599
+ }
590
600
  /**
591
601
  * Every device of this account, as this node sees them - its own included.
592
602
  *
@@ -811,6 +811,25 @@ export interface RelinkDeviceRequest {
811
811
  */
812
812
  applications?: string[];
813
813
  }
814
+ /**
815
+ * The scope to hold: every application, or only the ones named.
816
+ *
817
+ * A tagged shape rather than a list whose emptiness means everything - `all`
818
+ * and `only: []` differ by a whole order of magnitude in what they grant, and
819
+ * an empty `only` is refused rather than treated as `all`.
820
+ */
821
+ export type DeviceScope = 'all' | {
822
+ only: string[];
823
+ };
824
+ /**
825
+ * Replace a device's scope outright, in one root-signed op.
826
+ *
827
+ * {@link relinkAccountDevice} only ever adds applications; this is how a
828
+ * scope is narrowed, or an `all` device is given a fixed list instead.
829
+ */
830
+ export interface RescopeDeviceRequest {
831
+ scope: DeviceScope;
832
+ }
814
833
  /** One namespace the relink published the device's link into. */
815
834
  export interface RelinkOutcomeEntry {
816
835
  /** The namespace id, 64 hex characters. */
@@ -834,6 +853,31 @@ export interface RelinkSkipEntry {
834
853
  */
835
854
  reason: string;
836
855
  }
856
+ /** One namespace a scope replacement no longer reaches. */
857
+ export interface RescopeDescopeEntry {
858
+ /** The namespace id, 64 hex characters. */
859
+ namespaceId: string;
860
+ /**
861
+ * Whether the scope key was rotated. `false` means the device stopped writing
862
+ * there but still holds the key it had, until an admin rotates.
863
+ */
864
+ keyRotated: boolean;
865
+ }
866
+ /** The scope a device now holds, and what each namespace did about it. */
867
+ export interface RescopeDeviceResponseData {
868
+ /** The account the device speaks for, 64 hex characters. */
869
+ accountId: string;
870
+ /** The device that was rescoped, 64 hex characters. */
871
+ deviceId: string;
872
+ /** The scope after the request, 64 hex each. Empty means every application. */
873
+ applications: string[];
874
+ /** Namespaces the new scope no longer reaches. */
875
+ descoped: RescopeDescopeEntry[];
876
+ /** Namespaces the device was linked into by this call. */
877
+ linkedIn: RelinkOutcomeEntry[];
878
+ /** Namespaces nothing was published into, and why. */
879
+ skipped: RelinkSkipEntry[];
880
+ }
837
881
  /** What the relink repaired, and what it left alone. */
838
882
  export interface RelinkDeviceResponseData {
839
883
  /** The account the device speaks for, 64 hex characters. */
@@ -240,6 +240,22 @@ export interface AccountLinkProof {
240
240
  /** `signAccountLink`'s output — standard base64. */
241
241
  signature: string;
242
242
  }
243
+ /** Where to send someone so a cloud login can consent to linking an account. */
244
+ export interface AccountLinkHandoff {
245
+ /** The cloud portal URL to open — a new tab, a popup, or the system browser. */
246
+ url: string;
247
+ /** The account the consent will be for, echoed so a caller can show it. */
248
+ accountId: string;
249
+ }
250
+ /** What comes back on the callback: a grant, a refusal, or neither. */
251
+ export interface AccountLinkCallback {
252
+ /** The grant to spend with {@link CloudClient.linkAccountWithGrant}. */
253
+ grant?: string;
254
+ /** The account the grant is for, as the portal echoed it. */
255
+ accountId?: string;
256
+ /** `'denied'` when the person cancelled. Absent on success. */
257
+ error?: string;
258
+ }
243
259
  /** A single-use challenge for an account root to sign itself in with. */
244
260
  export interface AccountLoginChallenge {
245
261
  /** Sign this verbatim. Opaque and sealed by the cloud; nonces live ~2 minutes. */
@@ -544,6 +560,64 @@ export declare class CloudClient {
544
560
  * key rather than letting a user type either.
545
561
  */
546
562
  submitAccountLink(proof: AccountLinkProof): Promise<CloudAccountLink>;
563
+ /**
564
+ * Where to send someone to authorise linking this account, and what to expect
565
+ * back. Pairs with {@link readAccountLinkCallback} and
566
+ * {@link linkAccountWithGrant}.
567
+ *
568
+ * This exists because a browser app is in a bind: it holds an account root and
569
+ * can never hold a *first* cloud session, since only Google issues one. So the
570
+ * person goes to the cloud, where they are signed in, and consents there. What
571
+ * comes back is a grant — consent to link this one account, spendable only by
572
+ * whoever can sign with its root.
573
+ *
574
+ * @param callbackUrl where the portal sends the person back. Must be an origin
575
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
576
+ * here. The grant arrives in the URL **fragment**, which browsers do not
577
+ * send to servers.
578
+ *
579
+ * Static, so it needs no client and no session: there is nothing to
580
+ * authenticate as yet.
581
+ */
582
+ static accountLinkHandoff(options: {
583
+ /** The cloud portal's origin, e.g. `https://cloud.calimero.network`. */
584
+ portalUrl: string;
585
+ /** The account to be linked, 64 hex — derive it, never let a user type it. */
586
+ accountId: string;
587
+ /** Where to come back to. */
588
+ callbackUrl: string;
589
+ }): AccountLinkHandoff;
590
+ /**
591
+ * Read the portal's answer out of a callback URL's fragment.
592
+ *
593
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
594
+ * load — which is how a redirected app finds out it has been sent back.
595
+ *
596
+ * @param href defaults to the current location, when there is one.
597
+ */
598
+ static readAccountLinkCallback(href?: string): AccountLinkCallback;
599
+ /**
600
+ * Spend a link grant: sign it with the account root and post the pair.
601
+ *
602
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
603
+ * which is the whole reason the grant exists. The grant carries the consenting
604
+ * login sealed inside it, so nothing here names one; a caller-supplied email
605
+ * would be a field to lie in.
606
+ *
607
+ * The signature is an ordinary account-link signature with the grant as the
608
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
609
+ * this one. Both halves are required: a grant without the key links nothing,
610
+ * which is what makes it safe to carry through a browser redirect.
611
+ *
612
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
613
+ * that names a different account; `409` that the account belongs to another
614
+ * login; `402` that the plan is full.
615
+ */
616
+ linkAccountWithGrant(options: {
617
+ grant: string;
618
+ /** The root of the account the grant names, 64 hex. */
619
+ rootSecret: string;
620
+ }): Promise<CloudAccountLink>;
547
621
  /** The accounts linked to this login, and how many the plan allows. */
548
622
  getMyAccounts(): Promise<CloudLinkedAccounts>;
549
623
  private adoptSession;
@@ -489,6 +489,101 @@ export class CloudClient {
489
489
  alreadyLinked: body.already_linked === true,
490
490
  };
491
491
  }
492
+ /**
493
+ * Where to send someone to authorise linking this account, and what to expect
494
+ * back. Pairs with {@link readAccountLinkCallback} and
495
+ * {@link linkAccountWithGrant}.
496
+ *
497
+ * This exists because a browser app is in a bind: it holds an account root and
498
+ * can never hold a *first* cloud session, since only Google issues one. So the
499
+ * person goes to the cloud, where they are signed in, and consents there. What
500
+ * comes back is a grant — consent to link this one account, spendable only by
501
+ * whoever can sign with its root.
502
+ *
503
+ * @param callbackUrl where the portal sends the person back. Must be an origin
504
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
505
+ * here. The grant arrives in the URL **fragment**, which browsers do not
506
+ * send to servers.
507
+ *
508
+ * Static, so it needs no client and no session: there is nothing to
509
+ * authenticate as yet.
510
+ */
511
+ static accountLinkHandoff(options) {
512
+ const portal = options.portalUrl.replace(/\/+$/, '');
513
+ const query = new URLSearchParams({
514
+ 'link-account': options.accountId,
515
+ 'callback-url': options.callbackUrl,
516
+ });
517
+ return { url: `${portal}/?${query.toString()}`, accountId: options.accountId };
518
+ }
519
+ /**
520
+ * Read the portal's answer out of a callback URL's fragment.
521
+ *
522
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
523
+ * load — which is how a redirected app finds out it has been sent back.
524
+ *
525
+ * @param href defaults to the current location, when there is one.
526
+ */
527
+ static readAccountLinkCallback(href) {
528
+ const source = href ?? (typeof window === 'undefined' ? '' : window.location.href);
529
+ if (!source)
530
+ return {};
531
+ let hash;
532
+ try {
533
+ hash = new URL(source).hash;
534
+ }
535
+ catch {
536
+ return {};
537
+ }
538
+ const params = new URLSearchParams(hash.replace(/^#/, ''));
539
+ const grant = params.get('grant');
540
+ const accountId = params.get('account');
541
+ const error = params.get('error');
542
+ const out = {};
543
+ if (grant)
544
+ out.grant = grant;
545
+ if (accountId)
546
+ out.accountId = accountId;
547
+ if (error)
548
+ out.error = error;
549
+ return out;
550
+ }
551
+ /**
552
+ * Spend a link grant: sign it with the account root and post the pair.
553
+ *
554
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
555
+ * which is the whole reason the grant exists. The grant carries the consenting
556
+ * login sealed inside it, so nothing here names one; a caller-supplied email
557
+ * would be a field to lie in.
558
+ *
559
+ * The signature is an ordinary account-link signature with the grant as the
560
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
561
+ * this one. Both halves are required: a grant without the key links nothing,
562
+ * which is what makes it safe to carry through a browser redirect.
563
+ *
564
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
565
+ * that names a different account; `409` that the account belongs to another
566
+ * login; `402` that the plan is full.
567
+ */
568
+ async linkAccountWithGrant(options) {
569
+ const root = await accountRootFromSecret(options.rootSecret);
570
+ const body = await this.request('POST', '/api/cloud/accounts/link', {
571
+ body: {
572
+ grant: options.grant,
573
+ root_public_key: root.publicKey,
574
+ signature: await signAccountLink({
575
+ rootSecret: options.rootSecret,
576
+ nonce: options.grant,
577
+ }),
578
+ },
579
+ anonymous: true,
580
+ });
581
+ return {
582
+ accountId: String(body.account_id ?? root.accountId),
583
+ linkedAt: body.linked_at ?? null,
584
+ alreadyLinked: body.already_linked === true,
585
+ };
586
+ }
492
587
  /** The accounts linked to this login, and how many the plan allows. */
493
588
  async getMyAccounts() {
494
589
  const body = await this.request('GET', '/api/cloud/me/accounts');
@@ -1,5 +1,5 @@
1
1
  export { CloudClient } from './cloud-client.js';
2
- export type { CloudClientConfig, CloudSession, CloudNamespace, CloudRelay, CloudNamespaceNode, CloudNamespaceRouting, CloudMachine, CloudMachineNamespace, CloudLinkedAccount, CloudLinkedAccounts, CloudAccountLink, AccountLinkChallenge, AccountLinkProof, AccountLoginChallenge, AccountLoginProof, EnableHAOptions, DisableHAOptions, } from './cloud-client.js';
2
+ export type { CloudClientConfig, CloudSession, CloudNamespace, CloudRelay, CloudNamespaceNode, CloudNamespaceRouting, CloudMachine, CloudMachineNamespace, CloudLinkedAccount, CloudLinkedAccounts, CloudAccountLink, AccountLinkChallenge, AccountLinkProof, AccountLinkHandoff, AccountLinkCallback, AccountLoginChallenge, AccountLoginProof, EnableHAOptions, DisableHAOptions, } from './cloud-client.js';
3
3
  export { connectCloud } from './connect.js';
4
4
  export type { ConnectCloudOptions, CloudConnection } from './connect.js';
5
5
  export { signRoutingChallenge, routingProofHeaders } from './routing-proof.js';