Skip to content

Commit

Permalink
cosmwasm: accounting: Use hex-encoding for TokenAddress
Browse files Browse the repository at this point in the history
Use hex rather than base64 when serializing / deserializing
`TokenAddress` so that it's more consistent with the other address
types.
  • Loading branch information
jynnantonix authored and evan-gray committed Jan 23, 2023
1 parent 1e3356b commit 1f8055a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 48 deletions.
1 change: 0 additions & 1 deletion cosmwasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions cosmwasm/packages/accounting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ library = []

[dependencies]
anyhow = "1"
base64 = "0.13"
cosmwasm-schema = "1"
cosmwasm-std = "1"
cw-storage-plus = "0.13.2"
cw_transcode = "0.1.0"
hex = "0.4.3"
hex = { version = "0.4.3", features = ["serde"] }
schemars = "0.8.8"
serde = { version = "1.0.137", default-features = false }
thiserror = "1"
Expand Down
53 changes: 8 additions & 45 deletions cosmwasm/packages/accounting/src/state/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ use std::{
};

use anyhow::{anyhow, Context};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{StdError, StdResult};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use schemars::JsonSchema;
use serde::{de, Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, JsonSchema)]
#[cw_serde]
#[derive(Copy, Default, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct TokenAddress(#[schemars(with = "String")] [u8; 32]);
pub struct TokenAddress(
#[serde(with = "hex")]
#[schemars(with = "String")]
[u8; 32],
);

impl TokenAddress {
pub const fn new(addr: [u8; 32]) -> TokenAddress {
Expand Down Expand Up @@ -93,47 +97,6 @@ impl FromStr for TokenAddress {
}
}

impl Serialize for TokenAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&base64::encode(self.0))
}
}

impl<'de> Deserialize<'de> for TokenAddress {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_str(Base64Visitor)
}
}

struct Base64Visitor;

impl<'de> de::Visitor<'de> for Base64Visitor {
type Value = TokenAddress;

fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("a valid base64 encoded string of a 32-byte array")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
base64::decode(v)
.map_err(E::custom)
.and_then(|b| {
b.try_into()
.map_err(|b: Vec<u8>| E::invalid_length(b.len(), &self))
})
.map(TokenAddress)
}
}

impl KeyDeserialize for TokenAddress {
type Output = Self;

Expand Down

0 comments on commit 1f8055a

Please sign in to comment.