Skip to content

Commit 89b69ef

Browse files
committed
Use riffle_shuffle in std's MockApi
1 parent 2f5b15e commit 89b69ef

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

packages/std/src/mock.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ impl Api for MockApi {
8888
}
8989

9090
let mut out = Vec::from(human.as_str());
91-
let append = self.canonical_length - out.len();
92-
if append > 0 {
93-
out.extend(vec![0u8; append]);
91+
// pad to canonical_length wil NULL bytes
92+
out.resize(self.canonical_length, 0x00);
93+
// shuffle to destroy the most obvious structure (https://github.com/CosmWasm/cosmwasm/issues/552)
94+
for _ in 0..18 {
95+
out = riffle_shuffle(&out);
9496
}
95-
Ok(CanonicalAddr(Binary(out)))
97+
Ok(out.into())
9698
}
9799

98100
fn human_address(&self, canonical: &CanonicalAddr) -> StdResult<HumanAddr> {
@@ -102,16 +104,16 @@ impl Api for MockApi {
102104
));
103105
}
104106

105-
// remove trailing 0's (TODO: fix this - but fine for first tests)
106-
let trimmed: Vec<u8> = canonical
107-
.as_slice()
108-
.iter()
109-
.cloned()
110-
.filter(|&x| x != 0)
111-
.collect();
107+
let mut tmp: Vec<u8> = canonical.clone().into();
108+
// Shuffle two more times which restored the original value (24 elements are back to original after 20 rounds)
109+
for _ in 0..2 {
110+
tmp = riffle_shuffle(&tmp);
111+
}
112+
// Remove NULL bytes (i.e. the padding)
113+
let trimmed = tmp.into_iter().filter(|&x| x != 0x00).collect();
112114
// decode UTF-8 bytes into string
113115
let human = String::from_utf8(trimmed).map_err(StdError::invalid_utf8)?;
114-
Ok(HumanAddr(human))
116+
Ok(human.into())
115117
}
116118

117119
fn debug(&self, message: &str) {

0 commit comments

Comments
 (0)