Skip to content

Commit b63f951

Browse files
committed
WASM: add bindings for PublicAddress
1 parent 2ab16ac commit b63f951

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ironfish-rust-wasm/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ ironfish-jubjub = "0.1.0"
2222
ironfish_zkp = { version = "0.2.0", path = "../ironfish-zkp" }
2323
rand = "0.8.5"
2424
wasm-bindgen = "0.2.95"
25+
26+
[dev-dependencies]
27+
hex-literal = "0.4.1"

ironfish-rust-wasm/src/keys/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
pub mod public_address;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
use crate::errors::IronfishError;
6+
use wasm_bindgen::prelude::*;
7+
8+
#[wasm_bindgen]
9+
#[derive(Clone, PartialEq, Eq, Debug)]
10+
pub struct PublicAddress(ironfish::PublicAddress);
11+
12+
#[wasm_bindgen]
13+
impl PublicAddress {
14+
#[wasm_bindgen(constructor)]
15+
pub fn deserialize(bytes: &[u8]) -> Result<PublicAddress, IronfishError> {
16+
Ok(Self(ironfish::PublicAddress::read(bytes)?))
17+
}
18+
19+
#[wasm_bindgen]
20+
pub fn serialize(&self) -> Vec<u8> {
21+
self.0.public_address().to_vec()
22+
}
23+
24+
#[wasm_bindgen(getter)]
25+
pub fn bytes(&self) -> Vec<u8> {
26+
self.0.public_address().to_vec()
27+
}
28+
29+
#[wasm_bindgen(getter)]
30+
pub fn hex(&self) -> String {
31+
self.0.hex_public_address()
32+
}
33+
}
34+
35+
impl From<ironfish::PublicAddress> for PublicAddress {
36+
fn from(d: ironfish::PublicAddress) -> Self {
37+
Self(d)
38+
}
39+
}
40+
41+
impl AsRef<ironfish::PublicAddress> for PublicAddress {
42+
fn as_ref(&self) -> &ironfish::PublicAddress {
43+
&self.0
44+
}
45+
}
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use crate::keys::public_address::PublicAddress;
50+
use hex_literal::hex;
51+
52+
#[test]
53+
fn valid_address() {
54+
let bytes = hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0");
55+
let addr = PublicAddress::deserialize(&bytes[..])
56+
.expect("valid address deserialization should have succeeded");
57+
assert_eq!(addr.serialize(), bytes);
58+
assert_eq!(addr.bytes(), bytes);
59+
assert_eq!(
60+
addr.hex(),
61+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"
62+
);
63+
}
64+
65+
#[test]
66+
fn invalid_address() {
67+
let bytes = hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1");
68+
PublicAddress::deserialize(&bytes[..])
69+
.expect_err("invalid address deserialization should have failed");
70+
}
71+
}

ironfish-rust-wasm/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
15
#![warn(clippy::dbg_macro)]
26
#![warn(clippy::print_stderr)]
37
#![warn(clippy::print_stdout)]
@@ -10,4 +14,5 @@
1014
use getrandom as _;
1115

1216
pub mod errors;
17+
pub mod keys;
1318
pub mod primitives;

ironfish-rust/src/keys/public_address.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ impl PublicAddress {
3838
}
3939

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

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

103+
impl Eq for PublicAddress {}
104+
103105
#[cfg(test)]
104106
mod test {
105107
use crate::{

0 commit comments

Comments
 (0)