-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WASM: add bindings for
PublicAddress
- Loading branch information
Showing
5 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod public_address; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ | |
use getrandom as _; | ||
|
||
pub mod errors; | ||
pub mod keys; | ||
pub mod primitives; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters