Skip to content

Commit

Permalink
revert celestia-address changes from: Initialize genesis state form g…
Browse files Browse the repository at this point in the history
…enesis files. (Sovereign-Labs#972)
  • Loading branch information
zvolin committed Oct 6, 2023
1 parent ea5a84a commit c3ef44d
Showing 1 changed file with 4 additions and 59 deletions.
63 changes: 4 additions & 59 deletions adapters/celestia/src/verifier/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;

use bech32::WriteBase32;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Human Readable Part: "celestia" for Celestia network
Expand All @@ -14,41 +15,11 @@ const VARIANT: bech32::Variant = bech32::Variant::Bech32;
/// <https://github.com/celestiaorg/celestia-specs/blob/e59efd63a2165866584833e91e1cb8a6ed8c8203/src/specs/data_structures.md#address>
/// Spec says: "Addresses have a length of 32 bytes.", but in reality it is 32 `u5` elements, which can be compressed as 20 bytes.
/// TODO: Switch to bech32::u5 when it has repr transparent: <https://github.com/Sovereign-Labs/sovereign-sdk/issues/646>
#[derive(Debug, PartialEq, Clone, Eq, BorshDeserialize, BorshSerialize, Hash)]
#[derive(
Debug, PartialEq, Clone, Eq, Serialize, Deserialize, BorshDeserialize, BorshSerialize, Hash,
)]
pub struct CelestiaAddress([u8; 32]);

impl serde::Serialize for CelestiaAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
let serialized = format!("{}", &self);
serializer.serialize_str(&serialized)
} else {
serde::Serialize::serialize(&self.0, serializer)
}
}
}

impl<'de> serde::Deserialize<'de> for CelestiaAddress {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
if deserializer.is_human_readable() {
let address_bech32: String = serde::Deserialize::deserialize(deserializer)?;
address_bech32
.as_bytes()
.try_into()
.map_err(serde::de::Error::custom)
} else {
let addr = <[u8; 32] as serde::Deserialize>::deserialize(deserializer)?;
Ok(CelestiaAddress(addr))
}
}
}

impl AsRef<[u8]> for CelestiaAddress {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
Expand Down Expand Up @@ -209,32 +180,6 @@ mod tests {
assert_eq!(address_str2, address_str);
}

#[test]
fn test_human_readable_address_serialization() {
let address = CelestiaAddress([11; 32]);
let data: String = serde_json::to_string(&address).unwrap();
let deserialized_address = serde_json::from_str::<CelestiaAddress>(&data).unwrap();

assert_eq!(address, deserialized_address);
assert_eq!(
deserialized_address.to_string(),
"celestia1tttttttttttttttttttttttttttttttt9grhcq"
);
}

#[test]
fn test_binary_address_serialization() {
let address = CelestiaAddress([11; 32]);
let data = postcard::to_allocvec(&address).unwrap();
let deserialized_address = postcard::from_bytes::<CelestiaAddress>(&data).unwrap();

assert_eq!(address, deserialized_address);
assert_eq!(
deserialized_address.to_string(),
"celestia1tttttttttttttttttttttttttttttttt9grhcq"
);
}

proptest! {
#[test]
fn test_try_from_any_slice(input in prop::collection::vec(any::<u8>(), 0..100)) {
Expand Down

0 comments on commit c3ef44d

Please sign in to comment.