Add signature management methods to Contact class for enhanced key handling

This commit is contained in:
netkas 2025-03-25 12:58:42 -04:00
parent c981ed3d9d
commit db9fa3f92e
Signed by: netkas
GPG key ID: 4D8629441B76E4CC

View file

@ -5,6 +5,7 @@
use InvalidArgumentException; use InvalidArgumentException;
use Socialbox\Enums\Types\ContactRelationshipType; use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\ContactKnownKeyRecord;
use Socialbox\Objects\PeerAddress; use Socialbox\Objects\PeerAddress;
class Contact implements SerializableInterface class Contact implements SerializableInterface
@ -51,9 +52,13 @@
{ {
$this->knownKeys[] = $key; $this->knownKeys[] = $key;
} }
elseif($key instanceof ContactKnownKeyRecord)
{
$this->knownKeys[] = $key->toStandard();
}
else else
{ {
throw new InvalidArgumentException('Invalid known key data'); throw new InvalidArgumentException('Invalid known key data, got ' . gettype($key));
} }
} }
$this->addedTimestamp = $data['added_timestamp']; $this->addedTimestamp = $data['added_timestamp'];
@ -89,6 +94,13 @@
return $this->knownKeys; return $this->knownKeys;
} }
/**
* Checks if the contact has a signature with the given UUID.
*
* @param string $signatureUuid The UUID of the signature to check for.
*
* @return bool Returns true if the signature exists, otherwise false.
*/
public function signatureExists(string $signatureUuid): bool public function signatureExists(string $signatureUuid): bool
{ {
foreach($this->knownKeys as $key) foreach($this->knownKeys as $key)
@ -101,6 +113,60 @@
return false; return false;
} }
/**
* Retrieves the signature by the UUID.
*
* @param string $signatureUuid The UUID of the signature to retrieve.
* @return KnownSigningKey|null Returns the signature if found, otherwise null.
*/
public function getSignature(string $signatureUuid): ?KnownSigningKey
{
foreach($this->knownKeys as $key)
{
if($key->getUuid() === $signatureUuid)
{
return $key;
}
}
return null;
}
/**
* Checks if the contact has a signature with the given public key.
*
* @param string $publicSignatureKey The public key of the signature to check for.
* @return bool Returns true if the signature exists, otherwise false.
*/
public function signatureKeyExists(string $publicSignatureKey): bool
{
foreach($this->knownKeys as $key)
{
if($key->getPublicKey() === $publicSignatureKey)
{
return true;
}
}
return false;
}
/**
* Retrieves the signature key by the public key.
*
* @param string $publicSignatureKey The public key of the signature key to retrieve.
* @return KnownSigningKey|null Returns the signature key if found, otherwise null.
*/
public function getSignatureByPublicKey(string $publicSignatureKey): ?KnownSigningKey
{
foreach($this->knownKeys as $key)
{
if($key->getPublicKey() === $publicSignatureKey)
{
return $key;
}
}
return null;
}
/** /**
* Retrieves the timestamp when the contact was added. * Retrieves the timestamp when the contact was added.
* *