def test_root_seed_from_mnemonic(self): # Test Trezor vectors. for test_vector in self.trezor_vectors['english']: self.assertEqual( bip39.root_seed_from_mnemonic(test_vector['mnemonic'], 'TREZOR').hex(), test_vector['seed']) # Test vectors. for test_vector in self.test_vectors['english']: self.assertEqual( bip39.root_seed_from_mnemonic(test_vector['mnemonic']).hex(), test_vector['seed'])
def test_xpriv_generation(self): # Test vectors. for test_vector in self.english_vectors: root_seed = bip39.root_seed_from_mnemonic( mnemonic=test_vector['mnemonic'], salt=test_vector['salt']) root = HDKey.from_root_seed(root_seed=root_seed) self.assertEqual(root.xpriv, test_vector['root']['xpriv'])
def test_xpriv_when_derived(self): # Test vectors. for test_vector in self.english_vectors: root_seed = bip39.root_seed_from_mnemonic( mnemonic=test_vector['mnemonic'], salt=test_vector['salt']) root = HDKey.from_root_seed(root_seed=root_seed) node = root.derive_path(test_vector['derived_node']['path']) self.assertEqual(node.xpriv, test_vector['derived_node']['xpriv'])
def test_fingerprint_generation(self): # Test vectors. for test_vector in self.english_vectors: root_seed = bip39.root_seed_from_mnemonic( mnemonic=test_vector['mnemonic'], salt=test_vector['salt']) root = HDKey.from_root_seed(root_seed=root_seed) self.assertEqual(root.fingerprint, bytes.fromhex(test_vector['root']['fingerprint']))
def test_private_key_generation(self): # Test vectors. for test_vector in self.english_vectors: root_seed = bip39.root_seed_from_mnemonic( mnemonic=test_vector['mnemonic'], salt=test_vector['salt']) root = HDKey.from_root_seed(root_seed=root_seed) self.assertEqual(root.privkey.hex(), test_vector['root']['private_key'])
def from_mnemonic(HDKey, mnemonic: str, salt: Optional[str] = None, network: str = 'Bitcoin') -> 'HDKey': ''' Generates a HDKey object from entropy Args: mnemonic (str): the 12+ word mnemonic phrase salt (str, Optional): an optional salt for derivation network (str, Optional): Must be a selection from NETWORK_CODES, defaults to Bitcoin Returns: (HDKey) ''' root_seed = bip39.root_seed_from_mnemonic(mnemonic, salt, network) return HDKey.from_root_seed(root_seed, network)
def test_node_path_derivation(self): # Test vectors. for test_vector in self.english_vectors: root_seed = bip39.root_seed_from_mnemonic( mnemonic=test_vector['mnemonic'], salt=test_vector['salt']) root = HDKey.from_root_seed(root_seed=root_seed) node = root.derive_path(test_vector['derived_node']['path']) self.assertEqual(node.xpriv, test_vector['derived_node']['xpriv']) self.assertEqual(node.xpub, test_vector['derived_node']['xpub']) self.assertEqual(node.privkey.hex(), test_vector['derived_node']['private_key']) self.assertEqual(node.pubkey.hex(), test_vector['derived_node']['public_key']) self.assertEqual(node.index, test_vector['derived_node']['index']) self.assertEqual( node.fingerprint, bytes.fromhex(test_vector['derived_node']['fingerprint'])) self.assertEqual( node.chain_code, bytes.fromhex(test_vector['derived_node']['chain_code'])) self.assertEqual(node.depth, test_vector['derived_node']['depth'])