diff --git a/Cargo.toml b/Cargo.toml index 01df6eb..430427b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,11 +81,14 @@ android_logger = { version = "0.10.0", optional = true } rocket = { version = "0.5.0-rc.2", features = ["json"], optional = true } dotenv = { version = "0.15.0", optional = true } +node-bindgen = { version = "4.0", optional = true } + [features] ledger = ["ledger-apdu", "hmac", "ed25519-bip32", "ledger-transport-hid"] ledger_sapling = ["ledger"] dart_ffi = ["allo-isolate", "once_cell", "android_logger"] rpc = ["rocket", "dotenv"] +nodejs = ["node-bindgen"] # librustzcash synced to 35023ed8ca2fb1061e78fd740b640d4eefcc5edd diff --git a/README.md b/README.md index 4753979..f55caf1 100644 --- a/README.md +++ b/README.md @@ -35,4 +35,26 @@ get_balance, get_address, get_tx_history, pay, +``` + +## NodeJS + +NodeJS bindings are incomplete/unsupported and maybe dropped at any time. + +- Install `nj-cli` (one time) +- Edit `Cargo.toml` +- Build +- Use + +Ex: +``` +$ cargo install nj-cli +$ vim Cargo.toml +... +[lib] +#name = "warp_api_ffi" +crate-type = ["cdylib"] +... +$ nj-cli build --release -- --features=nodejs +$ node warp.js ``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 396c621..ca12737 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,3 +89,6 @@ pub use crate::ledger::sapling::build_tx_ledger; #[cfg(feature = "ledger")] pub use crate::ledger::sweep_ledger; + +#[cfg(feature = "nodejs")] +pub mod nodejs; diff --git a/src/nodejs.rs b/src/nodejs.rs new file mode 100644 index 0000000..b55d7c9 --- /dev/null +++ b/src/nodejs.rs @@ -0,0 +1,25 @@ +#![allow(non_snake_case)] +use node_bindgen::derive::node_bindgen; + +#[node_bindgen] +fn initCoin(coin: u32, db_path: String, lwd_url: String) { + let coin = coin as u8; + log::info!("Init coin"); + crate::init_coin(coin, &db_path).unwrap(); + crate::set_coin_lwd_url(coin, &lwd_url); +} + +#[node_bindgen] +fn newAccount( + coin: u32, + name: String, +) { + crate::api::account::new_account(coin as u8, &name, None, None).unwrap(); +} + +// Does not support tokio async executor atm +#[tokio::main] +#[node_bindgen] +async fn warp(coin: u32) { + crate::api::sync::coin_sync(coin as u8, true, 0, move |height| {}).await.unwrap(); +} diff --git a/warp.js b/warp.js new file mode 100644 index 0000000..2bf7570 --- /dev/null +++ b/warp.js @@ -0,0 +1,6 @@ +const warp = require('./dist/index.node') + +warp.initCoin(0, "./zec.db", "https://mainnet.lightwalletd.com:9067") +// warp.newAccount(0, "test_account") +warp.warp(0) +console.log("Finished")