<p>I think this video will explain it in details (python):
<a href="https://youtu.be/tX-XokHf_nI" rel="nofollow noreferrer">https://youtu.be/tX-XokHf_nI</a></p>

<p>Using the code from video, pip packages <code>ecdsa</code> , <code>hashlib</code> and <code>base58</code>:</p>

<pre><code>import ecdsa
import hashlib
import base58

# WIF to private key by https://en.bitcoin.it/wiki/Wallet_import_format
Private_key = base58.b58decode_check("5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC") 
Private_key = Private_key[1:]

# Private key to public key (ecdsa transformation)
signing_key = ecdsa.SigningKey.from_string(Private_key, curve = ecdsa.SECP256k1)
verifying_key = signing_key.get_verifying_key()
public_key = bytes.fromhex("04") + verifying_key.to_string()

# hash sha 256 of pubkey
sha256_1 = hashlib.sha256(public_key)

# hash ripemd of sha of pubkey
ripemd160 = hashlib.new("ripemd160")
ripemd160.update(sha256_1.digest())

# checksum
hashed_public_key = bytes.fromhex("00") + ripemd160.digest()
checksum_full = hashlib.sha256(hashlib.sha256(hashed_public_key).digest()).digest()
checksum = checksum_full[:4]
bin_addr = hashed_public_key + checksum

# encode address to base58 and print
result_address = base58.b58encode(bin_addr)
print ("Bitcoin address {}".format(result_address))
</code></pre>

CC BY-SA 4.0
2020-05-26T23:18:14.277Z
Hey Tony, answers that just link to an external source for information generally aren't well regarded on stackexchange (links can break, etc). Its preferred that you copy/transcribe the relevant info into an answer here :)
chytrik
CC BY-SA 4.0
2020-05-27T00:17:33.337Z
I understand, but it's a 30-minute long video that covers this topic in high detail, is beginner-friendly, and in my opinion is a great answer to this question. Also it's not preventing others from adding full-explanation answers, and maybe this was already helpful to R ford (0 votes on the question shows that nobody else is interested). But I got it, next time I'll try to add the outline of the solution as well alongside with link.
Tony Sanak
CC BY-SA 4.0
2020-05-27T00:56:46.573Z
Yea, thats all totally fair. I don't mean to berate your contribution, it is certainly appreciated by users of this site, I just mean to encourage best practices around the site when possible.
chytrik
CC BY-SA 4.0
2020-05-28T16:10:32.343Z
Thanks, I agree, finally found some time to reply with working code. Still can add some more explanations, but maybe when I find even more time...
Tony Sanak
CC BY-SA 4.0
2020-05-26T16:08:12.963Z
2020-05-28T16:08:22.813Z
2020-05-28T16:08:22.813Z
Tony Sanak
<p>I don't do a lot of work in python but I thought I'd take a stab at this question to discover how easy it is for a new developer entering the space to compute a simple thing like an address.</p>

<p>The first thing I noticed is that, although there are <a href="https://pypi.org/search/?q=bitcoin" rel="nofollow noreferrer">a lot of search results</a> for Bitcoin Python packages, it's very difficult to evaluate if any of them are legit or written by well-known developers. The most popular Bitcoin package it seems is one written by Vitalik and deprecated many years ago.</p>

<p>I decided to go another route: <a href="https://electrum.org/#home" rel="nofollow noreferrer">Electrum.</a></p>

<p>Electrum is an extremely popular wallet with excellent support and I have personally met the developers. The codebase is on github, is well maintained, and well reviewed. I happen to know it's written in python.</p>

<p>After cloning and installing the repo: <a href="https://github.com/spesmilo/electrum/" rel="nofollow noreferrer">https://github.com/spesmilo/electrum/</a></p>

<p>The answer to your question takes two lines:</p>

<pre><code>$ cd electrum
$ python3

Python 3.7.6 (default, Dec 30 2019, 19:38:28) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

&gt;&gt;&gt; from electrum import bitcoin
&gt;&gt;&gt; bitcoin.address_from_private_key('5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC')
'1AsSgrcaWWTdmJBufJiGWB87dmwUf2PLMZ'
</code></pre>

<p>As a sanity check, I re-ran the computation using bcoin (A Javascript Bitcoin library) which I'm more familiar with:</p>

<pre><code>$ node

Welcome to Node.js v12.13.0.
Type ".help" for more information.

&gt; const bcoin=require('bcoin')
undefined

&gt; bcoin.KeyRing.fromSecret('5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC')
{
  witness: false,
  nested: false,
  publicKey: '04fb95541bf75e809625f860758a1bc38ac3c1cf120d899096194b94a5e700e891c7b6277d32c52266ab94af215556316e31a9acde79a8b39643c6887544fdf58c',
  script: null,
  program: null,
  type: 'pubkeyhash',
  address: '1AsSgrcaWWTdmJBufJiGWB87dmwUf2PLMZ'
}
</code></pre>

CC BY-SA 4.0
2020-05-28T12:34:04.170Z
2020-05-28T12:34:04.170Z
pinhead
<p>I have private key like this <code>5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC</code> how to convert to bicoin address using python or php?</p>

<p>example bitcoin address
<code>18V7u8YNHKwG944TCkzYYj32hb6fdFPvQf</code></p>

<address><private-key><public-key><python><php>
convert private key to bitcoin address using python or php
R ford
# convert private key to bitcoin address using python or php

(Asked by: username_0 on 2020-05-26)

I have private key like this `5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC` how to convert to bicoin address using python or php?

example bitcoin address
`18V7u8YNHKwG944TCkzYYj32hb6fdFPvQf`

**Tags:** address, private-key, public-key, python, php


---

## 2 answers

### Answer by username_1 on 2020-05-26. Score: 4

I think this video will explain it in details (python):
<https://youtu.be/tX-XokHf_nI>

Using the code from video, pip packages `ecdsa` , `hashlib` and `base58`:

```
import ecdsa
import hashlib
import base58

# WIF to private key by https://en.bitcoin.it/wiki/Wallet_import_format
Private_key = base58.b58decode_check("5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC") 
Private_key = Private_key[1:]

# Private key to public key (ecdsa transformation)
signing_key = ecdsa.SigningKey.from_string(Private_key, curve = ecdsa.SECP256k1)
verifying_key = signing_key.get_verifying_key()
public_key = bytes.fromhex("04") + verifying_key.to_string()

# hash sha 256 of pubkey
sha256_1 = hashlib.sha256(public_key)

# hash ripemd of sha of pubkey
ripemd160 = hashlib.new("ripemd160")
ripemd160.update(sha256_1.digest())

# checksum
hashed_public_key = bytes.fromhex("00") + ripemd160.digest()
checksum_full = hashlib.sha256(hashlib.sha256(hashed_public_key).digest()).digest()
checksum = checksum_full[:4]
bin_addr = hashed_public_key + checksum

# encode address to base58 and print
result_address = base58.b58encode(bin_addr)
print ("Bitcoin address {}".format(result_address))

```

> **Comments:**
> **username_2**: Hey username_1, answers that just link to an external source for information generally aren't well regarded on stackexchange (links can break, etc). Its preferred that you copy/transcribe the relevant info into an answer here :)  
> **username_1**: I understand, but it's a 30-minute long video that covers this topic in high detail, is beginner-friendly, and in my opinion is a great answer to this question. Also it's not preventing others from adding full-explanation answers, and maybe this was already helpful to username_0 (0 votes on the question shows that nobody else is interested). But I got it, next time I'll try to add the outline of the solution as well alongside with link.  
> **username_2**: Yea, thats all totally fair. I don't mean to berate your contribution, it is certainly appreciated by users of this site, I just mean to encourage best practices around the site when possible.  
> **username_1**: Thanks, I agree, finally found some time to reply with working code. Still can add some more explanations, but maybe when I find even more time...  

---

### Answer by username_3 on 2020-05-28. Score: 0

I don't do a lot of work in python but I thought I'd take a stab at this question to discover how easy it is for a new developer entering the space to compute a simple thing like an address.

The first thing I noticed is that, although there are [a lot of search results](https://pypi.org/search/?q=bitcoin) for Bitcoin Python packages, it's very difficult to evaluate if any of them are legit or written by well-known developers. The most popular Bitcoin package it seems is one written by Vitalik and deprecated many years ago.

I decided to go another route: [Electrum.](https://electrum.org/#home)

Electrum is an extremely popular wallet with excellent support and I have personally met the developers. The codebase is on github, is well maintained, and well reviewed. I happen to know it's written in python.

After cloning and installing the repo: <https://github.com/spesmilo/electrum/>

The answer to your question takes two lines:

```
$ cd electrum
$ python3

Python 3.7.6 (default, Dec 30 2019, 19:38:28) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> from electrum import bitcoin
>>> bitcoin.address_from_private_key('5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC')
'1AsSgrcaWWTdmJBufJiGWB87dmwUf2PLMZ'

```

As a sanity check, I re-ran the computation using bcoin (A Javascript Bitcoin library) which I'm more familiar with:

```
$ node

Welcome to Node.js v12.13.0.
Type ".help" for more information.

> const bcoin=require('bcoin')
undefined

> bcoin.KeyRing.fromSecret('5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC')
{
  witness: false,
  nested: false,
  publicKey: '04fb95541bf75e809625f860758a1bc38ac3c1cf120d899096194b94a5e700e891c7b6277d32c52266ab94af215556316e31a9acde79a8b39643c6887544fdf58c',
  script: null,
  program: null,
  type: 'pubkeyhash',
  address: '1AsSgrcaWWTdmJBufJiGWB87dmwUf2PLMZ'
}

```


---

