def test_monitor_request(get_random_bp, get_random_privkey, get_random_address): balance_proof = get_random_bp() client_privkey = get_random_privkey() reward_sender_privkey = get_random_privkey() balance_proof.signature = encode_hex(eth_sign(client_privkey, balance_proof.serialize_bin())) monitor_request = MonitorRequest( balance_proof, non_closing_signature=balance_proof.signature, reward_proof_signature='', reward_amount=1, monitor_address=get_random_address(), ) monitor_request.reward_proof_signature = encode_hex( eth_sign( reward_sender_privkey, monitor_request.serialize_reward_proof(), ), ) serialized = monitor_request.serialize_data() monitor_request_verify = MonitorRequest.deserialize(serialized) balance_proof_verify = monitor_request_verify.balance_proof assert is_same_address(monitor_request_verify.monitor_address, monitor_request.monitor_address) assert is_same_address( monitor_request_verify.reward_proof_signer, monitor_request.reward_proof_signer, ) assert is_same_address( monitor_request.reward_proof_signer, private_key_to_address(reward_sender_privkey), ) assert monitor_request_verify.non_closing_signature == monitor_request.non_closing_signature assert monitor_request_verify.reward_amount == monitor_request.reward_amount assert is_same_address( balance_proof_verify.token_network_address, balance_proof.token_network_address, ) assert balance_proof_verify.chain_id == balance_proof.chain_id assert balance_proof_verify.channel_identifier == balance_proof.channel_identifier assert balance_proof_verify.nonce == balance_proof.nonce
def get_monitor_request( self, partner_address: Address, balance_proof: BalanceProof, reward_amount: int, monitor_address: Address, ) -> MonitorRequest: """Get monitor request message for a given balance proof.""" monitor_request = MonitorRequest( balance_proof, reward_proof_signature=None, reward_amount=reward_amount, monitor_address=monitor_address, ) monitor_request.reward_proof_signature = encode_hex( eth_sign( self.privkey, monitor_request.serialize_reward_proof(), ), ) monitor_request.non_closing_signature = encode_hex( eth_sign(self.privkey, monitor_request.non_closing_data), ) return monitor_request
def test_serialize_deserialize(get_random_bp, get_random_privkey): bp = get_random_bp() privkey = get_random_privkey() bp.signature = encode_hex( eth_sign( privkey, bp.serialize_bin(), ), ) serialized_message = bp.serialize_full() deserialized_message = Message.deserialize(serialized_message) assert isinstance(deserialized_message, BalanceProof)
def get_fee_info(self, partner_address: Address, **kwargs) -> FeeInfo: """Get a signed fee info message for an open channel. Parameters: partner_address - address of a partner the node has channel open with **kwargs - arguments to FeeInfo constructor """ channel_id = self.partner_to_channel_id[partner_address] fee_info = FeeInfo( self.contract.address, channel_id, **kwargs, ) fee_info.signature = encode_hex( eth_sign(self.privkey, fee_info.serialize_bin())) return fee_info
def get_balance_proof(self, partner_address: Address, **kwargs) -> BalanceProof: """Get a signed balance proof for an open channel. Parameters: partner_address - address of a partner the node has channel open with **kwargs - arguments to BalanceProof constructor """ channel_id = self.partner_to_channel_id[partner_address] bp = BalanceProof( channel_id, self.contract.address, **kwargs, ) bp.signature = encode_hex(eth_sign(self.privkey, bp.serialize_bin())) return bp
def request_paths(self, target_address: Address, **kwargs) -> PathsRequest: """Generate a PathsRequest for pathfinding-service . Parameters: target_address: address of the transaction target, this method is agnostic that this is verified. **kwargs: arguments to FeeInfo constructor """ # FIXME implement a nonce for replay protection request = PathsRequest( self.contract.address, self.address, target_address, **kwargs, ) request.signature = encode_hex( eth_sign(self.privkey, request.serialize_bin())) return request
def update_transfer(self, partner_address: Address, balance_proof: BalanceProof): """Given a valid signed balance proof, this method calls `updateNonClosingBalanceProof` for an open channel """ serialized = balance_proof.serialize_bin( msg_type=MessageTypeId.BALANCE_PROOF_UPDATE) non_closing_data = serialized + decode_hex(balance_proof.signature) non_closing_signature = encode_hex( eth_sign(self.privkey, non_closing_data)) self.contract.functions.updateNonClosingBalanceProof( self.partner_to_channel_id[partner_address], partner_address, self.address, balance_proof.balance_hash, balance_proof.nonce, balance_proof.additional_hash, balance_proof.signature, non_closing_signature, ).transact({'from': self.address})
def sign_data(self, private_key, data: str): return encode_hex(eth_sign(private_key, data.encode()))