@owney/sdk 0.7.36 → 0.7.37-beta.1

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
@@ -4693,7 +4693,6 @@ function makeSponsoredDepositCallback(deps) {
4693
4693
  // src/lib/walletconnect-switch.ts
4694
4694
  var WALLET_SWITCH_TIMEOUT_MS = 12e4;
4695
4695
  var HOLD_CHAIN_MS = 15e3;
4696
- var MAX_RESENDS = 2;
4697
4696
  var holds = /* @__PURE__ */ new Map();
4698
4697
  function parseChainId(raw) {
4699
4698
  if (typeof raw === "number") return raw;
@@ -4702,28 +4701,76 @@ function parseChainId(raw) {
4702
4701
  const id = ref.startsWith("0x") ? Number.parseInt(ref, 16) : Number(ref);
4703
4702
  return Number.isFinite(id) ? id : void 0;
4704
4703
  }
4705
- function walletConnectCore(provider) {
4704
+ function walletConnectSession(provider) {
4706
4705
  const p = provider;
4707
4706
  const core = p?.signer ?? p;
4708
- if (typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
4707
+ if (typeof core?.request !== "function" || typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
4709
4708
  return null;
4710
4709
  }
4711
4710
  const peer = core.session.peer?.metadata?.name ?? "";
4712
- if (!/metamask/i.test(peer)) return null;
4713
- return core;
4711
+ if (/metamask/i.test(peer)) {
4712
+ return { core, strategy: "remote-wallet" };
4713
+ }
4714
+ if (/fireblocks/i.test(peer)) {
4715
+ return { core, strategy: "session-default" };
4716
+ }
4717
+ return null;
4718
+ }
4719
+ function approvedChains(core) {
4720
+ const approved = /* @__PURE__ */ new Set();
4721
+ for (const namespace of Object.values(core.session.namespaces ?? {})) {
4722
+ for (const chain of namespace.chains ?? []) approved.add(chain);
4723
+ for (const account of namespace.accounts ?? []) {
4724
+ const [namespaceName, reference] = account.split(":");
4725
+ if (namespaceName && reference) {
4726
+ approved.add(`${namespaceName}:${reference}`);
4727
+ }
4728
+ }
4729
+ }
4730
+ return approved;
4731
+ }
4732
+ async function selectSessionDefaultChain(core, chainId) {
4733
+ const target = `eip155:${chainId}`;
4734
+ if (!approvedChains(core).has(target)) {
4735
+ throw new Error(
4736
+ `The WalletConnect session has not approved ${target}. Reconnect the wallet and try again.`
4737
+ );
4738
+ }
4739
+ core.setDefaultChain(target);
4740
+ const selected = parseChainId(
4741
+ await core.request({ method: "eth_chainId" })
4742
+ );
4743
+ debugLog("wc-switch", "selected WalletConnect session default", {
4744
+ peer: core.session.peer?.metadata?.name,
4745
+ target: chainId,
4746
+ selected
4747
+ });
4748
+ if (selected !== chainId) {
4749
+ throw new Error(
4750
+ `WalletConnect stayed on chain ${selected ?? "unknown"}; expected ${chainId}.`
4751
+ );
4752
+ }
4714
4753
  }
4715
4754
  async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
4716
- const core = walletConnectCore(provider);
4717
- if (!core) return false;
4755
+ const session = walletConnectSession(provider);
4756
+ if (!session) return false;
4757
+ const { core, strategy } = session;
4718
4758
  holds.get(core.session.topic)?.();
4759
+ if (strategy === "session-default") {
4760
+ await selectSessionDefaultChain(core, chainId);
4761
+ return true;
4762
+ }
4719
4763
  await requestSwitch(core, provider, chainId, timeoutMs);
4720
- holdChain(core, provider, chainId);
4764
+ holdChain(core, chainId);
4721
4765
  return true;
4722
4766
  }
4723
4767
  async function requestSwitch(core, provider, chainId, timeoutMs) {
4724
- const current = Number(
4768
+ const current = parseChainId(
4725
4769
  await provider.request({ method: "eth_chainId" })
4726
4770
  );
4771
+ if (current === void 0) {
4772
+ throw new Error("WalletConnect could not report its current chain.");
4773
+ }
4727
4774
  const hexChainId = `0x${chainId.toString(16)}`;
4728
4775
  let timer;
4729
4776
  try {
@@ -4753,26 +4800,27 @@ async function requestSwitch(core, provider, chainId, timeoutMs) {
4753
4800
  } catch {
4754
4801
  }
4755
4802
  }
4756
- function holdChain(core, provider, chainId) {
4803
+ function holdChain(core, chainId) {
4757
4804
  if (typeof core.on !== "function" || typeof core.removeListener !== "function") {
4758
4805
  return;
4759
4806
  }
4760
- let resends = 0;
4807
+ let repins = 0;
4761
4808
  const listener = (raw) => {
4762
4809
  const reported = parseChainId(raw);
4763
4810
  debugLog("wc-switch", "wallet reported chain", {
4764
4811
  reported,
4765
4812
  holding: chainId,
4766
- resends
4813
+ repins
4767
4814
  });
4768
4815
  if (reported === void 0 || reported === chainId) return;
4769
- if (resends >= MAX_RESENDS) return;
4770
- resends += 1;
4771
- void requestSwitch(core, provider, chainId, WALLET_SWITCH_TIMEOUT_MS).catch(
4772
- (error) => debugLog("wc-switch", "re-switch failed", {
4816
+ repins += 1;
4817
+ try {
4818
+ core.setDefaultChain(`eip155:${chainId}`);
4819
+ } catch (error) {
4820
+ debugLog("wc-switch", "session re-pin failed", {
4773
4821
  error: error instanceof Error ? error.message : String(error)
4774
- })
4775
- );
4822
+ });
4823
+ }
4776
4824
  };
4777
4825
  const release = () => {
4778
4826
  clearTimeout(timer);
@@ -4786,7 +4834,7 @@ function holdChain(core, provider, chainId) {
4786
4834
  holds.set(core.session.topic, release);
4787
4835
  }
4788
4836
  function withWalletConnectSwitch(provider) {
4789
- if (!walletConnectCore(provider)) return provider;
4837
+ if (!walletConnectSession(provider)) return provider;
4790
4838
  const target = provider;
4791
4839
  const request2 = async (args) => {
4792
4840
  if (args?.method !== "wallet_switchEthereumChain") {
package/dist/index.d.cts CHANGED
@@ -1994,16 +1994,17 @@ type OwneySIWXConfig = {
1994
1994
  declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
1995
1995
 
1996
1996
  /**
1997
- * Ask the wallet ITSELF to switch networks, over WalletConnect.
1997
+ * Align a supported WalletConnect session with `chainId`.
1998
1998
  *
1999
1999
  * WalletConnect's provider answers `wallet_switchEthereumChain` locally when
2000
2000
  * the chain was approved at connect time: it moves its own default chain and
2001
2001
  * reports success without telling the wallet. MetaMask mobile then stays on
2002
2002
  * its network, keeps reporting it, and every guard downstream fails with
2003
2003
  * CHAIN_MISMATCH — after a swap, with the user's funds already moved (ROUT-727).
2004
- * Sending the request through the session client reaches the wallet, which
2005
- * shows a real "switch network" prompt; the client also deep-links the wallet
2006
- * app open on mobile.
2004
+ * Sending the request through the session client reaches MetaMask, which shows
2005
+ * a real "switch network" prompt. Fireblocks instead uses the approved
2006
+ * multi-chain session's local default; its subsequent signature request is
2007
+ * explicitly scoped to that chain and needs no separate network prompt.
2007
2008
  *
2008
2009
  * Returns false when `provider` is not a WalletConnect session, so the caller
2009
2010
  * can fall back to an ordinary switch.
package/dist/index.d.ts CHANGED
@@ -1994,16 +1994,17 @@ type OwneySIWXConfig = {
1994
1994
  declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
1995
1995
 
1996
1996
  /**
1997
- * Ask the wallet ITSELF to switch networks, over WalletConnect.
1997
+ * Align a supported WalletConnect session with `chainId`.
1998
1998
  *
1999
1999
  * WalletConnect's provider answers `wallet_switchEthereumChain` locally when
2000
2000
  * the chain was approved at connect time: it moves its own default chain and
2001
2001
  * reports success without telling the wallet. MetaMask mobile then stays on
2002
2002
  * its network, keeps reporting it, and every guard downstream fails with
2003
2003
  * CHAIN_MISMATCH — after a swap, with the user's funds already moved (ROUT-727).
2004
- * Sending the request through the session client reaches the wallet, which
2005
- * shows a real "switch network" prompt; the client also deep-links the wallet
2006
- * app open on mobile.
2004
+ * Sending the request through the session client reaches MetaMask, which shows
2005
+ * a real "switch network" prompt. Fireblocks instead uses the approved
2006
+ * multi-chain session's local default; its subsequent signature request is
2007
+ * explicitly scoped to that chain and needs no separate network prompt.
2007
2008
  *
2008
2009
  * Returns false when `provider` is not a WalletConnect session, so the caller
2009
2010
  * can fall back to an ordinary switch.
package/dist/index.js CHANGED
@@ -4673,7 +4673,6 @@ function makeSponsoredDepositCallback(deps) {
4673
4673
  // src/lib/walletconnect-switch.ts
4674
4674
  var WALLET_SWITCH_TIMEOUT_MS = 12e4;
4675
4675
  var HOLD_CHAIN_MS = 15e3;
4676
- var MAX_RESENDS = 2;
4677
4676
  var holds = /* @__PURE__ */ new Map();
4678
4677
  function parseChainId(raw) {
4679
4678
  if (typeof raw === "number") return raw;
@@ -4682,28 +4681,76 @@ function parseChainId(raw) {
4682
4681
  const id = ref.startsWith("0x") ? Number.parseInt(ref, 16) : Number(ref);
4683
4682
  return Number.isFinite(id) ? id : void 0;
4684
4683
  }
4685
- function walletConnectCore(provider) {
4684
+ function walletConnectSession(provider) {
4686
4685
  const p = provider;
4687
4686
  const core = p?.signer ?? p;
4688
- if (typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
4687
+ if (typeof core?.request !== "function" || typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
4689
4688
  return null;
4690
4689
  }
4691
4690
  const peer = core.session.peer?.metadata?.name ?? "";
4692
- if (!/metamask/i.test(peer)) return null;
4693
- return core;
4691
+ if (/metamask/i.test(peer)) {
4692
+ return { core, strategy: "remote-wallet" };
4693
+ }
4694
+ if (/fireblocks/i.test(peer)) {
4695
+ return { core, strategy: "session-default" };
4696
+ }
4697
+ return null;
4698
+ }
4699
+ function approvedChains(core) {
4700
+ const approved = /* @__PURE__ */ new Set();
4701
+ for (const namespace of Object.values(core.session.namespaces ?? {})) {
4702
+ for (const chain of namespace.chains ?? []) approved.add(chain);
4703
+ for (const account of namespace.accounts ?? []) {
4704
+ const [namespaceName, reference] = account.split(":");
4705
+ if (namespaceName && reference) {
4706
+ approved.add(`${namespaceName}:${reference}`);
4707
+ }
4708
+ }
4709
+ }
4710
+ return approved;
4711
+ }
4712
+ async function selectSessionDefaultChain(core, chainId) {
4713
+ const target = `eip155:${chainId}`;
4714
+ if (!approvedChains(core).has(target)) {
4715
+ throw new Error(
4716
+ `The WalletConnect session has not approved ${target}. Reconnect the wallet and try again.`
4717
+ );
4718
+ }
4719
+ core.setDefaultChain(target);
4720
+ const selected = parseChainId(
4721
+ await core.request({ method: "eth_chainId" })
4722
+ );
4723
+ debugLog("wc-switch", "selected WalletConnect session default", {
4724
+ peer: core.session.peer?.metadata?.name,
4725
+ target: chainId,
4726
+ selected
4727
+ });
4728
+ if (selected !== chainId) {
4729
+ throw new Error(
4730
+ `WalletConnect stayed on chain ${selected ?? "unknown"}; expected ${chainId}.`
4731
+ );
4732
+ }
4694
4733
  }
4695
4734
  async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
4696
- const core = walletConnectCore(provider);
4697
- if (!core) return false;
4735
+ const session = walletConnectSession(provider);
4736
+ if (!session) return false;
4737
+ const { core, strategy } = session;
4698
4738
  holds.get(core.session.topic)?.();
4739
+ if (strategy === "session-default") {
4740
+ await selectSessionDefaultChain(core, chainId);
4741
+ return true;
4742
+ }
4699
4743
  await requestSwitch(core, provider, chainId, timeoutMs);
4700
- holdChain(core, provider, chainId);
4744
+ holdChain(core, chainId);
4701
4745
  return true;
4702
4746
  }
4703
4747
  async function requestSwitch(core, provider, chainId, timeoutMs) {
4704
- const current = Number(
4748
+ const current = parseChainId(
4705
4749
  await provider.request({ method: "eth_chainId" })
4706
4750
  );
4751
+ if (current === void 0) {
4752
+ throw new Error("WalletConnect could not report its current chain.");
4753
+ }
4707
4754
  const hexChainId = `0x${chainId.toString(16)}`;
4708
4755
  let timer;
4709
4756
  try {
@@ -4733,26 +4780,27 @@ async function requestSwitch(core, provider, chainId, timeoutMs) {
4733
4780
  } catch {
4734
4781
  }
4735
4782
  }
4736
- function holdChain(core, provider, chainId) {
4783
+ function holdChain(core, chainId) {
4737
4784
  if (typeof core.on !== "function" || typeof core.removeListener !== "function") {
4738
4785
  return;
4739
4786
  }
4740
- let resends = 0;
4787
+ let repins = 0;
4741
4788
  const listener = (raw) => {
4742
4789
  const reported = parseChainId(raw);
4743
4790
  debugLog("wc-switch", "wallet reported chain", {
4744
4791
  reported,
4745
4792
  holding: chainId,
4746
- resends
4793
+ repins
4747
4794
  });
4748
4795
  if (reported === void 0 || reported === chainId) return;
4749
- if (resends >= MAX_RESENDS) return;
4750
- resends += 1;
4751
- void requestSwitch(core, provider, chainId, WALLET_SWITCH_TIMEOUT_MS).catch(
4752
- (error) => debugLog("wc-switch", "re-switch failed", {
4796
+ repins += 1;
4797
+ try {
4798
+ core.setDefaultChain(`eip155:${chainId}`);
4799
+ } catch (error) {
4800
+ debugLog("wc-switch", "session re-pin failed", {
4753
4801
  error: error instanceof Error ? error.message : String(error)
4754
- })
4755
- );
4802
+ });
4803
+ }
4756
4804
  };
4757
4805
  const release = () => {
4758
4806
  clearTimeout(timer);
@@ -4766,7 +4814,7 @@ function holdChain(core, provider, chainId) {
4766
4814
  holds.set(core.session.topic, release);
4767
4815
  }
4768
4816
  function withWalletConnectSwitch(provider) {
4769
- if (!walletConnectCore(provider)) return provider;
4817
+ if (!walletConnectSession(provider)) return provider;
4770
4818
  const target = provider;
4771
4819
  const request2 = async (args) => {
4772
4820
  if (args?.method !== "wallet_switchEthereumChain") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.7.36",
3
+ "version": "0.7.37-beta.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",