lib/bitcoin.rb in bitcoin-ruby-0.0.18 vs lib/bitcoin.rb in bitcoin-ruby-0.0.19

- old
+ new

@@ -5,18 +5,31 @@ require 'digest/rmd160' require 'openssl' require 'securerandom' module Bitcoin + # Determine the integer class to use. In older versions of ruby (< 2.4.0) the + # integer class is called Fixnum. In newer version (>= 2.4.0) Fixnum was + # deprecated in favor of a unification of Fixnum and BigInteger named Integer. + # Since this project strivers for backwards-compatability, we determine the + # appropriate class to use at initialization. + # + # This avoids annoying deprecation warnings on newer versions for ourselves + # and library consumers. + Integer = + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0') + Integer + else + Fixnum + end autoload :Bech32, 'bitcoin/bech32' autoload :Connection, 'bitcoin/connection' autoload :Protocol, 'bitcoin/protocol' autoload :P, 'bitcoin/protocol' autoload :Script, 'bitcoin/script' autoload :VERSION, 'bitcoin/version' - autoload :Logger, 'bitcoin/logger' autoload :Key, 'bitcoin/key' autoload :ExtKey, 'bitcoin/ext_key' autoload :ExtPubkey, 'bitcoin/ext_key' autoload :Builder, 'bitcoin/builder' autoload :BloomFilter,'bitcoin/bloom_filter' @@ -32,10 +45,11 @@ module Util def address_version; Bitcoin.network[:address_version]; end + def version_bytes; address_version.size / 2; end def p2sh_version; Bitcoin.network[:p2sh_version]; end # hash160 is a 20 bytes (160bits) rmd610-sha256 hexdigest. def hash160(hex) bytes = [hex].pack("H*") @@ -50,11 +64,11 @@ # verify base58 checksum for given +base58+ data. def base58_checksum?(base58) hex = decode_base58(base58) rescue nil return false unless hex - checksum( hex[0...42] ) == hex[-8..-1] + checksum(hex[0...(version_bytes + 20) * 2]) == hex[-8..-1] end alias :address_checksum? :base58_checksum? # check if given +address+ is valid. # this means having a correct version byte, length and checksum. @@ -66,20 +80,27 @@ def valid_pubkey?(pubkey) ::OpenSSL::PKey::EC::Point.from_hex(bitcoin_elliptic_curve.group, pubkey) true rescue OpenSSL::PKey::EC::Point::Error false + rescue OpenSSL::BNError + # Occasionally, a malformed value will fail hex decoding completely and + # instead of raising an `OpenSSL::PKey::EC::Point::Error` will raise this + # error. We capture this failure mode here as well. + false end # get hash160 for given +address+. returns nil if address is invalid. def hash160_from_address(address) case address_type(address) when :witness_v0_keyhash _, witness_program_hex = decode_segwit_address(address) witness_program_hex when :hash160, :p2sh - decode_base58(address)[2...42] + start_idx = version_bytes * 2 + stop_idx = start_idx + 40 # 20 bytes (2 chars per byte) + decode_base58(address)[start_idx...stop_idx] end end # get type of given +address+. def address_type(address) @@ -96,18 +117,20 @@ return :witness_v0_scripthash end end hex = decode_base58(address) rescue nil - if hex && hex.bytesize == 50 && address_checksum?(address) + + target_size = (version_bytes + 20 + 4) * 2 # version_bytes + 20 bytes hash + 4 bytes checksum + if hex && hex.bytesize == target_size && address_checksum?(address) # Litecoin updates the P2SH version byte, and this method should recognize both. p2sh_versions = [p2sh_version] if Bitcoin.network[:legacy_p2sh_versions] p2sh_versions += Bitcoin.network[:legacy_p2sh_versions] end - case hex[0...2] + case hex[0...(version_bytes * 2)] when address_version return :hash160 when *p2sh_versions return :p2sh end @@ -253,11 +276,11 @@ nbits end def decode_target(target_bits) case target_bits - when Fixnum + when Bitcoin::Integer [ decode_compact_bits(target_bits).to_i(16), target_bits ] when String [ target_bits.to_i(16), encode_compact_bits(target_bits) ] end end @@ -559,15 +582,15 @@ # Store the copy of network options so we can modify them in tests without breaking the defaults @network_options ||= NETWORKS[@network].dup end def self.network_name - @network + @network ||= nil end def self.network_project - @network_project + @network_project ||= nil end def self.network=(name) raise "Network descriptor '#{name}' not found." unless NETWORKS[name.to_sym] @network_options = nil # clear cached parameters @@ -651,11 +674,10 @@ "bitseed.xf2.org", "dnsseed.webbtc.com", ], genesis_hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", proof_of_work_limit: 0x1d00ffff, - alert_pubkeys: ["04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284"], known_nodes: [ 'relay.eligius.st', 'mining.bitcoin.cz', 'blockchain.info', 'blockexplorer.com', @@ -690,11 +712,10 @@ default_port: 18333, bip34_height: 21111, dns_seeds: [ ], genesis_hash: "00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008", proof_of_work_limit: 0x1d07fff8, - alert_pubkeys: ["04302390343f91cc401d56d68b123028bf52e5fca1939df127f63c6467cdf9c8e2c14b61104cf817d0b780da337893ecc4aaff1309e536162dabbdb45200ca2b0a"], known_nodes: [], checkpoints: {}, }) NETWORKS[:regtest] = NETWORKS[:testnet].merge({ @@ -755,11 +776,10 @@ "dnsseed.thrasher.io", "dnsseed.koin-project.com", ], genesis_hash: "12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2", proof_of_work_limit: 0x1e0fffff, - alert_pubkeys: ["040184710fa689ad5023690c80f3a49c8f13f8d45b8c857fbcbc8bc4a8e4d3eb4b10f4d4604fa08dce601aaf0f470216fe1b51850b4acf21b179c45070ac7b03a9"], known_nodes: [], checkpoints: { 1 => "80ca095ed10b02e53d769eb6eaf92cd04e9e0759e5be4a8477b42911ba49c78f", 2 => "13957807cdd1d02f993909fa59510e318763f99a506c4c426e3b254af09f40d7", 1500 => "841a2965955dd288cfa707a755d05a54e45f8bd476835ec9af4402a2b59a2967", @@ -795,11 +815,10 @@ "testnet-seed.ltc.xurious.com", "seed-b.litecoin.loshan.co.uk", "dnsseed-testnet.thrasher.io", ], genesis_hash: "4966625a4b2851d9fdee139e56211a0d88575f59ed816ff5e6a63deb4e3e29a0", - alert_pubkeys: ["04302390343f91cc401d56d68b123028bf52e5fca1939df127f63c6467cdf9c8e2c14b61104cf817d0b780da337893ecc4aaff1309e536162dabbdb45200ca2b0a"], known_nodes: [], checkpoints: { 546 => "bf434a4c665307f52a041ee40faa7bf56284c5f3b5d11bf6182aba537961f86c", } }) @@ -834,11 +853,10 @@ "seed.dogechain.info", "seed.dogecoin.com", ], genesis_hash: "1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691", proof_of_work_limit: 0x1e0fffff, - alert_pubkeys: [], known_nodes: [ "daemons.chain.so", "bootstrap.chain.so", ], checkpoints: { @@ -892,10 +910,9 @@ dns_seeds: [ "testdoge-seed.lionservers.de", ], genesis_hash: "bb0a78264637406b6360aad926284d544d7049f45189db5664f3c4d07350559e", proof_of_work_limit: 0x1e0fffff, - alert_pubkeys: [], known_nodes: [ "localhost", "testnets.chain.so", ], checkpoints: {