forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es512.rs
48 lines (38 loc) · 1.42 KB
/
es512.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
use super::Header;
use libipld_core::codec::Codec;
#[derive(Clone, Debug, PartialEq)]
pub struct Es512Header<C> {
pub codec: C,
}
impl<C: TryFrom<u64>> TryFrom<&[u8]> for Es512Header<C> {
type Error = (); // FIXME
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if let Ok((0x1202, inner)) = unsigned_varint::decode::u16(&bytes) {
if let Ok((0x13, 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(|_| ())?;
return Ok(Es512Header { codec });
}
}
}
Err(())
}
}
impl<C: Into<u64>> From<Es512Header<C>> for Vec<u8> {
fn from(es: Es512Header<C>) -> Vec<u8> {
let mut tag_buf: [u8; 3] = Default::default();
let tag = unsigned_varint::encode::u16(0x1202, &mut tag_buf);
let mut hash_buf: [u8; 2] = Default::default();
let hash = unsigned_varint::encode::u8(0x13, &mut hash_buf);
let mut enc_buf: [u8; 10] = Default::default();
let enc = unsigned_varint::encode::u64(es.codec.into(), &mut enc_buf);
[tag, hash, enc].concat().into()
}
}
impl<C: Codec> Header<C> for Es512Header<C> {
type Signature = p521::ecdsa::Signature;
type Verifier = p521::ecdsa::VerifyingKey;
fn codec(&self) -> &C {
&self.codec
}
}