@aptos-labs/ts-sdk 0.0.3 → 0.0.4

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 (56) hide show
  1. package/README.md +1 -0
  2. package/dist/browser/index.global.js +26 -25
  3. package/dist/browser/index.global.js.map +1 -1
  4. package/dist/cjs/index.d.ts +413 -227
  5. package/dist/cjs/index.js +451 -181
  6. package/dist/cjs/index.js.map +1 -1
  7. package/dist/esm/index.d.ts +413 -227
  8. package/dist/esm/index.mjs +440 -178
  9. package/dist/esm/index.mjs.map +1 -1
  10. package/dist/types/index.d.ts +11 -17
  11. package/dist/types/index.js.map +1 -1
  12. package/package.json +3 -2
  13. package/src/api/account.ts +17 -18
  14. package/src/api/ans.ts +58 -0
  15. package/src/api/aptos.ts +7 -1
  16. package/src/api/coin.ts +3 -3
  17. package/src/api/digitalAsset.ts +9 -8
  18. package/src/api/event.ts +4 -3
  19. package/src/api/faucet.ts +3 -2
  20. package/src/api/general.ts +2 -2
  21. package/src/api/staking.ts +5 -4
  22. package/src/api/transactionSubmission.ts +27 -18
  23. package/src/bcs/deserializer.ts +4 -4
  24. package/src/bcs/serializable/moveStructs.ts +5 -9
  25. package/src/bcs/serializer.ts +4 -4
  26. package/src/client/core.ts +14 -6
  27. package/src/core/account.ts +83 -29
  28. package/src/core/accountAddress.ts +34 -30
  29. package/src/core/authenticationKey.ts +11 -9
  30. package/src/core/crypto/ed25519.ts +48 -1
  31. package/src/core/crypto/hdKey.ts +105 -0
  32. package/src/core/crypto/index.ts +1 -0
  33. package/src/core/crypto/secp256k1.ts +36 -0
  34. package/src/index.ts +0 -1
  35. package/src/internal/account.ts +80 -58
  36. package/src/internal/ans.ts +177 -0
  37. package/src/internal/coin.ts +5 -5
  38. package/src/internal/digitalAsset.ts +16 -17
  39. package/src/internal/event.ts +7 -7
  40. package/src/internal/faucet.ts +4 -4
  41. package/src/internal/general.ts +3 -3
  42. package/src/internal/staking.ts +8 -8
  43. package/src/internal/transactionSubmission.ts +71 -18
  44. package/src/transactions/instances/index.ts +1 -0
  45. package/src/transactions/instances/moduleId.ts +1 -1
  46. package/src/transactions/instances/rotationProofChallenge.ts +58 -0
  47. package/src/transactions/transactionBuilder/helpers.ts +5 -1
  48. package/src/transactions/transactionBuilder/remoteAbi.ts +3 -3
  49. package/src/transactions/transactionBuilder/transactionBuilder.ts +31 -45
  50. package/src/transactions/typeTag/index.ts +10 -10
  51. package/src/transactions/typeTag/parser.ts +1 -1
  52. package/src/transactions/types.ts +28 -17
  53. package/src/types/index.ts +11 -16
  54. package/src/utils/apiEndpoints.ts +8 -0
  55. package/src/version.ts +1 -1
  56. package/src/utils/hdKey.ts +0 -113
@@ -0,0 +1,177 @@
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/name}. By moving the methods out into a separate file,
7
+ * other namespaces and processes can access these methods without depending on the entire
8
+ * name namespace and without having a dependency cycle error.
9
+ */
10
+
11
+ import { AptosConfig } from "../api/aptosConfig";
12
+ import { MoveOption, MoveString, U64 } from "../bcs";
13
+ import { Account, AccountAddress } from "../core";
14
+ import { InputGenerateTransactionOptions, InputSingleSignerTransaction } from "../transactions/types";
15
+ import { HexInput, MoveAddressType, MoveValue } from "../types";
16
+ import { Network } from "../utils/apiEndpoints";
17
+ import { view } from "./general";
18
+ import { generateTransaction } from "./transactionSubmission";
19
+
20
+ export const VALIDATION_RULES_DESCRIPTION = [
21
+ "A name must be between 3 and 63 characters long,",
22
+ "and can only contain lowercase a-z, 0-9, and hyphens.",
23
+ "A name may not start or end with a hyphen.",
24
+ ].join(" ");
25
+
26
+ /**
27
+ *
28
+ * @param fragment A fragment of a name, either the domain or subdomain
29
+ * @returns boolean indicating if the fragment is a valid fragment
30
+ */
31
+ export function isValidANSSegment(fragment: string): boolean {
32
+ if (!fragment) return false;
33
+ if (fragment.length < 3) return false;
34
+ if (fragment.length > 63) return false;
35
+ // only lowercase a-z and 0-9 are allowed, along with -. a domain may not start or end with a hyphen
36
+ if (!/^[a-z\d][a-z\d-]{1,61}[a-z\d]$/.test(fragment)) return false;
37
+ return true;
38
+ }
39
+
40
+ /**
41
+ * Checks if an ANS name is valid or not
42
+ *
43
+ * @param name A string of the domain name, can include or exclude the .apt suffix
44
+ */
45
+ export function isValidANSName(name: string): { domainName: string; subdomainName?: string } {
46
+ const [first, second, ...rest] = name.replace(/\.apt$/, "").split(".");
47
+
48
+ if (rest.length > 0) {
49
+ throw new Error(`${name} is invalid. A name can only have two parts, a domain and a subdomain separated by a "."`);
50
+ }
51
+
52
+ if (!isValidANSSegment(first)) {
53
+ throw new Error(`${first} is not valid. ${VALIDATION_RULES_DESCRIPTION}`);
54
+ }
55
+
56
+ if (second && !isValidANSSegment(second)) {
57
+ throw new Error(`${second} is not valid. ${VALIDATION_RULES_DESCRIPTION}`);
58
+ }
59
+
60
+ return {
61
+ domainName: second || first,
62
+ subdomainName: second ? first : undefined,
63
+ };
64
+ }
65
+
66
+ export const LOCAL_ANS_ACCOUNT_PK =
67
+ (process.env.ANS_TEST_ACCOUNT_PRIVATE_KEY as string) ||
68
+ "0x37368b46ce665362562c6d1d4ec01a08c8644c488690df5a17e13ba163e20221";
69
+ export const LOCAL_ANS_ACCOUNT_ADDRESS =
70
+ (process.env.ANS_TEST_ACCOUNT_ADDRESS as string) ||
71
+ "0x585fc9f0f0c54183b039ffc770ca282ebd87307916c215a3e692f2f8e4305e82";
72
+
73
+ const NetworkToAnsContract: Record<Network, string | null> = {
74
+ [Network.TESTNET]: "0x5f8fd2347449685cf41d4db97926ec3a096eaf381332be4f1318ad4d16a8497c",
75
+ [Network.MAINNET]: "0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c",
76
+ [Network.LOCAL]: LOCAL_ANS_ACCOUNT_ADDRESS,
77
+ [Network.CUSTOM]: null,
78
+ [Network.DEVNET]: null,
79
+ };
80
+
81
+ function getRouterAddress(aptosConfig: AptosConfig): string {
82
+ const address = NetworkToAnsContract[aptosConfig.network];
83
+ if (!address) throw new Error(`The ANS contract is not deployed to ${aptosConfig.network}`);
84
+ return address;
85
+ }
86
+
87
+ const Some = <T>(value: T): MoveValue => ({ vec: [value] } as any);
88
+ const None = (): MoveValue => ({ vec: [] } as any);
89
+ // != here is intentional, we want to check for null and undefined
90
+ // eslint-disable-next-line eqeqeq
91
+ const Option = <T>(value: T | undefined | null): MoveValue => (value != undefined ? Some(value) : None());
92
+
93
+ const unwrapOption = <T>(option: any): T | undefined => {
94
+ if (!!option && typeof option === "object" && "vec" in option && Array.isArray(option.vec)) {
95
+ return option.vec[0];
96
+ }
97
+
98
+ return undefined;
99
+ };
100
+
101
+ export async function getOwnerAddress(args: { aptosConfig: AptosConfig; name: string }): Promise<string | undefined> {
102
+ const { aptosConfig, name } = args;
103
+ const routerAddress = getRouterAddress(aptosConfig);
104
+ const { domainName, subdomainName } = isValidANSName(name);
105
+
106
+ const res = await view({
107
+ aptosConfig,
108
+ payload: {
109
+ function: `${routerAddress}::router::get_owner_addr`,
110
+ functionArguments: [domainName, Option(subdomainName)],
111
+ },
112
+ });
113
+
114
+ const owner = unwrapOption<MoveAddressType>(res[0]);
115
+
116
+ return owner ? AccountAddress.fromRelaxed(owner).toString() : undefined;
117
+ }
118
+
119
+ export interface RegisterNameParameters {
120
+ aptosConfig: AptosConfig;
121
+ sender: Account;
122
+ name: string;
123
+ expiration:
124
+ | { policy: "domain"; years: 1 }
125
+ | { policy: "subdomain:follow-domain" }
126
+ | { policy: "subdomain:independent"; expirationDate: Date };
127
+ transferable?: boolean;
128
+ toAddress?: HexInput;
129
+ targetAddress?: HexInput;
130
+ options?: InputGenerateTransactionOptions;
131
+ }
132
+
133
+ export async function registerName(args: RegisterNameParameters): Promise<InputSingleSignerTransaction> {
134
+ const { aptosConfig, expiration, name, sender, targetAddress, toAddress, options } = args;
135
+ const routerAddress = getRouterAddress(aptosConfig);
136
+ const { domainName, subdomainName } = isValidANSName(name);
137
+
138
+ const hasSubdomainPolicy =
139
+ expiration.policy === "subdomain:independent" || expiration.policy === "subdomain:follow-domain";
140
+
141
+ if (subdomainName && !hasSubdomainPolicy) {
142
+ throw new Error(
143
+ "Subdomains must have an expiration policy of either 'subdomain:independent' or 'subdomain:follow-domain'",
144
+ );
145
+ }
146
+
147
+ if (hasSubdomainPolicy && !subdomainName) {
148
+ throw new Error(`Policy is set to ${expiration.policy} but no subdomain was provided`);
149
+ }
150
+
151
+ if (expiration.policy === "domain") {
152
+ if (expiration.years !== 1) {
153
+ throw new Error("For now, names can only be registered for 1 year at a time");
154
+ }
155
+
156
+ const registrationDuration = expiration.years * 31536000;
157
+
158
+ const transaction = await generateTransaction({
159
+ aptosConfig,
160
+ sender: sender.accountAddress.toString(),
161
+ data: {
162
+ function: `${routerAddress}::router::register_domain`,
163
+ functionArguments: [
164
+ new MoveString(domainName),
165
+ new U64(registrationDuration),
166
+ new MoveOption(targetAddress ? AccountAddress.from(targetAddress) : null),
167
+ new MoveOption(toAddress ? AccountAddress.from(toAddress) : null),
168
+ ],
169
+ },
170
+ options,
171
+ });
172
+
173
+ return transaction as InputSingleSignerTransaction;
174
+ }
175
+
176
+ throw new Error(`Policy ${expiration.policy} is not supported yet`);
177
+ }
@@ -1,8 +1,8 @@
1
1
  import { AptosConfig } from "../api/aptosConfig";
2
2
  import { U64 } from "../bcs/serializable/movePrimitives";
3
- import { Account, AccountAddress } from "../core";
3
+ import { Account, AccountAddress, AccountAddressInput } from "../core";
4
4
  import { InputGenerateTransactionOptions, InputSingleSignerTransaction } from "../transactions/types";
5
- import { HexInput, AnyNumber, MoveStructType } from "../types";
5
+ import { AnyNumber, MoveStructType } from "../types";
6
6
  import { APTOS_COIN } from "../utils/const";
7
7
  import { generateTransaction } from "./transactionSubmission";
8
8
  import { parseTypeTag } from "../transactions/typeTag/parser";
@@ -10,7 +10,7 @@ import { parseTypeTag } from "../transactions/typeTag/parser";
10
10
  export async function transferCoinTransaction(args: {
11
11
  aptosConfig: AptosConfig;
12
12
  sender: Account;
13
- recipient: HexInput;
13
+ recipient: AccountAddressInput;
14
14
  amount: AnyNumber;
15
15
  coinType?: MoveStructType;
16
16
  options?: InputGenerateTransactionOptions;
@@ -19,11 +19,11 @@ export async function transferCoinTransaction(args: {
19
19
  const coinStructType = coinType ?? APTOS_COIN;
20
20
  const transaction = await generateTransaction({
21
21
  aptosConfig,
22
- sender: sender.accountAddress.toString(),
22
+ sender: sender.accountAddress,
23
23
  data: {
24
24
  function: "0x1::aptos_account::transfer_coins",
25
25
  typeArguments: [parseTypeTag(coinStructType)],
26
- functionArguments: [AccountAddress.fromHexInput(recipient), new U64(amount)],
26
+ functionArguments: [AccountAddress.from(recipient), new U64(amount)],
27
27
  },
28
28
  options,
29
29
  });
@@ -10,7 +10,7 @@
10
10
 
11
11
  import { AptosConfig } from "../api/aptosConfig";
12
12
  import { MoveString, MoveVector, Bool, U64, U8 } from "../bcs";
13
- import { Account, Hex } from "../core";
13
+ import { Account, AccountAddress, AccountAddressInput } from "../core";
14
14
  import { InputGenerateTransactionOptions, InputSingleSignerTransaction } from "../transactions/types";
15
15
  import {
16
16
  AnyNumber,
@@ -19,7 +19,6 @@ import {
19
19
  GetOwnedTokensResponse,
20
20
  GetTokenActivityResponse,
21
21
  GetTokenDataResponse,
22
- HexInput,
23
22
  OrderBy,
24
23
  PaginationArgs,
25
24
  TokenStandard,
@@ -60,7 +59,7 @@ export async function mintTokenTransaction(args: {
60
59
  const { aptosConfig, options, creator } = args;
61
60
  const transaction = await generateTransaction({
62
61
  aptosConfig,
63
- sender: creator.accountAddress.toString(),
62
+ sender: creator.accountAddress,
64
63
  data: {
65
64
  function: "0x4::aptos_token::mint",
66
65
  functionArguments: [
@@ -80,12 +79,12 @@ export async function mintTokenTransaction(args: {
80
79
 
81
80
  export async function getTokenData(args: {
82
81
  aptosConfig: AptosConfig;
83
- tokenAddress: HexInput;
82
+ tokenAddress: AccountAddressInput;
84
83
  }): Promise<GetTokenDataResponse> {
85
84
  const { aptosConfig, tokenAddress } = args;
86
85
 
87
- const whereCondition: any = {
88
- token_data_id: { _eq: Hex.fromHexInput(tokenAddress).toString() },
86
+ const whereCondition: { token_data_id: { _eq: string } } = {
87
+ token_data_id: { _eq: AccountAddress.from(tokenAddress).toStringLong() },
89
88
  };
90
89
 
91
90
  const graphqlQuery = {
@@ -106,12 +105,12 @@ export async function getTokenData(args: {
106
105
 
107
106
  export async function getCurrentTokenOwnership(args: {
108
107
  aptosConfig: AptosConfig;
109
- tokenAddress: HexInput;
108
+ tokenAddress: AccountAddressInput;
110
109
  }): Promise<GetCurrentTokenOwnershipResponse> {
111
110
  const { aptosConfig, tokenAddress } = args;
112
111
 
113
112
  const whereCondition: CurrentTokenOwnershipsV2BoolExp = {
114
- token_data_id: { _eq: Hex.fromHexInput(tokenAddress).toString() },
113
+ token_data_id: { _eq: AccountAddress.from(tokenAddress).toStringLong() },
115
114
  };
116
115
 
117
116
  const graphqlQuery = {
@@ -132,7 +131,7 @@ export async function getCurrentTokenOwnership(args: {
132
131
 
133
132
  export async function getOwnedTokens(args: {
134
133
  aptosConfig: AptosConfig;
135
- ownerAddress: HexInput;
134
+ ownerAddress: AccountAddressInput;
136
135
  options?: {
137
136
  pagination?: PaginationArgs;
138
137
  orderBy?: OrderBy<GetTokenActivityResponse[0]>;
@@ -141,7 +140,7 @@ export async function getOwnedTokens(args: {
141
140
  const { aptosConfig, ownerAddress, options } = args;
142
141
 
143
142
  const whereCondition: CurrentTokenOwnershipsV2BoolExp = {
144
- owner_address: { _eq: Hex.fromHexInput(ownerAddress).toString() },
143
+ owner_address: { _eq: AccountAddress.from(ownerAddress).toStringLong() },
145
144
  };
146
145
 
147
146
  const graphqlQuery = {
@@ -165,7 +164,7 @@ export async function getOwnedTokens(args: {
165
164
 
166
165
  export async function getTokenActivity(args: {
167
166
  aptosConfig: AptosConfig;
168
- tokenAddress: HexInput;
167
+ tokenAddress: AccountAddressInput;
169
168
  options?: {
170
169
  pagination?: PaginationArgs;
171
170
  orderBy?: OrderBy<GetTokenActivityResponse[0]>;
@@ -174,7 +173,7 @@ export async function getTokenActivity(args: {
174
173
  const { aptosConfig, tokenAddress, options } = args;
175
174
 
176
175
  const whereCondition: TokenActivitiesV2BoolExp = {
177
- token_data_id: { _eq: Hex.fromHexInput(tokenAddress).toString() },
176
+ token_data_id: { _eq: AccountAddress.from(tokenAddress).toStringLong() },
178
177
  };
179
178
 
180
179
  const graphqlQuery = {
@@ -224,7 +223,7 @@ export async function createCollectionTransaction(
224
223
  const { aptosConfig, options, creator } = args;
225
224
  const transaction = await generateTransaction({
226
225
  aptosConfig,
227
- sender: creator.accountAddress.toString(),
226
+ sender: creator.accountAddress,
228
227
  data: {
229
228
  function: "0x4::aptos_token::create_collection",
230
229
  functionArguments: [
@@ -253,18 +252,18 @@ export async function createCollectionTransaction(
253
252
 
254
253
  export async function getCollectionData(args: {
255
254
  aptosConfig: AptosConfig;
256
- creatorAddress: HexInput;
255
+ creatorAddress: AccountAddressInput;
257
256
  collectionName: string;
258
257
  options?: {
259
258
  tokenStandard?: TokenStandard;
260
259
  };
261
260
  }): Promise<GetCollectionDataResponse> {
262
261
  const { aptosConfig, creatorAddress, collectionName, options } = args;
263
- const address = Hex.fromHexInput(creatorAddress).toString();
262
+ const address = AccountAddress.from(creatorAddress);
264
263
 
265
264
  const whereCondition: any = {
266
265
  collection_name: { _eq: collectionName },
267
- creator_address: { _eq: address },
266
+ creator_address: { _eq: address.toStringLong() },
268
267
  };
269
268
 
270
269
  if (options?.tokenStandard) {
@@ -288,7 +287,7 @@ export async function getCollectionData(args: {
288
287
 
289
288
  export async function getCollectionId(args: {
290
289
  aptosConfig: AptosConfig;
291
- creatorAddress: HexInput;
290
+ creatorAddress: AccountAddressInput;
292
291
  collectionName: string;
293
292
  options?: {
294
293
  tokenStandard?: TokenStandard;
@@ -9,8 +9,8 @@
9
9
  */
10
10
 
11
11
  import { AptosConfig } from "../api/aptosConfig";
12
- import { AccountAddress } from "../core";
13
- import { AnyNumber, GetEventsResponse, HexInput, PaginationArgs, MoveStructType, OrderBy } from "../types";
12
+ import { AccountAddress, AccountAddressInput } from "../core";
13
+ import { AnyNumber, GetEventsResponse, PaginationArgs, MoveStructType, OrderBy } from "../types";
14
14
  import { GetEventsQuery } from "../types/generated/operations";
15
15
  import { GetEvents } from "../types/generated/queries";
16
16
  import { EventsBoolExp } from "../types/generated/types";
@@ -18,14 +18,14 @@ import { queryIndexer } from "./general";
18
18
 
19
19
  export async function getAccountEventsByCreationNumber(args: {
20
20
  aptosConfig: AptosConfig;
21
- accountAddress: HexInput;
21
+ accountAddress: AccountAddressInput;
22
22
  creationNumber: AnyNumber;
23
23
  }): Promise<GetEventsResponse> {
24
24
  const { accountAddress, aptosConfig, creationNumber } = args;
25
- const address = AccountAddress.fromHexInput(accountAddress).toString();
25
+ const address = AccountAddress.from(accountAddress);
26
26
 
27
27
  const whereCondition: EventsBoolExp = {
28
- account_address: { _eq: address },
28
+ account_address: { _eq: address.toStringLong() },
29
29
  creation_number: { _eq: creationNumber },
30
30
  };
31
31
 
@@ -34,7 +34,7 @@ export async function getAccountEventsByCreationNumber(args: {
34
34
 
35
35
  export async function getAccountEventsByEventType(args: {
36
36
  aptosConfig: AptosConfig;
37
- accountAddress: HexInput;
37
+ accountAddress: AccountAddressInput;
38
38
  eventType: MoveStructType;
39
39
  options?: {
40
40
  pagination?: PaginationArgs;
@@ -42,7 +42,7 @@ export async function getAccountEventsByEventType(args: {
42
42
  };
43
43
  }): Promise<GetEventsResponse> {
44
44
  const { accountAddress, aptosConfig, eventType, options } = args;
45
- const address = AccountAddress.fromHexInput(accountAddress).toString();
45
+ const address = AccountAddress.fromRelaxed(accountAddress).toStringLong();
46
46
 
47
47
  const whereCondition: EventsBoolExp = {
48
48
  account_address: { _eq: address },
@@ -10,14 +10,14 @@
10
10
 
11
11
  import { AptosConfig } from "../api/aptosConfig";
12
12
  import { postAptosFaucet } from "../client";
13
- import { AccountAddress } from "../core";
14
- import { HexInput, WaitForTransactionOptions } from "../types";
13
+ import { AccountAddress, AccountAddressInput } from "../core";
14
+ import { WaitForTransactionOptions } from "../types";
15
15
  import { DEFAULT_TXN_TIMEOUT_SEC } from "../utils/const";
16
16
  import { waitForTransaction } from "./transaction";
17
17
 
18
18
  export async function fundAccount(args: {
19
19
  aptosConfig: AptosConfig;
20
- accountAddress: HexInput;
20
+ accountAddress: AccountAddressInput;
21
21
  amount: number;
22
22
  options?: WaitForTransactionOptions;
23
23
  }): Promise<string> {
@@ -27,7 +27,7 @@ export async function fundAccount(args: {
27
27
  aptosConfig,
28
28
  path: "fund",
29
29
  body: {
30
- address: AccountAddress.fromHexInput(accountAddress).toString(),
30
+ address: AccountAddress.fromRelaxed(accountAddress).toString(),
31
31
  amount,
32
32
  },
33
33
  originMethod: "fundAccount",
@@ -66,12 +66,12 @@ export async function getBlockByHeight(args: {
66
66
  return data;
67
67
  }
68
68
 
69
- export async function getTableItem(args: {
69
+ export async function getTableItem<T>(args: {
70
70
  aptosConfig: AptosConfig;
71
71
  handle: string;
72
72
  data: TableItemRequest;
73
73
  options?: LedgerVersion;
74
- }): Promise<any> {
74
+ }): Promise<T> {
75
75
  const { aptosConfig, handle, data, options } = args;
76
76
  const response = await postAptosFullNode<TableItemRequest, any>({
77
77
  aptosConfig,
@@ -80,7 +80,7 @@ export async function getTableItem(args: {
80
80
  params: { ledger_version: options?.ledgerVersion },
81
81
  body: data,
82
82
  });
83
- return response.data;
83
+ return response.data as T;
84
84
  }
85
85
 
86
86
  export async function view(args: {
@@ -9,18 +9,18 @@
9
9
  */
10
10
 
11
11
  import { AptosConfig } from "../api/aptosConfig";
12
- import { Hex } from "../core";
13
- import { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsResponse, HexInput, OrderBy } from "../types";
12
+ import { AccountAddress, AccountAddressInput } from "../core";
13
+ import { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsResponse, OrderBy } from "../types";
14
14
  import { GetDelegatedStakingActivitiesQuery, GetNumberOfDelegatorsQuery } from "../types/generated/operations";
15
15
  import { GetDelegatedStakingActivities, GetNumberOfDelegators } from "../types/generated/queries";
16
16
  import { queryIndexer } from "./general";
17
17
 
18
18
  export async function getNumberOfDelegators(args: {
19
19
  aptosConfig: AptosConfig;
20
- poolAddress: HexInput;
20
+ poolAddress: AccountAddressInput;
21
21
  }): Promise<number> {
22
22
  const { aptosConfig, poolAddress } = args;
23
- const address = Hex.fromHexInput(poolAddress).toString();
23
+ const address = AccountAddress.fromRelaxed(poolAddress).toStringLong();
24
24
  const query = {
25
25
  query: GetNumberOfDelegators,
26
26
  variables: { where_condition: { pool_address: { _eq: address } } },
@@ -52,15 +52,15 @@ export async function getNumberOfDelegatorsForAllPools(args: {
52
52
 
53
53
  export async function getDelegatedStakingActivities(args: {
54
54
  aptosConfig: AptosConfig;
55
- delegatorAddress: HexInput;
56
- poolAddress: HexInput;
55
+ delegatorAddress: AccountAddressInput;
56
+ poolAddress: AccountAddressInput;
57
57
  }): Promise<GetDelegatedStakingActivitiesResponse> {
58
58
  const { aptosConfig, delegatorAddress, poolAddress } = args;
59
59
  const query = {
60
60
  query: GetDelegatedStakingActivities,
61
61
  variables: {
62
- delegatorAddress: Hex.fromHexInput(delegatorAddress).toString(),
63
- poolAddress: Hex.fromHexInput(poolAddress).toString(),
62
+ delegatorAddress: AccountAddress.fromRelaxed(delegatorAddress).toStringLong(),
63
+ poolAddress: AccountAddress.fromRelaxed(poolAddress).toStringLong(),
64
64
  },
65
65
  };
66
66
  const data = await queryIndexer<GetDelegatedStakingActivitiesQuery>({ aptosConfig, query });
@@ -6,10 +6,13 @@
6
6
  */
7
7
 
8
8
  import { AptosConfig } from "../api/aptosConfig";
9
- import { MoveVector } from "../bcs";
9
+ import { MoveVector, U8 } from "../bcs";
10
10
  import { postAptosFullNode } from "../client";
11
11
  import { Account } from "../core/account";
12
+ import { AccountAddress, AccountAddressInput } from "../core/accountAddress";
13
+ import { PrivateKey } from "../core/crypto";
12
14
  import { AccountAuthenticator } from "../transactions/authenticator/account";
15
+ import { RotationProofChallenge } from "../transactions/instances/rotationProofChallenge";
13
16
  import {
14
17
  buildTransaction,
15
18
  generateTransactionPayload,
@@ -24,13 +27,15 @@ import {
24
27
  InputGenerateTransactionOptions,
25
28
  InputSingleSignerTransaction,
26
29
  InputGenerateTransactionPayloadDataWithRemoteABI,
30
+ InputSubmitTransactionData,
27
31
  } from "../transactions/types";
28
- import { UserTransactionResponse, PendingTransactionResponse, MimeType, HexInput } from "../types";
32
+ import { getInfo } from "./account";
33
+ import { UserTransactionResponse, PendingTransactionResponse, MimeType, HexInput, TransactionResponse } from "../types";
29
34
 
30
35
  /**
31
36
  * Generates any transaction by passing in the required arguments
32
37
  *
33
- * @param args.sender The transaction sender's account address as a HexInput
38
+ * @param args.sender The transaction sender's account address as a AccountAddressInput
34
39
  * @param args.data EntryFunctionData | ScriptData | MultiSigData
35
40
  * @param args.feePayerAddress optional. For a fee payer (aka sponsored) transaction
36
41
  * @param args.secondarySignerAddresses optional. For a multi-agent or fee payer (aka sponsored) transactions
@@ -58,10 +63,10 @@ import { UserTransactionResponse, PendingTransactionResponse, MimeType, HexInput
58
63
  * }
59
64
  * ```
60
65
  *
61
- * @return A raw transaction type (note that it holds the raw transaction as a bcs serialized data)
66
+ * @return An instance of a RawTransaction, plus optional secondary/fee payer addresses
62
67
  * ```
63
68
  * {
64
- * rawTransaction: Uint8Array,
69
+ * rawTransaction: RawTransaction,
65
70
  * secondarySignerAddresses? : Array<AccountAddress>,
66
71
  * feePayerAddress?: AccountAddress
67
72
  * }
@@ -107,10 +112,10 @@ export async function generateTransaction(
107
112
  * Sign a transaction that can later be submitted to chain
108
113
  *
109
114
  * @param args.signer The signer account to sign the transaction
110
- * @param args.transaction A raw transaction type (note that it holds the raw transaction as a bcs serialized data)
115
+ * @param args.transaction An instance of a RawTransaction, plus optional secondary/fee payer addresses
111
116
  * ```
112
117
  * {
113
- * rawTransaction: Uint8Array,
118
+ * rawTransaction: RawTransaction,
114
119
  * secondarySignerAddresses? : Array<AccountAddress>,
115
120
  * feePayerAddress?: AccountAddress
116
121
  * }
@@ -169,15 +174,11 @@ export async function simulateTransaction(
169
174
  *
170
175
  * @return PendingTransactionResponse
171
176
  */
172
- export async function submitTransaction(args: {
173
- aptosConfig: AptosConfig;
174
- transaction: AnyRawTransaction;
175
- senderAuthenticator: AccountAuthenticator;
176
- secondarySignerAuthenticators?: {
177
- feePayerAuthenticator?: AccountAuthenticator;
178
- additionalSignersAuthenticators?: Array<AccountAuthenticator>;
179
- };
180
- }): Promise<PendingTransactionResponse> {
177
+ export async function submitTransaction(
178
+ args: {
179
+ aptosConfig: AptosConfig;
180
+ } & InputSubmitTransactionData,
181
+ ): Promise<PendingTransactionResponse> {
181
182
  const { aptosConfig } = args;
182
183
  const signedTransaction = generateSignedTransaction({ ...args });
183
184
  const { data } = await postAptosFullNode<Uint8Array, PendingTransactionResponse>({
@@ -206,7 +207,7 @@ export async function signAndSubmitTransaction(args: {
206
207
 
207
208
  export async function publicPackageTransaction(args: {
208
209
  aptosConfig: AptosConfig;
209
- account: HexInput;
210
+ account: AccountAddressInput;
210
211
  metadataBytes: HexInput;
211
212
  moduleBytecode: Array<HexInput>;
212
213
  options?: InputGenerateTransactionOptions;
@@ -217,7 +218,7 @@ export async function publicPackageTransaction(args: {
217
218
 
218
219
  const transaction = await generateTransaction({
219
220
  aptosConfig,
220
- sender: account,
221
+ sender: AccountAddress.fromRelaxed(account),
221
222
  data: {
222
223
  function: "0x1::code::publish_package_txn",
223
224
  functionArguments: [MoveVector.U8(metadataBytes), new MoveVector(totalByteCode)],
@@ -226,3 +227,55 @@ export async function publicPackageTransaction(args: {
226
227
  });
227
228
  return transaction as InputSingleSignerTransaction;
228
229
  }
230
+
231
+ /**
232
+ * TODO: Need to refactor and move this function out of transactionSubmission
233
+ */
234
+ export async function rotateAuthKey(args: {
235
+ aptosConfig: AptosConfig;
236
+ fromAccount: Account;
237
+ toNewPrivateKey: PrivateKey;
238
+ }): Promise<TransactionResponse> {
239
+ const { aptosConfig, fromAccount, toNewPrivateKey } = args;
240
+ const accountInfo = await getInfo({
241
+ aptosConfig,
242
+ accountAddress: fromAccount.accountAddress.toString(),
243
+ });
244
+
245
+ const newAccount = Account.fromPrivateKey({ privateKey: toNewPrivateKey, legacy: true });
246
+
247
+ const challenge = new RotationProofChallenge({
248
+ sequenceNumber: BigInt(accountInfo.sequence_number),
249
+ originator: fromAccount.accountAddress,
250
+ currentAuthKey: AccountAddress.from(accountInfo.authentication_key),
251
+ newPublicKey: newAccount.publicKey,
252
+ });
253
+
254
+ // Sign the challenge
255
+ const challengeHex = challenge.bcsToBytes();
256
+ const proofSignedByCurrentPrivateKey = fromAccount.sign(challengeHex);
257
+ const proofSignedByNewPrivateKey = newAccount.sign(challengeHex);
258
+
259
+ // Generate transaction
260
+ const rawTxn = await generateTransaction({
261
+ aptosConfig,
262
+ sender: fromAccount.accountAddress.toString(),
263
+ data: {
264
+ function: "0x1::account::rotate_authentication_key",
265
+ functionArguments: [
266
+ new U8(fromAccount.signingScheme.valueOf()), // from scheme
267
+ MoveVector.U8(fromAccount.publicKey.toUint8Array()),
268
+ new U8(newAccount.signingScheme.valueOf()), // to scheme
269
+ MoveVector.U8(newAccount.publicKey.toUint8Array()),
270
+ MoveVector.U8(proofSignedByCurrentPrivateKey.toUint8Array()),
271
+ MoveVector.U8(proofSignedByNewPrivateKey.toUint8Array()),
272
+ ],
273
+ },
274
+ });
275
+ const pendingTxn = await signAndSubmitTransaction({
276
+ aptosConfig,
277
+ signer: fromAccount,
278
+ transaction: rawTxn,
279
+ });
280
+ return pendingTxn;
281
+ }
@@ -5,6 +5,7 @@ export * from "./chainId";
5
5
  export * from "./identifier";
6
6
  export * from "./moduleId";
7
7
  export * from "./rawTransaction";
8
+ export * from "./rotationProofChallenge";
8
9
  export * from "./signedTransaction";
9
10
  export * from "./transactionArgument";
10
11
  export * from "./transactionPayload";
@@ -37,7 +37,7 @@ export class ModuleId extends Serializable {
37
37
  if (parts.length !== 2) {
38
38
  throw new Error("Invalid module id.");
39
39
  }
40
- return new ModuleId(AccountAddress.fromString(parts[0]), new Identifier(parts[1]));
40
+ return new ModuleId(AccountAddress.fromStringRelaxed(parts[0]), new Identifier(parts[1]));
41
41
  }
42
42
 
43
43
  serialize(serializer: Serializer): void {