Skip to content

Commit

Permalink
WASM: add bindings for EphemeralKeyPair
Browse files Browse the repository at this point in the history
  • Loading branch information
andiflabs committed Feb 12, 2025
1 parent f0a55f6 commit c73d1bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions ironfish-rust-wasm/src/keys/ephemeral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* 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 EphemeralKeyPair(ironfish::keys::EphemeralKeyPair);
}

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

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

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

#[wasm_bindgen(getter)]
pub fn secret(&self) -> Fr {
self.0.secret().to_owned().into()
}

#[wasm_bindgen(getter)]
pub fn public(&self) -> SubgroupPoint {
self.0.public().to_owned().into()
}
}
2 changes: 2 additions & 0 deletions ironfish-rust-wasm/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
* 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 ephemeral;
mod mnemonics;
mod proof_generation_key;
mod public_address;
mod sapling_key;
mod view_keys;

pub use ephemeral::EphemeralKeyPair;
pub use mnemonics::Language;
pub use proof_generation_key::ProofGenerationKey;
pub use public_address::PublicAddress;
Expand Down

0 comments on commit c73d1bf

Please sign in to comment.