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

Updates new custom chain config #1859

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions packages/adapters/base-evm-adapter/src/baseEvmAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export abstract class BaseEvmAdapter<T> extends BaseAdapter<T> {

async authenticateUser(): Promise<UserAuthInfo> {
if (!this.provider || this.status !== ADAPTER_STATUS.CONNECTED) throw WalletLoginError.notConnectedError();
const { chainNamespace, chainId } = this.chainConfig;
const { chainNamespace, id: chainId } = this.chainConfig;
const accounts = await this.provider.request<never, string[]>({
method: "eth_accounts",
});
Expand All @@ -38,7 +38,7 @@ export abstract class BaseEvmAdapter<T> extends BaseAdapter<T> {
domain: window.location.origin,
uri: window.location.href,
address: accounts[0],
chainId: parseInt(chainId, 16),
chainId,
version: "1",
nonce: Math.random().toString(36).slice(2),
issuedAt: new Date().toISOString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class BaseSolanaAdapter<T> extends BaseAdapter<T> {
async authenticateUser(): Promise<UserAuthInfo> {
if (!this.provider || this.status !== ADAPTER_STATUS.CONNECTED) throw WalletLoginError.notConnectedError();

const { chainNamespace, chainId } = this.chainConfig;
const { chainNamespace, id: chainId } = this.chainConfig;

const accounts = await this.provider.request<never, string[]>({
method: "getAccounts",
Expand All @@ -41,7 +41,7 @@ export abstract class BaseSolanaAdapter<T> extends BaseAdapter<T> {
domain: window.location.origin,
uri: window.location.href,
address: accounts[0],
chainId: parseInt(chainId, 16),
chainId,
version: "1",
nonce: Math.random().toString(36).slice(2),
issuedAt: new Date().toISOString(),
Expand Down
24 changes: 12 additions & 12 deletions packages/adapters/coinbase-adapter/src/coinbaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CoinbaseAdapter extends BaseEvmAdapter<void> {
super.checkInitializationRequirements();
this.coinbaseInstance = new CoinbaseWalletSDK({
...this.coinbaseOptions,
appChainIds: [Number.parseInt(this.chainConfig.chainId, 16)],
appChainIds: [this.chainConfig.id],
});
this.coinbaseProvider = this.coinbaseInstance.makeWeb3Provider({ options: "all" });
this.status = ADAPTER_STATUS.READY;
Expand All @@ -93,9 +93,9 @@ class CoinbaseAdapter extends BaseEvmAdapter<void> {
try {
await this.coinbaseProvider.request({ method: "eth_requestAccounts" });
const { chainId } = (await this.coinbaseProvider.request({ method: "eth_chainId" })) as { chainId: string };
if (chainId !== (this.chainConfig as CustomChainConfig).chainId) {
if (chainId !== this.chainConfig.id.toString(16)) {
await this.addChain(this.chainConfig as CustomChainConfig);
await this.switchChain(this.chainConfig as CustomChainConfig, true);
await this.switchChain({ chainId: this.chainConfig.id }, true);
}
this.status = ADAPTER_STATUS.CONNECTED;
if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider");
Expand Down Expand Up @@ -143,14 +143,14 @@ class CoinbaseAdapter extends BaseEvmAdapter<void> {
method: "wallet_addEthereumChain",
params: [
{
chainId: chainConfig.chainId,
chainName: chainConfig.displayName,
rpcUrls: [chainConfig.rpcTarget],
blockExplorerUrls: [chainConfig.blockExplorerUrl],
chainId: chainConfig.id,
chainName: chainConfig.name,
rpcUrls: [chainConfig.rpcUrls.default.http[0]],
blockExplorerUrls: [chainConfig.blockExplorers?.default?.url],
nativeCurrency: {
name: chainConfig.tickerName,
symbol: chainConfig.ticker,
decimals: chainConfig.decimals || 18,
name: chainConfig.nativeCurrency.name,
symbol: chainConfig.nativeCurrency.symbol,
decimals: chainConfig.nativeCurrency.symbol || 18,
},
iconUrls: [chainConfig.logo],
},
Expand All @@ -159,11 +159,11 @@ class CoinbaseAdapter extends BaseEvmAdapter<void> {
super.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
await this.coinbaseProvider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: params.chainId }],
params: [{ chainId: params.chainId.toString(16) }],
});
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/default-evm-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const getDefaultExternalAdapters = async (params: { options: IWeb3AuthCor
const { clientId, chainConfig, sessionTime, web3AuthNetwork, useCoreKitKey } = options;
if (!Object.values(CHAIN_NAMESPACES).includes(chainConfig.chainNamespace)) throw new Error(`Invalid chainNamespace: ${chainConfig.chainNamespace}`);
const finalChainConfig = {
...(getChainConfig(chainConfig.chainNamespace, chainConfig?.chainId) as CustomChainConfig),
...(getChainConfig(chainConfig.chainNamespace, chainConfig?.id) as CustomChainConfig),
...(chainConfig || {}),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/default-solana-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const getDefaultExternalAdapters = async (params: { options: IWeb3AuthCor
const { clientId, chainConfig, sessionTime, web3AuthNetwork, useCoreKitKey } = options;
if (!Object.values(CHAIN_NAMESPACES).includes(chainConfig.chainNamespace)) throw new Error(`Invalid chainNamespace: ${chainConfig.chainNamespace}`);
const finalChainConfig = {
...(getChainConfig(chainConfig.chainNamespace, chainConfig?.chainId) as CustomChainConfig),
...(getChainConfig(chainConfig.chainNamespace, chainConfig?.id) as CustomChainConfig),
...(chainConfig || {}),
};
const [{ SolanaWalletAdapter }, { PhantomAdapter }] = await Promise.all([
Expand Down
22 changes: 9 additions & 13 deletions packages/adapters/metamask-adapter/src/metamaskAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class MetamaskAdapter extends BaseEvmAdapter<void> {
try {
await this.metamaskProvider.request({ method: "eth_requestAccounts" });
const { chainId } = this.metamaskProvider;
if (chainId !== (this.chainConfig as CustomChainConfig).chainId) {
if (chainId !== this.chainConfig.id.toString(16)) {
await this.addChain(this.chainConfig as CustomChainConfig, true);
await this.switchChain(this.chainConfig as CustomChainConfig, true);
await this.switchChain({ chainId: this.chainConfig.id }, true);
}
this.status = ADAPTER_STATUS.CONNECTED;
if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider");
Expand Down Expand Up @@ -153,27 +153,23 @@ class MetamaskAdapter extends BaseEvmAdapter<void> {
method: "wallet_addEthereumChain",
params: [
{
chainId: chainConfig.chainId,
chainName: chainConfig.displayName,
rpcUrls: [chainConfig.rpcTarget],
blockExplorerUrls: [chainConfig.blockExplorerUrl],
nativeCurrency: {
name: chainConfig.tickerName,
symbol: chainConfig.ticker,
decimals: chainConfig.decimals || 18,
},
chainId: chainConfig.id,
chainName: chainConfig.name,
rpcUrls: [chainConfig.rpcUrls.default.http[0]],
blockExplorerUrls: [chainConfig.blockExplorers?.default?.url],
nativeCurrency: chainConfig.nativeCurrency,
iconUrls: [chainConfig.logo],
},
],
});
this.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
await this.metamaskProvider?.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: params.chainId }],
params: [{ chainId: params.chainId.toString(16) }],
});
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class OpenloginAdapter extends BaseAdapter<OpenloginLoginParams> {
this.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
await this.privateKeyProvider?.switchChain(params);
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
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 @@ -156,7 +156,7 @@ export class PhantomAdapter extends BaseSolanaAdapter<void> {
this.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
await this.phantomProvider?.switchChain(params);
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
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 @@ -134,7 +134,7 @@ export class SlopeAdapter extends BaseSolanaAdapter<void> {
throw new Error("Method Not implemented");
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
await this.slopeProxyProvider?.switchChain(params);
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
Expand Down
8 changes: 4 additions & 4 deletions packages/adapters/solflare-adapter/src/solflareAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export class SolflareAdapter extends BaseSolanaAdapter<void> {
this.status = ADAPTER_STATUS.CONNECTING;
this.emit(ADAPTER_EVENTS.CONNECTING, { adapter: WALLET_ADAPTERS.SOLFLARE });
let cluster: Cluster = "mainnet-beta";
if (this.chainConfig?.chainId === "0x1") {
if (this.chainConfig?.id === 1) {
cluster = "mainnet-beta";
} else if (this.chainConfig?.chainId === "0x2") {
} else if (this.chainConfig?.id === 2) {
cluster = "devnet";
} else if (this.chainConfig?.chainId === "0x3") {
} else if (this.chainConfig?.id === 3) {
cluster = "testnet";
} else {
throw WalletLoginError.connectionError("Invalid chainId, solflare doesn't support custom solana networks");
Expand Down Expand Up @@ -144,7 +144,7 @@ export class SolflareAdapter extends BaseSolanaAdapter<void> {
this.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
await this.solflareProvider?.switchChain(params);
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
Expand Down
48 changes: 24 additions & 24 deletions packages/adapters/torus-evm-adapter/src/torusWalletAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export class TorusWalletAdapter extends BaseEvmAdapter<never> {
await super.init(options);
super.checkInitializationRequirements();

const { chainId, blockExplorerUrl, displayName, rpcTarget, ticker, tickerName } = this.chainConfig as CustomChainConfig;
const { id, blockExplorers, name, rpcUrls, nativeCurrency } = this.chainConfig as CustomChainConfig;
const network: NetworkInterface = {
chainId: Number.parseInt(chainId, 16),
host: rpcTarget,
blockExplorer: blockExplorerUrl,
networkName: displayName,
ticker,
tickerName,
chainId: id,
host: rpcUrls?.default?.http?.[0],
blockExplorer: blockExplorers?.default?.url,
networkName: name,
ticker: nativeCurrency?.symbol,
tickerName: nativeCurrency?.name,
// decimals: decimals || 18,
};

Expand Down Expand Up @@ -111,15 +111,15 @@ export class TorusWalletAdapter extends BaseEvmAdapter<never> {
try {
await this.torusInstance.login(this.loginSettings);
const chainId = await this.torusInstance.provider.request<string>({ method: "eth_chainId" });
if (chainId && parseInt(chainId) !== parseInt((this.chainConfig as CustomChainConfig).chainId, 16)) {
const { chainId: _chainId, blockExplorerUrl, displayName, rpcTarget, ticker, tickerName } = this.chainConfig as CustomChainConfig;
if (chainId && parseInt(chainId) !== this.chainConfig.id) {
const { id: _chainId, blockExplorers, name, rpcUrls, nativeCurrency } = this.chainConfig as CustomChainConfig;
const network: NetworkInterface = {
chainId: Number.parseInt(_chainId, 16),
host: rpcTarget,
blockExplorer: blockExplorerUrl,
networkName: displayName,
tickerName,
ticker,
chainId: _chainId,
host: rpcUrls?.default?.http?.[0],
blockExplorer: blockExplorers?.default?.url,
networkName: name,
tickerName: nativeCurrency?.symbol,
ticker: nativeCurrency?.name,
};
// in some cases when user manually switches chain and relogin then adapter will not connect to initially passed
// chainConfig but will connect to the one that user switched to.
Expand All @@ -128,10 +128,10 @@ export class TorusWalletAdapter extends BaseEvmAdapter<never> {
...network,
});
const updatedChainID = await this.torusInstance.ethereum.request<string>({ method: "eth_chainId" });
if (updatedChainID && parseInt(updatedChainID) !== parseInt((this.chainConfig as CustomChainConfig).chainId, 16)) {
if (updatedChainID && parseInt(updatedChainID) !== this.chainConfig.id) {
throw WalletInitializationError.fromCode(
5000,
`Not connected to correct chainId. Expected: ${(this.chainConfig as CustomChainConfig).chainId}, Current: ${updatedChainID}`
`Not connected to correct chainId. Expected: ${this.chainConfig.id}, Current: ${updatedChainID}`
);
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ export class TorusWalletAdapter extends BaseEvmAdapter<never> {
this.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
// TODO: add these in torus wallet.
// await this.torusInstance?.provider.request({
Expand All @@ -201,12 +201,12 @@ export class TorusWalletAdapter extends BaseEvmAdapter<never> {
// });
const chainConfig = this.getChainConfig(params.chainId) as CustomChainConfig;
await this.torusInstance?.setProvider({
host: chainConfig.rpcTarget,
chainId: parseInt(chainConfig.chainId, 16),
networkName: chainConfig.displayName,
blockExplorer: chainConfig.blockExplorerUrl,
ticker: chainConfig.ticker,
tickerName: chainConfig.tickerName,
host: chainConfig.rpcUrls?.default?.http?.[0],
chainId: chainConfig.id,
networkName: chainConfig.name,
blockExplorer: chainConfig.blockExplorers?.default?.url,
ticker: chainConfig.nativeCurrency?.symbol,
tickerName: chainConfig.nativeCurrency?.name,
});
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
}
Expand Down
38 changes: 27 additions & 11 deletions packages/adapters/torus-solana-adapter/src/solanaWalletAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ export class SolanaWalletAdapter extends BaseSolanaAdapter<void> {
async init(options: AdapterInitOptions = {}): Promise<void> {
await super.init(options);
super.checkInitializationRequirements();
const { chainId, blockExplorerUrl, displayName, rpcTarget, ticker, tickerName, logo } = this.chainConfig as CustomChainConfig;
const network: NetworkInterface = { chainId, rpcTarget, blockExplorerUrl, displayName, tickerName, ticker, logo };
const { id: chainId, blockExplorers, name, rpcUrls, nativeCurrency, logo } = this.chainConfig as CustomChainConfig;
const network: NetworkInterface = {
chainId: chainId.toString(16),
rpcTarget: rpcUrls?.default?.http?.[0],
blockExplorerUrl: blockExplorers?.default?.url,
displayName: name,
tickerName: nativeCurrency?.symbol,
ticker: nativeCurrency?.name,
logo,
};

this.torusInstance = new Torus(this.torusWalletOptions);
log.debug("initializing torus solana adapter init");
Expand Down Expand Up @@ -122,8 +130,16 @@ export class SolanaWalletAdapter extends BaseSolanaAdapter<void> {
// some issue in solana wallet, always connecting to mainnet on init.
// fallback to change network if not connected to correct one on login.
if (error instanceof Web3AuthError && error.code === 5010) {
const { chainId, blockExplorerUrl, logo, displayName, rpcTarget, ticker, tickerName } = this.chainConfig as CustomChainConfig;
const network = { chainId, rpcTarget, blockExplorerUrl, displayName, tickerName, ticker, logo };
const { id: chainId, blockExplorers, name, rpcUrls, nativeCurrency, logo } = this.chainConfig as CustomChainConfig;
const network: NetworkInterface = {
chainId: chainId.toString(16),
rpcTarget: rpcUrls?.default?.http?.[0],
blockExplorerUrl: blockExplorers?.default?.url,
displayName: name,
tickerName: nativeCurrency?.symbol,
ticker: nativeCurrency?.name,
logo,
};
await this.torusInstance.setProvider(network);
} else {
throw error;
Expand Down Expand Up @@ -172,16 +188,16 @@ export class SolanaWalletAdapter extends BaseSolanaAdapter<void> {
this.addChainConfig(chainConfig);
}

public async switchChain(params: { chainId: string }, init = false): Promise<void> {
public async switchChain(params: { chainId: number }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
const chainConfig = this.getChainConfig(params.chainId) as CustomChainConfig;
await this.torusInstance?.setProvider({
rpcTarget: chainConfig.rpcTarget,
chainId: chainConfig.chainId,
displayName: chainConfig.displayName,
blockExplorerUrl: chainConfig.blockExplorerUrl,
ticker: chainConfig.ticker,
tickerName: chainConfig.tickerName,
rpcTarget: chainConfig.rpcUrls?.default?.http?.[0],
chainId: chainConfig.id.toString(16),
displayName: chainConfig.name,
blockExplorerUrl: chainConfig.blockExplorers?.default?.url,
ticker: chainConfig.nativeCurrency?.name,
tickerName: chainConfig.nativeCurrency?.symbol,
logo: chainConfig.logo || "https://images.web3auth.io/login-torus-solana.svg",
});
this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) as CustomChainConfig });
Expand Down
Loading
Loading