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?

2 Answers 2

1

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

5
  • 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
0

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)
privKey.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.