@@ -3,13 +3,18 @@ import { type CaipNetwork, ConstantsUtil as CommonConstantsUtil } from '@reown/a
33import { CoreHelperUtil , type RequestArguments } from '@reown/appkit-controllers'
44import { PresetsUtil } from '@reown/appkit-utils'
55import type { BitcoinConnector } from '@reown/appkit-utils/bitcoin'
6- import { bitcoin } from '@reown/appkit/networks'
6+ import { bitcoin , bitcoinTestnet } from '@reown/appkit/networks'
77
88import { MethodNotSupportedError } from '../errors/MethodNotSupportedError.js'
99import { AddressPurpose } from '../utils/BitcoinConnector.js'
1010import { ProviderEventEmitter } from '../utils/ProviderEventEmitter.js'
1111import { UnitsUtil } from '../utils/UnitsUtil.js'
1212
13+ const OKX_NETWORK_KEYS = {
14+ [ bitcoin . caipNetworkId ] : 'bitcoin' ,
15+ [ bitcoinTestnet . caipNetworkId ] : 'bitcoinTestnet'
16+ } as const
17+
1318export class OKXConnector extends ProviderEventEmitter implements BitcoinConnector {
1419 public readonly id = 'OKX'
1520 public readonly name = 'OKX Wallet'
@@ -21,7 +26,7 @@ export class OKXConnector extends ProviderEventEmitter implements BitcoinConnect
2126
2227 public readonly provider = this
2328
24- private readonly wallet : OKXConnector . Wallet
29+ private wallet : OKXConnector . Wallet
2530 private readonly requestedChains : CaipNetwork [ ] = [ ]
2631 private readonly getActiveNetwork : ( ) => CaipNetwork | undefined
2732
@@ -116,8 +121,25 @@ export class OKXConnector extends ProviderEventEmitter implements BitcoinConnect
116121 }
117122 }
118123
119- public async switchNetwork ( _caipNetworkId : string ) : Promise < void > {
120- throw new Error ( `${ this . name } wallet does not support network switching` )
124+ public async switchNetwork ( _caipNetworkId : CaipNetwork [ 'caipNetworkId' ] ) : Promise < void > {
125+ const connector = OKXConnector . getWallet ( {
126+ requestedChains : this . requestedChains ,
127+ getActiveNetwork : this . getActiveNetwork ,
128+ requestedCaipNetworkId : _caipNetworkId
129+ } )
130+
131+ if ( ! connector ) {
132+ throw new Error ( `${ this . name } wallet does not support network switching` )
133+ }
134+
135+ this . unbindEvents ( )
136+ this . wallet = connector . wallet
137+
138+ try {
139+ await this . connect ( )
140+ } catch ( error ) {
141+ throw new Error ( `${ this . name } wallet does not support network switching` )
142+ }
121143 }
122144
123145 public request < T > ( _args : RequestArguments ) : Promise < T > {
@@ -146,9 +168,17 @@ export class OKXConnector extends ProviderEventEmitter implements BitcoinConnect
146168 return undefined
147169 }
148170
171+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
172+ let wallet : any = undefined
173+
149174 // eslint-disable-next-line @typescript-eslint/no-explicit-any
150175 const okxwallet = ( window as any ) ?. okxwallet
151- const wallet = okxwallet ?. bitcoin
176+
177+ const networkKey =
178+ OKX_NETWORK_KEYS [ params . requestedCaipNetworkId as keyof typeof OKX_NETWORK_KEYS ]
179+
180+ wallet = okxwallet ?. [ networkKey ] || okxwallet ?. bitcoin
181+
152182 /**
153183 * OKX doesn't provide a way to get the image URL specifally for bitcoin
154184 * so we use the icon for cardano as a fallback
@@ -173,6 +203,7 @@ export namespace OKXConnector {
173203 requestedChains : CaipNetwork [ ]
174204 getActiveNetwork : ( ) => CaipNetwork | undefined
175205 imageUrl : string
206+ requestedCaipNetworkId ?: CaipNetwork [ 'caipNetworkId' ]
176207 }
177208
178209 export type Wallet = {
0 commit comments