@aptos-labs/wallet-adapter-core 2.3.3 → 2.5.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @aptos-labs/wallet-adapter-core
2
2
 
3
+ ## 2.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c95933a: Update the `onNetworkChange` type interface to conform with `WalletCore`'s usage of the callback
8
+
9
+ ## 2.4.0
10
+
11
+ ### Minor Changes
12
+
13
+ - d2a0bbd: Support error message on connect and disconnect functions
14
+
15
+ ### Patch Changes
16
+
17
+ - b0586e8: Separate connect and connectWallet for SRP
18
+
3
19
  ## 2.3.3
4
20
 
5
21
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -45,6 +45,7 @@ interface AptosWalletErrorResult {
45
45
  name: string;
46
46
  message: string;
47
47
  }
48
+ declare type OnNetworkChange = (callBack: (networkInfo: NetworkInfo) => Promise<void>) => Promise<void>;
48
49
  interface PluginProvider {
49
50
  connect: () => Promise<AccountInfo>;
50
51
  account: () => Promise<AccountInfo>;
@@ -55,12 +56,10 @@ interface PluginProvider {
55
56
  signMessage: (message: SignMessagePayload) => Promise<SignMessageResponse>;
56
57
  network: () => Promise<NetworkName>;
57
58
  onAccountChange: (listener: (newAddress: AccountInfo) => Promise<void>) => Promise<void>;
58
- onNetworkChange: (listener: (network: {
59
- networkName: NetworkInfo;
60
- }) => Promise<void>) => Promise<void>;
59
+ onNetworkChange: OnNetworkChange;
61
60
  }
62
61
  interface AdapterPluginEvents {
63
- onNetworkChange(callback: any): Promise<any>;
62
+ onNetworkChange: OnNetworkChange;
64
63
  onAccountChange(callback: any): Promise<any>;
65
64
  }
66
65
  interface AdapterPluginProps<Name extends string = string> {
@@ -155,16 +154,21 @@ declare class WalletCore extends EventEmitter<WalletCoreEvents> {
155
154
  */
156
155
  get network(): NetworkInfo | null;
157
156
  /**
158
- Connects a wallet to the app. If a wallet is already connected,
159
- we first disconnect the current connected wallet and then connect the selected wallet.
160
- On connect success, we set the current account and the network, and keeping the selected wallet
161
- name in LocalStorage to support autoConnect function.
162
-
163
- @param walletName. The wallet name we want to connect as a WalletName type.
164
- @emit emits "connect" event
165
- @throws WalletConnectionError
166
- */
157
+ * We first make sure we can connect a dapp to a wallet.
158
+ * If all good, we connect the wallet by calling `this.connectWallet`
159
+ * @param walletName. The wallet name we want to connect.
160
+ */
167
161
  connect(walletName: WalletName): Promise<void | string>;
162
+ /**
163
+ * Connects a wallet to the dapp.
164
+ * On connect success, we set the current account and the network, and keeping the selected wallet
165
+ * name in LocalStorage to support autoConnect function.
166
+ *
167
+ * @param selectedWallet. The wallet we want to connect.
168
+ * @emit emits "connect" event
169
+ * @throws WalletConnectionError
170
+ */
171
+ connectWallet(selectedWallet: Wallet): Promise<void>;
168
172
  /**
169
173
  Disconnect the exisitng wallet. On success, we clear the
170
174
  current account, current network and LocalStorage data.
@@ -228,4 +232,4 @@ declare function isMobile(): boolean;
228
232
  declare function isInAppBrowser(): boolean;
229
233
  declare function isRedirectable(): boolean;
230
234
 
231
- export { AccountInfo, AdapterPlugin, AdapterPluginEvents, AdapterPluginProps, AptosWalletErrorResult, NetworkInfo, NetworkName, PluginProvider, SignMessagePayload, SignMessageResponse, TransactionOptions, Wallet, WalletCore, WalletCoreEvents, WalletInfo, WalletName, WalletReadyState, getLocalStorage, isInAppBrowser, isMobile, isRedirectable, removeLocalStorage, scopePollingDetectionStrategy, setLocalStorage };
235
+ export { AccountInfo, AdapterPlugin, AdapterPluginEvents, AdapterPluginProps, AptosWalletErrorResult, NetworkInfo, NetworkName, OnNetworkChange, PluginProvider, SignMessagePayload, SignMessageResponse, TransactionOptions, Wallet, WalletCore, WalletCoreEvents, WalletInfo, WalletName, WalletReadyState, getLocalStorage, isInAppBrowser, isMobile, isRedirectable, removeLocalStorage, scopePollingDetectionStrategy, setLocalStorage };
package/dist/index.js CHANGED
@@ -335,30 +335,32 @@ var WalletCore = class extends import_eventemitter3.default {
335
335
  }
336
336
  }
337
337
  async connect(walletName) {
338
- var _a, _b;
339
- try {
340
- const selectedWallet = (_a = this._wallets) == null ? void 0 : _a.find(
341
- (wallet) => wallet.name === walletName
342
- );
343
- if (!selectedWallet)
344
- return;
345
- if (this._connected) {
346
- if (((_b = this.wallet) == null ? void 0 : _b.name) === walletName)
347
- throw new WalletConnectionError(
348
- `${walletName} wallet is already connected`
349
- ).message;
350
- await this.disconnect();
351
- }
352
- if (isRedirectable()) {
353
- if (selectedWallet.deeplinkProvider) {
354
- const url = encodeURIComponent(window.location.href);
355
- const location = selectedWallet.deeplinkProvider({ url });
356
- window.location.href = location;
357
- }
358
- }
359
- if (selectedWallet.readyState !== "Installed" /* Installed */ && selectedWallet.readyState !== "Loadable" /* Loadable */) {
360
- return;
338
+ var _a;
339
+ const selectedWallet = (_a = this._wallets) == null ? void 0 : _a.find(
340
+ (wallet) => wallet.name === walletName
341
+ );
342
+ if (!selectedWallet)
343
+ return;
344
+ if (this._connected) {
345
+ if (selectedWallet.name === walletName)
346
+ throw new WalletConnectionError(
347
+ `${walletName} wallet is already connected`
348
+ ).message;
349
+ }
350
+ if (isRedirectable()) {
351
+ if (selectedWallet.deeplinkProvider) {
352
+ const url = encodeURIComponent(window.location.href);
353
+ const location = selectedWallet.deeplinkProvider({ url });
354
+ window.location.href = location;
361
355
  }
356
+ }
357
+ if (selectedWallet.readyState !== "Installed" /* Installed */ && selectedWallet.readyState !== "Loadable" /* Loadable */) {
358
+ return;
359
+ }
360
+ await this.connectWallet(selectedWallet);
361
+ }
362
+ async connectWallet(selectedWallet) {
363
+ try {
362
364
  this._connecting = true;
363
365
  this.setWallet(selectedWallet);
364
366
  const account = await selectedWallet.connect();
@@ -371,7 +373,8 @@ var WalletCore = class extends import_eventemitter3.default {
371
373
  this.emit("connect", account);
372
374
  } catch (error) {
373
375
  this.clearData();
374
- throw new WalletConnectionError(error).message;
376
+ const errMsg = typeof error == "object" && "message" in error ? error.message : error;
377
+ throw new WalletConnectionError(errMsg).message;
375
378
  } finally {
376
379
  this._connecting = false;
377
380
  }
@@ -384,7 +387,8 @@ var WalletCore = class extends import_eventemitter3.default {
384
387
  this.clearData();
385
388
  this.emit("disconnect");
386
389
  } catch (error) {
387
- throw new WalletDisconnectionError(error).message;
390
+ const errMsg = typeof error == "object" && "message" in error ? error.message : error;
391
+ throw new WalletDisconnectionError(errMsg).message;
388
392
  }
389
393
  }
390
394
  async signAndSubmitTransaction(transaction, options) {
@@ -429,7 +433,10 @@ var WalletCore = class extends import_eventemitter3.default {
429
433
  }
430
434
  try {
431
435
  this.doesWalletExist();
432
- const response = await this._wallet.signTransaction(transaction, options);
436
+ const response = await this._wallet.signTransaction(
437
+ transaction,
438
+ options
439
+ );
433
440
  return response;
434
441
  } catch (error) {
435
442
  const errMsg = typeof error == "object" && "message" in error ? error.message : error;
package/dist/index.mjs CHANGED
@@ -294,30 +294,32 @@ var WalletCore = class extends EventEmitter {
294
294
  }
295
295
  }
296
296
  async connect(walletName) {
297
- var _a, _b;
298
- try {
299
- const selectedWallet = (_a = this._wallets) == null ? void 0 : _a.find(
300
- (wallet) => wallet.name === walletName
301
- );
302
- if (!selectedWallet)
303
- return;
304
- if (this._connected) {
305
- if (((_b = this.wallet) == null ? void 0 : _b.name) === walletName)
306
- throw new WalletConnectionError(
307
- `${walletName} wallet is already connected`
308
- ).message;
309
- await this.disconnect();
310
- }
311
- if (isRedirectable()) {
312
- if (selectedWallet.deeplinkProvider) {
313
- const url = encodeURIComponent(window.location.href);
314
- const location = selectedWallet.deeplinkProvider({ url });
315
- window.location.href = location;
316
- }
317
- }
318
- if (selectedWallet.readyState !== "Installed" /* Installed */ && selectedWallet.readyState !== "Loadable" /* Loadable */) {
319
- return;
297
+ var _a;
298
+ const selectedWallet = (_a = this._wallets) == null ? void 0 : _a.find(
299
+ (wallet) => wallet.name === walletName
300
+ );
301
+ if (!selectedWallet)
302
+ return;
303
+ if (this._connected) {
304
+ if (selectedWallet.name === walletName)
305
+ throw new WalletConnectionError(
306
+ `${walletName} wallet is already connected`
307
+ ).message;
308
+ }
309
+ if (isRedirectable()) {
310
+ if (selectedWallet.deeplinkProvider) {
311
+ const url = encodeURIComponent(window.location.href);
312
+ const location = selectedWallet.deeplinkProvider({ url });
313
+ window.location.href = location;
320
314
  }
315
+ }
316
+ if (selectedWallet.readyState !== "Installed" /* Installed */ && selectedWallet.readyState !== "Loadable" /* Loadable */) {
317
+ return;
318
+ }
319
+ await this.connectWallet(selectedWallet);
320
+ }
321
+ async connectWallet(selectedWallet) {
322
+ try {
321
323
  this._connecting = true;
322
324
  this.setWallet(selectedWallet);
323
325
  const account = await selectedWallet.connect();
@@ -330,7 +332,8 @@ var WalletCore = class extends EventEmitter {
330
332
  this.emit("connect", account);
331
333
  } catch (error) {
332
334
  this.clearData();
333
- throw new WalletConnectionError(error).message;
335
+ const errMsg = typeof error == "object" && "message" in error ? error.message : error;
336
+ throw new WalletConnectionError(errMsg).message;
334
337
  } finally {
335
338
  this._connecting = false;
336
339
  }
@@ -343,7 +346,8 @@ var WalletCore = class extends EventEmitter {
343
346
  this.clearData();
344
347
  this.emit("disconnect");
345
348
  } catch (error) {
346
- throw new WalletDisconnectionError(error).message;
349
+ const errMsg = typeof error == "object" && "message" in error ? error.message : error;
350
+ throw new WalletDisconnectionError(errMsg).message;
347
351
  }
348
352
  }
349
353
  async signAndSubmitTransaction(transaction, options) {
@@ -388,7 +392,10 @@ var WalletCore = class extends EventEmitter {
388
392
  }
389
393
  try {
390
394
  this.doesWalletExist();
391
- const response = await this._wallet.signTransaction(transaction, options);
395
+ const response = await this._wallet.signTransaction(
396
+ transaction,
397
+ options
398
+ );
392
399
  return response;
393
400
  } catch (error) {
394
401
  const errMsg = typeof error == "object" && "message" in error ? error.message : error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aptos-labs/wallet-adapter-core",
3
- "version": "2.3.3",
3
+ "version": "2.5.0",
4
4
  "description": "Aptos Wallet Adapter Core",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
package/src/WalletCore.ts CHANGED
@@ -172,51 +172,58 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
172
172
  }
173
173
  }
174
174
 
175
- /**
176
- Connects a wallet to the app. If a wallet is already connected,
177
- we first disconnect the current connected wallet and then connect the selected wallet.
178
- On connect success, we set the current account and the network, and keeping the selected wallet
179
- name in LocalStorage to support autoConnect function.
180
-
181
- @param walletName. The wallet name we want to connect as a WalletName type.
182
- @emit emits "connect" event
183
- @throws WalletConnectionError
184
- */
175
+ /**
176
+ * We first make sure we can connect a dapp to a wallet.
177
+ * If all good, we connect the wallet by calling `this.connectWallet`
178
+ * @param walletName. The wallet name we want to connect.
179
+ */
185
180
  async connect(walletName: WalletName): Promise<void | string> {
186
- try {
187
- const selectedWallet = this._wallets?.find(
188
- (wallet: Wallet) => wallet.name === walletName
189
- );
190
-
191
- if (!selectedWallet) return;
192
-
193
- if (this._connected) {
194
- // if the selected wallet is already connected, we don't need to connect again
195
- if (this.wallet?.name === walletName)
196
- throw new WalletConnectionError(
197
- `${walletName} wallet is already connected`
198
- ).message;
181
+ const selectedWallet = this._wallets?.find(
182
+ (wallet: Wallet) => wallet.name === walletName
183
+ );
184
+
185
+ if (!selectedWallet) return;
186
+
187
+ if (this._connected) {
188
+ // if the selected wallet is already connected, we don't need to connect again
189
+ if (selectedWallet.name === walletName)
190
+ throw new WalletConnectionError(
191
+ `${walletName} wallet is already connected`
192
+ ).message;
193
+ }
199
194
 
200
- await this.disconnect();
195
+ // check if we are in a redirectable view (i.e on mobile AND not in an in-app browser) and
196
+ // since wallet readyState can be NotDetected, we check it before the next check
197
+ if (isRedirectable()) {
198
+ // use wallet deep link
199
+ if (selectedWallet.deeplinkProvider) {
200
+ const url = encodeURIComponent(window.location.href);
201
+ const location = selectedWallet.deeplinkProvider({ url });
202
+ window.location.href = location;
201
203
  }
204
+ }
205
+ if (
206
+ selectedWallet.readyState !== WalletReadyState.Installed &&
207
+ selectedWallet.readyState !== WalletReadyState.Loadable
208
+ ) {
209
+ return;
210
+ }
202
211
 
203
- // check if we are in a redirectable view (i.e on mobile AND not in an in-app browser) and
204
- // since wallet readyState can be NotDetected, we check it before the next check
205
- if (isRedirectable()) {
206
- // use wallet deep link
207
- if (selectedWallet.deeplinkProvider) {
208
- const url = encodeURIComponent(window.location.href);
209
- const location = selectedWallet.deeplinkProvider({ url });
210
- window.location.href = location;
211
- }
212
- }
213
- if (
214
- selectedWallet.readyState !== WalletReadyState.Installed &&
215
- selectedWallet.readyState !== WalletReadyState.Loadable
216
- ) {
217
- return;
218
- }
212
+ // Now we can connect to the wallet
213
+ await this.connectWallet(selectedWallet);
214
+ }
219
215
 
216
+ /**
217
+ * Connects a wallet to the dapp.
218
+ * On connect success, we set the current account and the network, and keeping the selected wallet
219
+ * name in LocalStorage to support autoConnect function.
220
+ *
221
+ * @param selectedWallet. The wallet we want to connect.
222
+ * @emit emits "connect" event
223
+ * @throws WalletConnectionError
224
+ */
225
+ async connectWallet(selectedWallet: Wallet) {
226
+ try {
220
227
  this._connecting = true;
221
228
  this.setWallet(selectedWallet);
222
229
  const account = await selectedWallet.connect();
@@ -229,7 +236,9 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
229
236
  this.emit("connect", account);
230
237
  } catch (error: any) {
231
238
  this.clearData();
232
- throw new WalletConnectionError(error).message;
239
+ const errMsg =
240
+ typeof error == "object" && "message" in error ? error.message : error;
241
+ throw new WalletConnectionError(errMsg).message;
233
242
  } finally {
234
243
  this._connecting = false;
235
244
  }
@@ -248,7 +257,9 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
248
257
  this.clearData();
249
258
  this.emit("disconnect");
250
259
  } catch (error: any) {
251
- throw new WalletDisconnectionError(error).message;
260
+ const errMsg =
261
+ typeof error == "object" && "message" in error ? error.message : error;
262
+ throw new WalletDisconnectionError(errMsg).message;
252
263
  }
253
264
  }
254
265
 
@@ -327,7 +338,10 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
327
338
 
328
339
  try {
329
340
  this.doesWalletExist();
330
- const response = await (this._wallet as any).signTransaction(transaction, options);
341
+ const response = await (this._wallet as any).signTransaction(
342
+ transaction,
343
+ options
344
+ );
331
345
  return response;
332
346
  } catch (error: any) {
333
347
  const errMsg =
package/src/types.ts CHANGED
@@ -24,6 +24,10 @@ export interface AptosWalletErrorResult {
24
24
  message: string;
25
25
  }
26
26
 
27
+ export type OnNetworkChange = (
28
+ callBack: (networkInfo: NetworkInfo) => Promise<void>
29
+ ) => Promise<void>;
30
+
27
31
  export interface PluginProvider {
28
32
  connect: () => Promise<AccountInfo>;
29
33
  account: () => Promise<AccountInfo>;
@@ -37,13 +41,11 @@ export interface PluginProvider {
37
41
  onAccountChange: (
38
42
  listener: (newAddress: AccountInfo) => Promise<void>
39
43
  ) => Promise<void>;
40
- onNetworkChange: (
41
- listener: (network: { networkName: NetworkInfo }) => Promise<void>
42
- ) => Promise<void>;
44
+ onNetworkChange: OnNetworkChange;
43
45
  }
44
46
 
45
47
  export interface AdapterPluginEvents {
46
- onNetworkChange(callback: any): Promise<any>;
48
+ onNetworkChange: OnNetworkChange;
47
49
  onAccountChange(callback: any): Promise<any>;
48
50
  }
49
51
 
@@ -110,4 +112,4 @@ export interface SignMessageResponse {
110
112
  export interface TransactionOptions {
111
113
  max_gas_amount?: bigint;
112
114
  gas_unit_price?: bigint;
113
- }
115
+ }