Skip to content

Commit 6e0f4a2

Browse files
committed
refactor: use only prism-serde for base64 + hex
1 parent 9e8af18 commit 6e0f4a2

File tree

14 files changed

+181
-136
lines changed

14 files changed

+181
-136
lines changed

Cargo.lock

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ prism-keys.workspace = true
1414
# serde
1515
prism-serde.workspace = true
1616
serde.workspace = true
17-
hex.workspace = true
18-
bincode.workspace = true
1917

2018
# celestia
2119
celestia-types.workspace = true

crates/common/src/digest.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
use anyhow::{anyhow, Result};
1+
use anyhow::Result;
22
use jmt::RootHash;
33
use serde::{Deserialize, Serialize};
44

55
use crate::hasher::Hasher;
6-
use prism_serde::raw_or_hex_fixed;
6+
use prism_serde::{
7+
base64::FromBase64,
8+
hex::{FromHex, ToHex},
9+
raw_or_hex,
10+
};
711

812
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Copy)]
9-
pub struct Digest(#[serde(with = "raw_or_hex_fixed")] pub [u8; 32]);
13+
pub struct Digest(#[serde(with = "raw_or_hex")] pub [u8; 32]);
1014

1115
impl Digest {
16+
pub const fn new(bytes: [u8; 32]) -> Self {
17+
Digest(bytes)
18+
}
19+
1220
pub fn hash(data: impl AsRef<[u8]>) -> Self {
1321
let mut hasher = Hasher::new();
1422
hasher.update(data.as_ref());
@@ -26,6 +34,10 @@ impl Digest {
2634
pub const fn zero() -> Self {
2735
Self([0u8; 32])
2836
}
37+
38+
pub fn to_bytes(&self) -> [u8; 32] {
39+
self.0
40+
}
2941
}
3042

3143
// serializer and deserializer for rocksdb
@@ -58,29 +70,24 @@ impl AsRef<[u8]> for Digest {
5870
}
5971
}
6072

61-
impl std::fmt::Display for Digest {
62-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63-
write!(f, "{}", self.to_hex())
64-
}
65-
}
73+
impl FromHex for Digest {
74+
type Error = anyhow::Error;
6675

67-
impl Digest {
68-
pub const fn new(bytes: [u8; 32]) -> Self {
69-
Digest(bytes)
76+
fn from_hex<T: AsRef<[u8]>>(hex: T) -> std::result::Result<Self, Self::Error> {
77+
Ok(Self(<[u8; 32]>::from_hex(hex)?))
7078
}
79+
}
7180

72-
pub fn from_hex(hex_str: &str) -> Result<Self> {
73-
let mut bytes = [0u8; 32];
74-
hex::decode_to_slice(hex_str, &mut bytes)
75-
.map_err(|e| anyhow!(format!("Invalid Format: {e}")))?;
76-
Ok(Digest(bytes))
77-
}
81+
impl FromBase64 for Digest {
82+
type Error = anyhow::Error;
7883

79-
pub fn to_hex(&self) -> String {
80-
hex::encode(self.0)
84+
fn from_base64<T: AsRef<[u8]>>(base64: T) -> Result<Self, Self::Error> {
85+
Ok(Self(<[u8; 32]>::from_base64(base64)?))
8186
}
87+
}
8288

83-
pub fn to_bytes(&self) -> [u8; 32] {
84-
self.0
89+
impl std::fmt::Display for Digest {
90+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91+
write!(f, "{}", self.to_hex())
8592
}
8693
}

crates/da/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ async-trait = { workspace = true }
1515
serde = { workspace = true }
1616
ed25519-consensus = { workspace = true }
1717
tokio = { workspace = true }
18-
bincode = { workspace = true }
19-
hex = { workspace = true }
2018
log = { workspace = true }
2119
celestia-rpc = { workspace = true }
2220
celestia-types = { workspace = true }

crates/da/src/celestia.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use celestia_types::{nmt::Namespace, Blob, TxConfig};
66
use log::{debug, error, trace, warn};
77
use prism_common::transaction::Transaction;
88
use prism_errors::{DataAvailabilityError, GeneralError};
9-
use prism_serde::binary::{FromBinary, ToBinary};
9+
use prism_serde::{
10+
binary::{FromBinary, ToBinary},
11+
hex::FromHex,
12+
};
1013
use serde::{Deserialize, Serialize};
1114
use std::{
1215
self,
@@ -88,7 +91,7 @@ impl CelestiaConnection {
8891
}
8992

9093
fn create_namespace(namespace_hex: &str) -> Result<Namespace> {
91-
let decoded_hex = hex::decode(namespace_hex).context(format!(
94+
let decoded_hex = Vec::<u8>::from_hex(namespace_hex).context(format!(
9295
"Failed to decode namespace hex '{}'",
9396
namespace_hex
9497
))?;

crates/da/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use anyhow::Result;
22
use async_trait::async_trait;
33
use ed25519_consensus::{Signature, SigningKey, VerificationKey as VerifyingKey};
44
use prism_common::{digest::Digest, transaction::Transaction};
5-
use prism_serde::binary::ToBinary;
5+
use prism_serde::{
6+
binary::ToBinary,
7+
hex::{FromHex, ToHex},
8+
};
69
use serde::{Deserialize, Serialize};
710
use sp1_sdk::SP1ProofWithPublicValues;
811
use tokio::sync::broadcast;
@@ -25,7 +28,7 @@ impl FinalizedEpoch {
2528
pub fn insert_signature(&mut self, key: &SigningKey) {
2629
let plaintext = self.encode_to_bytes().unwrap();
2730
let signature = key.sign(&plaintext);
28-
self.signature = Some(hex::encode(signature.to_bytes()));
31+
self.signature = Some(signature.to_bytes().to_hex());
2932
}
3033

3134
pub fn verify_signature(&self, vk: VerifyingKey) -> Result<()> {
@@ -44,7 +47,7 @@ impl FinalizedEpoch {
4447
let signature =
4548
self.signature.as_ref().ok_or_else(|| anyhow::anyhow!("No signature present"))?;
4649

47-
let signature_bytes = hex::decode(signature)
50+
let signature_bytes = Vec::<u8>::from_hex(signature)
4851
.map_err(|e| anyhow::anyhow!("Failed to decode signature: {}", e))?;
4952

5053
if signature_bytes.len() != 64 {

crates/keys/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ pub use verifying_keys::*;
99
#[cfg(test)]
1010
mod tests {
1111
use super::*;
12-
use base64::{engine::general_purpose::STANDARD as engine, Engine as _};
1312
use ed25519_consensus::SigningKey as Ed25519SigningKey;
13+
use prism_serde::base64::ToBase64;
1414
use rand::rngs::OsRng;
1515
use secp256k1::SecretKey as Secp256k1SigningKey;
1616

@@ -101,7 +101,7 @@ mod tests {
101101
fn test_verifying_key_from_string_ed25519() {
102102
let original_key: VerifyingKey =
103103
SigningKey::Ed25519(Box::new(Ed25519SigningKey::new(OsRng))).into();
104-
let encoded = engine.encode(original_key.to_bytes());
104+
let encoded = original_key.to_bytes().to_base64();
105105

106106
let result = VerifyingKey::try_from(encoded);
107107
assert!(result.is_ok());
@@ -114,7 +114,7 @@ mod tests {
114114
fn test_verifying_key_from_string_secp256k1() {
115115
let original_key: VerifyingKey =
116116
SigningKey::Secp256k1(Secp256k1SigningKey::new(&mut OsRng)).into();
117-
let encoded = engine.encode(original_key.to_bytes());
117+
let encoded = original_key.to_bytes().to_base64();
118118

119119
let result = VerifyingKey::try_from(encoded);
120120
assert!(result.is_ok());
@@ -126,7 +126,7 @@ mod tests {
126126
#[test]
127127
fn test_verifying_key_from_string_invalid_length() {
128128
let invalid_bytes: [u8; 31] = [1; 31];
129-
let encoded = engine.encode(invalid_bytes);
129+
let encoded = invalid_bytes.to_base64();
130130

131131
let result = VerifyingKey::try_from(encoded);
132132
assert!(result.is_err());

crates/keys/src/verifying_keys.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::{anyhow, bail, Result};
2-
use base64::{engine::general_purpose::STANDARD as engine, Engine as _};
32
use ed25519_consensus::{SigningKey as Ed25519SigningKey, VerificationKey as Ed25519VerifyingKey};
43
use p256::ecdsa::{
54
signature::DigestVerifier, SigningKey as Secp256r1SigningKey,
@@ -18,7 +17,10 @@ use std::{
1817
};
1918

2019
use crate::{Signature, SigningKey};
21-
use prism_serde::CryptoPayload;
20+
use prism_serde::{
21+
base64::{FromBase64, ToBase64},
22+
CryptoPayload,
23+
};
2224

2325
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
2426
#[serde(try_from = "CryptoPayload", into = "CryptoPayload")]
@@ -162,7 +164,7 @@ impl From<Ed25519SigningKey> for VerifyingKey {
162164

163165
impl From<Secp256k1SigningKey> for VerifyingKey {
164166
fn from(sk: Secp256k1SigningKey) -> Self {
165-
sk.public_key(SECP256K1).into()
167+
VerifyingKey::Secp256k1(sk.public_key(SECP256K1))
166168
}
167169
}
168170

@@ -182,14 +184,14 @@ impl From<SigningKey> for VerifyingKey {
182184
}
183185
}
184186

185-
impl TryFrom<String> for VerifyingKey {
187+
impl FromBase64 for VerifyingKey {
186188
type Error = anyhow::Error;
187189

188190
/// Attempts to create a `VerifyingKey` from a base64-encoded string.
189191
///
190192
/// # Arguments
191193
///
192-
/// * `s` - The base64-encoded string representation of the public key.
194+
/// * `base64` - The base64-encoded string representation of the public key.
193195
///
194196
/// Depending on the length of the input string, the function will attempt to
195197
/// decode it and create a `VerifyingKey` instance. According to the specifications,
@@ -200,9 +202,8 @@ impl TryFrom<String> for VerifyingKey {
200202
///
201203
/// * `Ok(VerifyingKey)` if the conversion was successful.
202204
/// * `Err` if the input is invalid or the conversion failed.
203-
fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
204-
let bytes =
205-
engine.decode(s).map_err(|e| anyhow!("Failed to decode base64 string: {}", e))?;
205+
fn from_base64<T: AsRef<[u8]>>(base64: T) -> Result<Self, Self::Error> {
206+
let bytes = Vec::<u8>::from_base64(base64)?;
206207

207208
match bytes.len() {
208209
32 => {
@@ -220,9 +221,17 @@ impl TryFrom<String> for VerifyingKey {
220221
}
221222
}
222223

224+
impl TryFrom<String> for VerifyingKey {
225+
type Error = anyhow::Error;
226+
227+
fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
228+
Self::from_base64(s)
229+
}
230+
}
231+
223232
impl std::fmt::Display for VerifyingKey {
224233
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
225-
let encoded = engine.encode(self.to_bytes());
234+
let encoded = self.to_bytes().to_base64();
226235
write!(f, "{}", encoded)
227236
}
228237
}

crates/serde/src/base64.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use anyhow::Result;
2+
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
3+
4+
pub trait ToBase64 {
5+
fn to_base64(&self) -> String;
6+
}
7+
8+
impl<T> ToBase64 for T
9+
where
10+
T: AsRef<[u8]>,
11+
{
12+
fn to_base64(&self) -> String {
13+
BASE64.encode(self.as_ref())
14+
}
15+
}
16+
17+
pub trait FromBase64: Sized {
18+
type Error;
19+
20+
fn from_base64<T: AsRef<[u8]>>(base64: T) -> Result<Self, Self::Error>;
21+
}
22+
23+
impl FromBase64 for Vec<u8> {
24+
type Error = anyhow::Error;
25+
26+
fn from_base64<T: AsRef<[u8]>>(base64: T) -> Result<Self> {
27+
BASE64.decode(base64.as_ref()).map_err(|e| e.into())
28+
}
29+
}
30+
31+
impl FromBase64 for [u8; 32] {
32+
type Error = anyhow::Error;
33+
34+
fn from_base64<T: AsRef<[u8]>>(base64: T) -> Result<Self> {
35+
let mut output = [0u8; 32];
36+
BASE64.decode_slice(base64, &mut output)?;
37+
Ok(output)
38+
}
39+
}

crates/serde/src/hex.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use anyhow::Result;
2+
use std::error::Error as StdError;
3+
4+
pub trait ToHex {
5+
fn to_hex(&self) -> String;
6+
}
7+
8+
impl<T> ToHex for T
9+
where
10+
T: hex::ToHex,
11+
{
12+
fn to_hex(&self) -> String {
13+
self.encode_hex()
14+
}
15+
}
16+
17+
pub trait FromHex: Sized {
18+
type Error;
19+
20+
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error>;
21+
}
22+
23+
impl<T> FromHex for T
24+
where
25+
T: hex::FromHex,
26+
T::Error: StdError + Send + Into<anyhow::Error>,
27+
{
28+
type Error = anyhow::Error;
29+
30+
fn from_hex<U: AsRef<[u8]>>(hex: U) -> Result<Self, Self::Error> {
31+
T::from_hex(hex).map_err(|e| e.into())
32+
}
33+
}

0 commit comments

Comments
 (0)