Skip to content

Commit befe45f

Browse files
committed
WASM: add methods to create randomized keys
1 parent dc95e90 commit befe45f

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

ironfish-rust-wasm/src/keys/view_keys.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

55
use crate::{
6-
errors::IronfishError, keys::PublicAddress, primitives::PublicKey, wasm_bindgen_wrapper,
6+
errors::IronfishError,
7+
keys::PublicAddress,
8+
primitives::{Fr, PublicKey},
9+
wasm_bindgen_wrapper,
710
};
11+
use ironfish_zkp::constants::SPENDING_KEY_GENERATOR;
12+
use rand::thread_rng;
813
use wasm_bindgen::prelude::*;
914

1015
wasm_bindgen_wrapper! {
@@ -116,4 +121,46 @@ impl ViewKey {
116121
pub fn nullifier_deriving_key(&self) -> PublicKey {
117122
self.0.nullifier_deriving_key.into()
118123
}
124+
125+
#[wasm_bindgen(js_name = randomizedPublicKey)]
126+
pub fn randomized_public_key_pair(&self) -> RandomizedPublicKeyPair {
127+
let (r, s) = self.0.randomized_public_key(thread_rng());
128+
RandomizedPublicKeyPair::new(r.into(), s.into())
129+
}
130+
}
131+
132+
#[wasm_bindgen]
133+
#[derive(Clone, Debug)]
134+
pub struct RandomizedPublicKeyPair(Fr, PublicKey);
135+
136+
#[wasm_bindgen]
137+
impl RandomizedPublicKeyPair {
138+
#[wasm_bindgen(constructor)]
139+
pub fn new(public_key_randomness: Fr, randomized_public_key: PublicKey) -> Self {
140+
Self(public_key_randomness, randomized_public_key)
141+
}
142+
143+
#[wasm_bindgen(js_name = fromViewKey)]
144+
pub fn from_view_key(view_key: &ViewKey) -> Self {
145+
let public_key_randomness = Fr::random();
146+
Self::from_view_key_and_randomness(view_key, public_key_randomness)
147+
}
148+
149+
#[wasm_bindgen(js_name = fromViewKeyAndRandomness)]
150+
pub fn from_view_key_and_randomness(view_key: &ViewKey, public_key_randomness: Fr) -> Self {
151+
let randomized_public_key = view_key
152+
.authorizing_key()
153+
.randomize(&public_key_randomness, &(*SPENDING_KEY_GENERATOR).into());
154+
Self(public_key_randomness, randomized_public_key)
155+
}
156+
157+
#[wasm_bindgen(js_name = publicKeyRandomness)]
158+
pub fn public_key_randomness(&self) -> Fr {
159+
self.0
160+
}
161+
162+
#[wasm_bindgen(js_name = randomizedPublicKey)]
163+
pub fn randomized_public_key(&self) -> PublicKey {
164+
self.1.clone()
165+
}
119166
}

ironfish-rust-wasm/src/primitives.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

55
use crate::{errors::IronfishError, wasm_bindgen_wrapper};
6+
use group::ff::Field;
67
use group::GroupEncoding;
78
use ironfish::errors::IronfishErrorKind;
89
use ironfish_zkp::redjubjub;
@@ -16,6 +17,11 @@ wasm_bindgen_wrapper! {
1617

1718
#[wasm_bindgen]
1819
impl Scalar {
20+
#[wasm_bindgen]
21+
pub fn random() -> Self {
22+
Self(blstrs::Scalar::random(thread_rng()))
23+
}
24+
1925
#[wasm_bindgen(js_name = toBytesBe)]
2026
pub fn to_bytes_be(&self) -> Vec<u8> {
2127
self.0.to_bytes_be().to_vec()
@@ -34,6 +40,11 @@ wasm_bindgen_wrapper! {
3440

3541
#[wasm_bindgen]
3642
impl Fr {
43+
#[wasm_bindgen]
44+
pub fn random() -> Self {
45+
Self(ironfish_jubjub::Fr::random(thread_rng()))
46+
}
47+
3748
#[wasm_bindgen(js_name = toBytes)]
3849
pub fn to_bytes(&self) -> Vec<u8> {
3950
self.0.to_bytes().to_vec()

0 commit comments

Comments
 (0)