-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathBitgetConnector.ts
198 lines (159 loc) · 5.61 KB
/
BitgetConnector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { type CaipNetwork, ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common'
import { CoreHelperUtil, type RequestArguments } from '@reown/appkit-controllers'
import { PresetsUtil } from '@reown/appkit-utils'
import { bitcoin } from '@reown/appkit/networks'
import { MethodNotSupportedError } from '../errors/MethodNotSupportedError.js'
import type { BitcoinConnector } from '../utils/BitcoinConnector.js'
import { ProviderEventEmitter } from '../utils/ProviderEventEmitter.js'
export class BitgetConnector extends ProviderEventEmitter implements BitcoinConnector {
public readonly id = 'Bitget'
public readonly name = 'Bitget Wallet'
public readonly chain = 'bip122'
public readonly type = 'ANNOUNCED'
public readonly explorerId =
PresetsUtil.ConnectorExplorerIds[CommonConstantsUtil.CONNECTOR_ID.BITGET]
public readonly imageUrl: string
public readonly provider = this
private readonly wallet: BitgetConnector.Wallet
private readonly requestedChains: CaipNetwork[] = []
private readonly getActiveNetwork: () => CaipNetwork | undefined
constructor({
wallet,
requestedChains,
getActiveNetwork,
imageUrl
}: BitgetConnector.ConstructorParams) {
super()
this.wallet = wallet
this.requestedChains = requestedChains
this.getActiveNetwork = getActiveNetwork
this.imageUrl = imageUrl
}
public get chains() {
return this.requestedChains.filter(chain => chain.caipNetworkId === bitcoin.caipNetworkId)
}
public async connect(): Promise<string> {
const [address] = await this.wallet.requestAccounts()
if (!address) {
throw new Error('No account available')
}
this.bindEvents()
return address
}
public async disconnect(): Promise<void> {
this.unbindEvents()
await this.wallet.disconnect()
}
public async getAccountAddresses(): Promise<BitcoinConnector.AccountAddress[]> {
const accounts = await this.wallet.getAccounts()
const publicKeyOfActiveAccount = await this.wallet.getPublicKey()
const accountList = accounts.map(account => ({
address: account,
purpose: 'payment' as const,
publicKey: publicKeyOfActiveAccount
}))
return accountList
}
public async signMessage(params: BitcoinConnector.SignMessageParams): Promise<string> {
return this.wallet.signMessage(params.message)
}
public async sendTransfer(params: BitcoinConnector.SendTransferParams): Promise<string> {
const network = this.getActiveNetwork()
if (!network) {
throw new Error('No active network available')
}
const from = (await this.wallet.getAccounts())[0]
if (!from) {
throw new Error('No account available')
}
const txId = await this.wallet.sendBitcoin(params.recipient, params.amount)
return txId
}
public async signPSBT(
params: BitcoinConnector.SignPSBTParams
): Promise<BitcoinConnector.SignPSBTResponse> {
const psbtHex = Buffer.from(params.psbt, 'base64').toString('hex')
const signedPsbtHex = await this.wallet.signPsbt(psbtHex, {
toSignInputs: params.signInputs
})
let txid: string | undefined = undefined
if (params.broadcast) {
txid = await this.wallet.pushPsbt(signedPsbtHex)
}
return {
psbt: Buffer.from(signedPsbtHex, 'hex').toString('base64'),
txid
}
}
public request<T>(_args: RequestArguments): Promise<T> {
return Promise.reject(new MethodNotSupportedError(this.id, 'request'))
}
private bindEvents(): void {
this.unbindEvents()
this.wallet.on('accountChanged', account => {
if (typeof account === 'object' && account && 'address' in account) {
this.emit('accountsChanged', [account.address])
}
})
this.wallet.on('disconnect', () => {
this.emit('disconnect')
})
}
private unbindEvents(): void {
this.wallet.removeAllListeners()
}
public static getWallet(params: BitgetConnector.GetWalletParams): BitgetConnector | undefined {
if (!CoreHelperUtil.isClient()) {
return undefined
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bitkeep = (window as any)?.bitkeep
const wallet = bitkeep?.unisat
/**
* Bitget doesn't provide a way to get the image URL specifally for bitcoin
* so we use the icon for cardano as a fallback
*/
const imageUrl = bitkeep?.suiWallet?.icon || ''
if (wallet) {
return new BitgetConnector({ wallet, imageUrl, ...params })
}
return undefined
}
public async getPublicKey(): Promise<string> {
return this.wallet.getPublicKey()
}
}
export namespace BitgetConnector {
export type ConstructorParams = {
wallet: Wallet
requestedChains: CaipNetwork[]
getActiveNetwork: () => CaipNetwork | undefined
imageUrl: string
}
export type Wallet = {
/*
* This interface doesn't include all available methods
* Reference: https://www.okx.com/web3/build/docs/sdks/chains/bitcoin/provider
*/
requestAccounts(): Promise<string[]>
disconnect(): Promise<void>
getAccounts(): Promise<string[]>
pushPsbt(psbtHex: string): Promise<string>
signMessage(signStr: string, type?: 'ecdsa' | 'bip322-simple'): Promise<string>
signPsbt(
psbtHex: string,
params: {
toSignInputs: {
index: number
address: string
sighashTypes: number[]
}[]
}
): Promise<string>
sendBitcoin(toAddress: string, amount: string): Promise<string>
on(event: string, listener: (param?: unknown) => void): void
removeAllListeners(): void
getPublicKey(): Promise<string>
}
export type GetWalletParams = Omit<ConstructorParams, 'wallet' | 'imageUrl'>
}