@manahippo/aptos-wallet-adapter 0.4.5 → 0.4.6
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 +4 -2
- package/dist/WalletAdapters/AptosSnap.d.ts +15 -9
- package/dist/WalletAdapters/AptosSnap.d.ts.map +1 -1
- package/dist/WalletAdapters/AptosSnap.js +41 -2
- package/dist/WalletAdapters/AptosSnap.js.map +1 -1
- package/dist/WalletAdapters/BaseAdapter.d.ts +25 -8
- package/dist/WalletAdapters/BaseAdapter.d.ts.map +1 -1
- package/dist/WalletAdapters/BaseAdapter.js +7 -1
- package/dist/WalletAdapters/BaseAdapter.js.map +1 -1
- package/dist/WalletAdapters/FewchaWallet.d.ts +11 -5
- package/dist/WalletAdapters/FewchaWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/FewchaWallet.js +54 -3
- package/dist/WalletAdapters/FewchaWallet.js.map +1 -1
- package/dist/WalletAdapters/FletchWallet.d.ts +11 -5
- package/dist/WalletAdapters/FletchWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/FletchWallet.js +41 -2
- package/dist/WalletAdapters/FletchWallet.js.map +1 -1
- package/dist/WalletAdapters/HippoExtensionWallet.d.ts +11 -6
- package/dist/WalletAdapters/HippoExtensionWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/HippoExtensionWallet.js +41 -2
- package/dist/WalletAdapters/HippoExtensionWallet.js.map +1 -1
- package/dist/WalletAdapters/HippoWallet.d.ts +11 -6
- package/dist/WalletAdapters/HippoWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/HippoWallet.js +33 -2
- package/dist/WalletAdapters/HippoWallet.js.map +1 -1
- package/dist/WalletAdapters/HyperPayWallet.d.ts +13 -8
- package/dist/WalletAdapters/HyperPayWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/HyperPayWallet.js +39 -0
- package/dist/WalletAdapters/HyperPayWallet.js.map +1 -1
- package/dist/WalletAdapters/MartianWallet.d.ts +19 -8
- package/dist/WalletAdapters/MartianWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/MartianWallet.js +63 -2
- package/dist/WalletAdapters/MartianWallet.js.map +1 -1
- package/dist/WalletAdapters/NightlyWallet.d.ts +7 -1
- package/dist/WalletAdapters/NightlyWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/NightlyWallet.js +41 -2
- package/dist/WalletAdapters/NightlyWallet.js.map +1 -1
- package/dist/WalletAdapters/PetraWallet.d.ts +25 -11
- package/dist/WalletAdapters/PetraWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/PetraWallet.js +66 -2
- package/dist/WalletAdapters/PetraWallet.js.map +1 -1
- package/dist/WalletAdapters/PontemWallet.d.ts +17 -9
- package/dist/WalletAdapters/PontemWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/PontemWallet.js +65 -2
- package/dist/WalletAdapters/PontemWallet.js.map +1 -1
- package/dist/WalletAdapters/RiseWallet.d.ts +12 -7
- package/dist/WalletAdapters/RiseWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/RiseWallet.js +41 -2
- package/dist/WalletAdapters/RiseWallet.js.map +1 -1
- package/dist/WalletAdapters/SpikaWallet.d.ts +12 -6
- package/dist/WalletAdapters/SpikaWallet.d.ts.map +1 -1
- package/dist/WalletAdapters/SpikaWallet.js +41 -2
- package/dist/WalletAdapters/SpikaWallet.js.map +1 -1
- package/dist/WalletProviders/WalletProvider.d.ts.map +1 -1
- package/dist/WalletProviders/WalletProvider.js +45 -6
- package/dist/WalletProviders/WalletProvider.js.map +1 -1
- package/dist/WalletProviders/errors.d.ts +9 -0
- package/dist/WalletProviders/errors.d.ts.map +1 -1
- package/dist/WalletProviders/errors.js +22 -1
- package/dist/WalletProviders/errors.js.map +1 -1
- package/dist/WalletProviders/useWallet.d.ts +6 -5
- package/dist/WalletProviders/useWallet.d.ts.map +1 -1
- package/dist/WalletProviders/useWallet.js.map +1 -1
- package/dist/utilities/util.d.ts +2 -2
- package/dist/utilities/util.d.ts.map +1 -1
- package/dist/utilities/util.js.map +1 -1
- package/package.json +2 -2
- package/src/WalletAdapters/AptosSnap.ts +58 -20
- package/src/WalletAdapters/BaseAdapter.ts +29 -8
- package/src/WalletAdapters/FewchaWallet.ts +72 -10
- package/src/WalletAdapters/FletchWallet.ts +226 -182
- package/src/WalletAdapters/HippoExtensionWallet.ts +50 -8
- package/src/WalletAdapters/HippoWallet.ts +51 -10
- package/src/WalletAdapters/HyperPayWallet.ts +53 -8
- package/src/WalletAdapters/MartianWallet.ts +86 -10
- package/src/WalletAdapters/NightlyWallet.ts +46 -3
- package/src/WalletAdapters/PetraWallet.ts +91 -11
- package/src/WalletAdapters/PontemWallet.ts +87 -11
- package/src/WalletAdapters/RiseWallet.ts +51 -9
- package/src/WalletAdapters/SpikaWallet.ts +54 -8
- package/src/WalletProviders/WalletProvider.tsx +57 -8
- package/src/WalletProviders/errors.ts +11 -0
- package/src/WalletProviders/useWallet.ts +6 -4
- package/src/utilities/util.ts +3 -3
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { MaybeHexString } from 'aptos';
|
|
2
|
-
import { TransactionPayload, HexEncodedBytes } from 'aptos/src/generated';
|
|
1
|
+
import { MaybeHexString, Types } from 'aptos';
|
|
3
2
|
import {
|
|
3
|
+
WalletAccountChangeError,
|
|
4
4
|
WalletDisconnectionError,
|
|
5
|
+
WalletGetNetworkError,
|
|
6
|
+
WalletNetworkChangeError,
|
|
5
7
|
WalletNotConnectedError,
|
|
6
8
|
WalletNotReadyError,
|
|
7
9
|
WalletSignAndSubmitMessageError,
|
|
@@ -11,9 +13,11 @@ import {
|
|
|
11
13
|
import {
|
|
12
14
|
AccountKeys,
|
|
13
15
|
BaseWalletAdapter,
|
|
16
|
+
NetworkInfo,
|
|
14
17
|
scopePollingDetectionStrategy,
|
|
15
18
|
SignMessagePayload,
|
|
16
19
|
SignMessageResponse,
|
|
20
|
+
WalletAdapterNetwork,
|
|
17
21
|
WalletName,
|
|
18
22
|
WalletReadyState
|
|
19
23
|
} from './BaseAdapter';
|
|
@@ -37,21 +41,24 @@ interface IPontemWallet {
|
|
|
37
41
|
publicKey(): Promise<MaybeHexString>;
|
|
38
42
|
generateTransaction(sender: MaybeHexString, payload: any): Promise<any>;
|
|
39
43
|
signAndSubmit(
|
|
40
|
-
transaction: TransactionPayload,
|
|
44
|
+
transaction: Types.TransactionPayload,
|
|
41
45
|
options?: any
|
|
42
46
|
): Promise<{
|
|
43
47
|
success: boolean;
|
|
44
48
|
result: {
|
|
45
|
-
hash: HexEncodedBytes;
|
|
49
|
+
hash: Types.HexEncodedBytes;
|
|
46
50
|
};
|
|
47
51
|
}>;
|
|
48
52
|
isConnected(): Promise<boolean>;
|
|
49
|
-
signTransaction(transaction: TransactionPayload, options?: any): Promise<Uint8Array>;
|
|
53
|
+
signTransaction(transaction: Types.TransactionPayload, options?: any): Promise<Uint8Array>;
|
|
50
54
|
signMessage(message: SignMessagePayload): Promise<{
|
|
51
55
|
success: boolean;
|
|
52
56
|
result: SignMessageResponse;
|
|
53
57
|
}>;
|
|
54
58
|
disconnect(): Promise<void>;
|
|
59
|
+
network(): Promise<NetworkInfo>;
|
|
60
|
+
onChangeAccount(listener: (address: string) => void): Promise<void>;
|
|
61
|
+
onChangeNetwork(listener: (network: NetworkInfo) => void): Promise<void>;
|
|
55
62
|
}
|
|
56
63
|
|
|
57
64
|
interface PontemWindow extends Window {
|
|
@@ -78,7 +85,12 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
78
85
|
|
|
79
86
|
protected _provider: IPontemWallet | undefined;
|
|
80
87
|
|
|
81
|
-
|
|
88
|
+
protected _network: WalletAdapterNetwork;
|
|
89
|
+
|
|
90
|
+
protected _chainId: string;
|
|
91
|
+
|
|
92
|
+
protected _api: string;
|
|
93
|
+
|
|
82
94
|
protected _timeout: number;
|
|
83
95
|
|
|
84
96
|
protected _readyState: WalletReadyState =
|
|
@@ -92,13 +104,13 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
92
104
|
|
|
93
105
|
constructor({
|
|
94
106
|
// provider,
|
|
95
|
-
// network = WalletAdapterNetwork.
|
|
107
|
+
// network = WalletAdapterNetwork.Testnet,
|
|
96
108
|
timeout = 10000
|
|
97
109
|
}: PontemWalletAdapterConfig = {}) {
|
|
98
110
|
super();
|
|
99
111
|
|
|
100
112
|
this._provider = typeof window !== 'undefined' ? window.pontem : undefined;
|
|
101
|
-
|
|
113
|
+
this._network = undefined;
|
|
102
114
|
this._timeout = timeout;
|
|
103
115
|
this._connecting = false;
|
|
104
116
|
this._wallet = null;
|
|
@@ -123,6 +135,14 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
123
135
|
};
|
|
124
136
|
}
|
|
125
137
|
|
|
138
|
+
get network(): NetworkInfo {
|
|
139
|
+
return {
|
|
140
|
+
name: this._network,
|
|
141
|
+
api: this._api,
|
|
142
|
+
chainId: this._chainId
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
126
146
|
get connecting(): boolean {
|
|
127
147
|
return this._connecting;
|
|
128
148
|
}
|
|
@@ -166,7 +186,19 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
166
186
|
publicKey,
|
|
167
187
|
isConnected: true
|
|
168
188
|
};
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
const networkInfo = await provider?.network();
|
|
192
|
+
this._network = networkInfo.name;
|
|
193
|
+
this._chainId = networkInfo.chainId;
|
|
194
|
+
this._api = networkInfo.api;
|
|
195
|
+
} catch (error: any) {
|
|
196
|
+
const errMsg = error.message;
|
|
197
|
+
this.emit('error', new WalletGetNetworkError(errMsg));
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
169
200
|
}
|
|
201
|
+
|
|
170
202
|
this.emit('connect', this._wallet?.address || '');
|
|
171
203
|
} catch (error: any) {
|
|
172
204
|
this.emit('error', new Error('User has rejected the connection'));
|
|
@@ -192,7 +224,10 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
192
224
|
this.emit('disconnect');
|
|
193
225
|
}
|
|
194
226
|
|
|
195
|
-
async signTransaction(
|
|
227
|
+
async signTransaction(
|
|
228
|
+
transactionPyld: Types.TransactionPayload,
|
|
229
|
+
options?: any
|
|
230
|
+
): Promise<Uint8Array> {
|
|
196
231
|
try {
|
|
197
232
|
const wallet = this._wallet;
|
|
198
233
|
const provider = this._provider || window.pontem;
|
|
@@ -207,9 +242,9 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
207
242
|
}
|
|
208
243
|
|
|
209
244
|
async signAndSubmitTransaction(
|
|
210
|
-
transactionPyld: TransactionPayload,
|
|
245
|
+
transactionPyld: Types.TransactionPayload,
|
|
211
246
|
options?: any
|
|
212
|
-
): Promise<{ hash: HexEncodedBytes }> {
|
|
247
|
+
): Promise<{ hash: Types.HexEncodedBytes }> {
|
|
213
248
|
try {
|
|
214
249
|
const wallet = this._wallet;
|
|
215
250
|
const provider = this._provider || window.pontem;
|
|
@@ -243,4 +278,45 @@ export class PontemWalletAdapter extends BaseWalletAdapter {
|
|
|
243
278
|
throw error;
|
|
244
279
|
}
|
|
245
280
|
}
|
|
281
|
+
|
|
282
|
+
async onAccountChange(): Promise<void> {
|
|
283
|
+
try {
|
|
284
|
+
const wallet = this._wallet;
|
|
285
|
+
const provider = this._provider || window.pontem;
|
|
286
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
287
|
+
const handleAccountChange = async (newAccount: string) => {
|
|
288
|
+
console.log('account Changed >>>', newAccount);
|
|
289
|
+
this._wallet = {
|
|
290
|
+
...this._wallet,
|
|
291
|
+
address: newAccount || this._wallet?.address
|
|
292
|
+
};
|
|
293
|
+
this.emit('accountChange', newAccount);
|
|
294
|
+
};
|
|
295
|
+
await provider?.onChangeAccount(handleAccountChange);
|
|
296
|
+
} catch (error: any) {
|
|
297
|
+
const errMsg = error.message;
|
|
298
|
+
this.emit('error', new WalletAccountChangeError(errMsg));
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async onNetworkChange(): Promise<void> {
|
|
304
|
+
try {
|
|
305
|
+
const wallet = this._wallet;
|
|
306
|
+
const provider = this._provider || window.pontem;
|
|
307
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
308
|
+
const handleNetworkChange = async (network: NetworkInfo) => {
|
|
309
|
+
console.log('network Changed >>>', network);
|
|
310
|
+
this._network = network.name;
|
|
311
|
+
this._api = network.api;
|
|
312
|
+
this._chainId = network.chainId;
|
|
313
|
+
this.emit('networkChange', this._network);
|
|
314
|
+
};
|
|
315
|
+
await provider?.onChangeNetwork(handleNetworkChange);
|
|
316
|
+
} catch (error: any) {
|
|
317
|
+
const errMsg = error.message;
|
|
318
|
+
this.emit('error', new WalletNetworkChangeError(errMsg));
|
|
319
|
+
throw error;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
246
322
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MaybeHexString, Types } from 'aptos';
|
|
2
2
|
import {
|
|
3
|
+
WalletAccountChangeError,
|
|
3
4
|
WalletDisconnectionError,
|
|
5
|
+
WalletNetworkChangeError,
|
|
4
6
|
WalletNotConnectedError,
|
|
5
7
|
WalletNotReadyError,
|
|
6
8
|
WalletSignMessageError,
|
|
@@ -9,13 +11,14 @@ import {
|
|
|
9
11
|
import {
|
|
10
12
|
AccountKeys,
|
|
11
13
|
BaseWalletAdapter,
|
|
14
|
+
NetworkInfo,
|
|
12
15
|
scopePollingDetectionStrategy,
|
|
13
16
|
SignMessagePayload,
|
|
14
17
|
SignMessageResponse,
|
|
18
|
+
WalletAdapterNetwork,
|
|
15
19
|
WalletName,
|
|
16
20
|
WalletReadyState
|
|
17
21
|
} from './BaseAdapter';
|
|
18
|
-
import { MaybeHexString } from 'aptos';
|
|
19
22
|
|
|
20
23
|
interface RiseAccount {
|
|
21
24
|
address: MaybeHexString;
|
|
@@ -28,7 +31,7 @@ interface IRiseWallet {
|
|
|
28
31
|
connect: () => Promise<{ address: string }>;
|
|
29
32
|
account(): Promise<RiseAccount>;
|
|
30
33
|
isConnected: () => Promise<boolean>;
|
|
31
|
-
signAndSubmitTransaction(transaction: any): Promise<{ hash: HexEncodedBytes }>;
|
|
34
|
+
signAndSubmitTransaction(transaction: any): Promise<{ hash: Types.HexEncodedBytes }>;
|
|
32
35
|
signTransaction(transaction: any, options?: any): Promise<Uint8Array>;
|
|
33
36
|
signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
|
|
34
37
|
disconnect(): Promise<void>;
|
|
@@ -57,7 +60,12 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
|
|
|
57
60
|
|
|
58
61
|
protected _provider: IRiseWallet | undefined;
|
|
59
62
|
|
|
60
|
-
|
|
63
|
+
protected _network: WalletAdapterNetwork;
|
|
64
|
+
|
|
65
|
+
protected _chainId: string;
|
|
66
|
+
|
|
67
|
+
protected _api: string;
|
|
68
|
+
|
|
61
69
|
protected _timeout: number;
|
|
62
70
|
|
|
63
71
|
protected _readyState: WalletReadyState =
|
|
@@ -71,13 +79,13 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
|
|
|
71
79
|
|
|
72
80
|
constructor({
|
|
73
81
|
// provider,
|
|
74
|
-
// network = WalletAdapterNetwork.
|
|
82
|
+
// network = WalletAdapterNetwork.Testnet,
|
|
75
83
|
timeout = 10000
|
|
76
84
|
}: RiseWalletAdapterConfig = {}) {
|
|
77
85
|
super();
|
|
78
86
|
|
|
79
87
|
this._provider = typeof window !== 'undefined' ? window.rise : undefined;
|
|
80
|
-
|
|
88
|
+
this._network = undefined;
|
|
81
89
|
this._timeout = timeout;
|
|
82
90
|
this._connecting = false;
|
|
83
91
|
this._wallet = null;
|
|
@@ -102,6 +110,14 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
|
|
|
102
110
|
};
|
|
103
111
|
}
|
|
104
112
|
|
|
113
|
+
get network(): NetworkInfo {
|
|
114
|
+
return {
|
|
115
|
+
name: this._network,
|
|
116
|
+
api: this._api,
|
|
117
|
+
chainId: this._chainId
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
105
121
|
get connecting(): boolean {
|
|
106
122
|
return this._connecting;
|
|
107
123
|
}
|
|
@@ -175,7 +191,7 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
|
|
|
175
191
|
this.emit('disconnect');
|
|
176
192
|
}
|
|
177
193
|
|
|
178
|
-
async signTransaction(transaction: TransactionPayload): Promise<Uint8Array> {
|
|
194
|
+
async signTransaction(transaction: Types.TransactionPayload): Promise<Uint8Array> {
|
|
179
195
|
try {
|
|
180
196
|
const wallet = this._wallet;
|
|
181
197
|
const provider = this._provider || window.rise;
|
|
@@ -195,8 +211,8 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
|
|
|
195
211
|
}
|
|
196
212
|
|
|
197
213
|
async signAndSubmitTransaction(
|
|
198
|
-
transaction: TransactionPayload
|
|
199
|
-
): Promise<{ hash: HexEncodedBytes }> {
|
|
214
|
+
transaction: Types.TransactionPayload
|
|
215
|
+
): Promise<{ hash: Types.HexEncodedBytes }> {
|
|
200
216
|
try {
|
|
201
217
|
const wallet = this._wallet;
|
|
202
218
|
const provider = this._provider || window.rise;
|
|
@@ -235,4 +251,30 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
|
|
|
235
251
|
throw error;
|
|
236
252
|
}
|
|
237
253
|
}
|
|
254
|
+
|
|
255
|
+
async onAccountChange(): Promise<void> {
|
|
256
|
+
try {
|
|
257
|
+
const wallet = this._wallet;
|
|
258
|
+
const provider = this._provider || window.rise;
|
|
259
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
260
|
+
//To be implemented
|
|
261
|
+
} catch (error: any) {
|
|
262
|
+
const errMsg = error.message;
|
|
263
|
+
this.emit('error', new WalletAccountChangeError(errMsg));
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async onNetworkChange(): Promise<void> {
|
|
269
|
+
try {
|
|
270
|
+
const wallet = this._wallet;
|
|
271
|
+
const provider = this._provider || window.rise;
|
|
272
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
273
|
+
//To be implemented
|
|
274
|
+
} catch (error: any) {
|
|
275
|
+
const errMsg = error.message;
|
|
276
|
+
this.emit('error', new WalletNetworkChangeError(errMsg));
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
238
280
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Types } from 'aptos';
|
|
2
2
|
import {
|
|
3
|
+
WalletAccountChangeError,
|
|
3
4
|
WalletDisconnectionError,
|
|
5
|
+
WalletNetworkChangeError,
|
|
4
6
|
WalletNotConnectedError,
|
|
5
7
|
WalletNotReadyError,
|
|
6
8
|
WalletSignAndSubmitMessageError,
|
|
@@ -10,7 +12,9 @@ import {
|
|
|
10
12
|
import {
|
|
11
13
|
AccountKeys,
|
|
12
14
|
BaseWalletAdapter,
|
|
15
|
+
NetworkInfo,
|
|
13
16
|
scopePollingDetectionStrategy,
|
|
17
|
+
WalletAdapterNetwork,
|
|
14
18
|
WalletName,
|
|
15
19
|
WalletReadyState
|
|
16
20
|
} from './BaseAdapter';
|
|
@@ -19,7 +23,10 @@ interface ISpikaWallet {
|
|
|
19
23
|
connect: () => Promise<{ publicKey: string; account: string; authKey: string }>;
|
|
20
24
|
account: () => Promise<string>;
|
|
21
25
|
isConnected: () => Promise<boolean>;
|
|
22
|
-
signAndSubmitTransaction(
|
|
26
|
+
signAndSubmitTransaction(
|
|
27
|
+
transaction: any,
|
|
28
|
+
options?: any
|
|
29
|
+
): Promise<{ hash: Types.HexEncodedBytes }>;
|
|
23
30
|
signTransaction(transaction: any, options?: any): Promise<Uint8Array>;
|
|
24
31
|
signMessage(message: string): Promise<string>;
|
|
25
32
|
disconnect(): Promise<void>;
|
|
@@ -48,7 +55,12 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
48
55
|
|
|
49
56
|
protected _provider: ISpikaWallet | undefined;
|
|
50
57
|
|
|
51
|
-
|
|
58
|
+
protected _network: WalletAdapterNetwork;
|
|
59
|
+
|
|
60
|
+
protected _chainId: string;
|
|
61
|
+
|
|
62
|
+
protected _api: string;
|
|
63
|
+
|
|
52
64
|
protected _timeout: number;
|
|
53
65
|
|
|
54
66
|
protected _readyState: WalletReadyState =
|
|
@@ -62,13 +74,13 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
62
74
|
|
|
63
75
|
constructor({
|
|
64
76
|
// provider,
|
|
65
|
-
// network = WalletAdapterNetwork.
|
|
77
|
+
// network = WalletAdapterNetwork.Testnet,
|
|
66
78
|
timeout = 10000
|
|
67
79
|
}: SpikaWalletAdapterConfig = {}) {
|
|
68
80
|
super();
|
|
69
81
|
|
|
70
82
|
this._provider = typeof window !== 'undefined' ? window.spika : undefined;
|
|
71
|
-
|
|
83
|
+
this._network = undefined;
|
|
72
84
|
this._timeout = timeout;
|
|
73
85
|
this._connecting = false;
|
|
74
86
|
this._wallet = null;
|
|
@@ -93,6 +105,14 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
93
105
|
};
|
|
94
106
|
}
|
|
95
107
|
|
|
108
|
+
get network(): NetworkInfo {
|
|
109
|
+
return {
|
|
110
|
+
name: this._network,
|
|
111
|
+
api: this._api,
|
|
112
|
+
chainId: this._chainId
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
96
116
|
get connecting(): boolean {
|
|
97
117
|
return this._connecting;
|
|
98
118
|
}
|
|
@@ -166,7 +186,7 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
166
186
|
this.emit('disconnect');
|
|
167
187
|
}
|
|
168
188
|
|
|
169
|
-
async signTransaction(transaction: TransactionPayload, options?: any): Promise<Uint8Array> {
|
|
189
|
+
async signTransaction(transaction: Types.TransactionPayload, options?: any): Promise<Uint8Array> {
|
|
170
190
|
try {
|
|
171
191
|
const wallet = this._wallet;
|
|
172
192
|
const provider = this._provider || window.spika;
|
|
@@ -186,9 +206,9 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
186
206
|
}
|
|
187
207
|
|
|
188
208
|
async signAndSubmitTransaction(
|
|
189
|
-
transaction: TransactionPayload,
|
|
209
|
+
transaction: Types.TransactionPayload,
|
|
190
210
|
options?: any
|
|
191
|
-
): Promise<{ hash: HexEncodedBytes }> {
|
|
211
|
+
): Promise<{ hash: Types.HexEncodedBytes }> {
|
|
192
212
|
try {
|
|
193
213
|
const wallet = this._wallet;
|
|
194
214
|
const provider = this._provider || window.spika;
|
|
@@ -224,4 +244,30 @@ export class SpikaWalletAdapter extends BaseWalletAdapter {
|
|
|
224
244
|
throw error;
|
|
225
245
|
}
|
|
226
246
|
}
|
|
247
|
+
|
|
248
|
+
async onAccountChange(): Promise<void> {
|
|
249
|
+
try {
|
|
250
|
+
const wallet = this._wallet;
|
|
251
|
+
const provider = this._provider || window.spika;
|
|
252
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
253
|
+
//To be implemented
|
|
254
|
+
} catch (error: any) {
|
|
255
|
+
const errMsg = error.message;
|
|
256
|
+
this.emit('error', new WalletAccountChangeError(errMsg));
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async onNetworkChange(): Promise<void> {
|
|
262
|
+
try {
|
|
263
|
+
const wallet = this._wallet;
|
|
264
|
+
const provider = this._provider || window.spika;
|
|
265
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
|
266
|
+
//To be implemented
|
|
267
|
+
} catch (error: any) {
|
|
268
|
+
const errMsg = error.message;
|
|
269
|
+
this.emit('error', new WalletNetworkChangeError(errMsg));
|
|
270
|
+
throw error;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
227
273
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FC, ReactNode, useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { Types } from 'aptos';
|
|
3
3
|
import {
|
|
4
4
|
WalletError,
|
|
5
5
|
WalletNotConnectedError,
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { useLocalStorage } from '../hooks/useLocalStorage';
|
|
10
10
|
import {
|
|
11
11
|
AccountKeys,
|
|
12
|
+
NetworkInfo,
|
|
12
13
|
SignMessagePayload,
|
|
13
14
|
WalletAdapter,
|
|
14
15
|
WalletName,
|
|
@@ -29,11 +30,13 @@ const initialState: {
|
|
|
29
30
|
adapter: WalletAdapter | null;
|
|
30
31
|
account: AccountKeys | null;
|
|
31
32
|
connected: boolean;
|
|
33
|
+
network: NetworkInfo | null;
|
|
32
34
|
} = {
|
|
33
35
|
wallet: null,
|
|
34
36
|
adapter: null,
|
|
35
37
|
account: null,
|
|
36
|
-
connected: false
|
|
38
|
+
connected: false,
|
|
39
|
+
network: null
|
|
37
40
|
};
|
|
38
41
|
|
|
39
42
|
export const WalletProvider: FC<WalletProviderProps> = ({
|
|
@@ -44,7 +47,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
44
47
|
localStorageKey = 'walletName'
|
|
45
48
|
}) => {
|
|
46
49
|
const [name, setName] = useLocalStorage<WalletName | null>(localStorageKey, null);
|
|
47
|
-
const [{ wallet, adapter, account, connected }, setState] = useState(initialState);
|
|
50
|
+
const [{ wallet, adapter, account, connected, network }, setState] = useState(initialState);
|
|
48
51
|
const readyState = adapter?.readyState || WalletReadyState.Unsupported;
|
|
49
52
|
const [connecting, setConnecting] = useState(false);
|
|
50
53
|
const [disconnecting, setDisconnecting] = useState(false);
|
|
@@ -110,7 +113,8 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
110
113
|
wallet: selectedWallet,
|
|
111
114
|
adapter: selectedWallet.adapter,
|
|
112
115
|
connected: selectedWallet.adapter.connected,
|
|
113
|
-
account: selectedWallet.adapter.publicAccount
|
|
116
|
+
account: selectedWallet.adapter.publicAccount,
|
|
117
|
+
network: selectedWallet.adapter.network
|
|
114
118
|
});
|
|
115
119
|
} else {
|
|
116
120
|
setState(initialState);
|
|
@@ -137,6 +141,31 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
137
141
|
return {
|
|
138
142
|
...state,
|
|
139
143
|
connected: adapter.connected,
|
|
144
|
+
account: adapter.publicAccount,
|
|
145
|
+
network: adapter.network
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}, [adapter]);
|
|
149
|
+
|
|
150
|
+
// Handle the adapter's network event
|
|
151
|
+
const handleNetworkChange = useCallback(() => {
|
|
152
|
+
if (!adapter) return;
|
|
153
|
+
console.log('adapter: handleNetworkChange', adapter.network);
|
|
154
|
+
setState((state) => {
|
|
155
|
+
return {
|
|
156
|
+
...state,
|
|
157
|
+
network: adapter.network
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
}, [adapter]);
|
|
161
|
+
|
|
162
|
+
// Handle the adapter's account event
|
|
163
|
+
const handleAccountChange = useCallback(() => {
|
|
164
|
+
if (!adapter) return;
|
|
165
|
+
console.log('adapter: handleAccountChange', adapter.publicAccount);
|
|
166
|
+
setState((state) => {
|
|
167
|
+
return {
|
|
168
|
+
...state,
|
|
140
169
|
account: adapter.publicAccount
|
|
141
170
|
};
|
|
142
171
|
});
|
|
@@ -158,19 +187,38 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
158
187
|
[isUnloading, onError]
|
|
159
188
|
);
|
|
160
189
|
|
|
190
|
+
// Listen on the adapter's network/account changes
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
if (adapter && connected) {
|
|
193
|
+
adapter.onAccountChange();
|
|
194
|
+
adapter.onNetworkChange();
|
|
195
|
+
}
|
|
196
|
+
}, [adapter, connected]);
|
|
197
|
+
|
|
161
198
|
// Setup and teardown event listeners when the adapter changes
|
|
162
199
|
useEffect(() => {
|
|
163
200
|
if (adapter) {
|
|
164
201
|
adapter.on('connect', handleConnect);
|
|
202
|
+
adapter.on('networkChange', handleNetworkChange);
|
|
203
|
+
adapter.on('accountChange', handleAccountChange);
|
|
165
204
|
adapter.on('disconnect', handleDisconnect);
|
|
166
205
|
adapter.on('error', handleError);
|
|
167
206
|
return () => {
|
|
168
207
|
adapter.off('connect', handleConnect);
|
|
208
|
+
adapter.off('networkChange', handleNetworkChange);
|
|
209
|
+
adapter.off('accountChange', handleAccountChange);
|
|
169
210
|
adapter.off('disconnect', handleDisconnect);
|
|
170
211
|
adapter.off('error', handleError);
|
|
171
212
|
};
|
|
172
213
|
}
|
|
173
|
-
}, [
|
|
214
|
+
}, [
|
|
215
|
+
adapter,
|
|
216
|
+
handleAccountChange,
|
|
217
|
+
handleConnect,
|
|
218
|
+
handleDisconnect,
|
|
219
|
+
handleError,
|
|
220
|
+
handleNetworkChange
|
|
221
|
+
]);
|
|
174
222
|
|
|
175
223
|
// When the adapter changes, disconnect the old one
|
|
176
224
|
useEffect(() => {
|
|
@@ -265,7 +313,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
265
313
|
|
|
266
314
|
// Send a transaction using the provided connection
|
|
267
315
|
const signAndSubmitTransaction = useCallback(
|
|
268
|
-
async (transaction: TransactionPayload, option?: any) => {
|
|
316
|
+
async (transaction: Types.TransactionPayload, option?: any) => {
|
|
269
317
|
if (!adapter) throw handleError(new WalletNotSelectedError());
|
|
270
318
|
if (!connected) throw handleError(new WalletNotConnectedError());
|
|
271
319
|
const response = await adapter.signAndSubmitTransaction(transaction, option);
|
|
@@ -275,7 +323,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
275
323
|
);
|
|
276
324
|
|
|
277
325
|
const signTransaction = useCallback(
|
|
278
|
-
async (transaction: TransactionPayload, option?: any) => {
|
|
326
|
+
async (transaction: Types.TransactionPayload, option?: any) => {
|
|
279
327
|
if (!adapter) throw handleError(new WalletNotSelectedError());
|
|
280
328
|
if (!connected) throw handleError(new WalletNotConnectedError());
|
|
281
329
|
return adapter.signTransaction(transaction, option);
|
|
@@ -307,7 +355,8 @@ export const WalletProvider: FC<WalletProviderProps> = ({
|
|
|
307
355
|
disconnect,
|
|
308
356
|
signAndSubmitTransaction,
|
|
309
357
|
signTransaction,
|
|
310
|
-
signMessage
|
|
358
|
+
signMessage,
|
|
359
|
+
network
|
|
311
360
|
}}>
|
|
312
361
|
{children}
|
|
313
362
|
</WalletContext.Provider>
|
|
@@ -39,6 +39,17 @@ export class WalletDisconnectionError extends WalletError {
|
|
|
39
39
|
export class WalletAccountError extends WalletError {
|
|
40
40
|
name = 'WalletAccountError';
|
|
41
41
|
}
|
|
42
|
+
export class WalletGetNetworkError extends WalletError {
|
|
43
|
+
name = 'WalletGetNetworkError';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class WalletAccountChangeError extends WalletError {
|
|
47
|
+
name = 'WalletAccountChangeError';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class WalletNetworkChangeError extends WalletError {
|
|
51
|
+
name = 'WalletNetworkChangeError';
|
|
52
|
+
}
|
|
42
53
|
|
|
43
54
|
export class WalletPublicKeyError extends WalletError {
|
|
44
55
|
name = 'WalletPublicKeyError';
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Types } from 'aptos';
|
|
2
2
|
import { createContext, useContext } from 'react';
|
|
3
3
|
import {
|
|
4
4
|
AccountKeys,
|
|
5
|
+
NetworkInfo,
|
|
5
6
|
SignMessagePayload,
|
|
6
7
|
SignMessageResponse,
|
|
7
8
|
WalletAdapter,
|
|
@@ -22,14 +23,15 @@ export interface WalletContextState {
|
|
|
22
23
|
connecting: boolean;
|
|
23
24
|
connected: boolean;
|
|
24
25
|
disconnecting: boolean;
|
|
26
|
+
network: NetworkInfo;
|
|
25
27
|
select(walletName: WalletName): void;
|
|
26
28
|
connect(): Promise<void>;
|
|
27
29
|
disconnect(): Promise<void>;
|
|
28
30
|
signAndSubmitTransaction(
|
|
29
|
-
transaction: TransactionPayload,
|
|
31
|
+
transaction: Types.TransactionPayload,
|
|
30
32
|
options?: any
|
|
31
|
-
): Promise<{ hash: HexEncodedBytes }>;
|
|
32
|
-
signTransaction(transaction: TransactionPayload, options?: any): Promise<Uint8Array>;
|
|
33
|
+
): Promise<{ hash: Types.HexEncodedBytes }>;
|
|
34
|
+
signTransaction(transaction: Types.TransactionPayload, options?: any): Promise<Uint8Array>;
|
|
33
35
|
signMessage(
|
|
34
36
|
message: string | SignMessagePayload | Uint8Array
|
|
35
37
|
): Promise<SignMessageResponse | string>;
|
package/src/utilities/util.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Types } from 'aptos';
|
|
2
2
|
|
|
3
|
-
export const payloadV1ToV0 = (payload: TransactionPayload) => {
|
|
4
|
-
const v1 = payload as TransactionPayload_EntryFunctionPayload;
|
|
3
|
+
export const payloadV1ToV0 = (payload: Types.TransactionPayload) => {
|
|
4
|
+
const v1 = payload as Types.TransactionPayload_EntryFunctionPayload;
|
|
5
5
|
return {
|
|
6
6
|
type: 'script_function_payload',
|
|
7
7
|
function: v1.function,
|