@pollar/core 0.9.0-rc.1 → 0.9.0-rc.2

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
@@ -1171,7 +1171,7 @@ function defaultStorage(options = {}) {
1171
1171
  }
1172
1172
 
1173
1173
  // src/version.ts
1174
- var POLLAR_CORE_VERSION = "0.9.0-rc.1" ;
1174
+ var POLLAR_CORE_VERSION = "0.9.0-rc.2" ;
1175
1175
 
1176
1176
  // src/visibility/noop.ts
1177
1177
  function createNoopVisibilityProvider() {
@@ -1343,7 +1343,10 @@ function openAlbedoPopup(url) {
1343
1343
  }
1344
1344
  function waitForAlbedoPopup() {
1345
1345
  return new Promise((resolve, reject) => {
1346
- const timeout = setTimeout(() => reject(new Error("Albedo response timeout")), 2 * 60 * 1e3);
1346
+ const timeout = setTimeout(() => {
1347
+ window.removeEventListener("message", handler);
1348
+ reject(new Error("Albedo response timeout"));
1349
+ }, 2 * 60 * 1e3);
1347
1350
  function handler(event) {
1348
1351
  if (event.origin !== window.location.origin || event.data?.type !== "ALBEDO_RESULT") return;
1349
1352
  clearTimeout(timeout);
@@ -1353,24 +1356,6 @@ function waitForAlbedoPopup() {
1353
1356
  window.addEventListener("message", handler);
1354
1357
  });
1355
1358
  }
1356
- function waitForAlbedoResult() {
1357
- return new Promise((resolve, reject) => {
1358
- const timeout = setTimeout(() => reject(new Error("Albedo response timeout")), 2 * 60 * 1e3);
1359
- const parseResult = () => {
1360
- const params = new URLSearchParams(window.location.search);
1361
- if (!params.has("pubkey") && !params.has("signed_envelope_xdr") && !params.has("signed_xdr")) return;
1362
- clearTimeout(timeout);
1363
- const result = {};
1364
- params.forEach((value, key) => {
1365
- result[key] = value;
1366
- });
1367
- window.history.replaceState({}, document.title, window.location.pathname);
1368
- resolve(result);
1369
- };
1370
- parseResult();
1371
- window.addEventListener("popstate", parseResult);
1372
- });
1373
- }
1374
1359
  var AlbedoAdapter = class {
1375
1360
  /**
1376
1361
  * Network used for `connect` and `signAuthEntry` (which carry no per-call
@@ -1412,10 +1397,10 @@ var AlbedoAdapter = class {
1412
1397
  url.searchParams.set("xdr", xdr);
1413
1398
  url.searchParams.set("app_name", "Pollar");
1414
1399
  url.searchParams.set("network", albedoNetwork(options, this.network));
1415
- url.searchParams.set("callback", window.location.href);
1400
+ url.searchParams.set("callback", `${window.location.origin}/albedo-callback`);
1416
1401
  url.searchParams.set("origin", window.location.origin);
1417
- window.location.href = url.toString();
1418
- const result = await waitForAlbedoResult();
1402
+ openAlbedoPopup(url.toString());
1403
+ const result = await waitForAlbedoPopup();
1419
1404
  if (!result.signed_envelope_xdr) throw new Error("Albedo signing rejected");
1420
1405
  return { signedTxXdr: result.signed_envelope_xdr };
1421
1406
  }
@@ -1425,10 +1410,10 @@ var AlbedoAdapter = class {
1425
1410
  url.searchParams.set("xdr", entryXdr);
1426
1411
  url.searchParams.set("app_name", "Pollar");
1427
1412
  url.searchParams.set("network", this.network);
1428
- url.searchParams.set("callback", window.location.href);
1413
+ url.searchParams.set("callback", `${window.location.origin}/albedo-callback`);
1429
1414
  url.searchParams.set("origin", window.location.origin);
1430
- window.location.href = url.toString();
1431
- const result = await waitForAlbedoResult();
1415
+ openAlbedoPopup(url.toString());
1416
+ const result = await waitForAlbedoPopup();
1432
1417
  if (!result.signed_xdr) throw new Error("Albedo auth entry signing rejected");
1433
1418
  return { signedAuthEntry: result.signed_xdr };
1434
1419
  }
@@ -1511,8 +1496,8 @@ function isValidSession(value, logger = console) {
1511
1496
  return false;
1512
1497
  }
1513
1498
  const w = wallet;
1514
- if (w["type"] !== "custodial" && w["type"] !== "smart" && w["type"] !== "external") {
1515
- logger.debug("[PollarClient:session] Invalid session \u2014 wallet.type must be custodial|smart|external");
1499
+ if (w["type"] !== "internal" && w["type"] !== "smart" && w["type"] !== "external") {
1500
+ logger.debug("[PollarClient:session] Invalid session \u2014 wallet.type must be internal|smart|external");
1516
1501
  return false;
1517
1502
  }
1518
1503
  if (w["address"] !== null && !isBoundedString(w["address"], MAX_WALLET_PUBLIC_KEY)) {
@@ -1543,6 +1528,9 @@ async function readStorage(storage, apiKeyHash, logger = console) {
1543
1528
  if (w && w["address"] == null && typeof w["publicKey"] === "string") {
1544
1529
  w["address"] = w["publicKey"];
1545
1530
  }
1531
+ if (w && w["type"] === "custodial") {
1532
+ w["type"] = "internal";
1533
+ }
1546
1534
  }
1547
1535
  if (!isValidSession(session, logger)) {
1548
1536
  await storage.remove(sessionStorageKey(apiKeyHash));
@@ -1643,16 +1631,12 @@ async function streamUntilFound(api, clientSessionId, check, retryDelayMs = 200,
1643
1631
  }
1644
1632
  const reader = data.getReader();
1645
1633
  const decoder = new TextDecoder();
1646
- let streamDone = false;
1647
1634
  let sawAnyChunk = false;
1648
1635
  try {
1649
1636
  while (true) {
1650
1637
  throwIfAborted(signal);
1651
1638
  const { done, value } = await reader.read();
1652
- if (done) {
1653
- streamDone = true;
1654
- break;
1655
- }
1639
+ if (done) break;
1656
1640
  sawAnyChunk = true;
1657
1641
  const chunk = decoder.decode(value);
1658
1642
  for (const message of chunk.split("\n\n").filter(Boolean)) {
@@ -1680,8 +1664,7 @@ async function streamUntilFound(api, clientSessionId, check, retryDelayMs = 200,
1680
1664
  }
1681
1665
  if (sawAnyChunk) backoff = retryDelayMs;
1682
1666
  else backoff = Math.min(backoff * 2, MAX_BACKOFF_MS);
1683
- const delay = streamDone ? backoff : 0;
1684
- if (delay) await sleep(delay);
1667
+ await sleep(backoff);
1685
1668
  }
1686
1669
  }
1687
1670
  async function pollUntilFound(baseUrl, clientSessionId, check, intervalMs = 500, signal, logger = console) {
@@ -1761,17 +1744,18 @@ async function authenticate(clientSessionId, deps, expectedWallet) {
1761
1744
  signal
1762
1745
  });
1763
1746
  if (data?.code === "SDK_LOGIN_SUCCESS" && isValidSession(data?.content, logger)) {
1764
- if (expectedWallet && data.content.data.providers.wallet?.address !== expectedWallet) {
1747
+ const sessionWallet = data.content.data?.providers?.wallet?.address;
1748
+ if (expectedWallet && sessionWallet !== expectedWallet) {
1765
1749
  setAuthState({
1766
1750
  step: "error",
1767
1751
  previousStep: "authenticating",
1768
1752
  message: "Wallet mismatch: session wallet does not match connected wallet",
1769
1753
  errorCode: AUTH_ERROR_CODES.WALLET_AUTH_FAILED
1770
1754
  });
1771
- clearSession();
1755
+ await clearSession();
1772
1756
  return;
1773
1757
  }
1774
- storeSession(data.content);
1758
+ await storeSession(data.content);
1775
1759
  } else {
1776
1760
  setAuthState({
1777
1761
  step: "error",
@@ -1779,7 +1763,7 @@ async function authenticate(clientSessionId, deps, expectedWallet) {
1779
1763
  message: "Failed to load session",
1780
1764
  errorCode: AUTH_ERROR_CODES.AUTH_FAILED
1781
1765
  });
1782
- clearSession();
1766
+ await clearSession();
1783
1767
  }
1784
1768
  }
1785
1769
 
@@ -2095,7 +2079,7 @@ var PollarClient = class {
2095
2079
  this._visibilityProvider = config.visibilityProvider ?? defaultVisibilityProvider();
2096
2080
  this._maxIdleMs = config.maxIdleMs;
2097
2081
  this._openAuthUrl = config.openAuthUrl ?? defaultWebOAuthOpener;
2098
- this._oauthRedirectUri = config.oauthRedirectUri ?? (isBrowser ? window.location.origin : "");
2082
+ this._oauthRedirectUri = config.oauthRedirectUri ?? (isBrowser ? window.location?.origin ?? "" : "");
2099
2083
  this._api = createApiClient(this.basePath);
2100
2084
  this._wireMiddlewares();
2101
2085
  this._networkState = { step: "connected", network: config.stellarNetwork ?? "testnet" };
@@ -3409,6 +3393,9 @@ var PollarClient = class {
3409
3393
  void this._resume();
3410
3394
  } else {
3411
3395
  this._log.info("[PollarClient] No session in storage");
3396
+ if (this._authState.step !== "idle") {
3397
+ await this._clearSession();
3398
+ }
3412
3399
  }
3413
3400
  }
3414
3401
  /**
@@ -3455,8 +3442,11 @@ var PollarClient = class {
3455
3442
  user: session.user,
3456
3443
  // The wire response still carries the legacy `publicKey` alias (kept for
3457
3444
  // older SDKs); the persisted session standardizes on `address` only.
3445
+ // The wire also still emits the legacy type `'custodial'` (unchanged for
3446
+ // SDKs ≤0.8.x); we remap it to `'internal'` here so the SDK surface and
3447
+ // persisted session speak one vocabulary while the wire stays compatible.
3458
3448
  wallet: {
3459
- type: w.type,
3449
+ type: w.type === "custodial" ? "internal" : w.type,
3460
3450
  address: w.address ?? w.publicKey ?? null,
3461
3451
  ...w.existsOnStellar !== void 0 ? { existsOnStellar: w.existsOnStellar } : {},
3462
3452
  ...w.createdAt !== void 0 ? { createdAt: w.createdAt } : {},