From f4c255b3c2961a46dfa4f77f042841e92a1ee9b8 Mon Sep 17 00:00:00 2001 From: Felix Zhang Date: Fri, 15 Mar 2024 14:31:54 -0700 Subject: [PATCH 1/2] initialize SCWStateManager with dapp chainIds --- packages/wallet-sdk/src/sign/scw/SCWSigner.ts | 1 + packages/wallet-sdk/src/sign/scw/SCWStateManager.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/wallet-sdk/src/sign/scw/SCWSigner.ts b/packages/wallet-sdk/src/sign/scw/SCWSigner.ts index e4f7a68549..b5c595ef68 100644 --- a/packages/wallet-sdk/src/sign/scw/SCWSigner.ts +++ b/packages/wallet-sdk/src/sign/scw/SCWSigner.ts @@ -39,6 +39,7 @@ export class SCWSigner implements Signer { this.puc = options.puc; this.keyManager = new SCWKeyManager(); this.stateManager = new SCWStateManager({ + appChainIds: this.appChainIds, updateListener: { onAccountsUpdate: (...args) => options.updateListener.onAccountsUpdate(this, ...args), onChainUpdate: (...args) => options.updateListener.onChainUpdate(this, ...args), diff --git a/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts b/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts index 7316da1ca7..74de0aa6cc 100644 --- a/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts +++ b/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts @@ -20,7 +20,7 @@ export class SCWStateManager { return this._activeChain; } - constructor(options: { updateListener: StateUpdateListener }) { + constructor(options: { appChainIds: number[]; updateListener: StateUpdateListener }) { this.updateListener = options.updateListener; this.availableChains = this.loadItemFromStorage(AVAILABLE_CHAINS_STORAGE_KEY); @@ -41,7 +41,7 @@ export class SCWStateManager { } this._accounts = accounts || []; - this._activeChain = chain || { id: 1 }; + this._activeChain = chain || { id: options.appChainIds[0] }; } updateAccounts(accounts: AddressString[]) { From 1416b4ecb2abbefaffa7f05278dfbef7e8c1cddf Mon Sep 17 00:00:00 2001 From: Felix Zhang Date: Fri, 15 Mar 2024 14:53:04 -0700 Subject: [PATCH 2/2] test --- .../src/sign/scw/SCWStateManager.test.ts | 31 +++++++++++++++++++ .../src/sign/scw/SCWStateManager.ts | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/wallet-sdk/src/sign/scw/SCWStateManager.test.ts b/packages/wallet-sdk/src/sign/scw/SCWStateManager.test.ts index ec4664f6e0..02e8524a9d 100644 --- a/packages/wallet-sdk/src/sign/scw/SCWStateManager.test.ts +++ b/packages/wallet-sdk/src/sign/scw/SCWStateManager.test.ts @@ -11,6 +11,7 @@ describe('SCWStateManager', () => { beforeEach(() => { stateManager = new SCWStateManager({ + appChainIds: [DEFAULT_CHAIN.id], updateListener: { onAccountsUpdate: jest.fn(), onChainUpdate: jest.fn(), @@ -22,6 +23,35 @@ describe('SCWStateManager', () => { stateManager.clear(); }); + describe('fallback to appChainIds[0]', () => { + const appChainIds = [10]; + + let stateManager: SCWStateManager; + beforeEach(() => { + stateManager = new SCWStateManager({ + appChainIds, + updateListener: { + onAccountsUpdate: jest.fn(), + onChainUpdate: jest.fn(), + }, + }); + }); + it('should use the first chain id from appChainIds as the active chain', () => { + expect(stateManager.activeChain.id).toBe(appChainIds[0]); + }); + + it('should use the first chain id from appChainIds as the active chain when appChainIds is empty', () => { + stateManager = new SCWStateManager({ + appChainIds: [], + updateListener: { + onAccountsUpdate: jest.fn(), + onChainUpdate: jest.fn(), + }, + }); + expect(stateManager.activeChain.id).toBe(1); + }); + }); + describe('switchChain', () => { beforeEach(() => { stateManager.updateAvailableChains(AVAILABLE_CHAINS); @@ -64,6 +94,7 @@ describe('SCWStateManager', () => { beforeEach(() => { stateManager = new SCWStateManager({ + appChainIds: [1], updateListener: { onAccountsUpdate: jest.fn(), onChainUpdate: chainUpdatedListener, diff --git a/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts b/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts index 74de0aa6cc..6d012e2f70 100644 --- a/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts +++ b/packages/wallet-sdk/src/sign/scw/SCWStateManager.ts @@ -41,7 +41,7 @@ export class SCWStateManager { } this._accounts = accounts || []; - this._activeChain = chain || { id: options.appChainIds[0] }; + this._activeChain = chain || { id: options.appChainIds?.[0] ?? 1 }; } updateAccounts(accounts: AddressString[]) {