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 2ab16ac commit b63f951
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
5 changes: 5 additions & 0 deletions ironfish-rust-wasm/src/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

pub mod public_address;
71 changes: 71 additions & 0 deletions ironfish-rust-wasm/src/keys/public_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

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");
}
}
5 changes: 5 additions & 0 deletions ironfish-rust-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#![warn(clippy::dbg_macro)]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]
Expand All @@ -10,4 +14,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 b63f951

Please sign in to comment.