Allow using configs for database
This commit is contained in:
parent
23c17ca841
commit
6f01c38a71
@ -44,15 +44,15 @@ open class RustBuffer : Structure() {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
internal fun alloc(size: Int = 0) = rustCall() { status ->
|
internal fun alloc(size: Int = 0) = rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.ffi_bdk_a71d_rustbuffer_alloc(size, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f91_rustbuffer_alloc(size, status)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
|
internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.ffi_bdk_a71d_rustbuffer_free(buf, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f91_rustbuffer_free(buf, status)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun reserve(buf: RustBuffer.ByValue, additional: Int) = rustCall() { status ->
|
internal fun reserve(buf: RustBuffer.ByValue, additional: Int) = rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.ffi_bdk_a71d_rustbuffer_reserve(buf, additional, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f91_rustbuffer_reserve(buf, additional, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,6 +247,18 @@ internal fun String.write(buf: RustBufferBuilder) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -283,31 +295,31 @@ internal interface _UniFFILib : Library {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ffi_bdk_a71d_OfflineWallet_object_free(ptr: Pointer,
|
fun ffi_bdk_f91_OfflineWallet_object_free(ptr: Pointer,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): Unit
|
): Unit
|
||||||
|
|
||||||
fun bdk_a71d_OfflineWallet_new(descriptor: RustBuffer.ByValue,
|
fun bdk_f91_OfflineWallet_new(descriptor: RustBuffer.ByValue,database_config: RustBuffer.ByValue,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): Pointer
|
): Pointer
|
||||||
|
|
||||||
fun bdk_a71d_OfflineWallet_get_new_address(ptr: Pointer,
|
fun bdk_f91_OfflineWallet_get_new_address(ptr: Pointer,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
fun ffi_bdk_a71d_rustbuffer_alloc(size: Int,
|
fun ffi_bdk_f91_rustbuffer_alloc(size: Int,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
fun ffi_bdk_a71d_rustbuffer_from_bytes(bytes: ForeignBytes.ByValue,
|
fun ffi_bdk_f91_rustbuffer_from_bytes(bytes: ForeignBytes.ByValue,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
fun ffi_bdk_a71d_rustbuffer_free(buf: RustBuffer.ByValue,
|
fun ffi_bdk_f91_rustbuffer_free(buf: RustBuffer.ByValue,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): Unit
|
): Unit
|
||||||
|
|
||||||
fun ffi_bdk_a71d_rustbuffer_reserve(buf: RustBuffer.ByValue,additional: Int,
|
fun ffi_bdk_f91_rustbuffer_reserve(buf: RustBuffer.ByValue,additional: Int,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
@ -481,6 +493,66 @@ abstract class FFIObject(
|
|||||||
|
|
||||||
// Public interface members begin here.
|
// Public interface members begin here.
|
||||||
// Public facing enums
|
// Public facing enums
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
sealed class DatabaseConfig {
|
||||||
|
|
||||||
|
data class Memory(
|
||||||
|
val junk: String
|
||||||
|
) : DatabaseConfig()
|
||||||
|
|
||||||
|
data class Sled(
|
||||||
|
val configuration: SledDbConfiguration
|
||||||
|
) : DatabaseConfig()
|
||||||
|
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
internal fun lift(rbuf: RustBuffer.ByValue): DatabaseConfig {
|
||||||
|
return liftFromRustBuffer(rbuf) { buf -> DatabaseConfig.read(buf) }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun read(buf: ByteBuffer): DatabaseConfig {
|
||||||
|
return when(buf.getInt()) {
|
||||||
|
1 -> DatabaseConfig.Memory(
|
||||||
|
String.read(buf)
|
||||||
|
)
|
||||||
|
2 -> DatabaseConfig.Sled(
|
||||||
|
SledDbConfiguration.read(buf)
|
||||||
|
)
|
||||||
|
else -> throw RuntimeException("invalid enum value, something is very wrong!!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun lower(): RustBuffer.ByValue {
|
||||||
|
return lowerIntoRustBuffer(this, {v, buf -> v.write(buf)})
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun write(buf: RustBufferBuilder) {
|
||||||
|
when(this) {
|
||||||
|
is DatabaseConfig.Memory -> {
|
||||||
|
buf.putInt(1)
|
||||||
|
this.junk.write(buf)
|
||||||
|
|
||||||
|
}
|
||||||
|
is DatabaseConfig.Sled -> {
|
||||||
|
buf.putInt(2)
|
||||||
|
this.configuration.write(buf)
|
||||||
|
|
||||||
|
}
|
||||||
|
}.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Error definitions
|
// Error definitions
|
||||||
@Structure.FieldOrder("code", "error_buf")
|
@Structure.FieldOrder("code", "error_buf")
|
||||||
internal open class RustCallStatus : Structure() {
|
internal open class RustCallStatus : Structure() {
|
||||||
@ -649,6 +721,39 @@ private inline fun <U> rustCall(callback: (RustCallStatus) -> U): U {
|
|||||||
|
|
||||||
// Public facing records
|
// Public facing records
|
||||||
|
|
||||||
|
data class SledDbConfiguration (
|
||||||
|
var path: String,
|
||||||
|
var treeName: String
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
internal fun lift(rbuf: RustBuffer.ByValue): SledDbConfiguration {
|
||||||
|
return liftFromRustBuffer(rbuf) { buf -> SledDbConfiguration.read(buf) }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun read(buf: ByteBuffer): SledDbConfiguration {
|
||||||
|
return SledDbConfiguration(
|
||||||
|
String.read(buf),
|
||||||
|
String.read(buf)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun lower(): RustBuffer.ByValue {
|
||||||
|
return lowerIntoRustBuffer(this, {v, buf -> v.write(buf)})
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun write(buf: RustBufferBuilder) {
|
||||||
|
this.path.write(buf)
|
||||||
|
|
||||||
|
this.treeName.write(buf)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Namespace functions
|
// Namespace functions
|
||||||
|
|
||||||
|
|
||||||
@ -664,10 +769,10 @@ public interface OfflineWalletInterface {
|
|||||||
class OfflineWallet(
|
class OfflineWallet(
|
||||||
pointer: Pointer
|
pointer: Pointer
|
||||||
) : FFIObject(pointer), OfflineWalletInterface {
|
) : FFIObject(pointer), OfflineWalletInterface {
|
||||||
constructor(descriptor: String ) :
|
constructor(descriptor: String, databaseConfig: DatabaseConfig ) :
|
||||||
this(
|
this(
|
||||||
rustCallWithError(BdkException) { status ->
|
rustCallWithError(BdkException) { status ->
|
||||||
_UniFFILib.INSTANCE.bdk_a71d_OfflineWallet_new(descriptor.lower() ,status)
|
_UniFFILib.INSTANCE.bdk_f91_OfflineWallet_new(descriptor.lower(), databaseConfig.lower() ,status)
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -680,7 +785,7 @@ class OfflineWallet(
|
|||||||
*/
|
*/
|
||||||
override protected fun freeRustArcPtr() {
|
override protected fun freeRustArcPtr() {
|
||||||
rustCall() { status ->
|
rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.ffi_bdk_a71d_OfflineWallet_object_free(this.pointer, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f91_OfflineWallet_object_free(this.pointer, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -695,7 +800,7 @@ class OfflineWallet(
|
|||||||
override fun getNewAddress(): String =
|
override fun getNewAddress(): String =
|
||||||
callWithPointer {
|
callWithPointer {
|
||||||
rustCall() { status ->
|
rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.bdk_a71d_OfflineWallet_get_new_address(it, status)
|
_UniFFILib.INSTANCE.bdk_f91_OfflineWallet_get_new_address(it, status)
|
||||||
}
|
}
|
||||||
}.let {
|
}.let {
|
||||||
String.lift(it)
|
String.lift(it)
|
||||||
|
@ -14,7 +14,8 @@ class LibTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun walletNewAddress() {
|
fun walletNewAddress() {
|
||||||
val wallet = OfflineWallet(desc)
|
val config = DatabaseConfig.Memory("")
|
||||||
|
val wallet = OfflineWallet(desc, config)
|
||||||
val address = wallet.getNewAddress()
|
val address = wallet.getNewAddress()
|
||||||
assertNotNull(address)
|
assertNotNull(address)
|
||||||
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
|
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
|
||||||
@ -22,6 +23,7 @@ class LibTest {
|
|||||||
|
|
||||||
@Test(expected=BdkException.Descriptor::class)
|
@Test(expected=BdkException.Descriptor::class)
|
||||||
fun invalidDescriptorExceptionIsThrown() {
|
fun invalidDescriptorExceptionIsThrown() {
|
||||||
OfflineWallet("invalid-descriptor")
|
val config = DatabaseConfig.Memory("")
|
||||||
|
OfflineWallet("invalid-descriptor", config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
src/bdk.udl
13
src/bdk.udl
@ -44,8 +44,19 @@ enum BdkError {
|
|||||||
"Sled",
|
"Sled",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
dictionary SledDbConfiguration {
|
||||||
|
string path;
|
||||||
|
string tree_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
[Enum]
|
||||||
|
interface DatabaseConfig {
|
||||||
|
Memory(string junk);
|
||||||
|
Sled(SledDbConfiguration configuration);
|
||||||
|
};
|
||||||
|
|
||||||
interface OfflineWallet {
|
interface OfflineWallet {
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
constructor(string descriptor);
|
constructor(string descriptor, DatabaseConfig database_config);
|
||||||
string get_new_address();
|
string get_new_address();
|
||||||
};
|
};
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -1,5 +1,6 @@
|
|||||||
use bdk::bitcoin::Network;
|
use bdk::bitcoin::Network;
|
||||||
use bdk::database::MemoryDatabase;
|
use bdk::database::any::{AnyDatabase, SledDbConfiguration};
|
||||||
|
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
|
||||||
use bdk::wallet::AddressIndex;
|
use bdk::wallet::AddressIndex;
|
||||||
use bdk::Error;
|
use bdk::Error;
|
||||||
use bdk::Wallet;
|
use bdk::Wallet;
|
||||||
@ -8,14 +9,24 @@ use std::sync::Mutex;
|
|||||||
|
|
||||||
type BdkError = Error;
|
type BdkError = Error;
|
||||||
|
|
||||||
|
pub enum DatabaseConfig {
|
||||||
|
Memory { junk: String },
|
||||||
|
Sled { configuration: SledDbConfiguration },
|
||||||
|
}
|
||||||
|
|
||||||
uniffi_macros::include_scaffolding!("bdk");
|
uniffi_macros::include_scaffolding!("bdk");
|
||||||
|
|
||||||
struct OfflineWallet {
|
struct OfflineWallet {
|
||||||
wallet: Mutex<Wallet<(), MemoryDatabase>>,
|
wallet: Mutex<Wallet<(), AnyDatabase>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OfflineWallet {
|
impl OfflineWallet {
|
||||||
fn new(descriptor: String) -> Result<Self, BdkError> {
|
fn new(descriptor: String, database_config: DatabaseConfig) -> Result<Self, BdkError> {
|
||||||
let database = MemoryDatabase::default();
|
let any_database_config = match database_config {
|
||||||
|
DatabaseConfig::Memory { .. } => AnyDatabaseConfig::Memory(()),
|
||||||
|
DatabaseConfig::Sled { configuration } => AnyDatabaseConfig::Sled(configuration),
|
||||||
|
};
|
||||||
|
let database = AnyDatabase::from_config(&any_database_config)?;
|
||||||
let wallet = Mutex::new(Wallet::new_offline(
|
let wallet = Mutex::new(Wallet::new_offline(
|
||||||
&descriptor,
|
&descriptor,
|
||||||
None,
|
None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user