def parse_as_header(class_, f): """ Parse the Block header from the file-like object """ version, previous_block_hash, merkle_root, height = parse_struct("L##L", f) # https://github.com/BTCGPU/BTCGPU/wiki/Technical-Spec f.read(28) # reserved area (timestamp, difficulty, nonce, solution) = parse_struct("LL#S", f) return class_(version, previous_block_hash, merkle_root, timestamp, difficulty, nonce, height, solution)
def parse(class_, f, allow_segwit=None): """Parse a Bitcoin transaction Tx. :param f: a file-like object that contains a binary streamed transaction :param allow_segwit: (optional) set to True to allow parsing of segwit transactions. The default value is defined by the class variable ALLOW_SEGWIT """ if allow_segwit is None: allow_segwit = class_.ALLOW_SEGWIT txs_in = [] txs_out = [] version, = parse_struct("L", f) v1 = ord(f.read(1)) is_segwit = allow_segwit and (v1 == 0) v2 = None if is_segwit: flag = f.read(1) if flag == b'\0': raise ValueError("bad flag in segwit") if flag == b'\1': v1 = None else: is_segwit = False v2 = ord(flag) count = parse_satoshi_int(f, v=v1) txs_in = [] for i in range(count): txs_in.append(class_.TxIn.parse(f)) count = parse_satoshi_int(f, v=v2) txs_out = [] for i in range(count): txs_out.append(class_.TxOut.parse(f)) if is_segwit: for tx_in in txs_in: stack = [] count = parse_satoshi_int(f) for i in range(count): stack.append(parse_satoshi_string(f)) tx_in.witness = stack lock_time, = parse_struct("L", f) if (version & 0xFFFF) >= 3 and (version >> 16) & 0xFFFF != 0: extraPayloadSize = parse_satoshi_int(f) extraPayload = f.read(extraPayloadSize) return class_(version, txs_in, txs_out, lock_time, None, extraPayload)
def parse(class_, f, allow_segwit=None): """Parse a Bitcoin transaction Tx. :param f: a file-like object that contains a binary streamed transaction :param allow_segwit: (optional) set to True to allow parsing of segwit transactions. The default value is defined by the class variable ALLOW_SEGWIT """ if allow_segwit is None: allow_segwit = class_.ALLOW_SEGWIT txs_in = [] txs_out = [] version, = parse_struct("L", f) v1 = ord(f.read(1)) is_segwit = allow_segwit and (v1 == 0) v2 = None if is_segwit: flag = f.read(1) if flag == b'\0': raise ValueError("bad flag in segwit") if flag == b'\1': v1 = None else: is_segwit = False v2 = ord(flag) count = parse_satoshi_int(f, v=v1) txs_in = [] for i in range(count): txs_in.append(class_.TxIn.parse(f)) count = parse_satoshi_int(f, v=v2) txs_out = [] for i in range(count): txs_out.append(class_.TxOut.parse(f)) if is_segwit: for tx_in in txs_in: stack = [] count = parse_satoshi_int(f) for i in range(count): stack.append(parse_satoshi_string(f)) tx_in.witness = stack lock_time, = parse_struct("L", f) return class_(version, txs_in, txs_out, lock_time)
def parse(self, f): return self(*parse_struct("L#", f), dont_check=True)
def parse(cls, f): return cls(*parse_struct("QS#LIbI", f))
def parse(self, f): return self(*parse_struct("#LSL", f))
def parse(self, f): services, ip_bin, port = parse_struct("Q@h", f) self.ip_bin = ip_bin return self(services, self.ip_bin, port)
def parse(cls, f): return cls(*parse_struct("QS", f))