Creating bitcoin address in ruby

0 votes

I am trying to create a bitcoin address in ruby according to the documentation of bitcoin wiki (bitcoin creation according bitcoin wiki). Starting point is just some random string which emulates the output of ripmed160. Unfortunately I don't quite succeed in doing so, here is my code:

require 'base58_gmp'
tx_hash = "a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5"

ripmed160 = tx_hash[0..39]
ripmed160_with_pre = "00" + ripmed160

sha1 = Digest::SHA256.hexdigest ripmed160_with_pre
sha2 = Digest::SHA256.hexdigest sha1

bin_address = Integer("0x" + ripmed160_with_pre + sha2[0..7])

bitcoin_address = "1" + Base58GMP.encode(bin_address, 'bitcoin')  # => "1GPcbTYDBwJ42MfKkedxjmJ3nrgoaNd2Sf"
Aug 28, 2018 in Blockchain by digger
• 26,740 points
515 views

1 answer to this question.

0 votes

When you calculate the SHA256 checksum, make sure to calculate it over the actual bytes of the previous step, not the hex encoding of those bytes:

# First convert to actual bytes.
bytes = [ripmed160_with_pre].pack('H*')

# Now calculate the first hash over the raw bytes, and
# return the raw bytes again for the next hash
# (note: digest not hexdigest).
sha1 = Digest::SHA256.digest bytes

# Second SHA256, using the raw bytes from the previous step
# but this time we can use hexdigest as the rest of the code
# assumes hex encoded strings
sha2 = Digest::SHA256.hexdigest sha1
answered Aug 28, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

+1 vote
1 answer
0 votes
1 answer

How to create Bitcoin Address in PHP?

You need to understand that Bitcoin address and ...READ MORE

answered Aug 28, 2018 in Blockchain by Perry
• 17,100 points
5,014 views
0 votes
1 answer

How to generate coin address using bitcoin-ruby?

The only difference between the addresses is ...READ MORE

answered Aug 29, 2018 in Blockchain by slayer
• 29,350 points
553 views
+1 vote
1 answer

Is there a way to send funds from a specific bitcoin address in a wallet?

I'll answer my own question, It was unbelievably ...READ MORE

answered Sep 3, 2018 in Blockchain by Perry
• 17,100 points
1,228 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,663 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
43,813 views
0 votes
1 answer

How do I add multiple recipients for transactions via Blockchain API?

Convert the recipes into JSON objects. x = ...READ MORE

answered Jul 6, 2018 in Blockchain by Perry
• 17,100 points
673 views
0 votes
1 answer

How to generate Bitcoin address in Ruby?

In your generate_address_from_public_key_hash method, the checksum should be over ...READ MORE

answered Aug 22, 2018 in Blockchain by slayer
• 29,350 points
2,287 views
0 votes
1 answer

How are unique public address generated in bitcoin?

In brief, the public addresses are generated ...READ MORE

answered Jul 16, 2018 in Blockchain by slayer
• 29,350 points
558 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP