Creating and Managing Accounts
There are several ways to generate account credentials using the TypeScript SDK. You can use:
Account.generate()
Account.fromPrivateKey()
Account.fromPrivateKeyAndAddress()
Account.fromDerivationPath()
Account.generate()
is the most commonly used method to create keys for a new account.
It defaults to ED25519
key encodings, but you can also manually specify which signing scheme you would prefer like so:
const account = Account.generate(); // defaults to Legacy Ed25519
const account = Account.generate({ scheme: SigningSchemeInput.Secp256k1 }); // Single Sender Secp256k1
const account = Account.generate({
scheme: SigningSchemeInput.Ed25519,
legacy: false,
}); // Single Sender Ed25519
Following AIP-55 the SDK supports Legacy
and Unified
authentications. Legacy
includes ED25519
and MultiED25519
and Unified
includes SingleSender
and MultiSender
authenticators.
Once you have generated credentials, you must fund it for the network to know it exists.
In test environments this can be done with a faucet by running the following command:
const transaction = await aptos.fundAccount({
accountAddress: account.accountAddress,
amount: 100,
});
Other Ways To Represent Accounts
If you have a private key, or equivalent representation, you can use that to create an Account
object to manage those credentials while using the TypeScript SDK.
Here are several examples that show how to do so with specific encoding schemes.
Derive an account from private key
The SDK supports deriving an account from a private key with fromPrivateKey()
static method.
This method uses a local calculation and therefore is used to derive an Account
that has not had its authentication key rotated.
// to derive an account with a legacy Ed25519 key scheme
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const account = Account.fromPrivateKey({ privateKey });
// to derive an account with a Single Sender Ed25519 key scheme
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const account = Account.fromPrivateKey({ privateKey, legacy: false });
// to derive an account with a Single Sender Secp256k1 key scheme
const privateKey = new Secp256k1PrivateKey(privateKeyBytes);
const account = Account.fromPrivateKey({ privateKey });
Derive an account from private key and address
The SDK supports deriving an account from a private key and address with fromPrivateKeyAndAddress()
static method.
// to derive an account with a legacy Ed25519 key scheme
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const accountAddress = AccountAddress.from(address);
const account = Account.fromPrivateKeyAndAddress({
privateKey,
address: accountAddress,
legacy: true,
});
// to derive an account with a Single Sender Ed25519 key scheme
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const accountAddress = AccountAddress.from(address);
const account = Account.fromPrivateKeyAndAddress({
privateKey,
address: accountAddress,
legacy: false,
});
// to derive an account with a Single Sender Secp256k1 key scheme
const privateKey = new Secp256k1PrivateKey(privateKeyBytes);
const accountAddress = AccountAddress.from(address);
const account = Account.fromPrivateKeyAndAddress({
privateKey,
address: accountAddress,
});
Derive an account from derivation path
The SDK supports deriving an account from derivation path with fromDerivationPath()
static method.
// to derive an account with a legacy Ed25519 key scheme
const { mnemonic, address, path } = wallet;
const account = Account.fromDerivationPath({
path,
mnemonic,
scheme: SigningSchemeInput.Ed25519,
});
// to derive an account with a Single Sender Ed25519 key scheme
const { mnemonic, address, path } = wallet;
const account = Account.fromDerivationPath({
path,
mnemonic,
scheme: SigningSchemeInput.Ed25519,
legacy: false,
});
// to derive an account with a Single Sender Secp256k1 key scheme
const { mnemonic, address, path } = wallet;
const account = Account.fromDerivationPath({
path,
mnemonic,
scheme: SigningSchemeInput.Secp256k1Ecdsa,
});