Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions packages/eip-1193-provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,19 @@ export const createEIP1193Provider = async (
organizationId,
walletId,
});
walletAccounts.accounts.map(({ address }: { address: string }) => {
accounts.add(address as Address);
});
walletAccounts.accounts.map(
({
address,
addressFormat,
}: {
address: string;
addressFormat: string;
}) => {
if (addressFormat === "ADDRESS_FORMAT_ETHEREUM") {
accounts.add(address as Address);
}
},
);
setConnected(true, { chainId: activeChain.chainId });
return [...accounts];
}
Expand Down Expand Up @@ -186,6 +196,8 @@ export const createEIP1193Provider = async (
return `0x${signedTransaction}`;
}
case "wallet_addEthereumChain": {
console.log("wallet_addEthereumChain: all added chains", addedChains);

const [chain] = params as [AddEthereumChainParameter];

// Validate the to be added
Expand All @@ -206,15 +218,35 @@ export const createEIP1193Provider = async (
throw new ChainIdMismatchError(chain.chainId as Hex, rpcChainId);
}

addedChains.push({ ...chain, connected: true });
// Only add if it hasn't been added already
if (!addedChains.some((c) => c.chainId === rpcChainId)) {
addedChains.push({ ...chain, connected: true });
}

console.log("wallet_addEthereumChain: new chain id", rpcChainId);
console.log("wallet_addEthereumChain: new added chains", addedChains);

return null;
}

case "wallet_switchEthereumChain": {
const [targetChainId] = params as [string];
const targetChain = addedChains.find(
(chain) => chain.chainId === targetChainId,
const [targetChainId] = params as [{ chainId: string }];
const targetChain = addedChains.find((chain) => {
console.log("iterating through added chains:", chain);
return chain.chainId === targetChainId.chainId;
});

console.log(
"wallet_switchEthereumChain: target chain id",
targetChainId,
);
console.log(
"wallet_switchEthereumChain: target chain id type",
typeof targetChainId,
);
console.log(
"wallet_switchEthereumChain: all added chains",
addedChains,
);

if (!targetChain) {
Expand Down
4 changes: 2 additions & 2 deletions packages/eip-1193-provider/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { WalletAddEthereumChain, TransactionType } from "./types";
import {
BlockExplorerUrlError,
RpcUrlsRequiredError,
ChainIdAlreadyAddedError,
InvalidChainIdFormatError,
ChainIdValueExceedsError,
InvalidRpcUrlError,
Expand Down Expand Up @@ -123,7 +122,8 @@ export const validateChain = (
const { rpcUrls, blockExplorerUrls, chainId, nativeCurrency } = chain;

if (addedChains.some((c) => c.chainId === chainId)) {
throw new ChainIdAlreadyAddedError();
// Chain already added; return early
return;
}

let decimalChainId: Chain["id"];
Expand Down
Loading