@dfns/lib-aptos 0.6.1

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.
Files changed (3) hide show
  1. package/index.d.ts +19 -0
  2. package/index.js +99 -0
  3. package/package.json +15 -0
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { DfnsApiClient } from '@dfns/sdk';
2
+ import { AnyRawTransaction, RawTransaction, SignedTransaction } from '@aptos-labs/ts-sdk';
3
+ export declare const hexToBuffer: (hex: string) => Buffer;
4
+ export type DfnsWalletOptions = {
5
+ walletId: string;
6
+ dfnsClient: DfnsApiClient;
7
+ };
8
+ export declare class DfnsWallet {
9
+ private metadata;
10
+ private readonly dfnsClient;
11
+ private constructor();
12
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
13
+ get address(): string;
14
+ get publicKey(): string;
15
+ signTransaction(transaction: RawTransaction | SignedTransaction | AnyRawTransaction): Promise<SignedTransaction>;
16
+ private prepareTransaction;
17
+ private prepareSimpleTransaction;
18
+ private prepareMultiAgentTransaction;
19
+ }
package/index.js ADDED
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = exports.hexToBuffer = void 0;
4
+ const sdk_1 = require("@dfns/sdk");
5
+ const ts_sdk_1 = require("@aptos-labs/ts-sdk");
6
+ const hexToBuffer = (hex) => {
7
+ return Buffer.from(stripHexPrefix(hex), 'hex');
8
+ };
9
+ exports.hexToBuffer = hexToBuffer;
10
+ const stripHexPrefix = (hex) => {
11
+ return hex.replace(/^0x/, '');
12
+ };
13
+ const assertSigned = (res) => {
14
+ if (res.status === 'Failed') {
15
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
16
+ }
17
+ else if (res.status !== 'Signed') {
18
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
19
+ }
20
+ };
21
+ class DfnsWallet {
22
+ constructor(metadata, options) {
23
+ this.metadata = metadata;
24
+ this.dfnsClient = options.dfnsClient;
25
+ }
26
+ static async init(options) {
27
+ const { walletId, dfnsClient } = options;
28
+ const res = await dfnsClient.wallets.getWallet({ walletId });
29
+ if (res.status !== 'Active') {
30
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
31
+ }
32
+ if (res.network !== 'Aptos' && res.network !== 'AptosTestnet') {
33
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Aptos or AptosTestnet', { walletId, network: res.network });
34
+ }
35
+ return new DfnsWallet(res, options);
36
+ }
37
+ get address() {
38
+ return this.metadata.address;
39
+ }
40
+ get publicKey() {
41
+ return this.metadata.signingKey.publicKey;
42
+ }
43
+ // DFNS can sign:
44
+ // - RawTransaction directly for simple transaction
45
+ // - AnyRawTransaction
46
+ // - SignedTransaction for transactions containing additional signers or fee payer (need to provide the
47
+ // right transaction authenticator)
48
+ async signTransaction(transaction) {
49
+ transaction = this.prepareTransaction(transaction);
50
+ const res = await this.dfnsClient.wallets.generateSignature({
51
+ walletId: this.metadata.id,
52
+ body: {
53
+ kind: 'Transaction',
54
+ transaction: transaction.bcsToHex().toString(),
55
+ },
56
+ });
57
+ assertSigned(res);
58
+ if (!res.signedData) {
59
+ throw new sdk_1.DfnsError(-1, 'encoded signature missing', res);
60
+ }
61
+ const deserializer = new ts_sdk_1.Deserializer((0, exports.hexToBuffer)(res.signedData));
62
+ return ts_sdk_1.SignedTransaction.deserialize(deserializer);
63
+ }
64
+ prepareTransaction(transaction) {
65
+ if (isSimpleTransaction(transaction)) {
66
+ return this.prepareSimpleTransaction(transaction);
67
+ }
68
+ if (isMultiAgentTransaction(transaction)) {
69
+ return this.prepareMultiAgentTransaction(transaction);
70
+ }
71
+ // If already a SignedTransaction or RawTransaction, return as is
72
+ return transaction;
73
+ }
74
+ prepareSimpleTransaction(transaction) {
75
+ if (transaction.feePayerAddress) {
76
+ return new ts_sdk_1.SignedTransaction(transaction.rawTransaction, new ts_sdk_1.TransactionAuthenticatorFeePayer(new ts_sdk_1.AccountAuthenticatorNoAccountAuthenticator(), [], [], {
77
+ address: transaction.feePayerAddress,
78
+ authenticator: new ts_sdk_1.AccountAuthenticatorNoAccountAuthenticator(),
79
+ }));
80
+ }
81
+ return transaction.rawTransaction; // If no fee payer, use the raw transaction directly
82
+ }
83
+ prepareMultiAgentTransaction(transaction) {
84
+ if (transaction.feePayerAddress) {
85
+ return new ts_sdk_1.SignedTransaction(transaction.rawTransaction, new ts_sdk_1.TransactionAuthenticatorFeePayer(new ts_sdk_1.AccountAuthenticatorNoAccountAuthenticator(), transaction.secondarySignerAddresses, [], {
86
+ address: transaction.feePayerAddress,
87
+ authenticator: new ts_sdk_1.AccountAuthenticatorNoAccountAuthenticator(),
88
+ }));
89
+ }
90
+ return new ts_sdk_1.SignedTransaction(transaction.rawTransaction, new ts_sdk_1.TransactionAuthenticatorMultiAgent(new ts_sdk_1.AccountAuthenticatorNoAccountAuthenticator(), transaction.secondarySignerAddresses, []));
91
+ }
92
+ }
93
+ exports.DfnsWallet = DfnsWallet;
94
+ const isSimpleTransaction = (transaction) => {
95
+ return transaction instanceof ts_sdk_1.SimpleTransaction;
96
+ };
97
+ const isMultiAgentTransaction = (transaction) => {
98
+ return transaction instanceof ts_sdk_1.MultiAgentTransaction;
99
+ };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@dfns/lib-aptos",
3
+ "version": "0.6.1",
4
+ "peerDependencies": {
5
+ "@aptos-labs/ts-sdk": "1.33.0",
6
+ "@dfns/sdk": "0.6.1"
7
+ },
8
+ "dependencies": {
9
+ "buffer": "6.0.3",
10
+ "cross-fetch": "3.1.6",
11
+ "uuid": "9.0.0"
12
+ },
13
+ "main": "./index.js",
14
+ "type": "commonjs"
15
+ }