diff --git a/example/components/ConnectWithSelect.tsx b/example/components/ConnectWithSelect.tsx index d11314021..3568c6574 100644 --- a/example/components/ConnectWithSelect.tsx +++ b/example/components/ConnectWithSelect.tsx @@ -26,14 +26,12 @@ function ChainSelect({ }} disabled={switchChain === undefined} > - - + {chainIds.map((chainId) => ( - ))} diff --git a/packages/walletconnect-v2/package.json b/packages/walletconnect-v2/package.json index dbe777fba..db2bd38cd 100644 --- a/packages/walletconnect-v2/package.json +++ b/packages/walletconnect-v2/package.json @@ -24,7 +24,7 @@ "start": "tsc --watch" }, "dependencies": { - "@walletconnect/ethereum-provider": "^2.9.1", + "@walletconnect/ethereum-provider": "^2.9.2", "@walletconnect/modal": "^2.6.1", "@web3-react/types": "^8.2.0", "eventemitter3": "^4.0.7" diff --git a/packages/walletconnect-v2/src/index.ts b/packages/walletconnect-v2/src/index.ts index 1b1672c51..c26845a21 100644 --- a/packages/walletconnect-v2/src/index.ts +++ b/packages/walletconnect-v2/src/index.ts @@ -23,6 +23,10 @@ export type WalletConnectOptions = Omit = { + 0: T +} & Array + /** * Options to configure the WalletConnect connector. */ @@ -48,10 +52,9 @@ export class WalletConnect extends Connector { public provider?: WalletConnectProvider public readonly events = new EventEmitter3() - private readonly options: Omit + private readonly options: Omit private readonly rpcMap?: Record - private readonly chains: number[] private readonly defaultChainId?: number private readonly timeout: number @@ -60,10 +63,9 @@ export class WalletConnect extends Connector { constructor({ actions, options, defaultChainId, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) { super(actions, onError) - const { rpcMap, rpc, chains, ...rest } = options + const { rpcMap, rpc, ...rest } = options this.options = rest - this.chains = chains this.defaultChainId = defaultChainId this.rpcMap = rpcMap || rpc this.timeout = timeout @@ -91,12 +93,32 @@ export class WalletConnect extends Connector { ): Promise { if (this.eagerConnection) return this.eagerConnection + function isArrayOneOrMore(input: T[]): input is ArrayOneOrMore { + return input.length > 0 + } + const rpcMap = this.rpcMap ? getBestUrlMap(this.rpcMap, this.timeout) : undefined - const chains = desiredChainId ? getChainsWithDefault(this.chains, desiredChainId) : this.chains + const chains = desiredChainId ? getChainsWithDefault(this.options.chains, desiredChainId) : this.options.chains + + let optionalChains: ArrayOneOrMore = [this.defaultChainId || 1] + + if ( + this.options.optionalChains && + this.options.optionalChains.length > 0 && + isArrayOneOrMore(this.options.optionalChains) + ) { + optionalChains = this.options.optionalChains + } + + console.log('this.options.chains', this.options.chains) + console.log('chains', chains) + console.log('this.options.optionalChains', this.options.optionalChains) + console.log('optionalChains', optionalChains) return (this.eagerConnection = import('@walletconnect/ethereum-provider').then(async (ethProviderModule) => { const provider = (this.provider = await ethProviderModule.default.init({ ...this.options, + optionalChains, chains, rpcMap: await rpcMap, })) diff --git a/packages/walletconnect-v2/src/utils.ts b/packages/walletconnect-v2/src/utils.ts index 5e8963340..f7dee880d 100644 --- a/packages/walletconnect-v2/src/utils.ts +++ b/packages/walletconnect-v2/src/utils.ts @@ -1,3 +1,5 @@ +import { ArrayOneOrMore } from '.' + /** * @param rpcMap - Map of chainIds to rpc url(s). * @param timeout - Timeout, in milliseconds, after which to consider network calls failed. @@ -85,7 +87,10 @@ async function getBestUrl(urls: string | string[], timeout: number): Promise = [], + defaultChainId: number +): ArrayOneOrMore { const idx = chains.indexOf(defaultChainId) if (idx === -1) { throw new Error( diff --git a/yarn.lock b/yarn.lock index cd0f34af2..089695e88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2730,10 +2730,10 @@ "@walletconnect/types" "^1.8.0" "@walletconnect/utils" "^1.8.0" -"@walletconnect/core@2.9.1": - version "2.9.1" - resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.9.1.tgz#1a333933750f5f933d9b7788a8dae44ce1173063" - integrity sha512-xyWeP0eLhEEDQAVJSmqs4n/AClKUM+8os2ZFe7BTuw1tFYjeLNVDtKCHziVOSTh8wEChMsKSGKA4zerQoH8mAQ== +"@walletconnect/core@2.9.2": + version "2.9.2" + resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.9.2.tgz#c46734ca63771b28fd77606fd521930b7ecfc5e1" + integrity sha512-VARMPAx8sIgodeyngDHbealP3B621PQqjqKsByFUTOep8ZI1/R/20zU+cmq6j9RCrL+kLKZcrZqeVzs8Z7OlqQ== dependencies: "@walletconnect/heartbeat" "1.2.1" "@walletconnect/jsonrpc-provider" "1.0.13" @@ -2746,8 +2746,8 @@ "@walletconnect/relay-auth" "^1.0.4" "@walletconnect/safe-json" "^1.0.2" "@walletconnect/time" "^1.0.2" - "@walletconnect/types" "2.9.1" - "@walletconnect/utils" "2.9.1" + "@walletconnect/types" "2.9.2" + "@walletconnect/utils" "2.9.2" events "^3.3.0" lodash.isequal "4.5.0" uint8arrays "^3.1.0" @@ -2803,19 +2803,19 @@ eip1193-provider "1.0.1" eventemitter3 "4.0.7" -"@walletconnect/ethereum-provider@^2.9.1": - version "2.9.1" - resolved "https://registry.yarnpkg.com/@walletconnect/ethereum-provider/-/ethereum-provider-2.9.1.tgz#6f41acb38259a59f383d490396aa9c0933839d0b" - integrity sha512-JiMatBFVgzJSQrckpbOoOsmQmKnbTn9wzmU10MBPe9W6ZV2mf2JuxW0luWKLpQkuCmM9mL6+nCKX7nfW9V6qrQ== +"@walletconnect/ethereum-provider@^2.9.2": + version "2.9.2" + resolved "https://registry.yarnpkg.com/@walletconnect/ethereum-provider/-/ethereum-provider-2.9.2.tgz#fb3a6fca279bb4e98e75baa2fb9730545d41bb99" + integrity sha512-eO1dkhZffV1g7vpG19XUJTw09M/bwGUwwhy1mJ3AOPbOSbMPvwiCuRz2Kbtm1g9B0Jv15Dl+TvJ9vTgYF8zoZg== dependencies: "@walletconnect/jsonrpc-http-connection" "^1.0.7" "@walletconnect/jsonrpc-provider" "^1.0.13" "@walletconnect/jsonrpc-types" "^1.0.3" "@walletconnect/jsonrpc-utils" "^1.0.8" - "@walletconnect/sign-client" "2.9.1" - "@walletconnect/types" "2.9.1" - "@walletconnect/universal-provider" "2.9.1" - "@walletconnect/utils" "2.9.1" + "@walletconnect/sign-client" "2.9.2" + "@walletconnect/types" "2.9.2" + "@walletconnect/universal-provider" "2.9.2" + "@walletconnect/utils" "2.9.2" events "^3.3.0" "@walletconnect/events@^1.0.1": @@ -2991,19 +2991,19 @@ dependencies: tslib "1.14.1" -"@walletconnect/sign-client@2.9.1": - version "2.9.1" - resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.9.1.tgz#e4aa9c7b15849f450fdd1b03754a7517cb5c8811" - integrity sha512-Z7tFRrJ9btA1vU427vsjUS6cPlHQVcTWdKH90khEc2lv3dB6mU8FNO0VJsw+I2D7CW7WaMWF3nnj6Z1FfotbDg== +"@walletconnect/sign-client@2.9.2": + version "2.9.2" + resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.9.2.tgz#ff4c81c082c2078878367d07f24bcb20b1f7ab9e" + integrity sha512-anRwnXKlR08lYllFMEarS01hp1gr6Q9XUgvacr749hoaC/AwGVlxYFdM8+MyYr3ozlA+2i599kjbK/mAebqdXg== dependencies: - "@walletconnect/core" "2.9.1" + "@walletconnect/core" "2.9.2" "@walletconnect/events" "^1.0.1" "@walletconnect/heartbeat" "1.2.1" "@walletconnect/jsonrpc-utils" "1.0.8" "@walletconnect/logger" "^2.0.1" "@walletconnect/time" "^1.0.2" - "@walletconnect/types" "2.9.1" - "@walletconnect/utils" "2.9.1" + "@walletconnect/types" "2.9.2" + "@walletconnect/utils" "2.9.2" events "^3.3.0" "@walletconnect/signer-connection@^1.8.0": @@ -3034,10 +3034,10 @@ dependencies: tslib "1.14.1" -"@walletconnect/types@2.9.1": - version "2.9.1" - resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.9.1.tgz#cb32ff396cc8880a7395f28716d1e82f407e1372" - integrity sha512-xbGgTPuD6xsb7YMvCESBIH55cjB86QAnnVL50a/ED42YkQzDsOdJ0VGTbrm0tG5cxUOF933rpxZQjxGdP+ovww== +"@walletconnect/types@2.9.2": + version "2.9.2" + resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.9.2.tgz#d5fd5a61dc0f41cbdca59d1885b85207ac7bf8c5" + integrity sha512-7Rdn30amnJEEal4hk83cdwHUuxI1SWQ+K7fFFHBMqkuHLGi3tpMY6kpyfDxnUScYEZXqgRps4Jo5qQgnRqVM7A== dependencies: "@walletconnect/events" "^1.0.1" "@walletconnect/heartbeat" "1.2.1" @@ -3051,25 +3051,25 @@ resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-1.8.0.tgz#3f5e85b2d6b149337f727ab8a71b8471d8d9a195" integrity sha512-Cn+3I0V0vT9ghMuzh1KzZvCkiAxTq+1TR2eSqw5E5AVWfmCtECFkVZBP6uUJZ8YjwLqXheI+rnjqPy7sVM4Fyg== -"@walletconnect/universal-provider@2.9.1": - version "2.9.1" - resolved "https://registry.yarnpkg.com/@walletconnect/universal-provider/-/universal-provider-2.9.1.tgz#b9034dfa748f11b692d45b584f202cfcfdd289ea" - integrity sha512-Ychf+/J0Ql3UvaiPVEGtdpYXXDa87e6hP+NUEl/+nF41x3dlH0P1zoIgX5sWbpGP8HRaKd8Qsm0N6S7RalC+LQ== +"@walletconnect/universal-provider@2.9.2": + version "2.9.2" + resolved "https://registry.yarnpkg.com/@walletconnect/universal-provider/-/universal-provider-2.9.2.tgz#40e54e98bc48b1f2f5f77eb5b7f05462093a8506" + integrity sha512-JmaolkO8D31UdRaQCHwlr8uIFUI5BYhBzqYFt54Mc6gbIa1tijGOmdyr6YhhFO70LPmS6gHIjljwOuEllmlrxw== dependencies: "@walletconnect/jsonrpc-http-connection" "^1.0.7" "@walletconnect/jsonrpc-provider" "1.0.13" "@walletconnect/jsonrpc-types" "^1.0.2" "@walletconnect/jsonrpc-utils" "^1.0.7" "@walletconnect/logger" "^2.0.1" - "@walletconnect/sign-client" "2.9.1" - "@walletconnect/types" "2.9.1" - "@walletconnect/utils" "2.9.1" + "@walletconnect/sign-client" "2.9.2" + "@walletconnect/types" "2.9.2" + "@walletconnect/utils" "2.9.2" events "^3.3.0" -"@walletconnect/utils@2.9.1": - version "2.9.1" - resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.9.1.tgz#92abc24b3af3ead42a3864e019dbf2f651ab2e47" - integrity sha512-tXeQVebF5oPBvhdmuUyVSkSIBYx/egIi4czav1QrnUpwrUS1LsrFhyWBxSbhN7TXY287ULWkEf6aFpWOHdp5EA== +"@walletconnect/utils@2.9.2": + version "2.9.2" + resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.9.2.tgz#035bdb859ee81a4bcc6420f56114cc5ec3e30afb" + integrity sha512-D44hwXET/8JhhIjqljY6qxSu7xXnlPrf63UN/Qfl98vDjWlYVcDl2+JIQRxD9GPastw0S8XZXdRq59XDXLuZBg== dependencies: "@stablelib/chacha20poly1305" "1.0.1" "@stablelib/hkdf" "1.0.1" @@ -3079,7 +3079,7 @@ "@walletconnect/relay-api" "^1.0.9" "@walletconnect/safe-json" "^1.0.2" "@walletconnect/time" "^1.0.2" - "@walletconnect/types" "2.9.1" + "@walletconnect/types" "2.9.2" "@walletconnect/window-getters" "^1.0.1" "@walletconnect/window-metadata" "^1.0.1" detect-browser "5.3.0"