-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathadapter.ts
295 lines (248 loc) · 11 KB
/
adapter.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import type { WalletAdapterNetwork, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
WalletConfigError,
WalletConnectionError,
WalletDisconnectedError,
WalletDisconnectionError,
WalletError,
WalletLoadError,
WalletNotConnectedError,
WalletNotReadyError,
WalletPublicKeyError,
WalletReadyState,
WalletSendTransactionError,
WalletSignMessageError,
WalletSignTransactionError,
isIosAndRedirectable,
isVersionedTransaction,
scopePollingDetectionStrategy,
type SendTransactionOptions,
} from '@solana/wallet-adapter-base';
import type { Transaction, TransactionVersion, VersionedTransaction } from '@solana/web3.js';
import { PublicKey, type Connection, type TransactionSignature } from '@solana/web3.js';
import type { default as Solflare } from '@solflare-wallet/sdk';
import { detectAndRegisterSolflareMetaMaskWallet } from './metamask/detect.js';
interface SolflareWindow extends Window {
solflare?: {
isSolflare?: boolean;
};
SolflareApp?: unknown;
}
declare const window: SolflareWindow;
export interface SolflareWalletAdapterConfig {
network?: WalletAdapterNetwork;
}
export const SolflareWalletName = 'Solflare' as WalletName<'Solflare'>;
export class SolflareWalletAdapter extends BaseMessageSignerWalletAdapter {
name = SolflareWalletName;
url = 'https://solflare.com';
icon =
'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIGlkPSJTIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MCA1MCI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMwMjA1MGE7c3Ryb2tlOiNmZmVmNDY7c3Ryb2tlLW1pdGVybGltaXQ6MTA7c3Ryb2tlLXdpZHRoOi41cHg7fS5jbHMtMntmaWxsOiNmZmVmNDY7fTwvc3R5bGU+PC9kZWZzPjxyZWN0IGNsYXNzPSJjbHMtMiIgeD0iMCIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiByeD0iMTIiIHJ5PSIxMiIvPjxwYXRoIGNsYXNzPSJjbHMtMSIgZD0iTTI0LjIzLDI2LjQybDIuNDYtMi4zOCw0LjU5LDEuNWMzLjAxLDEsNC41MSwyLjg0LDQuNTEsNS40MywwLDEuOTYtLjc1LDMuMjYtMi4yNSw0LjkzbC0uNDYuNS4xNy0xLjE3Yy42Ny00LjI2LS41OC02LjA5LTQuNzItNy40M2wtNC4zLTEuMzhoMFpNMTguMDUsMTEuODVsMTIuNTIsNC4xNy0yLjcxLDIuNTktNi41MS0yLjE3Yy0yLjI1LS43NS0zLjAxLTEuOTYtMy4zLTQuNTF2LS4wOGgwWk0xNy4zLDMzLjA2bDIuODQtMi43MSw1LjM0LDEuNzVjMi44LjkyLDMuNzYsMi4xMywzLjQ2LDUuMThsLTExLjY1LTQuMjJoMFpNMTMuNzEsMjAuOTVjMC0uNzkuNDItMS41NCwxLjEzLTIuMTcuNzUsMS4wOSwyLjA1LDIuMDUsNC4wOSwyLjcxbDQuNDIsMS40Ni0yLjQ2LDIuMzgtNC4zNC0xLjQyYy0yLS42Ny0yLjg0LTEuNjctMi44NC0yLjk2TTI2LjgyLDQyLjg3YzkuMTgtNi4wOSwxNC4xMS0xMC4yMywxNC4xMS0xNS4zMiwwLTMuMzgtMi01LjI2LTYuNDMtNi43MmwtMy4zNC0xLjEzLDkuMTQtOC43Ny0xLjg0LTEuOTYtMi43MSwyLjM4LTEyLjgxLTQuMjJjLTMuOTcsMS4yOS04Ljk3LDUuMDktOC45Nyw4Ljg5LDAsLjQyLjA0LjgzLjE3LDEuMjktMy4zLDEuODgtNC42MywzLjYzLTQuNjMsNS44LDAsMi4wNSwxLjA5LDQuMDksNC41NSw1LjIybDIuNzUuOTItOS41Miw5LjE0LDEuODQsMS45NiwyLjk2LTIuNzEsMTQuNzMsNS4yMmgwWiIvPjwvc3ZnPg==';
supportedTransactionVersions: ReadonlySet<TransactionVersion> = new Set(['legacy', 0]);
private _connecting: boolean;
private _wallet: Solflare | null;
private _publicKey: PublicKey | null;
private _config: SolflareWalletAdapterConfig;
private _readyState: WalletReadyState =
typeof window === 'undefined' || typeof document === 'undefined'
? WalletReadyState.Unsupported
: WalletReadyState.Loadable;
constructor(config: SolflareWalletAdapterConfig = {}) {
super();
this._connecting = false;
this._publicKey = null;
this._wallet = null;
this._config = config;
if (this._readyState !== WalletReadyState.Unsupported) {
scopePollingDetectionStrategy(() => {
if (window.solflare?.isSolflare || window.SolflareApp) {
this._readyState = WalletReadyState.Installed;
this.emit('readyStateChange', this._readyState);
return true;
}
return false;
});
detectAndRegisterSolflareMetaMaskWallet();
}
}
get publicKey() {
return this._publicKey;
}
get connecting() {
return this._connecting;
}
get connected() {
return !!this._wallet?.connected;
}
get readyState() {
return this._readyState;
}
async autoConnect(): Promise<void> {
// Skip autoconnect in the Loadable state on iOS
// We can't redirect to a universal link without user input
if (!(this.readyState === WalletReadyState.Loadable && isIosAndRedirectable())) {
await this.connect();
}
}
async connect(): Promise<void> {
try {
if (this.connected || this.connecting) return;
if (this._readyState !== WalletReadyState.Loadable && this._readyState !== WalletReadyState.Installed)
throw new WalletNotReadyError();
// redirect to the Solflare /browse universal link
// this will open the current URL in the Solflare in-wallet browser
if (this.readyState === WalletReadyState.Loadable && isIosAndRedirectable()) {
const url = encodeURIComponent(window.location.href);
const ref = encodeURIComponent(window.location.origin);
window.location.href = `https://solflare.com/ul/v1/browse/${url}?ref=${ref}`;
return;
}
let SolflareClass: typeof Solflare;
try {
SolflareClass = (await import('@solflare-wallet/sdk')).default;
} catch (error: any) {
throw new WalletLoadError(error?.message, error);
}
let wallet: Solflare;
try {
wallet = new SolflareClass({ network: this._config.network });
} catch (error: any) {
throw new WalletConfigError(error?.message, error);
}
this._connecting = true;
if (!wallet.connected) {
try {
await wallet.connect();
} catch (error: any) {
throw new WalletConnectionError(error?.message, error);
}
}
if (!wallet.publicKey) throw new WalletConnectionError();
let publicKey: PublicKey;
try {
publicKey = new PublicKey(wallet.publicKey.toBytes());
} catch (error: any) {
throw new WalletPublicKeyError(error?.message, error);
}
wallet.on('disconnect', this._disconnected);
wallet.on('accountChanged', this._accountChanged);
this._wallet = wallet;
this._publicKey = publicKey;
this.emit('connect', publicKey);
} catch (error: any) {
this.emit('error', error);
throw error;
} finally {
this._connecting = false;
}
}
async disconnect(): Promise<void> {
const wallet = this._wallet;
if (wallet) {
wallet.off('disconnect', this._disconnected);
wallet.off('accountChanged', this._accountChanged);
this._wallet = null;
this._publicKey = null;
try {
await wallet.disconnect();
} catch (error: any) {
this.emit('error', new WalletDisconnectionError(error?.message, error));
}
}
this.emit('disconnect');
}
async sendTransaction<T extends Transaction | VersionedTransaction>(
transaction: T,
connection: Connection,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
const wallet = this._wallet;
if (!wallet) throw new WalletNotConnectedError();
try {
const { signers, ...sendOptions } = options;
if (isVersionedTransaction(transaction)) {
signers?.length && transaction.sign(signers);
} else {
transaction = (await this.prepareTransaction(transaction, connection, sendOptions)) as T;
signers?.length && (transaction as Transaction).partialSign(...signers);
}
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
return await wallet.signAndSendTransaction(transaction, sendOptions);
} catch (error: any) {
if (error instanceof WalletError) throw error;
throw new WalletSendTransactionError(error?.message, error);
}
} catch (error: any) {
this.emit('error', error);
throw error;
}
}
async signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T> {
try {
const wallet = this._wallet;
if (!wallet) throw new WalletNotConnectedError();
try {
return ((await wallet.signTransaction(transaction)) as T) || transaction;
} catch (error: any) {
throw new WalletSignTransactionError(error?.message, error);
}
} catch (error: any) {
this.emit('error', error);
throw error;
}
}
async signAllTransactions<T extends Transaction | VersionedTransaction>(transactions: T[]): Promise<T[]> {
try {
const wallet = this._wallet;
if (!wallet) throw new WalletNotConnectedError();
try {
return ((await wallet.signAllTransactions(transactions)) as T[]) || transactions;
} catch (error: any) {
throw new WalletSignTransactionError(error?.message, error);
}
} catch (error: any) {
this.emit('error', error);
throw error;
}
}
async signMessage(message: Uint8Array): Promise<Uint8Array> {
try {
const wallet = this._wallet;
if (!wallet) throw new WalletNotConnectedError();
try {
return await wallet.signMessage(message, 'utf8');
} catch (error: any) {
throw new WalletSignMessageError(error?.message, error);
}
} catch (error: any) {
this.emit('error', error);
throw error;
}
}
private _disconnected = () => {
const wallet = this._wallet;
if (wallet) {
wallet.off('disconnect', this._disconnected);
this._wallet = null;
this._publicKey = null;
this.emit('error', new WalletDisconnectedError());
this.emit('disconnect');
}
};
private _accountChanged = (newPublicKey?: PublicKey) => {
if (!newPublicKey) return;
const publicKey = this._publicKey;
if (!publicKey) return;
try {
newPublicKey = new PublicKey(newPublicKey.toBytes());
} catch (error: any) {
this.emit('error', new WalletPublicKeyError(error?.message, error));
return;
}
if (publicKey.equals(newPublicKey)) return;
this._publicKey = newPublicKey;
this.emit('connect', newPublicKey);
};
}