I have private key like this 5JYJWrRd7sbqEzL9KR9dYTGrxyLqZEhPtnCtcvhC5t8ZvWgS9iC
how to convert to bicoin address using python or php?
example bitcoin address
18V7u8YNHKwG944TCkzYYj32hb6fdFPvQf
Bitcoin Stack Exchange is a question and answer site for Bitcoin users, developers, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI 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))
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'
}