@aptos-labs/wallet-adapter-core 0.2.3 → 2.0.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,17 @@
1
1
  # @aptos-labs/wallet-adapter-core
2
2
 
3
+ ## 2.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 06a2e0d: Add support to submit BCS transaction
8
+
9
+ ## 1.0.0
10
+
11
+ ### Major Changes
12
+
13
+ - e10ea7b: Add ANS support
14
+
3
15
  ## 0.2.3
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Types } from 'aptos';
2
1
  import EventEmitter from 'eventemitter3';
2
+ import { Types, TxnBuilderTypes } from 'aptos';
3
3
 
4
4
  declare enum WalletReadyState {
5
5
  /**
@@ -38,6 +38,7 @@ declare type AccountInfo = {
38
38
  address: string;
39
39
  publicKey: string | string[];
40
40
  minKeysRequired?: number;
41
+ ansName?: string | null;
41
42
  };
42
43
  interface AptosWalletErrorResult {
43
44
  code: number;
@@ -62,6 +63,7 @@ interface AdapterPluginEvents {
62
63
  onNetworkChange(callback: any): Promise<any>;
63
64
  onAccountChange(callback: any): Promise<any>;
64
65
  }
66
+ declare type TransactionPayload = Types.TransactionPayload | TxnBuilderTypes.TransactionPayload;
65
67
  interface AdapterPluginProps<Name extends string = string> {
66
68
  name: WalletName<Name>;
67
69
  url: string;
@@ -71,7 +73,7 @@ interface AdapterPluginProps<Name extends string = string> {
71
73
  connect(): Promise<any>;
72
74
  disconnect: () => Promise<any>;
73
75
  network: () => Promise<any>;
74
- signAndSubmitTransaction<T extends Types.TransactionPayload, V>(transaction: T, options?: V): Promise<{
76
+ signAndSubmitTransaction<T extends TransactionPayload, V>(transaction: T, options?: V): Promise<{
75
77
  hash: Types.HexEncodedBytes;
76
78
  }>;
77
79
  signMessage<T extends SignMessagePayload>(message: T): Promise<SignMessageResponse>;
@@ -122,6 +124,7 @@ declare class WalletCore extends EventEmitter<WalletCoreEvents> {
122
124
  private scopePollingDetectionStrategy;
123
125
  private doesWalletExist;
124
126
  private clearData;
127
+ private setAnsName;
125
128
  setWallet(wallet: Wallet | null): void;
126
129
  setAccount(account: AccountInfo | null): void;
127
130
  setNetwork(network: NetworkInfo | null): void;
@@ -169,14 +172,14 @@ declare class WalletCore extends EventEmitter<WalletCoreEvents> {
169
172
  @return response from the wallet's signAndSubmitTransaction function
170
173
  @throws WalletSignAndSubmitMessageError
171
174
  */
172
- signAndSubmitTransaction(transaction: Types.TransactionPayload): Promise<any>;
175
+ signAndSubmitTransaction(transaction: TransactionPayload): Promise<any>;
173
176
  /**
174
177
  Sign transaction (doesnt submit to chain).
175
178
  @param transaction
176
179
  @return response from the wallet's signTransaction function
177
180
  @throws WalletSignTransactionError
178
181
  */
179
- signTransaction(transaction: Types.TransactionPayload): Promise<Uint8Array | null>;
182
+ signTransaction(transaction: TransactionPayload): Promise<Uint8Array | null>;
180
183
  /**
181
184
  Sign message (doesnt submit to chain).
182
185
  @param message
@@ -199,4 +202,4 @@ declare class WalletCore extends EventEmitter<WalletCoreEvents> {
199
202
  signMessageAndVerify(message: SignMessagePayload): Promise<boolean>;
200
203
  }
201
204
 
202
- export { AccountInfo, AdapterPlugin, AdapterPluginEvents, AdapterPluginProps, AptosWalletErrorResult, NetworkInfo, NetworkName, PluginProvider, SignMessagePayload, SignMessageResponse, Wallet, WalletCore, WalletCoreEvents, WalletInfo, WalletName, WalletReadyState };
205
+ export { AccountInfo, AdapterPlugin, AdapterPluginEvents, AdapterPluginProps, AptosWalletErrorResult, NetworkInfo, NetworkName, PluginProvider, SignMessagePayload, SignMessageResponse, TransactionPayload, Wallet, WalletCore, WalletCoreEvents, WalletInfo, WalletName, WalletReadyState };
package/dist/index.js CHANGED
@@ -184,6 +184,26 @@ function removeLocalStorage() {
184
184
  localStorage.removeItem(LOCAL_STORAGE_ITEM_KEY);
185
185
  }
186
186
 
187
+ // src/ans.ts
188
+ var ChainIdToAnsContractAddressMap = {
189
+ "1": "mainnet",
190
+ "2": "testnet"
191
+ };
192
+ var getNameByAddress = async (chainId, address) => {
193
+ try {
194
+ if (!ChainIdToAnsContractAddressMap[chainId])
195
+ return null;
196
+ const response = await fetch(
197
+ `https://www.aptosnames.com/api/${ChainIdToAnsContractAddressMap[chainId]}/v1/name/${address}`
198
+ );
199
+ const data = await response.json();
200
+ return data.name;
201
+ } catch (e) {
202
+ console.log("error", e);
203
+ return null;
204
+ }
205
+ };
206
+
187
207
  // src/WalletCore.ts
188
208
  var WalletCore = class extends import_eventemitter3.default {
189
209
  constructor(plugins) {
@@ -231,6 +251,16 @@ var WalletCore = class extends import_eventemitter3.default {
231
251
  this.setNetwork(null);
232
252
  removeLocalStorage();
233
253
  }
254
+ async setAnsName() {
255
+ var _a;
256
+ if (((_a = this._network) == null ? void 0 : _a.chainId) && this._account) {
257
+ const name = await getNameByAddress(
258
+ this._network.chainId,
259
+ this._account.address
260
+ );
261
+ this._account.ansName = name;
262
+ }
263
+ }
234
264
  setWallet(wallet) {
235
265
  this._wallet = wallet;
236
266
  }
@@ -291,6 +321,7 @@ var WalletCore = class extends import_eventemitter3.default {
291
321
  this.setAccount({ ...account });
292
322
  const network = await selectedWallet.network();
293
323
  this.setNetwork({ ...network });
324
+ await this.setAnsName();
294
325
  setLocalStorage(selectedWallet.name);
295
326
  this._connected = true;
296
327
  this.emit("connect", account);
@@ -358,8 +389,9 @@ var WalletCore = class extends import_eventemitter3.default {
358
389
  var _a;
359
390
  try {
360
391
  this.doesWalletExist();
361
- await ((_a = this._wallet) == null ? void 0 : _a.onAccountChange((data) => {
392
+ await ((_a = this._wallet) == null ? void 0 : _a.onAccountChange(async (data) => {
362
393
  this.setAccount({ ...data });
394
+ await this.setAnsName();
363
395
  this.emit("accountChange", this._account);
364
396
  }));
365
397
  } catch (error) {
@@ -371,8 +403,9 @@ var WalletCore = class extends import_eventemitter3.default {
371
403
  var _a;
372
404
  try {
373
405
  this.doesWalletExist();
374
- await ((_a = this._wallet) == null ? void 0 : _a.onNetworkChange((data) => {
406
+ await ((_a = this._wallet) == null ? void 0 : _a.onNetworkChange(async (data) => {
375
407
  this.setNetwork({ ...data });
408
+ await this.setAnsName();
376
409
  this.emit("networkChange", this._network);
377
410
  }));
378
411
  } catch (error) {
package/dist/index.mjs CHANGED
@@ -150,6 +150,26 @@ function removeLocalStorage() {
150
150
  localStorage.removeItem(LOCAL_STORAGE_ITEM_KEY);
151
151
  }
152
152
 
153
+ // src/ans.ts
154
+ var ChainIdToAnsContractAddressMap = {
155
+ "1": "mainnet",
156
+ "2": "testnet"
157
+ };
158
+ var getNameByAddress = async (chainId, address) => {
159
+ try {
160
+ if (!ChainIdToAnsContractAddressMap[chainId])
161
+ return null;
162
+ const response = await fetch(
163
+ `https://www.aptosnames.com/api/${ChainIdToAnsContractAddressMap[chainId]}/v1/name/${address}`
164
+ );
165
+ const data = await response.json();
166
+ return data.name;
167
+ } catch (e) {
168
+ console.log("error", e);
169
+ return null;
170
+ }
171
+ };
172
+
153
173
  // src/WalletCore.ts
154
174
  var WalletCore = class extends EventEmitter {
155
175
  constructor(plugins) {
@@ -197,6 +217,16 @@ var WalletCore = class extends EventEmitter {
197
217
  this.setNetwork(null);
198
218
  removeLocalStorage();
199
219
  }
220
+ async setAnsName() {
221
+ var _a;
222
+ if (((_a = this._network) == null ? void 0 : _a.chainId) && this._account) {
223
+ const name = await getNameByAddress(
224
+ this._network.chainId,
225
+ this._account.address
226
+ );
227
+ this._account.ansName = name;
228
+ }
229
+ }
200
230
  setWallet(wallet) {
201
231
  this._wallet = wallet;
202
232
  }
@@ -257,6 +287,7 @@ var WalletCore = class extends EventEmitter {
257
287
  this.setAccount({ ...account });
258
288
  const network = await selectedWallet.network();
259
289
  this.setNetwork({ ...network });
290
+ await this.setAnsName();
260
291
  setLocalStorage(selectedWallet.name);
261
292
  this._connected = true;
262
293
  this.emit("connect", account);
@@ -324,8 +355,9 @@ var WalletCore = class extends EventEmitter {
324
355
  var _a;
325
356
  try {
326
357
  this.doesWalletExist();
327
- await ((_a = this._wallet) == null ? void 0 : _a.onAccountChange((data) => {
358
+ await ((_a = this._wallet) == null ? void 0 : _a.onAccountChange(async (data) => {
328
359
  this.setAccount({ ...data });
360
+ await this.setAnsName();
329
361
  this.emit("accountChange", this._account);
330
362
  }));
331
363
  } catch (error) {
@@ -337,8 +369,9 @@ var WalletCore = class extends EventEmitter {
337
369
  var _a;
338
370
  try {
339
371
  this.doesWalletExist();
340
- await ((_a = this._wallet) == null ? void 0 : _a.onNetworkChange((data) => {
372
+ await ((_a = this._wallet) == null ? void 0 : _a.onNetworkChange(async (data) => {
341
373
  this.setNetwork({ ...data });
374
+ await this.setAnsName();
342
375
  this.emit("networkChange", this._network);
343
376
  }));
344
377
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aptos-labs/wallet-adapter-core",
3
- "version": "0.2.3",
3
+ "version": "2.0.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
@@ -1,4 +1,4 @@
1
- import { HexString, Types } from "aptos";
1
+ import { HexString } from "aptos";
2
2
  import EventEmitter from "eventemitter3";
3
3
  import nacl from "tweetnacl";
4
4
  import { Buffer } from "buffer";
@@ -29,12 +29,14 @@ import {
29
29
  Wallet,
30
30
  WalletInfo,
31
31
  WalletCoreEvents,
32
+ TransactionPayload,
32
33
  } from "./types";
33
34
  import {
34
35
  removeLocalStorage,
35
36
  setLocalStorage,
36
37
  scopePollingDetectionStrategy,
37
38
  } from "./utils";
39
+ import { getNameByAddress } from "./ans";
38
40
 
39
41
  export class WalletCore extends EventEmitter<WalletCoreEvents> {
40
42
  private _wallets: Wallet[] = [];
@@ -95,6 +97,16 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
95
97
  removeLocalStorage();
96
98
  }
97
99
 
100
+ private async setAnsName() {
101
+ if (this._network?.chainId && this._account) {
102
+ const name = await getNameByAddress(
103
+ this._network.chainId,
104
+ this._account.address
105
+ );
106
+ this._account.ansName = name;
107
+ }
108
+ }
109
+
98
110
  setWallet(wallet: Wallet | null) {
99
111
  this._wallet = wallet;
100
112
  }
@@ -192,6 +204,7 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
192
204
  this.setAccount({ ...account });
193
205
  const network = await selectedWallet.network();
194
206
  this.setNetwork({ ...network });
207
+ await this.setAnsName();
195
208
  setLocalStorage(selectedWallet.name);
196
209
  this._connected = true;
197
210
  this.emit("connect", account);
@@ -227,7 +240,7 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
227
240
  @throws WalletSignAndSubmitMessageError
228
241
  */
229
242
  async signAndSubmitTransaction(
230
- transaction: Types.TransactionPayload
243
+ transaction: TransactionPayload
231
244
  ): Promise<any> {
232
245
  try {
233
246
  this.doesWalletExist();
@@ -249,7 +262,7 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
249
262
  @throws WalletSignTransactionError
250
263
  */
251
264
  async signTransaction(
252
- transaction: Types.TransactionPayload
265
+ transaction: TransactionPayload
253
266
  ): Promise<Uint8Array | null> {
254
267
  if (this._wallet && !("signTransaction" in this._wallet)) {
255
268
  throw new WalletNotSupportedMethod(
@@ -297,8 +310,9 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
297
310
  async onAccountChange(): Promise<void> {
298
311
  try {
299
312
  this.doesWalletExist();
300
- await this._wallet?.onAccountChange((data: AccountInfo) => {
313
+ await this._wallet?.onAccountChange(async (data: AccountInfo) => {
301
314
  this.setAccount({ ...data });
315
+ await this.setAnsName();
302
316
  this.emit("accountChange", this._account);
303
317
  });
304
318
  } catch (error: any) {
@@ -316,8 +330,9 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
316
330
  async onNetworkChange(): Promise<void> {
317
331
  try {
318
332
  this.doesWalletExist();
319
- await this._wallet?.onNetworkChange((data: NetworkInfo) => {
333
+ await this._wallet?.onNetworkChange(async (data: NetworkInfo) => {
320
334
  this.setNetwork({ ...data });
335
+ await this.setAnsName();
321
336
  this.emit("networkChange", this._network);
322
337
  });
323
338
  } catch (error: any) {
@@ -1,4 +1,5 @@
1
1
  import { AptosAccount } from "aptos";
2
+ import { getNameByAddress } from "../ans";
2
3
  import {
3
4
  SignMessagePayload,
4
5
  SignMessageResponse,
@@ -104,3 +105,13 @@ describe("signMessageAndVerify", () => {
104
105
  expect(verified).toBeFalsy();
105
106
  });
106
107
  });
108
+
109
+ describe("ans", () => {
110
+ it("should fetch the correct name", async () => {
111
+ const name = await getNameByAddress(
112
+ "2",
113
+ "0x54fac6e5d52953c75e749a8ad260bc450cad0b8ed2f06c1e98707879e13956d1"
114
+ );
115
+ expect(name).toBe("adapter");
116
+ });
117
+ });
package/src/ans.ts ADDED
@@ -0,0 +1,21 @@
1
+ const ChainIdToAnsContractAddressMap: Record<string, string> = {
2
+ "1": "mainnet", // mainnet
3
+ "2": "testnet", // testnet
4
+ };
5
+
6
+ export const getNameByAddress = async (
7
+ chainId: string,
8
+ address: string
9
+ ): Promise<string | null> => {
10
+ try {
11
+ if (!ChainIdToAnsContractAddressMap[chainId]) return null;
12
+ const response = await fetch(
13
+ `https://www.aptosnames.com/api/${ChainIdToAnsContractAddressMap[chainId]}/v1/name/${address}`
14
+ );
15
+ const data = await response.json();
16
+ return data.name;
17
+ } catch (e) {
18
+ console.log("error", e);
19
+ return null;
20
+ }
21
+ };
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Types } from "aptos";
1
+ import { TxnBuilderTypes, Types } from "aptos";
2
2
  import { NetworkName, WalletReadyState } from "./constants";
3
3
 
4
4
  // WalletName is a nominal type that wallet adapters should use, e.g. `'MyCryptoWallet' as WalletName<'MyCryptoWallet'>`
@@ -14,7 +14,8 @@ export type NetworkInfo = {
14
14
  export type AccountInfo = {
15
15
  address: string;
16
16
  publicKey: string | string[];
17
- minKeysRequired?: number
17
+ minKeysRequired?: number;
18
+ ansName?: string | null;
18
19
  };
19
20
 
20
21
  export interface AptosWalletErrorResult {
@@ -22,7 +23,6 @@ export interface AptosWalletErrorResult {
22
23
  name: string;
23
24
  message: string;
24
25
  }
25
-
26
26
  export interface PluginProvider {
27
27
  connect: () => Promise<AccountInfo>;
28
28
  account: () => Promise<AccountInfo>;
@@ -46,16 +46,20 @@ export interface AdapterPluginEvents {
46
46
  onAccountChange(callback: any): Promise<any>;
47
47
  }
48
48
 
49
+ export type TransactionPayload =
50
+ | Types.TransactionPayload
51
+ | TxnBuilderTypes.TransactionPayload;
52
+
49
53
  export interface AdapterPluginProps<Name extends string = string> {
50
54
  name: WalletName<Name>;
51
55
  url: string;
52
56
  icon: `data:image/${"svg+xml" | "webp" | "png" | "gif"};base64,${string}`;
53
- providerName?: string
57
+ providerName?: string;
54
58
  provider: any;
55
59
  connect(): Promise<any>;
56
60
  disconnect: () => Promise<any>;
57
61
  network: () => Promise<any>;
58
- signAndSubmitTransaction<T extends Types.TransactionPayload, V>(
62
+ signAndSubmitTransaction<T extends TransactionPayload, V>(
59
63
  transaction: T,
60
64
  options?: V
61
65
  ): Promise<{ hash: Types.HexEncodedBytes }>;