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

Feat: add feature flag for python build #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "timelock_wasm_wrapper"
version = "0.0.1"
version = "0.0.2"
edition = "2021"
license = "Apache-2.0"
description = "Wasm bidings for the timelock encryption crate"
Expand Down Expand Up @@ -37,8 +37,11 @@ rand_chacha = { version = "0.3.1", default-features = false }
w3f-bls = { version = "0.1.3", default-features = false }
sp-consensus-beefy-etf = { git = "https://github.com/ideal-lab5/idn-sdk.git", features = ["bls-experimental"]}
sha2 = { version = "0.10.2", default-features = false }
pyo3 = { version = "0.23.1", features = ["extension-module", "macros"], default-features = false }
libc = "0.2" # Make sure to use the latest version available
pyo3 = { version = "0.23.1", features = ["extension-module", "macros"], default-features = false, optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.0"

[features]
default = []
python = [ "pyo3" ]
10 changes: 5 additions & 5 deletions wasm/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# WASM Bindings for Timelock

This crate provides wasm compatibility for the timelock crate. It provides compatibility for both javascript and python (3+).
This crate provides wasm compatibility for the timelock crate. It provides compatibility for both JavaScript and Python (3+).

## Build

Expand All @@ -22,14 +22,14 @@ First create a virtual env, then run:
pip install maturin
# specify your python version
export PYO3_CROSS_PYTHON_VERSION="3.10"
maturin develop
maturin develop --features "python"
```

#### Publish

``` sh
# Create a release build
maturin build --release
# publish with twine
twine upload dist/* timelock
maturin build --features "python" --release
# publish to PyPi with maturing
maturin publish --features "python"
```
2 changes: 2 additions & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
*/

pub mod js;

#[cfg(feature = "python")]
pub mod py;
1 change: 1 addition & 0 deletions wasm/src/py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use timelock::{
TLECiphertext,
},
};

/// The encrypt wrapper used by the Python bindings to call tlock.rs encrypt
/// function
/// * 'id_py': ID string for which the message will be encrypted
Expand Down
19 changes: 2 additions & 17 deletions wasm/wasm_build_py.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
#!/usr/bin/env bash
#
# This shell script compiles the rust library to WebAssembly and creates a JS package using wasm-pack.
# It also replaces any Node.js specific code with PythonMonkey code using sed.
#
# Note: this script is only compatible with unix.
#

cargo build
# compiles rust code to wasm and creates a package targetting Node.js
wasm-pack build --target nodejs

# replaces calls to require('path') and require('fs') with pythonmonkey non Node.js equivalents
NEW_READ_FILE_SYNC="function pyReadFileSync(filename) {\n python.exec(\n\\\`\ndef getBytes(filename):\n file = open(filename, 'rb')\n return bytearray(file.read())\n\\\`\n );\n return python.eval('getBytes')(filename)\n}\n"

find ./pkg/*js -type f -exec sed -i "s/require('path').join(\(.*\));/[\1]\.join('\/');/g" {} \;
find ./pkg/*js -type f -exec sed -i "s/require('fs').readFileSync(\(.*\));/(${NEW_READ_FILE_SYNC})(\1)/g" {} \;
cargo build --features "python" --release
maturin develop --features "python"