@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
104 lines • 4.71 kB
JavaScript
import { SigningSchemeInput } from "../types/index.js";
import { Ed25519PrivateKey } from "../core/crypto/ed25519.js";
import { Ed25519Account } from "./Ed25519Account.js";
import { SingleKeyAccount } from "./SingleKeyAccount.js";
/**
* Abstract class representing a generic Aptos account.
*
* This class serves as a single entry point for account generation, allowing accounts to be created
* either through `Account.generate()` or `Account.fromDerivationPath`. Although it is defined as an
* abstract class, it should be treated as an interface and enforced using the `implements` keyword.
*
* Note: Generating an account instance does not create the account on-chain.
* @group Implementation
* @category Account (On-Chain Model)
*/
export class Account {
static generate(args = {}) {
const { scheme = SigningSchemeInput.Ed25519, legacy = true } = args;
if (scheme === SigningSchemeInput.Ed25519 && legacy) {
return Ed25519Account.generate();
}
return SingleKeyAccount.generate({ scheme });
}
static fromPrivateKey(args) {
const { privateKey, address, legacy = true } = args;
if (privateKey instanceof Ed25519PrivateKey && legacy) {
return new Ed25519Account({
privateKey,
address,
});
}
return new SingleKeyAccount({ privateKey, address });
}
/**
* @deprecated use `fromPrivateKey` instead.
* Instantiates an account using a private key and a specified account address. This is primarily used to instantiate an
* `Account` that has had its authentication key rotated.
*
* @param args - The arguments required to create an account from a private key.
* @param args.privateKey - The underlying private key for the account.
* @param args.address - The account address the `Account` will sign for.
* @param args.legacy - Optional. If set to false, the keypair generated is a Unified keypair. Defaults to generating a Legacy
* Ed25519 keypair.
*
* @returns Account
* @group Implementation
* @category Account (On-Chain Model)
*/
static fromPrivateKeyAndAddress(args) {
return this.fromPrivateKey(args);
}
static fromDerivationPath(args) {
const { scheme = SigningSchemeInput.Ed25519, mnemonic, path, legacy = true } = args;
if (scheme === SigningSchemeInput.Ed25519 && legacy) {
return Ed25519Account.fromDerivationPath({ mnemonic, path });
}
return SingleKeyAccount.fromDerivationPath({ scheme, mnemonic, path });
}
/**
* Retrieve the authentication key for the associated account using the provided public key.
* This key enables account owners to rotate their private key(s) associated with the account without changing the address that
* hosts their account.
* See here for more info: {@link https://aptos.dev/concepts/accounts#single-signer-authentication}
*
* @param args - The arguments for retrieving the authentication key.
* @param args.publicKey - The public key of the account.
* @returns The authentication key for the associated account.
* @group Implementation
* @category Account (On-Chain Model)
*/
static authKey(args) {
const { publicKey } = args;
return publicKey.authKey();
}
/**
* Verify the given message and signature with the public key.
* This function helps ensure the integrity and authenticity of a message by validating its signature.
*
* @param args - The arguments for verifying the signature.
* @param args.message - The raw message data in HexInput format.
* @param args.signature - The signed message signature.
* @returns A boolean indicating whether the signature is valid.
* @group Implementation
* @category Account (On-Chain Model)
*/
verifySignature(args) {
return this.publicKey.verifySignature(args);
}
/**
* Verify the given message and signature with the public key. It fetches any on chain state if needed for verification.
*
* @param args - The arguments for verifying the signature.
* @param args.aptosConfig - The configuration object for connecting to the Aptos network
* @param args.message - Raw message data in HexInput format.
* @param args.signature - Signed message signature.
* @returns A boolean indicating whether the signature is valid.
* @group Implementation
* @category Account (On-Chain Model)
*/
async verifySignatureAsync(args) {
return this.publicKey.verifySignatureAsync(args);
}
}
//# sourceMappingURL=Account.js.map