Skip to content

Commit

Permalink
Disconnect to reset state completely (#1367)
Browse files Browse the repository at this point in the history
* disconnect

* test

* ensureInitialized before signer = null
  • Loading branch information
fan-zhang-sv authored Aug 5, 2024
1 parent 2676d62 commit e296efa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('Signer configuration', () => {
expect(mockRequest).toHaveBeenCalledWith(request);

await providerLoadedFromStorage.disconnect();
expect(mockDisconnect).toHaveBeenCalled();
expect(provider['signer']).toBeNull();
});

it('should throw error if signer is not initialized', async () => {
Expand All @@ -158,10 +158,10 @@ describe('Signer configuration', () => {
);
});

it('should call signer.disconnect on provider disconnect', async () => {
it('should set signer to null', async () => {
await provider.request({ method: 'eth_requestAccounts' });

await provider.disconnect();
expect(mockDisconnect).toHaveBeenCalled();
expect(provider['signer']).toBeNull();
});
});
7 changes: 3 additions & 4 deletions packages/wallet-sdk/src/CoinbaseWalletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export class CoinbaseWalletProvider extends ProviderEventEmitter implements Prov
break;
}
case 'net_version':
return 1;
return 1; // default value
case 'eth_chainId':
return hexStringFromNumber(1);
return hexStringFromNumber(1); // default value
default: {
throw standardErrors.provider.unauthorized(
"Must call 'eth_requestAccounts' before other methods"
Expand Down Expand Up @@ -87,8 +87,7 @@ export class CoinbaseWalletProvider extends ProviderEventEmitter implements Prov

async disconnect() {
await this.ensureInitialized();

await this.signer?.disconnect();
this.signer = null;
await clearAllStorage();
this.emit('disconnect', standardErrors.provider.disconnected('User initiated disconnection'));
}
Expand Down
5 changes: 5 additions & 0 deletions packages/wallet-sdk/src/sign/scw/SCWSigner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {

jest.mock('./SCWKeyManager');
const storageStoreSpy = jest.spyOn(ScopedAsyncStorage.prototype, 'storeObject');
const storageClearSpy = jest.spyOn(ScopedAsyncStorage.prototype, 'clear');

jest.mock(':util/cipher', () => ({
decryptContent: jest.fn(),
encryptContent: jest.fn(),
Expand Down Expand Up @@ -209,7 +211,10 @@ describe('SCWSigner', () => {
it('should disconnect successfully', async () => {
await signer.disconnect();

expect(storageClearSpy).toHaveBeenCalled();
expect(mockKeyManager.clear).toHaveBeenCalled();
expect(signer['accounts']).toEqual([]);
expect(signer['chain']).toEqual({ id: 1 });
});
});
});
1 change: 0 additions & 1 deletion packages/wallet-sdk/src/sign/scw/SCWSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export class SCWSigner implements Signer {
}

async disconnect() {
this.callback = null;
await this.storage.clear();
await this.keyManager.clear();
this.accounts = [];
Expand Down

0 comments on commit e296efa

Please sign in to comment.