def main(): # always remember to setup the network setup('testnet') # could also instantiate from existing WIF key priv = PrivateKey.from_wif( 'cVdte9ei2xsVjmZSPtyucG43YZgNkmKTqhwiUA8M4Fc3LdPJxPmZ') # compressed is the default print("\nPrivate key WIF:", priv.to_wif(compressed=True)) # get the public key pub = priv.get_public_key() # compressed is the default print("Public key:", pub.to_hex(compressed=True)) # get address from public key address = pub.get_segwit_address() # print the address and hash - default is compressed address print("Native Address:", address.to_string()) segwit_hash = address.to_hash() print("Segwit Hash:", segwit_hash) print("Segwit Version:", address.get_type()) # test to_string addr2 = P2wpkhAddress.from_hash(segwit_hash) print("Created P2wpkhAddress from Segwit Hash and calculate address:") print("Native Address:", addr2.to_string()) # # display P2SH-P2WPKH # # create segwit address addr3 = PrivateKey.from_wif( 'cTmyBsxMQ3vyh4J3jCKYn2Au7AhTKvqeYuxxkinsg6Rz3BBPrYKK').get_public_key( ).get_segwit_address() # wrap in P2SH address addr4 = P2shAddress.from_script(addr3.to_script_pub_key()) print("P2SH(P2WPKH):", addr4.to_string()) # # display P2WSH # p2wpkh_key = PrivateKey.from_wif( 'cNn8itYxAng4xR4eMtrPsrPpDpTdVNuw7Jb6kfhFYZ8DLSZBCg37') script = Script([ 'OP_1', p2wpkh_key.get_public_key().to_hex(), 'OP_1', 'OP_CHECKMULTISIG' ]) p2wsh_addr = P2wshAddress.from_script(script) print("P2WSH of P2PK:", p2wsh_addr.to_string()) # # display P2SH-P2WSH # p2sh_p2wsh_addr = P2shAddress.from_script(p2wsh_addr.to_script_pub_key()) print("P2SH(P2WSH of P2PK):", p2sh_p2wsh_addr.to_string())
def test_p2sh_p2wpkh_creation_pubkey(self): addr = PrivateKey.from_wif( 'cTmyBsxMQ3vyh4J3jCKYn2Au7AhTKvqeYuxxkinsg6Rz3BBPrYKK' ).get_public_key().get_segwit_address() p2sh_addr = P2shAddress.from_script(addr.to_script_pub_key()) self.assertTrue(p2sh_addr.to_string(), self.correct_p2sh_p2wpkh_address)
def get_pub_key_from_priv_key_obj(priv_key_str): """ priv to pub key object """ priv_key_obj = PrivateKey.from_wif(priv_key_str) pub_key_obj = priv_key_obj.get_public_key() return pub_key_obj
def get_pub_key_hash_from_priv_key_obj(priv_key_str): """ priv key to pub hash """ priv_key_obj = PrivateKey.from_wif(priv_key_str) pub_key_obj = priv_key_obj.get_public_key() pub_key_hash160 = pub_key_obj.get_address().to_hash160() return pub_key_hash160
def setUp(self): setup('testnet') self.priv = PrivateKey.from_wif('cVdte9ei2xsVjmZSPtyucG43YZgNkmKTqhwiUA8M4Fc3LdPJxPmZ') self.pub = self.priv.get_public_key() self.correct_p2wpkh_address = 'tb1qxmt9xgewg6mxc4mvnzvrzu4f2v0gy782fydg0w' self.correct_p2sh_p2wpkh_address = '2N8Z5t3GyPW1hSAEJZqQ1GUkZ9ofoGhgKPf' self.correct_p2wsh_address = 'tb1qy4kdfavhluvnhpwcqmqrd8x0ge2ynnsl7mv2mdmdskx4g3fc6ckq8f44jg' self.correct_p2sh_p2wsh_address = 'tb1qy4kdfavhluvnhpwcqmqrd8x0ge2ynnsl7mv2mdmdskx4g3fc6ckq8f44jg'
def test_p2wsh_creation_1multisig(self): p2wpkh_key = PrivateKey.from_wif( 'cNn8itYxAng4xR4eMtrPsrPpDpTdVNuw7Jb6kfhFYZ8DLSZBCg37') script = Script([ 'OP_1', p2wpkh_key.get_public_key().to_hex(), 'OP_1', 'OP_CHECKMULTISIG' ]) p2wsh_addr = P2wshAddress.from_script(script) self.assertTrue(p2wsh_addr.to_string(), self.correct_p2wsh_address)
def setUp(self): setup('testnet') self.sk1 = PrivateKey.from_wif("cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo") self.sk2 = PrivateKey.from_wif("cRvyLwCPLU88jsyj94L7iJjQX5C2f8koG4G2gevN4BeSGcEvfKe9") # 2-2 Multi-sign Script self.p2wsh_script = Script(['OP_2', self.sk1.get_public_key().to_hex(), self.sk2.get_public_key().to_hex(), 'OP_2', 'OP_CHECKMULTISIG']) # tb1q89t0jucv7un4qq85u0a0tkc9qkepvg3vra72r00msx58wqplewfsfrlunx self.p2wsh_addr = P2wshAddress.from_script(self.p2wsh_script) # n4bkvTyU1dVdzsrhWBqBw8fEMbHjJvtmJR self.p2pkh_addr = self.sk1.get_public_key().get_address() # P2PKH to P2WSH self.txin1 = TxInput("6e9a0692ed4b3328909d66d41531854988dc39edba5df186affaefda91824e69", 0) self.txout1 = TxOutput(to_satoshis(0.0097), self.p2wsh_addr.to_script_pub_key()) # P2WSH to P2PKH self.txin_spend = TxInput("6233aca9f2d6165da2d7b4e35d73b039a22b53f58ce5af87dddee7682be937ea", 0) self.txin_spend_amount = to_satoshis(0.0097) self.txout2 = TxOutput(to_satoshis(0.0096), self.p2pkh_addr.to_script_pub_key()) self.p2wsh_redeem_script = self.p2wsh_script # Multiple input multiple output # P2PKH UTXO self.txin1_multiple = TxInput("24d949f8c77d7fc0cd09c8d5fccf7a0249178c16170c738da19f6c4b176c9f4b", 0) self.txin1_multiple_amount = to_satoshis(0.005) # P2WSH UTXO self.txin2_multiple = TxInput("65f4d69c91a8de54dc11096eaa315e84ef91a389d1d1c17a691b72095100a3a4", 0) self.txin2_multiple_amount = to_satoshis(0.0069) # P2WPKH UTXO self.txin3_multiple = TxInput("6c8fc6453a2a3039c2b5b55dcc59587e8b0afa52f92607385b5f4c7e84f38aa2", 0) self.txin3_multiple_amount = to_satoshis(0.0079) self.output1_multiple = TxOutput(to_satoshis(0.001), self.p2wsh_addr.to_script_pub_key()) self.output2_multiple = TxOutput(to_satoshis(0.001), self.sk1.get_public_key().get_segwit_address().to_script_pub_key()) self.output3_multiple = TxOutput(to_satoshis(0.0177), self.p2pkh_addr.to_script_pub_key()) # result self.create_send_to_p2pkh_result = "0200000001694e8291daeffaaf86f15dbaed39dc8849853115d4669d9028334bed92069a6e000000006a473044022038516db4e67c9217b871c690c09f60a57235084f888e23b8ac77ba01d0cba7ae022027a811be50cf54718fc6b88ea900bfa9c8d3e218208fef0e185163e3a47d9a08012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a546ffffffff0110cd0e00000000002200203956f9730cf7275000f4e3faf5db0505b216222c1f7ca1bdfb81a877003fcb9300000000" self.spend_p2pkh_result = "02000000000101ea37e92b68e7dedd87afe58cf5532ba239b0735de3b4d7a25d16d6f2a9ac33620000000000ffffffff0100a60e00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac040047304402205c88b6c247c6b59e1cc48493b66629b6c011d97b99ecf991b595e891542cf1a802204fa0e3c238818a65adc87a0b2511ba780e4b57ff6c1ba6b27815b1dca7b72c1c01483045022100cec7df71d36a37b979b693c9408ddc1f63325654566fd1b4ee7b6fc18ec601e502203ed5a2c68096d403686cf60b6c1d740b8ae254ec94fc4c35d54f70e4c59a28340147522102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a5462103a2fef1829e0742b89c218c51898d9e7cb9d51201ba2bf9d9e9214ebb6af3270852ae00000000" self.multiple_input_multiple_ouput_result = "020000000001034b9f6c174b6c9fa18d730c17168c1749027acffcd5c809cdc07f7dc7f849d924000000006a47304402206932c93458a6ebb85f9fd6f69666cd383a3b8c8d517a096501438840d90493070220544d996a737ca9affda3573635b09e215be1ffddbee9b1260fc3d85d61d90ae5012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a546ffffffffa4a3005109721b697ac1d1d189a391ef845e31aa6e0911dc54dea8919cd6f4650000000000ffffffffa28af3847e4c5f5b380726f952fa0a8b7e5859cc5db5b5c239302a3a45c68f6c0000000000ffffffff03a0860100000000002200203956f9730cf7275000f4e3faf5db0505b216222c1f7ca1bdfb81a877003fcb93a086010000000000160014fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a10021b00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac000400483045022100ff3d84fea8f9ed0f467a4451846a834f94ee00538c5f70637540b8d3ec762e8802204f35aadacaff8e3c5b7d9ba9bb068141f6950feb9da68707249a485f519cbd9d01483045022100b0e1a9f73cb4674d780e24e0a1a7dc96719ce119eb2df2804653990ae2ca32e402203398b490e9caa872463191ba7a4ae84845a94d3b4025ee845491a9f71ea623390147522102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a5462103a2fef1829e0742b89c218c51898d9e7cb9d51201ba2bf9d9e9214ebb6af3270852ae024730440220733fcbd21517a1559e9561668e480ffd0a24b62520cfa16ca7689b20f7f82be402204f053a27f19e0bd1346676c74c65e9e452515bc6510ab307ac3a3fb6d3c89ca7012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000"
def setUp(self): setup('mainnet') self.message = "The test!" self.key_wifc = "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" self.priv = PrivateKey.from_wif(self.key_wifc) self.pub = self.priv.get_public_key() self.address = self.pub.get_address().to_string() self.external_address = '1LbxJuEHPsoFRVo3qM1YJRg7DfRD1RvUDe' self.deterministic_signature = 'IEiQ7kHfGqlxHSOcUftzR4gChjupJbuIIJCiY3LryQ9SXwPeRoBtJYkrNd/rgU7RP9jX6i2IaGGYMLt9bW+/bbI=' self.external_signature = 'H+yEsMrKoLqcdegOxYbZ4MFpQkRJligl1whXQDY2+g7EptxmOj9vC3n5ykdHkof0qEbmyV62syaKh+9C95V5R34='
def getAddress(id: str): # Initialise bitcoin.py setup('mainnet') wif = db.getWIF(id) priv = PrivateKey.from_wif(f"{wif}") pub = priv.get_public_key() address = pub.get_segwit_address().to_string() return address
def get_priv_key(address): # read master secret from hsm_secret hsm_secret = open(expanduser('~/.lightning/bitcoin/hsm_secret'), 'rb').read() # derive the bip32_seed salt = bytes([0]) or b'\x00' bip32_seed = Hkdf(salt, hsm_secret, hash=hashlib.sha256).expand(b'bip32 seed') master = network.keys.bip32_seed(bip32_seed) index = 0 while True: # derive an index subkey = master.subkey_for_path('0/0/' + str(index)) setup('mainnet') # create segwit address segwit = PrivateKey.from_wif( subkey.wif()).get_public_key().get_segwit_address() # wrap in P2SH address wrapped_p2sh = P2shAddress.from_script(segwit.to_script_pub_key()) if wrapped_p2sh.to_string() == address: break index += 1 plugin.log('found the address at index {}'.format(index), level="info") return master.subkey_for_path('0/0/' + str(index))
def setUp(self): setup('testnet') self.sk = PrivateKey.from_wif( "cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo") # n4bkvTyU1dVdzsrhWBqBw8fEMbHjJvtmJR self.p2pkh_addr = self.sk.get_public_key().get_address() # tb1ql5eh45als8sgdkt2drsl344q55g03sj2krzqe3 self.p2wpkh_addr = self.sk.get_public_key().get_segwit_address() # P2PKH to P2WPKH self.txin1 = TxInput( "5a7b3aaa66d6b7b7abcdc9f1d05db4eee94a700297a319e19454e143875e1078", 0) self.txout1 = TxOutput(to_satoshis(0.0099), self.p2wpkh_addr.to_script_pub_key()) # P2WPKH to P2PKH self.txin_spend = TxInput( "b3ca1c4cc778380d1e5376a5517445104e46e97176e40741508a3b07a6483ad3", 0) self.txin_spend_amount = to_satoshis(0.0099) self.txout2 = TxOutput(to_satoshis(0.0098), self.p2pkh_addr.to_script_pub_key()) self.p2pkh_redeem_script = Script([ 'OP_DUP', 'OP_HASH160', self.p2pkh_addr.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]) # P2WPKH P2PKH to P2PKH self.txin_spend_p2pkh = TxInput( "1e2a5279c868d61fb2ff0b1c2b04aa3eff02cd74952a8b4e799532635a9132cc", 0) self.txin_spend_p2pkh_amount = to_satoshis(0.01) self.txin_spend_p2wpkh = TxInput( "fff39047310fbf04bdd0e0bc75dde4267ae4d25219d8ad95e0ca1cee907a60da", 0) self.txin_spend_p2wpkh_amount = to_satoshis(0.0095) self.txout3 = TxOutput(to_satoshis(0.0194), self.p2pkh_addr.to_script_pub_key()) # SIGHASH NONE type send self.txin1_signone = TxInput( "fb4c338a00a75d73f9a6bd203ed4bd8884edeb111fac25a7946d5df6562f1942", 0) self.txin1_signone_amount = to_satoshis(0.01) self.txout1_signone = TxOutput(to_satoshis(0.0080), self.p2pkh_addr.to_script_pub_key()) self.txout2_signone = TxOutput(to_satoshis(0.0019), self.p2pkh_addr.to_script_pub_key()) # SIGHASH SINGLE type send self.txin1_sigsingle = TxInput( "b04909d4b5239a56d676c1d9d722f325a86878c9aa535915aa0df97df47cedeb", 0) self.txin1_sigsingle_amount = to_satoshis(0.0193) self.txout1_sigsingle = TxOutput(to_satoshis(0.01), self.p2pkh_addr.to_script_pub_key()) self.txout2_sigsingle = TxOutput(to_satoshis(0.0092), self.p2pkh_addr.to_script_pub_key()) # SIGHASH_ALL | SIGHASH_ANYONECANPAY type send self.txin1_siganyonecanpay_all = TxInput( "f67e97a2564dceed405e214843e3c954b47dd4f8b26ea48f82382f51f7626036", 0) self.txin1_siganyonecanpay_all_amount = to_satoshis(0.0018) self.txin2_siganyonecanpay_all = TxInput( "f4afddb77cd11a79bed059463085382c50d60c7f9e4075d8469cfe60040f68eb", 0) self.txin2_siganyonecanpay_all_amount = to_satoshis(0.0018) self.txout1_siganyonecanpay_all = TxOutput( to_satoshis(0.0018), self.p2pkh_addr.to_script_pub_key()) self.txout2_siganyonecanpay_all = TxOutput( to_satoshis(0.0017), self.p2pkh_addr.to_script_pub_key()) # SIGHASH_NONE | SIGHASH_ANYONECANPAY type send self.txin1_siganyonecanpay_none = TxInput( "d2ae5d4a3f390f108769139c9b5757846be6693b785c4e21eab777eec7289095", 0) self.txin1_siganyonecanpay_none_amount = to_satoshis(0.009) self.txin2_siganyonecanpay_none = TxInput( "ee5062d426677372e6de96e2eb47d572af5deaaef3ef225f3179dfa1ece3f4f5", 0) self.txin2_siganyonecanpay_none_amount = to_satoshis(0.007) self.txout1_siganyonecanpay_none = TxOutput( to_satoshis(0.008), self.p2pkh_addr.to_script_pub_key()) self.txout2_siganyonecanpay_none = TxOutput( to_satoshis(0.007), self.p2pkh_addr.to_script_pub_key()) # SIGHASH_SINGLE | SIGHASH_ANYONECANPAY type send self.txin1_siganyonecanpay_single = TxInput( "c7bb5672266c8a5b64fe91e953a9e23e3206e3b1a2ddc8e5999b607b82485042", 0) self.txin1_siganyonecanpay_single_amount = to_satoshis(0.01) self.txout1_siganyonecanpay_single = TxOutput( to_satoshis(0.005), self.p2pkh_addr.to_script_pub_key()) self.txout2_siganyonecanpay_single = TxOutput( to_satoshis(0.0049), self.p2pkh_addr.to_script_pub_key()) # result self.create_send_to_p2wpkh_result = "020000000178105e8743e15494e119a39702704ae9eeb45dd0f1c9cdabb7b7d666aa3a7b5a000000006b4830450221009ad68e1ecdd38d6abe515a52582a441a56f0fedb21816eb2f583183685da2eb502203c4fc7522ad7ab0c1854180cfd337e484ad3ba70d23bcf4380c6e2ff4e6e7985012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a546ffffffff01301b0f0000000000160014fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a00000000" self.spend_p2pkh_result = "02000000000101d33a48a6073b8a504107e47671e9464e10457451a576531e0d3878c74c1ccab30000000000ffffffff0120f40e00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac0247304402201c7ec9b049daa99c78675810b5e36b0b61add3f84180eaeaa613f8525904bdc302204854830d463a4699b6d69e37c08b8d3c6158185d46499170cfcc24d4a9e9a37f012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000" self.p2pkh_and_p2wpkh_to_p2pkh_result = "02000000000102cc32915a633295794e8b2a9574cd02ff3eaa042b1c0bffb21fd668c879522a1e000000006a47304402200fe842622e656a6780093f60b0597a36a57481611543a2e9576f9e8f1b34edb8022008ba063961c600834760037be20f45bbe077541c533b3fd257eae8e08d0de3b3012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a546ffffffffda607a90ee1ccae095add81952d2e47a26e4dd75bce0d0bd04bf0f314790f3ff0000000000ffffffff01209a1d00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac00024730440220274bb5445294033a36c360c48cc5e441ba8cc2bc1554dcb7d367088ec40a0d0302202a36f6e03f969e1b0c582f006257eec8fa2ada8cd34fe41ae2aa90d6728999d1012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000" self.test_signone_send_result = "0200000000010142192f56f65d6d94a725ac1f11ebed8488bdd43e20bda6f9735da7008a334cfb0000000000ffffffff0200350c00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac30e60200000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac02483045022100d3e7d4fceb7cded91f5d09ef192b5308d325ead1047ee5972a62747b8a937da502205e6bdeebe048f7923be75e36b6d39a78891fccbf0084ac1445f27a77261a13c2022102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000" self.test_sigsingle_send_result = "02000000000101ebed7cf47df90daa155953aac97868a825f322d7d9c176d6569a23b5d40949b00000000000ffffffff0240420f00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88acc0090e00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac02483045022100e315efea11d21b0819425f164751e4bbdd20f7fee8b0ee949da466ee013b73b7022048cb056d4823272518023222b39cdead68dc3a9b1e60aae37a8dd5a5108d2a62032102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000" self.test_siganyonecanpay_all_send_result = "02000000000102366062f7512f38828fa46eb2f8d47db454c9e34348215e40edce4d56a2977ef60000000000ffffffffeb680f0460fe9c46d875409e7f0cd6502c3885304659d0be791ad17cb7ddaff40000000000ffffffff0220bf0200000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac10980200000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac02483045022100b963e68c5d133c16c0bb9cdf82c2ace5acd5c03fc03a4572699ac2712bbe772202202075cf8e35d4093e71635c49844a009a16ff08b9ee2ff5876ef2f3bd17b93c63812102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a5460247304402206fb60dc79b5ca6c699d04ec96c4f196938332c2909fd17c04023ebcc7408f36e02202b071771a58c84e20b7bf1fcec05c0ef55c1100436a055bfcb2bf7ed1c0683a9012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000" self.test_siganyonecanpay_none_send_result = "02000000000102959028c7ee77b7ea214e5c783b69e66b8457579b9c136987100f393f4a5daed20000000000fffffffff5f4e3eca1df79315f22eff3aeea5daf72d547ebe296dee672736726d46250ee0000000000ffffffff0200350c00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac60ae0a00000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac0247304402203bbcbd2003244e9ccde7f705d3017f3baa2cb2d47efb63ede7e39704eff3987702206932aa4b402de898ff2fd3b2182f344dc9051b4c326dacc07b1e59059042f3ad822102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54602483045022100bd00b75aaefc0ab139f44b5347a8159d7158ae911bbf5a76f7fe1b93e2b0f1d50220724189279c0c497e15b4e3e1d1291f0b15e3dc460e8b2adf8597dbbd3af32440012102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000" self.test_siganyonecanpay_single_send_result = "02000000000101425048827b609b99e5c8dda2b1e306323ee2a953e991fe645b8a6c267256bbc70000000000ffffffff0220a10700000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac107a0700000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac02483045022100ff22bf77115243a01f1c39eca2d3a222e1e176d272b3eab561b6d625af0ee21a0220520b07b72ba5ab11f33a0ed921aac29a05ad09cc65107f3931a25711679562b0832102d82c9860e36f15d7b72aa59e29347f951277c21cd4d34822acdeeadbcff8a54600000000"
def load_data_json(priv=False, pub=False, pub_hash=False, timelock_csv=False, timelock_tx=False, timelock_log=False, p2pk=False): """loads parameters used to create P2SH address and spending transactions from the data.json file - initiates/converts applicable objects/data returns : dict of data for the Attributes specified - e.g {'priv_key': object, 'pub': object, 'pub_hash': 'string'} """ # reads user data from data.json with open('data.json', 'r') as json_file: data = json.load(json_file) usr_inputs = data['csv_timelock']['inputs'] # initiates key objects if priv or pub or pub_hash: usr_priv = usr_inputs['priv_key'] usr_pub = usr_inputs['pub_key'] if priv and not usr_priv: errors.missing_data('private key') elif usr_priv: priv_key = PrivateKey.from_wif(usr_priv) pub_key = priv_key.get_public_key() elif usr_pub: pub_key = PublicKey.from_hex(usr_pub) else: # prints error msg & raises systemExit errors.missing_data('public key') # loads timelock condition and converts format if timelock_csv or timelock_tx or timelock_log: lock_height = usr_inputs['block_lock'] if lock_height: seq = Sequence(TYPE_RELATIVE_TIMELOCK, lock_height) timelock_script = seq.for_script() timelock_txin = seq.for_input_sequence() else: # prints error msg & raises systemExit errors.missing_data('timelock period') # initaites P2pkhAddress object if p2pk: address = usr_inputs['p2pkh_address'] if address: p2pkh_address = P2pkhAddress.from_address(address) else: # prints error msg & raises systemExit errors.missing_data('P2PKH address') # fills return outputs with specified data from func arguments outputs = {} if priv: outputs['priv_key'] = priv_key if pub: outputs['pub_key'] = pub_key if pub_hash: outputs['pub_key_hash'] = pub_key.to_hash160() if timelock_csv: outputs['timelock_csv'] = timelock_script if timelock_tx: outputs['timelock_tx'] = timelock_txin if timelock_log: outputs['timelock_log'] = lock_height if p2pk: outputs['p2pk_addr'] = p2pkh_address return outputs
def setUp(self): setup('testnet') self.priv = PrivateKey.from_wif( 'cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo') self.pub = self.priv.get_public_key() self.p2sh_address = '2NDkr9uD2MSY5em3rsjkff8fLZcJzCfY3W1'
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")