检查比特币地址是否有效?

时间:2014-03-02 12:07:00

标签: ruby-on-rails ruby bitcoin coinbase-api

我想知道有没有办法检查比特币地址是否有效?

我在铁轨上使用红宝石。也许我可以使用coinbase api向帐户发送一个satoshi并查看它是否结算?

2 个答案:

答案 0 :(得分:5)

如果您不想维护自己的实施,可以使用the bitcoin-ruby gem

具体来说:Bitcoin.valid_address? "your_address_here"

答案 1 :(得分:4)

来自github的bitcoin_address_validator.rb检查比特币地址是否有效:

require 'digest'

class BitcoinAddressValidator < ActiveModel::EachValidator
  def validate(record, field, value)
    unless valid_bitcoin_address?(value)
      record.errors[field] << "Bitcoin address is invalid"
    end
  end

  private

  B58Chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  B58Base = B58Chars.length

  def self.valid_bitcoin_address?(address)
    (address =~ /^[a-zA-Z1-9]{33,35}$/) and version(address)
  end

  def self.version(address)
    decoded = b58_decode(address, 25)

    version = decoded[0, 1]
    checksum = decoded[-4, decoded.length]
    vh160 = decoded[0, decoded.length - 4]

    hashed = (Digest::SHA2.new << (Digest::SHA2.new << vh160).digest).digest

    hashed[0, 4] == checksum ? version[0] : nil
  end

  def self.b58_decode(value, length)
    long_value = 0
    index = 0
    result = ""

    value.reverse.each_char do |c|
      long_value += B58Chars.index(c) * (B58Base ** index)
      index += 1
    end

    while long_value >= 256 do
      div, mod = long_value.divmod 256
      result = mod.chr + result
      long_value = div
    end

    result = long_value.chr + result

    if result.length < length
      result = 0.chr * (length - result.length) + result
    end

    result
  end
end

通过单元测试(见http://github.com/davout/bitcoin-bank

并检查格式

class User < ActiveRecord::Base
  validates :bitcoin_address, format: { with: /\A(1|3)[a-zA-Z1-9]{26,33}\z/,
  message: "invalid bitcoin address" }
  # This is NOT  calculate checksum 
end