Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
print(__doc__)
comp = bytes.fromhex(input("Please enter computer-generated entropy (in hex): ").strip())
trzr = bytes.fromhex(input("Please enter Trezor-generated entropy (in hex): ").strip())
word_count = int(input("How many words your mnemonic has? "))
strength = word_count * 32 // 3
entropy = generate_entropy(strength, trzr, comp)
words = mnemonic.Mnemonic('english').to_mnemonic(entropy)
if not mnemonic.Mnemonic('english').check(words):
print("Mnemonic is invalid")
return
if len(words.split(' ')) != word_count:
print("Mnemonic length mismatch!")
return
print("Generated mnemonic is:", words)
def combine(self, shares):
words = set([len(x.split(' ')) for x in shares])
if len(words) != 1:
raise Exception('Inconsistent number of words')
datalen = list(words)[0] * 4 // 3 - 1
shares = [binascii.hexlify(self.mnemo.to_entropy(x)) for x in shares]
if sys.version > '3':
if set([int(chr(x[0]), 16) for x in shares]) != set([len(shares)]):
raise Exception('Number of shares does not match the threshold')
points = [(int(chr(x[1]), 16), int(x[2:], 16)) for x in shares]
else:
if set([int(x[0], 16) for x in shares]) != set([len(shares)]):
raise Exception('Number of shares does not match the threshold')
points = [(int(x[1], 16), int(x[2:], 16)) for x in shares]
prime = self.primes[datalen]
r = points_to_secret_int(points, prime)
r = hex(r)[2:]
if r.endswith('L'):
r = r[:-1]
r = r.zfill(datalen * 2)
return binascii.unhexlify(r)
a multiple of 32 between 128 and 256.
passphrase (str): An optional passphrase for the generated
mnemonic string.
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
a multiple of 32 between 128 and 256.
passphrase (str): An optional passphrase for the generated
mnemonic string.
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
a multiple of 32 between 128 and 256.
passphrase (str): An optional passphrase for the generated
mnemonic string.
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
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
title, story = cap_story()
assert f'Path Used (index={index}):' in story
assert "m/83696968'/" in story
assert f"/{index}'" in story
got = re.findall(pattern, story)[0]
assert len(set(got)) >= 12
global HISTORY
assert got not in HISTORY
HISTORY.add(got)
if 'words' in mode:
exp = Mnemonic('english').to_mnemonic(a2b_hex(got)).split()
assert '\n'.join(f'{n+1:2d}: {w}' for n, w in enumerate(exp)) in story
elif 'XPRV' in mode:
node = BIP32Node.from_hwif(got)
assert str(b2a_hex(node.chain_code()), 'ascii') in story
assert hex(node.secret_exponent())[2:] in story
elif 'WIF' in mode:
key = Key.from_text(got)
assert hex(key.secret_exponent())[2:] in story
def shamir_test(l, m, n):
s = Shamir('english')
seed = b"Shamir's Secret Sharing Scheme!"[:l] # take first l characters
shares = s.split(seed, m, n)
print('original:', seed)
print('shares:')
for i, sh in enumerate(shares):
print('%2d :' % (i + 1), sh)
shares = shares[:m] # take first m shares
cmb = s.combine(shares)
print('combined:', cmb)
if seed == cmb:
print('TEST OK')
print()
else:
print('TEST FAILED !!!')
sys.exit(1)