Skip to content

Commit

Permalink
Use riffle_shuffle in std's MockApi
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 1, 2020
1 parent 2f5b15e commit 89b69ef
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ impl Api for MockApi {
}

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

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

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

fn debug(&self, message: &str) {
Expand Down

0 comments on commit 89b69ef

Please sign in to comment.