def test_from_invalid_words_num(self): for test_words_num in Bip39WordsNum: self.assertRaises(ValueError, Bip39MnemonicGenerator().FromWordsNumber, test_words_num - 1) self.assertRaises(ValueError, Bip39MnemonicGenerator().FromWordsNumber, test_words_num + 1)
def test_vector(self): for test in TEST_VECT: lang = test["lang"] if "lang" in test else Bip39Languages.ENGLISH # Test mnemonic generator mnemonic = Bip39MnemonicGenerator(lang).FromEntropy( binascii.unhexlify(test["entropy"])) self.assertEqual(test["mnemonic"], mnemonic) # Test mnemonic validator using string (language specified) bip39_mnemonic_validator = Bip39MnemonicValidator(mnemonic, lang) entropy = bip39_mnemonic_validator.GetEntropy() self.assertEqual(test["entropy"], binascii.hexlify(entropy)) self.assertTrue(bip39_mnemonic_validator.IsValid()) # Test mnemonic validator using list (automatic language detection) bip39_mnemonic_validator = Bip39MnemonicValidator( mnemonic.split(" ")) entropy = bip39_mnemonic_validator.GetEntropy() self.assertEqual(test["entropy"], binascii.hexlify(entropy)) self.assertTrue(bip39_mnemonic_validator.IsValid()) # Test seed generator seed = Bip39SeedGenerator(mnemonic, lang).Generate(TEST_PASSPHRASE) self.assertEqual(test["seed"], binascii.hexlify(seed))
def test_entropy_invalid_bitlen(self): for test_bit_len in TEST_VECT_ENTROPY_BITLEN_INVALID: self.assertRaises(ValueError, Bip39EntropyGenerator, test_bit_len) # Build a dummy entropy with that bit length # Subtract 8 because, otherwise, dividing by 8 could result in a correct byte length dummy_ent = b"\x00" * ((test_bit_len - 8) // 8) # Construct from it self.assertRaises(ValueError, Bip39MnemonicGenerator().FromEntropy, dummy_ent)
def test_entropy_invalid_bitlen(self): for test_bit_len in Bip39EntropyBitLen: self.assertRaises(ValueError, Bip39EntropyGenerator, test_bit_len - 1) self.assertRaises(ValueError, Bip39EntropyGenerator, test_bit_len + 1) # Build a dummy entropy with invalid bit length dummy_ent = b"\x00" * ((test_bit_len - 8) // 8) self.assertRaises(ValueError, Bip39MnemonicGenerator().FromEntropy, dummy_ent)
def test_entropy_valid_bitlen(self): for test_bit_len in TEST_VECT_ENTROPY_BITLEN_VALID: # Test generator entropy = Bip39EntropyGenerator(test_bit_len).Generate() self.assertEqual(len(entropy), test_bit_len // 8) # Generate mnemonic mnemonic = Bip39MnemonicGenerator().FromEntropy(entropy) # Compute the expected mnemonic length mnemonic_len = (test_bit_len + (test_bit_len // 32)) // 11 # Test generated mnemonic length self.assertEqual(len(mnemonic.split(" ")), mnemonic_len)
def test_entropy_valid_bitlen(self): for test_bit_len in Bip39EntropyBitLen: # Test generator entropy = Bip39EntropyGenerator(test_bit_len).Generate() self.assertEqual(len(entropy), test_bit_len // 8) # Generate mnemonic mnemonic = Bip39MnemonicGenerator().FromEntropy(entropy) # Compute the expected mnemonic length mnemonic_len = (test_bit_len + (test_bit_len // 32)) // 11 # Test generated mnemonic length self.assertEqual(mnemonic.WordsCount(), mnemonic_len)
def find_it(search_for: list): found = 0 while True: entropy = urandom(32) mnemonic = Bip39MnemonicGenerator().FromEntropy(entropy) # pk = token_hex(32) # +50% time, but supposed to be cryptographically secure addresses = get_addresses_from(mnemonic) for string in search_for: for index, address in enumerate(addresses): if not options.case: address = address.lower() if string in address: print(address, index, mnemonic) found += 1 if found > options.max: return
def test_vector(self): for test in TEST_VECT: lang = test["lang"] if "lang" in test else Bip39Languages.ENGLISH # Test mnemonic generator mnemonic = Bip39MnemonicGenerator(lang).FromEntropy( binascii.unhexlify(test["entropy"])) self.assertEqual(test["mnemonic"], mnemonic.ToStr()) self.assertEqual(test["mnemonic"], str(mnemonic)) self.assertEqual(test["mnemonic"].split(" "), mnemonic.ToList()) self.assertEqual(len(test["mnemonic"].split(" ")), mnemonic.WordsCount()) # Test mnemonic validator (language specified) mnemonic_validator = Bip39MnemonicValidator(lang) self.assertTrue(mnemonic_validator.IsValid(mnemonic)) # Test mnemonic validator (automatic language detection) mnemonic_validator = Bip39MnemonicValidator() self.assertTrue(mnemonic_validator.IsValid(mnemonic)) # Test decoder (language specified) entropy = Bip39MnemonicDecoder(lang).Decode(mnemonic) self.assertEqual(test["entropy"], binascii.hexlify(entropy)) # Test decoder (automatic language detection) entropy = Bip39MnemonicDecoder().Decode(mnemonic) self.assertEqual(test["entropy"], binascii.hexlify(entropy)) # Test decoder with checksum if "entropy_chksum" in test: entropy = Bip39MnemonicDecoder(lang).DecodeWithChecksum( mnemonic) self.assertEqual(test["entropy_chksum"], binascii.hexlify(entropy)) entropy = Bip39MnemonicDecoder().DecodeWithChecksum(mnemonic) self.assertEqual(test["entropy_chksum"], binascii.hexlify(entropy)) # Test seed generator seed = Bip39SeedGenerator(mnemonic, lang).Generate(TEST_PASSPHRASE) self.assertEqual(test["seed"], binascii.hexlify(seed))
def test_from_valid_words_num(self): for test_words_num in Bip39WordsNum: mnemonic = Bip39MnemonicGenerator().FromWordsNumber(test_words_num) self.assertEqual(mnemonic.WordsCount(), test_words_num)
"""Example of key derivation using Substrate (same addresses of PolkadotJS).""" from bip_utils import (Bip39WordsNum, Bip39MnemonicGenerator, SubstrateBip39SeedGenerator, SubstrateCoins, Substrate) # Generate random mnemonic mnemonic = Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_24) print(f"Mnemonic string: {mnemonic}") # Generate seed from mnemonic seed_bytes = SubstrateBip39SeedGenerator(mnemonic).Generate() # Construct from seed substrate_ctx = Substrate.FromSeed(seed_bytes, SubstrateCoins.POLKADOT) # Print master keys and address print( f"Master private key (bytes): {substrate_ctx.PrivateKey().Raw().ToHex()}") print( f"Master public key (bytes): {substrate_ctx.PublicKey().RawCompressed().ToHex()}" ) print(f"Address: {substrate_ctx.PublicKey().ToAddress()}") # Derive a child key substrate_ctx = substrate_ctx.ChildKey("//hard") # Print derived keys and address print( f"Derived private key (bytes): {substrate_ctx.PrivateKey().Raw().ToHex()}") print( f"Derived public key (bytes): {substrate_ctx.PublicKey().RawCompressed().ToHex()}" ) print(f"Derived address: {substrate_ctx.PublicKey().ToAddress()}") # Print path
def test_from_invalid_words_num(self): for test_words_num in TEST_VECT_WORDS_NUM_INVALID: self.assertRaises(ValueError, Bip39MnemonicGenerator().FromWordsNumber, test_words_num)
def test_from_valid_words_num(self): for test_words_num in TEST_VECT_WORDS_NUM_VALID: mnemonic = Bip39MnemonicGenerator().FromWordsNumber(test_words_num) self.assertEqual(len(mnemonic.split(" ")), test_words_num)