Method Signature
align.blockchain.wallets.encryptPrivateKey(
privateKey: string,
password: string
): Promise<EncryptedWallet>
Parameters
The private key to encrypt
Password for encryption (use a strong password!)
Returns
The wallet’s public address
The encrypted keystore JSON string
Examples
import Align from "@tolbel/align";
import fs from "fs";
const align = new Align({
apiKey: process.env.ALIGN_API_KEY!,
environment: "sandbox",
});
// Create a wallet
const wallet = await align.blockchain.wallets.create();
// Encrypt for storage
const encrypted = await align.blockchain.wallets.encryptPrivateKey(
wallet.privateKey,
"mySecurePassword123!"
);
console.log(`Address: ${encrypted.address}`);
// Save to file
fs.writeFileSync(
`./keystore/${encrypted.address}.json`,
encrypted.encryptedJson
);
console.log("Wallet encrypted and saved!");
const wallet = await align.blockchain.wallets.create();
const encrypted = await align.blockchain.wallets.encryptPrivateKey(
wallet.privateKey,
"mySecurePassword123!"
);
console.log("Encrypted JSON:", encrypted.encryptedJson);
Complete Encryption/Decryption Flow
// 1. Create wallet
const wallet = await align.blockchain.wallets.create();
console.log(`Created: ${wallet.address}`);
// 2. Encrypt
const encrypted = await align.blockchain.wallets.encryptPrivateKey(
wallet.privateKey,
password
);
// 3. Store encrypted JSON (in database, file, etc.)
await saveToDatabase(encrypted.address, encrypted.encryptedJson);
// ... Later ...
// 4. Retrieve and decrypt
const storedJson = await loadFromDatabase(wallet.address);
const restored = await align.blockchain.wallets.createFromEncrypted(
storedJson,
password
);
console.log(`Restored: ${restored.address}`);
// Addresses match!
Encryption uses scrypt key derivation, which is deliberately slow (~3-5
seconds) to resist brute-force attacks.
Password Requirements: - Use a strong, unique password - Never store
passwords alongside encrypted wallets - Consider using a key management
service for production