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:


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
>>> pubkey = pybitcointools.privtopub (privkey)
>>> pubkey
>>> h160 = pybitcointools.hash160 (pubkey.decode ('hex'))
>>> h160
>>> addr = pybitcointools.hex_to_b58check (h160)
>>> addr

Unfortunately I could not get from

 priv = random_key ()  --- > Hash160

How to do it?


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

    # 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())


if __name__ == "__main__":


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

  • I'll try to use this module! How to write a function in the code so that "PrivateKey" is generated in a random way? 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? 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.