<?php

declare(strict_types=1);

namespace PhpWA\Crypto\Signal;

use InvalidArgumentException;
use PhpWA\Crypto\X25519;

final class SignalKeyPair
{
    /** @var string */ private $publicKey;
    /** @var string */ private $privateKey;

    public function __construct(string $publicKey, string $privateKey)
    {
        SignalCurve::assertPublicKey($publicKey);
        if (strlen($privateKey) !== 32) {
            throw new InvalidArgumentException('Signal private key must contain exactly 32 bytes.');
        }
        $derived = SignalCurve::prefix(X25519::fromPrivate($privateKey)->publicKey());
        if (!hash_equals($derived, $publicKey)) {
            throw new InvalidArgumentException('Signal key-pair public key does not match its private key.');
        }
        $this->publicKey = $publicKey;
        $this->privateKey = $privateKey;
    }

    public function publicKey(): string { return $this->publicKey; }
    public function publicKey32(): string { return SignalCurve::unprefix($this->publicKey); }
    public function privateKey(): string { return $this->privateKey; }
}
