forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs256.rs
61 lines (48 loc) · 1.78 KB
/
rs256.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use super::Header;
use crate::crypto::rs256::{Signature, VerifyingKey};
use libipld_core::codec::Codec;
use thiserror::Error;
#[derive(Clone, Debug, PartialEq)]
pub struct Rs256Header<C> {
pub codec: C,
}
impl<C: TryFrom<u64>> TryFrom<&[u8]> for Rs256Header<C> {
type Error = ParseFromBytesError<<C as TryFrom<u64>>::Error>;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if let Ok((0x1205, inner)) = unsigned_varint::decode::u16(&bytes) {
if let Ok((0x12, more)) = unsigned_varint::decode::u8(&inner) {
if let Ok((codec_info, &[])) = unsigned_varint::decode::u64(&more) {
let codec =
C::try_from(codec_info).map_err(ParseFromBytesError::CodecPrefixError)?;
return Ok(Rs256Header { codec });
}
}
}
Err(ParseFromBytesError::InvalidHeader)
}
}
#[derive(Debug, PartialEq, Clone, Error)]
pub enum ParseFromBytesError<C> {
#[error("Invalid header")]
InvalidHeader,
#[error("Codec prefix error: {0}")]
CodecPrefixError(#[from] C),
}
impl<C: Into<u64>> From<Rs256Header<C>> for Vec<u8> {
fn from(rs: Rs256Header<C>) -> Vec<u8> {
let mut tag_buf: [u8; 3] = Default::default();
let tag = unsigned_varint::encode::u16(0x1205, &mut tag_buf);
let mut hash_buf: [u8; 2] = Default::default();
let hash = unsigned_varint::encode::u8(0x12, &mut hash_buf);
let mut enc_buf: [u8; 10] = Default::default();
let enc = unsigned_varint::encode::u64(rs.codec.into(), &mut enc_buf);
[tag, hash, enc].concat().into()
}
}
impl<C: Codec> Header<C> for Rs256Header<C> {
type Signature = Signature;
type Verifier = VerifyingKey;
fn codec(&self) -> &C {
&self.codec
}
}