Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] I Used These 3 Easy Steps to Create a Bitcoin Wallet in Python (Public/Private)

#1
I Used These 3 Easy Steps to Create a Bitcoin Wallet in Python (Public/Private)

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1091922&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p>As I write this, Bitcoin is in a deep bear market. That’s the perfect time to learn about the tech and start building! </p>
<p>After listening to a podcast from Lyn Alden today, I wondered if it is possible to <strong>programmatically create a Bitcoin wallet</strong>, i.e., a public/private key pair. </p>
<p>This can be extremely useful in practice, not only if you want to create an application that uses the <em>“decentralized money layer”</em> to transfer value between two parties in a fully automatic way, but also if you want to quickly create a public/private key pair to send and receive BTC without trusting a third party.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="373" height="560" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-337.png" alt="" class="wp-image-1092057" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-337.png 373w, https://blog.finxter.com/wp-content/uplo...00x300.png 200w" sizes="(max-width: 373px) 100vw, 373px" /></figure>
</div>
<p>You may not trust that wallet provider after all. It is in the nature of the Bitcoin protocol that if you desperately need it, you’ll need it quickly and without lots of trust assumptions. So better be prepared!</p>
<p>In this project, we’ll answer the following interesting question.</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1fa99.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Project</strong>: How to create a Bitcoin wallet in Python (public/private key pair)?</p>
<h2>Step 1: Install Library</h2>
<p>Use PIP to install the <code><a rel="noreferrer noopener" href="https://pypi.org/project/bitcoinaddress/" data-type="URL" data-id="https://pypi.org/project/bitcoinaddress/" target="_blank">bitcoinaddress</a></code> library in your actual or virtual environment. </p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f510.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Is It Safe?</strong> I investigated the library code from <a rel="noreferrer noopener" href="https://github.com/fortesp/bitcoinaddress" data-type="URL" data-id="https://github.com/fortesp/bitcoinaddress" target="_blank">the GitHub repository</a> associated with this library, and I couldn’t find any trust issues. Specifically, I searched for “hacks” in the code, such as sending the public/private key pair to a remote server, but the repository seems to be clean. It is also well-respected in the community, so unlikely to be tampered with. I didn’t check if the public/private key pairs have maximum entropy, i.e., are truly randomly created with all private keys having the same likelihood. I cannot guarantee that this is 100% safe because I don’t know the owner of the library — but it looks safe at first and second glance.</p>
<p>To install the library, here are three of the most common ways:</p>
<pre class="wp-block-preformatted"><strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Python 3</strong>
<code>pip3 install bitcoinaddress</code> <strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Standard Python and Python 2 Installation</strong>
<code>pip install bitcoinaddress</code> <strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Jupyter Notebook Cell</strong>
<code>!pip install bitcoinaddress</code></pre>
<p>Here’s what this looks like in my Jupyter Notebook:</p>
<figure class="wp-block-image size-large"><img decoding="async" loading="lazy" width="1024" height="234" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-334-1024x234.png" alt="" class="wp-image-1092026" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-334-1024x234.png 1024w, https://blog.finxter.com/wp-content/uplo...300x69.png 300w, https://blog.finxter.com/wp-content/uplo...68x176.png 768w, https://blog.finxter.com/wp-content/uplo...ge-334.png 1404w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-install-xxx-in-python/" data-type="post" data-id="653128" target="_blank" rel="noreferrer noopener">5 Steps to Install a Python Library</a></p>
</p>
<h2>Step 2: Import and Create Wallet</h2>
<p>The <code>Wallet</code> class from the <code>bitcoinaddress</code> module allows you to easily create a new and random public/private keypair using the <code>Wallet()</code> constructor method, i.e., all you need to create a new random Bitcoin wallet.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from bitcoinaddress import Wallet
wallet = Wallet()</pre>
<p>Stay with me. You’re almost done! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4aa.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Step 3: Print Wallet</h2>
<p>Next, <a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print</a> the content of the newly created wallet. This contains all the information you need about the public and private keys and addresses.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(wallet)</pre>
<p>In the following output, I <strong>bolded</strong> the two relevant lines with the public address and the private key:</p>
<pre class="wp-block-preformatted"><code>Private Key HEX: 6b789bec69f7f90c2ed73c8ee58f1f899b42fde5641359f6b76a27b4406399f7
<strong>Private Key WIF: 5JdcnccAMqs1t38VTPyeGHgBQ7KaYGueSqUAmLBTzVqFzh4ssUN</strong>
Private Key WIF compressed: KzpcxLACJzfktGQ4bWR1UUbvtzu133DNH2vv6ffC8nG1BFSUFBfr Public Key: 0415d47844bab349f12ae51a4b7f9d5eeab11ddf5d958e7fc67f6d29a456394be997d31989f6dcca716db63898c739621a86aa4a7bbe74c8936a6f1bbc7937c5c0 Public Key compressed: 0215d47844bab349f12ae51a4b7f9d5eeab11ddf5d958e7fc67f6d29a456394be9 <strong>Public Address 1: 14XyDoAgdGF7xiCrgux5Bd7P993PnXALuW </strong>
Public Address 1 compressed: 1LW26DRtBraVQ5ec7J5D3uQsM3AD3oVHXx Public Address 3: 32iX1WnnMkLQLc6beTQ6no5H4J6arvUeBP Public Address bc1 P2WPKH: bc1q6hn4e55vfh6ka0z88tpr2jmqze8w4j84axsjh4 Public Address bc1 P2WSH: bc1qhff5zxmy7rs5mvx037ztg95nnnqe97fet66l65xgsafv89tmz8xssm8tph </code></pre>
<p>The output of the <code>bitcoinaddress.Wallet()</code> method provides the details of a new bitcoin wallet.</p>
<p>It includes the private key in both HEX and Wallet Import Format (WIF) formats, as well as the compressed version of the WIF. </p>
<p>It also provides the public key, both in uncompressed and compressed formats, as well as three different public addresses generated from the public key. </p>
<p>I actually checked the address on a Blockchain explorer, and it’s the correct one:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="693" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-335-1024x693.png" alt="" class="wp-image-1092030" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-335-1024x693.png 1024w, https://blog.finxter.com/wp-content/uplo...00x203.png 300w, https://blog.finxter.com/wp-content/uplo...68x520.png 768w, https://blog.finxter.com/wp-content/uplo...6x1040.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-335.png 1657w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>I also checked if the public and private addresses match and they seem to do:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="716" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-336-1024x716.png" alt="" class="wp-image-1092033" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-336-1024x716.png 1024w, https://blog.finxter.com/wp-content/uplo...00x210.png 300w, https://blog.finxter.com/wp-content/uplo...68x537.png 768w, https://blog.finxter.com/wp-content/uplo...ge-336.png 1340w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Additionally, it provides 2 SegWit addresses generated from the public key; one in Pay-to-Witness-Public-Key-Hash (P2WPKH) format and one in Pay-to-Witness-Script-Hash (P2WSH) format.</p>
</div>


https://www.sickgaming.net/blog/2023/01/...c-private/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016