{"id":131482,"date":"2023-01-28T18:35:52","date_gmt":"2023-01-28T18:35:52","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1091922"},"modified":"2023-01-28T18:35:52","modified_gmt":"2023-01-28T18:35:52","slug":"i-used-these-3-easy-steps-to-create-a-bitcoin-wallet-in-python-public-private","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/28\/i-used-these-3-easy-steps-to-create-a-bitcoin-wallet-in-python-public-private\/","title":{"rendered":"I Used These 3 Easy Steps to Create a Bitcoin Wallet in Python (Public\/Private)"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>As I write this, Bitcoin is in a deep bear market. That&#8217;s the perfect time to learn about the tech and start building! <\/p>\n<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>\n<p>This can be extremely useful in practice, not only if you want to create an application that uses the <em>&#8220;decentralized money layer&#8221;<\/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>\n<div class=\"wp-block-image\">\n<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\/uploads\/2023\/01\/image-337-200x300.png 200w\" sizes=\"auto, (max-width: 373px) 100vw, 373px\" \/><\/figure>\n<\/div>\n<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&#8217;ll need it quickly and without lots of trust assumptions. So better be prepared!<\/p>\n<p>In this project, we&#8217;ll answer the following interesting question.<\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1fa99.png\" alt=\"\ud83e\ude99\" 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>\n<h2>Step 1: Install Library<\/h2>\n<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>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f510.png\" alt=\"\ud83d\udd10\" 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&#8217;t find any trust issues. Specifically, I searched for &#8220;hacks&#8221; 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&#8217;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&#8217;t know the owner of the library &#8212; but it looks safe at first and second glance.<\/p>\n<p>To install the library, here are three of the most common ways:<\/p>\n<pre class=\"wp-block-preformatted\"><strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Python 3<\/strong>\n<code>pip3 install bitcoinaddress<\/code> <strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Standard Python and Python 2 Installation<\/strong>\n<code>pip install bitcoinaddress<\/code> <strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Jupyter Notebook Cell<\/strong>\n<code>!pip install bitcoinaddress<\/code><\/pre>\n<p>Here&#8217;s what this looks like in my Jupyter Notebook:<\/p>\n<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\/uploads\/2023\/01\/image-334-300x69.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-334-768x176.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-334.png 1404w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<\/p>\n<h2>Step 2: Import and Create Wallet<\/h2>\n<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>\n<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\nwallet = Wallet()<\/pre>\n<p>Stay with me. You&#8217;re almost done! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4aa.png\" alt=\"\ud83d\udcaa\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Step 3: Print Wallet<\/h2>\n<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>\n<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>\n<p>In the following output, I <strong>bolded<\/strong> the two relevant lines with the public address and the private key:<\/p>\n<pre class=\"wp-block-preformatted\"><code>Private Key HEX: 6b789bec69f7f90c2ed73c8ee58f1f899b42fde5641359f6b76a27b4406399f7\n<strong>Private Key WIF: 5JdcnccAMqs1t38VTPyeGHgBQ7KaYGueSqUAmLBTzVqFzh4ssUN<\/strong>\nPrivate Key WIF compressed: KzpcxLACJzfktGQ4bWR1UUbvtzu133DNH2vv6ffC8nG1BFSUFBfr Public Key: 0415d47844bab349f12ae51a4b7f9d5eeab11ddf5d958e7fc67f6d29a456394be997d31989f6dcca716db63898c739621a86aa4a7bbe74c8936a6f1bbc7937c5c0 Public Key compressed: 0215d47844bab349f12ae51a4b7f9d5eeab11ddf5d958e7fc67f6d29a456394be9 <strong>Public Address 1: 14XyDoAgdGF7xiCrgux5Bd7P993PnXALuW <\/strong>\nPublic Address 1 compressed: 1LW26DRtBraVQ5ec7J5D3uQsM3AD3oVHXx Public Address 3: 32iX1WnnMkLQLc6beTQ6no5H4J6arvUeBP Public Address bc1 P2WPKH: bc1q6hn4e55vfh6ka0z88tpr2jmqze8w4j84axsjh4 Public Address bc1 P2WSH: bc1qhff5zxmy7rs5mvx037ztg95nnnqe97fet66l65xgsafv89tmz8xssm8tph <\/code><\/pre>\n<p>The output of the <code>bitcoinaddress.Wallet()<\/code> method provides the details of a new bitcoin wallet.<\/p>\n<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>\n<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>\n<p>I actually checked the address on a Blockchain explorer, and it&#8217;s the correct one:<\/p>\n<div class=\"wp-block-image\">\n<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\/uploads\/2023\/01\/image-335-300x203.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-335-768x520.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-335-1536x1040.png 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-335.png 1657w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>I also checked if the public and private addresses match and they seem to do:<\/p>\n<div class=\"wp-block-image\">\n<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\/uploads\/2023\/01\/image-336-300x210.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-336-768x537.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-336.png 1340w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) As I write this, Bitcoin is in a deep bear market. That&#8217;s the perfect time to learn about the tech and start building! After listening to a podcast from Lyn Alden today, I wondered if it is possible to programmatically create a Bitcoin wallet, i.e., a public\/private key pair. This can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-131482","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=131482"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131482\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}