feat(crypto): add extra types and conversions (#517)
This commit is contained in:
parent
de88df1986
commit
0ca7c34776
4 changed files with 63 additions and 19 deletions
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue