Define custom errors

This commit is contained in:
Kitaiti Makoto
2021-01-16 00:43:41 +09:00
parent f720dcbe9a
commit 6eee4b0b65
2 changed files with 15 additions and 9 deletions
+6 -3
View File
@@ -17,6 +17,9 @@ pub fn gen_keypair() -> (Vec<u8>, Vec<u8>) {
)
}
#[derive(Debug)]
pub struct Error();
pub trait Signer {
type Error;
@@ -29,7 +32,7 @@ pub trait Signer {
}
pub trait Signable {
fn sign<T>(&mut self, creator: &T) -> Result<&mut Self, ()>
fn sign<T>(&mut self, creator: &T) -> Result<&mut Self, Error>
where
T: Signer;
fn verify<T>(self, creator: &T) -> bool
@@ -43,7 +46,7 @@ pub trait Signable {
}
impl Signable for serde_json::Value {
fn sign<T: Signer>(&mut self, creator: &T) -> Result<&mut serde_json::Value, ()> {
fn sign<T: Signer>(&mut self, creator: &T) -> Result<&mut serde_json::Value, Error> {
let creation_date = Utc::now().to_rfc3339();
let mut options = json!({
"type": "RsaSignature2017",
@@ -61,7 +64,7 @@ impl Signable for serde_json::Value {
let document_hash = Self::hash(&self.to_string());
let to_be_signed = options_hash + &document_hash;
let signature = base64::encode(&creator.sign(&to_be_signed).map_err(|_| ())?);
let signature = base64::encode(&creator.sign(&to_be_signed).map_err(|_| Error())?);
options["signatureValue"] = serde_json::Value::String(signature);
self["signature"] = options;