@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.mjs CHANGED
@@ -5277,6 +5277,100 @@ var CloudClient = class {
5277
5277
  alreadyLinked: body.already_linked === true
5278
5278
  };
5279
5279
  }
5280
+ /**
5281
+ * Where to send someone to authorise linking this account, and what to expect
5282
+ * back. Pairs with {@link readAccountLinkCallback} and
5283
+ * {@link linkAccountWithGrant}.
5284
+ *
5285
+ * This exists because a browser app is in a bind: it holds an account root and
5286
+ * can never hold a *first* cloud session, since only Google issues one. So the
5287
+ * person goes to the cloud, where they are signed in, and consents there. What
5288
+ * comes back is a grant — consent to link this one account, spendable only by
5289
+ * whoever can sign with its root.
5290
+ *
5291
+ * @param callbackUrl where the portal sends the person back. Must be an origin
5292
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
5293
+ * here. The grant arrives in the URL **fragment**, which browsers do not
5294
+ * send to servers.
5295
+ *
5296
+ * Static, so it needs no client and no session: there is nothing to
5297
+ * authenticate as yet.
5298
+ */
5299
+ static accountLinkHandoff(options) {
5300
+ const portal = options.portalUrl.replace(/\/+$/, "");
5301
+ const query = new URLSearchParams({
5302
+ "link-account": options.accountId,
5303
+ "callback-url": options.callbackUrl
5304
+ });
5305
+ return { url: `${portal}/?${query.toString()}`, accountId: options.accountId };
5306
+ }
5307
+ /**
5308
+ * Read the portal's answer out of a callback URL's fragment.
5309
+ *
5310
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
5311
+ * load — which is how a redirected app finds out it has been sent back.
5312
+ *
5313
+ * @param href defaults to the current location, when there is one.
5314
+ */
5315
+ static readAccountLinkCallback(href) {
5316
+ const source = href ?? (typeof window === "undefined" ? "" : window.location.href);
5317
+ if (!source) return {};
5318
+ let hash;
5319
+ try {
5320
+ hash = new URL(source).hash;
5321
+ } catch {
5322
+ return {};
5323
+ }
5324
+ const params = new URLSearchParams(hash.replace(/^#/, ""));
5325
+ const grant = params.get("grant");
5326
+ const accountId = params.get("account");
5327
+ const error = params.get("error");
5328
+ const out = {};
5329
+ if (grant) out.grant = grant;
5330
+ if (accountId) out.accountId = accountId;
5331
+ if (error) out.error = error;
5332
+ return out;
5333
+ }
5334
+ /**
5335
+ * Spend a link grant: sign it with the account root and post the pair.
5336
+ *
5337
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
5338
+ * which is the whole reason the grant exists. The grant carries the consenting
5339
+ * login sealed inside it, so nothing here names one; a caller-supplied email
5340
+ * would be a field to lie in.
5341
+ *
5342
+ * The signature is an ordinary account-link signature with the grant as the
5343
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
5344
+ * this one. Both halves are required: a grant without the key links nothing,
5345
+ * which is what makes it safe to carry through a browser redirect.
5346
+ *
5347
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
5348
+ * that names a different account; `409` that the account belongs to another
5349
+ * login; `402` that the plan is full.
5350
+ */
5351
+ async linkAccountWithGrant(options) {
5352
+ const root = await accountRootFromSecret(options.rootSecret);
5353
+ const body = await this.request(
5354
+ "POST",
5355
+ "/api/cloud/accounts/link",
5356
+ {
5357
+ body: {
5358
+ grant: options.grant,
5359
+ root_public_key: root.publicKey,
5360
+ signature: await signAccountLink({
5361
+ rootSecret: options.rootSecret,
5362
+ nonce: options.grant
5363
+ })
5364
+ },
5365
+ anonymous: true
5366
+ }
5367
+ );
5368
+ return {
5369
+ accountId: String(body.account_id ?? root.accountId),
5370
+ linkedAt: body.linked_at ?? null,
5371
+ alreadyLinked: body.already_linked === true
5372
+ };
5373
+ }
5280
5374
  /** The accounts linked to this login, and how many the plan allows. */
5281
5375
  async getMyAccounts() {
5282
5376
  const body = await this.request("GET", "/api/cloud/me/accounts");