@manahippo/aptos-wallet-adapter 1.0.5 → 1.0.7

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.
@@ -0,0 +1,350 @@
1
+
2
+
3
+ import { MaybeHexString, Types } from 'aptos';
4
+ import {
5
+ WalletAccountChangeError,
6
+ WalletDisconnectionError,
7
+ WalletGetNetworkError,
8
+ WalletNetworkChangeError,
9
+ WalletNotConnectedError,
10
+ WalletNotReadyError,
11
+ WalletSignAndSubmitMessageError,
12
+ WalletSignMessageError,
13
+ WalletSignTransactionError
14
+ } from '../WalletProviders/errors';
15
+ import {
16
+ AccountKeys,
17
+ BaseWalletAdapter,
18
+ NetworkInfo,
19
+ scopePollingDetectionStrategy,
20
+ SignMessagePayload,
21
+ SignMessageResponse,
22
+ WalletAdapterNetwork,
23
+ WalletName,
24
+ WalletReadyState
25
+ } from './BaseAdapter';
26
+ interface ConnectSpacecyAccount {
27
+ address: MaybeHexString;
28
+ method: string;
29
+ publicKey: MaybeHexString;
30
+ status: number;
31
+ }
32
+
33
+ interface SpacecyAccount {
34
+ address: MaybeHexString;
35
+ publicKey?: MaybeHexString;
36
+ authKey?: MaybeHexString;
37
+ isConnected: boolean;
38
+ }
39
+ export interface ISpacecyWallet {
40
+ checkIsConnectedAndAccount: () => Promise<{ isConnected: boolean, accountWallet: MaybeHexString }>;
41
+ connect: () => Promise<ConnectSpacecyAccount>;
42
+ account(): Promise<MaybeHexString>;
43
+ publicKey(): Promise<MaybeHexString>;
44
+ signAndSubmitTransaction(
45
+ transaction: Types.TransactionPayload,
46
+ options?: any
47
+ ): Promise<{
48
+ status: number;
49
+ data: Types.HexEncodedBytes;
50
+ method: 'signAndSubmitTransaction';
51
+ }>;
52
+ isConnected(): Promise<boolean>;
53
+ signTransaction(transaction: Types.TransactionPayload, options?: any): Promise<{
54
+ status: number;
55
+ data: Uint8Array;
56
+ method: 'signTransaction';
57
+ }>
58
+ signMessage(message: SignMessagePayload): Promise<{
59
+ status: number;
60
+ data: SignMessageResponse;
61
+ method: 'signMessage';
62
+ }>;
63
+ generateTransaction(sender: MaybeHexString, payload: any, options?: any): Promise<any>;
64
+ disconnect(): Promise<void>;
65
+ chainId(): Promise<void>;
66
+ network(): Promise<NetworkInfo>;
67
+ onAccountChange(listener: (address: string | undefined) => void): Promise<void>;
68
+ onNetworkChange(listener: (network: NetworkInfo) => void): Promise<void>;
69
+ }
70
+
71
+
72
+
73
+ interface SpacecyWindow extends Window {
74
+ spacecy?: ISpacecyWallet;
75
+ }
76
+
77
+ declare const window: SpacecyWindow;
78
+
79
+ const SpacecyWalletName = 'Spacecy' as WalletName<'Spacecy'>;
80
+
81
+ export interface SpacecyWalletAdapterConfig {
82
+ provider?: ISpacecyWallet;
83
+ // network?: WalletAdapterNetwork;
84
+ timeout?: number;
85
+ }
86
+
87
+ export class SpacecyWalletAdapter extends BaseWalletAdapter {
88
+ name = SpacecyWalletName;
89
+
90
+ url = 'https://chrome.google.com/webstore/detail/spacecy-wallet/mkchoaaiifodcflmbaphdgeidocajadp?hl=en-US';
91
+
92
+ icon = 'https://spacecywallet.com/favicon.ico';
93
+
94
+ protected _provider: ISpacecyWallet | undefined;
95
+
96
+ protected _network: WalletAdapterNetwork | undefined;
97
+
98
+ protected _chainId: string | undefined;
99
+
100
+ protected _api: string | undefined;
101
+
102
+ protected _timeout: number;
103
+
104
+ protected _readyState: WalletReadyState =
105
+ typeof window === 'undefined' || typeof document === 'undefined'
106
+ ? WalletReadyState.Unsupported
107
+ : WalletReadyState.NotDetected;
108
+
109
+ protected _connecting: boolean;
110
+
111
+ protected _wallet: SpacecyAccount | null;
112
+
113
+ constructor({
114
+ // provider,
115
+ // network = WalletAdapterNetwork.Testnet,
116
+ timeout = 10000
117
+ }: SpacecyWalletAdapterConfig = {}) {
118
+ super();
119
+
120
+ this._provider = typeof window !== 'undefined' ? window.spacecy : undefined;
121
+ this._network = undefined;
122
+ this._timeout = timeout;
123
+ this._connecting = false;
124
+ this._wallet = null;
125
+
126
+ if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
127
+ scopePollingDetectionStrategy(() => {
128
+ if (window.spacecy) {
129
+ this._readyState = WalletReadyState.Installed;
130
+ this.emit('readyStateChange', this._readyState);
131
+ return true;
132
+ }
133
+ return false;
134
+ });
135
+ }
136
+ }
137
+
138
+ get publicAccount(): AccountKeys {
139
+ return {
140
+ publicKey: this._wallet?.publicKey || null,
141
+ address: this._wallet?.address || null,
142
+ authKey: this._wallet?.authKey || null
143
+ };
144
+ }
145
+
146
+ get network(): NetworkInfo {
147
+ return {
148
+ name: this._network,
149
+ api: this._api,
150
+ chainId: this._chainId
151
+ };
152
+ }
153
+
154
+ get connecting(): boolean {
155
+ return this._connecting;
156
+ }
157
+
158
+ get connected(): boolean {
159
+ return !!this._wallet?.isConnected;
160
+ }
161
+
162
+ get readyState(): WalletReadyState {
163
+ return this._readyState;
164
+ }
165
+
166
+ async connect(): Promise<void> {
167
+ try {
168
+ if (this.connected || this.connecting) return;
169
+ if (
170
+ !(
171
+ this._readyState === WalletReadyState.Loadable ||
172
+ this._readyState === WalletReadyState.Installed
173
+ )
174
+ )
175
+ throw new WalletNotReadyError();
176
+ this._connecting = true;
177
+ const provider = this._provider || window.spacecy;
178
+ const response: any = await provider?.connect()
179
+
180
+
181
+
182
+
183
+ if (!response) {
184
+ throw new WalletNotConnectedError('No connect response');
185
+ }
186
+ const walletAccount = response.data.address;
187
+ const publicKey = response.data.publicKey;
188
+ if (walletAccount) {
189
+ this._wallet = {
190
+ address: walletAccount,
191
+ publicKey,
192
+ isConnected: true
193
+ };
194
+
195
+ try {
196
+ const networkInfo: any = await provider?.chainId();
197
+ if (networkInfo) {
198
+ this._network = networkInfo.data.networkName;
199
+ this._chainId = networkInfo.data.chainId;
200
+ this._api = networkInfo.data.rpcProvider
201
+ }
202
+ } catch (error: any) {
203
+ const errMsg = error.message;
204
+ this.emit('error', new WalletGetNetworkError(errMsg));
205
+ throw error;
206
+ }
207
+ }
208
+
209
+
210
+ this.emit('connect', this._wallet?.address || '');
211
+ } catch (error: any) {
212
+ this.emit('error', new Error('User has rejected the connection'));
213
+ throw error;
214
+ } finally {
215
+ this._connecting = false;
216
+ }
217
+ }
218
+
219
+ async disconnect(): Promise<void> {
220
+ const wallet = this._wallet;
221
+ const provider = this._provider || window.spacecy;
222
+ if (wallet) {
223
+ this._wallet = null;
224
+ try {
225
+ await provider?.disconnect();
226
+ } catch (error: any) {
227
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
228
+ }
229
+ }
230
+ this.emit('disconnect');
231
+ }
232
+
233
+ async signTransaction(
234
+ transactionPyld: Types.TransactionPayload,
235
+ options?: any
236
+ ): Promise<Uint8Array> {
237
+ try {
238
+ const wallet = this._wallet;
239
+ const provider = this._provider || window.spacecy;
240
+ if (!wallet || !provider) throw new WalletNotConnectedError();
241
+ const tx = await provider.generateTransaction(wallet.address || '', transactionPyld, options);
242
+ if (!tx) throw new Error('Cannot generate transaction');
243
+ const response = await provider?.signTransaction(tx.data);
244
+ if (!response) {
245
+ throw new Error('No response');
246
+ }
247
+ return response.data;
248
+ } catch (error: any) {
249
+ this.emit('error', new WalletSignTransactionError(error));
250
+ throw error;
251
+ }
252
+ }
253
+ async signAndSubmitTransaction(
254
+ transactionPyld: Types.TransactionPayload,
255
+ options?: any
256
+ ): Promise<{ hash: Types.HexEncodedBytes }> {
257
+ try {
258
+ const wallet = this._wallet;
259
+ const provider = this._provider || window.spacecy;
260
+ if (!wallet || !provider) throw new WalletNotConnectedError();
261
+ const response = await provider?.signAndSubmitTransaction(transactionPyld, options);
262
+
263
+
264
+ if (!response || response.status != 200) {
265
+ throw new Error('No response');
266
+ }
267
+ return { hash: response.data };
268
+ } catch (error: any) {
269
+ this.emit('error', new WalletSignAndSubmitMessageError(error.message));
270
+ throw error;
271
+ }
272
+ }
273
+
274
+
275
+ async signMessage(msgPayload: SignMessagePayload): Promise<SignMessageResponse> {
276
+ try {
277
+ const wallet = this._wallet;
278
+ const provider = this._provider || window.spacecy;
279
+ if (!wallet || !provider) throw new WalletNotConnectedError();
280
+ if (typeof msgPayload !== 'object' || !msgPayload.nonce) {
281
+ throw new WalletSignMessageError('Invalid signMessage Payload');
282
+ }
283
+ const response = await provider?.signMessage(msgPayload);
284
+ if (response.status == 200) {
285
+ return response.data;
286
+ } else {
287
+ throw new Error('Sign Message failed');
288
+ }
289
+ } catch (error: any) {
290
+ const errMsg = error.message;
291
+ this.emit('error', new WalletSignMessageError(errMsg));
292
+ throw error;
293
+ }
294
+ }
295
+
296
+ async onAccountChange(): Promise<void> {
297
+ try {
298
+ const wallet = this._wallet;
299
+ const provider = this._provider || window.spacecy;
300
+ if (!wallet || !provider) throw new WalletNotConnectedError();
301
+ const handleAccountChange = async (newAccount: any) => {
302
+ // disconnect wallet if newAccount is undefined
303
+ if (newAccount.data == 'no account') {
304
+ if (this.connected) {
305
+ await this.disconnect();
306
+ }
307
+ return;
308
+ }
309
+ if (newAccount.data == "") {
310
+ this._wallet = { publicKey: "", address: "", authKey: "", isConnected: false }
311
+ }
312
+ // const newPublicKey = await provider?.publicKey();
313
+ if (this._wallet != null) {
314
+ this._wallet = {
315
+ ...this._wallet,
316
+ address: newAccount.data == 'no account' ? undefined : newAccount.data,
317
+ publicKey: undefined
318
+ };
319
+ }
320
+ this.emit('accountChange', newAccount);
321
+ };
322
+ await provider?.onAccountChange(handleAccountChange);
323
+ } catch (error: any) {
324
+ const errMsg = error.message;
325
+ this.emit('error', new WalletAccountChangeError(errMsg));
326
+ throw error;
327
+ }
328
+ }
329
+
330
+ async onNetworkChange(): Promise<void> {
331
+ try {
332
+ const wallet = this._wallet;
333
+ const provider = this._provider || window.spacecy;
334
+ if (!wallet || !provider) throw new WalletNotConnectedError();
335
+ const handleNetworkChange = (network: any) => {
336
+ this._network = network.data.networkName;
337
+ this._api = network.data.rpcProvider
338
+ this._chainId = network.data.chainId;
339
+ if (this._network) {
340
+ this.emit('networkChange', this._network);
341
+ }
342
+ };
343
+ await provider?.onNetworkChange(handleNetworkChange);
344
+ } catch (error: any) {
345
+ const errMsg = error.message;
346
+ this.emit('error', new WalletNetworkChangeError(errMsg));
347
+ throw error;
348
+ }
349
+ }
350
+ }
@@ -18,6 +18,7 @@ export * from './BloctoWallet';
18
18
  export * from './Coin98Wallet';
19
19
  export * from './SafePalWallet';
20
20
  export * from './FoxWallet';
21
- // export * from './MsafeWallet';
21
+ export * from './MsafeWallet';
22
22
  export * from './OpenBlockWallet';
23
23
  export * from './CloverWallet';
24
+ export * from './SpacecyWallet';