def parse(cls, s): '''Takes a byte stream and parses a merkle block. Returns a Merkle Block object''' # version - 4 bytes, Little-Endian integer header = Block.parse_header(s) # total transactions in block - 4 bytes, Little-Endian integer total = little_endian_to_int(s.read(4)) # number of transaction hashes - varint num_txs = read_varint(s) # each transaction is 32 bytes, Little-Endian hashes = [] for _ in range(num_txs): hashes.append(s.read(32)[::-1]) # flags field - varstr flags = read_varstr(s) # initialize class return cls(header, total, hashes, flags)
def parse(cls, s): version = little_endian_to_int(s.read(4)) services = little_endian_to_int(s.read(8)) timestamp = little_endian_to_int(s.read(8)) receiver_services = little_endian_to_int(s.read(8)) receiver_ip = s.read(16)[-4:] receiver_port = little_endian_to_int(s.read(2)) sender_services = little_endian_to_int(s.read(8)) sender_ip = s.read(16)[-4:] sender_port = little_endian_to_int(s.read(2)) nonce = s.read(8) user_agent = read_varstr(s) latest_block = little_endian_to_int(s.read(4)) relay = s.read(1) == b'\x01' return cls(version, services, timestamp, receiver_services, receiver_ip, receiver_port, sender_services, sender_ip, sender_port, nonce, user_agent, latest_block, relay)
def parse(cls, stream): # get the field raw = read_varstr(stream) length = len(raw) s = BytesIO(raw) # initialize the commands array commands = [] # initialize the number of bytes we've read to 0 count = 0 # loop until we've read length bytes while count < length: # get the current byte current = s.read(1) # increment the bytes we've read count += 1 # convert the current byte to an integer current_byte = current[0] # if the current byte is between 1 and 75 inclusive if current_byte >= 1 and current_byte <= 75: # we have a command set n to be the current byte n = current_byte # add the next n bytes as a command commands.append(s.read(n)) # increase the count by n count += n elif current_byte == 76: # op_pushdata1 data_length = little_endian_to_int(s.read(1)) commands.append(s.read(data_length)) count += data_length + 1 elif current_byte == 77: # op_pushdata2 data_length = little_endian_to_int(s.read(2)) commands.append(s.read(data_length)) count += data_length + 2 else: # we have an op code. set the current byte to op_code op_code = current_byte # add the op_code to the list of commands commands.append(op_code) return cls(commands)
def decrypt_message(self, s: BufferedIOBase) -> bytes: cipher = self.cipher(S256Point.parse(s.read(33))) return cipher.decrypt(read_varstr(s))
def parse(cls, s): message = read_varstr(s) code = s.read(1) reason = read_varstr(s) return cls(message, code, reason)
def parse(cls, s): filter_type = s.read(1)[0] block_hash = s.read(32)[::-1] filter_bytes = read_varstr(s) return cls(filter_type, block_hash, filter_bytes)