Skip to content

Commit cb03108

Browse files
authored
Merge pull request #286 from node-real/feat/1x-binance-web3-wallet-extension
fix: Restore BinanceWeb3WalletConnector for mobile in-app browser compatibility
2 parents aed1cb3 + 908d2cb commit cb03108

4 files changed

Lines changed: 64 additions & 16 deletions

File tree

.changeset/lemon-bugs-try.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@node-real/walletkit': minor
3+
---
4+
5+
fix: restore BinanceWeb3WalletConnector for mobile in-app browser compatibility
6+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { sleep } from '@/utils/common';
2+
import { Chain, WindowProvider } from 'wagmi';
3+
import { InjectedConnector } from 'wagmi/connectors/injected';
4+
import { BINANCE_WEB3_WALLET_ID, BINANCE_WEB3_WALLET_NAME, isBinanceWeb3Wallet } from '.';
5+
import { isMobile } from '@/base/utils/mobile';
6+
7+
export type BinanceWeb3WalletConnectorOptions = Required<
8+
ConstructorParameters<typeof InjectedConnector>
9+
>[0]['options'];
10+
11+
export interface CustomConstructorParams {
12+
chains?: Chain[];
13+
options?: BinanceWeb3WalletConnectorOptions;
14+
}
15+
16+
export class BinanceWeb3WalletConnector extends InjectedConnector {
17+
public id = BINANCE_WEB3_WALLET_ID;
18+
protected shimDisconnectKey = `${this.id}.shimDisconnect`;
19+
20+
constructor(props: CustomConstructorParams) {
21+
const { chains, options: _options } = props ?? {};
22+
23+
const options = {
24+
name: BINANCE_WEB3_WALLET_NAME,
25+
shimDisconnect: true,
26+
getProvider,
27+
..._options,
28+
};
29+
30+
if (typeof window !== 'undefined' && isMobile() && isBinanceWeb3Wallet()) {
31+
(window.ethereum as any)?.enable?.();
32+
}
33+
34+
super({
35+
chains,
36+
options,
37+
});
38+
}
39+
40+
public async getProvider(): Promise<WindowProvider | undefined> {
41+
await sleep();
42+
return this.options.getProvider();
43+
}
44+
}
45+
46+
function getProvider() {
47+
if (typeof window === 'undefined') return;
48+
49+
if (isMobile()) {
50+
return window.ethereum;
51+
}
52+
53+
// Desktop: use Binance Web3 Wallet browser extension
54+
return window.binancew3w?.ethereum;
55+
}

packages/walletkit/src/wallets/binanceWeb3Wallet/index.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { Chain } from 'wagmi';
22
import { PartialCustomProps, WalletProps } from '..';
33
import { BinanceWeb3WalletIcon, BinanceWeb3WalletTransparentIcon } from './icon';
44
import { hasInjectedProvider } from '../utils';
5-
import { CustomConnector } from '../custom/connector';
6-
import { isMobile } from '@/base/utils/mobile';
5+
import { BinanceWeb3WalletConnector } from './connector';
76

87
export const BINANCE_WEB3_WALLET_ID = 'binanceWeb3Wallet';
98
export const BINANCE_WEB3_WALLET_NAME = 'Binance Web3 Wallet';
@@ -25,21 +24,9 @@ export function binanceWeb3Wallet(props: PartialCustomProps = {}): WalletProps {
2524
showQRCode: false,
2625
isInstalled: isBinanceWeb3Wallet,
2726
createConnector: (chains: Chain[]) => {
28-
return new CustomConnector({
29-
id: BINANCE_WEB3_WALLET_ID,
27+
return new BinanceWeb3WalletConnector({
3028
chains,
3129
options: {
32-
name: BINANCE_WEB3_WALLET_NAME,
33-
shimDisconnect: true,
34-
getProvider() {
35-
if (typeof window === 'undefined') return;
36-
37-
if (isMobile()) {
38-
return window.ethereum;
39-
}
40-
41-
return window.binancew3w?.ethereum;
42-
},
4330
...connectorOptions,
4431
},
4532
});

website/src/pages/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm i @node-real/walletkit@^1 wagmi@^1 viem@^1
1818

1919
| wallet | mainnet | testnet |
2020
| ------------------- | ------- | ------- |
21-
| binanceWeb3Wallet() || |
21+
| binanceWeb3Wallet() || |
2222
| bitgetWallet() |||
2323
| coinbaseWallet() |||
2424
| metaMask() |||

0 commit comments

Comments
 (0)