@calimero-network/mero-js 19.9.0 → 19.10.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
@@ -5369,6 +5369,100 @@ var CloudClient = class {
5369
5369
  alreadyLinked: body.already_linked === true
5370
5370
  };
5371
5371
  }
5372
+ /**
5373
+ * Where to send someone to authorise linking this account, and what to expect
5374
+ * back. Pairs with {@link readAccountLinkCallback} and
5375
+ * {@link linkAccountWithGrant}.
5376
+ *
5377
+ * This exists because a browser app is in a bind: it holds an account root and
5378
+ * can never hold a *first* cloud session, since only Google issues one. So the
5379
+ * person goes to the cloud, where they are signed in, and consents there. What
5380
+ * comes back is a grant — consent to link this one account, spendable only by
5381
+ * whoever can sign with its root.
5382
+ *
5383
+ * @param callbackUrl where the portal sends the person back. Must be an origin
5384
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
5385
+ * here. The grant arrives in the URL **fragment**, which browsers do not
5386
+ * send to servers.
5387
+ *
5388
+ * Static, so it needs no client and no session: there is nothing to
5389
+ * authenticate as yet.
5390
+ */
5391
+ static accountLinkHandoff(options) {
5392
+ const portal = options.portalUrl.replace(/\/+$/, "");
5393
+ const query = new URLSearchParams({
5394
+ "link-account": options.accountId,
5395
+ "callback-url": options.callbackUrl
5396
+ });
5397
+ return { url: `${portal}/?${query.toString()}`, accountId: options.accountId };
5398
+ }
5399
+ /**
5400
+ * Read the portal's answer out of a callback URL's fragment.
5401
+ *
5402
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
5403
+ * load — which is how a redirected app finds out it has been sent back.
5404
+ *
5405
+ * @param href defaults to the current location, when there is one.
5406
+ */
5407
+ static readAccountLinkCallback(href) {
5408
+ const source = href ?? (typeof window === "undefined" ? "" : window.location.href);
5409
+ if (!source) return {};
5410
+ let hash;
5411
+ try {
5412
+ hash = new URL(source).hash;
5413
+ } catch {
5414
+ return {};
5415
+ }
5416
+ const params = new URLSearchParams(hash.replace(/^#/, ""));
5417
+ const grant = params.get("grant");
5418
+ const accountId = params.get("account");
5419
+ const error = params.get("error");
5420
+ const out = {};
5421
+ if (grant) out.grant = grant;
5422
+ if (accountId) out.accountId = accountId;
5423
+ if (error) out.error = error;
5424
+ return out;
5425
+ }
5426
+ /**
5427
+ * Spend a link grant: sign it with the account root and post the pair.
5428
+ *
5429
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
5430
+ * which is the whole reason the grant exists. The grant carries the consenting
5431
+ * login sealed inside it, so nothing here names one; a caller-supplied email
5432
+ * would be a field to lie in.
5433
+ *
5434
+ * The signature is an ordinary account-link signature with the grant as the
5435
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
5436
+ * this one. Both halves are required: a grant without the key links nothing,
5437
+ * which is what makes it safe to carry through a browser redirect.
5438
+ *
5439
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
5440
+ * that names a different account; `409` that the account belongs to another
5441
+ * login; `402` that the plan is full.
5442
+ */
5443
+ async linkAccountWithGrant(options) {
5444
+ const root = await accountRootFromSecret(options.rootSecret);
5445
+ const body = await this.request(
5446
+ "POST",
5447
+ "/api/cloud/accounts/link",
5448
+ {
5449
+ body: {
5450
+ grant: options.grant,
5451
+ root_public_key: root.publicKey,
5452
+ signature: await signAccountLink({
5453
+ rootSecret: options.rootSecret,
5454
+ nonce: options.grant
5455
+ })
5456
+ },
5457
+ anonymous: true
5458
+ }
5459
+ );
5460
+ return {
5461
+ accountId: String(body.account_id ?? root.accountId),
5462
+ linkedAt: body.linked_at ?? null,
5463
+ alreadyLinked: body.already_linked === true
5464
+ };
5465
+ }
5372
5466
  /** The accounts linked to this login, and how many the plan allows. */
5373
5467
  async getMyAccounts() {
5374
5468
  const body = await this.request("GET", "/api/cloud/me/accounts");