Skip to content

Commit d06b61c

Browse files
committed
primitives - ChannelId
1 parent 86d1ac0 commit d06b61c

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

primitives/src/channel.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,46 @@ use serde_hex::{SerHex, StrictPfx};
99
use crate::big_num::BigNum;
1010
use crate::util::serde::ts_milliseconds_option;
1111
use crate::{AdUnit, EventSubmission, TargetingTag, ValidatorDesc, ValidatorId};
12+
use hex::{FromHex, FromHexError};
13+
use std::ops::Deref;
1214

13-
pub type ChannelId = [u8; 32];
15+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Copy, Clone)]
16+
#[serde(transparent)]
17+
pub struct ChannelId(#[serde(with = "SerHex::<StrictPfx>")] [u8; 32]);
18+
19+
impl Deref for ChannelId {
20+
type Target = [u8];
21+
22+
fn deref(&self) -> &[u8] {
23+
&self.0
24+
}
25+
}
26+
27+
impl From<[u8; 32]> for ChannelId {
28+
fn from(array: [u8; 32]) -> Self {
29+
Self(array)
30+
}
31+
}
32+
33+
impl AsRef<[u8]> for ChannelId {
34+
fn as_ref(&self) -> &[u8] {
35+
&self.0
36+
}
37+
}
38+
39+
impl FromHex for ChannelId {
40+
type Error = FromHexError;
41+
42+
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
43+
let array = hex::FromHex::from_hex(hex.as_ref())?;
44+
45+
Ok(Self(array))
46+
}
47+
}
1448

1549
#[derive(Serialize, Deserialize, Debug, Clone)]
1650
#[serde(rename_all = "camelCase")]
1751
pub struct Channel {
18-
#[serde(with = "SerHex::<StrictPfx>")]
1952
pub id: ChannelId,
2053
pub creator: String,
2154
pub deposit_asset: String,

0 commit comments

Comments
 (0)