lightning/offers/
nonce.rs1use crate::io::{self, Read};
13use crate::ln::msgs::DecodeError;
14use crate::sign::EntropySource;
15use crate::util::ser::{Readable, Writeable, Writer};
16use core::ops::Deref;
17
18#[allow(unused_imports)]
19use crate::prelude::*;
20
21#[derive(Clone, Copy, Debug, Eq, PartialEq)]
30pub struct Nonce(pub(crate) [u8; Self::LENGTH]);
31
32impl Nonce {
33 pub const LENGTH: usize = 16;
35
36 pub fn from_entropy_source<ES: Deref>(entropy_source: ES) -> Self
38 where
39 ES::Target: EntropySource,
40 {
41 let mut bytes = [0u8; Self::LENGTH];
42 let rand_bytes = entropy_source.get_secure_random_bytes();
43 bytes.copy_from_slice(&rand_bytes[..Self::LENGTH]);
44
45 Nonce(bytes)
46 }
47
48 pub fn as_slice(&self) -> &[u8] {
50 &self.0
51 }
52}
53
54impl TryFrom<&[u8]> for Nonce {
55 type Error = ();
56
57 fn try_from(bytes: &[u8]) -> Result<Self, ()> {
58 if bytes.len() != Self::LENGTH {
59 return Err(());
60 }
61
62 let mut copied_bytes = [0u8; Self::LENGTH];
63 copied_bytes.copy_from_slice(bytes);
64
65 Ok(Self(copied_bytes))
66 }
67}
68
69impl Writeable for Nonce {
70 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
71 self.0.write(w)
72 }
73}
74
75impl Readable for Nonce {
76 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
77 Ok(Nonce(Readable::read(r)?))
78 }
79}