Skip to content

Commit

Permalink
removed supportsAddressSwitching logic (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
fan-zhang-sv authored Sep 13, 2023
1 parent 432f1a4 commit 04db88b
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions packages/wallet-sdk/src/provider/CoinbaseWalletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export interface CoinbaseWalletProviderOptions {
relayProvider: () => Promise<WalletSDKRelayAbstract>;
storage: ScopedLocalStorage;
diagnosticLogger?: DiagnosticLogger;
supportsAddressSwitching?: boolean;
isLedger?: boolean;
}

interface AddEthereumChainParams {
Expand Down Expand Up @@ -115,10 +113,6 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider

private hasMadeFirstChainChangedEmission = false;

private supportsAddressSwitching?: boolean;

private isLedger?: boolean;

constructor(options: Readonly<CoinbaseWalletProviderOptions>) {
super();

Expand Down Expand Up @@ -149,9 +143,6 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider

this.qrUrl = options.qrUrl;

this.supportsAddressSwitching = options.supportsAddressSwitching;
this.isLedger = options.isLedger;

const chainId = this.getChainId();
const chainIdStr = prepend0x(chainId.toString(16));
// indicate that we've connected, for EIP-1193 compliance
Expand All @@ -176,7 +167,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
}
);

if (this._addresses.length > 0) {
if (this._isAuthorized()) {
void this.initializeRelay();
}

Expand All @@ -189,7 +180,6 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
if (event.data.type !== 'walletLinkMessage') return; // compatibility with CBW extension

if (
event.data.data.action === 'defaultChainChanged' ||
event.data.data.action === 'dappChainSwitched'
) {
const _chainId = event.data.data.chainId;
Expand Down Expand Up @@ -255,13 +245,8 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
this.reloadOnDisconnect = false;
}

/**
* this function is called when coinbase provider is being injected to a dapp
* standalone + walletlinked extension, ledger, in-app browser using cipher-web-view
*/
public setProviderInfo(jsonRpcUrl: string, chainId: number) {
// extension tend to use the chianId from the dapp, while in-app browser and ledger overrides the default network
if (!(this.isLedger || this.isCoinbaseBrowser)) {
if (!this.isCoinbaseBrowser) {
this._chainIdFromOpts = chainId;
this._jsonRpcUrlFromOpts = jsonRpcUrl;
}
Expand Down Expand Up @@ -379,7 +364,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
sessionIdHash: this._relay ? Session.hash(this._relay.session.id) : undefined,
});

if (this._addresses.length > 0) {
if (this._isAuthorized()) {
return [...this._addresses];
}

Expand Down Expand Up @@ -619,7 +604,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
const { accounts } = res.result;

this._setAddresses(accounts);
if (!(this.isLedger || this.isCoinbaseBrowser)) {
if (!this.isCoinbaseBrowser) {
await this.switchEthereumChain(this.getChainId());
}

Expand Down Expand Up @@ -674,7 +659,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
return response;
}

private _setAddresses(addresses: string[], isDisconnect?: boolean): void {
protected _setAddresses(addresses: string[], _?: boolean): void {
if (!Array.isArray(addresses)) {
throw new Error('addresses is not an array');
}
Expand All @@ -685,14 +670,6 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
return;
}

if (this._addresses.length > 0 && this.supportsAddressSwitching === false && !isDisconnect) {
/**
* The extension currently doesn't support switching selected wallet index
* make sure walletlink doesn't update it's address in this case
*/
return;
}

this._addresses = newAddresses;
this.emit('accountsChanged', this._addresses);
this._storage.setItem(LOCAL_STORAGE_ADDRESSES_KEY, newAddresses.join(' '));
Expand Down Expand Up @@ -930,7 +907,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
};
}

private _isAuthorized(): boolean {
protected _isAuthorized(): boolean {
return this._addresses.length > 0;
}

Expand Down Expand Up @@ -1009,7 +986,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
sessionIdHash: this._relay ? Session.hash(this._relay.session.id) : undefined,
});

if (this._addresses.length > 0) {
if (this._isAuthorized()) {
return Promise.resolve({
jsonrpc: '2.0',
id: 0,
Expand All @@ -1033,7 +1010,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements Web3Provider
}

this._setAddresses(res.result);
if (!(this.isLedger || this.isCoinbaseBrowser)) {
if (!this.isCoinbaseBrowser) {
await this.switchEthereumChain(this.getChainId());
}

Expand Down

0 comments on commit 04db88b

Please sign in to comment.