【问题标题】:How to Generate a Bitcoin Address in Ruby如何在 Ruby 中生成比特币地址
【发布时间】:2016-09-02 09:49:00
【问题描述】:

我正在尝试使用本指南在 ruby​​ 中生成比特币地址:

https://bhelx.simst.im/articles/generating-bitcoin-keys-from-scratch-with-ruby/

但有些地方不太对劲,因为生成的地址并不完全正确。

这是我正在使用的课程:

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

它输出如下内容:

private_key_hex: C96DE079BAE4877E086288DEDD6F9F70B671862B7E6E4FC0EC401CADB81EDF45
public_key_hex: 0422435DF80F62E643D3CFBA66194052EC9ED0DFB47A1B26A4731079A5FF84FBF98FF0A540B6981D75BA789E6192F3B38BABEF6B0286CAEB4CAFCB51BB96D97B46
public_key_hash: db34927cc5ec0066411f366d9a95f9c6369c6e1d
address: Lz3xnxx6Uh79PEzPpWSMMZJVWR36hJgVL

如果我将此地址插入 blockchain.info 和类似工具,它会说这是一个无效地址。

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby sha256 bitcoin


    【解决方案1】:

    在您的generate_address_from_public_key_hash 方法中,校验和应该在散列包括地址前缀之上。在分配 pk 变量后,您实际上并没有真正使用它。代码应该类似于:

    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
    

    错误似乎也在你链接的页面上,我猜作者一定是复制/粘贴错误。


    顺便说一句,将所有内容保存在十六进制字符串中并来回解码似乎是一种奇怪的方式。我原以为使用原始二进制字符串会更容易,并且在打印出值时只编码为十六进制。

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多