From 4ad9b89e2591dccf36b48512a4caa3ce343e3ec5 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 28 Oct 2021 00:33:10 +0530 Subject: [PATCH 1/6] Remove callback interface --- src/bdk.udl | 6 +----- src/lib.rs | 16 ++++------------ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/bdk.udl b/src/bdk.udl index e3ab62e..27c554d 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -120,10 +120,6 @@ interface BlockchainConfig { Esplora(EsploraConfig config); }; -callback interface BdkProgress { - void update(f32 progress, string? message); -}; - interface OnlineWallet { [Throws=BdkError] constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config); @@ -140,7 +136,7 @@ interface OnlineWallet { // OnlineWalletInterface Network get_network(); [Throws=BdkError] - void sync(BdkProgress progress_update, u32? max_address_param); + void sync(u32? max_address_param); [Throws=BdkError] string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt); }; diff --git a/src/lib.rs b/src/lib.rs index e19ab21..9faf5f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,13 +154,10 @@ pub trait BdkProgress: Send + Sync { fn update(&self, progress: f32, message: Option); } -struct BdkProgressHolder { - progress_update: Box, -} +struct BdkProgressHolder {} impl Progress for BdkProgressHolder { - fn update(&self, progress: f32, message: Option) -> Result<(), Error> { - self.progress_update.update(progress, message); + fn update(&self, _progress: f32, _message: Option) -> Result<(), Error> { Ok(()) } } @@ -238,16 +235,11 @@ impl OnlineWallet { self.wallet.lock().unwrap().network() } - fn sync( - &self, - progress_update: Box, - max_address_param: Option, - ) -> Result<(), BdkError> { - progress_update.update(21.0, Some("message".to_string())); + fn sync(&self, max_address_param: Option) -> Result<(), BdkError> { self.wallet .lock() .unwrap() - .sync(BdkProgressHolder { progress_update }, max_address_param) + .sync(BdkProgressHolder {}, max_address_param) } fn broadcast<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result { From d1b24026e68b2de67c7fb1289dbc1f2f8a3649fe Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 28 Oct 2021 00:35:09 +0530 Subject: [PATCH 2/6] Copy kotlin libs only when building kotlin --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 13302c7..d0a9f28 100755 --- a/build.sh +++ b/build.sh @@ -44,6 +44,7 @@ copy_lib_kotlin() { ## bdk-bdk-kotlin jar build_kotlin() { + copy_lib_kotlin uniffi-bindgen generate src/bdk.udl --no-format --out-dir bindings/bdk-kotlin/jvm/src/main/kotlin --language kotlin } @@ -96,7 +97,6 @@ then help else build_rust - copy_lib_kotlin while [ -n "$1" ]; do # while loop starts case "$1" in From e98298090dbdc0a11c086fd39ca927699895e640 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 28 Oct 2021 00:35:22 +0530 Subject: [PATCH 3/6] Generate bindings for swift --- Cargo.toml | 2 +- build.sh | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 954e596..b53c75f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] -crate-type = ["cdylib"] +crate-type = ["staticlib", "cdylib"] [dependencies] bdk = { version = "^0.12.1-dev", features = ["all-keys", "use-esplora-ureq"] } diff --git a/build.sh b/build.sh index d0a9f28..4adf44c 100755 --- a/build.sh +++ b/build.sh @@ -48,6 +48,41 @@ build_kotlin() { uniffi-bindgen generate src/bdk.udl --no-format --out-dir bindings/bdk-kotlin/jvm/src/main/kotlin --language kotlin } +## bdk swift +build_swift() { + uniffi-bindgen generate src/bdk.udl --no-format --out-dir bindings/bdk-swift/ --language swift + TARGETDIR=target + RELDIR=debug + STATIC_LIB_NAME=libuniffi_bdk.a + + # We can't use cargo lipo because we can't link to universal libraries :( + # https://github.com/rust-lang/rust/issues/55235 + LIBS_ARCHS=("x86_64" "arm64") + IOS_TRIPLES=("x86_64-apple-ios" "aarch64-apple-ios") + for i in "${!LIBS_ARCHS[@]}"; do + cargo build --target "${IOS_TRIPLES[${i}]}" + done + + UNIVERSAL_BINARY=./${TARGETDIR}/ios/universal/${RELDIR}/${STATIC_LIB_NAME} + NEED_LIPO= + + # if the universal binary doesnt exist, or if it's older than the static libs, + # we need to run `lipo` again. + if [[ ! -f "${UNIVERSAL_BINARY}" ]]; then + NEED_LIPO=1 + elif [[ "$(stat -f "%m" "./${TARGETDIR}/x86_64-apple-ios/${RELDIR}/${STATIC_LIB_NAME}")" -gt "$(stat -f "%m" "${UNIVERSAL_BINARY}")" ]]; then + NEED_LIPO=1 + elif [[ "$(stat -f "%m" "./${TARGETDIR}/aarch64-apple-ios/${RELDIR}/${STATIC_LIB_NAME}")" -gt "$(stat -f "%m" "${UNIVERSAL_BINARY}")" ]]; then + NEED_LIPO=1 + fi + if [[ "${NEED_LIPO}" = "1" ]]; then + mkdir -p "${TARGETDIR}/ios/universal/${RELDIR}" + lipo -create -output "${UNIVERSAL_BINARY}" \ + "${TARGETDIR}/x86_64-apple-ios/${RELDIR}/${STATIC_LIB_NAME}" \ + "${TARGETDIR}/aarch64-apple-ios/${RELDIR}/${STATIC_LIB_NAME}" + fi +} + ## rust android build_android() { build_kotlin @@ -102,6 +137,7 @@ else case "$1" in -a) build_android ;; -k) build_kotlin ;; + -s) build_swift ;; -h) help ;; *) echo "Option $1 not recognized" ;; esac From ef73e38154c1f8c7fc89ff3a40281f7ccacc1dbb Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 28 Oct 2021 00:37:42 +0530 Subject: [PATCH 4/6] Add swiftmodule --- build.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/build.sh b/build.sh index 4adf44c..3a0d37c 100755 --- a/build.sh +++ b/build.sh @@ -51,6 +51,7 @@ build_kotlin() { ## bdk swift build_swift() { uniffi-bindgen generate src/bdk.udl --no-format --out-dir bindings/bdk-swift/ --language swift + swiftc -module-name bdk -emit-library -o libuniffi_bdk.dylib -emit-module -emit-module-path ./bindings/bdk-swift/ -parse-as-library -L ./target/debug/ -luniffi_bdk -Xcc -fmodule-map-file=./bindings/bdk-swift/bdkFFI.modulemap ./bindings/bdk-swift/bdk.swift TARGETDIR=target RELDIR=debug STATIC_LIB_NAME=libuniffi_bdk.a From 9681f30e19c65348b31cd104483994e6199cdc45 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 28 Oct 2021 01:51:16 +0530 Subject: [PATCH 5/6] Revert "Remove callback interface" This reverts commit 4ad9b89e2591dccf36b48512a4caa3ce343e3ec5. --- src/bdk.udl | 6 +++++- src/lib.rs | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/bdk.udl b/src/bdk.udl index 27c554d..e3ab62e 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -120,6 +120,10 @@ interface BlockchainConfig { Esplora(EsploraConfig config); }; +callback interface BdkProgress { + void update(f32 progress, string? message); +}; + interface OnlineWallet { [Throws=BdkError] constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config); @@ -136,7 +140,7 @@ interface OnlineWallet { // OnlineWalletInterface Network get_network(); [Throws=BdkError] - void sync(u32? max_address_param); + void sync(BdkProgress progress_update, u32? max_address_param); [Throws=BdkError] string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt); }; diff --git a/src/lib.rs b/src/lib.rs index 9faf5f7..e19ab21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,10 +154,13 @@ pub trait BdkProgress: Send + Sync { fn update(&self, progress: f32, message: Option); } -struct BdkProgressHolder {} +struct BdkProgressHolder { + progress_update: Box, +} impl Progress for BdkProgressHolder { - fn update(&self, _progress: f32, _message: Option) -> Result<(), Error> { + fn update(&self, progress: f32, message: Option) -> Result<(), Error> { + self.progress_update.update(progress, message); Ok(()) } } @@ -235,11 +238,16 @@ impl OnlineWallet { self.wallet.lock().unwrap().network() } - fn sync(&self, max_address_param: Option) -> Result<(), BdkError> { + fn sync( + &self, + progress_update: Box, + max_address_param: Option, + ) -> Result<(), BdkError> { + progress_update.update(21.0, Some("message".to_string())); self.wallet .lock() .unwrap() - .sync(BdkProgressHolder {}, max_address_param) + .sync(BdkProgressHolder { progress_update }, max_address_param) } fn broadcast<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result { From ab301f075b5bc4cebdd9317669aece0f908aafb9 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 28 Oct 2021 23:00:23 +0530 Subject: [PATCH 6/6] Update uniffi to 0.14.1 which supports callback interface in Swift --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b53c75f..b8512b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,12 @@ crate-type = ["staticlib", "cdylib"] [dependencies] bdk = { version = "^0.12.1-dev", features = ["all-keys", "use-esplora-ureq"] } -uniffi_macros = "0.14.0" -uniffi = "0.14.0" +uniffi_macros = "0.14.1" +uniffi = "0.14.1" thiserror = "1.0" [build-dependencies] -uniffi_build = "0.14.0" +uniffi_build = "0.14.1" [patch.crates-io] bdk = { git = "https://github.com/artfuldev/bdk.git", branch = "use-send-and-sync-on-memory-database" }