Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM: add convenience methods to PublicAddress to work with hex #5699

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
));
}
}