@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.
@@ -1,261 +1,262 @@
1
- // import { HexString, MaybeHexString, Types } from 'aptos';
2
- // import {
3
- // WalletAccountChangeError,
4
- // WalletDisconnectionError,
5
- // WalletGetNetworkError,
6
- // WalletNetworkChangeError,
7
- // WalletNotConnectedError,
8
- // WalletNotReadyError,
9
- // WalletSignAndSubmitMessageError,
10
- // WalletSignMessageError,
11
- // WalletSignTransactionError
12
- // } from '../WalletProviders/errors';
13
- // import {
14
- // AccountKeys,
15
- // BaseWalletAdapter,
16
- // NetworkInfo,
17
- // SignMessagePayload,
18
- // SignMessageResponse,
19
- // WalletAdapterNetwork,
20
- // WalletName,
21
- // WalletReadyState
22
- // } from './BaseAdapter';
23
- // import { Account, MsafeWallet } from 'msafe-iframe';
24
-
25
- // export const MsafeWalletName = 'Msafe' as WalletName<'Msafe'>;
26
-
27
- // interface MsafeAccount {
28
- // address: MaybeHexString;
29
- // publicKey: MaybeHexString;
30
- // authKey: MaybeHexString;
31
- // isConnected: boolean;
32
- // }
33
-
34
- // export class MsafeWalletAdapter extends BaseWalletAdapter {
35
- // name = MsafeWalletName;
36
-
37
- // url = 'https://app.m-safe.io';
38
-
39
- // icon = 'https://raw.githubusercontent.com/hippospace/aptos-wallet-adapter/main/logos/msafe.png';
40
-
41
- // protected _provider: MsafeWallet | undefined;
42
-
43
- // protected _network: WalletAdapterNetwork;
44
-
45
- // protected _chainId: string;
46
-
47
- // protected _api: string;
48
-
49
- // protected _readyState: WalletReadyState =
50
- // typeof window === 'undefined' || typeof document === 'undefined'
51
- // ? WalletReadyState.Unsupported
52
- // : WalletReadyState.NotDetected;
53
-
54
- // protected _connecting: boolean;
55
-
56
- // protected _wallet: MsafeAccount | null;
57
-
58
- // constructor(origin?: string) {
59
- // super();
60
-
61
- // this._network = undefined;
62
- // this._connecting = false;
63
- // MsafeWallet.new(origin)
64
- // .then((msafe) => {
65
- // this._provider = msafe;
66
- // this._readyState = WalletReadyState.Installed;
67
- // this.emit('readyStateChange', this._readyState);
68
- // })
69
- // .catch((e) => console.log(e));
70
- // }
71
-
72
- // get publicAccount(): AccountKeys {
73
- // return {
74
- // publicKey: this._wallet?.publicKey || null,
75
- // address: this._wallet?.address || null,
76
- // authKey: this._wallet?.authKey || null
77
- // };
78
- // }
79
-
80
- // get network(): NetworkInfo {
81
- // return {
82
- // name: this._network,
83
- // chainId: this._chainId
84
- // };
85
- // }
86
-
87
- // get connecting(): boolean {
88
- // return this._connecting;
89
- // }
90
-
91
- // get connected(): boolean {
92
- // return !!this._wallet?.isConnected;
93
- // }
94
-
95
- // get readyState(): WalletReadyState {
96
- // return this._readyState;
97
- // }
98
-
99
- // async connect(): Promise<void> {
100
- // try {
101
- // if (this.connected || this.connecting) return;
102
- // if (
103
- // !(
104
- // this._readyState === WalletReadyState.Loadable ||
105
- // this._readyState === WalletReadyState.Installed
106
- // )
107
- // )
108
- // throw new WalletNotReadyError();
109
- // this._connecting = true;
110
-
111
- // const provider = this._provider;
112
- // const isConnected = await provider?.isConnected();
113
- // if (isConnected) {
114
- // await provider?.disconnect();
115
- // }
116
- // const response = await provider?.connect();
117
-
118
- // if (!response) {
119
- // throw new WalletNotConnectedError('No connect response');
120
- // }
121
-
122
- // const walletAccount = await provider?.account();
123
- // if (walletAccount) {
124
- // this._wallet = {
125
- // ...walletAccount,
126
- // isConnected: true
127
- // } as any;
128
-
129
- // try {
130
- // const name = await provider?.network();
131
- // const chainId = await provider?.chainId();
132
-
133
- // this._network = name as WalletAdapterNetwork;
134
- // this._chainId = chainId.toString();
135
- // } catch (error: any) {
136
- // const errMsg = error.message;
137
- // this.emit('error', new WalletGetNetworkError(errMsg));
138
- // throw error;
139
- // }
140
- // }
141
- // this.emit('connect', this._wallet?.address || '');
142
- // } catch (error: any) {
143
- // this.emit('error', new Error(error));
144
- // throw error;
145
- // } finally {
146
- // this._connecting = false;
147
- // }
148
- // }
149
-
150
- // async disconnect(): Promise<void> {
151
- // const wallet = this._wallet;
152
- // const provider = this._provider;
153
- // if (wallet) {
154
- // this._wallet = null;
155
-
156
- // try {
157
- // await provider?.disconnect();
158
- // } catch (error: any) {
159
- // this.emit('error', new WalletDisconnectionError(error?.message, error));
160
- // }
161
- // }
162
-
163
- // this.emit('disconnect');
164
- // }
165
-
166
- // async signTransaction(
167
- // transactionPyld: Types.TransactionPayload,
168
- // options?: any
169
- // ): Promise<Uint8Array> {
170
- // try {
171
- // const wallet = this._wallet;
172
- // const provider = this._provider;
173
- // if (!wallet || !provider) throw new WalletNotConnectedError();
174
- // const response = await provider.signTransaction(transactionPyld as any, options);
175
-
176
- // if (!response) {
177
- // throw new Error('No response');
178
- // }
179
- // return response;
180
- // } catch (error: any) {
181
- // this.emit('error', new WalletSignTransactionError(error));
182
- // throw error;
183
- // }
184
- // }
185
-
186
- // async signAndSubmitTransaction(
187
- // transactionPyld: Types.TransactionPayload,
188
- // options?: any
189
- // ): Promise<{ hash: Types.HexEncodedBytes }> {
190
- // try {
191
- // const wallet = this._wallet;
192
- // const provider = this._provider;
193
- // if (!wallet || !provider) throw new WalletNotConnectedError();
194
- // const response = await provider.signAndSubmit(transactionPyld as any, options);
195
-
196
- // if (!response) {
197
- // throw new Error('No response');
198
- // }
199
- // return { hash: HexString.fromUint8Array(response).hex() };
200
- // } catch (error: any) {
201
- // this.emit('error', new WalletSignAndSubmitMessageError(error));
202
- // throw error;
203
- // }
204
- // }
205
-
206
- // async signMessage(msgPayload: SignMessagePayload): Promise<SignMessageResponse> {
207
- // try {
208
- // const wallet = this._wallet;
209
- // const provider = this._provider;
210
- // if (!wallet || !provider) throw new WalletNotConnectedError();
211
- // const response = await provider.signMessage(msgPayload as any);
212
- // if (response) {
213
- // return response as any;
214
- // } else {
215
- // throw new Error('Sign Message failed');
216
- // }
217
- // } catch (error: any) {
218
- // const errMsg = error.message;
219
- // this.emit('error', new WalletSignMessageError(errMsg));
220
- // throw error;
221
- // }
222
- // }
223
-
224
- // async onAccountChange(): Promise<void> {
225
- // try {
226
- // const wallet = this._wallet;
227
- // const provider = this._provider;
228
- // if (!wallet || !provider) throw new WalletNotConnectedError();
229
- // const handleChangeAccount = async (newAccount: Account) => {
230
- // this._wallet = {
231
- // ...this._wallet,
232
- // ...newAccount
233
- // };
234
- // this.emit('accountChange', newAccount.address);
235
- // };
236
- // provider.onChangeAccount(handleChangeAccount);
237
- // } catch (error: any) {
238
- // const errMsg = error.message;
239
- // this.emit('error', new WalletAccountChangeError(errMsg));
240
- // throw error;
241
- // }
242
- // }
243
-
244
- // async onNetworkChange(): Promise<void> {
245
- // try {
246
- // const wallet = this._wallet;
247
- // const provider = this._provider;
248
- // if (!wallet || !provider) throw new WalletNotConnectedError();
249
- // const handleNetworkChange = async (newNetwork: WalletAdapterNetwork) => {
250
- // this._network = newNetwork;
251
- // this._chainId = (await this._provider.chainId()).toString();
252
- // this.emit('networkChange', this._network);
253
- // };
254
- // provider.onChangeNetwork(handleNetworkChange);
255
- // } catch (error: any) {
256
- // const errMsg = error.message;
257
- // this.emit('error', new WalletNetworkChangeError(errMsg));
258
- // throw error;
259
- // }
260
- // }
261
- // }
1
+ import { HexString, MaybeHexString, Types } from 'aptos';
2
+ import {
3
+ WalletAccountChangeError,
4
+ WalletDisconnectionError,
5
+ WalletGetNetworkError,
6
+ WalletNetworkChangeError,
7
+ WalletNotConnectedError,
8
+ WalletNotReadyError,
9
+ WalletSignAndSubmitMessageError,
10
+ WalletSignMessageError,
11
+ WalletSignTransactionError
12
+ } from '../WalletProviders/errors';
13
+ import {
14
+ AccountKeys,
15
+ BaseWalletAdapter,
16
+ NetworkInfo,
17
+ SignMessagePayload,
18
+ SignMessageResponse,
19
+ WalletAdapterNetwork,
20
+ WalletName,
21
+ WalletReadyState
22
+ } from './BaseAdapter';
23
+ import { Account, MsafeWallet } from 'msafe-wallet';
24
+
25
+ export const MsafeWalletName = 'Msafe' as WalletName<'Msafe'>;
26
+
27
+ interface MsafeAccount {
28
+ address: MaybeHexString;
29
+ publicKey: MaybeHexString;
30
+ authKey: MaybeHexString;
31
+ isConnected: boolean;
32
+ }
33
+
34
+ export class MsafeWalletAdapter extends BaseWalletAdapter {
35
+ name = MsafeWalletName;
36
+
37
+ url = MsafeWallet.getOrigin();
38
+
39
+ icon = 'https://raw.githubusercontent.com/hippospace/aptos-wallet-adapter/main/logos/msafe.png';
40
+
41
+ protected _provider: MsafeWallet | undefined;
42
+
43
+ protected _network: WalletAdapterNetwork;
44
+
45
+ protected _chainId: string;
46
+
47
+ // MsafeWallet only works in msafe appstore iframe
48
+ protected _readyState: WalletReadyState = MsafeWallet.inMsafeWallet()
49
+ ? WalletReadyState.NotDetected
50
+ : WalletReadyState.Unsupported;
51
+
52
+ protected _connecting: boolean;
53
+
54
+ protected _wallet: MsafeAccount | null;
55
+
56
+ constructor(origin: 'Mainnet' | 'Testnet' | string = 'Mainnet') {
57
+ super();
58
+ this._network = undefined;
59
+ this._connecting = false;
60
+ const msafeOrigin = MsafeWallet.getOrigin(origin);
61
+ this.url = MsafeWallet.getAppUrl(origin);
62
+ if (this._readyState === WalletReadyState.NotDetected) {
63
+ MsafeWallet.new(msafeOrigin)
64
+ .then((msafe) => {
65
+ this._provider = msafe;
66
+ this._readyState = WalletReadyState.Installed;
67
+ this.emit('readyStateChange', this._readyState);
68
+ })
69
+ .catch((e) => console.log(e));
70
+ }
71
+ }
72
+
73
+ get publicAccount(): AccountKeys {
74
+ return {
75
+ publicKey: this._wallet?.publicKey || null,
76
+ address: this._wallet?.address || null,
77
+ authKey: this._wallet?.authKey || null
78
+ };
79
+ }
80
+
81
+ get network(): NetworkInfo {
82
+ return {
83
+ name: this._network,
84
+ chainId: this._chainId
85
+ };
86
+ }
87
+
88
+ get connecting(): boolean {
89
+ return this._connecting;
90
+ }
91
+
92
+ get connected(): boolean {
93
+ return !!this._wallet?.isConnected;
94
+ }
95
+
96
+ get readyState(): WalletReadyState {
97
+ return this._readyState;
98
+ }
99
+
100
+ async connect(): Promise<void> {
101
+ try {
102
+ if (this.connected || this.connecting) return;
103
+ if (
104
+ !(
105
+ this._readyState === WalletReadyState.Loadable ||
106
+ this._readyState === WalletReadyState.Installed
107
+ )
108
+ )
109
+ throw new WalletNotReadyError();
110
+ this._connecting = true;
111
+
112
+ const provider = this._provider;
113
+ const isConnected = await provider?.isConnected();
114
+ if (isConnected) {
115
+ await provider?.disconnect();
116
+ }
117
+ const response = await provider?.connect();
118
+
119
+ if (!response) {
120
+ throw new WalletNotConnectedError('No connect response');
121
+ }
122
+
123
+ const walletAccount = await provider?.account();
124
+ if (walletAccount) {
125
+ this._wallet = {
126
+ ...walletAccount,
127
+ isConnected: true
128
+ } as any;
129
+
130
+ try {
131
+ const name = await provider?.network();
132
+ const chainId = await provider?.chainId();
133
+
134
+ this._network = name as WalletAdapterNetwork;
135
+ this._chainId = chainId.toString();
136
+ } catch (error: any) {
137
+ const errMsg = error.message;
138
+ this.emit('error', new WalletGetNetworkError(errMsg));
139
+ throw error;
140
+ }
141
+ }
142
+ this.emit('connect', this._wallet?.address || '');
143
+ } catch (error: any) {
144
+ this.emit('error', new Error(error));
145
+ throw error;
146
+ } finally {
147
+ this._connecting = false;
148
+ }
149
+ }
150
+
151
+ async disconnect(): Promise<void> {
152
+ const wallet = this._wallet;
153
+ const provider = this._provider;
154
+ if (wallet) {
155
+ this._wallet = null;
156
+
157
+ try {
158
+ await provider?.disconnect();
159
+ } catch (error: any) {
160
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
161
+ }
162
+ }
163
+
164
+ this.emit('disconnect');
165
+ }
166
+
167
+ async signTransaction(
168
+ transactionPyld: Types.TransactionPayload,
169
+ options?: any
170
+ ): Promise<Uint8Array> {
171
+ try {
172
+ const wallet = this._wallet;
173
+ const provider = this._provider;
174
+ if (!wallet || !provider) throw new WalletNotConnectedError();
175
+ const response = await provider.signTransaction(transactionPyld as any, options);
176
+
177
+ if (!response) {
178
+ throw new Error('No response');
179
+ }
180
+ return response;
181
+ } catch (error: any) {
182
+ this.emit('error', new WalletSignTransactionError(error));
183
+ throw error;
184
+ }
185
+ }
186
+
187
+ async signAndSubmitTransaction(
188
+ transactionPyld: Types.TransactionPayload,
189
+ options?: any
190
+ ): Promise<{ hash: Types.HexEncodedBytes }> {
191
+ try {
192
+ const wallet = this._wallet;
193
+ const provider = this._provider;
194
+ if (!wallet || !provider) throw new WalletNotConnectedError();
195
+ const response = await provider.signAndSubmit(transactionPyld as any, options);
196
+
197
+ if (!response) {
198
+ throw new Error('No response');
199
+ }
200
+ return { hash: HexString.fromUint8Array(response).hex() };
201
+ } catch (error: any) {
202
+ this.emit('error', new WalletSignAndSubmitMessageError(error));
203
+ throw error;
204
+ }
205
+ }
206
+
207
+ async signMessage(msgPayload: SignMessagePayload): Promise<SignMessageResponse> {
208
+ try {
209
+ const wallet = this._wallet;
210
+ const provider = this._provider;
211
+ if (!wallet || !provider) throw new WalletNotConnectedError();
212
+ const response = await provider.signMessage(msgPayload as any);
213
+ if (response) {
214
+ return response as any;
215
+ } else {
216
+ throw new Error('Sign Message failed');
217
+ }
218
+ } catch (error: any) {
219
+ const errMsg = error.message;
220
+ this.emit('error', new WalletSignMessageError(errMsg));
221
+ throw error;
222
+ }
223
+ }
224
+
225
+ async onAccountChange(): Promise<void> {
226
+ try {
227
+ const wallet = this._wallet;
228
+ const provider = this._provider;
229
+ if (!wallet || !provider) throw new WalletNotConnectedError();
230
+ const handleChangeAccount = async (newAccount: Account) => {
231
+ this._wallet = {
232
+ ...this._wallet,
233
+ ...newAccount
234
+ };
235
+ this.emit('accountChange', newAccount.address);
236
+ };
237
+ provider.onChangeAccount(handleChangeAccount);
238
+ } catch (error: any) {
239
+ const errMsg = error.message;
240
+ this.emit('error', new WalletAccountChangeError(errMsg));
241
+ throw error;
242
+ }
243
+ }
244
+
245
+ async onNetworkChange(): Promise<void> {
246
+ try {
247
+ const wallet = this._wallet;
248
+ const provider = this._provider;
249
+ if (!wallet || !provider) throw new WalletNotConnectedError();
250
+ const handleNetworkChange = async (newNetwork: WalletAdapterNetwork) => {
251
+ this._network = newNetwork;
252
+ this._chainId = (await this._provider.chainId()).toString();
253
+ this.emit('networkChange', this._network);
254
+ };
255
+ provider.onChangeNetwork(handleNetworkChange);
256
+ } catch (error: any) {
257
+ const errMsg = error.message;
258
+ this.emit('error', new WalletNetworkChangeError(errMsg));
259
+ throw error;
260
+ }
261
+ }
262
+ }
@@ -28,8 +28,10 @@ interface RiseAccount {
28
28
  isConnected: boolean;
29
29
  }
30
30
 
31
+ type AddressInfo = { address: string; publicKey: string; authKey?: string };
32
+
31
33
  interface IRiseWallet {
32
- connect: () => Promise<{ address: string }>;
34
+ connect: () => Promise<AddressInfo>;
33
35
  account(): Promise<RiseAccount>;
34
36
  isConnected: () => Promise<boolean>;
35
37
  signAndSubmitTransaction(transaction: any): Promise<{ hash: Types.HexEncodedBytes }>;
@@ -37,6 +39,10 @@ interface IRiseWallet {
37
39
  signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
38
40
  disconnect(): Promise<void>;
39
41
  network(): Promise<NetworkInfo>;
42
+ on(event: string, listener: () => any): void;
43
+ off(event: string, listener: () => any): void;
44
+ onAccountChange: (listener: (newAddress: AddressInfo) => void) => void;
45
+ onNetworkChange: (listener: (network: NetworkInfo) => void) => void;
40
46
  }
41
47
 
42
48
  interface RiseWindow extends Window {
@@ -156,22 +162,20 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
156
162
  throw new WalletNotConnectedError('User has rejected the request');
157
163
  }
158
164
 
159
- // TODO - remove this check in the future
160
- // provider.network is still not live and we want smooth transition
161
- if (provider?.network) {
162
- try {
163
- const { chainId, api, name } = await provider.network();
164
-
165
- this._network = name;
166
- this._chainId = chainId;
167
- this._api = api;
168
- } catch (error: any) {
169
- const errMsg = error.message;
170
- this.emit('error', new WalletGetNetworkError(errMsg));
171
- throw error;
172
- }
165
+ try {
166
+ const { chainId, api, name } = await provider.network();
167
+
168
+ this._network = name;
169
+ this._chainId = chainId;
170
+ this._api = api;
171
+ } catch (error: any) {
172
+ const errMsg = error.message;
173
+ this.emit('error', new WalletGetNetworkError(errMsg));
174
+ throw error;
173
175
  }
174
176
 
177
+ provider?.on('disconnect', this.handleDisconnect);
178
+
175
179
  const account = await provider?.account();
176
180
  if (account) {
177
181
  const { publicKey, address, authKey } = account;
@@ -200,6 +204,7 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
200
204
 
201
205
  try {
202
206
  const provider = this._provider || window.rise;
207
+ provider?.off('disconnect', this.handleDisconnect);
203
208
  await provider?.disconnect();
204
209
  } catch (error: any) {
205
210
  this.emit('error', new WalletDisconnectionError(error?.message, error));
@@ -275,7 +280,18 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
275
280
  const wallet = this._wallet;
276
281
  const provider = this._provider || window.rise;
277
282
  if (!wallet || !provider) throw new WalletNotConnectedError();
278
- //To be implemented
283
+ const handleAccountChange = async (newAccount?: AddressInfo) => {
284
+ if (newAccount?.publicKey) {
285
+ this._wallet = {
286
+ ...this._wallet,
287
+ publicKey: newAccount.publicKey || this._wallet?.publicKey,
288
+ authKey: newAccount.authKey || this._wallet?.authKey,
289
+ address: newAccount.address || this._wallet?.address
290
+ };
291
+ this.emit('accountChange', newAccount?.publicKey);
292
+ }
293
+ };
294
+ await provider?.onAccountChange?.(handleAccountChange);
279
295
  } catch (error: any) {
280
296
  const errMsg = error.message;
281
297
  this.emit('error', new WalletAccountChangeError(errMsg));
@@ -288,11 +304,30 @@ export class RiseWalletAdapter extends BaseWalletAdapter {
288
304
  const wallet = this._wallet;
289
305
  const provider = this._provider || window.rise;
290
306
  if (!wallet || !provider) throw new WalletNotConnectedError();
291
- //To be implemented
307
+ const handleNetworkChange = async (newNetwork: NetworkInfo) => {
308
+ const { chainId, api, name } = newNetwork;
309
+ this._network = name;
310
+ this._chainId = chainId;
311
+ this._api = api;
312
+ this.emit('networkChange', this._network);
313
+ };
314
+ await provider?.onNetworkChange?.(handleNetworkChange);
292
315
  } catch (error: any) {
293
316
  const errMsg = error.message;
294
317
  this.emit('error', new WalletNetworkChangeError(errMsg));
295
318
  throw error;
296
319
  }
297
320
  }
321
+
322
+ private handleDisconnect = () => {
323
+ try {
324
+ const provider = this._provider || window.rise;
325
+
326
+ provider?.off('disconnect', this.handleDisconnect);
327
+
328
+ this._wallet = null;
329
+ } finally {
330
+ this.emit('disconnect');
331
+ }
332
+ };
298
333
  }