Generate a wallet
Create a new wallet with mnemonic phrase or restore from existing seed
import { generateWallet, generateSecretKey } from '@stacks/wallet-sdk';// Generate a new wallet with a new 24-word seed phraseconst secretKey = generateSecretKey(256); // 256 bits = 24 wordsconst wallet = await generateWallet({secretKey,password: 'your-secure-password',});// Access the first accountconst account = wallet.accounts[0];console.log('Address:', account.stxAddress);console.log('Mnemonic:', secretKey);
Use cases
- Creating new wallets for users
- Restoring wallets from seed phrases
- Generating deterministic wallet addresses
- Building wallet applications
Key concepts
The wallet SDK generates hierarchical deterministic (HD) wallets following BIP32/BIP39 standards:
- Secret key: Can be a mnemonic phrase or private key
- Password: Encrypts the wallet (different from mnemonic passphrase)
- Accounts: Multiple accounts can be derived from one seed
Restore from existing seed
import { generateWallet } from '@stacks/wallet-sdk';// Restore wallet from 24-word mnemonicconst existingSeed = "your twenty four word mnemonic phrase goes here...";const wallet = await generateWallet({secretKey: existingSeed,password: 'your-secure-password',});
Generate additional accounts
import { generateNewAccount } from '@stacks/wallet-sdk';// Create a second account from the same seedconst secondAccount = generateNewAccount(wallet);console.log('Second address:', secondAccount.stxAddress);// Accounts are derived using BIP44 paths// m/44'/5757'/0'/0/0 - First account// m/44'/5757'/1'/0/0 - Second account
Complete example
import {generateWallet,generateSecretKey,generateNewAccount} from '@stacks/wallet-sdk';async function createNewWallet() {// Generate new 24-word seed phraseconst mnemonic = generateSecretKey(256);// Create walletconst wallet = await generateWallet({secretKey: mnemonic,password: 'supersecret123',});// Get wallet detailsconst firstAccount = wallet.accounts[0];return {mnemonic,address: firstAccount.stxAddress,publicKey: firstAccount.stxPublicKey,privateKey: firstAccount.stxPrivateKey,};}// Create wallet and display infoconst walletInfo = await createNewWallet();console.log('New Wallet Created!');console.log('Mnemonic (save this!):', walletInfo.mnemonic);console.log('Address:', walletInfo.address);
Security considerations
Security warning
Never expose seed phrases or private keys in production code. Store them securely and never commit them to version control.
Package installation
Terminal
$npm install @stacks/wallet-sdk