<p>I find the Bit module the easiest and most straight forward, albeit a tad, limited but with bit you can simply:</p>
<pre class="lang-py prettyprint-override"><code>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

</code></pre>
<p>You can also import your own existing private keys like so:</p>
<pre class="lang-py prettyprint-override"><code>
# 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)

</code></pre>
<p>Exporting works pretty much the same way as importing except you replace <code>from.[format here]</code> with <code>to.[format here]</code>, you can even import or export a PEM file of your private key.</p>
<p>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</p>

CC BY-SA 4.0
2022-12-15T16:06:21.283Z
2022-12-15T16:31:04.437Z
2022-12-15T16:31:04.437Z
fredsta98
<p>I want to fix the script code. The script generates from a random private key Bitcoin Address by function <strong>(pubtoaddr)</strong> and works
on the Bitcoin module:</p>
<p><a href="https://github.com/primal100/pybitcointools/blob/master/cryptos/main.py" rel="nofollow noreferrer">https://github.com/primal100/pybitcointools/blob/master/cryptos/main.py</a></p>
<pre><code>from bitcoin import *
import os

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

....
....
</code></pre>
<p>But I need to get from a <strong>Random Private Key</strong> in <strong>Hash160</strong></p>
<p>For some reason I could not find such a function in <strong>main.py</strong></p>
<p>I searched and found an <a href="https://whatdoesthequantsay.com/2014/05/04/bitcoin-transactions-technically-part-2" rel="nofollow noreferrer">article</a></p>
<pre><code>&gt;&gt;&gt; import pybitcointools
&gt;&gt;&gt; privkey = pybitcointools.random_key ()
&gt;&gt;&gt; privkey
'34030ce14e32ac982419b3683af3b59d64a29cc93798cf479d610af49d425d13'
&gt;&gt;&gt; pubkey = pybitcointools.privtopub (privkey)
&gt;&gt;&gt; pubkey
'04ce0ed35340803b0c21f2f7f5d5ab9d687e5fa95a79471c9b5c9d97a0bb170eac1045230cc51d13b85a5f64feb80f8fc19358a396797926e3f89d49066b1abc07'
&gt;&gt;&gt; h160 = pybitcointools.hash160 (pubkey.decode ('hex'))
&gt;&gt;&gt; h160
'1558c7cd9825447a31990ff964f347bb2dbfe9be'
&gt;&gt;&gt; addr = pybitcointools.hex_to_b58check (h160)
&gt;&gt;&gt; addr
'12wsYc4B9c9JeREqstLZXNWa3n4i42M5jg'
</code></pre>
<p>Unfortunately I could not get from</p>
<pre><code> priv = random_key ()  --- &gt; Hash160
</code></pre>
<p>How to do it?</p>

<private-key><public-key><bitcoin-core-development><python><pybitcointools>
How to generate Hash160 from a private key with the Bitcoin module
Derick Swodnick
# How to generate Hash160 from a private key with the Bitcoin module

(Asked by: username_0 on 2020-09-11)

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](https://whatdoesthequantsay.com/2014/05/04/bitcoin-transactions-technically-part-2)

```
>>> 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?

**Tags:** private-key, public-key, bitcoin-core-development, python, pybitcointools


---

## 1 answers

### Answer by username_1 on 2022-12-15. Score: 0

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

```py
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:

```py

# 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


---

