Add Network enum as wallet constructor param
This commit is contained in:
parent
58ef298a42
commit
fffb2e2cbc
@ -41,4 +41,4 @@ See the [UniFFI User Guide](https://mozilla.github.io/uniffi-rs/)
|
|||||||
|
|
||||||
1. Use `build.sh` script (TODO do it all in build.rs instead)
|
1. Use `build.sh` script (TODO do it all in build.rs instead)
|
||||||
2. Create tests in `bindings/bdk-kotlin` and/or `bindings/bdk-swift`
|
2. Create tests in `bindings/bdk-kotlin` and/or `bindings/bdk-swift`
|
||||||
3. Use `test.sh` to run all bindings tests
|
3. Use `test.sh` to run all bindings tests
|
||||||
|
@ -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_f91_rustbuffer_alloc(size, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f470_rustbuffer_alloc(size, status)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
|
internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.ffi_bdk_f91_rustbuffer_free(buf, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f470_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_f91_rustbuffer_reserve(buf, additional, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f470_rustbuffer_reserve(buf, additional, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,6 +261,12 @@ internal fun String.write(buf: RustBufferBuilder) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -295,31 +301,31 @@ internal interface _UniFFILib : Library {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ffi_bdk_f91_OfflineWallet_object_free(ptr: Pointer,
|
fun ffi_bdk_f470_OfflineWallet_object_free(ptr: Pointer,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): Unit
|
): Unit
|
||||||
|
|
||||||
fun bdk_f91_OfflineWallet_new(descriptor: RustBuffer.ByValue,database_config: RustBuffer.ByValue,
|
fun bdk_f470_OfflineWallet_new(network: RustBuffer.ByValue,descriptor: RustBuffer.ByValue,database_config: RustBuffer.ByValue,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): Pointer
|
): Pointer
|
||||||
|
|
||||||
fun bdk_f91_OfflineWallet_get_new_address(ptr: Pointer,
|
fun bdk_f470_OfflineWallet_get_new_address(ptr: Pointer,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
fun ffi_bdk_f91_rustbuffer_alloc(size: Int,
|
fun ffi_bdk_f470_rustbuffer_alloc(size: Int,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
fun ffi_bdk_f91_rustbuffer_from_bytes(bytes: ForeignBytes.ByValue,
|
fun ffi_bdk_f470_rustbuffer_from_bytes(bytes: ForeignBytes.ByValue,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
fun ffi_bdk_f91_rustbuffer_free(buf: RustBuffer.ByValue,
|
fun ffi_bdk_f470_rustbuffer_free(buf: RustBuffer.ByValue,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): Unit
|
): Unit
|
||||||
|
|
||||||
fun ffi_bdk_f91_rustbuffer_reserve(buf: RustBuffer.ByValue,additional: Int,
|
fun ffi_bdk_f470_rustbuffer_reserve(buf: RustBuffer.ByValue,additional: Int,
|
||||||
uniffi_out_err: RustCallStatus
|
uniffi_out_err: RustCallStatus
|
||||||
): RustBuffer.ByValue
|
): RustBuffer.ByValue
|
||||||
|
|
||||||
@ -498,6 +504,35 @@ abstract class FFIObject(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
enum class Network {
|
||||||
|
BITCOIN,TESTNET,SIGNET,REGTEST;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
internal fun lift(rbuf: RustBuffer.ByValue): Network {
|
||||||
|
return liftFromRustBuffer(rbuf) { buf -> Network.read(buf) }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun read(buf: ByteBuffer) =
|
||||||
|
try { values()[buf.getInt() - 1] }
|
||||||
|
catch (e: IndexOutOfBoundsException) {
|
||||||
|
throw RuntimeException("invalid enum value, something is very wrong!!", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun lower(): RustBuffer.ByValue {
|
||||||
|
return lowerIntoRustBuffer(this, {v, buf -> v.write(buf)})
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun write(buf: RustBufferBuilder) {
|
||||||
|
buf.putInt(this.ordinal + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -769,10 +804,10 @@ public interface OfflineWalletInterface {
|
|||||||
class OfflineWallet(
|
class OfflineWallet(
|
||||||
pointer: Pointer
|
pointer: Pointer
|
||||||
) : FFIObject(pointer), OfflineWalletInterface {
|
) : FFIObject(pointer), OfflineWalletInterface {
|
||||||
constructor(descriptor: String, databaseConfig: DatabaseConfig ) :
|
constructor(network: Network, descriptor: String, databaseConfig: DatabaseConfig ) :
|
||||||
this(
|
this(
|
||||||
rustCallWithError(BdkException) { status ->
|
rustCallWithError(BdkException) { status ->
|
||||||
_UniFFILib.INSTANCE.bdk_f91_OfflineWallet_new(descriptor.lower(), databaseConfig.lower() ,status)
|
_UniFFILib.INSTANCE.bdk_f470_OfflineWallet_new(network.lower(), descriptor.lower(), databaseConfig.lower() ,status)
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -785,7 +820,7 @@ class OfflineWallet(
|
|||||||
*/
|
*/
|
||||||
override protected fun freeRustArcPtr() {
|
override protected fun freeRustArcPtr() {
|
||||||
rustCall() { status ->
|
rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.ffi_bdk_f91_OfflineWallet_object_free(this.pointer, status)
|
_UniFFILib.INSTANCE.ffi_bdk_f470_OfflineWallet_object_free(this.pointer, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -800,7 +835,7 @@ class OfflineWallet(
|
|||||||
override fun getNewAddress(): String =
|
override fun getNewAddress(): String =
|
||||||
callWithPointer {
|
callWithPointer {
|
||||||
rustCall() { status ->
|
rustCall() { status ->
|
||||||
_UniFFILib.INSTANCE.bdk_f91_OfflineWallet_get_new_address(it, status)
|
_UniFFILib.INSTANCE.bdk_f470_OfflineWallet_get_new_address(it, status)
|
||||||
}
|
}
|
||||||
}.let {
|
}.let {
|
||||||
String.lift(it)
|
String.lift(it)
|
||||||
|
@ -15,7 +15,7 @@ class LibTest {
|
|||||||
@Test
|
@Test
|
||||||
fun memoryWalletNewAddress() {
|
fun memoryWalletNewAddress() {
|
||||||
val config = DatabaseConfig.Memory("")
|
val config = DatabaseConfig.Memory("")
|
||||||
val wallet = OfflineWallet(desc, config)
|
val wallet = OfflineWallet(Network.REGTEST, desc, config)
|
||||||
val address = wallet.getNewAddress()
|
val address = wallet.getNewAddress()
|
||||||
assertNotNull(address)
|
assertNotNull(address)
|
||||||
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
|
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
|
||||||
@ -24,13 +24,13 @@ class LibTest {
|
|||||||
@Test(expected=BdkException.Descriptor::class)
|
@Test(expected=BdkException.Descriptor::class)
|
||||||
fun invalidDescriptorExceptionIsThrown() {
|
fun invalidDescriptorExceptionIsThrown() {
|
||||||
val config = DatabaseConfig.Memory("")
|
val config = DatabaseConfig.Memory("")
|
||||||
OfflineWallet("invalid-descriptor", config)
|
OfflineWallet(Network.REGTEST, "invalid-descriptor", config)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun sledWalletNewAddress() {
|
fun sledWalletNewAddress() {
|
||||||
val config = DatabaseConfig.Sled(SledDbConfiguration("/tmp/testdb", "testdb"))
|
val config = DatabaseConfig.Sled(SledDbConfiguration("/tmp/testdb", "testdb"))
|
||||||
val wallet = OfflineWallet(desc, config)
|
val wallet = OfflineWallet(Network.REGTEST, desc, config)
|
||||||
val address = wallet.getNewAddress()
|
val address = wallet.getNewAddress()
|
||||||
assertNotNull(address)
|
assertNotNull(address)
|
||||||
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
|
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
|
||||||
|
@ -44,6 +44,13 @@ enum BdkError {
|
|||||||
"Sled",
|
"Sled",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum Network {
|
||||||
|
"Bitcoin",
|
||||||
|
"Testnet",
|
||||||
|
"Signet",
|
||||||
|
"Regtest",
|
||||||
|
};
|
||||||
|
|
||||||
dictionary SledDbConfiguration {
|
dictionary SledDbConfiguration {
|
||||||
string path;
|
string path;
|
||||||
string tree_name;
|
string tree_name;
|
||||||
@ -57,6 +64,6 @@ interface DatabaseConfig {
|
|||||||
|
|
||||||
interface OfflineWallet {
|
interface OfflineWallet {
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
constructor(string descriptor, DatabaseConfig database_config);
|
constructor(Network network, string descriptor, DatabaseConfig database_config);
|
||||||
string get_new_address();
|
string get_new_address();
|
||||||
};
|
};
|
||||||
|
17
src/lib.rs
17
src/lib.rs
@ -7,6 +7,8 @@ use bdk::Wallet;
|
|||||||
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
uniffi_macros::include_scaffolding!("bdk");
|
||||||
|
|
||||||
type BdkError = Error;
|
type BdkError = Error;
|
||||||
|
|
||||||
pub enum DatabaseConfig {
|
pub enum DatabaseConfig {
|
||||||
@ -14,25 +16,22 @@ pub enum DatabaseConfig {
|
|||||||
Sled { configuration: SledDbConfiguration },
|
Sled { configuration: SledDbConfiguration },
|
||||||
}
|
}
|
||||||
|
|
||||||
uniffi_macros::include_scaffolding!("bdk");
|
|
||||||
|
|
||||||
struct OfflineWallet {
|
struct OfflineWallet {
|
||||||
wallet: Mutex<Wallet<(), AnyDatabase>>,
|
wallet: Mutex<Wallet<(), AnyDatabase>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OfflineWallet {
|
impl OfflineWallet {
|
||||||
fn new(descriptor: String, database_config: DatabaseConfig) -> Result<Self, BdkError> {
|
fn new(
|
||||||
|
network: Network,
|
||||||
|
descriptor: String,
|
||||||
|
database_config: DatabaseConfig,
|
||||||
|
) -> Result<Self, BdkError> {
|
||||||
let any_database_config = match database_config {
|
let any_database_config = match database_config {
|
||||||
DatabaseConfig::Memory { .. } => AnyDatabaseConfig::Memory(()),
|
DatabaseConfig::Memory { .. } => AnyDatabaseConfig::Memory(()),
|
||||||
DatabaseConfig::Sled { configuration } => AnyDatabaseConfig::Sled(configuration),
|
DatabaseConfig::Sled { configuration } => AnyDatabaseConfig::Sled(configuration),
|
||||||
};
|
};
|
||||||
let database = AnyDatabase::from_config(&any_database_config)?;
|
let database = AnyDatabase::from_config(&any_database_config)?;
|
||||||
let wallet = Mutex::new(Wallet::new_offline(
|
let wallet = Mutex::new(Wallet::new_offline(&descriptor, None, network, database)?);
|
||||||
&descriptor,
|
|
||||||
None,
|
|
||||||
Network::Regtest,
|
|
||||||
database,
|
|
||||||
)?);
|
|
||||||
Ok(OfflineWallet { wallet })
|
Ok(OfflineWallet { wallet })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user