use crate::protocol::payload::payload::PayloadResponseKind;
use crate::tools::server_id::ServerId;
use crate::tools::time::{TimeMillis, TimeMillisBytes, TIME_MILLIS_BYTES};
use crate::tools::types::{Hash, Id, PQCommitmentBytes, Pow, Salt, Signature, SignatureKey, VerificationKeyBytes, HASH_BYTES, ID_BYTES, PQ_COMMITMENT_BYTES, SALT_BYTES, SIGNATURE_BYTES, VERIFICATION_KEY_BYTES};
use crate::tools::{compression, config, signing, BytesGatherer};
use bitflags::bitflags;
use bytes::{Buf, Bytes};
bitflags! {
pub struct RpcResponsePacketTxFlags: u8 {
const COMPRESSED = 1 << 0;
}
}
pub struct RpcResponsePacketTx;
impl RpcResponsePacketTx {
#[allow(clippy::too_many_arguments)] pub fn encode(
server_id_signature_key: &SignatureKey,
server_id_verification_key_bytes: &VerificationKeyBytes,
server_id_pq_commitment_bytes: &PQCommitmentBytes,
server_id_verification_sponsor_id: &Id,
server_id_timestamp: &TimeMillis,
server_id_hash: &Hash,
server_id_salt: &Salt,
pow_content_hash: &Hash,
flags: RpcResponsePacketTxFlags,
payload_response_kind: PayloadResponseKind,
payload_uncompressed: BytesGatherer,
) -> anyhow::Result<BytesGatherer> {
let payload_compressed: BytesGatherer = match flags.contains(RpcResponsePacketTxFlags::COMPRESSED) {
true => compression::compress_for_speed(&payload_uncompressed.to_bytes())?,
false => payload_uncompressed,
};
let payload_compressed_len = payload_compressed.len();
if payload_compressed_len > config::PROTOCOL_MAX_BLOB_SIZE_RESPONSE {
anyhow::bail!("response payload size exceeds maximum allowed size: {} > {}", payload_compressed_len, config::PROTOCOL_MAX_BLOB_SIZE_RESPONSE);
}
let pow_content_hash_signature = signing::sign(server_id_signature_key, pow_content_hash.as_ref());
let mut result = BytesGatherer::default();
result.put_u8(1); result.put_u8(flags.bits());
result.put_u16_le(payload_response_kind as u16);
result.put_slice(server_id_verification_key_bytes.as_ref());
result.put_slice(server_id_pq_commitment_bytes.as_ref());
result.put_slice(server_id_verification_sponsor_id.as_ref());
result.put_slice(server_id_timestamp.encode_be().as_ref());
result.put_slice(server_id_hash.as_ref());
result.put_slice(server_id_salt.as_ref());
result.put_slice(pow_content_hash_signature.as_ref());
result.put_u32_le(payload_compressed_len as u32);
result.put_bytes_gatherer(payload_compressed);
Ok(result)
}
}
pub struct RpcResponsePacketRx {
pub response_request_kind: PayloadResponseKind,
pub bytes: Bytes,
}
impl RpcResponsePacketRx {
pub fn decode(destination_id: &Id, pow_content_hash: &Hash, pow_min: Pow, mut response_bytes: Bytes) -> anyhow::Result<Self> {
if response_bytes.len() < size_of::<u8>() + size_of::<u8>() + size_of::<u16>() + VERIFICATION_KEY_BYTES + PQ_COMMITMENT_BYTES + ID_BYTES + TIME_MILLIS_BYTES + HASH_BYTES + SALT_BYTES + SIGNATURE_BYTES + size_of::<u32>() {
anyhow::bail!("RpcResponsePacket is too short for header");
}
let version = response_bytes.get_u8();
if 1 != version {
anyhow::bail!("Unsupported RpcRequestPacket version: {}", version);
}
let flags = RpcResponsePacketTxFlags::from_bits(response_bytes.get_u8()).ok_or_else(|| anyhow::anyhow!("Invalid RpcResponsePacket flags"))?;
let response_request_kind = PayloadResponseKind::from_u16(response_bytes.get_u16_le())?;
let server_id_verification_key = VerificationKeyBytes(response_bytes.slice(..VERIFICATION_KEY_BYTES).as_ref().try_into()?);
response_bytes.advance(VERIFICATION_KEY_BYTES);
let server_id_pq_commitment_bytes = PQCommitmentBytes(response_bytes.slice(..PQ_COMMITMENT_BYTES).as_ref().try_into()?);
response_bytes.advance(PQ_COMMITMENT_BYTES);
let server_id_verification_sponsor_id = Id(response_bytes.slice(..ID_BYTES).as_ref().try_into()?);
response_bytes.advance(ID_BYTES);
let server_id_verification_timestamp_bytes: TimeMillisBytes = TimeMillisBytes(response_bytes.slice(..TIME_MILLIS_BYTES).as_ref().try_into()?);
response_bytes.advance(TIME_MILLIS_BYTES);
let server_id_verification_hash = Hash(response_bytes.slice(..HASH_BYTES).as_ref().try_into()?);
response_bytes.advance(HASH_BYTES);
let server_id_verification_salt = Salt(response_bytes.slice(..SALT_BYTES).as_ref().try_into()?);
response_bytes.advance(SALT_BYTES);
let pow_content_hash_signature: Signature = Signature(response_bytes.slice(..SIGNATURE_BYTES).as_ref().try_into()?);
response_bytes.advance(SIGNATURE_BYTES);
let response_payload_len = response_bytes.get_u32_le() as usize;
if response_payload_len > config::PROTOCOL_MAX_BLOB_SIZE_RESPONSE {
anyhow::bail!("RpcResponsePacket payload too large: {} > {}", response_payload_len, config::PROTOCOL_MAX_BLOB_SIZE_RESPONSE);
}
if response_bytes.len() < response_payload_len {
anyhow::bail!("RpcResponsePacket is too short for payload");
}
let response_payload = response_bytes.slice(..response_payload_len);
response_bytes.advance(response_payload_len);
if !response_bytes.is_empty() {
anyhow::bail!("RpcResponsePacket is too long");
}
let (pow, pow_hash) = ServerId::pow_measure(
&server_id_verification_sponsor_id,
&server_id_verification_key,
&server_id_pq_commitment_bytes,
&server_id_verification_timestamp_bytes,
&server_id_verification_hash,
&server_id_verification_salt,
)?;
if pow < pow_min {
anyhow::bail!(format!("Server ID pow is not sufficient: {} < {}", pow, pow_min));
}
let id = ServerId::server_pow_hash_to_id(pow_hash)?;
if id != *destination_id {
if !destination_id.is_zero() {
anyhow::bail!("Server ID verification failed");
}
}
let verification_key = server_id_verification_key.to_verification_key()?;
signing::verify(&verification_key, &pow_content_hash_signature, pow_content_hash.as_ref())?;
let response_payload_decompressed = match flags.contains(RpcResponsePacketTxFlags::COMPRESSED) {
true => compression::decompress(response_payload.as_ref())?.to_bytes(),
false => response_payload,
};
Ok(Self {
response_request_kind,
bytes: response_payload_decompressed,
})
}
}