def parse(cls, f, include_offsets=None): """Parse the Block from the file-like object in the standard way that blocks are sent in the network.""" if include_offsets is None: include_offsets = hasattr(f, "tell") (version, previous_block_hash, merkle_root, timestamp, difficulty, nonce, count) = parse_struct("L##LLLI", f) txs = [] for i in range(count): if include_offsets: offset_in_block = f.tell() tx_type, = parse_struct("L", f) # tx = cls.Transaction.parse(f) if 0x01 == tx_type: tx = cls.Transaction.parse(f) elif 0x02 == tx_type: tx = cls.TransactionCF.parse(f) txs.append(tx) if include_offsets: tx.offset_in_block = offset_in_block block = cls(version, previous_block_hash, merkle_root, timestamp, difficulty, nonce, txs, 0) for tx in txs: tx.block = block block.check_merkle_hash() return block
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_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(f): tx_type, = parse_struct("L", f) if 0x01 == tx_type: tx = Transaction.parse(f) elif 0x02 == tx_type: tx = TransactionCF.parse(f) return tx
def parse(class_, f, allow_segwit=None): """Parse a Bitcoin transaction Tx from the file-like object f.""" if allow_segwit is None: allow_segwit = class_.ALLOW_SEGWIT txs_in = [] txs_out = [] original_hash, target_amount, pubkey, end_time, pre_hash, lack_amount = parse_struct( "#QSL#Q", f) pubkey, certificate = class_.separate_cert(class_, pubkey) pubkey = pubkey.decode() 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_bc_int(f, v=v1) txs_in = [] for i in range(count): txs_in.append(class_.TxIn.parse(f)) count = parse_bc_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_bc_int(f) for i in range(count): stack.append(parse_bc_string(f)) tx_in.witness = stack lock_time, = parse_struct("L", f) return class_( CFHeader(original_hash, target_amount, pubkey, end_time, pre_hash, lack_amount, certificate), version, txs_in, txs_out, lock_time)
def parse(self, f, is_first_in_block=False): """Parse a Bitcoin transaction Tx from the file-like object f.""" version, count = parse_struct("LI", f) txs_in = [] if is_first_in_block: txs_in.append(TxInGeneration.parse(f)) count = count - 1 for i in range(count): txs_in.append(ProxyTxIn.parse(f)) count, = parse_struct("I", f) txs_out = [] for i in range(count): txs_out.append(ProxyTxOut.parse(f)) lock_time, = parse_struct("L", f) rv = self(version, txs_in, txs_out, lock_time) for txe in rv.txs_in + rv.txs_out: txe.tx = rv return rv
def parse_with_paths(class_, f, input_chain_paths, output_chain_paths): """Parse a Bitcoin transaction AccountTx from the file-like object f, attaching relevant chain paths.""" version, count = parse_struct("LI", f) txs_in = [] for i in range(count): txin = AccountTxIn.parse(f) txin.path = input_chain_paths[i] txs_in.append(txin) count, = parse_struct("I", f) txs_out = [] for i in range(count): path = output_chain_paths[i] if path: txout = AccountTxOut.parse(f) txout.path = path else: txout = TxOut.parse(f) txs_out.append(txout) lock_time, = parse_struct("L", f) return class_(version, txs_in, txs_out, lock_time)
def parse(class_, f, allow_segwit=None): """Parse a Bitcoin transaction Tx from the file-like object f.""" 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_bc_int(f, v=v1) txs_in = [] for i in range(count): txs_in.append(class_.TransactionIn.parse(f)) count = parse_bc_int(f, v=v2) txs_out = [] for i in range(count): txs_out.append(class_.TransactionOut.parse(f)) if is_segwit: for tx_in in txs_in: stack = [] count = parse_bc_int(f) for i in range(count): stack.append(parse_bc_string(f)) tx_in.witness = stack lock_time, = parse_struct("L", f) return class_(version, txs_in, txs_out, lock_time)
def parse(cls, f): return cls(*parse_struct("L#", f), dont_check=True)
def parse(self, f): return self(*parse_struct("L#", f))
def parse(self, f): return self(*parse_struct("#LSL", f))
def parse(self, f): return self(*parse_struct("L#", f), dont_check=True)
def parse(cls, f): return cls(*parse_struct("QS", f))
def parse(self, f): services, ip_bin, port = parse_struct("Q@h", f) if ip_bin.startswith(IP4_HEADER): ip_bin = ip_bin[len(IP4_HEADER):] ip_int = int.from_bytes(ip_bin, byteorder="big") return self(services, ip_int, port)
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)