Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Dec 11, 2023
1 parent 38b606f commit c3eb82f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 2 additions & 2 deletions gui/src/app/state/spend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl CreateSpendPanel {
current: 0,
steps: vec![
Box::new(
step::DefineSpend::new(descriptor, coins, timelock)
step::DefineSpend::new(network, descriptor, coins, timelock)
.with_coins_sorted(blockheight),
),
Box::new(step::SaveSpend::new(wallet)),
Expand All @@ -54,7 +54,7 @@ impl CreateSpendPanel {
current: 0,
steps: vec![
Box::new(
step::DefineSpend::new(descriptor, coins, timelock)
step::DefineSpend::new(network, descriptor, coins, timelock)
.with_preselected_coins(preselected_coins)
.with_coins_sorted(blockheight)
.self_send(),
Expand Down
46 changes: 40 additions & 6 deletions gui/src/app/state/spend/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use liana::{
miniscript::bitcoin::{
self, address, psbt::Psbt, secp256k1, Address, Amount, Denomination, Network, OutPoint,
},
spend::{create_spend, SpendCreationError, SpendOutputAddress, SpendTxFees, TxGetter},
spend::{
create_spend, CandidateCoin, SpendCreationError, SpendOutputAddress, SpendTxFees, TxGetter,
},
};

use liana_ui::{
Expand All @@ -20,7 +22,7 @@ use crate::{
app::{cache::Cache, error::Error, message::Message, state::psbt, view, wallet::Wallet},
daemon::{
model::{remaining_sequence, Coin, SpendTx},
Daemon, DaemonError,
Daemon,
},
};

Expand Down Expand Up @@ -74,6 +76,7 @@ pub struct DefineSpend {
is_valid: bool,
is_duplicate: bool,

network: Network,
descriptor: LianaDescriptor,
curve: secp256k1::Secp256k1<secp256k1::VerifyOnly>,
timelock: u16,
Expand All @@ -87,7 +90,12 @@ pub struct DefineSpend {
}

impl DefineSpend {
pub fn new(descriptor: LianaDescriptor, coins: &[Coin], timelock: u16) -> Self {
pub fn new(
network: Network,
descriptor: LianaDescriptor,
coins: &[Coin],
timelock: u16,
) -> Self {
let balance_available = coins
.iter()
.filter_map(|coin| {
Expand All @@ -101,7 +109,7 @@ impl DefineSpend {
let coins: Vec<(Coin, bool)> = coins
.iter()
.filter_map(|c| {
if c.spend_info.is_none() {
if c.spend_info.is_none() && !c.is_immature {
Some((c.clone(), false))
} else {
None
Expand All @@ -111,6 +119,7 @@ impl DefineSpend {

Self {
balance_available,
network,
descriptor,
curve: secp256k1::Secp256k1::verification_only(),
timelock,
Expand Down Expand Up @@ -201,6 +210,28 @@ impl DefineSpend {
})
.collect();

let coins: Vec<CandidateCoin> = if self.is_user_coin_selection {
self.coins
.iter()
.map(|(c, selected)| CandidateCoin {
amount: c.amount,
outpoint: c.outpoint,
deriv_index: c.derivation_index,
is_change: c.is_change,
sequence: None,
must_select: *selected,
})
.collect()
} else {
self.coins.iter().map(|(c, _)| CandidateCoin {}).collect()
};

let dummy_address = self
.descriptor
.change_descriptor()
.derive(0.into(), &self.curve)
.address(self.network);

let feerate_vb = self.feerate.value.parse::<u64>().expect("Checked before");
// Create a spend with empty inputs in order to use auto-selection.
match create_spend(
Expand All @@ -212,7 +243,7 @@ impl DefineSpend {
SpendTxFees::Regular(feerate_vb),
// we enter a dummy address to calculate
SpendOutputAddress {
addr: Address::from_str("").unwrap().assume_checked(),
addr: dummy_address,
info: None,
},
) {
Expand Down Expand Up @@ -252,7 +283,10 @@ impl DefineSpend {
pub struct DaemonTxGetter<'a>(&'a Arc<dyn Daemon + Sync + Send>);
impl<'a> TxGetter for DaemonTxGetter<'a> {
fn get_tx(&mut self, txid: &bitcoin::Txid) -> Option<bitcoin::Transaction> {
None
self.0
.list_txs(&[*txid])
.ok()
.and_then(|mut txs| txs.transactions.pop().map(|tx| tx.tx))
}
}

Expand Down
1 change: 1 addition & 0 deletions gui/src/app/view/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl From<&Error> for WarningMessage {
Error::Unexpected(_) => WarningMessage("Unknown error".to_string()),
Error::HardwareWallet(_) => WarningMessage("Hardware wallet error".to_string()),
Error::Desc(e) => WarningMessage(format!("Descriptor analysis error: '{}'.", e)),
Error::Spend(e) => WarningMessage(format!("Spend creation error: '{}'.", e)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gui/src/daemon/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};

use super::{model::*, Daemon, DaemonError};
use liana::{
commands::{CommandError, LabelItem},
commands::LabelItem,
config::Config,
miniscript::bitcoin::{address, psbt::Psbt, Address, OutPoint, Txid},
DaemonControl, DaemonHandle,
Expand Down

0 comments on commit c3eb82f

Please sign in to comment.