module RubyBitcoinWallet

Constants

CENT
COIN
NETWORKS
VERSION

Public Instance Methods

address_version() click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 130
def address_version; NETWORKS[:bitcoin][:address_version]; end
bitcoin_elliptic_curve() click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 68
def bitcoin_elliptic_curve
  ::OpenSSL::PKey::EC.new("secp256k1")
end
checksum(hex) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 103
def checksum(hex)
  b = [hex].pack("H*") # unpack hex
  Digest::SHA256.hexdigest( Digest::SHA256.digest(b) )[0...8]
end
encode_address(hex, version) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 89
def encode_address(hex, version)
  hex = version + hex
  encode_base58(hex + checksum(hex))
end
encode_base58(hex) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 98
def encode_base58(hex)
  leading_zero_bytes  = (hex.match(/^([0]+)/) ? $1 : '').size / 2
  ("1"*leading_zero_bytes) + Util.int_to_base58( hex.to_i(16) )
end
generate_key() click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 72
def generate_key
  key = bitcoin_elliptic_curve.generate_key
  inspect_key( key )
end
hash160(hex) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 108
def hash160(hex)
  bytes = [hex].pack("H*")
  Digest::RMD160.hexdigest Digest::SHA256.digest(bytes)
end
hash160_to_address(hex) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 85
def hash160_to_address(hex)
  encode_address hex, address_version
end
inspect_key(key) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 77
def inspect_key(key)
  [ key.private_key_hex, key.public_key_hex ]
end
network() click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 94
def network
  @network_options ||= NETWORKS[:bitcoin].dup
end
network=(name) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 113
def network=(name)
  raise "Network descriptor '#{name}' not found."  unless NETWORKS[name.to_sym]
  @network_options = nil # clear cached parameters
  @network = name.to_sym
  @network_project = network[:project] rescue nil
  Dogecoin.load  if dogecoin? || dogecoin_testnet?
  Namecoin.load  if namecoin? && defined?(Namecoin)
  @network
end
p2sh_version() click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 131
def p2sh_version; NETWORKS[:bitcoin][:p2sh_version]; end
pubkey_to_address(pubkey, type = :pubkey_hash) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 81
def pubkey_to_address(pubkey, type = :pubkey_hash)
  hash160_to_address( hash160(pubkey) )
end
valid_address?(address) click to toggle source
# File lib/ruby_bitcoin_wallet.rb, line 123
def valid_address?(address)
  hex = Util.decode_base58(address) rescue nil
  return false unless hex && hex.bytesize == 50
  return false unless [address_version, p2sh_version].include?(hex[0...2])
  Util.base58_checksum?(address)
end