0

I want to fix the script code. The script generates from a random private key Bitcoin Address by function (pubtoaddr) and works on the Bitcoin module:

https://github.com/primal100/pybitcointools/blob/master/cryptos/main.py

from bitcoin import *
import os

    priv = random_key ()
    pub = privtopub (priv)
    addr = pubtoaddr (pub)

....
....

But I need to get from a Random Private Key in Hash160

For some reason I could not find such a function in main.py

I searched and found an article

>>> import pybitcointools
>>> privkey = pybitcointools.random_key ()
>>> privkey
'34030ce14e32ac982419b3683af3b59d64a29cc93798cf479d610af49d425d13'
>>> pubkey = pybitcointools.privtopub (privkey)
>>> pubkey
'04ce0ed35340803b0c21f2f7f5d5ab9d687e5fa95a79471c9b5c9d97a0bb170eac1045230cc51d13b85a5f64feb80f8fc19358a396797926e3f89d49066b1abc07'
>>> h160 = pybitcointools.hash160 (pubkey.decode ('hex'))
>>> h160
'1558c7cd9825447a31990ff964f347bb2dbfe9be'
>>> addr = pybitcointools.hex_to_b58check (h160)
>>> addr
'12wsYc4B9c9JeREqstLZXNWa3n4i42M5jg'

Unfortunately I could not get from

 priv = random_key ()  --- > Hash160

How to do it?

0

I am using a different bitcoin library. This prints address and hash160 from a private key:


from bitcoinutils.setup import setup
from bitcoinutils.keys import P2pkhAddress, PrivateKey, PublicKey

def main():
    # setup the network
    setup('testnet')

    # provide a private WIF key
    priv = PrivateKey.from_wif('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87K7XCyj5v')

    # get the public key
    pub = priv.get_public_key()


    # get address from public key
    address = pub.get_address()


    # print address and hash160
    print("Address:", address.to_string())
    print("Hash160:", address.to_hash160())

    print("\n--------------------------------------\n")



if __name__ == "__main__":
    main()


python-output

For segwit addresses you can refer to this example: https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_segwit_addresses.py

4
  • I'll try to use this module! How to write a function in the code so that "PrivateKey" is generated in a random way? – Derick Swodnick Sep 12 '20 at 5:12
  • Replace priv = PrivateKey.from_wif('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87K7XCyj5v') with priv = PrivateKey() in the above code and also add a print statement below to see the new private key each time you run the code: print("Private Key:", priv.to_wif(compressed=True)) – Prayank Sep 12 '20 at 7:11
  • Can I use the HEX format here? – Derick Swodnick Sep 12 '20 at 7:35
  • If you are asking about private key WIF, it's base58: learnmeabitcoin.com/technical/wif – Prayank Sep 12 '20 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.