1
1
import type {
2
2
EIP1193Provider ,
3
3
ChainListener ,
4
- SimpleEventEmitter
4
+ SimpleEventEmitter ,
5
+ ChainId
5
6
} from '@web3-onboard/common'
6
7
7
8
import { createEIP1193Provider } from '@web3-onboard/common'
8
- import type { InjectedWalletModule , CustomWindow } from './types.js'
9
+ import type {
10
+ InjectedWalletModule ,
11
+ CustomWindow ,
12
+ BinanceProvider
13
+ } from './types.js'
9
14
10
15
import {
11
16
InjectedNameSpace ,
@@ -65,11 +70,11 @@ const binance: InjectedWalletModule = {
65
70
! ! provider && ! ! provider [ ProviderIdentityFlag . Binance ] ,
66
71
getIcon : async ( ) => ( await import ( './icons/binance.js' ) ) . default ,
67
72
getInterface : async ( ) => {
68
- // We add this to the BinanceChain provider as there is currently
69
- // no way to determine if the wallet is unlocked
70
- if ( window . BinanceChain ) {
71
- window . BinanceChain . isUnlocked = false
73
+ // Replace the provider as the BNB provider is readonly
74
+ let tempBNBProvider : BinanceProvider = {
75
+ ...window . BinanceChain
72
76
}
77
+ window . BinanceChain = tempBNBProvider
73
78
74
79
const addListener : SimpleEventEmitter [ 'on' ] = window . BinanceChain . on . bind (
75
80
window . BinanceChain
@@ -78,31 +83,22 @@ const binance: InjectedWalletModule = {
78
83
window . BinanceChain . on = ( event , func ) => {
79
84
// intercept chainChanged event and format string
80
85
if ( event === 'chainChanged' ) {
81
- addListener ( event , ( chainId : string ) => {
86
+ addListener ( event , ( chainId : ChainId ) => {
82
87
const cb = func as ChainListener
83
- cb ( `0x${ parseInt ( chainId ) . toString ( 16 ) } ` )
88
+ cb ( `0x${ parseInt ( chainId as string ) . toString ( 16 ) } ` )
84
89
} )
85
90
} else {
86
91
addListener ( event , func )
87
92
}
88
93
}
89
94
90
95
const provider = createEIP1193Provider ( window . BinanceChain , {
91
- // If the wallet is unlocked then we don't need to patch this request
92
- ...( ! window . BinanceChain . isUnlocked && {
93
- eth_accounts : ( ) => Promise . resolve ( [ ] )
94
- } ) ,
95
- eth_requestAccounts : ( { baseRequest } ) =>
96
- baseRequest ( { method : 'eth_requestAccounts' } ) . then ( accts => {
97
- window . BinanceChain . isUnlocked = true
98
- return accts
99
- } ) ,
100
- eth_selectAccounts : UNSUPPORTED_METHOD ,
101
96
eth_chainId : ( { baseRequest } ) =>
102
- baseRequest ( { method : 'eth_chainId' } ) . then (
103
- id => `0x${ parseInt ( id ) . toString ( 16 ) } `
104
- ) ,
97
+ baseRequest ( { method : 'eth_chainId' } ) . then ( id => {
98
+ return `0x${ parseInt ( id as string ) . toString ( 16 ) } `
99
+ } ) ,
105
100
// Unsupported method -- will throw error
101
+ eth_selectAccounts : UNSUPPORTED_METHOD ,
106
102
wallet_switchEthereumChain : UNSUPPORTED_METHOD
107
103
} )
108
104
0 commit comments