diff --git a/pallas-codec/src/utils.rs b/pallas-codec/src/utils.rs index 864065d..8ce5005 100644 --- a/pallas-codec/src/utils.rs +++ b/pallas-codec/src/utils.rs @@ -574,3 +574,67 @@ impl minicbor::Encode for KeepRaw<'_, T> { .map_err(minicbor::encode::Error::write) } } + +#[derive(Clone, Debug)] +pub enum Nullable { + Some(T), + Null, + Undefined, +} + +impl<'b, C, T> minicbor::Decode<'b, C> for Nullable +where + T: minicbor::Decode<'b, C>, +{ + fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result { + match d.datatype()? { + minicbor::data::Type::Null => { + d.null()?; + Ok(Self::Null) + } + minicbor::data::Type::Undefined => { + d.undefined()?; + Ok(Self::Undefined) + } + _ => { + let x = d.decode_with(ctx)?; + Ok(Self::Some(x)) + } + } + } +} + +impl minicbor::Encode for Nullable +where + T: minicbor::Encode, +{ + fn encode( + &self, + e: &mut minicbor::Encoder, + ctx: &mut C, + ) -> Result<(), minicbor::encode::Error> { + match self { + Nullable::Some(x) => { + e.encode_with(x, ctx)?; + Ok(()) + } + Nullable::Null => { + e.null()?; + Ok(()) + } + Nullable::Undefined => { + e.undefined()?; + Ok(()) + } + } + } +} + +impl From> for Nullable { + fn from(x: Option) -> Self { + match x { + Some(x) => Nullable::Some(x), + None => Nullable::Null, + } + } +} diff --git a/pallas-primitives/src/alonzo/model.rs b/pallas-primitives/src/alonzo/model.rs index 4062af0..e7afcb2 100644 --- a/pallas-primitives/src/alonzo/model.rs +++ b/pallas-primitives/src/alonzo/model.rs @@ -5,7 +5,7 @@ use pallas_codec::minicbor::{bytes::ByteVec, data::Int, data::Tag, Decode, Encode}; use pallas_crypto::hash::Hash; -use pallas_codec::utils::{AnyUInt, KeepRaw, KeyValuePairs, MaybeIndefArray}; +use pallas_codec::utils::{AnyUInt, KeepRaw, KeyValuePairs, MaybeIndefArray, Nullable}; // required for derive attrs to work use pallas_codec::minicbor; @@ -1373,7 +1373,7 @@ pub struct Tx { pub success: bool, #[n(3)] - pub auxiliary_data: Option, + pub auxiliary_data: Nullable, } #[derive(Encode, Decode, Debug, Clone)] @@ -1388,7 +1388,7 @@ pub struct MintedTx<'b> { pub success: bool, #[n(3)] - pub auxiliary_data: Option>, + pub auxiliary_data: Nullable>, } #[cfg(test)] diff --git a/pallas-primitives/src/babbage/model.rs b/pallas-primitives/src/babbage/model.rs index 32cbb9f..612ef03 100644 --- a/pallas-primitives/src/babbage/model.rs +++ b/pallas-primitives/src/babbage/model.rs @@ -5,7 +5,7 @@ use pallas_codec::minicbor::{bytes::ByteVec, Decode, Encode}; use pallas_crypto::hash::Hash; -use pallas_codec::utils::{CborWrap, KeepRaw, KeyValuePairs, MaybeIndefArray}; +use pallas_codec::utils::{CborWrap, KeepRaw, KeyValuePairs, MaybeIndefArray, Nullable}; // required for derive attrs to work use pallas_codec::minicbor; @@ -561,7 +561,7 @@ pub struct Tx { pub success: bool, #[n(3)] - pub auxiliary_data: Option, + pub auxiliary_data: Nullable, } #[derive(Encode, Decode, Debug, Clone)] @@ -576,7 +576,7 @@ pub struct MintedTx<'b> { pub success: bool, #[n(3)] - pub auxiliary_data: Option>, + pub auxiliary_data: Nullable>, } #[cfg(test)] diff --git a/pallas-traverse/src/support.rs b/pallas-traverse/src/support.rs index 2c0b603..7907957 100644 --- a/pallas-traverse/src/support.rs +++ b/pallas-traverse/src/support.rs @@ -25,7 +25,8 @@ macro_rules! clone_tx_fn { None } }) - .cloned(); + .cloned() + .into(); let x = $era::MintedTx { transaction_body, diff --git a/pallas-traverse/src/tx.rs b/pallas-traverse/src/tx.rs index 481ae4b..1af0667 100644 --- a/pallas-traverse/src/tx.rs +++ b/pallas-traverse/src/tx.rs @@ -219,8 +219,16 @@ impl<'b> MultiEraTx<'b> { fn aux_data(&self) -> Option<&KeepRaw<'_, AuxiliaryData>> { match self { - MultiEraTx::AlonzoCompatible(x, _) => x.auxiliary_data.as_ref(), - MultiEraTx::Babbage(x) => x.auxiliary_data.as_ref(), + MultiEraTx::AlonzoCompatible(x, _) => match &x.auxiliary_data { + pallas_codec::utils::Nullable::Some(x) => Some(x), + pallas_codec::utils::Nullable::Null => None, + pallas_codec::utils::Nullable::Undefined => None, + }, + MultiEraTx::Babbage(x) => match &x.auxiliary_data { + pallas_codec::utils::Nullable::Some(x) => Some(x), + pallas_codec::utils::Nullable::Null => None, + pallas_codec::utils::Nullable::Undefined => None, + }, MultiEraTx::Byron(_) => None, } }