<reponame>MIPPLTeam/bip_utils<filename>examples/bip32_ed25519.py
"""Example of key derivation using BIP32 (ed25519 curve based on SLIP-0010)."""

from bip_utils import (
    Bip39WordsNum, Bip39MnemonicGenerator, Bip39SeedGenerator, Bip32Ed25519Slip, SolAddr
)

# Generate random mnemonic
mnemonic = "wrap inquiry hip shrimp patient merry sheriff curve employ still all else" # Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_24)
print(f"Mnemonic string: {mnemonic}")
# Generate seed from mnemonic
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

# Construct from seed, using ed25519 curve for key derivation
bip32_mst_ctx = Bip32Ed25519Slip.FromSeed(seed_bytes)
# Print master key
print(f"Master key (bytes): {bip32_mst_ctx.PrivateKey().Raw().ToHex()}")
print(f"Master key (extended): {bip32_mst_ctx.PrivateKey().ToExtended()}")

# Derive a path
bip32_der_ctx = bip32_mst_ctx.DerivePath("m/44'/397'/0'")
# Print key
print(f"Derived private key (bytes): {bip32_der_ctx.PrivateKey().Raw().ToHex()}")
print(f"Derived private key (extended): {bip32_der_ctx.PrivateKey().ToExtended()}")
print(f"Derived public key (bytes): {bip32_der_ctx.PublicKey().RawCompressed().ToHex()}")
print(f"Derived public key (extended): {bip32_der_ctx.PublicKey().ToExtended()}")

# Print address in Solana encoding
# Like before, the BIP32 elliptic curve shall be the same one expected by the Solana address (ed25519 in this case)
sol_addr = SolAddr.EncodeKey(bip32_der_ctx.PublicKey().KeyObject())
print(f"Address (SOL): {sol_addr}")

"""Example of key derivation using BIP32 (ed25519 curve based on SLIP-0010)."""

from bip_utils import (
    Bip39WordsNum, Bip39MnemonicGenerator, Bip39SeedGenerator, Bip32Ed25519Slip, SolAddr
)

# Generate random mnemonic
mnemonic = "wrap inquiry hip shrimp patient merry sheriff curve employ still all else" # Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_24)
print(f"Mnemonic string: {mnemonic}")
# Generate seed from mnemonic
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

# Construct from seed, using ed25519 curve for key derivation
bip32_mst_ctx = Bip32Ed25519Slip.FromSeed(seed_bytes)
# Print master key
print(f"Master key (bytes): {bip32_mst_ctx.PrivateKey().Raw().ToHex()}")
print(f"Master key (extended): {bip32_mst_ctx.PrivateKey().ToExtended()}")

# Derive a path
bip32_der_ctx = bip32_mst_ctx.DerivePath("m/44'/397'/0'")
# Print key
print(f"Derived private key (bytes): {bip32_der_ctx.PrivateKey().Raw().ToHex()}")
print(f"Derived private key (extended): {bip32_der_ctx.PrivateKey().ToExtended()}")
print(f"Derived public key (bytes): {bip32_der_ctx.PublicKey().RawCompressed().ToHex()}")
print(f"Derived public key (extended): {bip32_der_ctx.PublicKey().ToExtended()}")

# Print address in Solana encoding
# Like before, the BIP32 elliptic curve shall be the same one expected by the Solana address (ed25519 in this case)
sol_addr = SolAddr.EncodeKey(bip32_der_ctx.PublicKey().KeyObject())
print(f"Address (SOL): {sol_addr}")
Example of key derivation using BIP32 (ed25519 curve based on SLIP-0010). # Generate random mnemonic # Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_24) # Generate seed from mnemonic # Construct from seed, using ed25519 curve for key derivation # Print master key # Derive a path # Print key # Print address in Solana encoding # Like before, the BIP32 elliptic curve shall be the same one expected by the Solana address (ed25519 in this case)