From aaf99f7e2dc4fa6ac3200bacdf2e4e0e83ed95fc Mon Sep 17 00:00:00 2001 From: lorban Date: Sun, 2 Jun 2024 12:51:16 +0200 Subject: [PATCH] into_static for serde_sv2 primitives --- .../serde-sv2/src/primitives/sequences/mod.rs | 18 ++++++ .../src/primitives/sequences/option.rs | 19 +++++- .../src/primitives/sequences/seq0255.rs | 19 +++++- .../src/primitives/sequences/seq064k.rs | 58 +++++++++++++++++-- 4 files changed, 104 insertions(+), 10 deletions(-) diff --git a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/mod.rs b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/mod.rs index 2cfc36e3af..8600ee5085 100644 --- a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/mod.rs +++ b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/mod.rs @@ -1,5 +1,6 @@ use super::{ShortTxId, Signature, B016M, B0255, B064K, U24, U256}; use crate::Error; +use alloc::vec::Vec; use core::convert::TryInto; use serde::{de::Visitor, Serialize}; @@ -22,6 +23,23 @@ struct Seq<'s, T: Clone + Serialize + TryFromBSlice<'s>> { _a: core::marker::PhantomData, } +impl<'s, T: Clone + Serialize + TryFromBSlice<'s>> Seq<'s, T> { + pub fn parse(self) -> Result, ()> { + let mut ret = Vec::with_capacity(self.size as usize); + let elem_len = self.size as usize; + let mut first = 0; + let mut last = elem_len; + while last <= self.data.len() { + let slice = &self.data[first..last]; + let elem = T::try_from_slice(slice).map_err(|_| ())?; + ret.push(elem); + first += elem_len; + last += elem_len; + } + Ok(ret) + } +} + struct SeqVisitor { inner_type_size: u8, max_len: SeqMaxLen, diff --git a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs index 05a57cf08d..5e378f2474 100644 --- a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs +++ b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs @@ -239,7 +239,15 @@ impl<'s> Sv2Option<'s, U256<'s>> { data: Some(data), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Sv2Option { + seq: None, + data: Some(data), + } } } pub fn inner_as_ref(&self) -> &[&[u8]] { @@ -254,7 +262,14 @@ impl<'s> Sv2Option<'s, u32> { data: Some(inner), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + Sv2Option { + seq: None, + data: Some(data), + } } } } diff --git a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs index 800f0fa20d..0e40191979 100644 --- a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs +++ b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs @@ -243,7 +243,15 @@ impl<'s> Seq0255<'s, U256<'s>> { data: Some(data), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Seq0255 { + seq: None, + data: Some(data), + } } } pub fn inner_as_ref(&self) -> &[&[u8]] { @@ -258,7 +266,14 @@ impl<'s> Seq0255<'s, u32> { data: Some(inner), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + Seq0255 { + seq: None, + data: Some(data), + } } } } diff --git a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq064k.rs b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq064k.rs index 7751dd154e..d0e1202176 100644 --- a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq064k.rs +++ b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq064k.rs @@ -452,7 +452,15 @@ impl<'s> Seq064K<'s, B064K<'s>> { data: Some(data), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Seq064K { + seq: None, + data: Some(data), + } } } } @@ -466,7 +474,15 @@ impl<'s> Seq064K<'s, B016M<'s>> { data: Some(data), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Seq064K { + seq: None, + data: Some(data), + } } } pub fn to_vec(&self) -> Vec> { @@ -486,7 +502,14 @@ impl<'s> Seq064K<'s, u32> { data: Some(inner), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + Seq064K { + seq: None, + data: Some(data), + } } } } @@ -498,7 +521,14 @@ impl<'s> Seq064K<'s, u16> { data: Some(inner), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + Seq064K { + seq: None, + data: Some(data), + } } } } @@ -513,7 +543,15 @@ impl<'s> Seq064K<'s, ShortTxId<'s>> { data: Some(data), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Seq064K { + seq: None, + data: Some(data), + } } } pub fn to_vec(&self) -> Vec> { @@ -535,7 +573,15 @@ impl<'s> Seq064K<'s, U256<'s>> { data: Some(data), } } else { - panic!() + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = self.seq.unwrap().parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Seq064K { + seq: None, + data: Some(data), + } } } }