Class: Tapyrus::Message::Headers

Inherits:
Base
  • Object
show all
Defined in:
lib/tapyrus/message/headers.rb

Overview

Constant Summary collapse

COMMAND =
"headers"

Constants included from Util

Util::DIGEST_NAME_SHA256

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_pkt

Methods included from Util

#byte_to_bit, #calc_checksum, #decode_base58_address, #double_sha256, #encode_base58_address, #hash160, #hmac_sha256, #pack_boolean, #pack_var_int, #pack_var_string, #padding_zero, #sha256, #unpack_boolean, #unpack_var_int, #unpack_var_int_from_io, #unpack_var_string, #valid_address?

Methods included from HexConverter

#to_hex

Constructor Details

#initialize(headers = []) ⇒ Headers

Returns a new instance of Headers.



11
12
13
# File 'lib/tapyrus/message/headers.rb', line 11

def initialize(headers = [])
  @headers = headers
end

Instance Attribute Details

#headersObject



9
10
11
# File 'lib/tapyrus/message/headers.rb', line 9

def headers
  @headers
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/tapyrus/message/headers.rb', line 15

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  header_count = Tapyrus.unpack_var_int_from_io(buf)
  h = new
  header_count.times do
    h.headers << Tapyrus::BlockHeader.parse_from_payload(buf)
    buf.read(1) # read tx count 0x00 (headers message doesn't include any tx.)
  end
  h
end

Instance Method Details

#to_payloadObject



26
27
28
# File 'lib/tapyrus/message/headers.rb', line 26

def to_payload
  Tapyrus.pack_var_int(headers.size) << headers.map { |h| h.to_payload << 0x00 }.join
end