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)
New contributor
Dan shpaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.

  • Also checksum is calculated after prefixing version byte to rmd160(sha256(pubkey)) not before. – dave_thompson_085 May 15 at 23:48
  • @dave_thompson_085 the calculation is the same if you haven't noticed. – Dan shpaner May 16 at 17:52
  • @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 6 hours ago

Your Answer

Dan shpaner is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.