use bls12_381 as bls; use serde::{Deserialize, Serialize}; struct Foo { x: bls::Scalar, } #[derive(Serialize, Deserialize, Debug)] struct FooObject { x: String, } fn from_slice(bytes: &[u8]) -> [u8; 32] { let mut array = [0; 32]; let bytes = &bytes[..array.len()]; // panics if not enough data array.copy_from_slice(bytes); array } trait FromString { fn from_string(string: &str) -> Self; } impl FromString for bls::Scalar { fn from_string(string: &str) -> Self { let bytes = from_slice(&hex::decode(string).unwrap()); Self::from_bytes(&bytes).unwrap() } } impl Foo { fn to_string(&self) -> String { let object = FooObject { x: hex::encode(self.x.to_bytes()), }; serde_json::to_string(&object).unwrap() } fn from_string(&self, serialized: &str) -> Self { let object: FooObject = serde_json::from_str(&serialized).unwrap(); Self { x: bls::Scalar::from_string(&object.x), } } } /* impl Serialize for Foo { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let bytes = self.x.to_bytes(); let mut state = serializer.serialize_struct("Foo", 1)?; state.serialize_field("x", &bytes); state.end() } } */ fn main() { let foo = Foo { x: bls::Scalar::one(), }; //let serialized = serde_json::to_string(&foo).unwrap(); //println!("serialized = {}", serialized); let x = foo.to_string(); println!("done {}", x); }