Exemplo n.º 1
0
    def stream(self, f, blank_solutions=False, include_unspents=False, include_witness_data=True):
        """Stream a Bitcoin transaction Tx to the file-like object f.

        :param f: writable file-like object to stream binary data of transaction
        :param blank_solutions: (optional) clear out the solutions scripts, effectively "unsigning" the
            transaction before writing it. Defaults to False
        :param include_unspents: (optional) stread out the Spendable objects after streaming the transaction.
            This is a pycoin-specific extension. Defaults to False.
        :param include_witness_data: (optional) stream segwit transactions including the witness data if the
            transaction has any witness data. Defaults to True.
        """
        include_witnesses = include_witness_data and self.has_witness_data()
        stream_struct("L", f, self.version)
        if include_witnesses:
            f.write(b'\0\1')
        stream_struct("I", f, len(self.txs_in))
        for t in self.txs_in:
            t.stream(f, blank_solutions=blank_solutions)
        stream_struct("I", f, len(self.txs_out))
        for t in self.txs_out:
            t.stream(f)
        if include_witnesses:
            for tx_in in self.txs_in:
                witness = tx_in.witness
                stream_struct("I", f, len(witness))
                for w in witness:
                    stream_satoshi_string(f, w)
        stream_struct("L", f, self.lock_time)
        if include_unspents and not self.missing_unspents():
            self.stream_unspents(f)
Exemplo n.º 2
0
 def stream_header(self, f):
     """Stream the block header in the standard way to the file-like object f."""
     stream_struct("L##L", f, self.version, self.previous_block_hash,
                   self.merkle_root, self.height)
     f.write(b'\0' * 28)
     stream_struct("LL#S", f, self.timestamp, self.difficulty, self.nonce,
                   self.solution)
Exemplo n.º 3
0
    def stream(self,
               f,
               blank_solutions=False,
               include_unspents=False,
               include_witness_data=True):
        """Stream a Bitcoin transaction Tx to the file-like object f.

        :param f: writable file-like object to stream binary data of transaction
        :param blank_solutions: (optional) clear out the solutions scripts, effectively "unsigning" the
            transaction before writing it. Defaults to False
        :param include_unspents: (optional) stread out the Spendable objects after streaming the transaction.
            This is a pycoin-specific extension. Defaults to False.
        :param include_witness_data: (optional) stream segwit transactions including the witness data if the
            transaction has any witness data. Defaults to True.
        """
        include_witnesses = include_witness_data and self.has_witness_data()
        stream_struct("L", f, self.version)
        if include_witnesses:
            f.write(b'\0\1')
        stream_struct("I", f, len(self.txs_in))
        for t in self.txs_in:
            t.stream(f, blank_solutions=blank_solutions)
        stream_struct("I", f, len(self.txs_out))
        for t in self.txs_out:
            t.stream(f)
        if include_witnesses:
            for tx_in in self.txs_in:
                witness = tx_in.witness
                stream_struct("I", f, len(witness))
                for w in witness:
                    stream_satoshi_string(f, w)
        stream_struct("L", f, self.lock_time)
        if include_unspents and not self.missing_unspents():
            self.stream_unspents(f)
Exemplo n.º 4
0
 def _hash_prevouts(self, hash_type):
     if hash_type & SIGHASH_ANYONECANPAY:
         return ZERO32
     f = io.BytesIO()
     for tx_in in self.tx.txs_in:
         f.write(tx_in.previous_hash)
         stream_struct("L", f, tx_in.previous_index)
     return sha256(f.getvalue())
Exemplo n.º 5
0
 def _hash_prevouts(self, hash_type):
     if hash_type & SIGHASH_ANYONECANPAY:
         return ZERO32
     f = io.BytesIO()
     for tx_in in self.tx.txs_in:
         f.write(tx_in.previous_hash)
         stream_struct("L", f, tx_in.previous_index)
     return sha256(f.getvalue())
Exemplo n.º 6
0
    def _hash_sequence(self, hash_type):
        if ((hash_type & SIGHASH_ANYONECANPAY)
                or ((hash_type & 0x1f) == SIGHASH_SINGLE)
                or ((hash_type & 0x1f) == SIGHASH_NONE)):
            return ZERO32

        f = io.BytesIO()
        for tx_in in self.tx.txs_in:
            stream_struct("L", f, tx_in.sequence)
        return sha256(f.getvalue())
Exemplo n.º 7
0
 def _hash_outputs(self, hash_type, tx_in_idx):
     txs_out = self.tx.txs_out
     if hash_type & 0x1f == SIGHASH_SINGLE:
         if tx_in_idx >= len(txs_out):
             return ZERO32
         txs_out = txs_out[tx_in_idx:tx_in_idx+1]
     elif hash_type & 0x1f == SIGHASH_NONE:
         return ZERO32
     f = io.BytesIO()
     for tx_out in txs_out:
         stream_struct("QS", f, tx_out.coin_value, tx_out.script)
     return sha256(f.getvalue())
Exemplo n.º 8
0
    def _hash_sequence(self, hash_type):
        if (
                (hash_type & SIGHASH_ANYONECANPAY) or
                ((hash_type & 0x1f) == SIGHASH_SINGLE) or
                ((hash_type & 0x1f) == SIGHASH_NONE)
        ):
            return ZERO32

        f = io.BytesIO()
        for tx_in in self.tx.txs_in:
            stream_struct("L", f, tx_in.sequence)
        return sha256(f.getvalue())
Exemplo n.º 9
0
    def hash(self, hash_type=None):
        """Return the binary hash for this :class:`Tx` object.

        :param hash_type: (optional) if set, generates a hash specific to a particular type of signature.

        :return: 32 byte long binary blob corresponding to the hash
        """
        s = io.BytesIO()
        self.stream(s, include_witness_data=False)
        if hash_type is not None:
            stream_struct("L", s, hash_type)
        return double_sha256(s.getvalue())
Exemplo n.º 10
0
 def _hash_outputs(self, hash_type, tx_in_idx):
     txs_out = self.tx.txs_out
     if hash_type & 0x1f == SIGHASH_SINGLE:
         if tx_in_idx >= len(txs_out):
             return ZERO32
         txs_out = txs_out[tx_in_idx:tx_in_idx + 1]
     elif hash_type & 0x1f == SIGHASH_NONE:
         return ZERO32
     f = io.BytesIO()
     for tx_out in txs_out:
         stream_struct("QS", f, tx_out.coin_value, tx_out.script)
     return sha256(f.getvalue())
Exemplo n.º 11
0
    def hash(self, hash_type=None):
        """Return the binary hash for this :class:`Tx` object.

        :param hash_type: (optional) if set, generates a hash specific to a particular type of signature.

        :return: 32 byte long binary blob corresponding to the hash
        """
        s = io.BytesIO()
        self.stream(s, include_witness_data=False)
        if hash_type is not None:
            stream_struct("L", s, hash_type)
        return double_sha256(s.getvalue())
Exemplo n.º 12
0
 def stream_header_legacy(self, f):
     """Stream the block header in the standard way to the file-like object f."""
     stream_struct("L##LL", f, self.version, self.previous_block_hash,
                   self.merkle_root, self.timestamp, self.difficulty)
     f.write(self.nonce[:4])
Exemplo n.º 13
0
 def stream(self, f, blank_solutions=False):
     script = b'' if blank_solutions else self.script
     stream_struct("#LSL", f, self.previous_hash, self.previous_index, script, self.sequence)
Exemplo n.º 14
0
 def stream(self, f):
     stream_struct("QS", f, self.coin_value, self.script)
Exemplo n.º 15
0
 def stream(self, f, blank_solutions=False):
     script = b'' if blank_solutions else self.script
     stream_struct("#LSL", f, self.previous_hash, self.previous_index,
                   script, self.sequence)
Exemplo n.º 16
0
 def stream(self, f):
     stream_struct("QS", f, self.coin_value, self.script)
Exemplo n.º 17
0
 def stream_header_legacy(self, f):
     """Stream the block header in the standard way to the file-like object f."""
     stream_struct("L##LL", f, self.version, self.previous_block_hash,
                   self.merkle_root, self.timestamp, self.difficulty)
     f.write(self.nonce[:4])
Exemplo n.º 18
0
 def stream(self, f, as_spendable=False):
     super(Spendable, self).stream(f)
     if as_spendable:
         stream_struct("#LIbI", f, self.previous_hash,
                       self.previous_index, self.block_index_available,
                       bool(self.does_seem_spent), self.block_index_spent)
Exemplo n.º 19
0
 def _segwit_signature_preimage(self, script, tx_in_idx, hash_type):
     f = io.BytesIO()
     stream_struct("L", f, self.tx.version)
     # calculate hash prevouts
     f.write(self._hash_prevouts(hash_type))
     f.write(self._hash_sequence(hash_type))
     tx_in = self.tx.txs_in[tx_in_idx]
     f.write(tx_in.previous_hash)
     stream_struct("L", f, tx_in.previous_index)
     tx_out = self.tx.unspents[tx_in_idx]
     stream_satoshi_string(f, script)
     stream_struct("Q", f, tx_out.coin_value)
     stream_struct("L", f, tx_in.sequence)
     f.write(self._hash_outputs(hash_type, tx_in_idx))
     stream_struct("L", f, self.tx.lock_time)
     stream_struct("L", f, hash_type)
     return f.getvalue()
Exemplo n.º 20
0
 def stream(self, f, as_spendable=False):
     super(Spendable, self).stream(f)
     if as_spendable:
         stream_struct("#LIbI", f, self.previous_hash, self.previous_index,
                       self.block_index_available, bool(self.does_seem_spent), self.block_index_spent)
Exemplo n.º 21
0
 def hash(self, hash_type=None):
     s = io.BytesIO()
     self.stream(s, include_witness_data=False)
     if hash_type is not None:
         stream_struct("L", s, hash_type)
     return sha256(s.getvalue())
Exemplo n.º 22
0
 def stream_header(self, f):
     """Stream the block header in the standard way to the file-like object f."""
     stream_struct("L##L", f, self.version, self.previous_block_hash,
                   self.merkle_root, self.height)
     f.write(b'\0' * 28)
     stream_struct("LL#S", f, self.timestamp, self.difficulty, self.nonce, self.solution)
Exemplo n.º 23
0
 def stream(self, f):
     stream_struct("L#", f, self.item_type, self.data)
Exemplo n.º 24
0
 def _segwit_signature_preimage(self, script, tx_in_idx, hash_type):
     f = io.BytesIO()
     stream_struct("L", f, self.tx.version)
     # calculate hash prevouts
     f.write(self._hash_prevouts(hash_type))
     f.write(self._hash_sequence(hash_type))
     tx_in = self.tx.txs_in[tx_in_idx]
     f.write(tx_in.previous_hash)
     stream_struct("L", f, tx_in.previous_index)
     tx_out = self.tx.unspents[tx_in_idx]
     stream_satoshi_string(f, script)
     stream_struct("Q", f, tx_out.coin_value)
     stream_struct("L", f, tx_in.sequence)
     f.write(self._hash_outputs(hash_type, tx_in_idx))
     stream_struct("L", f, self.tx.lock_time)
     stream_struct("L", f, hash_type)
     return f.getvalue()