Skip to content

Commit ab6c009

Browse files
authored
Merge pull request #88 from tnull/2023-05-expose-builder-in-bindings
Break builder pattern to expose all setters via bindings
2 parents 423e414 + de94a54 commit ab6c009

18 files changed

+432
-338
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ jobs:
2222
profile: minimal
2323
- name: Build on Rust ${{ matrix.toolchain }}
2424
run: cargo build --verbose --color always
25+
- name: Build with UniFFI support on Rust ${{ matrix.toolchain }}
26+
run: cargo build --features uniffi --verbose --color always
2527
- name: Check release build on Rust ${{ matrix.toolchain }}
2628
run: cargo check --release --verbose --color always
29+
- name: Check release build with UniFFI support on Rust ${{ matrix.toolchain }}
30+
run: cargo check --release --features uniffi --verbose --color always
31+
- name: Test on Rust ${{ matrix.toolchain }}
32+
run: cargo test
33+
- name: Test with UniFFI support on Rust ${{ matrix.toolchain }}
34+
run: cargo test --features uniffi
2735
- name: Check formatting on Rust ${{ matrix.toolchain }}
2836
if: matrix.check-fmt
2937
run: rustup component add rustfmt && cargo fmt --all -- --check
30-
- name: Test on Rust ${{ matrix.toolchain }}
31-
run: cargo test

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ opt-level = 'z' # Optimize for size.
2727
lto = true # Enable Link Time Optimization
2828
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
2929
panic = 'abort' # Abort on panic
30-
strip = true # Strip symbols from binary*
30+
31+
[features]
32+
default = []
3133

3234
[dependencies]
3335
lightning = { version = "0.0.115", features = ["max_level_trace", "std"] }
@@ -67,7 +69,7 @@ serde_json = { version = "1.0" }
6769
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "sync" ] }
6870
esplora-client = { version = "0.4", default-features = false }
6971
libc = "0.2"
70-
uniffi = { version = "0.23.0", features = ["build"] }
72+
uniffi = { version = "0.23.0", features = ["build"], optional = true }
7173

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

7880
[build-dependencies]
79-
uniffi = { version = "0.23.0", features = ["build", "cli"] }
80-
81+
uniffi = { version = "0.23.0", features = ["build", "cli"], optional = true }
8182

8283
[profile.release]
8384
panic = "abort"

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ The primary abstraction of the library is the `Node`, which can be retrieved by
1111
use ldk_node::{Builder, NetAddress};
1212
use ldk_node::lightning_invoice::Invoice;
1313
use ldk_node::bitcoin::secp256k1::PublicKey;
14+
use ldk_node::bitcoin::Network;
1415
use std::str::FromStr;
1516

1617
fn main() {
17-
let node = Builder::new()
18-
.set_network("testnet")
19-
.set_esplora_server_url("https://blockstream.info/testnet/api".to_string())
20-
.build();
18+
let mut builder = Builder::new();
19+
builder.set_network(Network::Testnet);
20+
builder.set_esplora_server_url("https://blockstream.info/testnet/api".to_string());
2121

22+
let node = builder.build();
2223
node.start().unwrap();
2324

2425
let _funding_address = node.new_funding_address();

bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode/LibraryTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ class LibraryTest {
6161
@Test fun fullCycle() {
6262
setup()
6363

64-
val network: Network = "regtest"
65-
assertEquals(network, "regtest")
64+
val network = Network.REGTEST
6665

6766
val tmpDir1 = createTempDirectory("ldk_node").toString()
6867
println("Random dir 1: $tmpDir1")

bindings/ldk_node.udl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace ldk_node {
33

44
dictionary Config {
55
string storage_dir_path;
6-
string esplora_server_url;
76
Network network;
87
NetAddress? listening_address;
98
u32 default_cltv_expiry_delta;
@@ -13,6 +12,15 @@ interface Builder {
1312
constructor();
1413
[Name=from_config]
1514
constructor(Config config);
15+
void set_entropy_seed_path(string seed_path);
16+
void set_entropy_seed_bytes(sequence<u8> seed_bytes);
17+
void set_entropy_bip39_mnemonic(Mnemonic mnemonic, string? passphrase);
18+
void set_esplora_server(string esplora_server_url);
19+
void set_gossip_source_p2p();
20+
void set_gossip_source_rgs(string rgs_server_url);
21+
void set_storage_dir_path(string storage_dir_path);
22+
void set_network(Network network);
23+
void set_listening_address(NetAddress listening_address);
1624
LDKNode build();
1725
};
1826

@@ -85,6 +93,7 @@ enum NodeError {
8593
"InvalidAddress",
8694
"InvalidNetAddress",
8795
"InvalidPublicKey",
96+
"InvalidSecretKey",
8897
"InvalidPaymentHash",
8998
"InvalidPaymentPreimage",
9099
"InvalidPaymentSecret",
@@ -117,6 +126,13 @@ enum PaymentStatus {
117126
"Failed",
118127
};
119128

129+
enum Network {
130+
"Bitcoin",
131+
"Testnet",
132+
"Signet",
133+
"Regtest",
134+
};
135+
120136
dictionary PaymentDetails {
121137
PaymentHash hash;
122138
PaymentPreimage? preimage;
@@ -187,4 +203,4 @@ typedef string ChannelId;
187203
typedef string UserChannelId;
188204

189205
[Custom]
190-
typedef string Network;
206+
typedef string Mnemonic;

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fn main() {
2+
#[cfg(feature = "uniffi")]
23
uniffi::generate_scaffolding("bindings/ldk_node.udl").unwrap();
34
}

scripts/uniffi_bindgen_generate_kotlin.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212

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

1818
mkdir -p "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin/"$PACKAGE_DIR" || exit 1

scripts/uniffi_bindgen_generate_kotlin_android.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ LLVM_ARCH_PATH="darwin-x86_64"
99
PATH="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$LLVM_ARCH_PATH/bin:$PATH"
1010

1111
rustup +nightly target add x86_64-linux-android aarch64-linux-android armv7-linux-androideabi
12-
#cargo build --release || exit 1
13-
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
14-
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
15-
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
12+
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
13+
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
14+
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
1615
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language kotlin -o "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin || exit 1
1716

1817
JNI_LIB_DIR="$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/jniLibs/

scripts/uniffi_bindgen_generate_python.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
BINDINGS_DIR="./bindings/python"
33
UNIFFI_BINDGEN_BIN="cargo +nightly run --features=uniffi/cli --bin uniffi-bindgen"
44

5-
cargo +nightly build --release || exit 1
5+
cargo +nightly build --release --features uniffi || exit 1
66
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language python -o "$BINDINGS_DIR" || exit 1
77
cp ./target/release/libldk_node.dylib "$BINDINGS_DIR"/libldk_node.dylib || exit 1

scripts/uniffi_bindgen_generate_swift.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ rustup target add aarch64-apple-ios-sim --toolchain nightly
1515
rustup target add aarch64-apple-darwin x86_64-apple-darwin
1616

1717
# Build rust target libs
18-
cargo build --profile release-smaller || exit 1
19-
cargo build --profile release-smaller --target x86_64-apple-darwin || exit 1
20-
cargo build --profile release-smaller --target aarch64-apple-darwin || exit 1
21-
cargo build --profile release-smaller --target x86_64-apple-ios || exit 1
22-
cargo build --profile release-smaller --target aarch64-apple-ios || exit 1
23-
cargo +nightly build --release --target aarch64-apple-ios-sim || exit 1
18+
cargo build --profile release-smaller --features uniffi || exit 1
19+
cargo build --profile release-smaller --features uniffi --target x86_64-apple-darwin || exit 1
20+
cargo build --profile release-smaller --features uniffi --target aarch64-apple-darwin || exit 1
21+
cargo build --profile release-smaller --features uniffi --target x86_64-apple-ios || exit 1
22+
cargo build --profile release-smaller --features uniffi --target aarch64-apple-ios || exit 1
23+
cargo +nightly build --release --features uniffi --target aarch64-apple-ios-sim || exit 1
2424

2525
# Combine ios-sim and apple-darwin (macos) libs for x86_64 and aarch64 (m1)
2626
mkdir -p target/lipo-ios-sim/release-smaller || exit 1

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub enum Error {
3737
InvalidNetAddress,
3838
/// The given public key is invalid.
3939
InvalidPublicKey,
40+
/// The given secret key is invalid.
41+
InvalidSecretKey,
4042
/// The given payment hash is invalid.
4143
InvalidPaymentHash,
4244
/// The given payment preimage is invalid.
@@ -79,6 +81,7 @@ impl fmt::Display for Error {
7981
Self::InvalidAddress => write!(f, "The given address is invalid."),
8082
Self::InvalidNetAddress => write!(f, "The given network address is invalid."),
8183
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
84+
Self::InvalidSecretKey => write!(f, "The given secret key is invalid."),
8285
Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
8386
Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),
8487
Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),

src/hex_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt::Write;
22

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

0 commit comments

Comments
 (0)