2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
import { Account as AccountModule } from "../account" ;
5
- import { AccountAddress , PrivateKey , AccountAddressInput , createObjectAddress } from "../core" ;
5
+ import {
6
+ AccountAddress ,
7
+ AccountAddressInput ,
8
+ createObjectAddress ,
9
+ BaseAccountPublicKey ,
10
+ PrivateKeyInput ,
11
+ } from "../core" ;
6
12
import {
7
13
AccountData ,
8
14
AnyNumber ,
@@ -31,6 +37,7 @@ import {
31
37
getAccountOwnedObjects ,
32
38
getAccountOwnedTokens ,
33
39
getAccountOwnedTokensFromCollectionAddress ,
40
+ getAccountsForPublicKey ,
34
41
getAccountTokensCount ,
35
42
getAccountTransactionsCount ,
36
43
getInfo ,
@@ -42,6 +49,8 @@ import {
42
49
getResourcesPage ,
43
50
getTransactions ,
44
51
lookupOriginalAccountAddress ,
52
+ deriveOwnedAccountsFromSigner ,
53
+ AccountInfo ,
45
54
} from "../internal/account" ;
46
55
import { APTOS_COIN , APTOS_FA , ProcessorType } from "../utils/const" ;
47
56
import { AptosConfig } from "./aptosConfig" ;
@@ -977,7 +986,131 @@ export class Account {
977
986
* @group Account
978
987
* @deprecated Note that more inspection is needed by the user to determine which account exists on-chain
979
988
*/
980
- async deriveAccountFromPrivateKey ( args : { privateKey : PrivateKey } ) : Promise < AccountModule > {
989
+ async deriveAccountFromPrivateKey ( args : {
990
+ privateKey : PrivateKeyInput ;
991
+ minimumLedgerVersion ?: AnyNumber ;
992
+ options ?: {
993
+ throwIfNoAccountFound ?: boolean ;
994
+ } ;
995
+ } ) : Promise < AccountModule > {
996
+ await waitForIndexerOnVersion ( {
997
+ config : this . config ,
998
+ minimumLedgerVersion : args . minimumLedgerVersion ,
999
+ processorType : ProcessorType . ACCOUNT_RESTORATION_PROCESSOR ,
1000
+ } ) ;
1001
+ await waitForIndexerOnVersion ( {
1002
+ config : this . config ,
1003
+ minimumLedgerVersion : args . minimumLedgerVersion ,
1004
+ processorType : ProcessorType . OBJECT_PROCESSOR ,
1005
+ } ) ;
981
1006
return deriveAccountFromPrivateKey ( { aptosConfig : this . config , ...args } ) ;
982
1007
}
1008
+
1009
+ /**
1010
+ * Derives all accounts owned by a signer. This function takes a signer (either an Account or PrivateKey)
1011
+ * and returns all accounts that can be derived from it, ordered by the most recently used account first.
1012
+ *
1013
+ * Note, this function will not return accounts that require more than one signer to be used.
1014
+ *
1015
+ * @param args - The arguments for deriving owned accounts
1016
+ * @param args.signer - The signer to derive accounts from (Account or PrivateKey)
1017
+ * @param args.minimumLedgerVersion - The minimum ledger version to wait for before querying
1018
+ * @param args.options.includeUnverified - Whether to include unverified accounts in the results. Unverified accounts
1019
+ * are accounts that can be authenticated with the signer, but there is no history of the signer using the account.
1020
+ * Default is false.
1021
+ * @param args.options.noMultiKey - If true, do not include multi-key accounts in the results. Default is false.
1022
+ * @returns Promise resolving to an array of derived Account objects
1023
+ *
1024
+ * @example
1025
+ * ```typescript
1026
+ * import { Aptos, AptosConfig, Network, Ed25519Account } from "@aptos-labs/ts-sdk";
1027
+ *
1028
+ * const config = new AptosConfig({ network: Network.TESTNET });
1029
+ * const aptos = new Aptos(config);
1030
+ *
1031
+ * async function getOwnedAccounts() {
1032
+ * const signer = Ed25519Account.generate();
1033
+ * const accounts = await aptos.deriveOwnedAccountsFromSigner({
1034
+ * signer
1035
+ * });
1036
+ * const account = accounts[0];
1037
+ * console.log(account);
1038
+ * }
1039
+ * ```
1040
+ * @group Account
1041
+ */
1042
+ async deriveOwnedAccountsFromSigner ( args : {
1043
+ signer : AccountModule | PrivateKeyInput ;
1044
+ minimumLedgerVersion ?: AnyNumber ;
1045
+ options ?: { includeUnverified ?: boolean ; noMultiKey ?: boolean } ;
1046
+ } ) : Promise < AccountModule [ ] > {
1047
+ await waitForIndexerOnVersion ( {
1048
+ config : this . config ,
1049
+ minimumLedgerVersion : args . minimumLedgerVersion ,
1050
+ processorType : ProcessorType . ACCOUNT_RESTORATION_PROCESSOR ,
1051
+ } ) ;
1052
+ await waitForIndexerOnVersion ( {
1053
+ config : this . config ,
1054
+ minimumLedgerVersion : args . minimumLedgerVersion ,
1055
+ processorType : ProcessorType . OBJECT_PROCESSOR ,
1056
+ } ) ;
1057
+ return deriveOwnedAccountsFromSigner ( { aptosConfig : this . config , ...args } ) ;
1058
+ }
1059
+
1060
+ /**
1061
+ * Gets all account info (address, account public key, last transaction version) that have are associated with a public key and **related public keys**
1062
+ *
1063
+ * For a given public key, it will query all multikeys that the public key is part of. Then for the provided public key and
1064
+ * any multikeys found in the previous step, it will query for any accounts that have an auth key that matches any of the
1065
+ * public keys.
1066
+ *
1067
+ * Note: If an Ed25519PublicKey or an AnyPublicKey that wraps Ed25519PublicKey is passed in, it will query for both legacy and single singer cases.
1068
+ *
1069
+ * @param args - The arguments for getting accounts for a public key
1070
+ * @param args.publicKey - The public key to look up accounts for
1071
+ * @param args.minimumLedgerVersion - The minimum ledger version to wait for before querying
1072
+ * @param args.options.includeUnverified - Whether to include unverified accounts in the results. Unverified accounts
1073
+ * are accounts that can be authenticated with the signer, but there is no history of the signer using the account. Default
1074
+ * is false.
1075
+ * @param args.options.noMultiKey - Whether to exclude multi-key accounts in the results. Default is false.
1076
+ * @returns Promise resolving to an array of account addresses and their associated public keys
1077
+ *
1078
+ * @example
1079
+ * ```typescript
1080
+ * import { Aptos, AptosConfig, Network, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";
1081
+ *
1082
+ * const config = new AptosConfig({ network: Network.TESTNET });
1083
+ * const aptos = new Aptos(config);
1084
+ *
1085
+ * async function getAccounts() {
1086
+ * const privateKey = Ed25519PrivateKey.generate();
1087
+ * const publicKey = privateKey.publicKey();
1088
+ * const accounts = await aptos.getAccountsForPublicKey({
1089
+ * publicKey
1090
+ * });
1091
+ * console.log(accounts);
1092
+ * }
1093
+ * ```
1094
+ * @group Account
1095
+ */
1096
+ async getAccountsForPublicKey ( args : {
1097
+ publicKey : BaseAccountPublicKey ;
1098
+ minimumLedgerVersion ?: AnyNumber ;
1099
+ options ?: { includeUnverified ?: boolean ; noMultiKey ?: boolean } ;
1100
+ } ) : Promise < AccountInfo [ ] > {
1101
+ await waitForIndexerOnVersion ( {
1102
+ config : this . config ,
1103
+ minimumLedgerVersion : args . minimumLedgerVersion ,
1104
+ processorType : ProcessorType . ACCOUNT_RESTORATION_PROCESSOR ,
1105
+ } ) ;
1106
+ await waitForIndexerOnVersion ( {
1107
+ config : this . config ,
1108
+ minimumLedgerVersion : args . minimumLedgerVersion ,
1109
+ processorType : ProcessorType . OBJECT_PROCESSOR ,
1110
+ } ) ;
1111
+ return getAccountsForPublicKey ( {
1112
+ aptosConfig : this . config ,
1113
+ ...args ,
1114
+ } ) ;
1115
+ }
983
1116
}
0 commit comments