diff --git a/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts b/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts index 38943c5c22..f9ff6e7fca 100644 --- a/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts +++ b/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts @@ -1,7 +1,6 @@ import { CoinbaseWalletProvider } from './CoinbaseWalletProvider'; import { standardErrors } from './core/error'; import * as util from './sign/util'; -import { AddressString } from ':core/type'; function createProvider() { return new CoinbaseWalletProvider({ diff --git a/packages/wallet-sdk/src/CoinbaseWalletProvider.ts b/packages/wallet-sdk/src/CoinbaseWalletProvider.ts index b85c488474..e1dc29b22f 100644 --- a/packages/wallet-sdk/src/CoinbaseWalletProvider.ts +++ b/packages/wallet-sdk/src/CoinbaseWalletProvider.ts @@ -144,15 +144,16 @@ export class CoinbaseWalletProvider extends EventEmitter implements ProviderInte } async disconnect(): Promise { - if (!this.signer) return; - this.signer.disconnect(); + if (this.signer) { + this.signer.disconnect(); + } ScopedLocalStorage.clearAll(); this.emit('disconnect', standardErrors.provider.disconnected('User initiated disconnection')); } readonly isCoinbaseWallet = true; - protected readonly updateListener = { + protected readonly updateListener = { onAccountsUpdate: (accounts: AddressString[]) => { if (areAddressArraysEqual(this.accounts, accounts)) return; this.emit('accountsChanged', this.accounts); @@ -162,7 +163,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements ProviderInte this.emit('chainChanged', hexStringFromIntNumber(IntNumber(chain.id))); }, }; - + private requestSignerSelection(): Promise { return fetchSignerType({ communicator: this.communicator, diff --git a/packages/wallet-sdk/src/core/provider/interface.ts b/packages/wallet-sdk/src/core/provider/interface.ts index 012af57dea..82aac930a8 100644 --- a/packages/wallet-sdk/src/core/provider/interface.ts +++ b/packages/wallet-sdk/src/core/provider/interface.ts @@ -1,7 +1,7 @@ import { EventEmitter } from 'eventemitter3'; import { Method } from './method'; -import { AddressString } from ':core/type'; +import { AddressString, Chain } from ':core/type'; export interface RequestArguments { readonly method: Method | string; @@ -53,6 +53,8 @@ export interface ConstructorOptions { } export interface Signer { + readonly accounts: AddressString[]; + readonly chain: Chain; handshake(): Promise; request(request: RequestArguments): Promise; disconnect: () => Promise; diff --git a/packages/wallet-sdk/src/sign/interface.ts b/packages/wallet-sdk/src/sign/interface.ts index 7bf8098594..5cf7b4d7d8 100644 --- a/packages/wallet-sdk/src/sign/interface.ts +++ b/packages/wallet-sdk/src/sign/interface.ts @@ -1,14 +1,5 @@ -import { RequestArguments } from ':core/provider/interface'; import { AddressString, Chain } from ':core/type'; -export interface Signer { - readonly accounts: AddressString[]; - readonly chain: Chain; - handshake(): Promise; - request(request: RequestArguments): Promise; - disconnect: () => Promise; -} - export interface StateUpdateListener { onAccountsUpdate: (_: AddressString[]) => void; onChainUpdate: (_: Chain) => void; diff --git a/packages/wallet-sdk/src/sign/scw/SCWSigner.ts b/packages/wallet-sdk/src/sign/scw/SCWSigner.ts index b548ede3f8..73c6d1e3db 100644 --- a/packages/wallet-sdk/src/sign/scw/SCWSigner.ts +++ b/packages/wallet-sdk/src/sign/scw/SCWSigner.ts @@ -29,17 +29,18 @@ export class SCWSigner implements Signer { private availableChains?: Chain[]; private _accounts: AddressString[]; - private _activeChain: Chain; + private _chain: Chain; get accounts() { return this._accounts; } - get activeChain() { - return this._activeChain; + get chain() { + return this._chain; } constructor(params: { metadata: AppMetadata; communicator: Communicator; + updateListener: StateUpdateListener; }) { this.metadata = params.metadata; this.communicator = params.communicator; @@ -48,7 +49,7 @@ export class SCWSigner implements Signer { this.storage = new ScopedLocalStorage('CBWSDK', 'SCWStateManager'); this._accounts = this.storage.loadObject(ACCOUNTS_KEY) ?? []; - this._activeChain = this.storage.loadObject(ACTIVE_CHAIN_STORAGE_KEY) || { + this._chain = this.storage.loadObject(ACTIVE_CHAIN_STORAGE_KEY) || { id: params.metadata.appChainIds?.[0] ?? 1, }; @@ -58,14 +59,6 @@ export class SCWSigner implements Signer { this.decryptResponseMessage = this.decryptResponseMessage.bind(this); } - get accounts() { - return this.stateManager.accounts; - } - - get chain() { - return this.stateManager.activeChain; - } - async handshake(): Promise { const handshakeMessage = await this.createRequestMessage({ handshake: { @@ -148,7 +141,7 @@ export class SCWSigner implements Signer { const encrypted = await encryptContent( { action: request, - chainId: this._activeChain.id, + chainId: this._chain.id, }, sharedSecret ); @@ -203,7 +196,7 @@ export class SCWSigner implements Signer { })); this.availableChains = chains; this.storage.storeObject(AVAILABLE_CHAINS_STORAGE_KEY, chains); - this.switchChain(this._activeChain.id); + this.switchChain(this._chain.id); } if (capabilities) { @@ -214,9 +207,9 @@ export class SCWSigner implements Signer { private switchChain(chainId: number): boolean { const chain = this.availableChains?.find((chain) => chain.id === chainId); if (!chain) return false; - if (chain === this._activeChain) return true; + if (chain === this._chain) return true; - this._activeChain = chain; + this._chain = chain; this.storage.storeObject(ACTIVE_CHAIN_STORAGE_KEY, chain); this.updateListener.onChainUpdate(chain); return true;