@@ -27,43 +27,19 @@ const getUnisatNetwork = (network: Network): WalletNetwork => {
27
27
28
28
type AccountsChangedEvent = ( event : 'accountsChanged' , handler : ( accounts : Array < string > ) => void ) => void ;
29
29
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
-
49
30
type Balance = { confirmed : number ; unconfirmed : number ; total : number } ;
50
31
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 = {
52
36
requestAccounts : ( ) => Promise < string [ ] > ;
53
37
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 > ;
63
38
getNetwork : ( ) => Promise < WalletNetwork > ;
39
+ switchNetwork : ( network : WalletNetwork ) => Promise < void > ;
64
40
getPublicKey : ( ) => Promise < string > ;
65
41
getBalance : ( ) => Promise < Balance > ;
66
- sendBitcoin : ( address : string , atomicAmount : number , options ?: { feeRate : number } ) => Promise < string > ;
42
+ signMessage : ( msg : string , type ?: 'ecdsa' | 'bip322-simple' ) => Promise < string > ;
67
43
signPsbt : (
68
44
psbtHex : string ,
69
45
options ?: {
@@ -77,30 +53,44 @@ type Unisat = {
77
53
} [ ] ;
78
54
}
79
55
) => Promise < string > ;
80
- signMessage : ( msg : string , type ?: 'ecdsa' | 'bip322-simple' ) => Promise < string > ;
56
+ on : AccountsChangedEvent ;
57
+ removeListener : AccountsChangedEvent ;
81
58
} ;
82
59
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' ;
84
66
85
67
declare global {
86
68
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 ;
90
75
} ;
91
76
}
92
77
}
93
78
94
79
const metadata : Record < WalletSource , { id : string ; name : string ; homepage : string ; icon ?: string } > = {
95
- bitget : {
96
- id : 'com. bitget.web3 ' ,
80
+ bitkeep : {
81
+ id : 'bitget' ,
97
82
name : 'Bitget Wallet' ,
98
83
homepage : 'https://web3.bitget.com' ,
99
84
icon : bitgetLogo
100
85
} ,
86
+ binancew3w : {
87
+ id : 'binancew3w' ,
88
+ name : 'Binance Web3 Wallet' ,
89
+ homepage : 'https://www.binance.com/en-GB'
90
+ } ,
101
91
unisat : {
102
92
id : 'unisat' ,
103
- name : 'Unisat ' ,
93
+ name : 'UniSat ' ,
104
94
homepage : 'https://unisat.io/'
105
95
}
106
96
} ;
@@ -116,12 +106,19 @@ class UnisatConnector extends SatsConnector {
116
106
this . source = source ;
117
107
}
118
108
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
+ }
121
118
}
122
119
123
120
async connect ( ) : Promise < void > {
124
- const walletSource = this . getSource ( ) ;
121
+ const walletSource = this . getSource ( ) ! ;
125
122
126
123
const network = await walletSource . getNetwork ( ) ;
127
124
const mappedNetwork = getLibNetwork ( network ) ;
@@ -132,14 +129,14 @@ class UnisatConnector extends SatsConnector {
132
129
await walletSource . switchNetwork ( expectedNetwork ) ;
133
130
}
134
131
135
- const [ accounts , publicKey ] = await Promise . all ( [ window . unisat . requestAccounts ( ) , window . unisat . getPublicKey ( ) ] ) ;
132
+ const [ accounts , publicKey ] = await Promise . all ( [ walletSource . requestAccounts ( ) , walletSource . getPublicKey ( ) ] ) ;
136
133
137
134
this . paymentAddress = accounts [ 0 ] ;
138
135
this . ordinalsAddress = accounts [ 0 ] ;
139
136
this . publicKey = publicKey ;
140
137
141
138
// 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 ) ) ;
143
140
}
144
141
145
142
disconnect ( ) {
@@ -148,19 +145,19 @@ class UnisatConnector extends SatsConnector {
148
145
}
149
146
150
147
signMessage ( message : string ) {
151
- return this . getSource ( ) . signMessage ( message ) ;
148
+ return this . getSource ( ) ! . signMessage ( message ) ;
152
149
}
153
150
154
151
on ( callback : ( account : string ) => void ) : void {
155
- this . getSource ( ) . on ( 'accountsChanged' , ( [ account ] ) => {
152
+ this . getSource ( ) ! . on ( 'accountsChanged' , ( [ account ] ) => {
156
153
callback ( account ) ;
157
154
158
155
this . changeAccount ( account ) ;
159
156
} ) ;
160
157
}
161
158
162
159
removeListener ( callback : ( account : string ) => void ) : void {
163
- this . getSource ( ) . removeListener ( 'accountsChanged' , ( [ account ] ) => {
160
+ this . getSource ( ) ! . removeListener ( 'accountsChanged' , ( [ account ] ) => {
164
161
callback ( account ) ;
165
162
166
163
this . changeAccount ( account ) ;
@@ -169,7 +166,7 @@ class UnisatConnector extends SatsConnector {
169
166
170
167
async changeAccount ( account : string ) {
171
168
this . paymentAddress = account ;
172
- this . publicKey = await this . getSource ( ) . getPublicKey ( ) ;
169
+ this . publicKey = await this . getSource ( ) ! . getPublicKey ( ) ;
173
170
}
174
171
175
172
async isReady ( ) {
@@ -179,7 +176,13 @@ class UnisatConnector extends SatsConnector {
179
176
}
180
177
181
178
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
+ }
183
186
}
184
187
185
188
async signPsbt ( psbtHex : string , psbtInputAccounts : PsbtInputAccounts [ ] ) : Promise < string > {
@@ -206,7 +209,7 @@ class UnisatConnector extends SatsConnector {
206
209
} ;
207
210
} ) ;
208
211
209
- const signedPsbtHex = await this . getSource ( ) . signPsbt ( psbtHex , {
212
+ const signedPsbtHex = await this . getSource ( ) ! . signPsbt ( psbtHex , {
210
213
autoFinalized : false ,
211
214
toSignInputs : toSignInputs
212
215
} ) ;
0 commit comments