AccountForSerialization struct for Account serialization

This commit is contained in:
Rostyslav Tyshko 2025-06-11 01:20:46 -04:00
parent 720263ae2d
commit 222b0136bf

View File

@ -16,7 +16,7 @@ use crate::key_management::{
pub type PublicKey = AffinePoint;
pub type AccountAddress = TreeHashType;
#[derive(Serialize, Deserialize, Clone)]
#[derive(Clone)]
pub struct Account {
pub key_holder: AddressKeyHolder,
pub address: AccountAddress,
@ -24,6 +24,64 @@ pub struct Account {
pub utxos: HashMap<TreeHashType, UTXO>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct AccountForSerialization {
pub key_holder: AddressKeyHolder,
pub address: AccountAddress,
pub balance: u64,
pub utxos: HashMap<String, UTXO>,
}
impl From<Account> for AccountForSerialization {
fn from(value: Account) -> Self {
AccountForSerialization {
key_holder: value.key_holder,
address: value.address,
balance: value.balance,
utxos: value
.utxos
.into_iter()
.map(|(key, val)| (hex::encode(key), val))
.collect(),
}
}
}
impl From<AccountForSerialization> for Account {
fn from(value: AccountForSerialization) -> Self {
Account {
key_holder: value.key_holder,
address: value.address,
balance: value.balance,
utxos: value
.utxos
.into_iter()
.map(|(key, val)| (hex::decode(key).unwrap().try_into().unwrap(), val))
.collect(),
}
}
}
impl Serialize for Account {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let account_for_serialization: AccountForSerialization = From::from(self.clone());
account_for_serialization.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Account {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let account_for_serialization = <AccountForSerialization>::deserialize(deserializer)?;
Ok(account_for_serialization.into())
}
}
///A strucure, which represents all the visible(public) information
///
/// known to each node about account `address`