Skip to content

Commit 10a13e8

Browse files
authored
fix: add support for Bitcoin test net in OKX Wallet (#4691)
1 parent fde2340 commit 10a13e8

3 files changed

Lines changed: 68 additions & 9 deletions

File tree

.changeset/warm-planets-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@reown/appkit-adapter-bitcoin': patch
3+
---
4+
5+
Adds support for Bitcoin testnet in OKX wallet

packages/adapters/bitcoin/src/connectors/OKXConnector.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { type CaipNetwork, ConstantsUtil as CommonConstantsUtil } from '@reown/a
33
import { CoreHelperUtil, type RequestArguments } from '@reown/appkit-controllers'
44
import { PresetsUtil } from '@reown/appkit-utils'
55
import type { BitcoinConnector } from '@reown/appkit-utils/bitcoin'
6-
import { bitcoin } from '@reown/appkit/networks'
6+
import { bitcoin, bitcoinTestnet } from '@reown/appkit/networks'
77

88
import { MethodNotSupportedError } from '../errors/MethodNotSupportedError.js'
99
import { AddressPurpose } from '../utils/BitcoinConnector.js'
1010
import { ProviderEventEmitter } from '../utils/ProviderEventEmitter.js'
1111
import { UnitsUtil } from '../utils/UnitsUtil.js'
1212

13+
const OKX_NETWORK_KEYS = {
14+
[bitcoin.caipNetworkId]: 'bitcoin',
15+
[bitcoinTestnet.caipNetworkId]: 'bitcoinTestnet'
16+
} as const
17+
1318
export 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 = {

packages/adapters/bitcoin/tests/connectors/OKXConnector.test.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,33 @@ describe('OKXConnector', () => {
245245
})
246246

247247
describe('switchNetwork', () => {
248-
it('should throw error saying network switching is not supported', async () => {
249-
await expect(
250-
connector.switchNetwork('bip122:000000000019d6689c085ae165831e93')
251-
).rejects.toThrow('OKX Wallet wallet does not support network switching')
248+
it('should switch to testnet network and connect', async () => {
249+
const testnetWallet = mockOKXWallet()
250+
const testnetConnector = new OKXConnector({
251+
wallet: testnetWallet,
252+
requestedChains,
253+
getActiveNetwork,
254+
imageUrl: 'mock_image'
255+
})
256+
257+
vi.spyOn(OKXConnector, 'getWallet').mockReturnValue(testnetConnector)
258+
259+
const accountsChangedListener = vi.fn()
260+
connector.on('accountsChanged', accountsChangedListener)
261+
262+
// Switch to testnet bitcoin
263+
await connector.switchNetwork('bip122:000000000933ea01ad0ee984209779ba')
264+
265+
expect(testnetWallet.connect).toHaveBeenCalled()
266+
expect(accountsChangedListener).toHaveBeenCalledWith(['mock_address'])
267+
})
268+
269+
it('should throw error when wallet is not available', async () => {
270+
vi.spyOn(OKXConnector, 'getWallet').mockReturnValue(undefined)
271+
272+
await expect(connector.switchNetwork('bip122:fake-network')).rejects.toThrow(
273+
'OKX Wallet wallet does not support network switching'
274+
)
252275
})
253276
})
254277
})

0 commit comments

Comments
 (0)