@aptos-labs/wallet-adapter-core 2.6.0 → 3.1.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 +23 -0
- package/dist/index.d.ts +71 -85
- package/dist/index.js +169 -66
- package/dist/index.mjs +182 -71
- package/package.json +3 -3
- package/src/WalletCore.ts +184 -120
- package/src/WalletCoreV1.ts +85 -0
- package/src/conversion.ts +49 -16
- package/src/types.ts +76 -61
- package/src/utils/helpers.ts +7 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
// src/WalletCore.ts
|
|
2
|
-
import { HexString } from "aptos";
|
|
3
|
-
import {
|
|
4
|
-
|
|
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";
|
|
5
11
|
import nacl from "tweetnacl";
|
|
6
12
|
import { Buffer } from "buffer";
|
|
7
13
|
|
|
@@ -174,6 +180,9 @@ function isRedirectable() {
|
|
|
174
180
|
return false;
|
|
175
181
|
return isMobile() && !isInAppBrowser();
|
|
176
182
|
}
|
|
183
|
+
function generalizedErrorMessage(error) {
|
|
184
|
+
return typeof error === "object" && "message" in error ? error.message : error;
|
|
185
|
+
}
|
|
177
186
|
|
|
178
187
|
// src/ans.ts
|
|
179
188
|
var ChainIdToAnsContractAddressMap = {
|
|
@@ -196,7 +205,10 @@ var getNameByAddress = async (chainId, address) => {
|
|
|
196
205
|
};
|
|
197
206
|
|
|
198
207
|
// src/conversion.ts
|
|
199
|
-
import {
|
|
208
|
+
import {
|
|
209
|
+
Network,
|
|
210
|
+
TypeTag
|
|
211
|
+
} from "@aptos-labs/ts-sdk";
|
|
200
212
|
import { BCS, TxnBuilderTypes } from "aptos";
|
|
201
213
|
function convertNetwork(networkInfo) {
|
|
202
214
|
switch (networkInfo == null ? void 0 : networkInfo.name.toLowerCase()) {
|
|
@@ -210,19 +222,83 @@ function convertNetwork(networkInfo) {
|
|
|
210
222
|
throw new Error("Invalid network name");
|
|
211
223
|
}
|
|
212
224
|
}
|
|
213
|
-
function
|
|
225
|
+
function convertV2TransactionPayloadToV1BCSPayload(payload) {
|
|
214
226
|
const deserializer = new BCS.Deserializer(payload.bcsToBytes());
|
|
215
227
|
return TxnBuilderTypes.TransactionPayload.deserialize(deserializer);
|
|
216
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
|
+
};
|
|
217
292
|
|
|
218
293
|
// src/WalletCore.ts
|
|
219
|
-
var WalletCore = class extends
|
|
294
|
+
var WalletCore = class extends EventEmitter2 {
|
|
220
295
|
constructor(plugins) {
|
|
221
296
|
super();
|
|
222
297
|
this._wallets = [];
|
|
223
298
|
this._wallet = null;
|
|
224
299
|
this._account = null;
|
|
225
300
|
this._network = null;
|
|
301
|
+
this.waletCoreV1 = new WalletCoreV1();
|
|
226
302
|
this._connecting = false;
|
|
227
303
|
this._connected = false;
|
|
228
304
|
this._wallets = plugins;
|
|
@@ -353,7 +429,7 @@ var WalletCore = class extends EventEmitter {
|
|
|
353
429
|
this.emit("connect", account);
|
|
354
430
|
} catch (error) {
|
|
355
431
|
this.clearData();
|
|
356
|
-
const errMsg =
|
|
432
|
+
const errMsg = generalizedErrorMessage(error);
|
|
357
433
|
throw new WalletConnectionError(errMsg).message;
|
|
358
434
|
} finally {
|
|
359
435
|
this._connecting = false;
|
|
@@ -367,99 +443,133 @@ var WalletCore = class extends EventEmitter {
|
|
|
367
443
|
this.clearData();
|
|
368
444
|
this.emit("disconnect");
|
|
369
445
|
} catch (error) {
|
|
370
|
-
const errMsg =
|
|
446
|
+
const errMsg = generalizedErrorMessage(error);
|
|
371
447
|
throw new WalletDisconnectionError(errMsg).message;
|
|
372
448
|
}
|
|
373
449
|
}
|
|
374
|
-
async signAndSubmitTransaction(
|
|
375
|
-
var _a;
|
|
376
|
-
try {
|
|
377
|
-
this.doesWalletExist();
|
|
378
|
-
const response = await ((_a = this._wallet) == null ? void 0 : _a.signAndSubmitTransaction(
|
|
379
|
-
transaction,
|
|
380
|
-
options
|
|
381
|
-
));
|
|
382
|
-
return response;
|
|
383
|
-
} catch (error) {
|
|
384
|
-
const errMsg = typeof error == "object" && "message" in error ? error.message : error;
|
|
385
|
-
throw new WalletSignAndSubmitMessageError(errMsg).message;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
async signAndSubmitBCSTransaction(transaction, options) {
|
|
389
|
-
var _a;
|
|
390
|
-
if (this._wallet && !("signAndSubmitBCSTransaction" in this._wallet)) {
|
|
391
|
-
throw new WalletNotSupportedMethod(
|
|
392
|
-
`Submit a BCS Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
|
|
393
|
-
).message;
|
|
394
|
-
}
|
|
450
|
+
async signAndSubmitTransaction(transactionInput, options) {
|
|
451
|
+
var _a, _b;
|
|
395
452
|
try {
|
|
396
453
|
this.doesWalletExist();
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
454
|
+
if (((_a = this._wallet) == null ? void 0 : _a.version) === "v2") {
|
|
455
|
+
const response2 = await this._wallet.signAndSubmitTransaction(
|
|
456
|
+
{
|
|
457
|
+
...transactionInput,
|
|
458
|
+
sender: (_b = transactionInput.sender) != null ? _b : this._account.address
|
|
459
|
+
},
|
|
460
|
+
options
|
|
461
|
+
);
|
|
462
|
+
return response2;
|
|
463
|
+
}
|
|
464
|
+
const payloadData = transactionInput.data;
|
|
465
|
+
if (typeof payloadData.functionArguments[0] === "object") {
|
|
466
|
+
const aptosConfig = new AptosConfig({
|
|
467
|
+
network: convertNetwork(this._network)
|
|
468
|
+
});
|
|
469
|
+
const newPayload = await generateTransactionPayload({
|
|
470
|
+
...payloadData,
|
|
471
|
+
aptosConfig
|
|
472
|
+
});
|
|
473
|
+
const oldTransactionPayload2 = convertV2TransactionPayloadToV1BCSPayload(newPayload);
|
|
474
|
+
const response2 = await this.waletCoreV1.signAndSubmitBCSTransaction(
|
|
475
|
+
oldTransactionPayload2,
|
|
476
|
+
this._wallet,
|
|
477
|
+
{
|
|
478
|
+
max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
|
|
479
|
+
gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
|
|
480
|
+
}
|
|
481
|
+
);
|
|
482
|
+
const { hash: hash2, ...output2 } = response2;
|
|
483
|
+
return { hash: hash2, output: output2 };
|
|
484
|
+
}
|
|
485
|
+
const oldTransactionPayload = convertV2PayloadToV1JSONPayload(payloadData);
|
|
486
|
+
const response = await this.waletCoreV1.signAndSubmitTransaction(
|
|
487
|
+
oldTransactionPayload,
|
|
488
|
+
this._wallet,
|
|
489
|
+
{
|
|
490
|
+
max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
|
|
491
|
+
gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
|
|
492
|
+
}
|
|
400
493
|
);
|
|
401
|
-
|
|
494
|
+
const { hash, ...output } = response;
|
|
495
|
+
return { hash, output };
|
|
402
496
|
} catch (error) {
|
|
403
|
-
const errMsg =
|
|
497
|
+
const errMsg = generalizedErrorMessage(error);
|
|
404
498
|
throw new WalletSignAndSubmitMessageError(errMsg).message;
|
|
405
499
|
}
|
|
406
500
|
}
|
|
407
|
-
async signTransaction(
|
|
408
|
-
var _a;
|
|
409
|
-
if (this._wallet && !("signTransaction" in this._wallet)) {
|
|
410
|
-
throw new WalletNotSupportedMethod(
|
|
411
|
-
`Sign Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
|
|
412
|
-
).message;
|
|
413
|
-
}
|
|
501
|
+
async signTransaction(transactionOrPayload, asFeePayer, options) {
|
|
502
|
+
var _a, _b, _c;
|
|
414
503
|
try {
|
|
415
504
|
this.doesWalletExist();
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
505
|
+
if ("rawTransaction" in transactionOrPayload) {
|
|
506
|
+
if (((_a = this._wallet) == null ? void 0 : _a.version) !== "v2") {
|
|
507
|
+
throw new WalletNotSupportedMethod(
|
|
508
|
+
`Sign Transaction V2 is not supported by ${(_b = this.wallet) == null ? void 0 : _b.name}`
|
|
509
|
+
).message;
|
|
510
|
+
}
|
|
511
|
+
const accountAuthenticator2 = this._wallet.signTransaction(
|
|
512
|
+
transactionOrPayload,
|
|
513
|
+
asFeePayer
|
|
514
|
+
);
|
|
515
|
+
return accountAuthenticator2;
|
|
516
|
+
}
|
|
517
|
+
if (this._wallet && !("signTransaction" in this._wallet)) {
|
|
518
|
+
throw new WalletNotSupportedMethod(
|
|
519
|
+
`Sign Transaction is not supported by ${(_c = this.wallet) == null ? void 0 : _c.name}`
|
|
520
|
+
).message;
|
|
521
|
+
}
|
|
522
|
+
const response = await this.waletCoreV1.signTransaction(
|
|
523
|
+
transactionOrPayload,
|
|
524
|
+
this._wallet,
|
|
525
|
+
{
|
|
526
|
+
max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
|
|
527
|
+
gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
|
|
528
|
+
}
|
|
419
529
|
);
|
|
420
|
-
|
|
530
|
+
if (!response) {
|
|
531
|
+
throw new Error("error");
|
|
532
|
+
}
|
|
533
|
+
const deserializer1 = new BCS2.Deserializer(response);
|
|
534
|
+
const deserializedSignature = TxnBuilderTypes2.SignedTransaction.deserialize(deserializer1);
|
|
535
|
+
const transactionAuthenticator = deserializedSignature.authenticator;
|
|
536
|
+
const publicKey = transactionAuthenticator.public_key.value;
|
|
537
|
+
const signature = transactionAuthenticator.signature.value;
|
|
538
|
+
const accountAuthenticator = new AccountAuthenticatorEd25519(
|
|
539
|
+
new Ed25519PublicKey(publicKey),
|
|
540
|
+
new Ed25519Signature(signature)
|
|
541
|
+
);
|
|
542
|
+
return accountAuthenticator;
|
|
421
543
|
} catch (error) {
|
|
422
|
-
const errMsg =
|
|
544
|
+
const errMsg = generalizedErrorMessage(error);
|
|
423
545
|
throw new WalletSignTransactionError(errMsg).message;
|
|
424
546
|
}
|
|
425
547
|
}
|
|
426
548
|
async signMessage(message) {
|
|
427
|
-
var _a;
|
|
428
549
|
try {
|
|
429
550
|
this.doesWalletExist();
|
|
430
|
-
|
|
431
|
-
return null;
|
|
432
|
-
const response = await ((_a = this._wallet) == null ? void 0 : _a.signMessage(message));
|
|
551
|
+
const response = await this._wallet.signMessage(message);
|
|
433
552
|
return response;
|
|
434
553
|
} catch (error) {
|
|
435
|
-
const errMsg =
|
|
554
|
+
const errMsg = generalizedErrorMessage(error);
|
|
436
555
|
throw new WalletSignMessageError(errMsg).message;
|
|
437
556
|
}
|
|
438
557
|
}
|
|
439
|
-
async submitTransaction(
|
|
440
|
-
const payloadData = transactionInput.data;
|
|
441
|
-
const aptosConfig = new AptosConfig({ network: convertNetwork(this._network) });
|
|
442
|
-
const newPayload = await generateTransactionPayload({ ...payloadData, aptosConfig });
|
|
443
|
-
const oldTransactionPayload = convertToBCSPayload(newPayload);
|
|
444
|
-
const response = await this.signAndSubmitBCSTransaction(oldTransactionPayload, options);
|
|
445
|
-
const { hash, ...output } = response;
|
|
446
|
-
return { hash, output };
|
|
447
|
-
}
|
|
448
|
-
async signMultiAgentTransaction(transaction) {
|
|
558
|
+
async submitTransaction(transaction) {
|
|
449
559
|
var _a;
|
|
450
|
-
if (this._wallet && !("
|
|
560
|
+
if (this._wallet && !("submitTransaction" in this._wallet)) {
|
|
451
561
|
throw new WalletNotSupportedMethod(
|
|
452
|
-
`
|
|
562
|
+
`Submit Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
|
|
453
563
|
).message;
|
|
454
564
|
}
|
|
455
565
|
try {
|
|
456
566
|
this.doesWalletExist();
|
|
457
|
-
const
|
|
567
|
+
const pendingTransaction = this._wallet.submitTransaction(
|
|
458
568
|
transaction
|
|
459
569
|
);
|
|
460
|
-
return
|
|
570
|
+
return pendingTransaction;
|
|
461
571
|
} catch (error) {
|
|
462
|
-
const errMsg =
|
|
572
|
+
const errMsg = generalizedErrorMessage(error);
|
|
463
573
|
throw new WalletSignTransactionError(errMsg).message;
|
|
464
574
|
}
|
|
465
575
|
}
|
|
@@ -473,7 +583,7 @@ var WalletCore = class extends EventEmitter {
|
|
|
473
583
|
this.emit("accountChange", this._account);
|
|
474
584
|
}));
|
|
475
585
|
} catch (error) {
|
|
476
|
-
const errMsg =
|
|
586
|
+
const errMsg = generalizedErrorMessage(error);
|
|
477
587
|
throw new WalletAccountChangeError(errMsg).message;
|
|
478
588
|
}
|
|
479
589
|
}
|
|
@@ -487,7 +597,7 @@ var WalletCore = class extends EventEmitter {
|
|
|
487
597
|
this.emit("networkChange", this._network);
|
|
488
598
|
}));
|
|
489
599
|
} catch (error) {
|
|
490
|
-
const errMsg =
|
|
600
|
+
const errMsg = generalizedErrorMessage(error);
|
|
491
601
|
throw new WalletNetworkChangeError(errMsg).message;
|
|
492
602
|
}
|
|
493
603
|
}
|
|
@@ -545,20 +655,21 @@ var WalletCore = class extends EventEmitter {
|
|
|
545
655
|
}
|
|
546
656
|
return verified;
|
|
547
657
|
} catch (error) {
|
|
548
|
-
const errMsg =
|
|
658
|
+
const errMsg = generalizedErrorMessage(error);
|
|
549
659
|
throw new WalletSignMessageAndVerifyError(errMsg).message;
|
|
550
660
|
}
|
|
551
661
|
}
|
|
552
662
|
};
|
|
553
663
|
|
|
554
664
|
// src/types.ts
|
|
555
|
-
import { TxnBuilderTypes as TxnBuilderTypes3, Types as
|
|
665
|
+
import { TxnBuilderTypes as TxnBuilderTypes3, Types as Types3 } from "aptos";
|
|
556
666
|
export {
|
|
557
667
|
NetworkName,
|
|
558
668
|
TxnBuilderTypes3 as TxnBuilderTypes,
|
|
559
|
-
|
|
669
|
+
Types3 as Types,
|
|
560
670
|
WalletCore,
|
|
561
671
|
WalletReadyState,
|
|
672
|
+
generalizedErrorMessage,
|
|
562
673
|
getLocalStorage,
|
|
563
674
|
isInAppBrowser,
|
|
564
675
|
isMobile,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aptos-labs/wallet-adapter-core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Aptos Wallet Adapter Core",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
"@aptos-labs/wallet-adapter-tsconfig": "0.0.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@aptos-labs/ts-sdk": "^0.0.3",
|
|
42
41
|
"buffer": "^6.0.3",
|
|
43
42
|
"eventemitter3": "^4.0.7",
|
|
44
43
|
"tweetnacl": "^1.0.3"
|
|
45
44
|
},
|
|
46
45
|
"peerDependencies": {
|
|
47
|
-
"aptos": "^1.
|
|
46
|
+
"aptos": "^1.20.0",
|
|
47
|
+
"@aptos-labs/ts-sdk": "^1.0.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|