<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')

privKey = Key.from_byte('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:14:55.700Z
2022-12-15T16:14:55.700Z
fredsta98
<p>For this very reduced case, you can take a look at <a href="https://github.com/circulosmeos/bitcoin-in-tiny-pieces" rel="nofollow noreferrer">https://github.com/circulosmeos/bitcoin-in-tiny-pieces</a></p>

<p>It has each operation coded for ease of understanding, isolated in a different file, but they're easy to join or to use directly from command line: for example:</p>

<p><code>$ echo 0x01 | ./bitcoin-public-from-private.py | ./bitcoin-address-from-public-key.py</code></p>

<p>And for the question:</p>

<blockquote>
  <p>For instance could there be an arbitrary or infinite number of pub keys from the private key?</p>
</blockquote>

<p>In principle, from a private key only one public key is derived: but <a href="https://bitcoin.stackexchange.com/a/67162/71774">this can be coded in three different Bitcoin addresses</a>. Normally only one or two of these formats are used. </p>

<p>This is in the most simple case, not considering more complex cases introduced later in Bitcoin (like <a href="https://en.bitcoin.it/wiki/Deterministic_wallet" rel="nofollow noreferrer">HD keys</a>, etc)...</p>

CC BY-SA 4.0
2018-11-19T13:47:41.147Z
Hey thank you!  I forked your repository at https://github.com/jamesyoungdigital/bitcoin-in-tiny-pieces.git to add a new little tool that checks brainwallet address balances.  I wasn't having much success with the shell script.  It now depends on my fork of pybitcointools.git, which is really easy to use and depends on almost nothing.  Hope you might like the little additions! :)
James Young
CC BY-SA 4.0
2018-11-19T17:25:26.840Z
Glad it has been useful! As for the shell script just open an issue and I'll take a look: it'd work flawlessly!
circulosmeos
CC BY-SA 4.0
2018-11-19T17:35:47.217Z
Star given!  I'd like to give a few more but they won't let me, haha. :-)  With the shell script, I try like this:  ./bitcoin-test-address-balance.sh satoshi but the output is always: ... 2 .... n and as far as I can tell, never ends.  The problem seems to be in the address balance python script, lines 62 to 64, when an exception is caught.  The URL should use the {} for interpolation.  Please see my pull request.  Thanks! :-)
James Young
CC BY-SA 4.0
2018-11-20T20:33:50.937Z
Hi James, I think that the exception is correctly caught: the thing is that blockchain.info sometimes discard the petition, probably due to high traffic: in that case the script sleeps 1 to n minutes between retries: as it is done for running in batches, this is not considered a problem :-o but thanks for your pull!
circulosmeos
CC BY-SA 4.0
2018-11-19T07:12:38.940Z
2018-11-19T07:29:57.177Z
2018-11-19T07:29:57.177Z
circulosmeos
<p>I'm looking for a simple, easy to use Bitcoin library for Python.</p>

<p>What I want to do is verify I can enter a hex private key that I want, and get the expected pubkey in compressed format, and the 1Btc... address.</p>

<p>I've looked at cryptotools on github which looked promising, and easy to use, but it would not accept a hex value that wasn't a multiple of 2.  I know that sounds odd, but I want to be able to enter a hex value like c12 and have it work as expected (unless I have to enter 0c12 and that will work, with the leading zero discarded).  I expect than when I enter 0cfa721d that this will be interpreted as big endian.</p>

<p>bitcoinlib looks promising, but I find it confusing and I've tried for a couple of hours but unsure of how to get what I want.</p>

<ol>
<li>Input private key in hex</li>
<li>Get back pub key in hash160 format, compressed</li>
<li>Get the 1Btc... address as well.</li>
</ol>

<p>I wonder if this is a strict mathematical relationship.  For instance could there be an arbitrary or infinite number of pub keys from the private key?  Is there a way to ensure I get the pubkey I want from the private key?  I want to test known private keys with the pubkey and BTC address generated that corresponds to known private keys and addresses, so I can try and figure out how whoever generated these addresses from choices of private keys I can try to replicate and do the same thing myself.</p>

<p>Thank you for any help or suggestions.</p>

CC BY-SA 4.0
2018-11-18T16:51:14.843Z
Hi, author of cryptotools here. The reason it wont accept odd digits hex is that it will store the private key inteternally as Python bytes object and the bytes.fromhex method requires full bytes. I'll have a look at it. As a workaround you can simply pad with a zero if it has odd amount of digits.0x0cfa721d is exactly the same as 0xcfa721d. The other functionality is there, just do `myprivatekey.to_public().to_address('P2PKH', compressed=True)`
Mike D
<address><private-key><public-key><address-generation><private-key-import>
Using Python: create a private key of my choosing, then generate public key and address
James Young
# Using Python: create a private key of my choosing, then generate public key and address

(Asked by: username_0 on 2018-11-18)

I'm looking for a simple, easy to use Bitcoin library for Python.

What I want to do is verify I can enter a hex private key that I want, and get the expected pubkey in compressed format, and the 1Btc... address.

I've looked at cryptotools on github which looked promising, and easy to use, but it would not accept a hex value that wasn't a multiple of 2. I know that sounds odd, but I want to be able to enter a hex value like c12 and have it work as expected (unless I have to enter 0c12 and that will work, with the leading zero discarded). I expect than when I enter 0cfa721d that this will be interpreted as big endian.

bitcoinlib looks promising, but I find it confusing and I've tried for a couple of hours but unsure of how to get what I want.

1. Input private key in hex
2. Get back pub key in hash160 format, compressed
3. Get the 1Btc... address as well.

I wonder if this is a strict mathematical relationship. For instance could there be an arbitrary or infinite number of pub keys from the private key? Is there a way to ensure I get the pubkey I want from the private key? I want to test known private keys with the pubkey and BTC address generated that corresponds to known private keys and addresses, so I can try and figure out how whoever generated these addresses from choices of private keys I can try to replicate and do the same thing myself.

Thank you for any help or suggestions.

**Tags:** address, private-key, public-key, address-generation, private-key-import

> **Comments:**
> **username_1**: Hi, author of cryptotools here. The reason it wont accept odd digits hex is that it will store the private key inteternally as Python bytes object and the bytes.fromhex method requires full bytes. I'll have a look at it. As a workaround you can simply pad with a zero if it has odd amount of digits.0x0cfa721d is exactly the same as 0xcfa721d. The other functionality is there, just do `myprivatekey.to_public().to_address('P2PKH', compressed=True)`  

---

## 2 answers

### Answer by username_3 on 2018-11-19. Score: 3 | Accepted answer

For this very reduced case, you can take a look at <https://github.com/username_3/bitcoin-in-tiny-pieces>

It has each operation coded for ease of understanding, isolated in a different file, but they're easy to join or to use directly from command line: for example:

`$ echo 0x01 | ./bitcoin-public-from-private.py | ./bitcoin-address-from-public-key.py`

And for the question:

> For instance could there be an arbitrary or infinite number of pub keys from the private key?

In principle, from a private key only one public key is derived: but [this can be coded in three different Bitcoin addresses](https://bitcoin.stackexchange.com/a/67162/71774). Normally only one or two of these formats are used.

This is in the most simple case, not considering more complex cases introduced later in Bitcoin (like [HD keys](https://en.bitcoin.it/wiki/Deterministic_wallet), etc)...

> **Comments:**
> **username_0**: Hey thank you!  I forked your repository at https://github.com/jamesyoungdigital/bitcoin-in-tiny-pieces.git to add a new little tool that checks brainwallet address balances.  I wasn't having much success with the shell script.  It now depends on my fork of pybitcointools.git, which is really easy to use and depends on almost nothing.  Hope you might like the little additions! :)  
> **username_3**: Glad it has been useful! As for the shell script just open an issue and I'll take a look: it'd work flawlessly!  
> **username_0**: Star given!  I'd like to give a few more but they won't let me, haha. :-)  With the shell script, I try like this:  ./bitcoin-test-address-balance.sh satoshi but the output is always: ... 2 .... n and as far as I can tell, never ends.  The problem seems to be in the address balance python script, lines 62 to 64, when an exception is caught.  The URL should use the {} for interpolation.  Please see my pull request.  Thanks! :-)  
> **username_3**: Hi username_0, I think that the exception is correctly caught: the thing is that blockchain.info sometimes discard the petition, probably due to high traffic: in that case the script sleeps 1 to n minutes between retries: as it is done for running in batches, this is not considered a problem :-o but thanks for your pull!  

---

### Answer by username_2 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')

privKey = Key.from_byte('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


---

