tempo_commonware_node_config/
lib.rs1#![cfg_attr(not(test), warn(unused_crate_dependencies))]
4#![cfg_attr(docsrs, feature(doc_cfg))]
5
6use std::{fmt::Display, path::Path};
7
8use commonware_codec::{DecodeExt as _, Encode as _};
9use commonware_cryptography::{
10 Signer,
11 bls12381::primitives::group::Share,
12 ed25519::{PrivateKey, PublicKey},
13};
14
15#[cfg(test)]
16mod tests;
17
18#[derive(Clone, Debug)]
19pub struct SigningKey {
20 inner: PrivateKey,
21}
22
23impl SigningKey {
24 pub fn into_inner(self) -> PrivateKey {
25 self.inner
26 }
27
28 pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, SigningKeyError> {
29 let hex = std::fs::read_to_string(path).map_err(SigningKeyErrorKind::Read)?;
30 Self::try_from_hex(&hex)
31 }
32
33 pub fn try_from_hex(hex: &str) -> Result<Self, SigningKeyError> {
34 let bytes = const_hex::decode(hex).map_err(SigningKeyErrorKind::Hex)?;
35 let inner = PrivateKey::decode(&bytes[..]).map_err(SigningKeyErrorKind::Parse)?;
36 Ok(Self { inner })
37 }
38
39 pub fn to_writer<W: std::io::Write>(&self, mut writer: W) -> Result<(), SigningKeyError> {
41 writer
42 .write_all(self.to_string().as_bytes())
43 .map_err(SigningKeyErrorKind::Write)?;
44 Ok(())
45 }
46
47 pub fn public_key(&self) -> PublicKey {
48 self.inner.public_key()
49 }
50}
51
52impl Display for SigningKey {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 f.write_str(&const_hex::encode_prefixed(self.inner.encode().as_ref()))
55 }
56}
57
58impl From<PrivateKey> for SigningKey {
59 fn from(inner: PrivateKey) -> Self {
60 Self { inner }
61 }
62}
63
64#[derive(Debug, thiserror::Error)]
65#[error(transparent)]
66pub struct SigningKeyError {
67 #[from]
68 inner: SigningKeyErrorKind,
69}
70
71#[derive(Debug, thiserror::Error)]
72enum SigningKeyErrorKind {
73 #[error("failed decoding file contents as hex-encoded bytes")]
74 Hex(#[source] const_hex::FromHexError),
75 #[error("failed parsing hex-decoded bytes as ed25519 private key")]
76 Parse(#[source] commonware_codec::Error),
77 #[error("failed reading file")]
78 Read(#[source] std::io::Error),
79 #[error("failed writing to file")]
80 Write(#[source] std::io::Error),
81}
82
83#[derive(Clone, Debug, PartialEq, Eq)]
84pub struct SigningShare {
85 inner: Share,
86}
87
88impl SigningShare {
89 pub fn into_inner(self) -> Share {
90 self.inner
91 }
92
93 pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, SigningShareError> {
94 let hex = std::fs::read_to_string(path).map_err(SigningShareErrorKind::Read)?;
95 Self::try_from_hex(&hex)
96 }
97
98 pub fn try_from_hex(hex: &str) -> Result<Self, SigningShareError> {
99 let bytes = const_hex::decode(hex).map_err(SigningShareErrorKind::Hex)?;
100 let inner = Share::decode(&bytes[..]).map_err(SigningShareErrorKind::Parse)?;
101 Ok(Self { inner })
102 }
103
104 pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), SigningShareError> {
105 std::fs::write(path, self.to_string()).map_err(SigningShareErrorKind::Write)?;
106 Ok(())
107 }
108}
109
110impl Display for SigningShare {
111 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112 f.write_str(&const_hex::encode_prefixed(self.inner.encode().as_ref()))
113 }
114}
115
116impl From<Share> for SigningShare {
117 fn from(inner: Share) -> Self {
118 Self { inner }
119 }
120}
121
122#[derive(Debug, thiserror::Error)]
123#[error(transparent)]
124pub struct SigningShareError {
125 #[from]
126 inner: SigningShareErrorKind,
127}
128
129#[derive(Debug, thiserror::Error)]
130enum SigningShareErrorKind {
131 #[error("failed decoding file contents as hex-encoded bytes")]
132 Hex(#[source] const_hex::FromHexError),
133 #[error("failed parsing hex-decoded bytes as bls12381 private share")]
134 Parse(#[source] commonware_codec::Error),
135 #[error("failed reading file")]
136 Read(#[source] std::io::Error),
137 #[error("failed writing to file")]
138 Write(#[source] std::io::Error),
139}