Skip to content

Commit

Permalink
Return a tuple from encrypt() instead of an EncryptionResult object
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Sep 19, 2022
1 parent 7196ed6 commit c4b4591
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Replaced `AsBackend`/`FromBackend`, `.inner()`, `.new()`, and `pub backend` with derived `AsRef`/`From`/`Into` where appropriate. (#[103])
- Using a workaround with `wasm-bindgen-derive` to support `Option<&T>` and `&Vec<T>` arguments, and `Vec<T>` return values in WASM bindings. Generating correct TypeScript signatures in all the relevant cases. Affected API: `Capsule.decryptReencrypted()`, `KeyFrag.verify()`, `generate_kfrags()`. (#[103])
- Removed `serde` usage in WASM bindings. ([#103])
- `encrypt()` now returns an actual tuple in WASM bindings instead of a special object. (#[103])


### Added
Expand Down
5 changes: 1 addition & 4 deletions umbral-pre-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ let bob_pk = bob_sk.publicKey();
let plaintext = "Plaintext message";
let plaintext_bytes = enc.encode(plaintext);

// The API here slightly differs from that in Rust.
// Since wasm-pack does not support returning tuples, we return an object containing
// the ciphertext and the capsule.
let {capsule, ciphertext} = umbral.encrypt(alice_pk, plaintext_bytes);
let [capsule, ciphertext] = umbral.encrypt(alice_pk, plaintext_bytes);
let ciphertext = result.ciphertext;
let capsule = result.capsule;

Expand Down
5 changes: 1 addition & 4 deletions umbral-pre-wasm/examples/bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ let bob_pk = bob_sk.publicKey();
let plaintext = "Plaintext message";
let plaintext_bytes = enc.encode(plaintext);

// The API here slightly differs from that in Rust.
// Since wasm-pack does not support returning tuples, we return an object containing
// the ciphertext and the capsule.
let {capsule, ciphertext} = umbral.encrypt(alice_pk, plaintext_bytes);
let [capsule, ciphertext] = umbral.encrypt(alice_pk, plaintext_bytes);

// Since data was encrypted with Alice's public key, Alice can open the capsule
// and decrypt the ciphertext with her private key.
Expand Down
5 changes: 1 addition & 4 deletions umbral-pre-wasm/examples/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ let bob_pk = bob_sk.publicKey();
let plaintext = "Plaintext message";
let plaintext_bytes = enc.encode(plaintext);

// The API here slightly differs from that in Rust.
// Since wasm-pack does not support returning tuples, we return an object containing
// the ciphertext and the capsule.
let {capsule, ciphertext} = umbral.encrypt(alice_pk, plaintext_bytes);
let [capsule, ciphertext] = umbral.encrypt(alice_pk, plaintext_bytes);

// Since data was encrypted with Alice's public key, Alice can open the capsule
// and decrypt the ciphertext with her private key.
Expand Down
5 changes: 1 addition & 4 deletions umbral-pre-wasm/examples/react/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ function App() {

let plaintext_bytes = enc.encode(plaintext);

// The API here slightly differs from that in Rust.
// Since wasm-pack does not support returning tuples, we return an object containing
// the ciphertext and the capsule.
let {capsule, ciphertext} = umbral.encrypt(alice_pk, plaintext_bytes);
let [capsule, ciphertext] = umbral.encrypt(alice_pk, plaintext_bytes);

// Since data was encrypted with Alice's public key, Alice can open the capsule
// and decrypt the ciphertext with her private key.
Expand Down
41 changes: 14 additions & 27 deletions umbral-pre/src/bindings_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

use js_sys::Error;
use js_sys::{Error, Uint8Array};
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};
use wasm_bindgen::JsCast;
use wasm_bindgen_derive::TryFromJsValue;
Expand All @@ -32,6 +32,9 @@ extern "C" {

#[wasm_bindgen(typescript_type = "VerifiedKeyFrag[]")]
pub type VerifiedKeyFragArray;

#[wasm_bindgen(typescript_type = "[Capsule, Uint8Array]")]
pub type EncryptionResult;
}

fn map_js_err<T: fmt::Display>(err: T) -> Error {
Expand Down Expand Up @@ -370,35 +373,19 @@ impl VerifiedCapsuleFrag {
}
}

#[wasm_bindgen]
pub struct EncryptionResult {
ciphertext: Box<[u8]>,
pub capsule: Capsule,
}

#[wasm_bindgen]
impl EncryptionResult {
fn new(ciphertext: Box<[u8]>, capsule: Capsule) -> Self {
Self {
ciphertext,
capsule,
}
}

// TODO (#24): currently can't just make the field public because `Box` doesn't implement `Copy`.
// See https://github.com/rustwasm/wasm-bindgen/issues/439
#[wasm_bindgen(getter)]
pub fn ciphertext(&self) -> Box<[u8]> {
self.ciphertext.clone()
}
}

#[wasm_bindgen]
pub fn encrypt(delegating_pk: &PublicKey, plaintext: &[u8]) -> Result<EncryptionResult, Error> {
let backend_pk = delegating_pk.0;
umbral_pre::encrypt(&backend_pk, plaintext)
.map(|(capsule, ciphertext)| EncryptionResult::new(ciphertext, Capsule(capsule)))
.map_err(map_js_err)
let (capsule, ciphertext) = umbral_pre::encrypt(&backend_pk, plaintext).map_err(map_js_err)?;

// TODO (#24): wasm-bindgen does not allow one to return a tuple directly.
// Have to cast it manually.
let capsule_js: JsValue = Capsule::from(capsule).into();
let ciphertext_js: JsValue = Uint8Array::from(ciphertext.as_ref()).into();
Ok([capsule_js, ciphertext_js]
.into_iter()
.collect::<js_sys::Array>()
.unchecked_into::<EncryptionResult>())
}

#[wasm_bindgen(js_name = decryptOriginal)]
Expand Down

0 comments on commit c4b4591

Please sign in to comment.