Skip to content

Commit 95afd8b

Browse files
committed
feat: add binance w3w support
Signed-off-by: Gregory Hill <[email protected]>
1 parent 959e579 commit 95afd8b

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

packages/sats-wagmi/src/connectors/unisat.ts

+52-49
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,19 @@ const getUnisatNetwork = (network: Network): WalletNetwork => {
2727

2828
type AccountsChangedEvent = (event: 'accountsChanged', handler: (accounts: Array<string>) => void) => void;
2929

30-
type Inscription = {
31-
inscriptionId: string;
32-
inscriptionNumber: string;
33-
address: string;
34-
outputValue: string;
35-
content: string;
36-
contentLength: string;
37-
contentType: string;
38-
preview: string;
39-
timestamp: number;
40-
offset: number;
41-
genesisTransaction: string;
42-
location: string;
43-
};
44-
45-
type getInscriptionsResult = { total: number; list: Inscription[] };
46-
47-
type SendInscriptionsResult = { txid: string };
48-
4930
type Balance = { confirmed: number; unconfirmed: number; total: number };
5031

51-
type Unisat = {
32+
// https://docs.bitkeep.com/en/docs/guide/wallet/btc.html
33+
// https://developers.binance.com/docs/binance-w3w/bitcoin-provider
34+
// https://docs.unisat.io/dev/unisat-developer-center/unisat-wallet
35+
type UniSatBase = {
5236
requestAccounts: () => Promise<string[]>;
5337
getAccounts: () => Promise<string[]>;
54-
on: AccountsChangedEvent;
55-
removeListener: AccountsChangedEvent;
56-
getInscriptions: (cursor: number, size: number) => Promise<getInscriptionsResult>;
57-
sendInscription: (
58-
address: string,
59-
inscriptionId: string,
60-
options?: { feeRate: number }
61-
) => Promise<SendInscriptionsResult>;
62-
switchNetwork: (network: 'livenet' | 'testnet') => Promise<void>;
6338
getNetwork: () => Promise<WalletNetwork>;
39+
switchNetwork: (network: WalletNetwork) => Promise<void>;
6440
getPublicKey: () => Promise<string>;
6541
getBalance: () => Promise<Balance>;
66-
sendBitcoin: (address: string, atomicAmount: number, options?: { feeRate: number }) => Promise<string>;
42+
signMessage: (msg: string, type?: 'ecdsa' | 'bip322-simple') => Promise<string>;
6743
signPsbt: (
6844
psbtHex: string,
6945
options?: {
@@ -77,30 +53,44 @@ type Unisat = {
7753
}[];
7854
}
7955
) => Promise<string>;
80-
signMessage: (msg: string, type?: 'ecdsa' | 'bip322-simple') => Promise<string>;
56+
on: AccountsChangedEvent;
57+
removeListener: AccountsChangedEvent;
8158
};
8259

83-
type WalletSource = 'bitget' | 'unisat';
60+
// additional methods not supported by all connectors
61+
type UniSatExt = {
62+
sendBitcoin: (address: string, atomicAmount: number, options?: { feeRate: number }) => Promise<string>;
63+
};
64+
65+
type WalletSource = 'bitkeep' | 'binancew3w' | 'unisat';
8466

8567
declare global {
8668
interface Window {
87-
unisat: Unisat;
88-
bitget: {
89-
unisat: Unisat;
69+
unisat: UniSatBase & UniSatExt;
70+
bitkeep: {
71+
unisat: UniSatBase & UniSatExt;
72+
};
73+
binancew3w: {
74+
unisat: UniSatBase;
9075
};
9176
}
9277
}
9378

9479
const metadata: Record<WalletSource, { id: string; name: string; homepage: string; icon?: string }> = {
95-
bitget: {
96-
id: 'com.bitget.web3',
80+
bitkeep: {
81+
id: 'bitget',
9782
name: 'Bitget Wallet',
9883
homepage: 'https://web3.bitget.com',
9984
icon: bitgetLogo
10085
},
86+
binancew3w: {
87+
id: 'binancew3w',
88+
name: 'Binance Web3 Wallet',
89+
homepage: 'https://www.binance.com/en-GB'
90+
},
10191
unisat: {
10292
id: 'unisat',
103-
name: 'Unisat',
93+
name: 'UniSat',
10494
homepage: 'https://unisat.io/'
10595
}
10696
};
@@ -116,12 +106,19 @@ class UnisatConnector extends SatsConnector {
116106
this.source = source;
117107
}
118108

119-
private getSource() {
120-
return this.source === 'bitget' ? window?.bitget?.unisat : window?.unisat;
109+
private getSource(): UniSatBase | (UniSatBase & UniSatExt) | undefined {
110+
switch (this.source) {
111+
case 'bitkeep':
112+
return window?.bitkeep?.unisat || undefined;
113+
case 'binancew3w':
114+
return window?.binancew3w?.unisat || undefined;
115+
case 'unisat':
116+
return window?.unisat || undefined;
117+
}
121118
}
122119

123120
async connect(): Promise<void> {
124-
const walletSource = this.getSource();
121+
const walletSource = this.getSource()!;
125122

126123
const network = await walletSource.getNetwork();
127124
const mappedNetwork = getLibNetwork(network);
@@ -132,14 +129,14 @@ class UnisatConnector extends SatsConnector {
132129
await walletSource.switchNetwork(expectedNetwork);
133130
}
134131

135-
const [accounts, publicKey] = await Promise.all([window.unisat.requestAccounts(), window.unisat.getPublicKey()]);
132+
const [accounts, publicKey] = await Promise.all([walletSource.requestAccounts(), walletSource.getPublicKey()]);
136133

137134
this.paymentAddress = accounts[0];
138135
this.ordinalsAddress = accounts[0];
139136
this.publicKey = publicKey;
140137

141138
// https://github.com/unisat-wallet/extension/blob/04cbfd6e7f7953815d35d8f77df457388fea2707/src/background/controller/provider/controller.ts#L39
142-
window.unisat.on('accountsChanged', ([account]) => this.changeAccount(account));
139+
walletSource.on('accountsChanged', ([account]) => this.changeAccount(account));
143140
}
144141

145142
disconnect() {
@@ -148,19 +145,19 @@ class UnisatConnector extends SatsConnector {
148145
}
149146

150147
signMessage(message: string) {
151-
return this.getSource().signMessage(message);
148+
return this.getSource()!.signMessage(message);
152149
}
153150

154151
on(callback: (account: string) => void): void {
155-
this.getSource().on('accountsChanged', ([account]) => {
152+
this.getSource()!.on('accountsChanged', ([account]) => {
156153
callback(account);
157154

158155
this.changeAccount(account);
159156
});
160157
}
161158

162159
removeListener(callback: (account: string) => void): void {
163-
this.getSource().removeListener('accountsChanged', ([account]) => {
160+
this.getSource()!.removeListener('accountsChanged', ([account]) => {
164161
callback(account);
165162

166163
this.changeAccount(account);
@@ -169,7 +166,7 @@ class UnisatConnector extends SatsConnector {
169166

170167
async changeAccount(account: string) {
171168
this.paymentAddress = account;
172-
this.publicKey = await this.getSource().getPublicKey();
169+
this.publicKey = await this.getSource()!.getPublicKey();
173170
}
174171

175172
async isReady() {
@@ -179,7 +176,13 @@ class UnisatConnector extends SatsConnector {
179176
}
180177

181178
async sendToAddress(toAddress: string, amount: number): Promise<string> {
182-
return this.getSource().sendBitcoin(toAddress, amount);
179+
const source = this.getSource()!;
180+
181+
if ('sendBitcoin' in source) {
182+
return source.sendBitcoin(toAddress, amount);
183+
} else {
184+
return super.sendToAddress(toAddress, amount);
185+
}
183186
}
184187

185188
async signPsbt(psbtHex: string, psbtInputAccounts: PsbtInputAccounts[]): Promise<string> {
@@ -206,7 +209,7 @@ class UnisatConnector extends SatsConnector {
206209
};
207210
});
208211

209-
const signedPsbtHex = await this.getSource().signPsbt(psbtHex, {
212+
const signedPsbtHex = await this.getSource()!.signPsbt(psbtHex, {
210213
autoFinalized: false,
211214
toSignInputs: toSignInputs
212215
});

packages/sats-wagmi/src/provider.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ const SatsWagmiConfig: FC<SatsWagmiConfigProps> = ({ children, queryClient, netw
6262

6363
readyConnectors.push(unisat);
6464

65-
const mmSnap = new MMSnapConnector(network);
65+
const bitkeep = new UnisatConnector(network, 'bitkeep');
6666

67-
readyConnectors.push(mmSnap);
67+
readyConnectors.push(bitkeep);
6868

69-
const bitkeep = new UnisatConnector(network, 'bitget');
69+
const binancew3w = new UnisatConnector(network, 'binancew3w');
7070

71-
readyConnectors.push(bitkeep);
71+
readyConnectors.push(binancew3w);
72+
73+
const mmSnap = new MMSnapConnector(network);
74+
75+
readyConnectors.push(mmSnap);
7276

7377
const leather = new LeatherConnector(network);
7478

0 commit comments

Comments
 (0)