Post
Topic
Board Project Development
Merits 2 from 1 user
50 BTC bounty for a PEM public key to bitcoin address convertor
by
grondilu
on 05/01/2011, 20:28:15 UTC
⭐ Merited by ABCbits (2)
*** EDIT:  bounty has closed.  Hal won it. ***

I've tried to make one but I've failed, so I start an other bounty.

dirtyfilthy has made a C program which exports a wallet's private key to a openssl readable PEM format.


So given a bitcoin address such as :

btc=1Hy9dexzNzjvQYkYy6zKRVZMU8k2j5vuPt


I can run :

bc_key $btc ~/.bitcoin/wallet.dat |
openssl ec -pubout

Which gives me a public key :

-----BEGIN PUBLIC KEY-----
MIH1MIGuBgcqhkjOPQIBMIGiAgEBMCwGByqGSM49AQECIQD/////////////////
///////////////////+///8LzAGBAEABAEHBEEEeb5mfvncu6xVoGKVzocLBwKb
/NstzijZWfKBWxb4F5hIOtp3JqPEZV2k+/wOEQio/Re0SKaFVBmcR9CP+xDUuAIh
AP////////////////////66rtzmr0igO7/SXozQNkFBAgEBA0IABJJ6TBhmiWm4
Y1ACBVJVn0oyG9Ay5IzEZq8cPyrs1PERl963YQh5UrGOT0NodynfHswkz8bUpaJW
FsowR/l9wXc=
-----END PUBLIC KEY-----


Now I need a program that reads this public key, and ouputs the corresponding bitcoin address.

Such a program exists in the bitcoin code.  I think it's the function "PubKeyToAddress" in base58.h.

I want to implement this function in bash, using only openssl et standard filters.

As I understand it, I have to do the following steps :

- computes the sha256 of the key ;
- computes the rmd160 of this hash ;
- add a byte at the begin for the version number (?) ;
- add four bytes at the end for the check sum ;
- encode the result in base58 ;




I've tried using blockexplorer.com/q/hashtoaddress for the last three steps.  This gave me this code :


wget -O - -q http://blockexplorer.com/q/hashtoaddress/$(
grep -v 'PUBLIC KEY' <<<"
-----BEGIN PUBLIC KEY-----
MIH1MIGuBgcqhkjOPQIBMIGiAgEBMCwGByqGSM49AQECIQD/////////////////
///////////////////+///8LzAGBAEABAEHBEEEeb5mfvncu6xVoGKVzocLBwKb
/NstzijZWfKBWxb4F5hIOtp3JqPEZV2k+/wOEQio/Re0SKaFVBmcR9CP+xDUuAIh
AP////////////////////66rtzmr0igO7/SXozQNkFBAgEBA0IABJJ6TBhmiWm4
Y1ACBVJVn0oyG9Ay5IzEZq8cPyrs1PERl963YQh5UrGOT0NodynfHswkz8bUpaJW
FsowR/l9wXc=
-----END PUBLIC KEY-----" |
base64 -d |
openssl dgst -sha256 |
cut -d\  -f2 |
xxd -r -p |
openssl dgst -rmd160 |
cut -d\  -f2
)


But it just doesn't give me the correct bitcoin address.

50 BTC for whoever fixes that.