Module: Bitcoin::Util
- Included in:
- Bitcoin, Message::Base, Message::Base, Wallet::MasterKey, Wallet::MasterKey
- Defined in:
- lib/bitcoin/util.rb
Overview
bitcoin utility. following methods can be used as follows.
Bitcoin.pack_var_int(5)
Constant Summary collapse
- DIGEST_NAME_SHA256 =
'sha256'
Instance Method Summary collapse
-
#byte_to_bit(byte) ⇒ Object
byte convert to the sequence of bits packed eight in a byte with the least significant bit first.
- #calc_checksum(hex) ⇒ Object
-
#decode_base58_address(addr) ⇒ Array
decode Base58 check encoding address.
- #double_sha256(payload) ⇒ Object
-
#encode_base58_address(hex, addr_version) ⇒ String
encode Base58 check address.
-
#hash160(hex) ⇒ Object
generate sha256-ripemd160 hash for value.
- #hmac_sha256(key, data) ⇒ Object
- #pack_boolean(b) ⇒ Object
- #pack_var_int(i) ⇒ Object
- #pack_var_string(payload) ⇒ Object
- #sha256(payload) ⇒ Object
- #unpack_boolean(payload) ⇒ Object
-
#unpack_var_int(payload) ⇒ Object
An integer for a valid payload, otherwise nil.
-
#unpack_var_int_from_io(buf) ⇒ Object
An integer for a valid payload, otherwise nil.
- #unpack_var_string(payload) ⇒ Object
Instance Method Details
#byte_to_bit(byte) ⇒ Object
byte convert to the sequence of bits packed eight in a byte with the least significant bit first.
81 82 83 |
# File 'lib/bitcoin/util.rb', line 81 def byte_to_bit(byte) byte.unpack('b*').first end |
#calc_checksum(hex) ⇒ Object
112 113 114 |
# File 'lib/bitcoin/util.rb', line 112 def calc_checksum(hex) double_sha256(hex.htb).bth[0..7] end |
#decode_base58_address(addr) ⇒ Array
decode Base58 check encoding address.
102 103 104 105 106 107 108 109 110 |
# File 'lib/bitcoin/util.rb', line 102 def decode_base58_address(addr) hex = Base58.decode(addr) if hex.size == 50 && calc_checksum(hex[0...-8]) == hex[-8..-1] raise 'Invalid version bytes.' unless [Bitcoin.chain_params.address_version, Bitcoin.chain_params.p2sh_version].include?(hex[0..1]) [hex[2...-8], hex[0..1]] else raise 'Invalid address.' end end |
#double_sha256(payload) ⇒ Object
76 77 78 |
# File 'lib/bitcoin/util.rb', line 76 def double_sha256(payload) sha256(sha256(payload)) end |
#encode_base58_address(hex, addr_version) ⇒ String
encode Base58 check address.
94 95 96 97 |
# File 'lib/bitcoin/util.rb', line 94 def encode_base58_address(hex, addr_version) base = addr_version + hex Base58.encode(base + calc_checksum(base)) end |
#hash160(hex) ⇒ Object
generate sha256-ripemd160 hash for value
86 87 88 |
# File 'lib/bitcoin/util.rb', line 86 def hash160(hex) Digest::RMD160.hexdigest(Digest::SHA256.digest(hex.htb)) end |
#hmac_sha256(key, data) ⇒ Object
118 119 120 |
# File 'lib/bitcoin/util.rb', line 118 def hmac_sha256(key, data) OpenSSL::HMAC.digest(DIGEST_NAME_SHA256, key, data) end |
#pack_boolean(b) ⇒ Object
63 64 65 |
# File 'lib/bitcoin/util.rb', line 63 def pack_boolean(b) b ? [0xFF].pack('C') : [0x00].pack('C') end |
#pack_var_int(i) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/bitcoin/util.rb', line 20 def pack_var_int(i) if i < 0xfd [i].pack('C') elsif i <= 0xffff [0xfd, i].pack('Cv') elsif i <= 0xffffffff [0xfe, i].pack('CV') elsif i <= 0xffffffffffffffff [0xff, i].pack('CQ') else raise "int(#{i}) too large!" end end |
#pack_var_string(payload) ⇒ Object
11 12 13 |
# File 'lib/bitcoin/util.rb', line 11 def pack_var_string(payload) pack_var_int(payload.bytesize) + payload end |
#sha256(payload) ⇒ Object
72 73 74 |
# File 'lib/bitcoin/util.rb', line 72 def sha256(payload) Digest::SHA256.digest(payload) end |
#unpack_boolean(payload) ⇒ Object
67 68 69 70 |
# File 'lib/bitcoin/util.rb', line 67 def unpack_boolean(payload) data, payload = payload.unpack('Ca*') [(data.zero? ? false : true), payload] end |
#unpack_var_int(payload) ⇒ Object
Returns an integer for a valid payload, otherwise nil.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bitcoin/util.rb', line 35 def unpack_var_int(payload) case payload.unpack('C').first when 0xfd payload.unpack('xva*') when 0xfe payload.unpack('xVa*') when 0xff payload.unpack('xQa*') else payload.unpack('Ca*') end end |
#unpack_var_int_from_io(buf) ⇒ Object
Returns an integer for a valid payload, otherwise nil.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bitcoin/util.rb', line 49 def unpack_var_int_from_io(buf) uchar = buf.read(1)&.unpack('C')&.first case uchar when 0xfd buf.read(2)&.unpack('v')&.first when 0xfe buf.read(4)&.unpack('V')&.first when 0xff buf.read(8)&.unpack('Q')&.first else uchar end end |
#unpack_var_string(payload) ⇒ Object
15 16 17 18 |
# File 'lib/bitcoin/util.rb', line 15 def unpack_var_string(payload) size, payload = unpack_var_int(payload) size > 0 ? payload.unpack("a#{size}a*") : [nil, payload] end |