Sha256: bcd7ef46ee3406251bdba25cd72cad1097e07a5d5d68b3a226ca20a17286627a

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Bitcoin
  module Message

    # block message
    # https://bitcoin.org/en/developer-reference#block
    class Block < Base

      attr_accessor :header
      attr_accessor :transactions
      attr_accessor :use_segwit

      COMMAND = 'block'

      def initialize(header, transactions = [], use_segwit = false)
        @header = header
        @transactions = transactions
        @use_segwit = use_segwit
      end

      def self.parse_from_payload(payload)
        buf = StringIO.new(payload)
        header = Bitcoin::BlockHeader.parse_from_payload(buf.read(80))
        transactions = []
        unless buf.eof?
          tx_count = Bitcoin.unpack_var_int_from_io(buf)
          tx_count.times do
            transactions << Bitcoin::Tx.parse_from_payload(buf)
          end
        end
        new(header, transactions)
      end

      def to_payload
        header.to_payload << Bitcoin.pack_var_int(transactions.size) <<
          transactions.map{|t|use_segwit ? t.to_payload : t.serialize_old_format}.join
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bitcoinrb-0.1.1 lib/bitcoin/message/block.rb
bitcoinrb-0.0.1 lib/bitcoin/message/block.rb