@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.
package/dist/index.cjs CHANGED
@@ -1455,6 +1455,21 @@ var AdminApiClient = class {
1455
1455
  )
1456
1456
  );
1457
1457
  }
1458
+ /**
1459
+ * Replace a device's scope outright, in one root-signed op.
1460
+ *
1461
+ * {@link relinkAccountDevice} only ever adds applications; this is how a
1462
+ * scope is narrowed - or an `all` device is given a fixed list instead. The
1463
+ * device need not be online.
1464
+ */
1465
+ async rescopeAccountDevice(deviceId, request) {
1466
+ return unwrap(
1467
+ await this.httpClient.put(
1468
+ `/admin-api/account/devices/${encodeURIComponent(deviceId)}/scope`,
1469
+ request
1470
+ )
1471
+ );
1472
+ }
1458
1473
  /**
1459
1474
  * Every device of this account, as this node sees them - its own included.
1460
1475
  *
@@ -5369,6 +5384,100 @@ var CloudClient = class {
5369
5384
  alreadyLinked: body.already_linked === true
5370
5385
  };
5371
5386
  }
5387
+ /**
5388
+ * Where to send someone to authorise linking this account, and what to expect
5389
+ * back. Pairs with {@link readAccountLinkCallback} and
5390
+ * {@link linkAccountWithGrant}.
5391
+ *
5392
+ * This exists because a browser app is in a bind: it holds an account root and
5393
+ * can never hold a *first* cloud session, since only Google issues one. So the
5394
+ * person goes to the cloud, where they are signed in, and consents there. What
5395
+ * comes back is a grant — consent to link this one account, spendable only by
5396
+ * whoever can sign with its root.
5397
+ *
5398
+ * @param callbackUrl where the portal sends the person back. Must be an origin
5399
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
5400
+ * here. The grant arrives in the URL **fragment**, which browsers do not
5401
+ * send to servers.
5402
+ *
5403
+ * Static, so it needs no client and no session: there is nothing to
5404
+ * authenticate as yet.
5405
+ */
5406
+ static accountLinkHandoff(options) {
5407
+ const portal = options.portalUrl.replace(/\/+$/, "");
5408
+ const query = new URLSearchParams({
5409
+ "link-account": options.accountId,
5410
+ "callback-url": options.callbackUrl
5411
+ });
5412
+ return { url: `${portal}/?${query.toString()}`, accountId: options.accountId };
5413
+ }
5414
+ /**
5415
+ * Read the portal's answer out of a callback URL's fragment.
5416
+ *
5417
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
5418
+ * load — which is how a redirected app finds out it has been sent back.
5419
+ *
5420
+ * @param href defaults to the current location, when there is one.
5421
+ */
5422
+ static readAccountLinkCallback(href) {
5423
+ const source = href ?? (typeof window === "undefined" ? "" : window.location.href);
5424
+ if (!source) return {};
5425
+ let hash;
5426
+ try {
5427
+ hash = new URL(source).hash;
5428
+ } catch {
5429
+ return {};
5430
+ }
5431
+ const params = new URLSearchParams(hash.replace(/^#/, ""));
5432
+ const grant = params.get("grant");
5433
+ const accountId = params.get("account");
5434
+ const error = params.get("error");
5435
+ const out = {};
5436
+ if (grant) out.grant = grant;
5437
+ if (accountId) out.accountId = accountId;
5438
+ if (error) out.error = error;
5439
+ return out;
5440
+ }
5441
+ /**
5442
+ * Spend a link grant: sign it with the account root and post the pair.
5443
+ *
5444
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
5445
+ * which is the whole reason the grant exists. The grant carries the consenting
5446
+ * login sealed inside it, so nothing here names one; a caller-supplied email
5447
+ * would be a field to lie in.
5448
+ *
5449
+ * The signature is an ordinary account-link signature with the grant as the
5450
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
5451
+ * this one. Both halves are required: a grant without the key links nothing,
5452
+ * which is what makes it safe to carry through a browser redirect.
5453
+ *
5454
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
5455
+ * that names a different account; `409` that the account belongs to another
5456
+ * login; `402` that the plan is full.
5457
+ */
5458
+ async linkAccountWithGrant(options) {
5459
+ const root = await accountRootFromSecret(options.rootSecret);
5460
+ const body = await this.request(
5461
+ "POST",
5462
+ "/api/cloud/accounts/link",
5463
+ {
5464
+ body: {
5465
+ grant: options.grant,
5466
+ root_public_key: root.publicKey,
5467
+ signature: await signAccountLink({
5468
+ rootSecret: options.rootSecret,
5469
+ nonce: options.grant
5470
+ })
5471
+ },
5472
+ anonymous: true
5473
+ }
5474
+ );
5475
+ return {
5476
+ accountId: String(body.account_id ?? root.accountId),
5477
+ linkedAt: body.linked_at ?? null,
5478
+ alreadyLinked: body.already_linked === true
5479
+ };
5480
+ }
5372
5481
  /** The accounts linked to this login, and how many the plan allows. */
5373
5482
  async getMyAccounts() {
5374
5483
  const body = await this.request("GET", "/api/cloud/me/accounts");