# Generating private keys

Hive layer 1 keys can be generated via two methods:

  1. Derivation of a set of private keys from a password (sometimes referred to as a "Master Password"). This is typically used to initially create active, owner, posting, and memo keys for a Hive account.
  2. Randomly generate a new private key (generally should only be used in one-time-use scenarios).

# Generate a private key for an account role from a password

import { createWaxFoundation } from '@hiveio/wax';

const waxApi = await createWaxFoundation();
const accountName = "your-account";
const role = "active"; // roles can be 'active', 'owner', 'posting', or 'memo'
// Important notice!!!
// The master password should always be a truly random and secure value, such
// as one retrieved from the JS crypto interface.
// We used Math.random() here just for convenience.
const masterPassword = Math.random().toString();

// Generating a new private key from a password
const privateKeyData = waxApi.getPrivateKeyFromPassword(accountName, role, masterPassword);

console.log(`Associated Public Key: ${privateKeyData.associatedPublicKey}`);
console.log(`WIF Private Key: ${privateKeyData.wifPrivateKey}`);
Test it yourself on github codespace: src/python/generate_keys.py
# Required for generating a secure, random master password
import secrets

# Provides a set of characters to use in the master password
import string

# Import the function to create a WAX Foundation instance from the 'wax' library
from wax import create_wax_foundation

# Create a WAX Foundation object that provides access to WAX blockchain utilities
wax = create_wax_foundation()
#
# Generate a private key using an account name, password, and role.
#
# Important notice!!!
# The master password should always be a truly random and secure value, such
# as one retrieved from the `secrets` python package.
characters = string.ascii_letters + string.digits + string.punctuation
master_password = "".join(secrets.choice(characters) for _ in range(24))

password_gen_key_data = wax.get_private_key_from_password(
    account="alice",
    password=master_password,
    role="active",  # roles can be 'active', 'owner', 'posting', or 'memo'
)

print("Example results from Password-derived key generation:")
print(f"Private key: {password_gen_key_data.wif_private_key}")
print(f"Public key: {password_gen_key_data.associated_public_key}")
Example results from Password-derived key generation:
Private key: 5JTxtE9jdDu43PUCr3ip2KWnWFoofD7dfkPWA9gREN1jn6YwZat
Public key: STM8b2ESwQaEBJq1AtnXqLSD5S1jSTNmijzoZNqQtok3FBoQFtQTr

# Suggest Brain Key

A brain key is a long passphrase that provides enough entropy to generate cryptographic keys. The suggestBrainKey function returns a brain key along with the corresponding private and public keys.

Using a brain key, you can regenerate the same key pairs whenever needed, provided the exact same mnemonic phrase is used. This is especially useful in scenarios requiring backup and recovery of cryptographic keys, ensuring they are never permanently lost.

import { createWaxFoundation } from '@hiveio/wax';

const waxApi = await createWaxFoundation();

// Suggest brain key
const privateKeyData = waxApi.suggestBrainKey();

console.log(`Associated Public Key: ${privateKeyData.associatedPublicKey}`);
console.log(`WIF Private Key: ${privateKeyData.wifPrivateKey}`);
console.log(`Brain Key: ${privateKeyData.brainKey}`);
Test it yourself on github codespace: src/python/generate_keys.py

# Import the function to create a WAX Foundation instance from the 'wax' library
from wax import create_wax_foundation

# Create a WAX Foundation object that provides access to WAX blockchain utilities
wax = create_wax_foundation()
# %% [markdown]
# ### 🧠 Generate Brain Key (Seed Phrase)
#
# The `suggest_brain_key()` function returns an object containing:
# - `brain_key`: mnemonic seed phrase (human-readable words)
# - `wif_private_key`: private key in Wallet Import Format (WIF)
# - `associated_public_key`: public key derived from the private key
brain_key_data = wax.suggest_brain_key()

print(f"Seed phrase: {brain_key_data.brain_key}")  # Mnemonic phrase used to recover the wallet
print(f"Private key: {brain_key_data.wif_private_key}")  # Private key (WIF), used to sign transactions
print(f"Public key: {brain_key_data.associated_public_key}")  # Public key, used to receive tokens
Seed phrase: MEMO FAUST DOB CHARGE WAXILY BURSATE POPWEED TOOTHY PEDICEL NICOTIC GUTTIDE SAVANT BUNT MOMME HORNIFY UNOIL
Private key: 5KhKdhiVPpJrMqamx4VukXtBPh7VDWfZvJsv2xdcHBHDRFanVKN
Public key: STM5Br7eDvpXavuxfajqDfhRu6cmVRzRq1z26ch4Dx9b4hoZxKgJt