Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple wasm-pack targets #60

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions umbral-pre-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
package-lock.json
pkg
fjarri marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 0 additions & 5 deletions umbral-pre-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,3 @@ umbral-pre = { path = "../umbral-pre" }
wasm-bindgen = "0.2.74"
js-sys = "0.3.51"
wee_alloc = "0.4"

[package.metadata.wasm-pack.profile.release]
# See https://github.com/rustwasm/wasm-pack/issues/886
# Maybe at some point in time this won't be necessary.
wasm-opt = ["-Oz", "--enable-mutable-globals"]
31 changes: 21 additions & 10 deletions umbral-pre-wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ SHELL = /bin/bash
build: pkg

pkg: src
wasm-pack build
# temporary fix for https://github.com/rustwasm/wasm-pack/issues/427
# (setting a custom package name)
sed -i ".bak" -e 's/"name": "umbral-pre-wasm"/"name": "umbral-pre"/g' pkg/package.json
# Temporary fix for https://github.com/rustwasm/wasm-pack/issues/922
# (adding missing package files to the manifest)
sed -i ".bak" -e 's/"umbral_pre_wasm_bg.wasm"/"umbral_pre_wasm_bg.wasm", "umbral_pre_wasm_bg.js", "umbral_pre_wasm_bg.wasm.d.ts"/g' pkg/package.json
# Add keywords
sed -i ".bak" -e 's/"sideEffects": false/"sideEffects": false, "keywords": ["umbral", "nucypher"]/g' pkg/package.json
rm pkg/package.json.bak
# Build for both targets
wasm-pack build -t bundler -d pkg/pkg-bundler
wasm-pack build -t nodejs -d pkg/pkg-node

# Clean-up non-essential files
(cd pkg/pkg-bundler && rm package.json README.md .gitignore LICENSE)
(cd pkg/pkg-node && rm package.json README.md .gitignore LICENSE)

# Types are duplicated, clean them up to avoid confusion
mv pkg/pkg-node/umbral_pre_wasm.d.ts pkg/
rm pkg/pkg-bundler/umbral_pre_wasm.d.ts

# Copy template
cp package.template.json pkg/package.json
cp LICENSE README.md pkg/


.PHONY: clean

clean:
rm -rf pkg
27 changes: 21 additions & 6 deletions umbral-pre-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,32 @@ console.assert(dec.decode(plaintext_bob) == plaintext, "decrypt_reencrypted() fa
## Build

The package is built using [`wasm-pack`](https://github.com/rustwasm/wasm-pack).
Instead of running `wasm-build` directly, use the included `Makefile`, since it has to do some additional actions that `wasm-build` currently does not support.
Instead of running `wasm-build` directly, use the included `Makefile`, since it has to do some additional actions that `wasm-build` currently does not support:

## Running the example

After you have successfully built the WASM package, in the `example` folder run
```bash
$ make
```
$ npm install
$ npm run start

## Running the examples

After you have successfully built the WASM package, run one of the example projects:

### `umbral-pre` in the browser
```bash
$ cd example-bundler
$ yarn install
$ yarn start
```
Go to [localhost:8080](http://localhost:8080/) in your browser and look in the JS console.

### `umbral-pre` in Node.js:
```bash
$ cd example-node
$ yarn install
$ yarn start
```
Inspect console output for results.

[js-npm-image]: https://img.shields.io/npm/v/umbral-pre
[js-npm-link]: https://www.npmjs.com/package/umbral-pre
[js-license-image]: https://img.shields.io/npm/l/umbral-pre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ let dec = new TextDecoder("utf-8");

// Key Generation (on Alice's side)
let alice_sk = umbral.SecretKey.random();
let alice_pk = umbral.PublicKey.fromSecretKey(alice_sk);
let alice_pk = alice_sk.publicKey();
let signing_sk = umbral.SecretKey.random();
let signer = new umbral.Signer(signing_sk);
let verifying_pk = umbral.PublicKey.fromSecretKey(signing_sk);

// Key Generation (on Bob's side)
let bob_sk = umbral.SecretKey.random();
let bob_pk = umbral.PublicKey.fromSecretKey(bob_sk);
let bob_pk = bob_sk.publicKey();

// Now let's encrypt data with Alice's public key.
// Invocation of `encrypt()` returns both the ciphertext and a capsule.
Expand Down Expand Up @@ -80,3 +79,5 @@ let plaintext_bob = capsule
.decryptReencrypted(bob_sk, alice_pk, ciphertext);

console.assert(dec.decode(plaintext_bob) == plaintext, "decrypt_reencrypted() failed");

console.log("Success!");
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "umbral-pre-example",
"name": "umbral-pre-example-bundler",
"version": "0.1.0",
"description": "A usage example for umbral-pre",
"main": "index.js",
Expand Down
Loading