Generate a secret key
Create mnemonic seed phrases for wallet generation
import { generateSecretKey } from '@stacks/wallet-sdk';// Generate a 24-word mnemonic (256 bits of entropy)const mnemonic24 = generateSecretKey();// Example: "aunt birth lounge misery utility blind holiday walnut fuel make gift parent gap picnic exact various express sphere family nerve oil drill engage youth"// Generate a 12-word mnemonic (128 bits of entropy)const mnemonic12 = generateSecretKey(128);// Example: "winter crash infant long upset beauty cram tank short remain obtain sauce"
Use cases
- Creating new wallet seed phrases
- Generating secure entropy for applications
- Building wallet creation flows
- Testing wallet functionality
Key concepts
Mnemonic seed phrases follow the BIP39 standard:
- Entropy: Random data used to generate the phrase
- Word count: 12 words (128 bits) or 24 words (256 bits)
- Word list: Standardized list of 2048 words
- Checksum: Built-in error detection
Entropy and security
// Different entropy levelsconst entropy128 = generateSecretKey(128); // 12 words - Good securityconst entropy256 = generateSecretKey(256); // 24 words - Excellent security// Custom entropy (advanced use)import { mnemonicFromEntropy } from '@stacks/wallet-sdk';import { randomBytes } from 'crypto';const customEntropy = randomBytes(32); // 256 bitsconst customMnemonic = mnemonicFromEntropy(customEntropy);
Validate mnemonic phrases
import { validateMnemonic } from '@stacks/wallet-sdk';const userMnemonic = "winter crash infant long upset beauty cram tank short remain obtain sauce";if (validateMnemonic(userMnemonic)) {console.log("Valid mnemonic phrase");} else {console.log("Invalid mnemonic phrase");}
Complete wallet generation flow
import {generateSecretKey,generateWallet,getStxAddress} from '@stacks/wallet-sdk';async function createNewWallet() {// Step 1: Generate mnemonicconst mnemonic = generateSecretKey(256);console.log("Save this mnemonic securely:", mnemonic);// Step 2: Create wallet from mnemonicconst wallet = await generateWallet({secretKey: mnemonic,password: 'wallet-password',});// Step 3: Get first accountconst account = wallet.accounts[0];const address = getStxAddress({ account, transactionVersion: 1 });return {mnemonic,address,publicKey: account.stxPublicKey,};}
Security best practices
- Never share or expose mnemonic phrases
- Store mnemonics securely (hardware wallet, encrypted storage)
- Use 24-word phrases for maximum security
- Never commit mnemonics to version control
Package installation
Terminal
$npm install @stacks/wallet-sdk