Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix switch chain in plugins #1491

Merged
merged 3 commits into from
Jun 14, 2023
Merged
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
118 changes: 78 additions & 40 deletions demo/react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@web3auth/openlogin-adapter": "file:../../packages/adapters/openlogin-adapter",
"@web3auth/solana-provider": "file:../../packages/providers/solana-provider",
"@web3auth/modal": "file:../../packages/modal",
"@web3auth/torus-wallet-connector-plugin": "file:../../packages/plugins/torus-wallet-connector-plugin",
"path-browserify": "^1.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
8 changes: 7 additions & 1 deletion demo/react-app/src/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useWeb3Auth } from "../services/web3auth";
import styles from "../styles/Home.module.css";

const Main = () => {
const { provider, login, logout, getUserInfo, getAccounts, getBalance, signMessage, signTransaction, signAndSendTransaction, web3Auth, chain } = useWeb3Auth();
const { provider, login, logout, getUserInfo, getAccounts, getBalance, signMessage, signTransaction, signAndSendTransaction, web3Auth, chain, addChain, switchChain } = useWeb3Auth();

const loggedInView = (
<>
Expand All @@ -19,6 +19,12 @@ const Main = () => {
<button onClick={signMessage} className={styles.card}>
Sign Message
</button>
<button onClick={addChain} className={styles.card}>
Add Chain
</button>
<button onClick={switchChain} className={styles.card}>
Switch Chain
</button>
{
(web3Auth?.connectedAdapterName === WALLET_ADAPTERS.OPENLOGIN || chain === "solana") &&
(<button onClick={signTransaction} className={styles.card}>
Expand Down
1 change: 0 additions & 1 deletion demo/react-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import App from "./App";
const container = document.getElementById("root") as HTMLElement;
const root = createRoot(container);


root.render(
<StrictMode>
<App />
Expand Down
54 changes: 51 additions & 3 deletions demo/react-app/src/services/web3auth.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ADAPTER_EVENTS, SafeEventEmitterProvider } from "@web3auth/base";
import { ADAPTER_EVENTS, CHAIN_NAMESPACES, SafeEventEmitterProvider } from "@web3auth/base";
import { Web3Auth } from "@web3auth/modal";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";
import { TorusWalletConnectorPlugin } from "@web3auth/torus-wallet-connector-plugin";
import { createContext, FunctionComponent, ReactNode, useCallback, useContext, useEffect, useState } from "react";
import { CHAIN_CONFIG, CHAIN_CONFIG_TYPE } from "../config/chainConfig";
import { WEB3AUTH_NETWORK_TYPE } from "../config/web3AuthNetwork";
Expand All @@ -20,6 +21,8 @@ export interface IWeb3AuthContext {
getBalance: () => Promise<any>;
signTransaction: () => Promise<void>;
signAndSendTransaction: () => Promise<void>;
addChain: () => Promise<void>;
switchChain: () => Promise<void>;
}

export const Web3AuthContext = createContext<IWeb3AuthContext>({
Expand All @@ -36,6 +39,8 @@ export const Web3AuthContext = createContext<IWeb3AuthContext>({
getBalance: async () => {},
signTransaction: async () => {},
signAndSendTransaction: async () => {},
addChain: async () => {},
switchChain: async () => {},
});

export function useWeb3Auth(): IWeb3AuthContext {
Expand Down Expand Up @@ -90,10 +95,9 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
});
};

const currentChainConfig = CHAIN_CONFIG[chain];

async function init() {
try {
const currentChainConfig = CHAIN_CONFIG[chain];
setIsLoading(true);
const clientId = "BKPxkCtfC9gZ5dj-eg-W6yb5Xfr3XkxHuGZl2o2Bn8gKQ7UYike9Dh6c-_LaXlUN77x0cBoPwcSx-IVm0llVsLA";
const web3AuthInstance = new Web3Auth({
Expand Down Expand Up @@ -123,6 +127,21 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
},
});
web3AuthInstance.configureAdapter(adapter);
const plugin = new TorusWalletConnectorPlugin({
walletInitOptions: {
whiteLabel: {
logoDark: "https://avatars.githubusercontent.com/u/37784849?s=200&v=4",
logoLight: "https://avatars.githubusercontent.com/u/37784849?s=200&v=4",
theme: {
isDark: true,
colors: {},
},
},
buildEnv: "production",
useWalletConnect: true,
},
});
web3AuthInstance.addPlugin(plugin);
subscribeAuthEvents(web3AuthInstance);
setWeb3Auth(web3AuthInstance);
await web3AuthInstance.initModal({
Expand Down Expand Up @@ -183,6 +202,33 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
uiConsole(user);
};

const addChain = async () => {
if (!provider) {
uiConsole("provider not initialized yet");
return;
}
const newChain = {
chainId: "0x19",
displayName: "Cronos Mainnet Beta",
chainNamespace: CHAIN_NAMESPACES.EIP155,
tickerName: "Cronos",
ticker: "CRO",
decimals: 18,
rpcTarget: "https://cronos-evm.publicnode.com",
blockExplorer: "https://goerli.etherscan.io",
};
await web3Auth?.addChain(newChain);
uiConsole("New Chain Added");
};
const switchChain = async () => {
if (!provider) {
uiConsole("provider not initialized yet");
return;
}
await web3Auth?.switchChain({ chainId: "0x19" });
uiConsole("Chain Switched");
};

const getAccounts = async () => {
if (!provider) {
console.log("provider not initialized yet");
Expand Down Expand Up @@ -249,6 +295,8 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
signMessage,
signTransaction,
signAndSendTransaction,
addChain,
switchChain,
};
return <Web3AuthContext.Provider value={contextProvider}>{children}</Web3AuthContext.Provider>;
};
2 changes: 1 addition & 1 deletion packages/adapters/coinbase-adapter/src/coinbaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class CoinbaseAdapter extends BaseEvmAdapter<void> {
}

public async addChain(chainConfig: CustomChainConfig, init = false): Promise<void> {
super.checkAddChainRequirements(init);
super.checkAddChainRequirements(chainConfig, init);
await this.coinbaseProvider.request({
method: "wallet_addEthereumChain",
params: [
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/metamask-adapter/src/metamaskAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class MetamaskAdapter extends BaseEvmAdapter<void> {
}

public async addChain(chainConfig: CustomChainConfig, init = false): Promise<void> {
super.checkAddChainRequirements(init);
super.checkAddChainRequirements(chainConfig, init);
await this.metamaskProvider?.request({
method: "wallet_addEthereumChain",
params: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class OpenloginAdapter extends BaseAdapter<OpenloginLoginParams> {
}

public async addChain(chainConfig: CustomChainConfig, init = false): Promise<void> {
super.checkAddChainRequirements(init);
super.checkAddChainRequirements(chainConfig, init);
this.privateKeyProvider?.addChain(chainConfig);
this.addChainConfig(chainConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/phantom-adapter/src/phantomAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class PhantomAdapter extends BaseSolanaAdapter<void> {
}

public async addChain(chainConfig: CustomChainConfig, init = false): Promise<void> {
super.checkAddChainRequirements(init);
super.checkAddChainRequirements(chainConfig, init);
this.phantomProvider?.addChain(chainConfig);
this.addChainConfig(chainConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/slope-adapter/src/slopeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class SlopeAdapter extends BaseSolanaAdapter<void> {
}

public async addChain(chainConfig: CustomChainConfig, init = false): Promise<void> {
super.checkAddChainRequirements(init);
super.checkAddChainRequirements(chainConfig, init);
this.slopeProxyProvider?.addChain(chainConfig);
this.addChainConfig(chainConfig);
}
Expand Down
Loading