From 4c19491b7a0f24fa1aa6294273bab29e9790dce4 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 12 Feb 2025 14:36:00 -0800 Subject: [PATCH] WASM: add bindings for `EphemeralKeyPair` --- ironfish-rust-wasm/src/keys/ephemeral.rs | 47 ++++++++++++++++++++++++ ironfish-rust-wasm/src/keys/mod.rs | 2 + 2 files changed, 49 insertions(+) create mode 100644 ironfish-rust-wasm/src/keys/ephemeral.rs diff --git a/ironfish-rust-wasm/src/keys/ephemeral.rs b/ironfish-rust-wasm/src/keys/ephemeral.rs new file mode 100644 index 0000000000..6aa0b96f85 --- /dev/null +++ b/ironfish-rust-wasm/src/keys/ephemeral.rs @@ -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 { + Ok(Self(ironfish::keys::EphemeralKeyPair::read(bytes)?)) + } + + #[wasm_bindgen] + pub fn serialize(&self) -> Vec { + 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() + } +} diff --git a/ironfish-rust-wasm/src/keys/mod.rs b/ironfish-rust-wasm/src/keys/mod.rs index b6ed99d489..51c86e8760 100644 --- a/ironfish-rust-wasm/src/keys/mod.rs +++ b/ironfish-rust-wasm/src/keys/mod.rs @@ -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;