@meshconnect/uwc-injected-connector 0.22.1 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/injected-connector.d.ts +23 -7
- package/dist/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +54 -9
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/bitcoin/bitcoin-wallet-service.d.ts +136 -0
- package/dist/services/bitcoin/bitcoin-wallet-service.d.ts.map +1 -0
- package/dist/services/bitcoin/bitcoin-wallet-service.js +385 -0
- package/dist/services/bitcoin/bitcoin-wallet-service.js.map +1 -0
- package/dist/services/connection-manager.d.ts +17 -2
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +129 -3
- package/dist/services/connection-manager.js.map +1 -1
- package/dist/wallet-standard-discovery.d.ts +45 -1
- package/dist/wallet-standard-discovery.d.ts.map +1 -1
- package/dist/wallet-standard-discovery.js +221 -1
- package/dist/wallet-standard-discovery.js.map +1 -1
- package/package.json +13 -12
- package/src/injected-connector.test.ts +1 -1
- package/src/injected-connector.ts +78 -8
- package/src/services/bitcoin/bitcoin-wallet-service.test.ts +646 -0
- package/src/services/bitcoin/bitcoin-wallet-service.ts +493 -0
- package/src/services/connection-manager.test.ts +214 -5
- package/src/services/connection-manager.ts +169 -4
- package/src/wallet-standard-discovery.test.ts +215 -1
- package/src/wallet-standard-discovery.ts +325 -2
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AvailableAddress,
|
|
3
|
+
BitcoinAccount,
|
|
4
|
+
BitcoinConfig,
|
|
5
|
+
BitcoinTransactionRequest,
|
|
6
|
+
ConnectorResult,
|
|
7
|
+
ExtensionInjectedProvider,
|
|
8
|
+
IntegratedBrowserInjectedProvider,
|
|
9
|
+
Network,
|
|
10
|
+
NetworkId,
|
|
11
|
+
SignatureType,
|
|
12
|
+
TransactionResult,
|
|
13
|
+
WalletMetadata
|
|
14
|
+
} from '@meshconnect/uwc-types'
|
|
15
|
+
import {
|
|
16
|
+
BitcoinNamespace,
|
|
17
|
+
WalletStandardBitcoinAdapter,
|
|
18
|
+
OkxBitcoinAdapter,
|
|
19
|
+
bytesToHex,
|
|
20
|
+
type WalletStandardBitcoinWallet,
|
|
21
|
+
type OkxBitcoinProvider
|
|
22
|
+
} from '@meshconnect/uwc-bitcoin'
|
|
23
|
+
import { bitcoinNetwork } from '@meshconnect/uwc-constants'
|
|
24
|
+
import {
|
|
25
|
+
getBitcoinWallets,
|
|
26
|
+
type DetectedBitcoinWallet
|
|
27
|
+
} from '../../wallet-standard-discovery'
|
|
28
|
+
import { resolveInjectedProperty } from '../../legacy-injected-solana-adapter'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Minimal structural shape of a wallet-standard wallet's `standard:events`
|
|
32
|
+
* change stream — just enough to observe live account switches, mirroring
|
|
33
|
+
* `BridgeParent.ts`'s identically-named local type. Duck-typed here rather
|
|
34
|
+
* than imported from `@wallet-standard/features`, matching this codebase's
|
|
35
|
+
* existing convention of not adding a new dependency for a few fields.
|
|
36
|
+
*/
|
|
37
|
+
interface StandardEventsCapableWallet {
|
|
38
|
+
accounts?: readonly { address: string }[]
|
|
39
|
+
features?: {
|
|
40
|
+
'standard:events'?: {
|
|
41
|
+
on?: (
|
|
42
|
+
event: 'change',
|
|
43
|
+
listener: (properties: { accounts?: readonly unknown[] }) => void
|
|
44
|
+
) => () => void
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Bitcoin (bip122) wallet service for the injected transport — the peer of
|
|
51
|
+
* `EthereumWalletService` / `SolanaWalletService` / `TronWalletService` /
|
|
52
|
+
* `TonWalletService`. Owns the namespace-specific work (Wallet Standard
|
|
53
|
+
* `bitcoin:*` discovery, adapter construction, and the connected session
|
|
54
|
+
* state) so `InjectedConnector`/`ConnectionManager` only delegate, keeping
|
|
55
|
+
* chain logic out of the shared classes. Templated on `TonWalletService`,
|
|
56
|
+
* the closest analogue — it also owns its own connect/signMessage/
|
|
57
|
+
* sendTransaction, yet still implements the full shared service shape.
|
|
58
|
+
*
|
|
59
|
+
* Broadcast is not wired yet (sign-only): a PSBT send returns the wallet-signed
|
|
60
|
+
* PSBT (base64), not a txid.
|
|
61
|
+
*/
|
|
62
|
+
export class BitcoinWalletService {
|
|
63
|
+
private detectedWallets: DetectedBitcoinWallet[] = []
|
|
64
|
+
private namespace: BitcoinNamespace | null = null
|
|
65
|
+
private account: BitcoinAccount | null = null
|
|
66
|
+
/** uuid of the currently-connected wallet — lets `ConnectionManager` detect a
|
|
67
|
+
* wallet switch (release-other-wallet-connections guard), same as the
|
|
68
|
+
* eip155/solana/tron identity checks. */
|
|
69
|
+
private connectedWalletUuid: string | null = null
|
|
70
|
+
/**
|
|
71
|
+
* The connected wallet handle itself (not just the adapter wrapping it) —
|
|
72
|
+
* kept so `getLiveAccount()`/`onAccountsChanged()` can read/observe its
|
|
73
|
+
* live `accounts` and `standard:events` directly. Bitcoin has no adapter
|
|
74
|
+
* class re-emitting connect/disconnect on a switch (unlike Solana's
|
|
75
|
+
* `StandardWalletAdapter`), so both live reads and subscriptions must go
|
|
76
|
+
* straight to the wallet, not through `this.namespace`.
|
|
77
|
+
*/
|
|
78
|
+
private connectedWallet: WalletStandardBitcoinWallet | null = null
|
|
79
|
+
/**
|
|
80
|
+
* The connected OKX (legacy-injected) provider, when the active session is on
|
|
81
|
+
* OKX rather than a Wallet Standard wallet. OKX isn't Wallet Standard, so it
|
|
82
|
+
* has no `accounts`/`standard:events` on `connectedWallet` — the live-account
|
|
83
|
+
* read and account-change subscription go through this provider instead.
|
|
84
|
+
*/
|
|
85
|
+
private connectedOkxProvider: OkxBitcoinProvider | null = null
|
|
86
|
+
|
|
87
|
+
constructor(private readonly config?: BitcoinConfig) {}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Initialize wallet discovery from the Wallet Standard `bitcoin:*` registry,
|
|
91
|
+
* honoring `BitcoinConfig.enabledWallets` when configured.
|
|
92
|
+
*/
|
|
93
|
+
async initializeDiscovery(
|
|
94
|
+
expectedWallets?: WalletMetadata[],
|
|
95
|
+
usingIntegratedBrowser = false
|
|
96
|
+
): Promise<void> {
|
|
97
|
+
this.detectedWallets = await getBitcoinWallets(
|
|
98
|
+
expectedWallets,
|
|
99
|
+
usingIntegratedBrowser,
|
|
100
|
+
this.config?.enabledWallets
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
getDetectedWallets(): DetectedBitcoinWallet[] {
|
|
105
|
+
return this.detectedWallets
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
findWalletByUuid(uuid: string): DetectedBitcoinWallet | undefined {
|
|
109
|
+
return this.detectedWallets.find(w => w.uuid === uuid)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Check if a Bitcoin wallet is already authorized (non-prompting). Reads
|
|
114
|
+
* the Wallet Standard wallet's already-exposed `accounts` directly — never
|
|
115
|
+
* calls `bitcoin:connect`, which pops a wallet dialog.
|
|
116
|
+
*/
|
|
117
|
+
async checkExistingConnection(
|
|
118
|
+
detectedWallet: DetectedBitcoinWallet
|
|
119
|
+
): Promise<string | null> {
|
|
120
|
+
const wallet =
|
|
121
|
+
detectedWallet.wallet as unknown as WalletStandardBitcoinWallet
|
|
122
|
+
const account = wallet.accounts?.[0]
|
|
123
|
+
return account?.address ?? null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async connect(
|
|
127
|
+
network: Network,
|
|
128
|
+
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider
|
|
129
|
+
): Promise<ConnectorResult> {
|
|
130
|
+
// Resolve the SPECIFIC wallet the session matched by uuid (mirrors every
|
|
131
|
+
// other service) — fails closed rather than picking an arbitrary wallet.
|
|
132
|
+
const uuid = provider.namespaceMetaData?.bip122?.detectedWallet?.uuid
|
|
133
|
+
const detected = uuid ? this.findWalletByUuid(uuid) : undefined
|
|
134
|
+
if (!detected) {
|
|
135
|
+
// Covers both kinds — Wallet Standard `bitcoin:*` and OKX legacy-injected.
|
|
136
|
+
throw new Error('No bitcoin wallet found for this connection')
|
|
137
|
+
}
|
|
138
|
+
// Wallet Standard vs OKX (legacy-injected) — build the matching adapter.
|
|
139
|
+
const { namespace, okxProvider } = this.buildNamespace(detected)
|
|
140
|
+
namespace.assertSupportedNetwork(network)
|
|
141
|
+
|
|
142
|
+
const accounts = await namespace.getAccounts()
|
|
143
|
+
const primary = accounts[0]
|
|
144
|
+
if (!primary) {
|
|
145
|
+
throw new Error('Bitcoin wallet returned no accounts')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Commit session state only now that the (prompting) connect has succeeded.
|
|
149
|
+
this.namespace = namespace
|
|
150
|
+
this.account = primary
|
|
151
|
+
this.connectedWalletUuid = uuid ?? null
|
|
152
|
+
// OKX has no Wallet Standard handle to observe — its live-account reads go
|
|
153
|
+
// through `connectedOkxProvider`, so leave `connectedWallet` null for it.
|
|
154
|
+
this.connectedWallet = detected.legacyInjectedId
|
|
155
|
+
? null
|
|
156
|
+
: (detected.wallet as unknown as WalletStandardBitcoinWallet)
|
|
157
|
+
this.connectedOkxProvider = okxProvider
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
networkId: network.id,
|
|
161
|
+
address: primary.address,
|
|
162
|
+
availableAddresses: accounts.map(account => ({
|
|
163
|
+
address: account.address,
|
|
164
|
+
networkId: network.id
|
|
165
|
+
})),
|
|
166
|
+
...(primary.publicKey ? { publicKey: primary.publicKey } : {})
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async signMessage(message: string): Promise<SignatureType> {
|
|
171
|
+
if (!this.namespace || !this.account) {
|
|
172
|
+
throw new Error('No connected Bitcoin wallet')
|
|
173
|
+
}
|
|
174
|
+
// Refresh `this.account` to the wallet's LIVE active account before signing
|
|
175
|
+
// so `getAccount()` and the returned signer address stay consistent after
|
|
176
|
+
// an in-wallet account switch (otherwise they diverge from
|
|
177
|
+
// `getLiveAccount()`).
|
|
178
|
+
await this.refreshLiveAccount()
|
|
179
|
+
return this.namespace.signMessage({ account: this.account, message })
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async sendTransaction(
|
|
183
|
+
request: BitcoinTransactionRequest
|
|
184
|
+
): Promise<TransactionResult> {
|
|
185
|
+
if (!this.namespace) {
|
|
186
|
+
throw new Error('No connected Bitcoin wallet')
|
|
187
|
+
}
|
|
188
|
+
// Broadcast is not wired yet: a PSBT request returns the wallet-signed PSBT
|
|
189
|
+
// (base64), NOT a txid. A high-level transfer is a WalletConnect-only path.
|
|
190
|
+
if ('psbt' in request) {
|
|
191
|
+
return this.namespace.signPsbt(request)
|
|
192
|
+
}
|
|
193
|
+
return this.namespace.sendTransfer(request)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Build available addresses for the supported Bitcoin network. Restricted to
|
|
198
|
+
* Bitcoin MAINNET (`bitcoinNetwork.id`), not any `bip122:` id — this connector
|
|
199
|
+
* only supports Bitcoin (see `assertBitcoinNetwork`), so stamping the address
|
|
200
|
+
* onto other bip122 chains (Dogecoin/Litecoin) would misrepresent it as a
|
|
201
|
+
* valid address there (a fund-loss risk if a consumer renders it as e.g. a
|
|
202
|
+
* DOGE deposit address).
|
|
203
|
+
*/
|
|
204
|
+
buildAvailableAddresses(
|
|
205
|
+
supportedNetworkIds: string[],
|
|
206
|
+
address: string
|
|
207
|
+
): AvailableAddress[] {
|
|
208
|
+
return supportedNetworkIds
|
|
209
|
+
.filter(id => id === bitcoinNetwork.id)
|
|
210
|
+
.map(networkId => ({ address, networkId: networkId as NetworkId }))
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
getAccount(): string | null {
|
|
214
|
+
return this.account?.address ?? null
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The LIVE active address, read at call time from the connected wallet's
|
|
219
|
+
* own `accounts` list — never a cache. Unlike Solana's adapter.publicKey
|
|
220
|
+
* pull (which awaits a per-call Comlink read), Bitcoin has nothing to
|
|
221
|
+
* await: the Wallet Standard wallet keeps `accounts` current in place, the
|
|
222
|
+
* same property `checkExistingConnection` already reads without prompting.
|
|
223
|
+
* Returns null when there's no connected wallet or it exposes no accounts;
|
|
224
|
+
* the caller decides whether to throw.
|
|
225
|
+
*/
|
|
226
|
+
async getLiveAccount(): Promise<string | null> {
|
|
227
|
+
if (this.connectedOkxProvider) {
|
|
228
|
+
// OKX exposes its live accounts via a (non-prompting) provider call.
|
|
229
|
+
try {
|
|
230
|
+
const addresses = await this.connectedOkxProvider.getAccounts()
|
|
231
|
+
return addresses?.[0] ?? this.account?.address ?? null
|
|
232
|
+
} catch {
|
|
233
|
+
return this.account?.address ?? null
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return this.connectedWallet?.accounts?.[0]?.address ?? null
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Refresh `this.account` to the wallet's live active account. Wallet Standard
|
|
241
|
+
* keeps `connectedWallet.accounts` current in place; OKX exposes it via a
|
|
242
|
+
* (non-prompting) provider call — for OKX only the address is available, so
|
|
243
|
+
* the prior public key is retained (OKX signs with its own active account and
|
|
244
|
+
* ignores the passed one). Best-effort: keeps the cached account on failure.
|
|
245
|
+
*/
|
|
246
|
+
private async refreshLiveAccount(): Promise<void> {
|
|
247
|
+
if (!this.account) return
|
|
248
|
+
if (this.connectedOkxProvider) {
|
|
249
|
+
try {
|
|
250
|
+
const [address] = await this.connectedOkxProvider.getAccounts()
|
|
251
|
+
if (address) this.account = { ...this.account, address }
|
|
252
|
+
} catch {
|
|
253
|
+
// keep the cached account
|
|
254
|
+
}
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
const live = this.connectedWallet?.accounts?.[0]
|
|
258
|
+
if (live) {
|
|
259
|
+
this.account = {
|
|
260
|
+
address: live.address,
|
|
261
|
+
publicKey: bytesToHex(live.publicKey),
|
|
262
|
+
...(live.purpose ? { purpose: live.purpose } : {})
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Subscribe to the connected Bitcoin wallet's account changes. Bitcoin has
|
|
269
|
+
* no adapter-level `connect`/`disconnect` re-emission to lean on the way
|
|
270
|
+
* Solana's `StandardWalletAdapter` does — `standard:events` `change` is the
|
|
271
|
+
* ONLY account-change signal a bitcoin:* wallet exposes, so BOTH the local
|
|
272
|
+
* (direct) and bridged paths read it, unlike Solana where only the bridge
|
|
273
|
+
* side needs to.
|
|
274
|
+
*
|
|
275
|
+
* Bridge guard: in the production iframe the wallet published to this
|
|
276
|
+
* service is the Comlink-marshaled `Bip122WireWallet` shape (name, accounts,
|
|
277
|
+
* three feature methods only — no `standard:events`), so a direct
|
|
278
|
+
* subscription is impossible here. Instead, relay through
|
|
279
|
+
* `window.__uwcSubscribeBip122WalletEvent` (installed by
|
|
280
|
+
* `@meshconnect/uwc-bridge-child`, only present in bridge mode), which asks
|
|
281
|
+
* the PARENT frame — where the live wallet actually lives — to subscribe on
|
|
282
|
+
* our behalf. Falls back to `undefined` (polling remains the fallback) when
|
|
283
|
+
* the global or a connected wallet id isn't available, e.g. an older
|
|
284
|
+
* bridge-parent that predates this capability.
|
|
285
|
+
*/
|
|
286
|
+
onAccountsChanged(
|
|
287
|
+
listener: (accounts: string[]) => void
|
|
288
|
+
): (() => void) | undefined {
|
|
289
|
+
// OKX (legacy-injected) is always in-frame — subscribe to its own
|
|
290
|
+
// `accountsChanged` event directly, never the bridge. The provider emits
|
|
291
|
+
// `accountsChanged` with the new address list.
|
|
292
|
+
if (this.connectedOkxProvider) {
|
|
293
|
+
const provider = this.connectedOkxProvider
|
|
294
|
+
const handler = (accounts: string[]) =>
|
|
295
|
+
listener(Array.isArray(accounts) ? accounts : [])
|
|
296
|
+
provider.on('accountsChanged', handler)
|
|
297
|
+
return () => {
|
|
298
|
+
try {
|
|
299
|
+
provider.removeListener('accountsChanged', handler)
|
|
300
|
+
} catch {
|
|
301
|
+
// Unsubscribe must never throw.
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (
|
|
307
|
+
typeof window !== 'undefined' &&
|
|
308
|
+
(window as unknown as { UWCBridgeChildInitialized?: boolean })
|
|
309
|
+
.UWCBridgeChildInitialized === true
|
|
310
|
+
) {
|
|
311
|
+
return this.subscribeAccountsChangedViaBridge(listener)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const wallet = this
|
|
315
|
+
.connectedWallet as unknown as StandardEventsCapableWallet | null
|
|
316
|
+
const standardEvents = wallet?.features?.['standard:events']
|
|
317
|
+
if (!wallet || typeof standardEvents?.on !== 'function') return undefined
|
|
318
|
+
|
|
319
|
+
const off = standardEvents.on('change', properties => {
|
|
320
|
+
// React only to account-set changes; ignore pure chain/feature deltas.
|
|
321
|
+
if (!properties || !('accounts' in properties)) return
|
|
322
|
+
const address = wallet.accounts?.[0]?.address ?? null
|
|
323
|
+
listener(address ? [address] : [])
|
|
324
|
+
})
|
|
325
|
+
return () => {
|
|
326
|
+
try {
|
|
327
|
+
off?.()
|
|
328
|
+
} catch {
|
|
329
|
+
// Unsubscribe must never throw.
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Bridge half of `onAccountsChanged`. `walletId` is `connectedWalletUuid`
|
|
336
|
+
* — the SAME id `BridgeParent`'s `bip122Wallets[].uuid` publishes (see
|
|
337
|
+
* `generateBip122WalletId` in `BridgeParent.ts`), which this service
|
|
338
|
+
* captured at connect time. The bridge global forwards a plain
|
|
339
|
+
* `string | null` address (never the raw account object); we map it to the
|
|
340
|
+
* `[address]` / `[]` shape `onAccountsChanged`'s direct-mode branch already
|
|
341
|
+
* uses (core reads `[]` as a namespace disconnect).
|
|
342
|
+
*/
|
|
343
|
+
private subscribeAccountsChangedViaBridge(
|
|
344
|
+
listener: (accounts: string[]) => void
|
|
345
|
+
): (() => void) | undefined {
|
|
346
|
+
const walletId = this.connectedWalletUuid
|
|
347
|
+
const bridgeSubscribe = (
|
|
348
|
+
window as unknown as {
|
|
349
|
+
__uwcSubscribeBip122WalletEvent?: (
|
|
350
|
+
walletId: string,
|
|
351
|
+
callback: (address: string | null) => void
|
|
352
|
+
) => () => void
|
|
353
|
+
}
|
|
354
|
+
).__uwcSubscribeBip122WalletEvent
|
|
355
|
+
if (!walletId || !bridgeSubscribe) return undefined
|
|
356
|
+
|
|
357
|
+
return bridgeSubscribe(walletId, address => {
|
|
358
|
+
listener(address ? [address] : [])
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
isConnected(): boolean {
|
|
363
|
+
return this.account !== null
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
getConnectedWalletUuid(): string | null {
|
|
367
|
+
return this.connectedWalletUuid
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Set connection state directly — used by the silent existing-connection
|
|
372
|
+
* restore path (`ConnectionManager.checkExistingConnections`). Re-derives
|
|
373
|
+
* the full `BitcoinAccount` (with hex `publicKey`) from the wallet's own
|
|
374
|
+
* `accounts` list so a subsequent `signMessage`/`sendTransaction` has what
|
|
375
|
+
* it needs, without re-prompting.
|
|
376
|
+
*/
|
|
377
|
+
setConnectionState(
|
|
378
|
+
detectedWallet: DetectedBitcoinWallet | null,
|
|
379
|
+
address: string | null
|
|
380
|
+
): void {
|
|
381
|
+
if (!detectedWallet || !address) {
|
|
382
|
+
this.namespace = null
|
|
383
|
+
this.account = null
|
|
384
|
+
this.connectedWalletUuid = null
|
|
385
|
+
this.connectedWallet = null
|
|
386
|
+
this.connectedOkxProvider = null
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const wallet =
|
|
391
|
+
detectedWallet.wallet as unknown as WalletStandardBitcoinWallet
|
|
392
|
+
const matched = wallet.accounts?.find(a => a.address === address)
|
|
393
|
+
|
|
394
|
+
if (detectedWallet.legacyInjectedId) {
|
|
395
|
+
// OKX restore: rebuild the OKX adapter; the handle's accounts were seeded
|
|
396
|
+
// (non-prompting) at discovery, so `matched` carries the public key.
|
|
397
|
+
const provider = resolveInjectedProperty(
|
|
398
|
+
detectedWallet.legacyInjectedId
|
|
399
|
+
) as OkxBitcoinProvider | null
|
|
400
|
+
if (!provider || !matched) {
|
|
401
|
+
this.namespace = null
|
|
402
|
+
this.account = null
|
|
403
|
+
this.connectedWalletUuid = null
|
|
404
|
+
this.connectedWallet = null
|
|
405
|
+
this.connectedOkxProvider = null
|
|
406
|
+
return
|
|
407
|
+
}
|
|
408
|
+
this.namespace = new BitcoinNamespace(new OkxBitcoinAdapter(provider))
|
|
409
|
+
this.connectedOkxProvider = provider
|
|
410
|
+
this.connectedWallet = null
|
|
411
|
+
this.account = {
|
|
412
|
+
address: matched.address,
|
|
413
|
+
publicKey: bytesToHex(matched.publicKey),
|
|
414
|
+
...(matched.purpose ? { purpose: matched.purpose } : {})
|
|
415
|
+
}
|
|
416
|
+
this.connectedWalletUuid = detectedWallet.uuid
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (!matched) {
|
|
421
|
+
// The address to restore isn't among the wallet's exposed accounts —
|
|
422
|
+
// fail closed rather than fabricate a `{ publicKey: '' }` account that
|
|
423
|
+
// could flow into a later sign. (Not reachable via the current caller,
|
|
424
|
+
// which passes `accounts[0].address`, but keeps the restore honest.)
|
|
425
|
+
this.namespace = null
|
|
426
|
+
this.account = null
|
|
427
|
+
this.connectedWalletUuid = null
|
|
428
|
+
this.connectedWallet = null
|
|
429
|
+
return
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Seed the adapter with the wallet's already-authorized accounts so the
|
|
433
|
+
// first sign after a silent restore doesn't re-invoke `bitcoin:connect`.
|
|
434
|
+
this.namespace = new BitcoinNamespace(
|
|
435
|
+
new WalletStandardBitcoinAdapter(wallet, wallet.accounts)
|
|
436
|
+
)
|
|
437
|
+
this.connectedOkxProvider = null
|
|
438
|
+
this.account = {
|
|
439
|
+
address: matched.address,
|
|
440
|
+
publicKey: bytesToHex(matched.publicKey),
|
|
441
|
+
...(matched.purpose ? { purpose: matched.purpose } : {})
|
|
442
|
+
}
|
|
443
|
+
this.connectedWalletUuid = detectedWallet.uuid
|
|
444
|
+
this.connectedWallet = wallet
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
async disconnect(): Promise<void> {
|
|
448
|
+
if (this.namespace) {
|
|
449
|
+
await this.namespace.disconnect()
|
|
450
|
+
}
|
|
451
|
+
this.namespace = null
|
|
452
|
+
this.account = null
|
|
453
|
+
this.connectedWalletUuid = null
|
|
454
|
+
this.connectedWallet = null
|
|
455
|
+
this.connectedOkxProvider = null
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Build the namespace for a detected wallet, selecting the adapter by its
|
|
460
|
+
* kind: OKX legacy-injected (`legacyInjectedId` set) → `OkxBitcoinAdapter`
|
|
461
|
+
* over the resolved `window.*` provider; otherwise the Wallet Standard
|
|
462
|
+
* adapter. Sets `connectedOkxProvider` as a side effect so the live-account
|
|
463
|
+
* reads know which path is active.
|
|
464
|
+
*/
|
|
465
|
+
private buildNamespace(detected: DetectedBitcoinWallet): {
|
|
466
|
+
namespace: BitcoinNamespace
|
|
467
|
+
okxProvider: OkxBitcoinProvider | null
|
|
468
|
+
} {
|
|
469
|
+
if (detected.legacyInjectedId) {
|
|
470
|
+
const provider = resolveInjectedProperty(
|
|
471
|
+
detected.legacyInjectedId
|
|
472
|
+
) as OkxBitcoinProvider | null
|
|
473
|
+
if (!provider) {
|
|
474
|
+
throw new Error(
|
|
475
|
+
`OKX bitcoin provider not found at "${detected.legacyInjectedId}"`
|
|
476
|
+
)
|
|
477
|
+
}
|
|
478
|
+
// Return the provider rather than assigning `this.connectedOkxProvider`
|
|
479
|
+
// here: `connect()` commits it only AFTER assertSupportedNetwork /
|
|
480
|
+
// getAccounts succeed, so a rejected prompt can't leave it set on an
|
|
481
|
+
// otherwise-unconnected service.
|
|
482
|
+
return {
|
|
483
|
+
namespace: new BitcoinNamespace(new OkxBitcoinAdapter(provider)),
|
|
484
|
+
okxProvider: provider
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const wallet = detected.wallet as unknown as WalletStandardBitcoinWallet
|
|
488
|
+
return {
|
|
489
|
+
namespace: new BitcoinNamespace(new WalletStandardBitcoinAdapter(wallet)),
|
|
490
|
+
okxProvider: null
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|