<p>Some usage examples are now available in the repository.</p>
<ul>
<li><p><a href="https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2wpkh.py" rel="nofollow noreferrer">This example</a> demonstrates how to generate a key pair, and create a transaction paying to the public key hash (P2WPKH).<br />
There is also <a href="https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2pkh-txout.py" rel="nofollow noreferrer">the legacy version</a> (P2PKH).</p>
</li>
<li><p><a href="https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2wsh-txout.py" rel="nofollow noreferrer">This example</a> demonstrates how to use more complex Bitcoin Script with P2WSH.<br />
There is also <a href="https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2sh-txout.py" rel="nofollow noreferrer">the legacy version</a> (P2SH).</p>
</li>
</ul>

CC BY-SA 4.0
2020-08-12T10:31:14.117Z
2020-08-12T10:31:14.117Z
Antoine Poinsot
<p>Sorry I am not familiar with python-bitcoinlib but if your goal is to just create keys/addresses you can use <a href="https://github.com/mcdallas/cryptotools" rel="noreferrer">this</a> library (disclosure: I wrote it) the following way:</p>

<blockquote>
  <p>Generating a private key</p>
</blockquote>

<p><code>private = PrivateKey.random()</code> </p>

<p>or <code>private = PrivateKey.from_hex('yourhexkeyhere')</code> </p>

<p>or <code>private = PrivateKey.from_wif('yourwifhere')</code></p>

<p>and then to get the hex representation use <code>private.hex()</code> of <code>private.wif()</code></p>

<blockquote>
  <p>Creating a public key</p>
</blockquote>

<pre><code>public = private.to_public()
public.hex()
'044cc549c65f4b0b7713d7a763a01221f85fd34602ac8ff0386995349c55de7111d16b5...'
public.hex(compressed=True)
'034cc549c65f4b0b7713d7a763a01221f85fd34602ac8ff0386995349c55de7111'
</code></pre>

<blockquote>
  <p>Creating an address from the public key</p>
</blockquote>

<pre><code>public.to_address('P2PKH')
'16B2Ghyu5C1CofKfXF2Zi9JycqaAyaE8cd'
public.to_address('P2WPKH')
'bc1qtqshpjw8kr7jlk27gcvj7pvzt8lp6vn5dc2f24'
</code></pre>

<p>If you use this library please use it only as a learning tool and not to send real coins as there might still be some bugs.</p>

CC BY-SA 4.0
2018-05-31T09:21:33.770Z
2018-05-31T09:21:33.770Z
Mike D
<p>I'm trying to use the <a href="https://github.com/petertodd/python-bitcoinlib/" rel="noreferrer">python-bitcoinlib</a> library but its docs aren't entirely clear to me.
I'm a programmer but quite new to Python, and not very versed in the Bitcoin protocol.</p>

<p>What's the process, code-wise, with the library, for the following?</p>

<ol>
<li>Generating a private key</li>
<li>Creating a public key</li>
<li>Creating an address from the public key</li>
</ol>

<p>I tried various things and am not completely sure what arguments I supply to what functions, and what functions I'm supposed to use in the process.</p>

<p>I'd appreciate a lot, and I think it'd help me the most if I were provided code for achieving each step.</p>

<p>I was able to follow the Bitcoin protocol (specifically, the <a href="https://bitcoin.org/en/developer-reference" rel="noreferrer">developer reference</a> and the <a href="https://bitcoin.org/en/developer-guide" rel="noreferrer">developer guide</a> on <a href="http://bitcoin.org" rel="noreferrer">bitcoin.org</a> to do all of those things, and I'm looking specifically how to do it with the library, so I could have my code fully compatible with other functionality of it, since I'd like to use the library for additional purposes.</p>

<private-key><public-key><address-generation><python><python-bitcoinlib>
python-bitcoinlib | Create Private Keys, Public Keys and Addresses - How to Do It?
Gal Grünfeld
# python-bitcoinlib | Create Private Keys, Public Keys and Addresses - How to Do It?

(Asked by: username_0 on 2018-05-29)

I'm trying to use the [python-bitcoinlib](https://github.com/petertodd/python-bitcoinlib/) library but its docs aren't entirely clear to me.
I'm a programmer but quite new to Python, and not very versed in the Bitcoin protocol.

What's the process, code-wise, with the library, for the following?

1. Generating a private key
2. Creating a public key
3. Creating an address from the public key

I tried various things and am not completely sure what arguments I supply to what functions, and what functions I'm supposed to use in the process.

I'd appreciate a lot, and I think it'd help me the most if I were provided code for achieving each step.

I was able to follow the Bitcoin protocol (specifically, the [developer reference](https://bitcoin.org/en/developer-reference) and the [developer guide](https://bitcoin.org/en/developer-guide) on [bitcoin.org](http://bitcoin.org) to do all of those things, and I'm looking specifically how to do it with the library, so I could have my code fully compatible with other functionality of it, since I'd like to use the library for additional purposes.

**Tags:** private-key, public-key, address-generation, python, python-bitcoinlib


---

## 2 answers

### Answer by username_2 on 2018-05-31. Score: 6 | Accepted answer

Sorry I am not familiar with python-bitcoinlib but if your goal is to just create keys/addresses you can use this library (disclosure: I wrote it) the following way:

Generating a private key

private = PrivateKey.random() 
or private = PrivateKey.from_hex('yourhexkeyhere') 
or private = PrivateKey.from_wif('yourwifhere')
and then to get the hex representation use private.hex() of private.wif()

Creating a public key

public = private.to_public()
public.hex()
'044cc549c65f4b0b7713d7a763a01221f85fd34602ac8ff0386995349c55de7111d16b5...'
public.hex(compressed=True)
'034cc549c65f4b0b7713d7a763a01221f85fd34602ac8ff0386995349c55de7111'


Creating an address from the public key

public.to_address('P2PKH')
'16B2Ghyu5C1CofKfXF2Zi9JycqaAyaE8cd'
public.to_address('P2WPKH')
'bc1qtqshpjw8kr7jlk27gcvj7pvzt8lp6vn5dc2f24'

If you use this library please use it only as a learning tool and not to send real coins as there might still be some bugs.


---

### Answer by username_1 on 2020-08-12. Score: 3

Some usage examples are now available in the repository.

* [This example](https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2wpkh.py) demonstrates how to generate a key pair, and create a transaction paying to the public key hash (P2WPKH).  
  There is also [the legacy version](https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2pkh-txout.py) (P2PKH).
* [This example](https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2wsh-txout.py) demonstrates how to use more complex Bitcoin Script with P2WSH.  
  There is also [the legacy version](https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2sh-txout.py) (P2SH).


---

