def test_bip39(self): for [seed, exp_mnemonic, hex_seed, xprv] in VECTORS: act_mnemonic = mnemonic_from_bytes(unhexlify(seed)) act_xkey = HDKey.from_seed( mnemonic_to_seed(act_mnemonic, password="******")) self.assertEqual(act_mnemonic, exp_mnemonic) self.assertTrue(mnemonic_is_valid(act_mnemonic)) self.assertEqual( hexlify(mnemonic_to_bytes(act_mnemonic)).decode(), seed) self.assertEqual(act_xkey.to_base58(), xprv)
def set_mnemonic(self, mnemonic=None, password=""): """Load mnemonic and password and create root key""" if mnemonic is not None: self.mnemonic = mnemonic.strip() if not bip39.mnemonic_is_valid(self.mnemonic): raise KeyStoreError("Invalid mnemonic") seed = bip39.mnemonic_to_seed(self.mnemonic, password) self.root = bip32.HDKey.from_seed(seed) self.fingerprint = self.root.child(0).fingerprint # id key to sign and encrypt wallet files # stored on untrusted external chip self.idkey = self.root.child(0x1D, hardened=True).key.serialize()
def handle_next_recovery_phrase_word_button(self, obj, event): if event == lv.EVENT.RELEASED and obj.get_state( ) != BUTTON_DISABLED_STATE: self.recovery_phrase.append(self.recovery_phrase_input.get_text()) if len(self.recovery_phrase) == self.recovery_phrase_target_length: recovery_phrase = " ".join(self.recovery_phrase) if bip39.mnemonic_is_valid(recovery_phrase): self.show_password_input() else: self.show_recovery_failed() else: self.show_phrase_input()
async def process_host_command(self, stream, show_fn): # reads prefix from the stream (until first space) prefix = self.get_prefix(stream) if prefix not in self.prefixes: # WTF? It's not our data... raise AppError("Prefix is not valid: %s" % prefix.decode()) mnemonic = stream.read().strip().decode() if not bip39.mnemonic_is_valid(mnemonic): raise AppError("Invalid mnemonic!") scr = Prompt("Load this mnemonic to memory?", "Mnemonic:") table = MnemonicTable(scr) table.align(scr.message, lv.ALIGN.OUT_BOTTOM_MID, 0, 30) table.set_mnemonic(mnemonic) if await show_fn(scr): self.keystore.set_mnemonic(mnemonic) return None
def set_mnemonic(self, mnemonic=None, password=""): if mnemonic == self.mnemonic and password != "": # probably checking mnemonic after saving self.show_loader() else: self.show_loader(title="Generating keys...") """Load mnemonic and password and create root key""" if mnemonic is not None: self.mnemonic = mnemonic.strip() if not bip39.mnemonic_is_valid(self.mnemonic): raise KeyStoreError("Invalid mnemonic") seed = bip39.mnemonic_to_seed(self.mnemonic, password) self.root = bip32.HDKey.from_seed(seed) self.fingerprint = self.root.child(0).fingerprint # id key to sign and encrypt wallet files # stored on untrusted external chip self.idkey = self.root.child(0x1D, hardened=True).key.serialize()
def test_invalid_checksum(self): words = ( "ivory canyon lend simple system regret test cool clip foam answer abandon" ) self.assertFalse(mnemonic_is_valid(words))
def test_invalid_word(self): words = "fljsafk minute glow ride mask ceiling old limb rookie discover cotton biology" self.assertFalse(mnemonic_is_valid(words))
def test_invalid_length(self): words = "panel trumpet seek bridge income piano history car flower aim loan accident embark canoe" self.assertFalse(mnemonic_is_valid(words))
#################################### # # # key generation - part 2 # # # #################################### ################# BIP-39 ##################### phrase = bip39.mnemonic_from_bytes(entropy) print("Your recovery phrase:\n%s\n" % phrase) # uncomment this line to make invalid mnemonic: # phrase += " satoshi" # you can check if recovery phrase is valid or not: if not bip39.mnemonic_is_valid(phrase): raise ValueError("Meh... Typo in the recovery?") # convert mnemonic and password to bip-32 seed seed = bip39.mnemonic_to_seed(phrase, password="******") print("Seed:", hexlify(seed).decode()) ################# BIP-32 ##################### # we will use signet: network = NETWORKS["signet"] # create HDKey from 64-byte seed root_key = bip32.HDKey.from_seed(seed) # generate an account child key: # purpose: 84h - BIP-84
from bitcoin import bip32, bip39, script # NETWORKS contains all constants for HD keys and addresses from bitcoin.networks import NETWORKS # we will use testnet: network = NETWORKS["test"] entropy = b'\x64\xd3\xe4\xa0\xa3\x87\xe2\x80\x21\xdf\x55\xa5\x1d\x45\x4d\xcf' recovery_phrase = bip39.mnemonic_from_bytes(entropy) print("Your recovery phrase:\n%s\n" % recovery_phrase) # uncomment this line to make invalid mnemonic: # recovery_phrase += " satoshi" # you can check if recovery phrase is valid or not: if not bip39.mnemonic_is_valid(recovery_phrase): raise ValueError("Meh... Typo in the recovery?") # convert mnemonic and password to bip-32 seed seed = bip39.mnemonic_to_seed(recovery_phrase, password="******") print("Seed:", hexlify(seed).decode("ascii")) # create HDKey from 64-byte seed root_key = bip32.HDKey.from_seed(seed) # generate an account child key: # purpose: 84h - BIP-84 # coin type: 1h - Testnet # account: 0h - first account account = root_key.derive("m/84h/1h/0h") # convert HD private key to HD public key account_pub = account.to_public()