@owney/sdk 0.7.34-beta.0 → 0.7.35

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
@@ -1383,11 +1383,12 @@ var ZyfaiAgent = class _ZyfaiAgent {
1383
1383
  if (typeof window === "undefined") return;
1384
1384
  const internals = this.sdk;
1385
1385
  const original = internals.authenticateUser.bind(internals);
1386
- internals.authenticateUser = async () => {
1386
+ let inFlight = null;
1387
+ const authenticate = async () => {
1387
1388
  if (internals.authenticatedUserId !== null) return;
1388
1389
  const address = internals.walletClient?.account?.address;
1389
- const chainId = internals.currentChainId;
1390
- if (address && chainId) {
1390
+ const chainId = internals.currentChainId ?? 0;
1391
+ if (address) {
1391
1392
  const cached = readSession(address, chainId);
1392
1393
  if (cached) {
1393
1394
  internals.httpClient.setAuthToken(cached.token);
@@ -1399,9 +1400,9 @@ var ZyfaiAgent = class _ZyfaiAgent {
1399
1400
  }
1400
1401
  await original();
1401
1402
  const newAddress = internals.walletClient?.account?.address;
1402
- const newChainId = internals.currentChainId;
1403
+ const newChainId = internals.currentChainId ?? 0;
1403
1404
  const newToken = internals.httpClient.authToken;
1404
- if (newAddress && newChainId && newToken) {
1405
+ if (newAddress && newToken) {
1405
1406
  writeSession(newAddress, newChainId, {
1406
1407
  token: newToken,
1407
1408
  userId: internals.authenticatedUserId,
@@ -1410,6 +1411,15 @@ var ZyfaiAgent = class _ZyfaiAgent {
1410
1411
  });
1411
1412
  }
1412
1413
  };
1414
+ internals.authenticateUser = () => {
1415
+ const address = internals.walletClient?.account?.address?.toLowerCase();
1416
+ if (inFlight && inFlight.address === address) return inFlight.promise;
1417
+ const promise = authenticate().finally(() => {
1418
+ if (inFlight?.promise === promise) inFlight = null;
1419
+ });
1420
+ inFlight = { address, promise };
1421
+ return promise;
1422
+ };
1413
1423
  }
1414
1424
  // --- Private helpers ---
1415
1425
  getAddress() {
@@ -2718,7 +2728,7 @@ var CHAIN_NAMES = {
2718
2728
  function chainName(chainId) {
2719
2729
  return CHAIN_NAMES[chainId] ?? `chain ${chainId}`;
2720
2730
  }
2721
- var SETTLE_TIMEOUT_MS = 8e3;
2731
+ var SETTLE_TIMEOUT_MS = 15e3;
2722
2732
  var POLL_INTERVAL_MS = 200;
2723
2733
  var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2724
2734
  async function readChainId(pub) {
@@ -4650,6 +4660,16 @@ function makeSponsoredDepositCallback(deps) {
4650
4660
 
4651
4661
  // src/lib/walletconnect-switch.ts
4652
4662
  var WALLET_SWITCH_TIMEOUT_MS = 12e4;
4663
+ var HOLD_CHAIN_MS = 15e3;
4664
+ var MAX_RESENDS = 2;
4665
+ var holds = /* @__PURE__ */ new Map();
4666
+ function parseChainId(raw) {
4667
+ if (typeof raw === "number") return raw;
4668
+ if (typeof raw !== "string") return void 0;
4669
+ const ref = raw.split(":").pop() ?? raw;
4670
+ const id = ref.startsWith("0x") ? Number.parseInt(ref, 16) : Number(ref);
4671
+ return Number.isFinite(id) ? id : void 0;
4672
+ }
4653
4673
  function walletConnectCore(provider) {
4654
4674
  const p = provider;
4655
4675
  const core = p?.signer ?? p;
@@ -4663,6 +4683,12 @@ function walletConnectCore(provider) {
4663
4683
  async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
4664
4684
  const core = walletConnectCore(provider);
4665
4685
  if (!core) return false;
4686
+ holds.get(core.session.topic)?.();
4687
+ await requestSwitch(core, provider, chainId, timeoutMs);
4688
+ holdChain(core, provider, chainId);
4689
+ return true;
4690
+ }
4691
+ async function requestSwitch(core, provider, chainId, timeoutMs) {
4666
4692
  const current = Number(
4667
4693
  await provider.request({ method: "eth_chainId" })
4668
4694
  );
@@ -4694,7 +4720,38 @@ async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SW
4694
4720
  core.setDefaultChain(`eip155:${chainId}`);
4695
4721
  } catch {
4696
4722
  }
4697
- return true;
4723
+ }
4724
+ function holdChain(core, provider, chainId) {
4725
+ if (typeof core.on !== "function" || typeof core.removeListener !== "function") {
4726
+ return;
4727
+ }
4728
+ let resends = 0;
4729
+ const listener = (raw) => {
4730
+ const reported = parseChainId(raw);
4731
+ debugLog("wc-switch", "wallet reported chain", {
4732
+ reported,
4733
+ holding: chainId,
4734
+ resends
4735
+ });
4736
+ if (reported === void 0 || reported === chainId) return;
4737
+ if (resends >= MAX_RESENDS) return;
4738
+ resends += 1;
4739
+ void requestSwitch(core, provider, chainId, WALLET_SWITCH_TIMEOUT_MS).catch(
4740
+ (error) => debugLog("wc-switch", "re-switch failed", {
4741
+ error: error instanceof Error ? error.message : String(error)
4742
+ })
4743
+ );
4744
+ };
4745
+ const release = () => {
4746
+ clearTimeout(timer);
4747
+ core.removeListener?.("chainChanged", listener);
4748
+ if (holds.get(core.session.topic) === release) {
4749
+ holds.delete(core.session.topic);
4750
+ }
4751
+ };
4752
+ const timer = setTimeout(release, HOLD_CHAIN_MS);
4753
+ core.on("chainChanged", listener);
4754
+ holds.set(core.session.topic, release);
4698
4755
  }
4699
4756
  function withWalletConnectSwitch(provider) {
4700
4757
  if (!walletConnectCore(provider)) return provider;
package/dist/index.js CHANGED
@@ -1342,11 +1342,12 @@ var ZyfaiAgent = class _ZyfaiAgent {
1342
1342
  if (typeof window === "undefined") return;
1343
1343
  const internals = this.sdk;
1344
1344
  const original = internals.authenticateUser.bind(internals);
1345
- internals.authenticateUser = async () => {
1345
+ let inFlight = null;
1346
+ const authenticate = async () => {
1346
1347
  if (internals.authenticatedUserId !== null) return;
1347
1348
  const address = internals.walletClient?.account?.address;
1348
- const chainId = internals.currentChainId;
1349
- if (address && chainId) {
1349
+ const chainId = internals.currentChainId ?? 0;
1350
+ if (address) {
1350
1351
  const cached = readSession(address, chainId);
1351
1352
  if (cached) {
1352
1353
  internals.httpClient.setAuthToken(cached.token);
@@ -1358,9 +1359,9 @@ var ZyfaiAgent = class _ZyfaiAgent {
1358
1359
  }
1359
1360
  await original();
1360
1361
  const newAddress = internals.walletClient?.account?.address;
1361
- const newChainId = internals.currentChainId;
1362
+ const newChainId = internals.currentChainId ?? 0;
1362
1363
  const newToken = internals.httpClient.authToken;
1363
- if (newAddress && newChainId && newToken) {
1364
+ if (newAddress && newToken) {
1364
1365
  writeSession(newAddress, newChainId, {
1365
1366
  token: newToken,
1366
1367
  userId: internals.authenticatedUserId,
@@ -1369,6 +1370,15 @@ var ZyfaiAgent = class _ZyfaiAgent {
1369
1370
  });
1370
1371
  }
1371
1372
  };
1373
+ internals.authenticateUser = () => {
1374
+ const address = internals.walletClient?.account?.address?.toLowerCase();
1375
+ if (inFlight && inFlight.address === address) return inFlight.promise;
1376
+ const promise = authenticate().finally(() => {
1377
+ if (inFlight?.promise === promise) inFlight = null;
1378
+ });
1379
+ inFlight = { address, promise };
1380
+ return promise;
1381
+ };
1372
1382
  }
1373
1383
  // --- Private helpers ---
1374
1384
  getAddress() {
@@ -2677,7 +2687,7 @@ var CHAIN_NAMES = {
2677
2687
  function chainName(chainId) {
2678
2688
  return CHAIN_NAMES[chainId] ?? `chain ${chainId}`;
2679
2689
  }
2680
- var SETTLE_TIMEOUT_MS = 8e3;
2690
+ var SETTLE_TIMEOUT_MS = 15e3;
2681
2691
  var POLL_INTERVAL_MS = 200;
2682
2692
  var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2683
2693
  async function readChainId(pub) {
@@ -4630,6 +4640,16 @@ function makeSponsoredDepositCallback(deps) {
4630
4640
 
4631
4641
  // src/lib/walletconnect-switch.ts
4632
4642
  var WALLET_SWITCH_TIMEOUT_MS = 12e4;
4643
+ var HOLD_CHAIN_MS = 15e3;
4644
+ var MAX_RESENDS = 2;
4645
+ var holds = /* @__PURE__ */ new Map();
4646
+ function parseChainId(raw) {
4647
+ if (typeof raw === "number") return raw;
4648
+ if (typeof raw !== "string") return void 0;
4649
+ const ref = raw.split(":").pop() ?? raw;
4650
+ const id = ref.startsWith("0x") ? Number.parseInt(ref, 16) : Number(ref);
4651
+ return Number.isFinite(id) ? id : void 0;
4652
+ }
4633
4653
  function walletConnectCore(provider) {
4634
4654
  const p = provider;
4635
4655
  const core = p?.signer ?? p;
@@ -4643,6 +4663,12 @@ function walletConnectCore(provider) {
4643
4663
  async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
4644
4664
  const core = walletConnectCore(provider);
4645
4665
  if (!core) return false;
4666
+ holds.get(core.session.topic)?.();
4667
+ await requestSwitch(core, provider, chainId, timeoutMs);
4668
+ holdChain(core, provider, chainId);
4669
+ return true;
4670
+ }
4671
+ async function requestSwitch(core, provider, chainId, timeoutMs) {
4646
4672
  const current = Number(
4647
4673
  await provider.request({ method: "eth_chainId" })
4648
4674
  );
@@ -4674,7 +4700,38 @@ async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SW
4674
4700
  core.setDefaultChain(`eip155:${chainId}`);
4675
4701
  } catch {
4676
4702
  }
4677
- return true;
4703
+ }
4704
+ function holdChain(core, provider, chainId) {
4705
+ if (typeof core.on !== "function" || typeof core.removeListener !== "function") {
4706
+ return;
4707
+ }
4708
+ let resends = 0;
4709
+ const listener = (raw) => {
4710
+ const reported = parseChainId(raw);
4711
+ debugLog("wc-switch", "wallet reported chain", {
4712
+ reported,
4713
+ holding: chainId,
4714
+ resends
4715
+ });
4716
+ if (reported === void 0 || reported === chainId) return;
4717
+ if (resends >= MAX_RESENDS) return;
4718
+ resends += 1;
4719
+ void requestSwitch(core, provider, chainId, WALLET_SWITCH_TIMEOUT_MS).catch(
4720
+ (error) => debugLog("wc-switch", "re-switch failed", {
4721
+ error: error instanceof Error ? error.message : String(error)
4722
+ })
4723
+ );
4724
+ };
4725
+ const release = () => {
4726
+ clearTimeout(timer);
4727
+ core.removeListener?.("chainChanged", listener);
4728
+ if (holds.get(core.session.topic) === release) {
4729
+ holds.delete(core.session.topic);
4730
+ }
4731
+ };
4732
+ const timer = setTimeout(release, HOLD_CHAIN_MS);
4733
+ core.on("chainChanged", listener);
4734
+ holds.set(core.session.topic, release);
4678
4735
  }
4679
4736
  function withWalletConnectSwitch(provider) {
4680
4737
  if (!walletConnectCore(provider)) return provider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.7.34-beta.0",
3
+ "version": "0.7.35",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",