From 01bfe5d10ebccdc4f9719ea1a74062f6cd0582a1 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Tue, 12 Oct 2021 18:22:02 -0700 Subject: [PATCH] Update README, build.sh and test.sh, rust fmt --- .gitignore | 3 ++- README.md | 56 +++++++++++++++++++++++++++--------------------------- build.rs | 2 +- build.sh | 31 +++++++++++------------------- src/lib.rs | 33 +++++++++----------------------- test.sh | 34 +++++---------------------------- 6 files changed, 56 insertions(+), 103 deletions(-) diff --git a/.gitignore b/.gitignore index 2a8baf6..8fd477d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ wallet_db bdk_ffi_test local.properties *.log -*.dylib \ No newline at end of file +*.dylib +*.so \ No newline at end of file diff --git a/README.md b/README.md index e363d70..95133d9 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,44 @@ +# BDK UniFFI Language Bindings -UniFFI +## Setup Android build environment -1. cargo install uniffi_bindgen -2. cargo build -3. uniffi-bindgen generate --no-format --out-dir bindings/bdk-kotlin/src/main/kotlin src/bdk.udl --language kotlin -4. cp target/debug/libuniffi_bdk.dylib bindings/bdk-kotlin/src/main/resources/darwin-x86-64 -5. cd bindings/bdk-kotlin; gradle build -Djna.debug_load=true -Djna.debug_load.jna + 1. Add Android rust targets + ```sh + rustup target add x86_64-apple-darwin x86_64-unknown-linux-gnu x86_64-linux-android aarch64-linux-android armv7-linux-androideabi i686-linux-android + ``` -Setup Android build environment + 2. Set ANDROID_NDK_HOME + + ```sh + export ANDROID_NDK_HOME=/home//Android/Sdk/ndk/ + ``` -1. Add Android rust targets +## Setup Swift build environment -```sh -rustup target add x86_64-apple-darwin x86_64-unknown-linux-gnu x86_64-linux-android aarch64-linux-android armv7-linux-androideabi i686-linux-android -``` + 1. install Swift, see ["Download Swift"](https://swift.org/download/) page + (or on Mac OSX install the latest Xcode) -2. Set ANDROID_NDK_HOME +## Setup UniFFI -```sh -export ANDROID_NDK_HOME=/home//Android/Sdk/ndk/ -``` + 1. `cargo install uniffi_bindgen` -Setup Swift build environment +## Adding new structs and functions -1. Install Swift, see ["Download Swift"](https://swift.org/download/) page +See the [UniFFI User Guide](https://mozilla.github.io/uniffi-rs/) -Adding new structs and functions +### For pass by value objects -1. Create C safe Rust structs and related functions using safer-ffi + 1. create new rust struct with only fields that are supported UniFFI types + 2. update mapping `bdk.udl` file with new `dictionary` -2. Test generated library and `bdk_ffi.h` file with c language tests in `cc/bdk_ffi_test.c` +### For pass by reference values -3. Use `build.sh` and `test.sh` to build c test program and verify functionality and - memory de-allocation via `valgrind` + 1. create wrapper rust struct/impl with only fields that are `Sync + Send` + 2. update mapping `bdk.udl` file with new `interface` -4. Update the kotlin native interface LibJna.kt in the `bdk-kotlin` `jvm` module to match `bdk_ffi.h` +### Build and test -5. Create kotlin wrapper classes and interfaces as needed - -6. Add tests to `bdk-kotlin` `test-fixtures` module - -7. Use `build.sh` and `test.sh` to build and test `bdk-kotlin` `jvm` and `android` modules \ No newline at end of file + 1. Use `build.sh` script (TODO do it all in build.rs instead) + 2. Create tests in `bindings/bdk-kotlin` and/or `bindings/bdk-swift` + 3. Use `test.sh` to run all bindings tests \ No newline at end of file diff --git a/build.rs b/build.rs index 9213f51..153077f 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,3 @@ fn main() { uniffi_build::generate_scaffolding("src/bdk.udl").unwrap(); -} \ No newline at end of file +} diff --git a/build.sh b/build.sh index 32f6369..783e847 100755 --- a/build.sh +++ b/build.sh @@ -7,29 +7,21 @@ set -eo pipefail help() { # Display Help - echo "Build bdk-ffi and related libraries." + echo "Build bdk-uniffi and related libraries." echo - echo "Syntax: build [-a|h|k]" + echo "Syntax: build [-h|k]" echo "options:" - echo "-a Android aar." echo "-h Print this Help." - echo "-k JVM jar." + echo "-k Kotlin." echo } ## rust build_rust() { - echo "Build Rust library and C headers" + echo "Build Rust library" cargo fmt cargo build - cargo test --features c-headers -- generate_headers -} - -## cc -build_cc() { - echo "Build C test library" - export LD_LIBRARY_PATH=`pwd`/target/debug - cc cc/bdk_ffi_test.c -o cc/bdk_ffi_test -L target/debug -l bdk_ffi -l pthread -l dl -l m + cargo test } ## copy to bdk-bdk-kotlin @@ -38,13 +30,13 @@ copy_lib_kotlin() { case $OS in "Darwin") echo -n "darwin " - mkdir -p bdk-bdk-kotlin/jvm/src/main/resources/darwin-x86-64 - cp target/debug/libbdk_ffi.dylib bdk-bdk-kotlin/jvm/src/main/resources/darwin-x86-64 + mkdir -p bindings/bdk-kotlin/src/main/resources/darwin-x86-64 + cp target/debug/libuniffi_bdk.dylib bindings/bdk-kotlin/src/main/resources/darwin-x86-64 ;; "Linux") echo -n "linux " - mkdir -p bdk-bdk-kotlin/jvm/src/main/resources/linux-x86-64 - cp target/debug/libbdk_ffi.so bdk-bdk-kotlin/jvm/src/main/resources/linux-x86-64 + mkdir -p bindings/bdk-kotlin/src/main/resources/linux-x86-64 + cp target/debug/libuniffi_bdk.so bindings/bdk-kotlin/src/main/resources/linux-x86-64 ;; esac echo "libs to kotlin sub-project" @@ -52,7 +44,8 @@ copy_lib_kotlin() { ## bdk-bdk-kotlin jar build_kotlin() { - (cd bdk-bdk-kotlin && ./gradlew :jvm:build && ./gradlew :jvm:publishToMavenLocal) + uniffi-bindgen generate src/bdk.udl --no-format --out-dir bindings/bdk-kotlin/src/main/kotlin --language kotlin + (cd bindings/bdk-kotlin && ./gradlew build) } ## rust android @@ -99,13 +92,11 @@ then help else build_rust - build_cc copy_lib_kotlin while [ -n "$1" ]; do # while loop starts case "$1" in -k) build_kotlin ;; - -a) build_android ;; -h) help ;; *) echo "Option $1 not recognized" ;; esac diff --git a/src/lib.rs b/src/lib.rs index 4002e1f..994a884 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,9 @@ -use bdk::Wallet; -use bdk::wallet::AddressIndex; -use bdk::database::MemoryDatabase; use bdk::bitcoin::Network; -// use crate::error::FfiError; -use std::sync::{RwLock, Mutex}; -use std::vec::Vec; -use bdk::database::BatchDatabase; use bdk::sled; use bdk::sled::Tree; -//mod error; -//mod types; -//mod wallet; +use bdk::wallet::AddressIndex; +use bdk::Wallet; +use std::sync::Mutex; uniffi_macros::include_scaffolding!("bdk"); @@ -24,25 +17,19 @@ impl OfflineWallet { let database = sled::open("testdb").unwrap(); let tree = database.open_tree("test").unwrap(); - let wallet = Wallet::new_offline( - &descriptor, - None, - Network::Regtest, - tree, - ).unwrap(); + let wallet = Wallet::new_offline(&descriptor, None, Network::Regtest, tree).unwrap(); OfflineWallet { - wallet: Mutex::new(wallet) + wallet: Mutex::new(wallet), } -// OfflineWallet { -// wallet: RwLock::new(Vec::new()) -// } + // OfflineWallet { + // wallet: RwLock::new(Vec::new()) + // } } fn get_new_address(&self) -> String { - self - .wallet + self.wallet .lock() .unwrap() .get_address(AddressIndex::New) @@ -53,5 +40,3 @@ impl OfflineWallet { } uniffi::deps::static_assertions::assert_impl_all!(OfflineWallet: Sync, Send); - - diff --git a/test.sh b/test.sh index 45b497e..4f83359 100755 --- a/test.sh +++ b/test.sh @@ -7,54 +7,30 @@ set -eo pipefail help() { # Display Help - echo "Test bdk-ffi and related libraries." + echo "Test bdk-uniffi and related libraries." echo echo "Syntax: build [-a|h|k|v]" echo "options:" - echo "-a Android aar tests." echo "-h Print this Help." - echo "-k JVM jar tests." - echo "-v Valgrind tests." + echo "-k Kotlin tests." echo } -# rust -c_headers() { - cargo test --features c-headers -- generate_headers -} - -# cc -test_c() { - export LD_LIBRARY_PATH=`pwd`/target/debug - cc/bdk_ffi_test -} - -test_valgrind() { - valgrind --leak-check=full --show-leak-kinds=all cc/bdk_ffi_test -} - test_kotlin() { - (cd bdk-kotlin && ./gradlew test) -} - -test_android() { - (cd bdk-kotlin && ./gradlew :android:connectedDebugAndroidTest) + (cd bindings/bdk-kotlin && ./gradlew test) } if [ $1 = "-h" ] then help else - c_headers - test_c - + cargo test + # optional tests while [ -n "$1" ]; do # while loop starts case "$1" in - -a) test_android ;; -h) help ;; -k) test_kotlin ;; - -v) test_valgrind ;; *) echo "Option $1 not recognized" ;; esac shift