4

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

example bitcoin address 18V7u8YNHKwG944TCkzYYj32hb6fdFPvQf

pinhead
  • 4,932
  • 2
  • 23
  • 38
R ford
  • 71
  • 2
  • 4

2 Answers2

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))
Tony Sanak
  • 1,654
  • 4
  • 22
  • 1
    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 May 26 '20 at 23:18
  • 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 May 27 '20 at 00:17
  • 1
    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 May 27 '20 at 00:56
  • 2
    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 May 28 '20 at 16:10
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 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.

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'
}
pinhead
  • 4,932
  • 2
  • 23
  • 38