@aptos-labs/wallet-adapter-core 2.5.1 → 3.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 +26 -0
- package/dist/index.d.ts +78 -78
- package/dist/index.js +201 -53
- package/dist/index.mjs +207 -51
- package/package.json +5 -2
- package/src/WalletCore.ts +206 -98
- package/src/WalletCoreV1.ts +85 -0
- package/src/conversion.ts +57 -0
- package/src/types.ts +68 -56
- package/src/utils/helpers.ts +7 -1
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
// src/WalletCore.ts
|
|
2
|
-
import { HexString } from "aptos";
|
|
3
|
-
import
|
|
2
|
+
import { HexString, TxnBuilderTypes as TxnBuilderTypes2, BCS as BCS2 } from "aptos";
|
|
3
|
+
import {
|
|
4
|
+
AccountAuthenticatorEd25519,
|
|
5
|
+
Ed25519PublicKey,
|
|
6
|
+
Ed25519Signature,
|
|
7
|
+
AptosConfig,
|
|
8
|
+
generateTransactionPayload
|
|
9
|
+
} from "@aptos-labs/ts-sdk";
|
|
10
|
+
import EventEmitter2 from "eventemitter3";
|
|
4
11
|
import nacl from "tweetnacl";
|
|
5
12
|
import { Buffer } from "buffer";
|
|
6
13
|
|
|
@@ -173,6 +180,9 @@ function isRedirectable() {
|
|
|
173
180
|
return false;
|
|
174
181
|
return isMobile() && !isInAppBrowser();
|
|
175
182
|
}
|
|
183
|
+
function generalizedErrorMessage(error) {
|
|
184
|
+
return typeof error === "object" && "message" in error ? error.message : error;
|
|
185
|
+
}
|
|
176
186
|
|
|
177
187
|
// src/ans.ts
|
|
178
188
|
var ChainIdToAnsContractAddressMap = {
|
|
@@ -194,14 +204,101 @@ var getNameByAddress = async (chainId, address) => {
|
|
|
194
204
|
}
|
|
195
205
|
};
|
|
196
206
|
|
|
207
|
+
// src/conversion.ts
|
|
208
|
+
import {
|
|
209
|
+
Network,
|
|
210
|
+
TypeTag
|
|
211
|
+
} from "@aptos-labs/ts-sdk";
|
|
212
|
+
import { BCS, TxnBuilderTypes } from "aptos";
|
|
213
|
+
function convertNetwork(networkInfo) {
|
|
214
|
+
switch (networkInfo == null ? void 0 : networkInfo.name.toLowerCase()) {
|
|
215
|
+
case "mainnet":
|
|
216
|
+
return Network.MAINNET;
|
|
217
|
+
case "testnet":
|
|
218
|
+
return Network.TESTNET;
|
|
219
|
+
case "devnet":
|
|
220
|
+
return Network.DEVNET;
|
|
221
|
+
default:
|
|
222
|
+
throw new Error("Invalid network name");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function convertV2TransactionPayloadToV1BCSPayload(payload) {
|
|
226
|
+
const deserializer = new BCS.Deserializer(payload.bcsToBytes());
|
|
227
|
+
return TxnBuilderTypes.TransactionPayload.deserialize(deserializer);
|
|
228
|
+
}
|
|
229
|
+
function convertV2PayloadToV1JSONPayload(payload) {
|
|
230
|
+
var _a;
|
|
231
|
+
if ("bytecode" in payload) {
|
|
232
|
+
throw new Error("script payload not supported");
|
|
233
|
+
} else {
|
|
234
|
+
const stringTypeTags = (_a = payload.typeArguments) == null ? void 0 : _a.map(
|
|
235
|
+
(typeTag) => {
|
|
236
|
+
if (typeTag instanceof TypeTag) {
|
|
237
|
+
return typeTag.toString();
|
|
238
|
+
}
|
|
239
|
+
return typeTag;
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
const newPayload = {
|
|
243
|
+
type: "entry_function_payload",
|
|
244
|
+
function: payload.function,
|
|
245
|
+
type_arguments: stringTypeTags || [],
|
|
246
|
+
arguments: payload.functionArguments
|
|
247
|
+
};
|
|
248
|
+
return newPayload;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/WalletCoreV1.ts
|
|
253
|
+
import EventEmitter from "eventemitter3";
|
|
254
|
+
var WalletCoreV1 = class extends EventEmitter {
|
|
255
|
+
async signAndSubmitTransaction(transaction, wallet, options) {
|
|
256
|
+
try {
|
|
257
|
+
const response = await wallet.signAndSubmitTransaction(
|
|
258
|
+
transaction,
|
|
259
|
+
options
|
|
260
|
+
);
|
|
261
|
+
return response;
|
|
262
|
+
} catch (error) {
|
|
263
|
+
const errMsg = typeof error == "object" && "message" in error ? error.message : error;
|
|
264
|
+
throw new WalletSignAndSubmitMessageError(errMsg).message;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
async signAndSubmitBCSTransaction(transaction, wallet, options) {
|
|
268
|
+
try {
|
|
269
|
+
const response = await wallet.signAndSubmitBCSTransaction(
|
|
270
|
+
transaction,
|
|
271
|
+
options
|
|
272
|
+
);
|
|
273
|
+
return response;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
const errMsg = typeof error == "object" && "message" in error ? error.message : error;
|
|
276
|
+
throw new WalletSignAndSubmitMessageError(errMsg).message;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
async signTransaction(transaction, wallet, options) {
|
|
280
|
+
try {
|
|
281
|
+
const response = await wallet.signTransaction(
|
|
282
|
+
transaction,
|
|
283
|
+
options
|
|
284
|
+
);
|
|
285
|
+
return response;
|
|
286
|
+
} catch (error) {
|
|
287
|
+
const errMsg = typeof error == "object" && "message" in error ? error.message : error;
|
|
288
|
+
throw new WalletSignTransactionError(errMsg).message;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
197
293
|
// src/WalletCore.ts
|
|
198
|
-
var WalletCore = class extends
|
|
294
|
+
var WalletCore = class extends EventEmitter2 {
|
|
199
295
|
constructor(plugins) {
|
|
200
296
|
super();
|
|
201
297
|
this._wallets = [];
|
|
202
298
|
this._wallet = null;
|
|
203
299
|
this._account = null;
|
|
204
300
|
this._network = null;
|
|
301
|
+
this.waletCoreV1 = new WalletCoreV1();
|
|
205
302
|
this._connecting = false;
|
|
206
303
|
this._connected = false;
|
|
207
304
|
this._wallets = plugins;
|
|
@@ -332,7 +429,7 @@ var WalletCore = class extends EventEmitter {
|
|
|
332
429
|
this.emit("connect", account);
|
|
333
430
|
} catch (error) {
|
|
334
431
|
this.clearData();
|
|
335
|
-
const errMsg =
|
|
432
|
+
const errMsg = generalizedErrorMessage(error);
|
|
336
433
|
throw new WalletConnectionError(errMsg).message;
|
|
337
434
|
} finally {
|
|
338
435
|
this._connecting = false;
|
|
@@ -346,73 +443,131 @@ var WalletCore = class extends EventEmitter {
|
|
|
346
443
|
this.clearData();
|
|
347
444
|
this.emit("disconnect");
|
|
348
445
|
} catch (error) {
|
|
349
|
-
const errMsg =
|
|
446
|
+
const errMsg = generalizedErrorMessage(error);
|
|
350
447
|
throw new WalletDisconnectionError(errMsg).message;
|
|
351
448
|
}
|
|
352
449
|
}
|
|
353
|
-
async signAndSubmitTransaction(
|
|
450
|
+
async signAndSubmitTransaction(transactionInput, options) {
|
|
354
451
|
var _a;
|
|
355
452
|
try {
|
|
356
453
|
this.doesWalletExist();
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
454
|
+
if (((_a = this._wallet) == null ? void 0 : _a.version) === "v2") {
|
|
455
|
+
const response2 = await this._wallet.signAndSubmitTransaction(
|
|
456
|
+
transactionInput,
|
|
457
|
+
options
|
|
458
|
+
);
|
|
459
|
+
return response2;
|
|
460
|
+
}
|
|
461
|
+
const payloadData = transactionInput.data;
|
|
462
|
+
if (typeof payloadData.functionArguments[0] === "object") {
|
|
463
|
+
const aptosConfig = new AptosConfig({
|
|
464
|
+
network: convertNetwork(this._network)
|
|
465
|
+
});
|
|
466
|
+
const newPayload = await generateTransactionPayload({
|
|
467
|
+
...payloadData,
|
|
468
|
+
aptosConfig
|
|
469
|
+
});
|
|
470
|
+
const oldTransactionPayload2 = convertV2TransactionPayloadToV1BCSPayload(newPayload);
|
|
471
|
+
const response2 = await this.waletCoreV1.signAndSubmitBCSTransaction(
|
|
472
|
+
oldTransactionPayload2,
|
|
473
|
+
this._wallet,
|
|
474
|
+
{
|
|
475
|
+
max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
|
|
476
|
+
gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
|
|
477
|
+
}
|
|
478
|
+
);
|
|
479
|
+
const { hash: hash2, ...output2 } = response2;
|
|
480
|
+
return { hash: hash2, output: output2 };
|
|
481
|
+
}
|
|
482
|
+
const oldTransactionPayload = convertV2PayloadToV1JSONPayload(payloadData);
|
|
483
|
+
const response = await this.waletCoreV1.signAndSubmitTransaction(
|
|
484
|
+
oldTransactionPayload,
|
|
485
|
+
this._wallet,
|
|
486
|
+
{
|
|
487
|
+
max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
|
|
488
|
+
gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
|
|
489
|
+
}
|
|
490
|
+
);
|
|
491
|
+
const { hash, ...output } = response;
|
|
492
|
+
return { hash, output };
|
|
362
493
|
} catch (error) {
|
|
363
|
-
const errMsg =
|
|
494
|
+
const errMsg = generalizedErrorMessage(error);
|
|
364
495
|
throw new WalletSignAndSubmitMessageError(errMsg).message;
|
|
365
496
|
}
|
|
366
497
|
}
|
|
367
|
-
async
|
|
368
|
-
var _a;
|
|
369
|
-
if (this._wallet && !("signAndSubmitBCSTransaction" in this._wallet)) {
|
|
370
|
-
throw new WalletNotSupportedMethod(
|
|
371
|
-
`Submit a BCS Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
|
|
372
|
-
).message;
|
|
373
|
-
}
|
|
498
|
+
async signTransaction(transactionOrPayload, asFeePayer, options) {
|
|
499
|
+
var _a, _b, _c;
|
|
374
500
|
try {
|
|
375
501
|
this.doesWalletExist();
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
502
|
+
if ("rawTransaction" in transactionOrPayload) {
|
|
503
|
+
if (((_a = this._wallet) == null ? void 0 : _a.version) !== "v2") {
|
|
504
|
+
throw new WalletNotSupportedMethod(
|
|
505
|
+
`Sign Transaction V2 is not supported by ${(_b = this.wallet) == null ? void 0 : _b.name}`
|
|
506
|
+
).message;
|
|
507
|
+
}
|
|
508
|
+
const accountAuthenticator2 = this._wallet.signTransaction(
|
|
509
|
+
transactionOrPayload,
|
|
510
|
+
asFeePayer
|
|
511
|
+
);
|
|
512
|
+
return accountAuthenticator2;
|
|
513
|
+
}
|
|
514
|
+
if (this._wallet && !("signTransaction" in this._wallet)) {
|
|
515
|
+
throw new WalletNotSupportedMethod(
|
|
516
|
+
`Sign Transaction is not supported by ${(_c = this.wallet) == null ? void 0 : _c.name}`
|
|
517
|
+
).message;
|
|
518
|
+
}
|
|
519
|
+
const response = await this.waletCoreV1.signTransaction(
|
|
520
|
+
transactionOrPayload,
|
|
521
|
+
this._wallet,
|
|
522
|
+
{
|
|
523
|
+
max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
|
|
524
|
+
gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
|
|
525
|
+
}
|
|
379
526
|
);
|
|
380
|
-
|
|
527
|
+
if (!response) {
|
|
528
|
+
throw new Error("error");
|
|
529
|
+
}
|
|
530
|
+
const deserializer1 = new BCS2.Deserializer(response);
|
|
531
|
+
const deserializedSignature = TxnBuilderTypes2.SignedTransaction.deserialize(deserializer1);
|
|
532
|
+
const transactionAuthenticator = deserializedSignature.authenticator;
|
|
533
|
+
const publicKey = transactionAuthenticator.public_key.value;
|
|
534
|
+
const signature = transactionAuthenticator.signature.value;
|
|
535
|
+
const accountAuthenticator = new AccountAuthenticatorEd25519(
|
|
536
|
+
new Ed25519PublicKey(publicKey),
|
|
537
|
+
new Ed25519Signature(signature)
|
|
538
|
+
);
|
|
539
|
+
return accountAuthenticator;
|
|
381
540
|
} catch (error) {
|
|
382
|
-
const errMsg =
|
|
383
|
-
throw new
|
|
541
|
+
const errMsg = generalizedErrorMessage(error);
|
|
542
|
+
throw new WalletSignTransactionError(errMsg).message;
|
|
384
543
|
}
|
|
385
544
|
}
|
|
386
|
-
async
|
|
387
|
-
var _a;
|
|
388
|
-
if (this._wallet && !("signTransaction" in this._wallet)) {
|
|
389
|
-
throw new WalletNotSupportedMethod(
|
|
390
|
-
`Sign Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
|
|
391
|
-
).message;
|
|
392
|
-
}
|
|
545
|
+
async signMessage(message) {
|
|
393
546
|
try {
|
|
394
547
|
this.doesWalletExist();
|
|
395
|
-
const response = await this._wallet.
|
|
396
|
-
transaction,
|
|
397
|
-
options
|
|
398
|
-
);
|
|
548
|
+
const response = await this._wallet.signMessage(message);
|
|
399
549
|
return response;
|
|
400
550
|
} catch (error) {
|
|
401
|
-
const errMsg =
|
|
402
|
-
throw new
|
|
551
|
+
const errMsg = generalizedErrorMessage(error);
|
|
552
|
+
throw new WalletSignMessageError(errMsg).message;
|
|
403
553
|
}
|
|
404
554
|
}
|
|
405
|
-
async
|
|
555
|
+
async submitTransaction(transaction) {
|
|
406
556
|
var _a;
|
|
557
|
+
if (this._wallet && !("submitTransaction" in this._wallet)) {
|
|
558
|
+
throw new WalletNotSupportedMethod(
|
|
559
|
+
`Submit Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
|
|
560
|
+
).message;
|
|
561
|
+
}
|
|
407
562
|
try {
|
|
408
563
|
this.doesWalletExist();
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
return
|
|
564
|
+
const pendingTransaction = this._wallet.submitTransaction(
|
|
565
|
+
transaction
|
|
566
|
+
);
|
|
567
|
+
return pendingTransaction;
|
|
413
568
|
} catch (error) {
|
|
414
|
-
const errMsg =
|
|
415
|
-
throw new
|
|
569
|
+
const errMsg = generalizedErrorMessage(error);
|
|
570
|
+
throw new WalletSignTransactionError(errMsg).message;
|
|
416
571
|
}
|
|
417
572
|
}
|
|
418
573
|
async onAccountChange() {
|
|
@@ -425,7 +580,7 @@ var WalletCore = class extends EventEmitter {
|
|
|
425
580
|
this.emit("accountChange", this._account);
|
|
426
581
|
}));
|
|
427
582
|
} catch (error) {
|
|
428
|
-
const errMsg =
|
|
583
|
+
const errMsg = generalizedErrorMessage(error);
|
|
429
584
|
throw new WalletAccountChangeError(errMsg).message;
|
|
430
585
|
}
|
|
431
586
|
}
|
|
@@ -439,7 +594,7 @@ var WalletCore = class extends EventEmitter {
|
|
|
439
594
|
this.emit("networkChange", this._network);
|
|
440
595
|
}));
|
|
441
596
|
} catch (error) {
|
|
442
|
-
const errMsg =
|
|
597
|
+
const errMsg = generalizedErrorMessage(error);
|
|
443
598
|
throw new WalletNetworkChangeError(errMsg).message;
|
|
444
599
|
}
|
|
445
600
|
}
|
|
@@ -497,20 +652,21 @@ var WalletCore = class extends EventEmitter {
|
|
|
497
652
|
}
|
|
498
653
|
return verified;
|
|
499
654
|
} catch (error) {
|
|
500
|
-
const errMsg =
|
|
655
|
+
const errMsg = generalizedErrorMessage(error);
|
|
501
656
|
throw new WalletSignMessageAndVerifyError(errMsg).message;
|
|
502
657
|
}
|
|
503
658
|
}
|
|
504
659
|
};
|
|
505
660
|
|
|
506
661
|
// src/types.ts
|
|
507
|
-
import { TxnBuilderTypes as
|
|
662
|
+
import { TxnBuilderTypes as TxnBuilderTypes3, Types as Types3 } from "aptos";
|
|
508
663
|
export {
|
|
509
664
|
NetworkName,
|
|
510
|
-
|
|
511
|
-
|
|
665
|
+
TxnBuilderTypes3 as TxnBuilderTypes,
|
|
666
|
+
Types3 as Types,
|
|
512
667
|
WalletCore,
|
|
513
668
|
WalletReadyState,
|
|
669
|
+
generalizedErrorMessage,
|
|
514
670
|
getLocalStorage,
|
|
515
671
|
isInAppBrowser,
|
|
516
672
|
isMobile,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aptos-labs/wallet-adapter-core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Aptos Wallet Adapter Core",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -38,11 +38,14 @@
|
|
|
38
38
|
"@aptos-labs/wallet-adapter-tsconfig": "0.0.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"aptos": "^1.14.0",
|
|
42
41
|
"buffer": "^6.0.3",
|
|
43
42
|
"eventemitter3": "^4.0.7",
|
|
44
43
|
"tweetnacl": "^1.0.3"
|
|
45
44
|
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"aptos": "^1.19.0",
|
|
47
|
+
"@aptos-labs/ts-sdk": "^0.0.7"
|
|
48
|
+
},
|
|
46
49
|
"scripts": {
|
|
47
50
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
48
51
|
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|