forked from informalsystems/tendermint-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add base64 `tendermint::hash::Hash` encoding/decoding support - Add base64 `Option<tendermint::hash::Hash>` encoding/decoding support - Add missing `FromStr` import - Rename `hash_base64` serializer to `tx_hash_base64` Relates informalsystems#942 Links informalsystems#832
- Loading branch information
Showing
8 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
.changelog/unreleased/bug-fixes/832-block-by-hash-encoding.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- `[tendermint-rpc]` The encoding of the `hash` field for requests to the `/block_by_hash` | ||
endpoint has been changed to base64 (from hex) to accommodate discrepancies in | ||
how the Tendermint RPC encodes this field for different RPC interfaces | ||
([#942](https://github.com/informalsystems/tendermint-rs/issues/942)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Encoding/decoding Option Tendermint hashes to/from base64. | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use tendermint::hash::Hash; | ||
|
||
use crate::prelude::*; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct Helper(#[serde(with = "crate::serializers::tm_hash_base64")] Hash); | ||
|
||
/// Deserialize base64-encoded string into an Option<tendermint::Hash> | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Hash>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let helper: Option<Helper> = Option::deserialize(deserializer)?; | ||
Ok(helper.map(|Helper(hash)| hash)) | ||
} | ||
|
||
/// Serialize from an Option<tendermint::Hash> into a base64-encoded string | ||
pub fn serialize<S>(value: &Option<Hash>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
value.map(Helper).serialize(serializer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Encoding/decoding Tendermint hashes to/from base64. | ||
|
||
use serde::{Deserialize, Deserializer, Serializer}; | ||
use subtle_encoding::base64; | ||
use tendermint::hash::{Algorithm::Sha256, Hash, SHA256_HASH_SIZE}; | ||
|
||
use crate::prelude::*; | ||
|
||
/// Deserialize a base64-encoded string into an tendermint::Hash | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Hash, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s = Option::<String>::deserialize(deserializer)?.unwrap_or_default(); | ||
let decoded = base64::decode(&s).map_err(serde::de::Error::custom)?; | ||
if decoded.len() != SHA256_HASH_SIZE { | ||
return Err(serde::de::Error::custom( | ||
"unexpected transaction length for hash", | ||
)); | ||
} | ||
let mut decoded_bytes = [0u8; SHA256_HASH_SIZE]; | ||
decoded_bytes.copy_from_slice(decoded.as_ref()); | ||
Ok(Hash::from_bytes(Sha256, &decoded_bytes).map_err(serde::de::Error::custom)?) | ||
} | ||
|
||
/// Serialize from a tendermint::Hash into a base64-encoded string | ||
pub fn serialize<S>(value: &Hash, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let base64_bytes = base64::encode(value.as_bytes()); | ||
let base64_string = String::from_utf8(base64_bytes).map_err(serde::ser::Error::custom)?; | ||
serializer.serialize_str(&base64_string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters