From a7698115069dfaadd7f0518a50bd2ab69424fe6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 11 Mar 2024 14:50:28 +0100 Subject: [PATCH] Place 'parsers' (or rather converters) directly in ASN schema. --- kairos-tx/src/asn.rs | 37 +++++++++++++++++++++++++++++++++++++ kairos-tx/src/lib.rs | 1 - kairos-tx/src/parsers.rs | 40 ---------------------------------------- 3 files changed, 37 insertions(+), 41 deletions(-) delete mode 100644 kairos-tx/src/parsers.rs diff --git a/kairos-tx/src/asn.rs b/kairos-tx/src/asn.rs index 4c189f8d..1e695455 100644 --- a/kairos-tx/src/asn.rs +++ b/kairos-tx/src/asn.rs @@ -3,6 +3,7 @@ use crate::error::TxError; // Expose types for the public API. pub use rasn::types::{Integer, OctetString}; +use num_traits::cast::ToPrimitive; use rasn::types::AsnType; use rasn::{Decode, Encode}; @@ -10,14 +11,50 @@ use rasn::{Decode, Encode}; #[rasn(delegate)] pub struct PublicKey(pub(crate) OctetString); +// Converts an ASN.1 decoded public key into raw byte representation. +impl From for Vec { + fn from(value: PublicKey) -> Self { + value.0.into() + } +} + #[derive(AsnType, Encode, Decode, Debug)] #[rasn(delegate)] pub struct Amount(pub(crate) Integer); +// Attempts to convert an ASN.1 decoded amount (which is represented as a big integer) +// into a `u64`. This conversion can fail if the decoded value is outside the `u64` range, +// thereby enforcing the specified ASN.1 constraints on the value: `INTEGER +// (0..18446744073709551615)`. +impl TryFrom for u64 { + type Error = TxError; + + fn try_from(value: Amount) -> Result { + value + .0 + .to_u64() + .ok_or(TxError::ConstraintViolation { field: "amount" }) + } +} + #[derive(AsnType, Encode, Decode, Debug)] #[rasn(delegate)] pub struct Nonce(pub(crate) Integer); +// Similar to `Amount`, attempts to convert an ASN.1 decoded nonce into a `u64`. +// This is crucial for validating that the nonce adheres to expected constraint: +// `INTEGER (0..18446744073709551615)`. +impl TryFrom for u64 { + type Error = TxError; + + fn try_from(value: Nonce) -> Result { + value + .0 + .to_u64() + .ok_or(TxError::ConstraintViolation { field: "nonce" }) + } +} + #[derive(AsnType, Encode, Decode, Debug)] #[non_exhaustive] pub struct SigningPayload { diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index 66735511..930a804f 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,4 +1,3 @@ pub mod asn; pub mod error; pub mod helpers; -pub mod parsers; diff --git a/kairos-tx/src/parsers.rs b/kairos-tx/src/parsers.rs deleted file mode 100644 index 690a14c3..00000000 --- a/kairos-tx/src/parsers.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::asn; -use crate::error::TxError; - -use num_traits::cast::ToPrimitive; - -// Converts an ASN.1 decoded public key into raw byte representation. -impl From for Vec { - fn from(value: asn::PublicKey) -> Self { - value.0.into() - } -} - -// Attempts to convert an ASN.1 decoded amount (which is represented as a big integer) -// into a `u64`. This conversion can fail if the decoded value is outside the `u64` range, -// thereby enforcing the specified ASN.1 constraints on the value: `INTEGER -// (0..18446744073709551615)`. -impl TryFrom for u64 { - type Error = TxError; - - fn try_from(value: asn::Amount) -> Result { - value - .0 - .to_u64() - .ok_or(TxError::ConstraintViolation { field: "amount" }) - } -} - -// Similar to `asn::Amount`, attempts to convert an ASN.1 decoded nonce into a `u64`. -// This is crucial for validating that the nonce adheres to expected constraint: -// `INTEGER (0..18446744073709551615)`. -impl TryFrom for u64 { - type Error = TxError; - - fn try_from(value: asn::Nonce) -> Result { - value - .0 - .to_u64() - .ok_or(TxError::ConstraintViolation { field: "nonce" }) - } -}