text: # Copyright (c) 2016-2017, Neil Booth
  # Copyright (c) 2017, the ElectrumX authors
  #
  # All rights reserved.
  #
  # The MIT License (MIT)
  #
  # Permission is hereby granted, free of charge, to any person obtaining
  # a copy of this software and associated documentation files (the
  # "Software"), to deal in the Software without restriction, including
  # without limitation the rights to use, copy, modify, merge, publish,
  # distribute, sublicense, and/or sell copies of the Software, and to
  # permit persons to whom the Software is furnished to do so, subject to
  # the following conditions:
  #
  # The above copyright notice and this permission notice shall be
  # included in all copies or substantial portions of the Software.
  #
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  # and warranty status of this software.
  
  '''Transaction-related classes and functions.'''
  
  from collections import namedtuple
  from hashlib import blake2s
  
  from electrumx.lib.hash import sha256, double_sha256, hash_to_hex_str
  from electrumx.lib.script import OpCodes
  from electrumx.lib.util import (
      unpack_le_int32_from, unpack_le_int64_from, unpack_le_uint16_from,
      unpack_be_uint16_from,
      unpack_le_uint32_from, unpack_le_uint64_from, pack_le_int32, pack_varint,
      pack_le_uint32, pack_le_int64, pack_varbytes,
  )
  
  ZERO = bytes(32)
  MINUS_1 = 4294967295
  
  
  class Tx(namedtuple("Tx", "version inputs outputs locktime")):
      '''Class representing a transaction.'''
  
      def serialize(self):
          return b''.join((
              pack_le_int32(self.version),
              pack_varint(len(self.inputs)),
              b''.join(tx_in.serialize() for tx_in in self.inputs),
              pack_varint(len(self.outputs)),
              b''.join(tx_out.serialize() for tx_out in self.outputs),
              pack_le_uint32(self.locktime)
          ))
  
  
  class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")):
      '''Class representing a transaction input.'''
      def __str__(self):
          script = self.script.hex()
          prev_hash = hash_to_hex_str(self.prev_hash)
          return ("Input({}, {:d}, script={}, sequence={:d})"
                  .format(prev_hash, self.prev_idx, script, self.sequence))
  
      def is_generation(self):
          '''Test if an input is generation/coinbase like'''
          return self.prev_idx == MINUS_1 and self.prev_hash == ZERO
  
      def serialize(self):
          return b''.join((
              self.prev_hash,
              pack_le_uint32(self.prev_idx),
              pack_varbytes(self.script),
              pack_le_uint32(self.sequence),
          ))
  
  
  class TxOutput(namedtuple("TxOutput", "value pk_script")):
  
      def serialize(self):
          return b''.join((
              pack_le_int64(self.value),
              pack_varbytes(self.pk_script),
          ))
  
  
  class Deserializer(object):
      '''Deserializes blocks into transactions.
  
      External entry points are read_tx(), read_tx_and_hash(),
      read_tx_and_vsize() and read_block().
  
      This code is performance sensitive as it is executed 100s of
      millions of times during sync.
      '''
  
      TX_HASH_FN = staticmethod(double_sha256)
  
      def __init__(self, binary, start=0):
          assert isinstance(binary, bytes)
          self.binary = binary
          self.binary_length = len(binary)
          self.cursor = start
  
      def read_tx(self):
          '''Return a deserialized transaction.'''
          return Tx(
              self._read_le_int32(),  # version
              self._read_inputs(),    # inputs
              self._read_outputs(),   # outputs
              self._read_le_uint32()  # locktime
          )
  
      def read_tx_and_hash(self):
          '''Return a (deserialized TX, tx_hash) pair.
  
          The hash needs to be reversed for human display; for efficiency
          we process it in the natural serialized order.
          '''
          start = self.cursor
          return self.read_tx(), self.TX_HASH_FN(self.binary[start:self.cursor])
  
      def read_tx_and_vsize(self):
          '''Return a (deserialized TX, vsize) pair.'''
          return self.read_tx(), self.binary_length
  
      def read_tx_block(self):
          '''Returns a list of (deserialized_tx, tx_hash) pairs.'''
          read = self.read_tx_and_hash
          # Some coins have excess data beyond the end of the transactions
          return [read() for _ in range(self._read_varint())]
  
      def _read_inputs(self):
          read_input = self._read_input
          return [read_input() for i in range(self._read_varint())]
  
      def _read_input(self):
          return TxInput(
              self._read_nbytes(32),   # prev_hash
              self._read_le_uint32(),  # prev_idx
              self._read_varbytes(),   # script
              self._read_le_uint32()   # sequence
          )
  
      def _read_outputs(self):
          read_output = self._read_output
          return [read_output() for i in range(self._read_varint())]
  
      def _read_output(self):
          return TxOutput(
              self._read_le_int64(),  # value
              self._read_varbytes(),  # pk_script
          )
  
      def _read_byte(self):
          cursor = self.cursor
          self.cursor += 1
          return self.binary[cursor]
  
      def _read_nbytes(self, n):
          cursor = self.cursor
          self.cursor = end = cursor + n
          assert self.binary_length >= end
          return self.binary[cursor:end]
  
      def _read_varbytes(self):
          return self._read_nbytes(self._read_varint())
  
      def _read_varint(self):
          n = self.binary[self.cursor]
          self.cursor += 1
          if n < 253:
              return n
          if n == 253:
              return self._read_le_uint16()
          if n == 254:
              return self._read_le_uint32()
          return self._read_le_uint64()
  
      def _read_le_int32(self):
          result, = unpack_le_int32_from(self.binary, self.cursor)
          self.cursor += 4
          return result
  
      def _read_le_int64(self):
          result, = unpack_le_int64_from(self.binary, self.cursor)
          self.cursor += 8
          return result
  
      def _read_le_uint16(self):
          result, = unpack_le_uint16_from(self.binary, self.cursor)
          self.cursor += 2
          return result
  
      def _read_be_uint16(self):
          result, = unpack_be_uint16_from(self.binary, self.cursor)
          self.cursor += 2
          return result
  
      def _read_le_uint32(self):
          result, = unpack_le_uint32_from(self.binary, self.cursor)
          self.cursor += 4
          return result
  
      def _read_le_uint64(self):
          result, = unpack_le_uint64_from(self.binary, self.cursor)
          self.cursor += 8
          return result
  
  
  class TxSegWit(namedtuple("Tx", "version marker flag inputs outputs "
                            "witness locktime")):
      '''Class representing a SegWit transaction.'''
  
  
  class DeserializerSegWit(Deserializer):
  
      # https://bitcoincore.org/en/segwit_wallet_dev/#transaction-serialization
  
      def _read_witness(self, fields):
          read_witness_field = self._read_witness_field
          return [read_witness_field() for i in range(fields)]
  
      def _read_witness_field(self):
          read_varbytes = self._read_varbytes
          return [read_varbytes() for i in range(self._read_varint())]
  
      def _read_tx_parts(self):
          '''Return a (deserialized TX, tx_hash, vsize) tuple.'''
          start = self.cursor
          marker = self.binary[self.cursor + 4]
          if marker:
              tx = super().read_tx()
              tx_hash = self.TX_HASH_FN(self.binary[start:self.cursor])
              return tx, tx_hash, self.binary_length
  
          # Ugh, this is nasty.
          version = self._read_le_int32()
          orig_ser = self.binary[start:self.cursor]
  
          marker = self._read_byte()
          flag = self._read_byte()
  
          start = self.cursor
          inputs = self._read_inputs()
          outputs = self._read_outputs()
          orig_ser += self.binary[start:self.cursor]
  
          base_size = self.cursor - start
          witness = self._read_witness(len(inputs))
  
          start = self.cursor
          locktime = self._read_le_uint32()
          orig_ser += self.binary[start:self.cursor]
          vsize = (3 * base_size + self.binary_length) // 4
  
          return TxSegWit(version, marker, flag, inputs, outputs, witness,
                          locktime), self.TX_HASH_FN(orig_ser), vsize
  
      def read_tx(self):
          return self._read_tx_parts()[0]
  
      def read_tx_and_hash(self):
          tx, tx_hash, _vsize = self._read_tx_parts()
          return tx, tx_hash
  
      def read_tx_and_vsize(self):
          tx, _tx_hash, vsize = self._read_tx_parts()
          return tx, vsize
  
  class DeserializerQtum(DeserializerSegWit):
  
      def read_varint(self):
          '''
          set _read_varint to public
          :return: int
          '''
          return self._read_varint()
  