Sha256: 8c3ae312b6934abf875ceb6b8b6f53e82be1d087cff27fb3280d7890a79cc1b7

Contents?: true

Size: 1.44 KB

Versions: 16

Compression:

Stored size: 1.44 KB

Contents

module Bitcoin
  module Message
    # inventory class. inventory is a part of message.
    # https://bitcoin.org/en/developer-reference#term-inventory
    class Inventory

      SEGWIT_FLAG = 1 << 30

      MSG_TX = 1
      MSG_BLOCK = 2
      MSG_FILTERED_BLOCK = 3
      MSG_CMPCT_BLOCK = 4
      MSG_WITNESS_TX = SEGWIT_FLAG | MSG_TX
      MSG_WITNESS_BLOCK = SEGWIT_FLAG | MSG_BLOCK
      MSG_FILTERED_WITNESS_BLOCK = SEGWIT_FLAG | MSG_FILTERED_BLOCK

      attr_accessor :identifier
      attr_accessor :hash

      def initialize(identifier, hash)
        raise Error, "invalid type identifier specified. identifier = #{identifier}" unless valid_identifier?(identifier)
        @identifier = identifier
        @hash = hash
      end

      # parse inventory payload
      def self.parse_from_payload(payload)
        raise Error, 'invalid inventory size.' if payload.bytesize != 36
        identifier = payload[0..4].unpack('V').first
        hash = payload[4..-1].bth # internal byte order
        new(identifier, hash)
      end

      def to_payload
        [identifier].pack('V') << hash.htb
      end

      def block?
        [MSG_BLOCK, MSG_WITNESS_BLOCK, MSG_FILTERED_WITNESS_BLOCK].include?(identifier)
      end

      private

      def valid_identifier?(identifier)
        [MSG_TX, MSG_BLOCK, MSG_FILTERED_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_TX,
         MSG_WITNESS_BLOCK, MSG_FILTERED_WITNESS_BLOCK].include?(identifier)
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
bitcoinrb-0.5.0 lib/bitcoin/message/inventory.rb
bitcoinrb-0.4.0 lib/bitcoin/message/inventory.rb
bitcoinrb-0.3.2 lib/bitcoin/message/inventory.rb
bitcoinrb-0.3.1 lib/bitcoin/message/inventory.rb
bitcoinrb-0.3.0 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.9 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.8 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.7 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.6 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.5 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.4 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.2 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.1 lib/bitcoin/message/inventory.rb
bitcoinrb-0.2.0 lib/bitcoin/message/inventory.rb
bitcoinrb-0.1.9 lib/bitcoin/message/inventory.rb
bitcoinrb-0.1.8 lib/bitcoin/message/inventory.rb