Skip to content

Break builder pattern to expose all setters via bindings #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ jobs:
profile: minimal
- name: Build on Rust ${{ matrix.toolchain }}
run: cargo build --verbose --color always
- name: Build with UniFFI support on Rust ${{ matrix.toolchain }}
run: cargo build --features uniffi --verbose --color always
- name: Check release build on Rust ${{ matrix.toolchain }}
run: cargo check --release --verbose --color always
- name: Check release build with UniFFI support on Rust ${{ matrix.toolchain }}
run: cargo check --release --features uniffi --verbose --color always
- name: Test on Rust ${{ matrix.toolchain }}
run: cargo test
- name: Test with UniFFI support on Rust ${{ matrix.toolchain }}
run: cargo test --features uniffi
- name: Check formatting on Rust ${{ matrix.toolchain }}
if: matrix.check-fmt
run: rustup component add rustfmt && cargo fmt --all -- --check
- name: Test on Rust ${{ matrix.toolchain }}
run: cargo test
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ opt-level = 'z' # Optimize for size.
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*

[features]
default = []

[dependencies]
lightning = { version = "0.0.115", features = ["max_level_trace", "std"] }
Expand Down Expand Up @@ -67,7 +69,7 @@ serde_json = { version = "1.0" }
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "sync" ] }
esplora-client = { version = "0.4", default-features = false }
libc = "0.2"
uniffi = { version = "0.23.0", features = ["build"] }
uniffi = { version = "0.23.0", features = ["build"], optional = true }

[dev-dependencies]
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
Expand All @@ -76,8 +78,7 @@ proptest = "1.0.0"
regex = "1.5.6"

[build-dependencies]
uniffi = { version = "0.23.0", features = ["build", "cli"] }

uniffi = { version = "0.23.0", features = ["build", "cli"], optional = true }

[profile.release]
panic = "abort"
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ The primary abstraction of the library is the `Node`, which can be retrieved by
use ldk_node::{Builder, NetAddress};
use ldk_node::lightning_invoice::Invoice;
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::bitcoin::Network;
use std::str::FromStr;

fn main() {
let node = Builder::new()
.set_network("testnet")
.set_esplora_server_url("https://blockstream.info/testnet/api".to_string())
.build();
let mut builder = Builder::new();
builder.set_network(Network::Testnet);
builder.set_esplora_server_url("https://blockstream.info/testnet/api".to_string());

let node = builder.build();
node.start().unwrap();

let _funding_address = node.new_funding_address();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class LibraryTest {
@Test fun fullCycle() {
setup()

val network: Network = "regtest"
assertEquals(network, "regtest")
val network = Network.REGTEST

val tmpDir1 = createTempDirectory("ldk_node").toString()
println("Random dir 1: $tmpDir1")
Expand Down
20 changes: 18 additions & 2 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace ldk_node {

dictionary Config {
string storage_dir_path;
string esplora_server_url;
Network network;
NetAddress? listening_address;
u32 default_cltv_expiry_delta;
Expand All @@ -13,6 +12,15 @@ interface Builder {
constructor();
[Name=from_config]
constructor(Config config);
void set_entropy_seed_path(string seed_path);
void set_entropy_seed_bytes(sequence<u8> seed_bytes);
void set_entropy_bip39_mnemonic(Mnemonic mnemonic, string? passphrase);
void set_esplora_server(string esplora_server_url);
void set_gossip_source_p2p();
void set_gossip_source_rgs(string rgs_server_url);
void set_storage_dir_path(string storage_dir_path);
void set_network(Network network);
void set_listening_address(NetAddress listening_address);
LDKNode build();
};

Expand Down Expand Up @@ -85,6 +93,7 @@ enum NodeError {
"InvalidAddress",
"InvalidNetAddress",
"InvalidPublicKey",
"InvalidSecretKey",
"InvalidPaymentHash",
"InvalidPaymentPreimage",
"InvalidPaymentSecret",
Expand Down Expand Up @@ -117,6 +126,13 @@ enum PaymentStatus {
"Failed",
};

enum Network {
"Bitcoin",
"Testnet",
"Signet",
"Regtest",
};

dictionary PaymentDetails {
PaymentHash hash;
PaymentPreimage? preimage;
Expand Down Expand Up @@ -187,4 +203,4 @@ typedef string ChannelId;
typedef string UserChannelId;

[Custom]
typedef string Network;
typedef string Mnemonic;
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
#[cfg(feature = "uniffi")]
uniffi::generate_scaffolding("bindings/ldk_node.udl").unwrap();
}
2 changes: 1 addition & 1 deletion scripts/uniffi_bindgen_generate_kotlin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fi

#rustup target add aarch64-apple-darwin
#cargo build --target aarch64-apple-darwin || exit 1
cargo build --release || exit 1
cargo build --release --features uniffi || exit 1
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language kotlin -o "$TARGET_DIR" || exit 1

mkdir -p "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin/"$PACKAGE_DIR" || exit 1
Expand Down
7 changes: 3 additions & 4 deletions scripts/uniffi_bindgen_generate_kotlin_android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ LLVM_ARCH_PATH="darwin-x86_64"
PATH="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$LLVM_ARCH_PATH/bin:$PATH"

rustup +nightly target add x86_64-linux-android aarch64-linux-android armv7-linux-androideabi
#cargo build --release || exit 1
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="x86_64-linux-android21-clang" CC="x86_64-linux-android21-clang" cargo +nightly build --profile release-smaller --target x86_64-linux-android || exit 1
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="armv7a-linux-androideabi21-clang" CC="armv7a-linux-androideabi21-clang" cargo +nightly build --profile release-smaller --target armv7-linux-androideabi || exit 1
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="aarch64-linux-android21-clang" CC="aarch64-linux-android21-clang" cargo +nightly build --profile release-smaller --target aarch64-linux-android || exit 1
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="x86_64-linux-android21-clang" CC="x86_64-linux-android21-clang" cargo +nightly build --profile release-smaller --features uniffi --target x86_64-linux-android || exit 1
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="armv7a-linux-androideabi21-clang" CC="armv7a-linux-androideabi21-clang" cargo +nightly build --profile release-smaller --features uniffi --target armv7-linux-androideabi || exit 1
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="aarch64-linux-android21-clang" CC="aarch64-linux-android21-clang" cargo +nightly build --profile release-smaller --features uniffi --target aarch64-linux-android || exit 1
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language kotlin -o "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin || exit 1

JNI_LIB_DIR="$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/jniLibs/
Expand Down
2 changes: 1 addition & 1 deletion scripts/uniffi_bindgen_generate_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
BINDINGS_DIR="./bindings/python"
UNIFFI_BINDGEN_BIN="cargo +nightly run --features=uniffi/cli --bin uniffi-bindgen"

cargo +nightly build --release || exit 1
cargo +nightly build --release --features uniffi || exit 1
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language python -o "$BINDINGS_DIR" || exit 1
cp ./target/release/libldk_node.dylib "$BINDINGS_DIR"/libldk_node.dylib || exit 1
12 changes: 6 additions & 6 deletions scripts/uniffi_bindgen_generate_swift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ rustup target add aarch64-apple-ios-sim --toolchain nightly
rustup target add aarch64-apple-darwin x86_64-apple-darwin

# Build rust target libs
cargo build --profile release-smaller || exit 1
cargo build --profile release-smaller --target x86_64-apple-darwin || exit 1
cargo build --profile release-smaller --target aarch64-apple-darwin || exit 1
cargo build --profile release-smaller --target x86_64-apple-ios || exit 1
cargo build --profile release-smaller --target aarch64-apple-ios || exit 1
cargo +nightly build --release --target aarch64-apple-ios-sim || exit 1
cargo build --profile release-smaller --features uniffi || exit 1
cargo build --profile release-smaller --features uniffi --target x86_64-apple-darwin || exit 1
cargo build --profile release-smaller --features uniffi --target aarch64-apple-darwin || exit 1
cargo build --profile release-smaller --features uniffi --target x86_64-apple-ios || exit 1
cargo build --profile release-smaller --features uniffi --target aarch64-apple-ios || exit 1
cargo +nightly build --release --features uniffi --target aarch64-apple-ios-sim || exit 1

# Combine ios-sim and apple-darwin (macos) libs for x86_64 and aarch64 (m1)
mkdir -p target/lipo-ios-sim/release-smaller || exit 1
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Error {
InvalidNetAddress,
/// The given public key is invalid.
InvalidPublicKey,
/// The given secret key is invalid.
InvalidSecretKey,
/// The given payment hash is invalid.
InvalidPaymentHash,
/// The given payment preimage is invalid.
Expand Down Expand Up @@ -79,6 +81,7 @@ impl fmt::Display for Error {
Self::InvalidAddress => write!(f, "The given address is invalid."),
Self::InvalidNetAddress => write!(f, "The given network address is invalid."),
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
Self::InvalidSecretKey => write!(f, "The given secret key is invalid."),
Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),
Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),
Expand Down
1 change: 1 addition & 0 deletions src/hex_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Write;

#[cfg(feature = "uniffi")]
pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(hex.len() / 2);

Expand Down
Loading