|
| 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