// Copyright 2024 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 use super::registration::{RegistrationData, RegistredData, RemainingBandwidthData}; use nym_service_provider_requests_common::{Protocol, ServiceProviderType}; use serde::{Deserialize, Serialize}; use crate::make_bincode_serializer; use super::VERSION; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct AuthenticatorResponse { pub protocol: Protocol, pub data: AuthenticatorResponseData, } impl AuthenticatorResponse { pub fn new_pending_registration_success( registration_data: RegistrationData, request_id: u64, ) -> Self { Self { protocol: Protocol { service_provider_type: ServiceProviderType::Authenticator, version: VERSION, }, data: AuthenticatorResponseData::PendingRegistration(PendingRegistrationResponse { reply: registration_data, request_id, }), } } pub fn new_registered(registred_data: RegistredData, request_id: u64) -> Self { Self { protocol: Protocol { service_provider_type: ServiceProviderType::Authenticator, version: VERSION, }, data: AuthenticatorResponseData::Registered(RegisteredResponse { reply: registred_data, request_id, }), } } pub fn new_remaining_bandwidth( remaining_bandwidth_data: Option, request_id: u64, ) -> Self { Self { protocol: Protocol { service_provider_type: ServiceProviderType::Authenticator, version: VERSION, }, data: AuthenticatorResponseData::RemainingBandwidth(RemainingBandwidthResponse { reply: remaining_bandwidth_data, request_id, }), } } pub fn new_topup_bandwidth( remaining_bandwidth_data: RemainingBandwidthData, request_id: u64, ) -> Self { Self { protocol: Protocol { service_provider_type: ServiceProviderType::Authenticator, version: VERSION, }, data: AuthenticatorResponseData::TopUpBandwidth(TopUpBandwidthResponse { reply: remaining_bandwidth_data, request_id, }), } } pub fn to_bytes(&self) -> Result, bincode::Error> { use bincode::Options; make_bincode_serializer().serialize(self) } pub fn from_reconstructed_message( message: &nym_sphinx::receiver::ReconstructedMessage, ) -> Result { use bincode::Options; make_bincode_serializer().deserialize(&message.message) } pub fn id(&self) -> Option { match &self.data { AuthenticatorResponseData::PendingRegistration(response) => Some(response.request_id), AuthenticatorResponseData::Registered(response) => Some(response.request_id), AuthenticatorResponseData::RemainingBandwidth(response) => Some(response.request_id), AuthenticatorResponseData::TopUpBandwidth(response) => Some(response.request_id), } } } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub enum AuthenticatorResponseData { PendingRegistration(PendingRegistrationResponse), Registered(RegisteredResponse), RemainingBandwidth(RemainingBandwidthResponse), TopUpBandwidth(TopUpBandwidthResponse), } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct PendingRegistrationResponse { pub request_id: u64, pub reply: RegistrationData, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct RegisteredResponse { pub request_id: u64, pub reply: RegistredData, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct RemainingBandwidthResponse { pub request_id: u64, pub reply: Option, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct TopUpBandwidthResponse { pub request_id: u64, pub reply: RemainingBandwidthData, }