|
| 1 | +# ldk-swift |
| 2 | + |
| 3 | +Automatic Swift bindings generation for [`rust-lightning`](https://github.com/lightningdevkit/rust-lightning). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +In order to use the automatically generated bindings, simply drag `*.xcframework` file into an Xcode project. |
| 8 | + |
| 9 | +The `*.xcframework` files are either available on |
| 10 | +the [Releases](https://github.com/lightningdevkit/ldk-swift/releases) page, or can be [compiled](#Compilation) |
| 11 | +from scratch. |
| 12 | + |
| 13 | + |
| 14 | +## Compilation |
| 15 | + |
| 16 | +### Cloning Dependencies |
| 17 | + |
| 18 | +In order to generate these bindings from scratch, you will need to clone two dependency repositories: |
| 19 | + |
| 20 | +**rust-lightning**, (a specific branch built for bindings compatibility) |
| 21 | +```shell |
| 22 | +git clone --branch 2021-03-java-bindings-base [email protected]:TheBlueMatt/rust-lightning.git |
| 23 | +``` |
| 24 | + |
| 25 | +**ldk-c-bindings** |
| 26 | +```shell |
| 27 | +git clone [email protected]:lightningdevkit/ldk-c-bindings.git |
| 28 | +``` |
| 29 | + |
| 30 | +### Generating Rust-to-C-bindings |
| 31 | + |
| 32 | +Now, navigate to the `ldk-c-bindings` directory and run the `genbindings.sh` script: |
| 33 | + |
| 34 | +```shell |
| 35 | +cd /path/to/ldk-c-bindings |
| 36 | +./genbindings.sh /path/to/rust-lightning true |
| 37 | +``` |
| 38 | + |
| 39 | +### Generating C-to-Swift-bindings |
| 40 | + |
| 41 | +<details> |
| 42 | +<summary>If using Docker</summary> |
| 43 | + |
| 44 | +If you're using Docker to generate the Swift bindings, navigate (if you're not already there from the |
| 45 | +previous step) to the `ldk-c-bindings` directory and open the file located here: |
| 46 | + |
| 47 | +`/path/to/ldk-c-bindings/lightning-c-bindings/Cargo.toml` |
| 48 | + |
| 49 | +In that file, you will see four lines specifying the `lightning`, `lightning-persister`, `lightning-invoice`, and |
| 50 | +`lightning-background-processor` dependencies. They will most likely show local paths to the `rust-lightning` |
| 51 | +folder due to the previous `genbindings.sh` step. As Docker won't have access to local paths, |
| 52 | +replace those lines with the following: |
| 53 | + |
| 54 | +```yaml |
| 55 | +lightning = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx", features = ["std"] } |
| 56 | +lightning-persister = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx" } |
| 57 | +lightning-invoice = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx" } |
| 58 | +lightning-background-processor = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx" } |
| 59 | +``` |
| 60 | + |
| 61 | +You will note that the revision is unspecified and is currently just placeholder `xxx`s. To obtain the revision, |
| 62 | +just navigate to the just clone custom `rust-lightning` directory and run: |
| 63 | + |
| 64 | +```shell |
| 65 | +cd /path/to/rust-lightning |
| 66 | +git rev-parse HEAD |
| 67 | +``` |
| 68 | + |
| 69 | +Take that commit hash and replace the `xxx` instances with it. |
| 70 | +</details> |
| 71 | + |
| 72 | +#### Generating the Swift files |
| 73 | + |
| 74 | +To generate the Swift files, navigate to the `ldk-swift` repository and run the following: |
| 75 | + |
| 76 | +```shell |
| 77 | +EXPORT LDK_SWIFT_GENERATOR_INPUT_HEADER_PATH="/path/to/ldk-c-bindings/lightning-c-bindings/include/lightning.h" |
| 78 | +python3 ./ |
| 79 | +``` |
| 80 | + |
| 81 | +Now, the contents of the `bindings/LDK` folder will have been completely regenerated. |
| 82 | + |
| 83 | +#### Generating the *.xcframework files |
| 84 | + |
| 85 | +With the Swift files completely regenerated, you will need to make sure the Xcode projects that serve as bases |
| 86 | +for the `*.xcframework`s reference them correctly. So navigate here: |
| 87 | + |
| 88 | +`path/to/ldk-swift/xcode` |
| 89 | + |
| 90 | +and open the respective `*.xcodeproj` files located in the `LDKFramework` and `LDKFramework_Mac` directories. |
| 91 | +Do the following steps for _both_ projects: |
| 92 | + |
| 93 | +1. Remove the `LDK` references in the Xcode file tree |
| 94 | +2. Drag the `path/to/ldk-swift/bindings/LDK` folder to that same location in the file tree |
| 95 | +3. Check `Copy items if needed` and pick `Create groups` in the dialog box |
| 96 | + |
| 97 | +Next, navigate back to the `xcode` directory, and run the following scripts: |
| 98 | + |
| 99 | +```shell |
| 100 | +./compile_dependency_binaries.sh /path/to/ldk-c-bindings |
| 101 | +./build_framework.sh |
| 102 | +``` |
| 103 | + |
| 104 | +Finally, navigate to `path/to/ldk-swift/bindings/bin`, and you should find multiple `*.xcframework` files |
| 105 | +waiting for you there. |
0 commit comments