Skip to content

Commit 89c5dbe

Browse files
committed
Use Checksum type in CodeInfoResponse
1 parent a2ca7ba commit 89c5dbe

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

packages/std/src/checksum.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::fmt;
22

3+
use schemars::JsonSchema;
4+
use serde::{de, ser, Deserialize, Deserializer, Serialize};
35
use sha2::{Digest, Sha256};
46
use thiserror::Error;
57

@@ -10,8 +12,8 @@ use crate::{StdError, StdResult};
1012
///
1113
/// This is often referred to as "code ID" in go-cosmwasm, even if code ID
1214
/// usually refers to an auto-incrementing number.
13-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
14-
pub struct Checksum([u8; 32]);
15+
#[derive(JsonSchema, Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
pub struct Checksum(#[schemars(with = "String")] [u8; 32]);
1517

1618
impl Checksum {
1719
pub fn generate(wasm: &[u8]) -> Self {
@@ -50,6 +52,46 @@ impl From<[u8; 32]> for Checksum {
5052
}
5153
}
5254

55+
/// Serializes as a hex string
56+
impl Serialize for Checksum {
57+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58+
where
59+
S: ser::Serializer,
60+
{
61+
serializer.serialize_str(&self.to_hex())
62+
}
63+
}
64+
65+
/// Deserializes as a hex string
66+
impl<'de> Deserialize<'de> for Checksum {
67+
fn deserialize<D>(deserializer: D) -> Result<Checksum, D::Error>
68+
where
69+
D: Deserializer<'de>,
70+
{
71+
deserializer.deserialize_str(ChecksumVisitor)
72+
}
73+
}
74+
75+
struct ChecksumVisitor;
76+
77+
impl<'de> de::Visitor<'de> for ChecksumVisitor {
78+
type Value = Checksum;
79+
80+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
81+
formatter.write_str("valid hex encoded 32 byte checksum")
82+
}
83+
84+
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
85+
where
86+
E: de::Error,
87+
{
88+
match Checksum::from_hex(v) {
89+
Ok(data) => Ok(data),
90+
Err(_) => Err(E::custom(format!("invalid checksum: {v}"))),
91+
}
92+
}
93+
}
94+
5395
#[derive(Error, Debug)]
5496
#[error("Checksum not of length 32")]
5597
pub struct ChecksumError;

packages/std/src/query/wasm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

4-
use crate::{Addr, Binary, HexBinary};
4+
use crate::{Addr, Binary, Checksum};
55

66
use super::query_response::QueryResponseType;
77

@@ -70,14 +70,14 @@ pub struct CodeInfoResponse {
7070
/// The address that initially stored the code
7171
pub creator: Addr,
7272
/// The hash of the Wasm blob
73-
pub checksum: HexBinary,
73+
pub checksum: Checksum,
7474
}
7575

7676
impl_response_constructor!(
7777
CodeInfoResponse,
7878
code_id: u64,
7979
creator: Addr,
80-
checksum: HexBinary
80+
checksum: Checksum
8181
);
8282

8383
impl QueryResponseType for CodeInfoResponse {}
@@ -129,12 +129,12 @@ mod tests {
129129
#[test]
130130
#[cfg(feature = "cosmwasm_1_2")]
131131
fn code_info_response_serialization() {
132-
use crate::HexBinary;
132+
use crate::Checksum;
133133

134134
let response = CodeInfoResponse {
135135
code_id: 67,
136136
creator: Addr::unchecked("jane"),
137-
checksum: HexBinary::from_hex(
137+
checksum: Checksum::from_hex(
138138
"f7bb7b18fb01bbf425cf4ed2cd4b7fb26a019a7fc75a4dc87e8a0b768c501f00",
139139
)
140140
.unwrap(),

packages/std/src/testing/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,13 +2234,13 @@ mod tests {
22342234
}
22352235
#[cfg(feature = "cosmwasm_1_2")]
22362236
WasmQuery::CodeInfo { code_id } => {
2237-
use crate::{CodeInfoResponse, HexBinary};
2237+
use crate::{Checksum, CodeInfoResponse};
22382238
let code_id = *code_id;
22392239
if code_id == 4 {
22402240
let response = CodeInfoResponse {
22412241
code_id,
22422242
creator: Addr::unchecked("lalala"),
2243-
checksum: HexBinary::from_hex(
2243+
checksum: Checksum::from_hex(
22442244
"84cf20810fd429caf58898c3210fcb71759a27becddae08dbde8668ea2f4725d",
22452245
)
22462246
.unwrap(),

0 commit comments

Comments
 (0)