Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Returns:
HDPrivateKey, str:
a tuple consisting of the master
private key and a mnemonic string from which the seed
can be recovered.
"""
if strength % 32 != 0:
raise ValueError("strength must be a multiple of 32")
if strength < 128 or strength > 256:
raise ValueError("strength should be >= 128 and <= 256")
entropy = rand_bytes(strength // 8)
m = Mnemonic(language='english')
n = m.to_mnemonic(entropy)
return HDPrivateKey.master_key_from_seed(
Mnemonic.to_seed(n, passphrase)), n
Returns:
HDPrivateKey, str:
a tuple consisting of the master
private key and a mnemonic string from which the seed
can be recovered.
"""
if strength % 32 != 0:
raise ValueError("strength must be a multiple of 32")
if strength < 128 or strength > 256:
raise ValueError("strength should be >= 128 and <= 256")
entropy = rand_bytes(strength // 8)
m = Mnemonic(language='english')
n = m.to_mnemonic(entropy)
return HDPrivateKey.master_key_from_seed(
Mnemonic.to_seed(n, passphrase)), n
Returns:
HDPrivateKey, str:
a tuple consisting of the master
private key and a mnemonic string from which the seed
can be recovered.
"""
if strength % 32 != 0:
raise ValueError("strength must be a multiple of 32")
if strength < 128 or strength > 256:
raise ValueError("strength should be >= 128 and <= 256")
entropy = rand_bytes(strength // 8)
m = Mnemonic(language='english')
n = m.to_mnemonic(entropy)
return HDPrivateKey.master_key_from_seed(
Mnemonic.to_seed(n, passphrase)), n
Args:
mnemonic (str): Mnemonic phrase
Returns:
dict
"""
# Create mnemonic phrase if None
if not mnemonic:
_mnemonic = Mnemonic(language='english')
mnemonic = _mnemonic.generate(cls.entropy_bits)
if len(mnemonic.split(' ')) != 12:
raise Exception('Mnemonic phrase should have 12 words.')
# Mnemonic to seed (bytes)
seed = Mnemonic.to_seed(mnemonic, '')
# Generate master key (key, hmac_key) from master seed
_I = hmac.new(cls.master_seed, seed, hashlib.sha512).hexdigest()
master_key = (int(_I[:64], 16), bytes.fromhex(_I[64:]))
# Get child keys from master key by path
keys = cls.from_path(
root_key=master_key, path=cls.seed_address_path
)
# Get private key
private_key = keys[-1][0].to_bytes(length=32, byteorder='big').hex()
# Get public key from private
public_key = cls.get_public_from_private(private_key)
# Get address from public key
address = cls.get_address_from_public_key(public_key)
def master_key_from_mnemonic(mnemonic, passphrase=''):
""" Generates a master key from a mnemonic.
Args:
mnemonic (str): The mnemonic sentence representing
the seed from which to generate the master key.
passphrase (str): Password if one was used.
Returns:
HDPrivateKey: the master private key.
"""
return HDPrivateKey.master_key_from_seed(
Mnemonic.to_seed(mnemonic, passphrase))
def master_key_from_mnemonic(mnemonic, passphrase=''):
""" Generates a master key from a mnemonic.
Args:
mnemonic (str): The mnemonic sentence representing
the seed from which to generate the master key.
passphrase (str): Password if one was used.
Returns:
HDPrivateKey: the master private key.
"""
return HDPrivateKey.master_key_from_seed(
Mnemonic.to_seed(mnemonic, passphrase))
def master_key_from_mnemonic(mnemonic, passphrase=''):
""" Generates a master key from a mnemonic.
Args:
mnemonic (str): The mnemonic sentence representing
the seed from which to generate the master key.
passphrase (str): Password if one was used.
Returns:
HDPrivateKey: the master private key.
"""
return HDPrivateKey.master_key_from_seed(
Mnemonic.to_seed(mnemonic, passphrase))