How to generate Bitcoin address in Ruby

0 votes

I'm trying to generate Bitcoin addresses in ruby.

Here's the class I'm using:

require 'openssl'

require 'ecdsa'

require 'securerandom'

require 'base58'


class BitcoinAddressGenerator


  ADDRESS_VERSION = '00'


  def self.generate_address

    # Bitcoin uses the secp256k1 curve

    curve = OpenSSL::PKey::EC.new('secp256k1')


    # Now we generate the public and private key together

    curve.generate_key


    private_key_hex = curve.private_key.to_s(16)

    puts "private_key_hex: #{private_key_hex}"

    public_key_hex = curve.public_key.to_bn.to_s(16)

    puts "public_key_hex: #{public_key_hex}"


    pub_key_hash = public_key_hash(public_key_hex)

    puts "pub_key_hash: #{pub_key_hash}"


    address = generate_address_from_public_key_hash(public_key_hash(public_key_hex))


    puts "address: #{address}"

  end


  def self.generate_address_from_public_key_hash(pub_key_hash)

    pk = ADDRESS_VERSION + pub_key_hash

    encode_base58(pub_key_hash + checksum(pub_key_hash))

  end


  def self.int_to_base58(int_val, leading_zero_bytes=0)

    alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

    base58_val, base = '', alpha.size

    while int_val > 0

      int_val, remainder = int_val.divmod(base)

      base58_val = alpha[remainder] + base58_val

    end

    base58_val

  end


  def self.encode_base58(hex)

    leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2

    ("1"*leading_zero_bytes) + int_to_base58( hex.to_i(16) )

  end


  def self.checksum(hex)

    sha256(sha256(hex))[0...8]

  end


  # RIPEMD-160 (160 bit) hash

  def self.rmd160(hex)

    Digest::RMD160.hexdigest([hex].pack("H*"))

  end


  def self.sha256(hex)

   Digest::SHA256.hexdigest([hex].pack("H*"))

  end


  # Turns public key into the 160 bit public key hash

  def self.public_key_hash(hex)

    rmd160(sha256(hex))

  end


end

It returns an invalid address.

Any suggestions?

Aug 22, 2018 in Blockchain by digger
• 26,740 points
2,681 views

1 answer to this question.

0 votes

In your generate_address_from_public_key_hash method, the checksum should be over the hash including the address prefix. You’re not actually using the pk variable at all at the moment after you assign it. The code should look something like:

def self.generate_address_from_public_key_hash(pub_key_hash)

  pk = ADDRESS_VERSION + pub_key_hash

  encode_base58(pk + checksum(pk)) # Using pk here, not pub_key_hash

end
answered Aug 22, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

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,451 views
+2 votes
1 answer

how to convert hash160 to bitcoin address in python?

Hey @Raj. I use the following script. ...READ MORE

answered Jul 12, 2019 in Blockchain by Tanisha
3,734 views
0 votes
0 answers

hd wallet bip44 in js - how to create an address for a chain other than bitcoin?

I have a small script basically taken ...READ MORE

Mar 2, 2022 in Blockchain by Aditya
• 7,680 points
989 views
+1 vote
0 answers

How to generate bitcoin address from XPUB

How to generate bitcoin addresses from XPUB ...READ MORE

Mar 9, 2022 in Blockchain by Soham
• 9,710 points
1,645 views
0 votes
1 answer

Creating bitcoin address in ruby

When you calculate the SHA256 checksum, make ...READ MORE

answered Aug 28, 2018 in Blockchain by slayer
• 29,370 points
758 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,426 views
0 votes
1 answer

Bitstamp API signature in Ruby

Here is a code that works: require 'open-uri' require ...READ MORE

answered Aug 28, 2018 in Blockchain by slayer
• 29,370 points
1,073 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,922 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,370 points
802 views
+1 vote
1 answer

How to generate Bitcoin address?

Convert the hex string back to bytes ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,370 points
1,150 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