Some POC nodejs bindings

This commit is contained in:
Hanh 2022-07-21 19:22:41 +08:00
parent 4fabf69589
commit 1eb6b949df
5 changed files with 59 additions and 0 deletions

View File

@ -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

View File

@ -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
```

View File

@ -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;

25
src/nodejs.rs Normal file
View File

@ -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();
}

6
warp.js Normal file
View File

@ -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")