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?

2 Answers 2


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, 2020 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))
    – user103136
    Sep 12, 2020 at 7:11
  • Can I use the HEX format here? Sep 12, 2020 at 7:35
  • If you are asking about private key WIF, it's base58: learnmeabitcoin.com/technical/wif
    – user103136
    Sep 12, 2020 at 8:05
  • IDK if the P2PKH address is good, probably because I can see the Testnet 'm' prefix. Maybe verify the checksum. That's a good point. AFAIK, if you want know all details to transform a private key in an address, you can have a look at royalforkblog.github.io/2014/08/11/graphical-address-generator. However, it's a valid answer I think.
    – Loopite
    Jun 4, 2022 at 7:56

I find the Bit module the easiest and most straight forward, albeit a tad, limited but with bit you can simply:

from bit import Key

# To generate private key
privKey= Key()

# Create Ripemd-160 Hash (address)

# or 

myAddress = privKey.address

# Segwit

MySegwitAddress = privKey.segwit_address

You can also import your own existing private keys like so:

# For wif format:
privKey = Key.('L1VotKmtZRLZSnSPhLhQxfts2aqBMru2APTs4Yuc8TYJ4jNhQoGB')

# For hex format:

privKey = Key.from_hex('ccb8c423403f4d5b6cde505d2a8f39d1d9399f6e06853339f5b11593cc502dc9')

# For int format:

privKey = Key.from_int('92598274663553697917256765988661195316245391320286084491562671024534692310473')

# For bytes format
privKey = Key.from_der(b'\xcc\xb8\xc4#@?M[l\xdeP]*\x8f9\xd1\xd99\x9fn\x06\x8539\xf5\xb1\x15\x93\xccP-\xc9)

Exporting works pretty much the same way as importing except you replace from.[format here] with to.[format here], you can even import or export a PEM file of your private key.

The documentation is enough for the library to give you the core functions, but is also somewhat lacking, for example you can't create compressed bitcoin addresses. But otherwise you can sign, send transactions, even select exactly which outputs you want to use .etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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