cosmwasm: accounting: Use hex-encoding for TokenAddress
Use hex rather than base64 when serializing / deserializing `TokenAddress` so that it's more consistent with the other address types.
This commit is contained in:
parent
1e3356b4c9
commit
1f8055adfa
|
@ -7,7 +7,6 @@ name = "accounting"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
|
||||||
"cosmwasm-schema",
|
"cosmwasm-schema",
|
||||||
"cosmwasm-std",
|
"cosmwasm-std",
|
||||||
"cw-storage-plus",
|
"cw-storage-plus",
|
||||||
|
|
|
@ -15,12 +15,11 @@ library = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
base64 = "0.13"
|
|
||||||
cosmwasm-schema = "1"
|
cosmwasm-schema = "1"
|
||||||
cosmwasm-std = "1"
|
cosmwasm-std = "1"
|
||||||
cw-storage-plus = "0.13.2"
|
cw-storage-plus = "0.13.2"
|
||||||
cw_transcode = "0.1.0"
|
cw_transcode = "0.1.0"
|
||||||
hex = "0.4.3"
|
hex = { version = "0.4.3", features = ["serde"] }
|
||||||
schemars = "0.8.8"
|
schemars = "0.8.8"
|
||||||
serde = { version = "1.0.137", default-features = false }
|
serde = { version = "1.0.137", default-features = false }
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
|
|
|
@ -5,14 +5,18 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::{anyhow, Context};
|
use anyhow::{anyhow, Context};
|
||||||
|
use cosmwasm_schema::cw_serde;
|
||||||
use cosmwasm_std::{StdError, StdResult};
|
use cosmwasm_std::{StdError, StdResult};
|
||||||
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
|
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)]
|
#[repr(transparent)]
|
||||||
pub struct TokenAddress(#[schemars(with = "String")] [u8; 32]);
|
pub struct TokenAddress(
|
||||||
|
#[serde(with = "hex")]
|
||||||
|
#[schemars(with = "String")]
|
||||||
|
[u8; 32],
|
||||||
|
);
|
||||||
|
|
||||||
impl TokenAddress {
|
impl TokenAddress {
|
||||||
pub const fn new(addr: [u8; 32]) -> TokenAddress {
|
pub const fn new(addr: [u8; 32]) -> TokenAddress {
|
||||||
|
@ -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 {
|
impl KeyDeserialize for TokenAddress {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue