Update README, build.sh and test.sh, rust fmt

This commit is contained in:
Steve Myers 2021-10-12 18:22:02 -07:00
parent 091c9994fa
commit 01bfe5d10e
6 changed files with 56 additions and 103 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ bdk_ffi_test
local.properties
*.log
*.dylib
*.so

View File

@ -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
1. Add Android rust targets
```sh
export ANDROID_NDK_HOME=/home/<user>/Android/Sdk/ndk/<NDK version, ie. 21.4.7075529>
```
```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 Swift build environment
2. Set ANDROID_NDK_HOME
1. install Swift, see ["Download Swift"](https://swift.org/download/) page
(or on Mac OSX install the latest Xcode)
```sh
export ANDROID_NDK_HOME=/home/<user>/Android/Sdk/ndk/<NDK version, ie. 21.4.7075529>
```
## Setup UniFFI
Setup Swift build environment
1. `cargo install uniffi_bindgen`
1. Install Swift, see ["Download Swift"](https://swift.org/download/) page
## Adding new structs and functions
Adding new structs and functions
See the [UniFFI User Guide](https://mozilla.github.io/uniffi-rs/)
1. Create C safe Rust structs and related functions using safer-ffi
### For pass by value objects
2. Test generated library and `bdk_ffi.h` file with c language tests in `cc/bdk_ffi_test.c`
1. create new rust struct with only fields that are supported UniFFI types
2. update mapping `bdk.udl` file with new `dictionary`
3. Use `build.sh` and `test.sh` to build c test program and verify functionality and
memory de-allocation via `valgrind`
### For pass by reference values
4. Update the kotlin native interface LibJna.kt in the `bdk-kotlin` `jvm` module to match `bdk_ffi.h`
1. create wrapper rust struct/impl with only fields that are `Sync + Send`
2. update mapping `bdk.udl` file with new `interface`
5. Create kotlin wrapper classes and interfaces as needed
### Build and test
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
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

View File

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

View File

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

32
test.sh
View File

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