Skip to content

Commit

Permalink
WASM: add convenience methods to PublicAddress to work with hex
Browse files Browse the repository at this point in the history
Added methods to convert a hex string to a `PublicAddress`, as well as a
method to check if a hex string is a valid address.

Removed the `bytes()` method, which is redundant and does the same job
as `serialize()`.
  • Loading branch information
andiflabs committed Dec 18, 2024
1 parent befe45f commit c713738
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions ironfish-rust-wasm/src/keys/public_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ wasm_bindgen_wrapper! {
#[wasm_bindgen]
impl PublicAddress {
#[wasm_bindgen(constructor)]
pub fn deserialize(bytes: &[u8]) -> Result<PublicAddress, IronfishError> {
pub fn deserialize(bytes: &[u8]) -> Result<Self, IronfishError> {
Ok(Self(ironfish::PublicAddress::read(bytes)?))
}

Expand All @@ -22,15 +22,20 @@ impl PublicAddress {
self.0.public_address().to_vec()
}

#[wasm_bindgen(getter)]
pub fn bytes(&self) -> Vec<u8> {
self.0.public_address().to_vec()
#[wasm_bindgen(js_name = "fromHex")]
pub fn from_hex(hex: &str) -> Result<Self, IronfishError> {
Ok(Self(ironfish::PublicAddress::from_hex(hex)?))
}

#[wasm_bindgen(getter)]
pub fn hex(&self) -> String {
self.0.hex_public_address()
}

#[wasm_bindgen(js_name = "isValid")]
pub fn is_valid(hex: &str) -> bool {
Self::from_hex(hex).is_ok()
}
}

#[cfg(test)]
Expand All @@ -41,12 +46,11 @@ mod tests {

#[test]
#[wasm_bindgen_test]
fn valid_address() {
fn deserialize_valid_address() {
let bytes = hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0");
let addr = PublicAddress::deserialize(&bytes[..])
.expect("valid address deserialization should have succeeded");
assert_eq!(addr.serialize(), bytes);
assert_eq!(addr.bytes(), bytes);
assert_eq!(
addr.hex(),
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"
Expand All @@ -55,9 +59,41 @@ mod tests {

#[test]
#[wasm_bindgen_test]
fn invalid_address() {
fn deserialize_invalid_address() {
let bytes = hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1");
PublicAddress::deserialize(&bytes[..])
.expect_err("invalid address deserialization should have failed");
}

#[test]
#[wasm_bindgen_test]
fn from_hex_valid_address() {
let hex = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0";
let addr = PublicAddress::from_hex(hex)
.expect("valid address deserialization should have succeeded");
assert_eq!(addr.hex(), hex);
assert_eq!(
addr.hex(),
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"
);
}

#[test]
#[wasm_bindgen_test]
fn from_hex_invalid_address() {
let hex = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1";
PublicAddress::from_hex(hex)
.expect_err("invalid address deserialization should have failed");
}

#[test]
#[wasm_bindgen_test]
fn is_valid() {
assert!(PublicAddress::is_valid(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"
));
assert!(!PublicAddress::is_valid(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1"
));
}
}

0 comments on commit c713738

Please sign in to comment.