Skip to content

Commit aa6b14a

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

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

bdk-jvm/examples/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ repositories {
1515
dependencies {
1616
implementation(project(":lib"))
1717
testImplementation(kotlin("test"))
18+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
19+
implementation("org.slf4j:slf4j-api:2.0.16")
20+
implementation("ch.qos.logback:logback-classic:1.5.13")
1821
}
1922

2023
tasks.test {
@@ -48,5 +51,12 @@ tasks.register<JavaExec>("MultisigTransaction") {
4851
classpath = sourceSets["main"].runtimeClasspath
4952
}
5053

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

5262

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

0 commit comments

Comments
 (0)