Skip to content

Commit

Permalink
Temp 5: hello world library working on JVM
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Sep 25, 2023
1 parent 1a3adcb commit e50f8be
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 65 deletions.
4 changes: 3 additions & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
namespace bdk {};
namespace bdk {
string hello_world();
};
8 changes: 6 additions & 2 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ mod wallet;
// use std::str::FromStr;
// use std::sync::Arc;
//
// uniffi::include_scaffolding!("bdk");
//
uniffi::include_scaffolding!("bdk");

pub fn hello_world() -> String {
String::from("Hello World")
}

// /// A output script and an amount of satoshis.
// pub struct ScriptAmount {
// pub script: Arc<Script>,
Expand Down
4 changes: 4 additions & 0 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
// RbfValue, Script, ScriptAmount, TransactionDetails, TxBuilderResult,
// };
//

// pub fn hello_world() -> String {
// String::from("Hello World")
// }
// #[derive(Debug)]
// pub(crate) struct Wallet {
// pub(crate) inner: BdkWallet,
Expand Down
128 changes: 68 additions & 60 deletions bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,76 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Files
import org.bitcoindevkit.helloWorld

/**
* Library test, which will execute on linux host.
*/
class JvmLibTest {

private fun getTestDataDir(): String {
return Files.createTempDirectory("bdk-test").toString()
}

private fun cleanupTestDataDir(testDataDir: String) {
File(testDataDir).deleteRecursively()
}

class LogProgress : Progress {
private val log: Logger = LoggerFactory.getLogger(JvmLibTest::class.java)

override fun update(progress: Float, message: String?) {
log.debug("Syncing...")
}
}

private val descriptor = Descriptor("wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", Network.TESTNET)

private val databaseConfig = DatabaseConfig.Memory

private val blockchainConfig = BlockchainConfig.Electrum(
ElectrumConfig(
"ssl://electrum.blockstream.info:60002",
null,
5u,
null,
100u,
true,
)
)

@Test
fun memoryWalletNewAddress() {
val wallet = Wallet(descriptor, null, Network.TESTNET, databaseConfig)
val address = wallet.getAddress(AddressIndex.New).address.asString()
assertEquals("tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", address)
}

@Test
fun memoryWalletSyncGetBalance() {
val wallet = Wallet(descriptor, null, Network.TESTNET, databaseConfig)
val blockchain = Blockchain(blockchainConfig)
wallet.sync(blockchain, LogProgress())
val balance: Balance = wallet.getBalance()
assertTrue(balance.total > 0u)
}

@Test
fun sqliteWalletSyncGetBalance() {
val testDataDir = getTestDataDir() + "/bdk-wallet.sqlite"
val databaseConfig = DatabaseConfig.Sqlite(SqliteDbConfiguration(testDataDir))
val wallet = Wallet(descriptor, null, Network.TESTNET, databaseConfig)
val blockchain = Blockchain(blockchainConfig)
wallet.sync(blockchain, LogProgress())
val balance: Balance = wallet.getBalance()
assertTrue(balance.total > 0u)
cleanupTestDataDir(testDataDir)
fun testHelloWorld() {
val message = helloWorld()
assertEquals("Hello World", message)
}
}
/**
* Library test, which will execute on linux host.
*/
// class JvmLibTest {
//
// private fun getTestDataDir(): String {
// return Files.createTempDirectory("bdk-test").toString()
// }
//
// private fun cleanupTestDataDir(testDataDir: String) {
// File(testDataDir).deleteRecursively()
// }
//
// class LogProgress : Progress {
// private val log: Logger = LoggerFactory.getLogger(JvmLibTest::class.java)
//
// override fun update(progress: Float, message: String?) {
// log.debug("Syncing...")
// }
// }
//
// private val descriptor = Descriptor("wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", Network.TESTNET)
//
// private val databaseConfig = DatabaseConfig.Memory
//
// private val blockchainConfig = BlockchainConfig.Electrum(
// ElectrumConfig(
// "ssl://electrum.blockstream.info:60002",
// null,
// 5u,
// null,
// 100u,
// true,
// )
// )
//
// @Test
// fun memoryWalletNewAddress() {
// val wallet = Wallet(descriptor, null, Network.TESTNET, databaseConfig)
// val address = wallet.getAddress(AddressIndex.New).address.asString()
// assertEquals("tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", address)
// }
//
// @Test
// fun memoryWalletSyncGetBalance() {
// val wallet = Wallet(descriptor, null, Network.TESTNET, databaseConfig)
// val blockchain = Blockchain(blockchainConfig)
// wallet.sync(blockchain, LogProgress())
// val balance: Balance = wallet.getBalance()
// assertTrue(balance.total > 0u)
// }
//
// @Test
// fun sqliteWalletSyncGetBalance() {
// val testDataDir = getTestDataDir() + "/bdk-wallet.sqlite"
// val databaseConfig = DatabaseConfig.Sqlite(SqliteDbConfiguration(testDataDir))
// val wallet = Wallet(descriptor, null, Network.TESTNET, databaseConfig)
// val blockchain = Blockchain(blockchainConfig)
// wallet.sync(blockchain, LogProgress())
// val balance: Balance = wallet.getBalance()
// assertTrue(balance.total > 0u)
// cleanupTestDataDir(testDataDir)
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,18 @@ internal class UniFfiJvmPlugin : Plugin<Project> {

dependsOn(moveNativeJvmLibs)

workingDir("${project.projectDir}/../../bdk-ffi")
val cargoArgs: List<String> = listOf("run", "--bin", "uniffi-bindgen", "generate", "src/bdk.udl", "--language", "kotlin", "--out-dir", "../bdk-jvm/lib/src/main/kotlin", "--no-format")
// TODO: Is the Windows name the correct one?
// TODO: This will not work on mac Intel (x86_64 architecture)
val libraryPath = when (operatingSystem) {
OS.LINUX -> "./target/x86_64-unknown-linux-gnu/release-smaller/libbdkffi.so"
OS.MAC -> "./target/aarch64-apple-darwin/release-smaller/libbdkffi.dylib"
OS.WINDOWS -> "./target/x86_64-pc-windows-msvc/release-smaller/bdkffi.dll"
else -> throw Exception("Unsupported OS")
}

// workingDir("${project.projectDir}/../../bdk-ffi")
workingDir("${project.projectDir}/../../")
val cargoArgs: List<String> = listOf("run", "--bin", "uniffi-bindgen", "generate", "--library", libraryPath, "--language", "kotlin", "--out-dir", "./bdk-jvm/lib/src/main/kotlin/", "--no-format")

executable("cargo")
args(cargoArgs)
Expand Down

0 comments on commit e50f8be

Please sign in to comment.