Bitcoin Public Key To Address Uncompressed

0

1

So after searching for HOURS on here and overall on the internet, I haven't managed to find an answer to my question so I'll ask here.

So I've been trying to convert a public key into an address. I've been writing all the hashing algorithms, and tested them with online hashing websites, so they are working. Here is my code:

function getBTCAddress(public_key) 
{
   var __SHA = SHA256_hash(public_key);
   console.log("__SHA: " + __SHA);
   var RIPEMD = RIPEMD_160(__SHA);
   console.log("RIPEMD_160: " + RIPEMD);

   var new_SHA = SHA256_hash(RIPEMD.toString());
   console.log("new_SHA: " + new_SHA);
   var _new_SHA = SHA256_hash(new_SHA);
   console.log("_new_SHA: " + _new_SHA);

   var checksum = _new_SHA.slice(-8, _new_SHA.length);
   console.log("checksum:" + checksum);

   var almost_address = "00" + RIPEMD.toString() + checksum;
   console.log("almost_address:" + almost_address);

   var buffer = Buffer.from(almost_address, "hex");

   return BS58.encode(buffer);

}

For some reason it is not giving me the proper results. I'm wondering, what are the exact steps of deriving an address from a public key?

For those of your who are wondering here is what my code is doing:

  checksum = SHA256(SHA256(RIPEMD160(SHA256(public_key)))) <-- get last 8 bytes

  almost_address = 0x00 (network byte) and RIPEMD160(SHA256(public_key)) and checksum <-- compacted into 1 string

  address = base58(almost(address)

Dan shpaner

Posted 2019-05-15T19:48:20.557

Reputation: 3

Answers

3

You're hashing the string representation of things, not the actual hash itself. Hash the bytes of the hash, not the bytes of its string representation.

All of the should operations are on the bytes themselves, not the hexadecimal representation of those bytes which is what you have done.

Andrew Chow

Posted 2019-05-15T19:48:20.557

Reputation: 40 910

Also checksum is calculated after prefixing version byte to rmd160(sha256(pubkey)) not before.dave_thompson_085 2019-05-15T23:48:50.837

@dave_thompson_085 the calculation is the same if you haven't noticed.Dan shpaner 2019-05-16T17:52:14.447

@Danshpaner: I have noticed it is not the same. But go ahead and do whatever you want, it doesn't hurt me a bit.dave_thompson_085 2019-05-21T06:53:22.407