feat(crypto): add extra types and conversions (#517)

This commit is contained in:
Andrew Westberg 2024-10-04 07:37:36 -04:00 committed by GitHub
parent de88df1986
commit 0ca7c34776
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 63 additions and 19 deletions

View file

@ -4,6 +4,7 @@ use minicbor::{
Decode, Encode,
};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::{collections::HashMap, fmt, hash::Hash as StdHash, ops::Deref};
static TAG_SET: u64 = 258;
@ -1348,6 +1349,14 @@ impl Deref for Bytes {
}
}
impl<const N: usize> TryFrom<&Bytes> for [u8; N] {
type Error = core::array::TryFromSliceError;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
value.0.as_slice().try_into()
}
}
impl TryFrom<String> for Bytes {
type Error = hex::FromHexError;
@ -1357,6 +1366,15 @@ impl TryFrom<String> for Bytes {
}
}
impl FromStr for Bytes {
type Err = hex::FromHexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let v = hex::decode(s)?;
Ok(Bytes(minicbor::bytes::ByteVec::from(v)))
}
}
impl From<Bytes> for String {
fn from(b: Bytes) -> Self {
hex::encode(b.deref())