def sign(self, key: Key, as_a: bool) -> None: """ Signs this block with the given key :param key: The key to sign this block with. :param as_a: Whether we are signing this block as party A or B. """ if as_a: # Party A is the first to sign the transaction self.signature_a = default_eccrypto.create_signature( key, self.pack(signature_a=False, signature_b=False)) else: # Party B is the first to sign the transaction self.signature_b = default_eccrypto.create_signature( key, self.pack(signature_b=False))
def on_generic_introduction_request(self, source_address, data, prefix): auth, dist, payload = self._ez_unpack_auth(IntroductionRequestPayload, data) peer = Peer(auth.public_key_bin, source_address) peer.last_response = time.time() self.network.add_verified_peer(peer) self.network.discover_services(peer, [ prefix[2:], ]) intro_peers = [ p for p in self.network.get_peers_for_service(prefix[2:]) if not (p == peer) ] if intro_peers: intro_peer = random.choice(intro_peers) else: intro_peer = None packet = self.create_introduction_response(payload.destination_address, peer.address, payload.identifier, introduction=intro_peer) packet = prefix + packet[22:-self.signature_length] signature = default_eccrypto.create_signature(self.my_peer.key, packet) self.endpoint.send(peer.address, packet + signature)
def __init__(self, metadata_type, reserved_flags, public_key, **kwargs): super().__init__() self.metadata_type = metadata_type self.reserved_flags = reserved_flags self.public_key = bytes(public_key) self.signature = bytes( kwargs["signature"] ) if "signature" in kwargs and kwargs["signature"] else None # Special case: free-for-all entries are allowed to go with zero key and without sig check if "unsigned" in kwargs and kwargs["unsigned"]: self.public_key = NULL_KEY self.signature = NULL_SIG return if "skip_key_check" in kwargs and kwargs["skip_key_check"]: return # This is integrity check for FFA payloads. if self.public_key == NULL_KEY: if self.signature == NULL_SIG: return raise InvalidSignatureException( "Tried to create FFA payload with non-null signature") serialized_data = default_serializer.pack_serializable(self) if "key" in kwargs and kwargs["key"]: key = kwargs["key"] if self.public_key != key.pub().key_to_bin()[10:]: raise KeysMismatchException(self.public_key, key.pub().key_to_bin()[10:]) self.signature = default_eccrypto.create_signature( key, serialized_data) elif "signature" in kwargs: # This check ensures that an entry with a wrong signature will not proliferate further if not default_eccrypto.is_valid_signature( default_eccrypto.key_from_public_bin(b"LibNaCLPK:" + self.public_key), serialized_data, self.signature): raise InvalidSignatureException( "Tried to create payload with wrong signature") else: raise InvalidSignatureException( "Tried to create payload without signature")