use hedera_proto::services;
use prost::Message;
use time::{
Duration,
OffsetDateTime,
};
use crate::protobuf::ToProtobuf;
use crate::{
AccountId,
FromProtobuf,
Key,
LedgerId,
TopicId,
};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct TopicInfo {
pub topic_id: TopicId,
pub topic_memo: String,
#[cfg_attr(feature = "ffi", serde(with = "serde_with::As::<serde_with::base64::Base64>"))]
pub running_hash: Vec<u8>,
pub sequence_number: u64,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::TimestampNanoSeconds>>")
)]
pub expiration_time: Option<OffsetDateTime>,
pub admin_key: Option<Key>,
pub submit_key: Option<Key>,
pub auto_renew_account_id: Option<AccountId>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::DurationSeconds<i64>>>")
)]
pub auto_renew_period: Option<Duration>,
pub ledger_id: LedgerId,
}
impl TopicInfo {
pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
FromProtobuf::<services::ConsensusGetTopicInfoResponse>::from_bytes(bytes)
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
services::ConsensusGetTopicInfoResponse {
topic_id: Some(self.topic_id.to_protobuf()),
topic_info: Some(services::ConsensusTopicInfo {
memo: self.topic_memo.clone(),
running_hash: self.running_hash.clone(),
sequence_number: self.sequence_number,
expiration_time: self.expiration_time.to_protobuf(),
admin_key: self.admin_key.to_protobuf(),
submit_key: self.submit_key.to_protobuf(),
auto_renew_period: self.auto_renew_period.to_protobuf(),
auto_renew_account: self.auto_renew_account_id.to_protobuf(),
ledger_id: self.ledger_id.to_bytes(),
}),
header: None,
}
.encode_to_vec()
}
}
impl FromProtobuf<services::response::Response> for TopicInfo {
fn from_protobuf(pb: services::response::Response) -> crate::Result<Self>
where
Self: Sized,
{
let response = pb_getv!(pb, ConsensusGetTopicInfo, services::response::Response);
Self::from_protobuf(response)
}
}
impl FromProtobuf<services::ConsensusGetTopicInfoResponse> for TopicInfo {
fn from_protobuf(pb: services::ConsensusGetTopicInfoResponse) -> crate::Result<Self>
where
Self: Sized,
{
let topic_id = pb_getf!(pb, topic_id)?;
let info = pb_getf!(pb, topic_info)?;
let admin_key = Option::from_protobuf(info.admin_key)?;
let submit_key = Option::from_protobuf(info.submit_key)?;
let expiration_time = info.expiration_time.map(Into::into);
let auto_renew_period = info.auto_renew_period.map(Into::into);
let auto_renew_account_id = Option::from_protobuf(info.auto_renew_account)?;
let ledger_id = LedgerId::from_bytes(info.ledger_id);
Ok(Self {
topic_id: TopicId::from_protobuf(topic_id)?,
admin_key,
submit_key,
auto_renew_period,
auto_renew_account_id,
running_hash: info.running_hash,
sequence_number: info.sequence_number,
expiration_time,
topic_memo: info.memo,
ledger_id,
})
}
}