How to generate Bitcoin address in Ruby?

0 votes

I'm trying to generate Bitcoin addresses in ruby by using this guide:

https://bhelx.simst.im/articles/generating-bitcoin-keys-from-scratch-with-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,600 points
921 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,240 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 27, 2018 in Blockchain by Perry
• 17,090 points
654 views
+2 votes
1 answer
0 votes
1 answer

How i can use nodejs to watch transactions in bitcoin network?

you can use  const Socket = require('blockchain.info/Socket'); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 26,600 points
60 views
0 votes
1 answer

How can I retrieve to and from address in transaction directly from blockchain?

The spending conditions, i.e., who is able ...READ MORE

answered Jul 11, 2018 in Blockchain by Shashank
• 10,350 points
90 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,240 points
55 views
0 votes
1 answer

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

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,340 points
103 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,240 points
129 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,240 points
77 views
0 votes
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,240 points
87 views