Skip to content

Commit

Permalink
enhance spend transaction view
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Feb 10, 2022
1 parent eafb17b commit d411011
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 166 deletions.
37 changes: 34 additions & 3 deletions src/app/context.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;

use super::menu::Menu;
use crate::{app::config, conversion::Converter, daemon::Daemon, revault::Role};
use revaultd::config::Config as DaemonConfig;
use revaultd::revault_tx::miniscript::DescriptorPublicKey;

use revault_hwi::{app::revault::RevaultHWI, HWIError};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

pub type HardwareWallet =
Box<dyn Future<Output = Result<Box<dyn RevaultHWI + Send>, HWIError>> + Send + Sync>;
Expand Down Expand Up @@ -82,6 +85,34 @@ impl Context {
})
.collect()
}

pub fn user_signed(&self, psbt: &Psbt) -> bool {
let man_fp = &self
.config
.daemon
.manager_config
.as_ref()
.map(|key| key.xpub.fingerprint());
let stk_fp = &self
.config
.daemon
.stakeholder_config
.as_ref()
.map(|key| key.xpub.fingerprint());
if let Some(input) = psbt.inputs.first() {
input.partial_sigs.keys().any(|key| {
input
.bip32_derivation
.get(key)
.map(|(fingerprint, _)| {
Some(*fingerprint) == *man_fp || Some(*fingerprint) == *stk_fp
})
.unwrap_or(false)
})
} else {
false
}
}
}

pub struct ConfigContext {
Expand Down
45 changes: 11 additions & 34 deletions src/app/state/spend_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use bitcoin::util::{
bip32::{ExtendedPubKey, Fingerprint},
psbt::PartiallySignedTransaction as Psbt,
};
use bitcoin::util::{bip32::Fingerprint, psbt::PartiallySignedTransaction as Psbt};
use std::convert::From;

use bitcoin::OutPoint;
Expand Down Expand Up @@ -52,12 +49,7 @@ impl SpendTransactionState {
change_index: None,
action: SpendTransactionAction::new(
ctx.managers_threshold,
&ctx.config
.daemon
.manager_config
.as_ref()
.expect("User is a manager")
.xpub,
ctx.user_signed(&psbt),
&ctx.managers_xpubs(),
&psbt,
),
Expand Down Expand Up @@ -142,6 +134,7 @@ impl State for SpendTransactionState {
self.action.view(ctx, &self.psbt),
self.warning.as_ref(),
show_delete_button,
ctx.user_signed(&self.psbt),
)
}

Expand Down Expand Up @@ -185,7 +178,7 @@ pub enum SpendTransactionAction {
impl SpendTransactionAction {
fn new(
managers_threshold: usize,
user_manager_xpub: &ExtendedPubKey,
user_signed: bool,
managers_xpubs: &Vec<DescriptorPublicKey>,
psbt: &Psbt,
) -> Self {
Expand All @@ -198,13 +191,7 @@ impl SpendTransactionAction {
warning: None,
view: SpendTransactionBroadcastView::new(),
};
} else if input.partial_sigs.keys().any(|key| {
input
.bip32_derivation
.get(key)
.map(|(fingerprint, _)| user_manager_xpub.fingerprint() == *fingerprint)
.unwrap_or(false)
}) {
} else if user_signed {
return Self::SharePsbt {
psbt_input: form::Value::default(),
processing: false,
Expand Down Expand Up @@ -277,12 +264,7 @@ impl SpendTransactionAction {
SpendTxMessage::UnselectDelete => {
*self = Self::new(
ctx.managers_threshold,
&ctx.config
.daemon
.manager_config
.as_ref()
.expect("User is a manager")
.xpub,
ctx.user_signed(psbt),
&ctx.managers_xpubs(),
psbt,
);
Expand Down Expand Up @@ -320,12 +302,7 @@ impl SpendTransactionAction {
*psbt = signer.target.spend_tx.clone();
*self = Self::new(
ctx.managers_threshold,
&ctx.config
.daemon
.manager_config
.as_ref()
.expect("User is a manager")
.xpub,
true,
&ctx.managers_xpubs(),
psbt,
);
Expand Down Expand Up @@ -599,7 +576,7 @@ mod tests {
let mut psbt = Psbt::from_str("cHNidP8BALQCAAAAAc1946BSKWX5trghNlBq/IIYScLPYqr9Bqs2LfqOYuqcAAAAAAAIAAAAA+BAAAAAAAAAIgAgCOQxrx6W/t0dSZikMBNYG2Yyam/3LIoVrAy6e8ZDUAyA8PoCAAAAACIAIMuwqNTx88KHHtIR0EeURzEu9pUmbnUxd22KzYKi25A2CBH6AgAAAAAiACB18mkXdMgWd4MYRrAoIgDiiLLFlxC1j3Qxg9SSVQfbxQAAAAAAAQEruFn1BQAAAAAiACBI6M9l6zams92tyCK/4gbWyNfJMJzgoOv34L0X7GTovAEDBAEAAAABBWEhAgKTOrEDfq0KpKeFjG1J1nBeH7O8X2awCRive58A7NUmrFGHZHapFHKpXyKvmhuuuFL5qVJy+MIdmPJkiKxrdqkUtsmtuJyMk3Jsg+KhtdlHidd7lWGIrGyTUodnWLJoIgYCApM6sQN+rQqkp4WMbUnWcF4fs7xfZrAJGK97nwDs1SYIJR1gCQAAAAAAIgICUHL04HZXilyJ1B118e1Smr+S8c1qtja46Le7DzMCaUMI+93szQAAAAAAACICAlgt7b9E9GVk5djNsGdTbWDr40zR0YAc/1G7+desKJtDCNZ9f+kAAAAAIgIDRwTey1W1qoj/0e9dBjZiSMExThllURNv8U6ri7pKSQ4IcqlfIgAAAAAA").unwrap();
let user_manager_xpub = ExtendedPubKey::from_str("xpub6CZFHPW1GiB8YgV7zGpeQDB6mMHZYPQyUaHrM1nMvKMgLxwok4xCtnzjuxQ3p1LHJUkz5i1Y7bRy5fmGrdg8UBVb39XdXNtWWd2wTsNd7T9").unwrap();

let action = SpendTransactionAction::new(2, &user_manager_xpub, &Vec::new(), &psbt);
let action = SpendTransactionAction::new(2, false, &Vec::new(), &psbt);
assert!(matches!(action, SpendTransactionAction::Sign { .. }));

psbt.inputs[0].partial_sigs.insert(
Expand All @@ -610,13 +587,13 @@ mod tests {
"304402202f5eec50f34929e4bd8f6b7e81426795b0cd3608a4dad53ffab3e7af38ab627a02204ff61d9df2432ff3272c17d9baee1ec6b6dcb72b198be7f4ef843d5d47010a0401".as_bytes().to_vec(),
);

let action = SpendTransactionAction::new(2, &user_manager_xpub, &Vec::new(), &psbt);
let action = SpendTransactionAction::new(2, true, &Vec::new(), &psbt);
assert!(matches!(action, SpendTransactionAction::SharePsbt { .. }));

let action = SpendTransactionAction::new(1, &user_manager_xpub, &Vec::new(), &psbt);
let action = SpendTransactionAction::new(1, true, &Vec::new(), &psbt);
assert!(matches!(action, SpendTransactionAction::Broadcast { .. }));

let action = SpendTransactionAction::new(0, &user_manager_xpub, &Vec::new(), &psbt);
let action = SpendTransactionAction::new(0, true, &Vec::new(), &psbt);
assert!(matches!(action, SpendTransactionAction::Broadcast { .. }));
}
}
6 changes: 3 additions & 3 deletions src/app/view/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,9 @@ pub fn spend_tx_with_feerate_view<'a, T: 'a>(
),
)
.push(
Row::new()
.push(col_input.width(Length::FillPortion(1)))
.push(right_column.width(Length::FillPortion(1)))
Column::new()
.push(col_input.width(Length::Fill))
.push(right_column.width(Length::Fill))
.spacing(20),
)
.spacing(20),
Expand Down
Loading

0 comments on commit d411011

Please sign in to comment.