diff --git a/packages/walletconnect-v2/src/index.spec.ts b/packages/walletconnect-v2/src/index.spec.ts index a27032452..5a36ecf1a 100644 --- a/packages/walletconnect-v2/src/index.spec.ts +++ b/packages/walletconnect-v2/src/index.spec.ts @@ -151,12 +151,12 @@ describe('WalletConnect', () => { expect(store.getState().chainId).toEqual(2) }) - test.skip('should throw an error when activating with an unknown chain', async () => { + test('should throw an error when activating with an unknown chain', async () => { const { connector } = createTestEnvironment({ chains }) await expect(connector.activate(99)).rejects.toThrow() }) - test.skip('should throw an error when using optional chain as default', async () => { + test('should throw an error when using optional chain as default', async () => { const { connector } = createTestEnvironment({ chains, optionalChains: [8] }, 8) await expect(connector.activate()).rejects.toThrow() }) diff --git a/packages/walletconnect-v2/src/index.ts b/packages/walletconnect-v2/src/index.ts index 722811302..f033bb0ba 100644 --- a/packages/walletconnect-v2/src/index.ts +++ b/packages/walletconnect-v2/src/index.ts @@ -61,18 +61,25 @@ export class WalletConnect extends Connector { public provider?: WalletConnectProvider public readonly events = new EventEmitter3() - private readonly defaultChainId?: number - private eagerConnection?: Promise - private readonly options: Omit + private readonly options: Omit + private readonly rpcMap?: Record + private readonly chains: number[] | ArrayOneOrMore | undefined + private readonly optionalChains: number[] | ArrayOneOrMore | undefined + private readonly defaultChainId?: number private readonly timeout: number + private eagerConnection?: Promise + constructor({ actions, defaultChainId, options, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) { super(actions, onError) - const { rpcMap, rpc, ...rest } = options - this.defaultChainId = defaultChainId + const { rpcMap, rpc, chains, optionalChains, ...rest } = options + + this.chains = chains + this.optionalChains = optionalChains this.options = rest + this.defaultChainId = defaultChainId this.rpcMap = rpcMap || rpc this.timeout = timeout } @@ -118,21 +125,10 @@ export class WalletConnect extends Connector { return this.handleProviderEvents(this.provider) } - // Helper function to reorder chains array based on desiredChainId - private reorderChainsBasedOnId( - chains: number[] | undefined, - desiredChainId: number | undefined - ): number[] | undefined { - if (!chains || !desiredChainId || !chains.includes(desiredChainId) || chains.length === 0) { - return chains - } - return getChainsWithDefault(chains, desiredChainId) - } - private getChainProps(desiredChainId: number | undefined = this.defaultChainId): ChainsProps { // Reorder chains and optionalChains if necessary - const orderedChains = this.reorderChainsBasedOnId(this.options.chains, desiredChainId) - const orderedOptionalChains = this.reorderChainsBasedOnId(this.options.optionalChains, desiredChainId) + const orderedChains = getChainsWithDefault(this.chains, desiredChainId) + const orderedOptionalChains = getChainsWithDefault(this.optionalChains, desiredChainId) // Validate and return the result if (isArrayOneOrMore(orderedChains)) { diff --git a/packages/walletconnect-v2/src/utils.ts b/packages/walletconnect-v2/src/utils.ts index d42f686d3..ae555bc8b 100644 --- a/packages/walletconnect-v2/src/utils.ts +++ b/packages/walletconnect-v2/src/utils.ts @@ -99,7 +99,13 @@ async function getBestUrl(urls: string | string[], timeout: number): Promise | undefined, + defaultChainId: number | undefined +) { + if (!chains || !defaultChainId || chains.length === 0) { + return chains + } const idx = chains.indexOf(defaultChainId) if (idx === -1) { throw new Error(