Skip to content

Commit b5159d1

Browse files
chore: add bdk-jvm library from bdk-ffi v2.0.0
1 parent de803cc commit b5159d1

22 files changed

+1056
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build/
2+
.gradle/
3+
bdk.kt
4+
libbdkffi.dylib

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# bdk-jvm
2+
3+
This project builds a .jar package for the JVM platform that provides Kotlin language bindings for the [BDK] libraries. The Kotlin language bindings are created by the `bdk-ffi` project which is included in the root of this repository.
4+
5+
## How to Use
6+
7+
To use the Kotlin language bindings for BDK in your JVM project add the following to your gradle dependencies:
8+
9+
```kotlin
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
implementation("org.bitcoindevkit:bdk-jvm:<version>")
16+
}
17+
```
18+
19+
### Snapshot releases
20+
21+
To use a snapshot release, specify the snapshot repository url in the `repositories` block and use the snapshot version in the `dependencies` block:
22+
23+
```kotlin
24+
repositories {
25+
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
26+
}
27+
28+
dependencies {
29+
implementation("org.bitcoindevkit:bdk-jvm:<version-SNAPSHOT>")
30+
}
31+
```
32+
33+
## Example Projects
34+
35+
- [Tatooine Faucet](https://github.com/thunderbiscuit/tatooine)
36+
- [Godzilla Wallet](https://github.com/thunderbiscuit/godzilla-wallet)
37+
38+
## How to build
39+
40+
_Note that Kotlin version `1.9.23` or later is required to build the library._
41+
1. Install JDK 17. For example, with SDKMAN!:
42+
```shell
43+
curl -s "https://get.sdkman.io" | bash
44+
source "$HOME/.sdkman/bin/sdkman-init.sh"
45+
sdk install java 17.0.2-tem
46+
```
47+
2. Build kotlin bindings
48+
```sh
49+
bash ./scripts/build-<your-local-architecture>.sh
50+
```
51+
52+
## How to publish to your local Maven repo
53+
54+
```shell
55+
cd bdk-jvm
56+
./gradlew publishToMavenLocal -P localBuild
57+
```
58+
59+
Note that the commands assume you don't need the local libraries to be signed. If you do wish to sign them, simply set your `~/.gradle/gradle.properties` signing key values like so:
60+
61+
```properties
62+
signing.gnupg.keyName=<YOUR_GNUPG_ID>
63+
signing.gnupg.passphrase=<YOUR_GNUPG_PASSPHRASE>
64+
```
65+
66+
and use the `publishToMavenLocal` task without the `localBuild` flag:
67+
68+
```shell
69+
./gradlew publishToMavenLocal
70+
```
71+
72+
## Known issues
73+
74+
## JNA dependency
75+
76+
Depending on the JVM version you use, you might not have the JNA dependency on your classpath. The exception thrown will be
77+
```shell
78+
class file for com.sun.jna.Pointer not found
79+
```
80+
81+
The solution is to add JNA as a dependency like so:
82+
```kotlin
83+
dependencies {
84+
// ...
85+
implementation("net.java.dev.jna:jna:5.12.1")
86+
}
87+
```
88+
89+
[BDK]: https://github.com/bitcoindevkit/
90+
[`bdk-ffi`]: https://github.com/bitcoindevkit/bdk-ffi

build.gradle.kts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
plugins {
2+
id("org.jetbrains.kotlin.jvm").version("2.1.10").apply(false)
3+
id("org.gradle.java-library")
4+
id("org.gradle.maven-publish")
5+
id("org.gradle.signing")
6+
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
7+
id("org.jetbrains.dokka").version("2.0.0").apply(false)
8+
id("org.jetbrains.dokka-javadoc").version("2.0.0").apply(false)
9+
}
10+
11+
// library version is defined in gradle.properties
12+
val libraryVersion: String by project
13+
14+
// These properties are required here so that the nexus publish-plugin
15+
// finds a staging profile with the correct group (group is otherwise set as "")
16+
// and knows whether to publish to a SNAPSHOT repository or not
17+
// https://github.com/gradle-nexus/publish-plugin#applying-the-plugin
18+
group = "org.bitcoindevkit"
19+
version = libraryVersion
20+
21+
nexusPublishing {
22+
repositories {
23+
create("sonatype") {
24+
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
25+
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
26+
27+
val ossrhUsername: String? by project
28+
val ossrhPassword: String? by project
29+
username.set(ossrhUsername)
30+
password.set(ossrhPassword)
31+
}
32+
}
33+
}

examples/build.gradle.kts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
3+
plugins {
4+
kotlin("jvm") version "2.1.10"
5+
}
6+
7+
group = "org.bitcoindevkit"
8+
version = "2.0.0-SNAPSHOT"
9+
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
implementation(project(":lib"))
16+
testImplementation(kotlin("test"))
17+
}
18+
19+
tasks.test {
20+
useJUnitPlatform()
21+
}
22+
23+
java {
24+
sourceCompatibility = JavaVersion.VERSION_11
25+
targetCompatibility = JavaVersion.VERSION_11
26+
withSourcesJar()
27+
withJavadocJar()
28+
}
29+
30+
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
31+
compilerOptions {
32+
jvmTarget.set(JvmTarget.JVM_11)
33+
}
34+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.bitcoindevkit
2+
import java.io.File
3+
import java.nio.file.Paths
4+
5+
fun main(){
6+
val (descriptor, changeDescriptor) = createDescriptorsFromBip32RootKey(
7+
ActiveWalletScriptType.P2WPKH,
8+
Network.REGTEST
9+
)
10+
println("Descriptor: $descriptor")
11+
println("Change descriptor: $changeDescriptor")
12+
13+
val persistenceFilePath = getPersistenceFilePath()
14+
println("Persistence file path: $persistenceFilePath")
15+
16+
val connection: Persister = Persister.newSqlite(persistenceFilePath)
17+
val wallet = Wallet(descriptor, changeDescriptor, Network.REGTEST, connection)
18+
val changeAddress = wallet.revealNextAddress(KeychainKind.INTERNAL).address
19+
val address = wallet.revealNextAddress(KeychainKind.EXTERNAL).address
20+
21+
println("Change address: $changeAddress")
22+
println("Address: $address")
23+
}
24+
25+
fun createDescriptorsFromBip32RootKey (
26+
activeWalletScriptType: ActiveWalletScriptType,
27+
network: Network) : Array<Descriptor>{
28+
val mnemonic = Mnemonic(WordCount.WORDS12)
29+
val bip32ExtendedRootKey = DescriptorSecretKey(network, mnemonic, null)
30+
println("Bip32 root key: $bip32ExtendedRootKey")
31+
32+
val descriptor: Descriptor = createScriptAppropriateDescriptor(
33+
activeWalletScriptType,
34+
bip32ExtendedRootKey,
35+
network,
36+
KeychainKind.EXTERNAL,
37+
)
38+
val changeDescriptor: Descriptor = createScriptAppropriateDescriptor(
39+
activeWalletScriptType,
40+
bip32ExtendedRootKey,
41+
network,
42+
KeychainKind.INTERNAL,
43+
)
44+
return arrayOf(descriptor, changeDescriptor)
45+
}
46+
47+
48+
fun createScriptAppropriateDescriptor(
49+
scriptType: ActiveWalletScriptType,
50+
bip32ExtendedRootKey: DescriptorSecretKey,
51+
network: Network,
52+
keychain: KeychainKind,
53+
): Descriptor {
54+
return when (scriptType) {
55+
ActiveWalletScriptType.P2WPKH -> Descriptor.newBip84(
56+
bip32ExtendedRootKey,
57+
keychain,
58+
network
59+
)
60+
ActiveWalletScriptType.P2TR -> Descriptor.newBip86(
61+
bip32ExtendedRootKey,
62+
keychain,
63+
network
64+
)
65+
}
66+
}
67+
68+
fun getPersistenceFilePath(): String {
69+
val currentDirectory = Paths.get("").toAbsolutePath().toString() + "/bdk-jvm/examples/src/main/kotlin/bdk_persistence.sqlite"
70+
File(currentDirectory).apply {
71+
if (exists()) delete()
72+
}
73+
return currentDirectory
74+
}
75+
76+
enum class ActiveWalletScriptType {
77+
P2WPKH,
78+
P2TR,
79+
}

gradle.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
org.gradle.jvmargs=-Xmx1536m
2+
android.enableJetifier=true
3+
kotlin.code.style=official
4+
libraryVersion=2.0.0
5+
org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
6+
org.jetbrains.dokka.experimental.gradle.pluginMode.noWarn=true

gradle/wrapper/gradle-wrapper.jar

62.2 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)