def serialize(self): '''Serialize this message to send over the network''' # version is 4 bytes little endian result = int_to_little_endian(self.version, 4) # services is 8 bytes little endian result += int_to_little_endian(self.services, 8) # timestamp is 8 bytes little endian result += int_to_little_endian(self.timestamp, 8) # receiver services is 8 bytes little endian result += int_to_little_endian(self.receiver_services, 8) # IPV4 is 10 00 bytes and 2 ff bytes then receiver ip result += b'\x00' * 10 + b'\xff\xff' + self.receiver_ip # receiver port is 2 bytes, big endian result += self.receiver_port.to_bytes(2, 'big') # sender services is 8 bytes little endian result += int_to_little_endian(self.sender_services, 8) # IPV4 is 10 00 bytes and 2 ff bytes then sender ip result += b'\x00' * 10 + b'\xff\xff' + self.sender_ip # sender port is 2 bytes, big endian result += self.sender_port.to_bytes(2, 'big') # nonce should be 8 bytes result += self.nonce # useragent is a variable string, so varint first result += serialize_varint(len(self.user_agent)) result += self.user_agent # latest block is 4 bytes little endian result += int_to_little_endian(self.latest_block, 4) # relay is 00 if false, 01 if true if self.relay: result += b'\x01' else: result += b'\x00' return result
def serialize(self): # get the raw serialization (no prepended length) result = self.raw_serialize() # get the length of the whole thing total = len(result) # serialize_varint the total length of the result and prepend return serialize_varint(total) + result
def serialize(self): # start with the number of items as a varint result = serialize_varint(len(self.data)) # loop through each tuple (data_type, identifier) in self.data for data_type, identifier in self.data: # data type is 4 bytes Little-Endian result += int_to_little_endian(data_type, 4) # identifier needs to be in Little-Endian result += identifier[::-1] return result
def serialize(self): '''Serialize this message to send over the network''' # protocol version is 4 bytes little-endian result = int_to_little_endian(self.version, 4) # number of hashes is a varint result += serialize_varint(self.num_hashes) # start block is in little-endian result += self.start_block[::-1] # end block is also in little-endian result += self.end_block[::-1] return result
def serialize(self): # FIXME: DELETE '''Returns the byte serialization of the transaction''' # serialize version (4 bytes, little endian) result = int_to_little_endian(self.version, 4) # serialize_varint on the number of inputs result += serialize_varint(len(self.tx_ins)) # iterate inputs for tx_in in self.tx_ins: # serialize each input result += tx_in.serialize() # serialize_varint on the number of outputs result += serialize_varint(len(self.tx_outs)) # iterate outputs for tx_out in self.tx_outs: # serialize each output result += tx_out.serialize() # serialize locktime (4 bytes, little endian) result += int_to_little_endian(self.locktime, 4) return result
def sig_hash(self, input_index, script_pubkey, redeem_script=None): # FIXME: DELETE '''Returns the byte serialization of the transaction''' '''Returns the integer representation of the hash that needs to get signed for index input_index''' # start the serialization with version # use int_to_little_endian in 4 bytes s = int_to_little_endian(self.version, 4) # add how many inputs there are using serialize_varint s += serialize_varint(len(self.tx_ins)) # loop through each input using enumerate, so we have the input index for i, tx_in in enumerate(self.tx_ins): # if the input index is the one we're signing if i == input_index: # if the RedeemScript was passed in, that's the ScriptSig # otherwise the previous tx's ScriptPubkey is the ScriptSig # script_sig = tx_in.script_pubkey(self.testnet) script_sig = script_pubkey # Otherwise, the ScriptSig is empty else: script_sig = None # add the serialization of the input with the ScriptSig we want s += TxIn( prev_tx=tx_in.prev_tx, prev_index=tx_in.prev_index, script_sig=script_sig, sequence=tx_in.sequence, ).serialize() # add how many outputs there are using serialize_varint s += serialize_varint(len(self.tx_outs)) # add the serialization of each output for tx_out in self.tx_outs: s += tx_out.serialize() # add the locktime using int_to_little_endian in 4 bytes s += int_to_little_endian(self.locktime, 4) # add SIGHASH_ALL using int_to_little_endian in 4 bytes s += int_to_little_endian(SIGHASH_ALL, 4) # double_sha256 the serialization h256 = double_sha256(s) # convert the result to an integer using int.from_bytes(x, 'big') return int.from_bytes(h256, 'big')