@@ -3,37 +3,67 @@ import { isMobile, isTMA } from '@/core/base/utils/mobile';
33import { binanceWalletConfig } from '@/core/configs/binanceWallet' ;
44import { EvmWallet } from '../types' ;
55import { getEvmInjectedProvider } from '../../utils/getEvmInjectedProvider' ;
6- import { sleep } from 'tronweb/ utils' ;
6+ import { sleep } from '@/core/ utils/common ' ;
77import { injected } from '../injected' ;
88
99export interface BinanceWalletOptions extends Partial < EvmWallet > {
1010 connectorOptions ?: BinanceW3WParameters ;
1111}
1212
13- export function binanceWallet ( props : BinanceWalletOptions = { } ) : EvmWallet {
14- const { connectorOptions = { } , ...restProps } = props ;
13+ /**
14+ * Detect if running inside the Binance App in-app browser.
15+ * The Binance App sets window.isBinance = true.
16+ */
17+ function isInBinanceApp ( ) : boolean {
18+ if ( typeof window === 'undefined' ) return false ;
19+ return Boolean ( window . isBinance ) ;
20+ }
1521
16- const getProvider = ( ) => {
17- if ( typeof window === 'undefined' ) return ;
18- return getEvmInjectedProvider ( 'isBinance' ) ;
19- } ;
22+ /**
23+ * Detect if the Binance Web3 Wallet browser extension is installed.
24+ * The extension injects window.binancew3w.
25+ */
26+ function isBinanceExtensionInstalled ( ) : boolean {
27+ if ( typeof window === 'undefined' ) return false ;
28+ return Boolean ( window . binancew3w ) ;
29+ }
2030
21- const isInstalled = ( ) => {
22- return ! ! getProvider ( ) ;
23- } ;
31+ /**
32+ * Get the Binance EVM provider.
33+ * Priority: window.ethereum.isBinance > window.binancew3w.ethereum
34+ */
35+ function getBinanceProvider ( ) : any {
36+ if ( typeof window === 'undefined' ) return undefined ;
37+ // Standard EIP-1193: injected by Binance App or extension via window.ethereum
38+ const injectedProvider = getEvmInjectedProvider ( 'isBinance' ) ;
39+ if ( injectedProvider ) return injectedProvider ;
40+ // Fallback: standalone provider from extension
41+ return window . binancew3w ?. ethereum ;
42+ }
43+
44+ /**
45+ * Returns true if any Binance wallet source is available.
46+ */
47+ function isBinanceInstalled ( ) : boolean {
48+ return isInBinanceApp ( ) || isBinanceExtensionInstalled ( ) || Boolean ( getBinanceProvider ( ) ) ;
49+ }
50+
51+ export function binanceWallet ( props : BinanceWalletOptions = { } ) : EvmWallet {
52+ const { connectorOptions = { } , ...restProps } = props ;
2453
2554 return {
2655 ...binanceWalletConfig ,
2756 id : 'binanceWeb3Wallet' ,
2857 walletType : 'evm' ,
2958 behaviors : [
59+ // Behavior 1: Telegram Mini App - always use SDK connector
3060 {
31- platforms : [ 'tg-android' , 'tg-ios' , 'tg-pc' , 'browser-pc' ] ,
32- connectType : 'sdk' ,
61+ platforms : [ 'tg-android' , 'tg-ios' , 'tg-pc' ] ,
62+ connectType : 'sdk' as const ,
3363 getCreateConnectorFn ( ) {
64+ // In TMA, intercept bnc:// deep links and convert to HTTPS download links
3465 if ( typeof window !== 'undefined' && isMobile ( ) && isTMA ( ) ) {
3566 const originalAppendChild = document . body . appendChild ;
36-
3767 document . body . appendChild = function ( node , ...params ) {
3868 if ( node instanceof HTMLAnchorElement && node . href ?. startsWith ( 'bnc://' ) ) {
3969 node . href = `https://app.binance.com/en/download?_dp=${ window . btoa ( node . href ) } ` ;
@@ -44,42 +74,67 @@ export function binanceWallet(props: BinanceWalletOptions = {}): EvmWallet {
4474 }
4575
4676 const connector = getWagmiConnectorV2 ( ) ;
47- return connector ( {
48- ...connectorOptions ,
49- } ) ;
77+ return ( connector as any ) ( { ...connectorOptions } ) ;
78+ } ,
79+ } ,
80+
81+ // Behavior 2: Desktop browser - smart connector selection
82+ // Binance App or extension installed -> injected (instant, like MetaMask)
83+ // No extension -> SDK (QR/deeplink modal)
84+ {
85+ platforms : [ 'browser-pc' ] ,
86+ get connectType ( ) {
87+ return isBinanceInstalled ( ) ? ( 'default' as const ) : ( 'sdk' as const ) ;
88+ } ,
89+ isInstalled : isBinanceInstalled ,
90+ getCreateConnectorFn ( ) {
91+ if ( isBinanceInstalled ( ) ) {
92+ return injected ( {
93+ shimDisconnect : true ,
94+ target : {
95+ id : 'binanceWeb3Wallet' ,
96+ name : 'Binance Wallet' ,
97+ async provider ( ) {
98+ return getBinanceProvider ( ) ;
99+ } ,
100+ } ,
101+ ...connectorOptions ,
102+ } ) ;
103+ }
104+ // No extension: SDK connector opens QR/deeplink modal
105+ const connector = getWagmiConnectorV2 ( ) ;
106+ return ( connector as any ) ( { ...connectorOptions } ) ;
50107 } ,
51108 } ,
109+
110+ // Behavior 3: Mobile browser - injected + deep link fallback
52111 {
53112 platforms : [ 'browser-android' , 'browser-ios' ] ,
54- connectType : 'default' ,
55- isInstalled,
113+ connectType : 'default' as const ,
114+ isInstalled : isBinanceInstalled ,
56115 getAppLink ( ) {
57116 const url = window . location . href ;
58117 const base = 'bnc://app.binance.com/mp/app' ;
59118 const appId = 'yFK5FCqYprrXDiVFbhyRx7' ;
60-
61119 const startPagePath = window . btoa ( '/pages/browser/index' ) ;
62120 const startPageQuery = window . btoa ( `url=${ url } ` ) ;
63121 const deeplink = `${ base } ?appId=${ appId } &startPagePath=${ startPagePath } &startPageQuery=${ startPageQuery } ` ;
64122 const dp = window . btoa ( deeplink ) ;
65- const http = `https://app.binance.com/en/download?_dp=${ dp } ` ;
66-
67- return http ;
123+ return `https://app.binance.com/en/download?_dp=${ dp } ` ;
68124 } ,
69125 getCreateConnectorFn ( ) {
70126 let isReady = false ;
71-
72127 return injected ( {
73128 shimDisconnect : true ,
74129 target : {
75- id : binanceWallet ( ) . id ,
76- name : binanceWallet ( ) . name ,
130+ id : 'binanceWeb3Wallet' ,
131+ name : 'Binance Wallet' ,
77132 async provider ( ) {
78- if ( isMobile ( ) && isInstalled ( ) && ! isReady ) {
133+ if ( isMobile ( ) && isBinanceInstalled ( ) && ! isReady ) {
79134 await sleep ( 3000 ) ;
80135 }
81136 isReady = true ;
82- return getProvider ( ) ;
137+ return getBinanceProvider ( ) ;
83138 } ,
84139 } ,
85140 ...connectorOptions ,
0 commit comments