def hash(self, hash_type=None): """Return the hash for this Tx object.""" s = io.BytesIO() self.stream(s) if hash_type is not None: stream_struct("L", s, hash_type) return double_sha256(s.getvalue())
def _tx_hash(self, hash_type=None): """Return the hash for this Tx object.""" 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())
def stream(self, f): """Stream the block in the standard way to the file-like object f.""" stream_struct("L##LLLI", f, self.version, self.previous_block_hash, self.merkle_root, self.timestamp, self.difficulty, self.nonce, len(self.txs)) for t in self.txs: t.stream(f)
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)
def hash_prevouts(self, hash_type): if hash_type & SIGHASH_ANYONECANPAY: return ZERO32 f = io.BytesIO() for tx_in in self.txs_in: f.write(tx_in.previous_hash) stream_struct("L", f, tx_in.previous_index) return double_sha256(f.getvalue())
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.txs_in: stream_struct("L", f, tx_in.sequence) return double_sha256(f.getvalue())
def hash_outputs(self, hash_type, tx_in_idx): txs_out = self.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("Q", f, tx_out.coin_value) tools.write_push_data([tx_out.script], f) return double_sha256(f.getvalue())
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.""" include_witnesses = include_witness_data and self.has_witness_data() #### if TransactionUtils.isCFTransation(self): stream_struct("L", f, 2) stream_struct("#", f, self.cf_header.original_hash) stream_struct("Q", f, self.cf_header.target_amount) stream_struct("S", f, self.cf_header.pubkey.encode(encoding="utf-8")) stream_struct("L", f, self.cf_header.end_time) stream_struct("#", f, self.cf_header.pre_hash) stream_struct("Q", f, self.cf_header.lack_amount) else: stream_struct("L", f, 1) #### 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_bc_string(f, w) stream_struct("L", f, self.lock_time) if include_unspents and not self.missing_unspents(): self.stream_unspents(f)
def stream(self, f): stream_struct("L#", f, self.item_type, self.data)
def stream(self, f): stream_struct("QS", f, self.coin_value, self.script)
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])
def stream(self, f, blank_solutions=False, include_unspents=False, include_witness_data=True, with_time=False): """Stream a Bitcoin transaction Tx to the file-like object f.""" include_witnesses = include_witness_data and self.has_witness_data() stream_struct("L", f, self.version) if with_time: stream_struct("L", f, int(time.time())) 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_bc_string(f, w) stream_struct("L", f, self.lock_time) if include_unspents and not self.missing_unspents(): self.stream_unspents(f)
def segwit_signature_preimage(self, script, tx_in_idx, hash_type): f = io.BytesIO() stream_struct("L", f, self.version) # calculate hash prevouts f.write(self.hash_prevouts(hash_type)) f.write(self.hash_sequence(hash_type)) tx_in = self.txs_in[tx_in_idx] f.write(tx_in.previous_hash) stream_struct("L", f, tx_in.previous_index) tx_out = self.unspents[tx_in_idx] stream_bc_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.lock_time) stream_struct("L", f, hash_type) return f.getvalue()
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)