예제 #1
0
    def test_vector(self):
        for test in TEST_VECT:
            # Test mnemonic generator
            mnemonic = Bip39MnemonicGenerator.FromEntropy(
                binascii.unhexlify(test["entropy"]))

            self.assertEqual(test["mnemonic"], mnemonic)

            # Test mnemonic validator using string
            bip39_mnemonic_validator = Bip39MnemonicValidator(mnemonic)
            entropy = bip39_mnemonic_validator.GetEntropy()

            self.assertEqual(test["entropy"], binascii.hexlify(entropy))
            self.assertTrue(bip39_mnemonic_validator.Validate())

            # Test mnemonic validator using list
            bip39_mnemonic_validator = Bip39MnemonicValidator(
                mnemonic.split(" "))
            entropy = bip39_mnemonic_validator.GetEntropy()

            self.assertEqual(test["entropy"], binascii.hexlify(entropy))
            self.assertTrue(bip39_mnemonic_validator.Validate())

            # Test seed generator
            seed = Bip39SeedGenerator(mnemonic).Generate(TEST_PASSPHRASE)

            self.assertEqual(test["seed"], binascii.hexlify(seed))
예제 #2
0
 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)
예제 #3
0
 def test_entropy(self):
     for test in TEST_ENTROPY_BITS_MAIN:
         if test["is_valid"]:
             # Test generator
             entropy = EntropyGenerator(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)
         else:
             self.assertRaises(ValueError, EntropyGenerator, test["bit_len"])
             # Build a dummy entropy with that bit length
             dummy_ent = b"\x00" * (test["bit_len"] // 8)
             # Construct from it
             self.assertRaises(ValueError, Bip39MnemonicGenerator.FromEntropy, dummy_ent)
예제 #4
0
def generate():
    # Tells the library what network we are using
    setup()

    if not mnemonic_phrase:
        # Generate mnemonic from random 192-bit entropy
        entropy_bytes = Bip39EntropyGenerator(
            Bip39EntropyBitLen.BIT_LEN_192).Generate()
        mnemonic = Bip39MnemonicGenerator.FromEntropy(entropy_bytes)
        print("Generated random mnemonic:\n" + mnemonic)
    else:
        print("Using included mnemonic.")
        mnemonic = mnemonic_phrase

    # Get seed bytes from mnemonic
    seed_bytes = Bip39SeedGenerator(mnemonic).Generate()
    bip44_mst = Bip44.FromSeed(
        seed_bytes, Bip44Coins.BITCOIN)  # Could add in multi currency support

    # Derive account 0 for Bitcoin: m/44'/0'/0'
    bip44_acc = bip44_mst.Purpose() \
                        .Coin()    \
                        .Account(0)

    # Derive the external chain: m/44'/0'/0'/0
    bip44_change = bip44_acc.Change(Bip44Changes.CHAIN_EXT)

    with open(output_dest, 'w') as output_file:  # Open the output file
        output_file.write("address, public_key" +
                          (", private_key" if privates else "") + "\n")
        # Go through each address
        for i in range(number):
            bip44_addr = bip44_change.AddressIndex(i)

            # create segwit address
            addr3 = PrivateKey.from_wif(bip44_addr.PrivateKey().ToWif(
            )).get_public_key().get_segwit_address()
            # wrap in P2SH address
            addr4 = P2shAddress.from_script(addr3.to_script_pub_key())

            if addr4.to_string() == compare:
                print("Found it!")
                print("Path: m/44'/0'/0'/0/" + str(i))
                break
            #print("P2SH(P2WPKH):", addr4.to_string())
            if (i % int(number / 10)) == 0:
                print('Finished {}'.format(i))

            out = "{0}, {1}".format(addr4.to_string(),
                                    bip44_addr.PublicKey().ToExtended()
                                    )  # Public addresses not including private
            if (privates):  # Include the private keys
                out = "{0}, {1}, {2}".format(
                    addr4.to_string(),
                    bip44_addr.PublicKey().RawCompressed().ToHex(),
                    bip44_addr.PrivateKey().ToWif())
                #bip44_addr.PublicKey().ToAddress() # This is the regular address (not P2SH(P2WPKH))

            # Print extended keys and address
            if (verbose):
                print(out)

            output_file.write(out + "\n")