Share Wallet::getBalance and Wallet::sign

This commit is contained in:
Sudarsan Balaji 2021-10-16 20:25:58 +05:30
parent 58d774a3f3
commit ca0a2cba15
3 changed files with 130 additions and 94 deletions

View File

@ -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_b468_rustbuffer_alloc(size, status) _UniFFILib.INSTANCE.ffi_bdk_70ef_rustbuffer_alloc(size, status)
} }
internal fun free(buf: RustBuffer.ByValue) = rustCall() { status -> internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
_UniFFILib.INSTANCE.ffi_bdk_b468_rustbuffer_free(buf, status) _UniFFILib.INSTANCE.ffi_bdk_70ef_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_b468_rustbuffer_reserve(buf, additional, status) _UniFFILib.INSTANCE.ffi_bdk_70ef_rustbuffer_reserve(buf, additional, status)
} }
} }
@ -548,75 +548,83 @@ internal interface _UniFFILib : Library {
} }
} }
fun ffi_bdk_b468_OfflineWallet_object_free(ptr: Pointer, fun ffi_bdk_70ef_OfflineWallet_object_free(ptr: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Unit ): Unit
fun bdk_b468_OfflineWallet_new(descriptor: RustBuffer.ByValue,network: RustBuffer.ByValue,database_config: RustBuffer.ByValue, fun bdk_70ef_OfflineWallet_new(descriptor: RustBuffer.ByValue,network: RustBuffer.ByValue,database_config: RustBuffer.ByValue,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun bdk_b468_OfflineWallet_get_new_address(ptr: Pointer, fun bdk_70ef_OfflineWallet_get_new_address(ptr: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
fun ffi_bdk_b468_OnlineWallet_object_free(ptr: Pointer, fun bdk_70ef_OfflineWallet_get_balance(ptr: Pointer,
uniffi_out_err: RustCallStatus
): Unit
fun bdk_b468_OnlineWallet_new(descriptor: RustBuffer.ByValue,network: RustBuffer.ByValue,database_config: RustBuffer.ByValue,blockchain_config: RustBuffer.ByValue,
uniffi_out_err: RustCallStatus
): Pointer
fun bdk_b468_OnlineWallet_get_new_address(ptr: Pointer,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun bdk_b468_OnlineWallet_get_network(ptr: Pointer,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun bdk_b468_OnlineWallet_sync(ptr: Pointer,progress_update: Long,max_address_param: RustBuffer.ByValue,
uniffi_out_err: RustCallStatus
): Unit
fun bdk_b468_OnlineWallet_get_balance(ptr: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Long ): Long
fun bdk_b468_OnlineWallet_sign(ptr: Pointer,psbt: Pointer, fun bdk_70ef_OfflineWallet_sign(ptr: Pointer,psbt: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Unit ): Unit
fun bdk_b468_OnlineWallet_broadcast(ptr: Pointer,psbt: Pointer, fun ffi_bdk_70ef_OnlineWallet_object_free(ptr: Pointer,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun ffi_bdk_b468_PartiallySignedBitcoinTransaction_object_free(ptr: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Unit ): Unit
fun bdk_b468_PartiallySignedBitcoinTransaction_new(wallet: Pointer,recipient: RustBuffer.ByValue,amount: Long, fun bdk_70ef_OnlineWallet_new(descriptor: RustBuffer.ByValue,network: RustBuffer.ByValue,database_config: RustBuffer.ByValue,blockchain_config: RustBuffer.ByValue,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun ffi_bdk_b468_BdkProgress_init_callback(callback_stub: ForeignCallback, fun bdk_70ef_OnlineWallet_get_new_address(ptr: Pointer,
uniffi_out_err: RustCallStatus
): Unit
fun ffi_bdk_b468_rustbuffer_alloc(size: Int,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
fun ffi_bdk_b468_rustbuffer_from_bytes(bytes: ForeignBytes.ByValue, fun bdk_70ef_OnlineWallet_get_balance(ptr: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): Long
fun ffi_bdk_b468_rustbuffer_free(buf: RustBuffer.ByValue, fun bdk_70ef_OnlineWallet_sign(ptr: Pointer,psbt: Pointer,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): Unit ): Unit
fun ffi_bdk_b468_rustbuffer_reserve(buf: RustBuffer.ByValue,additional: Int, fun bdk_70ef_OnlineWallet_get_network(ptr: Pointer,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun bdk_70ef_OnlineWallet_sync(ptr: Pointer,progress_update: Long,max_address_param: RustBuffer.ByValue,
uniffi_out_err: RustCallStatus
): Unit
fun bdk_70ef_OnlineWallet_broadcast(ptr: Pointer,psbt: Pointer,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun ffi_bdk_70ef_PartiallySignedBitcoinTransaction_object_free(ptr: Pointer,
uniffi_out_err: RustCallStatus
): Unit
fun bdk_70ef_PartiallySignedBitcoinTransaction_new(wallet: Pointer,recipient: RustBuffer.ByValue,amount: Long,
uniffi_out_err: RustCallStatus
): Pointer
fun ffi_bdk_70ef_BdkProgress_init_callback(callback_stub: ForeignCallback,
uniffi_out_err: RustCallStatus
): Unit
fun ffi_bdk_70ef_rustbuffer_alloc(size: Int,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun ffi_bdk_70ef_rustbuffer_from_bytes(bytes: ForeignBytes.ByValue,
uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun ffi_bdk_70ef_rustbuffer_free(buf: RustBuffer.ByValue,
uniffi_out_err: RustCallStatus
): Unit
fun ffi_bdk_70ef_rustbuffer_reserve(buf: RustBuffer.ByValue,additional: Int,
uniffi_out_err: RustCallStatus uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
@ -1313,20 +1321,22 @@ data class EsploraConfig (
// Objects // Objects
@ExperimentalUnsignedTypes
public interface OfflineWalletInterface { public interface OfflineWalletInterface {
fun getNewAddress(): String fun getNewAddress(): String
fun getBalance(): ULong
fun sign(psbt: PartiallySignedBitcoinTransaction )
} }
@ExperimentalUnsignedTypes
class OfflineWallet( class OfflineWallet(
pointer: Pointer pointer: Pointer
) : FFIObject(pointer), OfflineWalletInterface { ) : FFIObject(pointer), OfflineWalletInterface {
constructor(descriptor: String, network: Network, databaseConfig: DatabaseConfig ) : constructor(descriptor: String, network: Network, databaseConfig: DatabaseConfig ) :
this( this(
rustCallWithError(BdkException) { status -> rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_OfflineWallet_new(descriptor.lower(), network.lower(), databaseConfig.lower() ,status) _UniFFILib.INSTANCE.bdk_70ef_OfflineWallet_new(descriptor.lower(), network.lower(), databaseConfig.lower() ,status)
}) })
/** /**
@ -1339,7 +1349,7 @@ class OfflineWallet(
*/ */
override protected fun freeRustArcPtr() { override protected fun freeRustArcPtr() {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.ffi_bdk_b468_OfflineWallet_object_free(this.pointer, status) _UniFFILib.INSTANCE.ffi_bdk_70ef_OfflineWallet_object_free(this.pointer, status)
} }
} }
@ -1354,12 +1364,28 @@ class OfflineWallet(
override fun getNewAddress(): String = override fun getNewAddress(): String =
callWithPointer { callWithPointer {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.bdk_b468_OfflineWallet_get_new_address(it, status) _UniFFILib.INSTANCE.bdk_70ef_OfflineWallet_get_new_address(it, status)
} }
}.let { }.let {
String.lift(it) String.lift(it)
} }
override fun getBalance(): ULong =
callWithPointer {
rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_70ef_OfflineWallet_get_balance(it, status)
}
}.let {
ULong.lift(it)
}
override fun sign(psbt: PartiallySignedBitcoinTransaction ) =
callWithPointer {
rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_70ef_OfflineWallet_sign(it, psbt.lower() , status)
}
}
companion object { companion object {
@ -1380,10 +1406,10 @@ class OfflineWallet(
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
public interface OnlineWalletInterface { public interface OnlineWalletInterface {
fun getNewAddress(): String fun getNewAddress(): String
fun getNetwork(): Network
fun sync(progressUpdate: BdkProgress, maxAddressParam: UInt? )
fun getBalance(): ULong fun getBalance(): ULong
fun sign(psbt: PartiallySignedBitcoinTransaction ) fun sign(psbt: PartiallySignedBitcoinTransaction )
fun getNetwork(): Network
fun sync(progressUpdate: BdkProgress, maxAddressParam: UInt? )
fun broadcast(psbt: PartiallySignedBitcoinTransaction ): String fun broadcast(psbt: PartiallySignedBitcoinTransaction ): String
} }
@ -1395,7 +1421,7 @@ class OnlineWallet(
constructor(descriptor: String, network: Network, databaseConfig: DatabaseConfig, blockchainConfig: BlockchainConfig ) : constructor(descriptor: String, network: Network, databaseConfig: DatabaseConfig, blockchainConfig: BlockchainConfig ) :
this( this(
rustCallWithError(BdkException) { status -> rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_new(descriptor.lower(), network.lower(), databaseConfig.lower(), blockchainConfig.lower() ,status) _UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_new(descriptor.lower(), network.lower(), databaseConfig.lower(), blockchainConfig.lower() ,status)
}) })
/** /**
@ -1408,7 +1434,7 @@ class OnlineWallet(
*/ */
override protected fun freeRustArcPtr() { override protected fun freeRustArcPtr() {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.ffi_bdk_b468_OnlineWallet_object_free(this.pointer, status) _UniFFILib.INSTANCE.ffi_bdk_70ef_OnlineWallet_object_free(this.pointer, status)
} }
} }
@ -1423,32 +1449,16 @@ class OnlineWallet(
override fun getNewAddress(): String = override fun getNewAddress(): String =
callWithPointer { callWithPointer {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_get_new_address(it, status) _UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_get_new_address(it, status)
} }
}.let { }.let {
String.lift(it) String.lift(it)
} }
override fun getNetwork(): Network =
callWithPointer {
rustCall() { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_get_network(it, status)
}
}.let {
Network.lift(it)
}
override fun sync(progressUpdate: BdkProgress, maxAddressParam: UInt? ) =
callWithPointer {
rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_sync(it, CallbackInterfaceBdkProgressInternals.lower(progressUpdate), lowerOptionalu32(maxAddressParam) , status)
}
}
override fun getBalance(): ULong = override fun getBalance(): ULong =
callWithPointer { callWithPointer {
rustCallWithError(BdkException) { status -> rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_get_balance(it, status) _UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_get_balance(it, status)
} }
}.let { }.let {
ULong.lift(it) ULong.lift(it)
@ -1457,14 +1467,30 @@ class OnlineWallet(
override fun sign(psbt: PartiallySignedBitcoinTransaction ) = override fun sign(psbt: PartiallySignedBitcoinTransaction ) =
callWithPointer { callWithPointer {
rustCallWithError(BdkException) { status -> rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_sign(it, psbt.lower() , status) _UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_sign(it, psbt.lower() , status)
}
}
override fun getNetwork(): Network =
callWithPointer {
rustCall() { status ->
_UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_get_network(it, status)
}
}.let {
Network.lift(it)
}
override fun sync(progressUpdate: BdkProgress, maxAddressParam: UInt? ) =
callWithPointer {
rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_sync(it, CallbackInterfaceBdkProgressInternals.lower(progressUpdate), lowerOptionalu32(maxAddressParam) , status)
} }
} }
override fun broadcast(psbt: PartiallySignedBitcoinTransaction ): String = override fun broadcast(psbt: PartiallySignedBitcoinTransaction ): String =
callWithPointer { callWithPointer {
rustCallWithError(BdkException) { status -> rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_OnlineWallet_broadcast(it, psbt.lower() , status) _UniFFILib.INSTANCE.bdk_70ef_OnlineWallet_broadcast(it, psbt.lower() , status)
} }
}.let { }.let {
String.lift(it) String.lift(it)
@ -1499,7 +1525,7 @@ class PartiallySignedBitcoinTransaction(
constructor(wallet: OnlineWallet, recipient: String, amount: ULong ) : constructor(wallet: OnlineWallet, recipient: String, amount: ULong ) :
this( this(
rustCallWithError(BdkException) { status -> rustCallWithError(BdkException) { status ->
_UniFFILib.INSTANCE.bdk_b468_PartiallySignedBitcoinTransaction_new(wallet.lower(), recipient.lower(), amount.lower() ,status) _UniFFILib.INSTANCE.bdk_70ef_PartiallySignedBitcoinTransaction_new(wallet.lower(), recipient.lower(), amount.lower() ,status)
}) })
/** /**
@ -1512,7 +1538,7 @@ class PartiallySignedBitcoinTransaction(
*/ */
override protected fun freeRustArcPtr() { override protected fun freeRustArcPtr() {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.ffi_bdk_b468_PartiallySignedBitcoinTransaction_object_free(this.pointer, status) _UniFFILib.INSTANCE.ffi_bdk_70ef_PartiallySignedBitcoinTransaction_object_free(this.pointer, status)
} }
} }
@ -1590,7 +1616,7 @@ internal object CallbackInterfaceBdkProgressInternals: CallbackInternals<BdkProg
) { ) {
override fun register(lib: _UniFFILib) { override fun register(lib: _UniFFILib) {
rustCall() { status -> rustCall() { status ->
lib.ffi_bdk_b468_BdkProgress_init_callback(this.foreignCallback, status) lib.ffi_bdk_70ef_BdkProgress_init_callback(this.foreignCallback, status)
} }
} }
} }

View File

@ -66,7 +66,13 @@ interface DatabaseConfig {
interface OfflineWallet { interface OfflineWallet {
[Throws=BdkError] [Throws=BdkError]
constructor(string descriptor, Network network, DatabaseConfig database_config); constructor(string descriptor, Network network, DatabaseConfig database_config);
// OfflineWalletOperations
string get_new_address(); string get_new_address();
[Throws=BdkError]
u64 get_balance();
[Throws=BdkError]
void sign([ByRef] PartiallySignedBitcoinTransaction psbt);
}; };
dictionary ElectrumConfig { dictionary ElectrumConfig {
@ -98,14 +104,18 @@ callback interface BdkProgress {
interface OnlineWallet { interface OnlineWallet {
[Throws=BdkError] [Throws=BdkError]
constructor(string descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config); constructor(string descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config);
// OfflineWalletOperations
string get_new_address(); string get_new_address();
Network get_network();
[Throws=BdkError]
void sync(BdkProgress progress_update, u32? max_address_param);
[Throws=BdkError] [Throws=BdkError]
u64 get_balance(); u64 get_balance();
[Throws=BdkError] [Throws=BdkError]
void sign([ByRef] PartiallySignedBitcoinTransaction psbt); void sign([ByRef] PartiallySignedBitcoinTransaction psbt);
// OnlineWalletInterface
Network get_network();
[Throws=BdkError]
void sync(BdkProgress progress_update, u32? max_address_param);
[Throws=BdkError] [Throws=BdkError]
string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt); string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
}; };

View File

@ -65,6 +65,22 @@ trait OfflineWalletOperations<B>: WalletHolder<B> {
.address .address
.to_string() .to_string()
} }
fn get_balance(&self) -> Result<u64, Error> {
self.get_wallet().get_balance()
}
fn sign<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result<(), Error> {
let mut psbt = psbt.internal.lock().unwrap();
let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?;
match finalized {
true => Ok(()),
false => Err(BdkError::Generic(format!(
"transaction signing not finalized {:?}",
psbt
))),
}
}
} }
impl OfflineWallet { impl OfflineWallet {
@ -188,22 +204,6 @@ impl OnlineWallet {
.sync(BdkProgressHolder { progress_update }, max_address_param) .sync(BdkProgressHolder { progress_update }, max_address_param)
} }
fn get_balance(&self) -> Result<u64, Error> {
self.wallet.lock().unwrap().get_balance()
}
fn sign<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result<(), Error> {
let mut psbt = psbt.internal.lock().unwrap();
let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?;
match finalized {
true => Ok(()),
false => Err(BdkError::Generic(format!(
"transaction signing not finalized {:?}",
psbt
))),
}
}
fn broadcast<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result<String, Error> { fn broadcast<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result<String, Error> {
let tx = psbt.internal.lock().unwrap().clone().extract_tx(); let tx = psbt.internal.lock().unwrap().clone().extract_tx();
let tx_id = self.get_wallet().broadcast(tx)?; let tx_id = self.get_wallet().broadcast(tx)?;