containsParameter('peer')) { throw new MissingRpcArgumentException('peer'); } try { $address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); } catch(InvalidArgumentException $e) { throw new InvalidRpcArgumentException('peer', $e->getMessage()); } if(!$rpcRequest->containsParameter('uuid')) { throw new MissingRpcArgumentException('uuid'); } try { $uuid = Uuid::fromString($rpcRequest->getParameter('uuid')); } catch(InvalidArgumentException $e) { throw new InvalidRpcArgumentException('uuid', $e->getMessage()); } $signingKey = Socialbox::resolvePeerSignature($address, $uuid); try { // Check if the contact already exists $peer = $request->getPeer(); if(!ContactManager::isContact($peer, $address)) { ContactManager::createContact($peer, $address); } $contact = ContactManager::getContact($peer, $address); if(ContactManager::contactGetSigningKeysCount($contact) > Configuration::getPoliciesConfiguration()->getMaxContactSigningKeys()) { return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The contact has exceeded the maximum amount of trusted signatures'); } } catch (DatabaseOperationException $e) { throw new StandardRpcException('Failed to check contact state with calling peer', StandardError::INTERNAL_SERVER_ERROR, $e); } if($signingKey === null) { return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested signature key was not found'); } try { if(ContactManager::contactSigningKeyUuidExists($contact, $signingKey->getUuid())) { return $rpcRequest->produceResponse(false); } if(ContactManager::contactSigningKeyExists($contact, $signingKey->getPublicKey())) { return $rpcRequest->produceResponse(false); } ContactManager::addContactSigningKey($contact, $signingKey); } catch (DatabaseOperationException $e) { throw new StandardRpcException('Failed to trust contact signature', StandardError::INTERNAL_SERVER_ERROR, $e); } // Return success return $rpcRequest->produceResponse(true); } }