def deserialize_header(self, s): h = {} h['version'] = hex_to_int(s[0:4]) h['prev_block_hash'] = hash_encode(s[4:36]) h['merkle_root'] = hash_encode(s[36:68]) h['claim_trie_root'] = hash_encode(s[68:100]) h['timestamp'] = hex_to_int(s[100:104]) h['bits'] = hex_to_int(s[104:108]) h['nonce'] = hex_to_int(s[108:112]) return h
def tx_response(self, response): params, result = self.parse_response(response) if not params: return tx_hash, tx_height = params assert tx_hash == hash_encode(Hash(result.decode('hex'))) tx = Transaction(result) try: tx.deserialize() except Exception: log.info("cannot deserialize transaction, skipping: %s", tx_hash) return self.wallet.receive_tx_callback(tx_hash, tx, tx_height) self.requested_tx.remove((tx_hash, tx_height)) log.info("received tx %s height: %d bytes: %d", tx_hash, tx_height, len(tx.raw)) # callbacks self.network.trigger_callback('new_transaction', tx) if not self.requested_tx: self.network.trigger_callback('updated')
def parse_input(vds): d = {} prevout_hash = hash_encode(vds.read_bytes(32)) prevout_n = vds.read_uint32() scriptSig = vds.read_bytes(vds.read_compact_size()) d['scriptSig'] = scriptSig.encode('hex') sequence = vds.read_uint32() if prevout_hash == '00' * 32: d['is_coinbase'] = True else: d['is_coinbase'] = False d['prevout_hash'] = prevout_hash d['prevout_n'] = prevout_n d['sequence'] = sequence d['pubkeys'] = [] d['signatures'] = {} d['address'] = None if scriptSig: parse_scriptSig(d, scriptSig) return d
def tx_response(response): params, result = self.parse_response(response) if not params: log.warning("failed to get %s", txid) self.requested_tx.remove((txid, height)) return tx_hash, tx_height = params assert tx_hash == hash_encode(Hash(result.decode('hex'))) tx = Transaction(result) try: tx.deserialize() except Exception: log.info("cannot deserialize transaction, skipping: %s", tx_hash) return self.wallet.receive_tx_callback(tx_hash, tx, tx_height) self.requested_tx.remove((tx_hash, tx_height)) log.info("received tx %s height: %d bytes: %d", tx_hash, tx_height, len(tx.raw)) # callbacks self.network.trigger_callback('new_transaction', tx) if not self.requested_tx: self.network.trigger_callback('updated')
def pow_hash_header(self, header): if header is None: return '0' * 64 return hash_encode(PoWHash( self.serialize_header(header).decode('hex')))
def hash_merkle_root(self, merkle_s, target_hash, pos): h = hash_decode(target_hash) for i, item in enumerate(merkle_s): h = Hash(hash_decode(item) + h) if ((pos >> i) & 1) else Hash(h + hash_decode(item)) return hash_encode(h)
def pow_hash_header(self, header): if header is None: return '0' * 64 return hash_encode(PoWHash(self.serialize_header(header).decode('hex')))