Skip to content

Commit

Permalink
WASM: add bindings for PublicAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
andiflabs committed Nov 13, 2024
1 parent 301aa74 commit 53abc14
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ironfish-rust-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ ironfish-jubjub = "0.1.0"
ironfish_zkp = { version = "0.2.0", path = "../ironfish-zkp" }
rand = "0.8.5"
wasm-bindgen = "0.2.95"

[dev-dependencies]
hex-literal = "0.4.1"
1 change: 1 addition & 0 deletions ironfish-rust-wasm/src/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod public_address;
67 changes: 67 additions & 0 deletions ironfish-rust-wasm/src/keys/public_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::errors::IronfishError;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PublicAddress(ironfish::PublicAddress);

#[wasm_bindgen]
impl PublicAddress {
#[wasm_bindgen(constructor)]
pub fn deserialize(bytes: &[u8]) -> Result<PublicAddress, IronfishError> {
Ok(Self(ironfish::PublicAddress::read(bytes)?))
}

#[wasm_bindgen]
pub fn serialize(&self) -> Vec<u8> {
self.0.public_address().to_vec()
}

#[wasm_bindgen(getter)]
pub fn bytes(&self) -> Vec<u8> {
self.0.public_address().to_vec()
}

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

impl From<ironfish::PublicAddress> for PublicAddress {
fn from(d: ironfish::PublicAddress) -> Self {
Self(d)
}
}

impl AsRef<ironfish::PublicAddress> for PublicAddress {
fn as_ref(&self) -> &ironfish::PublicAddress {
&self.0
}
}

#[cfg(test)]
mod tests {
use crate::keys::public_address::PublicAddress;
use hex_literal::hex;

#[test]
fn 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"
);
}

#[test]
fn invalid_address() {
let bytes = hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1");
PublicAddress::deserialize(&bytes[..])
.expect_err("invalid address deserialization should have failed");
}
}
1 change: 1 addition & 0 deletions ironfish-rust-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
use getrandom as _;

pub mod errors;
pub mod keys;
pub mod primitives;
6 changes: 4 additions & 2 deletions ironfish-rust/src/keys/public_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ impl PublicAddress {
}

/// Load a public address from a Read implementation (e.g: socket, file)
pub fn read<R: io::Read>(reader: &mut R) -> Result<Self, IronfishError> {
pub fn read<R: io::Read>(mut reader: R) -> Result<Self, IronfishError> {
let mut address_bytes = [0; PUBLIC_ADDRESS_SIZE];
reader.read_exact(&mut address_bytes)?;
Self::new(&address_bytes)
}

pub fn read_unchecked<R: io::Read>(reader: &mut R) -> Result<Self, IronfishError> {
pub fn read_unchecked<R: io::Read>(mut reader: R) -> Result<Self, IronfishError> {
let mut address_bytes = [0; PUBLIC_ADDRESS_SIZE];
reader.read_exact(&mut address_bytes)?;
Self::new_unchecked(&address_bytes)
Expand Down Expand Up @@ -100,6 +100,8 @@ impl PartialEq for PublicAddress {
}
}

impl Eq for PublicAddress {}

#[cfg(test)]
mod test {
use crate::{
Expand Down

0 comments on commit 53abc14

Please sign in to comment.