Private Keys

Stacks applications can work with two different account types: web wallets (like Hiro Wallet or Xverse) that users control, or local accounts you manage the private keys directly.

Web wallets (user-controlled)

Most users interact with Stacks apps through web wallets, where the wallet handles all private key management and transaction signing.

import { connect } from '@stacks/connect';

// Users connect their wallet
const response = await connect();
console.log('Connected addresses:', response.addresses);

// The wallet handles all cryptographic operations
// when signing transactions or messages

Use web wallets when building user-facing applications where users should maintain control of their keys.

Local accounts (application-controlled)

Local accounts give your application direct control over private keys, enabling programmatic transaction signing without user interaction.

import { makeSTXTokenTransfer } from '@stacks/transactions';

// Your application controls the private key
const privateKey = 'your-private-key-here';

const txOptions = {
  recipient: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM',
  amount: 1000000n,
  senderKey: privateKey, // Direct private key usage
  network: 'testnet',
};

const transaction = await makeSTXTokenTransfer(txOptions);
// Transaction is signed programmatically

Use local accounts for backend services, automated systems, or development tools that need to sign transactions without user interaction.

Working with private keys

When building applications that use local accounts, you'll need to generate and manage private keys securely.

Generating random private keys

Create a new private key for one-time use or testing purposes.

Private key formats

Stacks.js supports multiple private key formats for different use cases.

The compressed format includes a suffix byte (01) that indicates the key should use compressed public key encoding.

Wallet generation with seed phrases

For better security and recoverability, use hierarchical deterministic (HD) wallets based on seed phrases.

1

Generate a seed phrase

Create a new 24-word mnemonic seed phrase that can regenerate all wallet accounts.

2

Create wallet from seed phrase

Generate a complete wallet structure from a seed phrase, including multiple accounts.

Each wallet can contain multiple accounts, all derived from the same seed phrase but with different private keys.

3

Managing multiple accounts

HD wallets support multiple accounts from a single seed phrase, useful for organizing funds or separating concerns.

All accounts can be regenerated from the original seed phrase, making backup simple while maintaining separate addresses.

Last updated

Was this helpful?