@aptos-labs/ts-sdk 0.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/LICENSE +201 -0
- package/README.md +144 -0
- package/dist/browser/index.global.js +410 -0
- package/dist/browser/index.global.js.map +1 -0
- package/dist/cjs/index.d.ts +4965 -0
- package/dist/cjs/index.js +4762 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.d.ts +4965 -0
- package/dist/esm/index.mjs +4645 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/index.d.ts +1247 -0
- package/dist/types/index.js +151 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +79 -0
- package/src/api/account.ts +360 -0
- package/src/api/aptos.ts +103 -0
- package/src/api/aptosConfig.ts +77 -0
- package/src/api/coin.ts +39 -0
- package/src/api/digitalAsset.ts +192 -0
- package/src/api/event.ts +78 -0
- package/src/api/faucet.ts +30 -0
- package/src/api/fungibleAsset.ts +82 -0
- package/src/api/general.ts +188 -0
- package/src/api/index.ts +5 -0
- package/src/api/staking.ts +58 -0
- package/src/api/transaction.ts +135 -0
- package/src/api/transactionSubmission.ts +168 -0
- package/src/bcs/consts.ts +12 -0
- package/src/bcs/deserializer.ts +248 -0
- package/src/bcs/index.ts +9 -0
- package/src/bcs/serializable/entryFunctionBytes.ts +61 -0
- package/src/bcs/serializable/fixedBytes.ts +65 -0
- package/src/bcs/serializable/movePrimitives.ts +211 -0
- package/src/bcs/serializable/moveStructs.ts +462 -0
- package/src/bcs/serializer.ts +353 -0
- package/src/client/core.ts +106 -0
- package/src/client/get.ts +109 -0
- package/src/client/index.ts +7 -0
- package/src/client/post.ts +90 -0
- package/src/client/types.ts +58 -0
- package/src/core/account.ts +180 -0
- package/src/core/accountAddress.ts +407 -0
- package/src/core/authenticationKey.ts +102 -0
- package/src/core/common.ts +40 -0
- package/src/core/crypto/asymmetricCrypto.ts +77 -0
- package/src/core/crypto/ed25519.ts +224 -0
- package/src/core/crypto/index.ts +7 -0
- package/src/core/crypto/multiEd25519.ts +251 -0
- package/src/core/crypto/secp256k1.ts +227 -0
- package/src/core/hex.ts +177 -0
- package/src/core/index.ts +9 -0
- package/src/index.ts +12 -0
- package/src/internal/account.ts +484 -0
- package/src/internal/coin.ts +32 -0
- package/src/internal/digitalAsset.ts +302 -0
- package/src/internal/event.ts +88 -0
- package/src/internal/faucet.ts +41 -0
- package/src/internal/fungibleAsset.ts +114 -0
- package/src/internal/general.ts +160 -0
- package/src/internal/queries/TokenActivitiesFieldsFragment.graphql +17 -0
- package/src/internal/queries/currentTokenOwnershipFieldsFragment.graphql +45 -0
- package/src/internal/queries/getAccountCoinCount.graphql +7 -0
- package/src/internal/queries/getAccountCoinsData.graphql +32 -0
- package/src/internal/queries/getAccountCollectionsWithOwnedTokens.graphql +33 -0
- package/src/internal/queries/getAccountOwnedObjects.graphql +16 -0
- package/src/internal/queries/getAccountOwnedTokens.graphql +11 -0
- package/src/internal/queries/getAccountOwnedTokensByTokenData.graphql +11 -0
- package/src/internal/queries/getAccountOwnedTokensFromCollectionAddress.graphql +11 -0
- package/src/internal/queries/getAccountTokensCount.graphql +7 -0
- package/src/internal/queries/getAccountTransactionsCount.graphql +7 -0
- package/src/internal/queries/getChainTopUserTransactions.graphql +5 -0
- package/src/internal/queries/getCollectionData.graphql +20 -0
- package/src/internal/queries/getCurrentFungibleAssetBalances.graphql +17 -0
- package/src/internal/queries/getDelegatedStakingActivities.graphql +12 -0
- package/src/internal/queries/getEvents.graphql +12 -0
- package/src/internal/queries/getFungibleAssetActivities.graphql +20 -0
- package/src/internal/queries/getFungibleAssetMetadata.graphql +16 -0
- package/src/internal/queries/getNumberOfDelegatorsQuery.graphql +9 -0
- package/src/internal/queries/getProcessorStatus.graphql +7 -0
- package/src/internal/queries/getTokenActivity.graphql +11 -0
- package/src/internal/queries/getTokenCurrentOwner.graphql +11 -0
- package/src/internal/queries/getTokenData.graphql +38 -0
- package/src/internal/staking.ts +68 -0
- package/src/internal/transaction.ts +245 -0
- package/src/internal/transactionSubmission.ts +162 -0
- package/src/transactions/authenticator/account.ts +121 -0
- package/src/transactions/authenticator/transaction.ts +222 -0
- package/src/transactions/instances/chainId.ts +26 -0
- package/src/transactions/instances/identifier.ts +28 -0
- package/src/transactions/instances/index.ts +9 -0
- package/src/transactions/instances/moduleId.ts +53 -0
- package/src/transactions/instances/rawTransaction.ts +199 -0
- package/src/transactions/instances/signedTransaction.ts +43 -0
- package/src/transactions/instances/transactionArgument.ts +37 -0
- package/src/transactions/instances/transactionPayload.ts +407 -0
- package/src/transactions/transaction_builder/transaction_builder.ts +541 -0
- package/src/transactions/typeTag/typeTag.ts +487 -0
- package/src/transactions/types.ts +262 -0
- package/src/types/codegen.yaml +33 -0
- package/src/types/generated/operations.ts +623 -0
- package/src/types/generated/queries.ts +737 -0
- package/src/types/generated/types.ts +10387 -0
- package/src/types/index.ts +944 -0
- package/src/types/indexer.ts +93 -0
- package/src/utils/apiEndpoints.ts +36 -0
- package/src/utils/const.ts +51 -0
- package/src/utils/hdKey.ts +113 -0
- package/src/utils/helpers.ts +12 -0
- package/src/utils/memoize.ts +68 -0
- package/src/version.ts +9 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This file contains the underlying implementations for exposed API surface in
|
|
6
|
+
* the {@link api/transaction}. By moving the methods out into a separate file,
|
|
7
|
+
* other namespaces and processes can access these methods without depending on the entire
|
|
8
|
+
* transaction namespace and without having a dependency cycle error.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { AptosConfig } from "../api/aptosConfig";
|
|
12
|
+
import { AptosApiError, getAptosFullNode, paginateWithCursor } from "../client";
|
|
13
|
+
import {
|
|
14
|
+
TransactionResponseType,
|
|
15
|
+
type AnyNumber,
|
|
16
|
+
type GasEstimation,
|
|
17
|
+
type HexInput,
|
|
18
|
+
type PaginationArgs,
|
|
19
|
+
type TransactionResponse,
|
|
20
|
+
} from "../types";
|
|
21
|
+
import { DEFAULT_TXN_TIMEOUT_SEC } from "../utils/const";
|
|
22
|
+
import { sleep } from "../utils/helpers";
|
|
23
|
+
import { memoizeAsync } from "../utils/memoize";
|
|
24
|
+
import { getIndexerLastSuccessVersion } from "./general";
|
|
25
|
+
|
|
26
|
+
export async function getTransactions(args: {
|
|
27
|
+
aptosConfig: AptosConfig;
|
|
28
|
+
options?: PaginationArgs;
|
|
29
|
+
}): Promise<TransactionResponse[]> {
|
|
30
|
+
const { aptosConfig, options } = args;
|
|
31
|
+
return paginateWithCursor<{}, TransactionResponse[]>({
|
|
32
|
+
aptosConfig,
|
|
33
|
+
originMethod: "getTransactions",
|
|
34
|
+
path: "transactions",
|
|
35
|
+
params: { start: options?.offset, limit: options?.limit },
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function getGasPriceEstimation(args: { aptosConfig: AptosConfig }) {
|
|
40
|
+
const { aptosConfig } = args;
|
|
41
|
+
|
|
42
|
+
return memoizeAsync(
|
|
43
|
+
async () => {
|
|
44
|
+
const { data } = await getAptosFullNode<{}, GasEstimation>({
|
|
45
|
+
aptosConfig,
|
|
46
|
+
originMethod: "getGasPriceEstimation",
|
|
47
|
+
path: "estimate_gas_price",
|
|
48
|
+
});
|
|
49
|
+
return data;
|
|
50
|
+
},
|
|
51
|
+
`gas-price-${aptosConfig.network}`,
|
|
52
|
+
1000 * 60 * 5, // 5 minutes
|
|
53
|
+
)();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function getTransactionByVersion(args: {
|
|
57
|
+
aptosConfig: AptosConfig;
|
|
58
|
+
ledgerVersion: AnyNumber;
|
|
59
|
+
}): Promise<TransactionResponse> {
|
|
60
|
+
const { aptosConfig, ledgerVersion } = args;
|
|
61
|
+
const { data } = await getAptosFullNode<{}, TransactionResponse>({
|
|
62
|
+
aptosConfig,
|
|
63
|
+
originMethod: "getTransactionByVersion",
|
|
64
|
+
path: `transactions/by_version/${ledgerVersion}`,
|
|
65
|
+
});
|
|
66
|
+
return data;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function getTransactionByHash(args: {
|
|
70
|
+
aptosConfig: AptosConfig;
|
|
71
|
+
transactionHash: HexInput;
|
|
72
|
+
}): Promise<TransactionResponse> {
|
|
73
|
+
const { aptosConfig, transactionHash } = args;
|
|
74
|
+
const { data } = await getAptosFullNode<{}, TransactionResponse>({
|
|
75
|
+
aptosConfig,
|
|
76
|
+
path: `transactions/by_hash/${transactionHash}`,
|
|
77
|
+
originMethod: "getTransactionByHash",
|
|
78
|
+
});
|
|
79
|
+
return data;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function isTransactionPending(args: {
|
|
83
|
+
aptosConfig: AptosConfig;
|
|
84
|
+
transactionHash: HexInput;
|
|
85
|
+
}): Promise<boolean> {
|
|
86
|
+
const { aptosConfig, transactionHash } = args;
|
|
87
|
+
try {
|
|
88
|
+
const transaction = await getTransactionByHash({ aptosConfig, transactionHash });
|
|
89
|
+
return transaction.type === TransactionResponseType.Pending;
|
|
90
|
+
} catch (e: any) {
|
|
91
|
+
if (e?.status === 404) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function waitForTransaction(args: {
|
|
99
|
+
aptosConfig: AptosConfig;
|
|
100
|
+
transactionHash: HexInput;
|
|
101
|
+
options?: { timeoutSecs?: number; checkSuccess?: boolean; indexerVersionCheck?: boolean };
|
|
102
|
+
}): Promise<TransactionResponse> {
|
|
103
|
+
const { aptosConfig, transactionHash, options } = args;
|
|
104
|
+
const timeoutSecs = options?.timeoutSecs ?? DEFAULT_TXN_TIMEOUT_SEC;
|
|
105
|
+
const checkSuccess = options?.checkSuccess ?? true;
|
|
106
|
+
const indexerVersionCheck = options?.indexerVersionCheck ?? true;
|
|
107
|
+
|
|
108
|
+
let isPending = true;
|
|
109
|
+
let timeElapsed = 0;
|
|
110
|
+
let lastTxn: TransactionResponse | undefined;
|
|
111
|
+
let lastError: AptosApiError | undefined;
|
|
112
|
+
let backoffIntervalMs = 200;
|
|
113
|
+
const backoffMultiplier = 1.5;
|
|
114
|
+
|
|
115
|
+
while (isPending) {
|
|
116
|
+
if (timeElapsed >= timeoutSecs) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
// eslint-disable-next-line no-await-in-loop
|
|
121
|
+
lastTxn = await getTransactionByHash({ aptosConfig, transactionHash });
|
|
122
|
+
|
|
123
|
+
isPending = lastTxn.type === TransactionResponseType.Pending;
|
|
124
|
+
|
|
125
|
+
if (!isPending) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
} catch (e) {
|
|
129
|
+
// In short, this means we will retry if it was an AptosApiError and the code was 404 or 5xx.
|
|
130
|
+
const isAptosApiError = e instanceof AptosApiError;
|
|
131
|
+
if (!isAptosApiError) {
|
|
132
|
+
throw e; // This would be unexpected
|
|
133
|
+
}
|
|
134
|
+
lastError = e;
|
|
135
|
+
const isRequestError = e.status !== 404 && e.status >= 400 && e.status < 500;
|
|
136
|
+
if (isRequestError) {
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// eslint-disable-next-line no-await-in-loop
|
|
141
|
+
await sleep(backoffIntervalMs);
|
|
142
|
+
timeElapsed += backoffIntervalMs / 1000; // Convert to seconds
|
|
143
|
+
backoffIntervalMs *= backoffMultiplier;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// There is a chance that lastTxn is still undefined. Let's throw the last error otherwise a WaitForTransactionError
|
|
147
|
+
if (lastTxn === undefined) {
|
|
148
|
+
if (lastError) {
|
|
149
|
+
throw lastError;
|
|
150
|
+
} else {
|
|
151
|
+
throw new WaitForTransactionError(
|
|
152
|
+
`Fetching transaction ${transactionHash} failed and timed out after ${timeoutSecs} seconds`,
|
|
153
|
+
lastTxn,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (lastTxn.type === TransactionResponseType.Pending) {
|
|
159
|
+
throw new WaitForTransactionError(
|
|
160
|
+
`Transaction ${transactionHash} timed out in pending state after ${timeoutSecs} seconds`,
|
|
161
|
+
lastTxn,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (!checkSuccess) {
|
|
165
|
+
return lastTxn;
|
|
166
|
+
}
|
|
167
|
+
if (!lastTxn.success) {
|
|
168
|
+
throw new FailedTransactionError(
|
|
169
|
+
`Transaction ${transactionHash} failed with an error: ${(lastTxn as any).vm_status}`,
|
|
170
|
+
lastTxn,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Make sure indexer is synced with the latest ledger version
|
|
175
|
+
if (indexerVersionCheck) {
|
|
176
|
+
try {
|
|
177
|
+
await waitForLastSuccessIndexerVersionSync({ aptosConfig, ledgerVersion: Number(lastTxn.version) });
|
|
178
|
+
} catch (_e) {
|
|
179
|
+
throw new WaitForTransactionError(
|
|
180
|
+
// eslint-disable-next-line max-len
|
|
181
|
+
`Transaction ${transactionHash} committed, but timed out waiting for indexer to sync with ledger version ${lastTxn.version}.` +
|
|
182
|
+
"You can disable this check by setting `indexerVersionCheck` to false in the `extraArgs` parameter.",
|
|
183
|
+
lastTxn,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return lastTxn;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Waits for the indexer to sync up to the ledgerVersion. Timeout is 3 seconds.
|
|
193
|
+
*/
|
|
194
|
+
async function waitForLastSuccessIndexerVersionSync(args: {
|
|
195
|
+
aptosConfig: AptosConfig;
|
|
196
|
+
ledgerVersion: number;
|
|
197
|
+
}): Promise<void> {
|
|
198
|
+
const { aptosConfig, ledgerVersion } = args;
|
|
199
|
+
const timeoutMilliseconds = 3000; // 3 seconds
|
|
200
|
+
const startTime = new Date().getTime();
|
|
201
|
+
let indexerVersion = -1;
|
|
202
|
+
|
|
203
|
+
while (indexerVersion < ledgerVersion) {
|
|
204
|
+
// check for timeout
|
|
205
|
+
if (new Date().getTime() - startTime > timeoutMilliseconds) {
|
|
206
|
+
throw new Error("waitForLastSuccessIndexerVersionSync timeout");
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// eslint-disable-next-line no-await-in-loop
|
|
210
|
+
indexerVersion = await getIndexerLastSuccessVersion({ aptosConfig });
|
|
211
|
+
if (indexerVersion >= ledgerVersion) {
|
|
212
|
+
// break out immediately if we are synced
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// eslint-disable-next-line no-await-in-loop
|
|
217
|
+
await sleep(200);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* This error is used by `waitForTransaction` when waiting for a
|
|
223
|
+
* transaction to time out or when the transaction response is undefined
|
|
224
|
+
*/
|
|
225
|
+
export class WaitForTransactionError extends Error {
|
|
226
|
+
public readonly lastSubmittedTransaction: TransactionResponse | undefined;
|
|
227
|
+
|
|
228
|
+
constructor(message: string, lastSubmittedTransaction: TransactionResponse | undefined) {
|
|
229
|
+
super(message);
|
|
230
|
+
this.lastSubmittedTransaction = lastSubmittedTransaction;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* This error is used by `waitForTransaction` if `checkSuccess` is true.
|
|
236
|
+
* See that function for more information.
|
|
237
|
+
*/
|
|
238
|
+
export class FailedTransactionError extends Error {
|
|
239
|
+
public readonly transaction: TransactionResponse;
|
|
240
|
+
|
|
241
|
+
constructor(message: string, transaction: TransactionResponse) {
|
|
242
|
+
super(message);
|
|
243
|
+
this.transaction = transaction;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file contains the underlying implementations for exposed API surface in
|
|
3
|
+
* the {@link api/transaction_submission}. By moving the methods out into a separate file,
|
|
4
|
+
* other namespaces and processes can access these methods without depending on the entire
|
|
5
|
+
* transaction_submission namespace and without having a dependency cycle error.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { AptosConfig } from "../api/aptosConfig";
|
|
9
|
+
import { postAptosFullNode } from "../client";
|
|
10
|
+
import { Account } from "../core/account";
|
|
11
|
+
import { AccountAuthenticator } from "../transactions/authenticator/account";
|
|
12
|
+
import {
|
|
13
|
+
buildTransaction,
|
|
14
|
+
generateTransactionPayload,
|
|
15
|
+
generateSignedTransactionForSimulation,
|
|
16
|
+
generateSignedTransaction,
|
|
17
|
+
sign,
|
|
18
|
+
} from "../transactions/transaction_builder/transaction_builder";
|
|
19
|
+
import { GenerateTransactionInput, AnyRawTransaction, SimulateTransactionData } from "../transactions/types";
|
|
20
|
+
import { UserTransactionResponse, PendingTransactionResponse, MimeType } from "../types";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Generates any transaction by passing in the required arguments
|
|
24
|
+
*
|
|
25
|
+
* @param args.sender The transaction sender's account address as a HexInput
|
|
26
|
+
* @param args.data EntryFunctionData | ScriptData | MultiSigData
|
|
27
|
+
* @param args.feePayerAddress optional. For a fee payer (aka sponsored) transaction
|
|
28
|
+
* @param args.secondarySignerAddresses optional. For a multi-agent or fee payer (aka sponsored) transactions
|
|
29
|
+
* @param args.options optional. GenerateTransactionOptions type
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* For a single signer entry function
|
|
33
|
+
* move function name, move function type arguments, move function arguments
|
|
34
|
+
* `
|
|
35
|
+
* data: {
|
|
36
|
+
* function:"0x1::aptos_account::transfer",
|
|
37
|
+
* type_arguments:[]
|
|
38
|
+
* arguments:[receiverAddress,10]
|
|
39
|
+
* }
|
|
40
|
+
* `
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* For a single signer script function
|
|
44
|
+
* module bytecode, move function type arguments, move function arguments
|
|
45
|
+
* ```
|
|
46
|
+
* data: {
|
|
47
|
+
* bytecode:"0x001234567",
|
|
48
|
+
* type_arguments:[],
|
|
49
|
+
* arguments:[receiverAddress,10]
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @return A raw transaction type (note that it holds the raw transaction as a bcs serialized data)
|
|
54
|
+
* ```
|
|
55
|
+
* {
|
|
56
|
+
* rawTransaction: Uint8Array,
|
|
57
|
+
* secondarySignerAddresses? : Array<AccountAddress>,
|
|
58
|
+
* feePayerAddress?: AccountAddress
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export async function generateTransaction(
|
|
63
|
+
args: { aptosConfig: AptosConfig } & GenerateTransactionInput,
|
|
64
|
+
): Promise<AnyRawTransaction> {
|
|
65
|
+
const { aptosConfig, sender, data, options, secondarySignerAddresses, feePayerAddress } = args;
|
|
66
|
+
const payload = await generateTransactionPayload(data);
|
|
67
|
+
return buildTransaction({
|
|
68
|
+
aptosConfig,
|
|
69
|
+
sender,
|
|
70
|
+
payload,
|
|
71
|
+
options,
|
|
72
|
+
secondarySignerAddresses,
|
|
73
|
+
feePayerAddress,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Sign a transaction that can later be submitted to chain
|
|
79
|
+
*
|
|
80
|
+
* @param args.signer The signer account to sign the transaction
|
|
81
|
+
* @param args.transaction A raw transaction type (note that it holds the raw transaction as a bcs serialized data)
|
|
82
|
+
* ```
|
|
83
|
+
* {
|
|
84
|
+
* rawTransaction: Uint8Array,
|
|
85
|
+
* secondarySignerAddresses? : Array<AccountAddress>,
|
|
86
|
+
* feePayerAddress?: AccountAddress
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @return The signer AccountAuthenticator
|
|
91
|
+
*/
|
|
92
|
+
export function signTransaction(args: { signer: Account; transaction: AnyRawTransaction }): AccountAuthenticator {
|
|
93
|
+
const accountAuthenticator = sign({ ...args });
|
|
94
|
+
return accountAuthenticator;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Simulates a transaction before singing it.
|
|
99
|
+
*
|
|
100
|
+
* @param args.signerPublicKey The signer public key
|
|
101
|
+
* @param args.transaction The raw transaction to simulate
|
|
102
|
+
* @param args.secondarySignersPublicKeys optional. For when the transaction is a multi signers transaction
|
|
103
|
+
* @param args.feePayerPublicKey optional. For when the transaction is a fee payer (aka sponsored) transaction
|
|
104
|
+
* @param args.options optional. A config to simulate the transaction with
|
|
105
|
+
*/
|
|
106
|
+
export async function simulateTransaction(
|
|
107
|
+
args: { aptosConfig: AptosConfig } & SimulateTransactionData,
|
|
108
|
+
): Promise<Array<UserTransactionResponse>> {
|
|
109
|
+
const { aptosConfig, transaction, signerPublicKey, secondarySignersPublicKeys, feePayerPublicKey, options } = args;
|
|
110
|
+
|
|
111
|
+
const signedTransaction = generateSignedTransactionForSimulation({
|
|
112
|
+
transaction,
|
|
113
|
+
signerPublicKey,
|
|
114
|
+
secondarySignersPublicKeys,
|
|
115
|
+
feePayerPublicKey,
|
|
116
|
+
options,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const { data } = await postAptosFullNode<Uint8Array, Array<UserTransactionResponse>>({
|
|
120
|
+
aptosConfig,
|
|
121
|
+
body: signedTransaction,
|
|
122
|
+
path: "transactions/simulate",
|
|
123
|
+
params: {
|
|
124
|
+
estimate_gas_unit_price: args.options?.estimateGasUnitPrice ?? false,
|
|
125
|
+
estimate_max_gas_amount: args.options?.estimateMaxGasAmount ?? false,
|
|
126
|
+
estimate_prioritized_gas_unit_price: args.options?.estimatePrioritizedGasUnitPrice ?? false,
|
|
127
|
+
},
|
|
128
|
+
originMethod: "simulateTransaction",
|
|
129
|
+
contentType: MimeType.BCS_SIGNED_TRANSACTION,
|
|
130
|
+
});
|
|
131
|
+
return data;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Submit transaction to chain
|
|
136
|
+
*
|
|
137
|
+
* @param args.transaction A aptos transaction type
|
|
138
|
+
* @param args.senderAuthenticator The account authenticator of the transaction sender
|
|
139
|
+
* @param args.secondarySignerAuthenticators optional. For when the transaction is a multi signers transaction
|
|
140
|
+
*
|
|
141
|
+
* @return PendingTransactionResponse
|
|
142
|
+
*/
|
|
143
|
+
export async function submitTransaction(args: {
|
|
144
|
+
aptosConfig: AptosConfig;
|
|
145
|
+
transaction: AnyRawTransaction;
|
|
146
|
+
senderAuthenticator: AccountAuthenticator;
|
|
147
|
+
secondarySignerAuthenticators?: {
|
|
148
|
+
feePayerAuthenticator?: AccountAuthenticator;
|
|
149
|
+
additionalSignersAuthenticators?: Array<AccountAuthenticator>;
|
|
150
|
+
};
|
|
151
|
+
}): Promise<PendingTransactionResponse> {
|
|
152
|
+
const { aptosConfig } = args;
|
|
153
|
+
const signedTransaction = generateSignedTransaction({ ...args });
|
|
154
|
+
const { data } = await postAptosFullNode<Uint8Array, PendingTransactionResponse>({
|
|
155
|
+
aptosConfig,
|
|
156
|
+
body: signedTransaction,
|
|
157
|
+
path: "transactions",
|
|
158
|
+
originMethod: "submitTransaction",
|
|
159
|
+
contentType: MimeType.BCS_SIGNED_TRANSACTION,
|
|
160
|
+
});
|
|
161
|
+
return data;
|
|
162
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
5
|
+
|
|
6
|
+
import { Serializer, Deserializer, Serializable } from "../../bcs";
|
|
7
|
+
import { Ed25519PublicKey, Ed25519Signature } from "../../core/crypto/ed25519";
|
|
8
|
+
import { MultiEd25519PublicKey, MultiEd25519Signature } from "../../core/crypto/multiEd25519";
|
|
9
|
+
import { Secp256k1PublicKey, Secp256k1Signature } from "../../core/crypto/secp256k1";
|
|
10
|
+
import { AccountAuthenticatorVariant } from "../../types";
|
|
11
|
+
|
|
12
|
+
export abstract class AccountAuthenticator extends Serializable {
|
|
13
|
+
abstract serialize(serializer: Serializer): void;
|
|
14
|
+
|
|
15
|
+
static deserialize(deserializer: Deserializer): AccountAuthenticator {
|
|
16
|
+
const index = deserializer.deserializeUleb128AsU32();
|
|
17
|
+
switch (index) {
|
|
18
|
+
case AccountAuthenticatorVariant.Ed25519:
|
|
19
|
+
return AccountAuthenticatorEd25519.load(deserializer);
|
|
20
|
+
case AccountAuthenticatorVariant.MultiEd25519:
|
|
21
|
+
return AccountAuthenticatorMultiEd25519.load(deserializer);
|
|
22
|
+
case AccountAuthenticatorVariant.Secp256k1:
|
|
23
|
+
return AccountAuthenticatorSecp256k1.load(deserializer);
|
|
24
|
+
default:
|
|
25
|
+
throw new Error(`Unknown variant index for AccountAuthenticator: ${index}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Transaction authenticator Ed25519 for a multi signer transaction
|
|
32
|
+
*
|
|
33
|
+
* @param public_key Account's Ed25519 public key.
|
|
34
|
+
* @param signature Account's Ed25519 signature
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
export class AccountAuthenticatorEd25519 extends AccountAuthenticator {
|
|
38
|
+
public readonly public_key: Ed25519PublicKey;
|
|
39
|
+
|
|
40
|
+
public readonly signature: Ed25519Signature;
|
|
41
|
+
|
|
42
|
+
constructor(public_key: Ed25519PublicKey, signature: Ed25519Signature) {
|
|
43
|
+
super();
|
|
44
|
+
this.public_key = public_key;
|
|
45
|
+
this.signature = signature;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
serialize(serializer: Serializer): void {
|
|
49
|
+
serializer.serializeU32AsUleb128(AccountAuthenticatorVariant.Ed25519);
|
|
50
|
+
this.public_key.serialize(serializer);
|
|
51
|
+
this.signature.serialize(serializer);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static load(deserializer: Deserializer): AccountAuthenticatorEd25519 {
|
|
55
|
+
const public_key = Ed25519PublicKey.deserialize(deserializer);
|
|
56
|
+
const signature = Ed25519Signature.deserialize(deserializer);
|
|
57
|
+
return new AccountAuthenticatorEd25519(public_key, signature);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Transaction authenticator Multi Ed25519 for a multi signers transaction
|
|
63
|
+
*
|
|
64
|
+
* @param public_key Account's MultiEd25519 public key.
|
|
65
|
+
* @param signature Account's MultiEd25519 signature
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
export class AccountAuthenticatorMultiEd25519 extends AccountAuthenticator {
|
|
69
|
+
public readonly public_key: MultiEd25519PublicKey;
|
|
70
|
+
|
|
71
|
+
public readonly signature: MultiEd25519Signature;
|
|
72
|
+
|
|
73
|
+
constructor(public_key: MultiEd25519PublicKey, signature: MultiEd25519Signature) {
|
|
74
|
+
super();
|
|
75
|
+
this.public_key = public_key;
|
|
76
|
+
this.signature = signature;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
serialize(serializer: Serializer): void {
|
|
80
|
+
serializer.serializeU32AsUleb128(AccountAuthenticatorVariant.MultiEd25519);
|
|
81
|
+
this.public_key.serialize(serializer);
|
|
82
|
+
this.signature.serialize(serializer);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static load(deserializer: Deserializer): AccountAuthenticatorMultiEd25519 {
|
|
86
|
+
const public_key = MultiEd25519PublicKey.deserialize(deserializer);
|
|
87
|
+
const signature = MultiEd25519Signature.deserialize(deserializer);
|
|
88
|
+
return new AccountAuthenticatorMultiEd25519(public_key, signature);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A Secp256k1 AccountAuthenticator for a single signer
|
|
94
|
+
*
|
|
95
|
+
* @param public_key A Secp256k1 public key
|
|
96
|
+
* @param signature A Secp256k1 signature
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
export class AccountAuthenticatorSecp256k1 extends AccountAuthenticator {
|
|
100
|
+
public readonly public_key: Secp256k1PublicKey;
|
|
101
|
+
|
|
102
|
+
public readonly signature: Secp256k1Signature;
|
|
103
|
+
|
|
104
|
+
constructor(public_key: Secp256k1PublicKey, signature: Secp256k1Signature) {
|
|
105
|
+
super();
|
|
106
|
+
this.public_key = public_key;
|
|
107
|
+
this.signature = signature;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
serialize(serializer: Serializer): void {
|
|
111
|
+
serializer.serializeU32AsUleb128(AccountAuthenticatorVariant.Secp256k1);
|
|
112
|
+
this.public_key.serialize(serializer);
|
|
113
|
+
this.signature.serialize(serializer);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static load(deserializer: Deserializer): AccountAuthenticatorSecp256k1 {
|
|
117
|
+
const public_key = Secp256k1PublicKey.deserialize(deserializer);
|
|
118
|
+
const signature = Secp256k1Signature.deserialize(deserializer);
|
|
119
|
+
return new AccountAuthenticatorSecp256k1(public_key, signature);
|
|
120
|
+
}
|
|
121
|
+
}
|