@manahippo/aptos-wallet-adapter 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/dist/WalletAdapters/BaseAdapter.d.ts +6 -6
- package/dist/WalletAdapters/BaseAdapter.d.ts.map +1 -1
- package/dist/WalletAdapters/BitkeepWallet.d.ts +1 -1
- package/dist/WalletAdapters/BitkeepWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/BloctoWallet.d.ts +4 -4
- package/dist/WalletAdapters/BloctoWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/BloctoWallet.js +3 -5
- package/dist/WalletAdapters/BloctoWallet.js.map +1 -1
- package/dist/WalletAdapters/CloverWallet.d.ts +57 -0
- package/dist/WalletAdapters/CloverWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/CloverWallet.js +213 -0
- package/dist/WalletAdapters/CloverWallet.js.map +1 -0
- package/dist/WalletAdapters/Coin98Wallet.d.ts +1 -1
- package/dist/WalletAdapters/Coin98Wallet.d.ts.map +1 -1
- package/dist/WalletAdapters/FoxWallet.d.ts +64 -0
- package/dist/WalletAdapters/FoxWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/FoxWallet.js +239 -0
- package/dist/WalletAdapters/FoxWallet.js.map +1 -0
- package/dist/WalletAdapters/HyperPayWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/HyperPayWallet.js +2 -2
- package/dist/WalletAdapters/HyperPayWallet.js.map +1 -1
- package/dist/WalletAdapters/MsafeWallet.d.ts +1 -0
- package/dist/WalletAdapters/MsafeWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/MsafeWallet.js +227 -0
- package/dist/WalletAdapters/MsafeWallet.js.map +1 -0
- package/dist/WalletAdapters/NightlyWallet.js +10 -10
- package/dist/WalletAdapters/NightlyWallet.js.map +1 -1
- package/dist/WalletAdapters/ONTOWallet.d.ts +1 -1
- package/dist/WalletAdapters/ONTOWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/OpenBlockWallet.d.ts +56 -0
- package/dist/WalletAdapters/OpenBlockWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/OpenBlockWallet.js +243 -0
- package/dist/WalletAdapters/OpenBlockWallet.js.map +1 -0
- package/dist/WalletAdapters/PetraWallet.d.ts +1 -1
- package/dist/WalletAdapters/PetraWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/PetraWallet.js +1 -1
- package/dist/WalletAdapters/PetraWallet.js.map +1 -1
- package/dist/WalletAdapters/PontemWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/PontemWallet.js +8 -7
- package/dist/WalletAdapters/PontemWallet.js.map +1 -1
- package/dist/WalletAdapters/SpikaWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/SpikaWallet.js +1 -1
- package/dist/WalletAdapters/SpikaWallet.js.map +1 -1
- package/dist/WalletAdapters/index.d.ts +3 -0
- package/dist/WalletAdapters/index.d.ts.map +1 -1
- package/dist/WalletAdapters/index.js +4 -0
- package/dist/WalletAdapters/index.js.map +1 -1
- package/dist/WalletProviders/WalletProvider.d.ts.map +1 -1
- package/dist/WalletProviders/WalletProvider.js +41 -13
- package/dist/WalletProviders/WalletProvider.js.map +1 -1
- package/dist/utilities/util.d.ts +4 -0
- package/dist/utilities/util.d.ts.map +1 -1
- package/dist/utilities/util.js +23 -1
- package/dist/utilities/util.js.map +1 -1
- package/package.json +3 -2
- package/src/WalletAdapters/BloctoWallet.ts +10 -11
- package/src/WalletAdapters/CloverWallet.ts +274 -0
- package/src/WalletAdapters/FoxWallet.ts +312 -0
- package/src/WalletAdapters/HyperPayWallet.ts +2 -2
- package/src/WalletAdapters/MsafeWallet.ts +261 -0
- package/src/WalletAdapters/NightlyWallet.ts +2 -2
- package/src/WalletAdapters/OpenBlockWallet.ts +293 -0
- package/src/WalletAdapters/PetraWallet.ts +1 -1
- package/src/WalletAdapters/PontemWallet.ts +14 -11
- package/src/WalletAdapters/SpikaWallet.ts +1 -1
- package/src/WalletAdapters/index.ts +4 -0
- package/src/WalletProviders/WalletProvider.tsx +42 -13
- package/src/utilities/util.ts +13 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { Types } from 'aptos';
|
|
2
|
+
import {
|
|
3
|
+
WalletAccountChangeError,
|
|
4
|
+
WalletDisconnectionError,
|
|
5
|
+
WalletNetworkChangeError,
|
|
6
|
+
WalletNotConnectedError,
|
|
7
|
+
WalletNotReadyError,
|
|
8
|
+
WalletSignAndSubmitMessageError,
|
|
9
|
+
WalletSignMessageError,
|
|
10
|
+
WalletSignTransactionError
|
|
11
|
+
} from '../WalletProviders/errors';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
AccountKeys,
|
|
15
|
+
BaseWalletAdapter,
|
|
16
|
+
NetworkInfo,
|
|
17
|
+
scopePollingDetectionStrategy,
|
|
18
|
+
SignMessagePayload,
|
|
19
|
+
SignMessageResponse,
|
|
20
|
+
WalletAdapterNetwork,
|
|
21
|
+
WalletName,
|
|
22
|
+
WalletReadyState
|
|
23
|
+
} from './BaseAdapter';
|
|
24
|
+
|
|
25
|
+
type AddressInfo = { address: string; publicKey: string; authKey?: string };
|
|
26
|
+
|
|
27
|
+
interface IOpenBlockWallet {
|
|
28
|
+
connect: () => Promise<AddressInfo>;
|
|
29
|
+
account: () => Promise<AddressInfo>;
|
|
30
|
+
network: () => Promise<NetworkInfo>;
|
|
31
|
+
isConnected: () => Promise<boolean>;
|
|
32
|
+
signAndSubmitTransaction(transaction: any): Promise<{ hash: string }>;
|
|
33
|
+
signTransaction(payload: any): Promise<string>;
|
|
34
|
+
signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
|
|
35
|
+
disconnect(): Promise<void>;
|
|
36
|
+
onAccountChange: (listener: (newAddress: AddressInfo) => void) => void;
|
|
37
|
+
onNetworkChange: (listener: (network: NetworkInfo) => void) => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface OpenBlockWindow extends Window {
|
|
41
|
+
openblock?: {
|
|
42
|
+
aptos?: IOpenBlockWallet;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare const window: OpenBlockWindow;
|
|
47
|
+
|
|
48
|
+
export const OpenBlockWalletName = 'OpenBlock' as WalletName<'OpenBlock'>;
|
|
49
|
+
|
|
50
|
+
export interface OpenBlockWalletAdapterConfig {
|
|
51
|
+
provider?: IOpenBlockWallet;
|
|
52
|
+
// network?: WalletAdapterNetwork;
|
|
53
|
+
timeout?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class OpenBlockWalletAdapter extends BaseWalletAdapter {
|
|
57
|
+
name = OpenBlockWalletName;
|
|
58
|
+
|
|
59
|
+
url = 'https://openblock.com/';
|
|
60
|
+
|
|
61
|
+
icon = 'https://obstatic.243096.com/download/dapp/sdk/images/logo_blue.svg';
|
|
62
|
+
|
|
63
|
+
protected _provider: IOpenBlockWallet;
|
|
64
|
+
|
|
65
|
+
protected _network: WalletAdapterNetwork;
|
|
66
|
+
|
|
67
|
+
protected _chainId: string;
|
|
68
|
+
|
|
69
|
+
protected _api: string;
|
|
70
|
+
|
|
71
|
+
protected _timeout: number;
|
|
72
|
+
|
|
73
|
+
protected _readyState: WalletReadyState =
|
|
74
|
+
typeof window === 'undefined' || typeof document === 'undefined'
|
|
75
|
+
? WalletReadyState.Unsupported
|
|
76
|
+
: WalletReadyState.NotDetected;
|
|
77
|
+
|
|
78
|
+
protected _connecting: boolean;
|
|
79
|
+
|
|
80
|
+
protected _wallet: any | null;
|
|
81
|
+
|
|
82
|
+
constructor({
|
|
83
|
+
// provider,
|
|
84
|
+
// network = WalletAdapterNetwork.Testnet,
|
|
85
|
+
timeout = 10000
|
|
86
|
+
}: OpenBlockWalletAdapterConfig = {}) {
|
|
87
|
+
super();
|
|
88
|
+
|
|
89
|
+
this._provider = typeof window !== 'undefined' ? window.openblock?.aptos : undefined;
|
|
90
|
+
this._network = undefined;
|
|
91
|
+
this._timeout = timeout;
|
|
92
|
+
this._connecting = false;
|
|
93
|
+
this._wallet = null;
|
|
94
|
+
|
|
95
|
+
if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
|
|
96
|
+
require('@openblockhq/dappsdk');
|
|
97
|
+
scopePollingDetectionStrategy(() => {
|
|
98
|
+
if (window.openblock?.aptos) {
|
|
99
|
+
this._readyState = WalletReadyState.Installed;
|
|
100
|
+
this.emit('readyStateChange', this._readyState);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get publicAccount(): AccountKeys {
|
|
109
|
+
return {
|
|
110
|
+
publicKey: this._wallet?.publicKey || null,
|
|
111
|
+
address: this._wallet?.address || null,
|
|
112
|
+
authKey: this._wallet?.authKey || null
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get network(): NetworkInfo {
|
|
117
|
+
return {
|
|
118
|
+
name: this._network,
|
|
119
|
+
api: this._api,
|
|
120
|
+
chainId: this._chainId
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get connecting(): boolean {
|
|
125
|
+
return this._connecting;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get connected(): boolean {
|
|
129
|
+
return !!this._wallet?.isConnected;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get readyState(): WalletReadyState {
|
|
133
|
+
return this._readyState;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async connect(): Promise<void> {
|
|
137
|
+
try {
|
|
138
|
+
if (this.connected || this.connecting) return;
|
|
139
|
+
if (
|
|
140
|
+
!(
|
|
141
|
+
this._readyState === WalletReadyState.Loadable ||
|
|
142
|
+
this._readyState === WalletReadyState.Installed
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
throw new WalletNotReadyError();
|
|
146
|
+
this._connecting = true;
|
|
147
|
+
|
|
148
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
149
|
+
const isConnected = await this._provider?.isConnected();
|
|
150
|
+
if (isConnected === true) {
|
|
151
|
+
await provider?.disconnect();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const response = await provider?.connect();
|
|
155
|
+
if (response.address) {
|
|
156
|
+
this._wallet = {
|
|
157
|
+
address: response?.address,
|
|
158
|
+
publicKey: response?.publicKey,
|
|
159
|
+
isConnected: true
|
|
160
|
+
};
|
|
161
|
+
const network = await provider?.network();
|
|
162
|
+
|
|
163
|
+
this._network = network.name;
|
|
164
|
+
this._chainId = network.chainId;
|
|
165
|
+
this._api = network.api;
|
|
166
|
+
} else {
|
|
167
|
+
throw new Error('failed to connect');
|
|
168
|
+
}
|
|
169
|
+
this.emit('connect', this._wallet.publicKey);
|
|
170
|
+
} catch (error: any) {
|
|
171
|
+
this.emit('error', error);
|
|
172
|
+
throw error;
|
|
173
|
+
} finally {
|
|
174
|
+
this._connecting = false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async disconnect(): Promise<void> {
|
|
179
|
+
const wallet = this._wallet;
|
|
180
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
181
|
+
if (wallet) {
|
|
182
|
+
this._wallet = null;
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
await provider?.disconnect();
|
|
186
|
+
} catch (error: any) {
|
|
187
|
+
this.emit('error', new WalletDisconnectionError(error?.message, error));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
this.emit('disconnect');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async signTransaction(transaction: Types.TransactionPayload): Promise<Uint8Array> {
|
|
195
|
+
try {
|
|
196
|
+
const wallet = this._wallet;
|
|
197
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
198
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
199
|
+
|
|
200
|
+
const response = await provider.signTransaction(transaction);
|
|
201
|
+
if (response) {
|
|
202
|
+
return new TextEncoder().encode(response);
|
|
203
|
+
}
|
|
204
|
+
throw new Error('failed to SignTransaction');
|
|
205
|
+
} catch (error: any) {
|
|
206
|
+
const errMsg = error.message;
|
|
207
|
+
this.emit('error', new WalletSignTransactionError(errMsg));
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async signAndSubmitTransaction(
|
|
213
|
+
transaction: Types.TransactionPayload
|
|
214
|
+
): Promise<{ hash: Types.HexEncodedBytes }> {
|
|
215
|
+
try {
|
|
216
|
+
const wallet = this._wallet;
|
|
217
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
218
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
219
|
+
const response = await provider.signAndSubmitTransaction(transaction);
|
|
220
|
+
if (response) {
|
|
221
|
+
return response;
|
|
222
|
+
}
|
|
223
|
+
throw new Error('Failed to SignAndSubmitTransaction');
|
|
224
|
+
} catch (error: any) {
|
|
225
|
+
const errMsg = error.message;
|
|
226
|
+
this.emit('error', new WalletSignAndSubmitMessageError(errMsg));
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async signMessage(msgPayload: SignMessagePayload): Promise<SignMessageResponse> {
|
|
232
|
+
try {
|
|
233
|
+
const wallet = this._wallet;
|
|
234
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
235
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
236
|
+
if (typeof msgPayload !== 'object' || !msgPayload.nonce) {
|
|
237
|
+
throw new WalletSignMessageError('Invalid signMessage Payload');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const response = await provider?.signMessage(msgPayload);
|
|
241
|
+
if (response) {
|
|
242
|
+
return response;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
throw new Error('Failed to signMessage');
|
|
246
|
+
} catch (error: any) {
|
|
247
|
+
const errMsg = error.message;
|
|
248
|
+
this.emit('error', new WalletSignMessageError(errMsg));
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async onAccountChange(): Promise<void> {
|
|
254
|
+
try {
|
|
255
|
+
const wallet = this._wallet;
|
|
256
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
257
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
258
|
+
const handleAccountChange = async (newAccount: AddressInfo) => {
|
|
259
|
+
this._wallet = {
|
|
260
|
+
...this._wallet,
|
|
261
|
+
publicKey: newAccount.publicKey || this._wallet?.publicKey,
|
|
262
|
+
authKey: newAccount.authKey || this._wallet?.authKey,
|
|
263
|
+
address: newAccount.address || this._wallet?.address
|
|
264
|
+
};
|
|
265
|
+
this.emit('accountChange', newAccount.publicKey);
|
|
266
|
+
};
|
|
267
|
+
provider?.onAccountChange(handleAccountChange);
|
|
268
|
+
} catch (error: any) {
|
|
269
|
+
const errMsg = error.message;
|
|
270
|
+
this.emit('error', new WalletAccountChangeError(errMsg));
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async onNetworkChange(): Promise<void> {
|
|
276
|
+
try {
|
|
277
|
+
const wallet = this._wallet;
|
|
278
|
+
const provider = this._provider || window?.openblock?.aptos;
|
|
279
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
280
|
+
const handleNetworkChange = async (newNetwork: NetworkInfo) => {
|
|
281
|
+
this._network = newNetwork.name;
|
|
282
|
+
this._api = newNetwork.api;
|
|
283
|
+
this._chainId = newNetwork.chainId;
|
|
284
|
+
this.emit('networkChange', this._network);
|
|
285
|
+
};
|
|
286
|
+
provider?.onNetworkChange(handleNetworkChange);
|
|
287
|
+
} catch (error: any) {
|
|
288
|
+
const errMsg = error.message;
|
|
289
|
+
this.emit('error', new WalletNetworkChangeError(errMsg));
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -175,7 +175,7 @@ export class AptosWalletAdapter extends BaseWalletAdapter {
|
|
|
175
175
|
throw error;
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
this.emit('connect', this._wallet.
|
|
178
|
+
this.emit('connect', this._wallet.address);
|
|
179
179
|
} catch (error: any) {
|
|
180
180
|
this.emit('error', error);
|
|
181
181
|
throw error;
|
|
@@ -287,18 +287,21 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
287
287
|
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
288
288
|
const handleAccountChange = async (newAccount: string | undefined) => {
|
|
289
289
|
// disconnect wallet if newAccount is undefined
|
|
290
|
-
if (newAccount
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
290
|
+
if (newAccount) {
|
|
291
|
+
const newPublicKey = await provider?.publicKey();
|
|
292
|
+
this._wallet = {
|
|
293
|
+
...this._wallet,
|
|
294
|
+
address: newAccount,
|
|
295
|
+
publicKey: newPublicKey
|
|
296
|
+
};
|
|
297
|
+
} else {
|
|
298
|
+
const response = await provider?.connect();
|
|
299
|
+
this._wallet = {
|
|
300
|
+
...this._wallet,
|
|
301
|
+
address: response?.address || this._wallet?.address,
|
|
302
|
+
publicKey: response?.publicKey || this._wallet?.publicKey
|
|
303
|
+
};
|
|
295
304
|
}
|
|
296
|
-
const newPublicKey = await provider?.publicKey();
|
|
297
|
-
this._wallet = {
|
|
298
|
-
...this._wallet,
|
|
299
|
-
address: newAccount,
|
|
300
|
-
publicKey: newPublicKey
|
|
301
|
-
};
|
|
302
305
|
this.emit('accountChange', newAccount);
|
|
303
306
|
};
|
|
304
307
|
await provider?.onAccountChange(handleAccountChange);
|
|
@@ -51,7 +51,7 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
51
51
|
|
|
52
52
|
url = 'https://chrome.google.com/webstore/detail/spika/fadkojdgchhfkdkklllhcphknohbmjmb';
|
|
53
53
|
|
|
54
|
-
icon = 'https://
|
|
54
|
+
icon = 'https://raw.githubusercontent.com/hippospace/aptos-wallet-adapter/main/logos/spika.svg';
|
|
55
55
|
|
|
56
56
|
protected _provider: ISpikaWallet | undefined;
|
|
57
57
|
|
|
@@ -17,3 +17,7 @@ export * from './ONTOWallet';
|
|
|
17
17
|
export * from './BloctoWallet';
|
|
18
18
|
export * from './Coin98Wallet';
|
|
19
19
|
export * from './SafePalWallet';
|
|
20
|
+
export * from './FoxWallet';
|
|
21
|
+
// export * from './MsafeWallet';
|
|
22
|
+
export * from './OpenBlockWallet';
|
|
23
|
+
export * from './CloverWallet';
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { FC, ReactNode, useCallback, useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { Types } from 'aptos';
|
|
3
3
|
import {
|
|
4
|
+
WalletConnectionError,
|
|
4
5
|
WalletError,
|
|
5
6
|
WalletNotConnectedError,
|
|
6
7
|
WalletNotReadyError,
|
|
7
|
-
WalletNotSelectedError
|
|
8
|
+
WalletNotSelectedError,
|
|
9
|
+
WalletSignAndSubmitMessageError,
|
|
10
|
+
WalletSignMessageError,
|
|
11
|
+
WalletSignTransactionError
|
|
8
12
|
} from './errors';
|
|
9
13
|
import { useLocalStorage } from '../hooks/useLocalStorage';
|
|
10
14
|
import {
|
|
@@ -16,6 +20,7 @@ import {
|
|
|
16
20
|
WalletReadyState
|
|
17
21
|
} from '../WalletAdapters/BaseAdapter';
|
|
18
22
|
import { Wallet, WalletContext } from './useWallet';
|
|
23
|
+
import { timeoutPromise } from '../utilities/util';
|
|
19
24
|
|
|
20
25
|
export interface WalletProviderProps {
|
|
21
26
|
children: ReactNode;
|
|
@@ -39,6 +44,8 @@ const initialState: {
|
|
|
39
44
|
network: null
|
|
40
45
|
};
|
|
41
46
|
|
|
47
|
+
const TIMEOUT = 90;
|
|
48
|
+
|
|
42
49
|
export const WalletProvider: FC<WalletProviderProps> = ({
|
|
43
50
|
children,
|
|
44
51
|
wallets: adapters,
|
|
@@ -51,7 +58,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
51
58
|
const readyState = adapter?.readyState || WalletReadyState.Unsupported;
|
|
52
59
|
const [connecting, setConnecting] = useState(false);
|
|
53
60
|
const [disconnecting, setDisconnecting] = useState(false);
|
|
54
|
-
const isConnecting = useRef(false);
|
|
61
|
+
// const isConnecting = useRef(false);
|
|
55
62
|
const isDisconnecting = useRef(false);
|
|
56
63
|
const isUnloading = useRef(false);
|
|
57
64
|
|
|
@@ -117,6 +124,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
117
124
|
|
|
118
125
|
window.addEventListener('beforeunload', listener);
|
|
119
126
|
return () => window.removeEventListener('beforeunload', listener);
|
|
127
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
120
128
|
}, [isUnloading, autoConnect]);
|
|
121
129
|
|
|
122
130
|
// Handle the adapter's connect event
|
|
@@ -218,7 +226,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
218
226
|
// Connect the adapter to the wallet
|
|
219
227
|
const connect = useCallback(
|
|
220
228
|
async (walletName?) => {
|
|
221
|
-
if (
|
|
229
|
+
if (isDisconnecting.current || connected || !walletName) return;
|
|
222
230
|
let walletToConnect = initialState;
|
|
223
231
|
if (!adapter || walletName !== adapter?.name) {
|
|
224
232
|
const selectedWallet = wallets.find((wAdapter) => wAdapter.adapter.name === walletName);
|
|
@@ -258,18 +266,23 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
258
266
|
|
|
259
267
|
throw handleError(new WalletNotReadyError('Wallet Not Ready'));
|
|
260
268
|
}
|
|
261
|
-
isConnecting.current = true;
|
|
269
|
+
// isConnecting.current = true;
|
|
262
270
|
setConnecting(true);
|
|
263
271
|
try {
|
|
264
|
-
|
|
272
|
+
const timeout = timeoutPromise(TIMEOUT * 1000);
|
|
273
|
+
await Promise.race([walletToConnect.adapter.connect(), timeout.promise]);
|
|
274
|
+
clearTimeout(timeout.timeoutId);
|
|
265
275
|
} catch (error: any) {
|
|
266
276
|
// Clear the selected wallet
|
|
267
277
|
setName(null);
|
|
268
|
-
|
|
269
|
-
|
|
278
|
+
if (error === 'timeout') {
|
|
279
|
+
throw handleError(new WalletConnectionError(error));
|
|
280
|
+
} else {
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
270
283
|
} finally {
|
|
271
284
|
setConnecting(false);
|
|
272
|
-
isConnecting.current = false;
|
|
285
|
+
// isConnecting.current = false;
|
|
273
286
|
}
|
|
274
287
|
},
|
|
275
288
|
[connected, adapter, handleError, wallets, setName, wallet, account, network]
|
|
@@ -278,7 +291,6 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
278
291
|
// If autoConnect is enabled, try to connect when the adapter changes and is ready
|
|
279
292
|
useEffect(() => {
|
|
280
293
|
if (
|
|
281
|
-
isConnecting.current ||
|
|
282
294
|
connected ||
|
|
283
295
|
!autoConnect ||
|
|
284
296
|
!name ||
|
|
@@ -287,7 +299,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
287
299
|
)
|
|
288
300
|
return;
|
|
289
301
|
connect(name);
|
|
290
|
-
}, [
|
|
302
|
+
}, [connected, autoConnect, name, connect, adapter, readyState]);
|
|
291
303
|
|
|
292
304
|
// Disconnect the adapter from the wallet
|
|
293
305
|
const disconnect = useCallback(async () => {
|
|
@@ -314,7 +326,13 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
314
326
|
async (transaction: Types.TransactionPayload, option?: any) => {
|
|
315
327
|
if (!adapter) throw handleError(new WalletNotSelectedError());
|
|
316
328
|
if (!connected) throw handleError(new WalletNotConnectedError());
|
|
317
|
-
const
|
|
329
|
+
const timeout = timeoutPromise(TIMEOUT * 1000);
|
|
330
|
+
const response = await Promise.race([
|
|
331
|
+
adapter.signAndSubmitTransaction(transaction, option),
|
|
332
|
+
timeout.promise
|
|
333
|
+
]);
|
|
334
|
+
clearTimeout(timeout.timeoutId);
|
|
335
|
+
if (!response) throw handleError(new WalletSignAndSubmitMessageError('Timeout'));
|
|
318
336
|
return response;
|
|
319
337
|
},
|
|
320
338
|
[adapter, handleError, connected]
|
|
@@ -324,7 +342,14 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
324
342
|
async (transaction: Types.TransactionPayload, option?: any) => {
|
|
325
343
|
if (!adapter) throw handleError(new WalletNotSelectedError());
|
|
326
344
|
if (!connected) throw handleError(new WalletNotConnectedError());
|
|
327
|
-
|
|
345
|
+
const timeout = timeoutPromise(TIMEOUT * 1000);
|
|
346
|
+
const response = await Promise.race([
|
|
347
|
+
adapter.signTransaction(transaction, option),
|
|
348
|
+
timeout.promise
|
|
349
|
+
]);
|
|
350
|
+
clearTimeout(timeout.timeoutId);
|
|
351
|
+
if (!response) throw handleError(new WalletSignTransactionError('Timeout'));
|
|
352
|
+
return response;
|
|
328
353
|
},
|
|
329
354
|
[adapter, handleError, connected]
|
|
330
355
|
);
|
|
@@ -333,7 +358,11 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
333
358
|
async (msgPayload: string | SignMessagePayload | Uint8Array) => {
|
|
334
359
|
if (!adapter) throw handleError(new WalletNotSelectedError());
|
|
335
360
|
if (!connected) throw handleError(new WalletNotConnectedError());
|
|
336
|
-
|
|
361
|
+
const timeout = timeoutPromise(TIMEOUT * 1000);
|
|
362
|
+
const response = await Promise.race([adapter.signMessage(msgPayload), timeout.promise]);
|
|
363
|
+
clearTimeout(timeout.timeoutId);
|
|
364
|
+
if (!response) throw handleError(new WalletSignMessageError('Timeout'));
|
|
365
|
+
return response;
|
|
337
366
|
},
|
|
338
367
|
[adapter, handleError, connected]
|
|
339
368
|
);
|
package/src/utilities/util.ts
CHANGED
|
@@ -9,3 +9,16 @@ export const payloadV1ToV0 = (payload: Types.TransactionPayload) => {
|
|
|
9
9
|
arguments: v1.arguments
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
|
+
|
|
13
|
+
export const timeoutPromise = (timeout) => {
|
|
14
|
+
let timeoutId;
|
|
15
|
+
const promise: Promise<void> = new Promise((resolve, reject) => {
|
|
16
|
+
timeoutId = setTimeout(async () => {
|
|
17
|
+
reject('timeout');
|
|
18
|
+
}, timeout);
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
timeoutId,
|
|
22
|
+
promise
|
|
23
|
+
};
|
|
24
|
+
};
|