Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gui: use descriptor method to detect psbt change outputs #938

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gui/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions gui/src/app/state/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::sync::Arc;

use iced::Command;

use liana::miniscript::bitcoin::bip32::{DerivationPath, Fingerprint};
use liana::miniscript::bitcoin::{
bip32::{DerivationPath, Fingerprint},
secp256k1,
};
use liana_ui::{component::form, widget::Element};

use crate::{
Expand Down Expand Up @@ -162,7 +165,14 @@ impl State for RecoveryPanel {
.any(|input| input.previous_output == coin.outpoint)
})
.collect();
Ok(SpendTx::new(None, psbt, coins, &desc, network))
Ok(SpendTx::new(
None,
psbt,
coins,
&desc,
&secp256k1::Secp256k1::verification_only(),
network,
))
},
Message::Recovery,
);
Expand Down
15 changes: 13 additions & 2 deletions gui/src/app/state/spend/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use liana_ui::{component::form, widget::Element};
use crate::{
app::{cache::Cache, error::Error, message::Message, state::psbt, view, wallet::Wallet},
daemon::{
model::{remaining_sequence, Coin, SpendTx},
model::{remaining_sequence, Coin, CreateSpendResult, SpendTx},
Daemon,
},
};
Expand Down Expand Up @@ -382,8 +382,16 @@ impl Step for DefineSpend {
async move {
daemon
.create_spend_tx(&inputs, &outputs, feerate_vb)
.map(|res| res.psbt)
.map_err(|e| e.into())
.and_then(|res| match res {
CreateSpendResult::Success { psbt, .. } => Ok(psbt),
CreateSpendResult::InsufficientFunds { missing } => {
Err(SpendCreationError::CoinSelection(
liana::spend::InsufficientFunds { missing },
)
.into())
}
})
},
Message::Psbt,
);
Expand Down Expand Up @@ -576,13 +584,15 @@ impl Recipient {
pub struct SaveSpend {
wallet: Arc<Wallet>,
spend: Option<psbt::PsbtState>,
curve: secp256k1::Secp256k1<secp256k1::VerifyOnly>,
}

impl SaveSpend {
pub fn new(wallet: Arc<Wallet>) -> Self {
Self {
wallet,
spend: None,
curve: secp256k1::Secp256k1::verification_only(),
}
}
}
Expand All @@ -595,6 +605,7 @@ impl Step for SaveSpend {
psbt,
draft.inputs.clone(),
&self.wallet.main_descriptor,
&self.curve,
draft.network,
);
tx.labels = draft.labels.clone();
Expand Down
20 changes: 17 additions & 3 deletions gui/src/app/state/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{
};

use iced::Command;
use liana::{miniscript::bitcoin::Txid, spend::MAX_FEERATE};
use liana::{
miniscript::bitcoin::Txid,
spend::{SpendCreationError, MAX_FEERATE},
};
use liana_ui::{
component::{form, modal::Modal},
widget::*,
Expand All @@ -20,7 +23,7 @@ use crate::app::{
};

use crate::daemon::{
model::{HistoryTransaction, Labelled},
model::{CreateSpendResult, HistoryTransaction, Labelled},
Daemon,
};

Expand Down Expand Up @@ -302,7 +305,18 @@ impl CreateRbfModal {
self.warning = None;

let psbt = match daemon.rbf_psbt(&self.txid, self.is_cancel, self.feerate_vb) {
Ok(res) => res.psbt,
Ok(res) => match res {
CreateSpendResult::Success { psbt, .. } => psbt,
CreateSpendResult::InsufficientFunds { missing } => {
self.warning = Some(
SpendCreationError::CoinSelection(
liana::spend::InsufficientFunds { missing },
)
.into(),
);
return Command::none();
}
},
Err(e) => {
self.warning = Some(e.into());
return Command::none();
Expand Down
3 changes: 2 additions & 1 deletion gui/src/daemon/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::iter::FromIterator;

use liana::commands::CreateRecoveryResult;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -154,7 +155,7 @@ impl<C: Client + Debug> Daemon for Lianad<C> {
feerate_vb: u64,
sequence: Option<u16>,
) -> Result<Psbt, DaemonError> {
let res: CreateSpendResult = self.call(
let res: CreateRecoveryResult = self.call(
"createrecovery",
Some(vec![json!(address), json!(feerate_vb), json!(sequence)]),
)?;
Expand Down
4 changes: 3 additions & 1 deletion gui/src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::iter::FromIterator;
use liana::{
commands::LabelItem,
config::Config,
miniscript::bitcoin::{address, psbt::Psbt, Address, OutPoint, Txid},
miniscript::bitcoin::{address, psbt::Psbt, secp256k1, Address, OutPoint, Txid},
StartupError,
};

Expand Down Expand Up @@ -102,6 +102,7 @@ pub trait Daemon: Debug {
let info = self.get_info()?;
let coins = self.list_coins()?.coins;
let mut spend_txs = Vec::new();
let curve = secp256k1::Secp256k1::verification_only();
for tx in self.list_spend_txs()?.spend_txs {
if let Some(txids) = txids {
if !txids.contains(&tx.psbt.unsigned_tx.txid()) {
Expand All @@ -125,6 +126,7 @@ pub trait Daemon: Debug {
tx.psbt,
coins,
&info.descriptors.main,
&curve,
info.network,
));
}
Expand Down
12 changes: 8 additions & 4 deletions gui/src/daemon/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use liana::{
miniscript::bitcoin::{
bip32::{DerivationPath, Fingerprint},
psbt::Psbt,
Address, Amount, Network, OutPoint, Transaction, Txid,
secp256k1, Address, Amount, Network, OutPoint, Transaction, Txid,
},
};

Expand Down Expand Up @@ -61,15 +61,19 @@ impl SpendTx {
psbt: Psbt,
coins: Vec<Coin>,
desc: &LianaDescriptor,
secp: &secp256k1::Secp256k1<impl secp256k1::Verification>,
network: Network,
) -> Self {
let max_vbytes = desc.unsigned_tx_max_vbytes(&psbt.unsigned_tx);
let mut change_indexes = Vec::new();
let change_indexes: Vec<usize> = desc
.change_indexes(&psbt, secp)
.into_iter()
.map(|c| c.index())
.collect();
let (change_amount, spend_amount) = psbt.unsigned_tx.output.iter().enumerate().fold(
(Amount::from_sat(0), Amount::from_sat(0)),
|(change, spend), (i, output)| {
if !psbt.outputs[i].bip32_derivation.is_empty() {
change_indexes.push(i);
if change_indexes.contains(&i) {
(change + output.value, spend)
} else {
(change, spend + output.value)
Expand Down
Loading