0% found this document useful (0 votes)
21 views2 pages

Code Pal Result

This document contains functions to convert a mnemonic phrase to a private key and private key to an Ethereum address. It takes a mnemonic phrase as input, generates a private key using BIP39, and returns the private key. It then takes a private key, generates the corresponding Ethereum address using secp256k1, and returns the address.

Uploaded by

eq8tiongroup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Code Pal Result

This document contains functions to convert a mnemonic phrase to a private key and private key to an Ethereum address. It takes a mnemonic phrase as input, generates a private key using BIP39, and returns the private key. It then takes a private key, generates the corresponding Ethereum address using secp256k1, and returns the address.

Uploaded by

eq8tiongroup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import hashlib

import binascii
import ecdsa
from ecdsa import SigningKey, SECP256k1
from eth_utils import to_checksum_address

def mnemonic_to_private_key(mnemonic: str) -> str:


"""
Converts a mnemonic phrase to a private key.

This function takes a mnemonic phrase as input and generates a private key
using the BIP39 algorithm. The private key is a 256-bit hexadecimal string.

Parameters:
- mnemonic (str): The mnemonic phrase to convert to a private key.

Returns:
str: The private key generated from the mnemonic phrase.

Raises:
- ValueError: If the mnemonic phrase is empty or invalid.
"""

# Checking if the mnemonic phrase is empty


if not mnemonic:
raise ValueError("Mnemonic phrase cannot be empty.")

# Converting the mnemonic phrase to a binary seed using PBKDF2


seed = hashlib.pbkdf2_hmac('sha512', mnemonic.encode('utf-8'), b'mnemonic',
2048)

# Extracting the private key from the seed


private_key = binascii.hexlify(seed[:32]).decode('utf-8')

# Returning the private key


return private_key

def private_key_to_eth_address(private_key: str) -> str:


"""
Converts a private key to an Ethereum address.

This function takes a private key as input and generates the corresponding
Ethereum address using the secp256k1 elliptic curve algorithm. The address
is a 40-character hexadecimal string with a '0x' prefix.

Parameters:
- private_key (str): The private key to convert to an Ethereum address.

Returns:
str: The Ethereum address generated from the private key.

Raises:
- ValueError: If the private key is empty or invalid.
"""

# Checking if the private key is empty


if not private_key:
raise ValueError("Private key cannot be empty.")
# Converting the private key to a signing key
signing_key = SigningKey.from_string(binascii.unhexlify(private_key),
curve=SECP256k1)

# Getting the public key from the signing key


public_key = signing_key.get_verifying_key().to_string()

# Hashing the public key using Keccak-256


keccak_hash = hashlib.sha3_256(public_key)

# Getting the last 20 bytes of the hash (address length)


address = keccak_hash[-20:]

# Converting the address to a hexadecimal string with a '0x' prefix


eth_address = to_checksum_address('0x' + address.hex())

# Returning the Ethereum address


return eth_address

# Example usage:

mnemonic_phrase = "example mnemonic phrase"


private_key = mnemonic_to_private_key(mnemonic_phrase)
eth_address = private_key_to_eth_address(private_key)

print(f"Mnemonic phrase: {mnemonic_phrase}")


print(f"Private key: {private_key}")
print(f"Ethereum address: {eth_address}")

You might also like