@owney/sdk 0.7.36 → 0.7.37-beta.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 +56 -8
- package/dist/index.d.cts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +56 -8
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4702,28 +4702,76 @@ function parseChainId(raw) {
|
|
|
4702
4702
|
const id = ref.startsWith("0x") ? Number.parseInt(ref, 16) : Number(ref);
|
|
4703
4703
|
return Number.isFinite(id) ? id : void 0;
|
|
4704
4704
|
}
|
|
4705
|
-
function
|
|
4705
|
+
function walletConnectSession(provider) {
|
|
4706
4706
|
const p = provider;
|
|
4707
4707
|
const core = p?.signer ?? p;
|
|
4708
|
-
if (typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
|
|
4708
|
+
if (typeof core?.request !== "function" || typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
|
|
4709
4709
|
return null;
|
|
4710
4710
|
}
|
|
4711
4711
|
const peer = core.session.peer?.metadata?.name ?? "";
|
|
4712
|
-
if (
|
|
4713
|
-
|
|
4712
|
+
if (/metamask/i.test(peer)) {
|
|
4713
|
+
return { core, strategy: "remote-wallet" };
|
|
4714
|
+
}
|
|
4715
|
+
if (/fireblocks/i.test(peer)) {
|
|
4716
|
+
return { core, strategy: "session-default" };
|
|
4717
|
+
}
|
|
4718
|
+
return null;
|
|
4719
|
+
}
|
|
4720
|
+
function approvedChains(core) {
|
|
4721
|
+
const approved = /* @__PURE__ */ new Set();
|
|
4722
|
+
for (const namespace of Object.values(core.session.namespaces ?? {})) {
|
|
4723
|
+
for (const chain of namespace.chains ?? []) approved.add(chain);
|
|
4724
|
+
for (const account of namespace.accounts ?? []) {
|
|
4725
|
+
const [namespaceName, reference] = account.split(":");
|
|
4726
|
+
if (namespaceName && reference) {
|
|
4727
|
+
approved.add(`${namespaceName}:${reference}`);
|
|
4728
|
+
}
|
|
4729
|
+
}
|
|
4730
|
+
}
|
|
4731
|
+
return approved;
|
|
4732
|
+
}
|
|
4733
|
+
async function selectSessionDefaultChain(core, chainId) {
|
|
4734
|
+
const target = `eip155:${chainId}`;
|
|
4735
|
+
if (!approvedChains(core).has(target)) {
|
|
4736
|
+
throw new Error(
|
|
4737
|
+
`The WalletConnect session has not approved ${target}. Reconnect the wallet and try again.`
|
|
4738
|
+
);
|
|
4739
|
+
}
|
|
4740
|
+
core.setDefaultChain(target);
|
|
4741
|
+
const selected = parseChainId(
|
|
4742
|
+
await core.request({ method: "eth_chainId" })
|
|
4743
|
+
);
|
|
4744
|
+
debugLog("wc-switch", "selected WalletConnect session default", {
|
|
4745
|
+
peer: core.session.peer?.metadata?.name,
|
|
4746
|
+
target: chainId,
|
|
4747
|
+
selected
|
|
4748
|
+
});
|
|
4749
|
+
if (selected !== chainId) {
|
|
4750
|
+
throw new Error(
|
|
4751
|
+
`WalletConnect stayed on chain ${selected ?? "unknown"}; expected ${chainId}.`
|
|
4752
|
+
);
|
|
4753
|
+
}
|
|
4714
4754
|
}
|
|
4715
4755
|
async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
|
|
4716
|
-
const
|
|
4717
|
-
if (!
|
|
4756
|
+
const session = walletConnectSession(provider);
|
|
4757
|
+
if (!session) return false;
|
|
4758
|
+
const { core, strategy } = session;
|
|
4718
4759
|
holds.get(core.session.topic)?.();
|
|
4760
|
+
if (strategy === "session-default") {
|
|
4761
|
+
await selectSessionDefaultChain(core, chainId);
|
|
4762
|
+
return true;
|
|
4763
|
+
}
|
|
4719
4764
|
await requestSwitch(core, provider, chainId, timeoutMs);
|
|
4720
4765
|
holdChain(core, provider, chainId);
|
|
4721
4766
|
return true;
|
|
4722
4767
|
}
|
|
4723
4768
|
async function requestSwitch(core, provider, chainId, timeoutMs) {
|
|
4724
|
-
const current =
|
|
4769
|
+
const current = parseChainId(
|
|
4725
4770
|
await provider.request({ method: "eth_chainId" })
|
|
4726
4771
|
);
|
|
4772
|
+
if (current === void 0) {
|
|
4773
|
+
throw new Error("WalletConnect could not report its current chain.");
|
|
4774
|
+
}
|
|
4727
4775
|
const hexChainId = `0x${chainId.toString(16)}`;
|
|
4728
4776
|
let timer;
|
|
4729
4777
|
try {
|
|
@@ -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 (!
|
|
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
|
-
*
|
|
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
|
|
2005
|
-
*
|
|
2006
|
-
*
|
|
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
|
-
*
|
|
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
|
|
2005
|
-
*
|
|
2006
|
-
*
|
|
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
|
@@ -4682,28 +4682,76 @@ function parseChainId(raw) {
|
|
|
4682
4682
|
const id = ref.startsWith("0x") ? Number.parseInt(ref, 16) : Number(ref);
|
|
4683
4683
|
return Number.isFinite(id) ? id : void 0;
|
|
4684
4684
|
}
|
|
4685
|
-
function
|
|
4685
|
+
function walletConnectSession(provider) {
|
|
4686
4686
|
const p = provider;
|
|
4687
4687
|
const core = p?.signer ?? p;
|
|
4688
|
-
if (typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
|
|
4688
|
+
if (typeof core?.request !== "function" || typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
|
|
4689
4689
|
return null;
|
|
4690
4690
|
}
|
|
4691
4691
|
const peer = core.session.peer?.metadata?.name ?? "";
|
|
4692
|
-
if (
|
|
4693
|
-
|
|
4692
|
+
if (/metamask/i.test(peer)) {
|
|
4693
|
+
return { core, strategy: "remote-wallet" };
|
|
4694
|
+
}
|
|
4695
|
+
if (/fireblocks/i.test(peer)) {
|
|
4696
|
+
return { core, strategy: "session-default" };
|
|
4697
|
+
}
|
|
4698
|
+
return null;
|
|
4699
|
+
}
|
|
4700
|
+
function approvedChains(core) {
|
|
4701
|
+
const approved = /* @__PURE__ */ new Set();
|
|
4702
|
+
for (const namespace of Object.values(core.session.namespaces ?? {})) {
|
|
4703
|
+
for (const chain of namespace.chains ?? []) approved.add(chain);
|
|
4704
|
+
for (const account of namespace.accounts ?? []) {
|
|
4705
|
+
const [namespaceName, reference] = account.split(":");
|
|
4706
|
+
if (namespaceName && reference) {
|
|
4707
|
+
approved.add(`${namespaceName}:${reference}`);
|
|
4708
|
+
}
|
|
4709
|
+
}
|
|
4710
|
+
}
|
|
4711
|
+
return approved;
|
|
4712
|
+
}
|
|
4713
|
+
async function selectSessionDefaultChain(core, chainId) {
|
|
4714
|
+
const target = `eip155:${chainId}`;
|
|
4715
|
+
if (!approvedChains(core).has(target)) {
|
|
4716
|
+
throw new Error(
|
|
4717
|
+
`The WalletConnect session has not approved ${target}. Reconnect the wallet and try again.`
|
|
4718
|
+
);
|
|
4719
|
+
}
|
|
4720
|
+
core.setDefaultChain(target);
|
|
4721
|
+
const selected = parseChainId(
|
|
4722
|
+
await core.request({ method: "eth_chainId" })
|
|
4723
|
+
);
|
|
4724
|
+
debugLog("wc-switch", "selected WalletConnect session default", {
|
|
4725
|
+
peer: core.session.peer?.metadata?.name,
|
|
4726
|
+
target: chainId,
|
|
4727
|
+
selected
|
|
4728
|
+
});
|
|
4729
|
+
if (selected !== chainId) {
|
|
4730
|
+
throw new Error(
|
|
4731
|
+
`WalletConnect stayed on chain ${selected ?? "unknown"}; expected ${chainId}.`
|
|
4732
|
+
);
|
|
4733
|
+
}
|
|
4694
4734
|
}
|
|
4695
4735
|
async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
|
|
4696
|
-
const
|
|
4697
|
-
if (!
|
|
4736
|
+
const session = walletConnectSession(provider);
|
|
4737
|
+
if (!session) return false;
|
|
4738
|
+
const { core, strategy } = session;
|
|
4698
4739
|
holds.get(core.session.topic)?.();
|
|
4740
|
+
if (strategy === "session-default") {
|
|
4741
|
+
await selectSessionDefaultChain(core, chainId);
|
|
4742
|
+
return true;
|
|
4743
|
+
}
|
|
4699
4744
|
await requestSwitch(core, provider, chainId, timeoutMs);
|
|
4700
4745
|
holdChain(core, provider, chainId);
|
|
4701
4746
|
return true;
|
|
4702
4747
|
}
|
|
4703
4748
|
async function requestSwitch(core, provider, chainId, timeoutMs) {
|
|
4704
|
-
const current =
|
|
4749
|
+
const current = parseChainId(
|
|
4705
4750
|
await provider.request({ method: "eth_chainId" })
|
|
4706
4751
|
);
|
|
4752
|
+
if (current === void 0) {
|
|
4753
|
+
throw new Error("WalletConnect could not report its current chain.");
|
|
4754
|
+
}
|
|
4707
4755
|
const hexChainId = `0x${chainId.toString(16)}`;
|
|
4708
4756
|
let timer;
|
|
4709
4757
|
try {
|
|
@@ -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 (!
|
|
4817
|
+
if (!walletConnectSession(provider)) return provider;
|
|
4770
4818
|
const target = provider;
|
|
4771
4819
|
const request2 = async (args) => {
|
|
4772
4820
|
if (args?.method !== "wallet_switchEthereumChain") {
|