-
Notifications
You must be signed in to change notification settings - Fork 573
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
EphemeralKeyPair
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 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
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() | ||
} | ||
} |