Skip to content

Commit f72c646

Browse files
authored
feat: add wallet setup example for JVM binding
1 parent 0ffb921 commit f72c646

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

.github/workflows/test-jvm.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ jobs:
4141
cd bdk-jvm
4242
bash ./scripts/build-linux-x86_64.sh
4343
./gradlew test -P excludeConnectedTests
44+
./gradlew :examples:build

bdk-jvm/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+
}

bdk-jvm/settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
rootProject.name = "bdk-jvm"
22

33
include(":lib")
4+
include(":examples")
45

56
pluginManagement {
67
repositories {

0 commit comments

Comments
 (0)