forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traits.rs
42 lines (35 loc) · 1.16 KB
/
traits.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use libipld_core::codec::{Codec, Encode};
use signature::Verifier;
use thiserror::Error;
pub trait Header<Enc: Codec>: for<'a> TryFrom<&'a [u8]> + Into<Vec<u8>> {
type Signature: signature::SignatureEncoding;
type Verifier: signature::Verifier<Self::Signature>;
fn codec(&self) -> &Enc;
fn encode_payload<T: Encode<Enc>, Buf: std::io::Write>(
&self,
payload: T,
buffer: &mut Buf,
) -> Result<(), libipld_core::error::Error> {
payload.encode(Self::codec(self).clone(), buffer)
}
fn try_verify<'a, T: Encode<Enc>>(
&self,
verifier: &'a Self::Verifier,
signature: &'a Self::Signature,
payload: T,
) -> Result<(), VerifyError> {
let mut buffer = vec![];
self.encode_payload(payload, &mut buffer)
.map_err(VerifyError::CodecError)?;
verifier
.verify(&buffer, signature)
.map_err(VerifyError::SignatureError)
}
}
#[derive(Debug, Error)]
pub enum VerifyError {
#[error("Varsig codec error: {0}")]
CodecError(libipld_core::error::Error),
#[error("varsig signature error: {0}")]
SignatureError(signature::Error),
}