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 decryption methods to MerkleNote #5681

Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions ironfish-rust-wasm/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ impl AssetIdentifier {
self.0.as_bytes().to_vec()
}

#[wasm_bindgen(getter)]
pub fn native() -> Self {
Self(ironfish::assets::asset_identifier::NATIVE_ASSET)
}

#[wasm_bindgen(getter, js_name = assetGenerator)]
pub fn asset_generator(&self) -> ExtendedPoint {
self.0.asset_generator().into()
Expand Down
6 changes: 6 additions & 0 deletions ironfish-rust-wasm/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
* 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/. */

mod proof_generation_key;
mod public_address;
mod sapling_key;
mod view_keys;

pub use proof_generation_key::ProofGenerationKey;
pub use public_address::PublicAddress;
pub use sapling_key::SaplingKey;
pub use view_keys::{IncomingViewKey, OutgoingViewKey, ViewKey};
36 changes: 36 additions & 0 deletions ironfish-rust-wasm/src/keys/proof_generation_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* 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,
primitives::{Fr, SubgroupPoint},
wasm_bindgen_wrapper,
};
use wasm_bindgen::prelude::*;

wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ProofGenerationKey(ironfish::keys::ProofGenerationKey);
}

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

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

#[wasm_bindgen(js_name = fromParts)]
pub fn from_parts(ak: SubgroupPoint, nsk: Fr) -> Self {
Self(ironfish::keys::ProofGenerationKey::new(
ak.into(),
nsk.into(),
))
}
}
105 changes: 105 additions & 0 deletions ironfish-rust-wasm/src/keys/sapling_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* 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,
keys::{IncomingViewKey, OutgoingViewKey, ProofGenerationKey, PublicAddress, ViewKey},
wasm_bindgen_wrapper,
};
use wasm_bindgen::prelude::*;

wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SaplingKey(ironfish::keys::SaplingKey);
}

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

#[wasm_bindgen]
pub fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.0
.write(&mut buf)
.expect("failed to serialize sapling key");
buf
}

#[wasm_bindgen]
pub fn random() -> Self {
Self(ironfish::keys::SaplingKey::generate_key())
}

#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: &str) -> Result<Self, IronfishError> {
Ok(Self(ironfish::keys::SaplingKey::from_hex(hex)?))
}

#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.hex_spending_key()
}

// TODO: to/fromWords

#[wasm_bindgen(getter, js_name = publicAddress)]
pub fn public_address(&self) -> PublicAddress {
self.0.public_address().into()
}

#[wasm_bindgen(getter, js_name = spendingKey)]
pub fn spending_key(&self) -> Vec<u8> {
self.0.spending_key().to_vec()
}

#[wasm_bindgen(getter, js_name = incomingViewKey)]
pub fn incoming_view_key(&self) -> IncomingViewKey {
self.0.incoming_view_key().to_owned().into()
}

#[wasm_bindgen(getter, js_name = outgoingViewKey)]
pub fn outgoing_view_key(&self) -> OutgoingViewKey {
self.0.outgoing_view_key().to_owned().into()
}

#[wasm_bindgen(getter, js_name = viewKey)]
pub fn view_key(&self) -> ViewKey {
self.0.view_key().to_owned().into()
}

#[wasm_bindgen(getter, js_name = proofGenerationKey)]
pub fn proof_generation_key(&self) -> ProofGenerationKey {
self.0.sapling_proof_generation_key().into()
}
}

#[cfg(test)]
mod tests {
use crate::keys::{IncomingViewKey, OutgoingViewKey, ProofGenerationKey, SaplingKey, ViewKey};
use wasm_bindgen_test::wasm_bindgen_test;

macro_rules! assert_serde_ok {
( $type:ty, $key:expr ) => {
assert_eq!(
$key,
<$type>::deserialize($key.serialize().as_slice()).expect("deserialization failed")
)
};
}

#[test]
#[wasm_bindgen_test]
fn serialization_roundtrip() {
let key = SaplingKey::random();
assert_serde_ok!(SaplingKey, key);
assert_serde_ok!(IncomingViewKey, key.incoming_view_key());
assert_serde_ok!(OutgoingViewKey, key.outgoing_view_key());
assert_serde_ok!(ViewKey, key.view_key());
assert_serde_ok!(ProofGenerationKey, key.proof_generation_key());
}
}
119 changes: 119 additions & 0 deletions ironfish-rust-wasm/src/keys/view_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* 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, keys::PublicAddress, primitives::PublicKey, wasm_bindgen_wrapper,
};
use wasm_bindgen::prelude::*;

wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct IncomingViewKey(ironfish::keys::IncomingViewKey);
}

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

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

#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: &str) -> Result<Self, IronfishError> {
Ok(Self(ironfish::keys::IncomingViewKey::from_hex(hex)?))
}

#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.hex_key()
}

// TODO: to/fromWords

#[wasm_bindgen(getter, js_name = publicAddress)]
pub fn public_address(&self) -> PublicAddress {
self.0.public_address().into()
}
}

wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct OutgoingViewKey(ironfish::keys::OutgoingViewKey);
}

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

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

#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: &str) -> Result<Self, IronfishError> {
Ok(Self(ironfish::keys::OutgoingViewKey::from_hex(hex)?))
}

#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.hex_key()
}

// TODO: to/fromWords
}

wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ViewKey(ironfish::keys::ViewKey);
}

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

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

#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: &str) -> Result<Self, IronfishError> {
Ok(Self(ironfish::keys::ViewKey::from_hex(hex)?))
}

#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.hex_key()
}

#[wasm_bindgen(getter, js_name = publicAddress)]
pub fn public_address(&self) -> Result<PublicAddress, IronfishError> {
self.0
.public_address()
.map(|a| a.into())
.map_err(|e| e.into())
}

#[wasm_bindgen(getter, js_name = authorizingKey)]
pub fn authorizing_key(&self) -> PublicKey {
self.0.authorizing_key.into()
}

#[wasm_bindgen(getter, js_name = nullifierDerivingKey)]
pub fn nullifier_deriving_key(&self) -> PublicKey {
self.0.nullifier_deriving_key.into()
}
}
1 change: 1 addition & 0 deletions ironfish-rust-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod assets;
pub mod errors;
pub mod keys;
pub mod merkle_note;
pub mod note;
pub mod primitives;
pub mod transaction;

Expand Down
Loading