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()]; 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),
}
}
}
fn main() {
let foo = Foo {
x: bls::Scalar::one(),
};
let x = foo.to_string();
println!("done {}", x);
}