Skip to content

Commit

Permalink
Add Wallet::get_psbt_input function
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Jun 7, 2023
1 parent 5ffb027 commit cdf8718
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ interface Wallet {
[Throws=BdkError]
sequence<LocalUtxo> list_unspent();

[Throws=BdkError]
Input get_psbt_input(LocalUtxo utxo, PsbtSighashType? sighash_type, boolean only_witness_utxo);

[Throws=BdkError]
sequence<TransactionDetails> list_transactions(boolean include_raw);

Expand Down
29 changes: 29 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ impl From<&OutPoint> for BdkOutPoint {
}
}

impl From<OutPoint> for BdkOutPoint {
fn from(outpoint: OutPoint) -> Self {
BdkOutPoint {
txid: Txid::from_str(&outpoint.txid).unwrap(),
vout: outpoint.vout,
}
}
}

pub struct Balance {
// All coinbase outputs not yet matured
pub immature: u64,
Expand Down Expand Up @@ -212,6 +221,15 @@ impl From<&BdkTxOut> for TxOut {
}
}

impl From<TxOut> for BdkTxOut {
fn from(tx_out: TxOut) -> Self {
BdkTxOut {
value: tx_out.value,
script_pubkey: tx_out.script_pubkey.script.clone(),
}
}
}

pub struct LocalUtxo {
outpoint: OutPoint,
txout: TxOut,
Expand All @@ -238,6 +256,17 @@ impl From<BdkLocalUtxo> for LocalUtxo {
}
}

impl From<LocalUtxo> for BdkLocalUtxo {
fn from(local_utxo: LocalUtxo) -> Self {
BdkLocalUtxo {
outpoint: local_utxo.outpoint.into(),
txout: local_utxo.txout.into(),
keychain: local_utxo.keychain,
is_spent: local_utxo.is_spent,
}
}
}

/// Trait that logs at level INFO every update received (if any).
pub trait Progress: Send + Sync + 'static {
/// Send a new progress update. The progress value should be in the range 0.0 - 100.0, and the message value is an
Expand Down
14 changes: 13 additions & 1 deletion bdk-ffi/src/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ impl PartiallySignedTransaction {
/// transaction.
#[derive(Debug)]
pub(crate) struct Input {
inner: BdkInput,
_inner: BdkInput,
}

impl From<BdkInput> for Input {
fn from(input: BdkInput) -> Self {
Input { _inner: input }
}
}

/// A Signature hash type for the corresponding input. As of taproot upgrade, the signature hash
Expand All @@ -109,6 +115,12 @@ impl PsbtSighashType {
}
}

impl From<&PsbtSighashType> for BdkPsbtSighashType {
fn from(psbt_hash_ty: &PsbtSighashType) -> Self {
psbt_hash_ty.inner
}
}

// The goal of these tests to to ensure `bdk-ffi` intermediate code correctly calls `bdk` APIs.
// These tests should not be used to verify `bdk` behavior that is already tested in the `bdk`
// crate.
Expand Down
18 changes: 17 additions & 1 deletion bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
use crate::blockchain::Blockchain;
use crate::database::DatabaseConfig;
use crate::descriptor::Descriptor;
use crate::psbt::PartiallySignedTransaction;
use crate::psbt::{Input, PartiallySignedTransaction, PsbtSighashType};
use crate::{
AddressIndex, AddressInfo, Balance, BdkError, LocalUtxo, OutPoint, Progress, ProgressHolder,
RbfValue, Script, ScriptAmount, TransactionDetails, TxBuilderResult,
Expand Down Expand Up @@ -156,6 +156,22 @@ impl Wallet {
let unspents: Vec<BdkLocalUtxo> = self.get_wallet().list_unspent()?;
Ok(unspents.into_iter().map(LocalUtxo::from).collect())
}

/// Get the corresponding PSBT Input for a LocalUtxo.
pub(crate) fn get_psbt_input(
&self,
utxo: LocalUtxo,
sighash_type: Option<Arc<PsbtSighashType>>,
only_witness_utxo: bool,
) -> Result<Arc<Input>, BdkError> {
self.get_wallet()
.get_psbt_input(
utxo.into(),
sighash_type.map(|s| s.deref().into()),
only_witness_utxo,
)
.map(|i| Arc::new(i.into()))
}
}

/// Options for a software signer
Expand Down

0 comments on commit cdf8718

Please sign in to comment.