mcpField
T
ts-sdk-account

ts-sdk-account

github.com/aptos-labsDevelopment2070 installsUpdated 24w ago
Repository

About ts-sdk-account

ts-sdk-account is a reference skill for creating and using Account signers with @aptos-labs/ts-sdk. It covers signing schemes, key handling, derivation, serialization, and signature checks, with guidance to keep private-key operations on servers or scripts.

Key features

  1. 01Generate accounts or construct them from private keys
  2. 02Work with multiple signing schemes
  3. 03Derive accounts from mnemonics and paths
  4. 04Serialize accounts and derive auth keys
  5. 05Sign and verify messages and transactions
  6. 06Look up accounts owned by a signer

Use cases

  1. 01Create signers for server-side scripts
  2. 02Select an account type for a signing scheme
  3. 03Sign or verify messages and transactions
  4. 04Find on-chain accounts associated with a key

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/aptos-labs/aptos-agent-skills --skill ts-sdk-account

After installing, type/ts-sdk-accountin Claude Code to confirm it loaded.

Details

From the project README

TypeScript SDK: Account (Signer)

Purpose

Guide creation and use of Account (signer) in @aptos-labs/ts-sdk. An Account holds address + key material and can sign transactions and messages. Creating an Account does NOT create the account on-chain; use faucet or transfer to fund it.

ALWAYS

  1. Use Account.generate() or Account.fromPrivateKey() only in server/script – never in frontend; use wallet adapter for end users.
  2. Load private keys from env (e.g. process.env.PRIVATE_KEY) on server – never hardcode.
  3. Use account.accountAddress when building transactions – pass as sender/secondary signers.
  4. Use aptos.signAndSubmitTransaction({ signer: account, transaction }) with the same Account instance that holds the key.

NEVER

  1. Do not use Account.generate() or raw private keys in browser/frontend – use wallet adapter.
  2. Do not hardcode private keys in source or commit to git.
  3. Do not confuse Account (API namespace) with Account (signer class) – API is aptos.account.*; signer is the class from Account module (e.g. Account.fromPrivateKey).

Account types (signing schemes)

TypeClassUse case
Ed25519 (legacy)Ed25519AccountSingle Ed25519 key, legacy auth
SingleKeySingleKeyAccountEd25519 or Secp256k1, unified auth
MultiKeyMultiKeyAccountMulti-sig
KeylessKeylessAccountKeyless (e.g. OIDC)
Federated KeylessFederatedKeylessAccountFederated keyless

Generate new account (server/script only)

import { Account, SigningSchemeInput } from "@aptos-labs/ts-sdk";

// Default: Ed25519 legacy
const ed25519Account = Account.generate();

// SingleKey (unified) with Ed25519
const singleKeyAccount = Account.generate({ scheme: SigningSchemeInput.Ed25519, legacy: false });

// SingleKey with Secp256k1
const secpAccount = Account.generate({ scheme: SigningSchemeInput.Secp256k1 });

// Access address and public key
const address = ed25519Account.accountAddress;
const pubKey = ed25519Account.publicKey;

From private key

import { Account, Ed25519PrivateKey, Secp256k1PrivateKey } from "@aptos-labs/ts-sdk";

// Ed25519 legacy (default)
const privateKeyHex = process.env.PRIVATE_KEY!; // e.g. "0x..." or AIP-80 prefixed
const privateKey = new Ed25519PrivateKey(privateKeyHex);
const account = Account.fromPrivateKey({ privateKey });

// Ed25519 SingleKey (unified)
const accountSingle = Account.fromPrivateKey({
  privateKey: new Ed25519PrivateKey(privateKeyHex),
  legacy: false
});

// Secp256k1 (always SingleKey)
const secpKey = new Secp256k1PrivateKey(process.env.SECP_KEY!);
const accountSecp = Account.fromPrivateKey({ privateKey: secpKey });

// Optional: fixed address (e.g. after key rotation)
const accountWithAddr = Account.fromPrivateKey({
  privateKey,
  address: "0x..."
});

From mnemonic (derivation path)

import { Account } from "@aptos-labs/ts-sdk";

const mnemonic = "word1 word2 ... word12";
const path = "m/44'/637'/0'/0'/0'"; // Aptos BIP-44 path

// Ed25519 legacy
const acc = Account.fromDerivationPath({ mnemonic, path });

// Ed25519 SingleKey
const accSingle = Account.fromDerivationPath({ mnemonic, path, legacy: false });

// Secp256k1
const accSecp = Account.fromDerivationPath({
  scheme: SigningSchemeInput.Secp256k1,
  mnemonic,
  path
});

Auth key (for rotation / lookup)

const authKey = Account.authKey({ publicKey: account.publicKey });
// Use authKey.derivedAddress() for address derivation; useful for multi-account lookup

Serialization (toHex / fromHex)

Use when persisting or sending account (e.g. server-only, never expose private key to frontend):

import { Account, AccountUtils } from "@aptos-labs/ts-sdk";

const account = Account.generate();

// Serialize to hex (includes private key – treat as secret)
const hex = AccountUtils.toHexString(account);

// Deserialize back
const restored = AccountUtils.fromHex(hex);

// Typed deserialize
const edAccount = AccountUtils.ed25519AccountFromHex(hex);
const singleAccount = AccountUtils.singleKeyAccountFromHex(hex);
const multiAccount = AccountUtils.multiKeyAccountFromHex(hex);
const keylessAccount = AccountUtils.keylessAccountFromHex(hex);

Signing

// Sign message (returns Signature)
const sig = account.sign(messageHex);

// Sign transaction (returns Signature; for submit use aptos.transaction.sign + submit)
const txSig = account.signTransaction(rawTransaction);

// With authenticator (used by SDK internally for submit)
const auth = account.signWithAuthenticator(messageHex);

Verify signature

const ok = account.verifySignature({ message: messageHex, signature: sig });

// Async (if key type needs on-chain state)
const okAsync = await account.verifySignatureAsync({
  aptosConfig: aptos.config,
  message: messageHex,
  signature: sig
});

Derive account from private key (on-chain lookup)

When the same key may have multiple on-chain accounts (e.g. after rotation), use internal derivation + lookup:

// Returns list of accounts owned by this key on chain
const accounts = await aptos.deriveOwnedAccountsFromSigner({
  signer: account
});
// Prefer wallet or explicit address for production; this is for scripts/tooling

Common mistakes

MistakeCorrect approach
Using Account.generate() in frontendUse wallet adapter; generate only in server/script
Hardcoding private keyLoad from process.env (server) and never commit
Using aptos.account as signeraptos.account is API namespace; signer is Account.fromPrivateKey() / Account.generate()
Expecting account to exist on-chain after generateFund with faucet or transfer first

References

Comments

Markdown supported