Skip to content

Commit

Permalink
Use addr_validate import
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Mar 2, 2021
1 parent 87dd82d commit 81fd75c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ extern "C" {
#[cfg(feature = "iterator")]
fn db_next(iterator_id: u32) -> u32;

fn addr_validate(source_ptr: u32) -> u32;
fn addr_canonicalize(source: u32, destination: u32) -> u32;
fn addr_humanize(source: u32, destination: u32) -> u32;

Expand Down
17 changes: 17 additions & 0 deletions packages/std/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern "C" {
#[cfg(feature = "iterator")]
fn db_next(iterator_id: u32) -> u32;

fn addr_validate(source_ptr: u32) -> u32;
fn addr_canonicalize(source_ptr: u32, destination_ptr: u32) -> u32;
fn addr_humanize(source_ptr: u32, destination_ptr: u32) -> u32;

Expand Down Expand Up @@ -155,6 +156,22 @@ impl ExternalApi {
}

impl Api for ExternalApi {
fn addr_validate(&self, human: &str) -> StdResult<HumanAddr> {
let source = build_region(human.as_bytes());
let source_ptr = &*source as *const Region as u32;

let result = unsafe { addr_validate(source_ptr) };
if result != 0 {
let error = unsafe { consume_string_region_written_by_vm(result as *mut Region) };
return Err(StdError::generic_err(format!(
"addr_validate errored: {}",
error
)));
}

Ok(human.into())
}

fn addr_canonicalize(&self, human: &str) -> StdResult<CanonicalAddr> {
let send = build_region(human.as_bytes());
let send_ptr = &*send as *const Region as u32;
Expand Down
5 changes: 5 additions & 0 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ impl Default for MockApi {
}

impl Api for MockApi {
fn addr_validate(&self, human: &str) -> StdResult<HumanAddr> {
self.addr_canonicalize(human).map(|_canonical| ())?;
Ok(human.into())
}

fn addr_canonicalize(&self, human: &str) -> StdResult<CanonicalAddr> {
// Dummy input validation. This is more sophisticated for formats like bech32, where format and checksum are validated.
if human.len() < 3 {
Expand Down
5 changes: 1 addition & 4 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ pub trait Api {
/// let validated: HumanAddr = api.addr_validate(input).unwrap();
/// assert_eq!(validated, input);
/// ```
fn addr_validate(&self, human: &str) -> StdResult<HumanAddr> {
self.addr_canonicalize(human).map(|_canonical| ())?;
Ok(human.into())
}
fn addr_validate(&self, human: &str) -> StdResult<HumanAddr>;

/// Takes a human readable address and returns a canonical binary representation of it.
/// This can be used when a compact fixed length representation is needed.
Expand Down

0 comments on commit 81fd75c

Please sign in to comment.