Skip to content

Commit dff9dfe

Browse files
committed
feat: Add Kyoto example
1 parent e5b90a4 commit dff9dfe

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

bdk-jvm/examples/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repositories {
1515
dependencies {
1616
implementation(project(":lib"))
1717
testImplementation(kotlin("test"))
18+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
1819
}
1920

2021
tasks.test {
@@ -48,5 +49,12 @@ tasks.register<JavaExec>("MultisigTransaction") {
4849
classpath = sourceSets["main"].runtimeClasspath
4950
}
5051

52+
tasks.register<JavaExec>("Kyoto") {
53+
group = "application"
54+
description = "Runs the main function in the Kyoto example"
55+
mainClass.set("org.bitcoindevkit.KyotoKt")
56+
classpath = sourceSets["main"].runtimeClasspath
57+
}
58+
5159

5260

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.bitcoindevkit
2+
3+
import kotlinx.coroutines.cancelAndJoin
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.runBlocking
6+
import java.nio.file.Files
7+
import java.nio.file.Paths
8+
9+
fun main() {
10+
// Regtest environment must have Compact block filter enabled to run this
11+
// Setup your environment according to the https://github.com/thunderbiscuit/podman-regtest-infinity-pro documentation.
12+
13+
// Add your regtest IP address
14+
val ip: IpAddress = IpAddress.fromIpv4(127u, 0u, 0u, 1u)
15+
val peer: Peer = Peer(ip, 18444u, false)
16+
val peers: List<Peer> = listOf(peer)
17+
val currentPath = Paths.get(".").toAbsolutePath().normalize()
18+
val persistenceFilePath = Files.createTempDirectory(currentPath, "tempDirPrefix_")
19+
20+
val wallet = getNewWallet(ActiveWalletScriptType.P2WPKH, Network.REGTEST)
21+
val address = wallet.revealNextAddress(KeychainKind.EXTERNAL)
22+
23+
// Fund this address. Send coins from your regtest to this address
24+
println("Receiving address. Send funds to this address: ${address.address}")
25+
26+
// Wait 70 seconds for funds to arrive before syncing
27+
println("Waiting 70 seconds for funds to arrive here ${address.address} before kyoto (compact block filter syncing) ...")
28+
Thread.sleep(70000)
29+
30+
// Create CBF node and client
31+
runBlocking {
32+
val lightClient = CbfBuilder()
33+
.peers(peers)
34+
.connections(1u)
35+
.scanType(ScanType.New)
36+
.dataDir(persistenceFilePath.toString())
37+
.build(wallet)
38+
val client = lightClient.client
39+
val node = lightClient.node
40+
val logJob = launch {
41+
while (true) {
42+
val log = client.nextLog()
43+
println(log)
44+
}
45+
}
46+
//Start CBF node
47+
node.run()
48+
println("Node running")
49+
50+
//Update wallet
51+
val update: Update = client.update()
52+
wallet.applyUpdate(update)
53+
println("Wallet balance: ${wallet.balance().total.toSat()}")
54+
55+
if(wallet.balance().total.toSat() > 0uL) {
56+
println("Wallet is synced and ready for use!")
57+
println("Test completed successfully")
58+
}else{
59+
println("Wallet balance is 0. Try sending funds to ${address.address} and try again.")
60+
}
61+
logJob.cancelAndJoin()
62+
client.shutdown()
63+
}
64+
}

0 commit comments

Comments
 (0)