Skip to content

Commit

Permalink
Merge pull request bitcoin#2 from Fi3/ImplDeserializeForPrimitives
Browse files Browse the repository at this point in the history
Impl deserialize for primitives
  • Loading branch information
Fi3 committed Feb 17, 2021
2 parents e335e4f + 7fd6b00 commit afc21f7
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 69 deletions.
141 changes: 111 additions & 30 deletions protocols/v2/serde-sv2/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ impl<'de> Deserializer<'de> {
Ok(u256)
}

fn parse_signature(&'de self) -> Result<&'de [u8; 64]> {
let signature: &[u8; 64] = self.get_slice(64)?.try_into().unwrap();
Ok(signature)
}

fn parse_string(&'de self) -> Result<&'de str> {
let len = self.parse_u8()?;
let str_ = self.get_slice(len as usize)?;
Expand All @@ -146,36 +151,6 @@ impl<'de> Deserializer<'de> {
Ok(self.get_slice(len as usize)?)
}

//// TODO REMOVE!! /////

// Signature is parsed as Signature rather then [u8; 4] or [u8] as Signature in Sv2 rapresent
// a big int and not an array of bytes
// fn parse_signature(&'de self) -> Result<Signature> {
// let signature: &[u8; 64] = self.get_slice(32)?.try_into().unwrap();
// Ok(Signature(signature))
// }

// fn parse_b0255(&'de self) -> Result<&'de [u8]> {
// let len = self.parse_u8()?;
// Ok(self.get_slice(len as usize)?)
// }

// fn parse_b064k(&'de self) -> Result<&'de [u8]> {
// let len = self.parse_u16()?;
// Ok(self.get_slice(len as usize)?)
// }


// fn parse_bytes(&'de self, len: usize) -> Result<&'de [u8]> {
// Ok(self.get_slice(len as usize)?)
// }

// // Pubkey is parsed as Pubkey and not as [u8; 4] or [u8] as Pubkey in Sv2 rapresent a big int and
// // not an array of bytes
// fn parse_pubkey(&'de self) -> Result<Pubkey> {
// let pk: &[u8; 32] = self.get_slice(32)?.try_into().unwrap();
// Ok(U256(pk))
// }
}

impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
Expand Down Expand Up @@ -253,6 +228,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
match _name {
"U24" => visitor.visit_u32(self.parse_u24()?),
"U256" => visitor.visit_bytes(self.parse_u256()?),
"Signature" => visitor.visit_bytes(self.parse_signature()?),
"B016M" => visitor.visit_bytes(self.parse_b016m()?),
"Seq0255" => Ok(visitor.visit_seq(Seq::new(&mut self, Sv2Seq::S255)?)?),
"Seq064K" => Ok(visitor.visit_seq(Seq::new(&mut self, Sv2Seq::S64k)?)?),
Expand Down Expand Up @@ -506,3 +482,108 @@ fn test_struct() {

assert_eq!(deserialized, expected);
}

#[test]
fn test_u256() {
use serde::Serialize;

let u256: crate::sv2_primitives::U256 = [6; 32].into();

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Test {
a: crate::sv2_primitives::U256,
}

let expected = Test {
a: u256,
};

let mut bytes = crate::ser::to_bytes(&expected).unwrap();
let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}

#[test]
fn test_signature() {
use serde::Serialize;

let s: crate::sv2_primitives::Signature = [6; 64].into();

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Test {
a: crate::sv2_primitives::Signature,
}

let expected = Test {
a: s,
};

let mut bytes = crate::ser::to_bytes(&expected).unwrap();
let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}

#[test]
fn test_b016m() {
use serde::Serialize;

let b: crate::sv2_primitives::B016M = vec![6; 3].try_into().unwrap();

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Test {
a: crate::sv2_primitives::B016M,
}

let expected = Test {
a: b,
};

let mut bytes = crate::ser::to_bytes(&expected).unwrap();
let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}

#[test]
fn test_seq0255() {
use serde::Serialize;

let s: crate::sv2_primitives::Seq0255<bool> = vec![true; 3].try_into().unwrap();

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Test {
a: crate::sv2_primitives::Seq0255<bool>,
}

let expected = Test {
a: s,
};

let mut bytes = crate::ser::to_bytes(&expected).unwrap();
let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}

#[test]
fn test_seq064k() {
use serde::Serialize;

let s: crate::sv2_primitives::Seq0255<u8> = vec![9; 3].try_into().unwrap();

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Test {
a: crate::sv2_primitives::Seq0255<u8>,
}

let expected = Test {
a: s,
};

let mut bytes = crate::ser::to_bytes(&expected).unwrap();
let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}
23 changes: 2 additions & 21 deletions protocols/v2/serde-sv2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@ pub enum Error {
// field is missing.
Message(String),

// Zero or more variants that can be created directly by the Serializer and
// Deserializer without going through `ser::Error` and `de::Error`. These
// are specific to the format, in this case JSON.
Eof,
Syntax,
ExpectedBoolean,
ExpectedInteger,
ExpectedString,
ExpectedNull,
ExpectedArray,
ExpectedArrayComma,
ExpectedArrayEnd,
ExpectedMap,
ExpectedMapColon,
ExpectedMapComma,
ExpectedMapEnd,
ExpectedEnum,
TrailingCharacters,

StringLenBiggerThan256,
InvalidUTF8,
LenBiggerThan16M,
Expand All @@ -63,9 +44,9 @@ impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(msg) => formatter.write_str(msg),
Error::Eof => formatter.write_str("unexpected end of input"),
Error::WriteError => formatter.write_str("write error"),
Error::ReadError => formatter.write_str("read error"),
_ => panic!(),
/* and so forth */
}
}
}
Expand Down

0 comments on commit afc21f7

Please sign in to comment.