|
3 | 3 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
4 | 4 |
|
5 | 5 | 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, |
7 | 10 | };
|
| 11 | +use ironfish_zkp::constants::SPENDING_KEY_GENERATOR; |
| 12 | +use rand::thread_rng; |
8 | 13 | use wasm_bindgen::prelude::*;
|
9 | 14 |
|
10 | 15 | wasm_bindgen_wrapper! {
|
@@ -116,4 +121,46 @@ impl ViewKey {
|
116 | 121 | pub fn nullifier_deriving_key(&self) -> PublicKey {
|
117 | 122 | self.0.nullifier_deriving_key.into()
|
118 | 123 | }
|
| 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 | + } |
119 | 166 | }
|
0 commit comments