Skip to content

Commit

Permalink
Merge #368: Changed Psbt to SpendTransaction.
Browse files Browse the repository at this point in the history
dfb1540 Changed `Psbt` to `SpendTransaction`. (Zshan0)

Pull request description:

  `ManagerImportSendTransactionState` currently stores the psbt directly in `psbt_imported`. This has been updated to `SpendTransaction`. Following this change is a chain of minor changes under `impl State` for the same.

  The implemented test only checks if the parsing for a `psbt` string happens through `SpendTransaction::from_psbt_str`. This PR resolves #79

ACKs for top commit:
  edouardparis:
    ACK dfb1540

Tree-SHA512: 16c2ade6c91a917811cfc2c228f94afdaf7331786c0b0fe3af14849ec9930a1214b290bb6c755984177e7a004153d3beac0e645d94d53adb4ff1806598c5146f
  • Loading branch information
edouardparis committed May 31, 2022
2 parents 0d3c1eb + dfb1540 commit 0859e52
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/app/state/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl From<ManagerSendState> for Box<dyn State> {

#[derive(Debug)]
pub struct ManagerImportSendTransactionState {
psbt_imported: Option<Psbt>,
psbt_imported: Option<SpendTransaction>,
psbt_input: form::Value<String>,
warning: Option<Error>,

Expand All @@ -455,10 +455,12 @@ impl ManagerImportSendTransactionState {
}
}

pub fn parse_pbst(&self) -> Option<Psbt> {
bitcoin::base64::decode(&self.psbt_input.value)
.ok()
.and_then(|bytes| bitcoin::consensus::encode::deserialize(&bytes).ok())
pub fn parse_pbst(&self) -> Option<SpendTransaction> {
SpendTransaction::from_psbt_str(&self.psbt_input.value).ok()
}

pub fn imported_state(&self) -> &Option<SpendTransaction> {
&self.psbt_imported
}
}

Expand All @@ -475,9 +477,9 @@ impl State for ManagerImportSendTransactionState {
}
Message::SpendTx(SpendTxMessage::Import) => {
if !self.psbt_input.value.is_empty() {
if let Some(psbt) = self.parse_pbst() {
if let Some(spend_tx) = self.parse_pbst() {
return Command::perform(
update_spend_tx(ctx.revaultd.clone(), psbt),
update_spend_tx(ctx.revaultd.clone(), spend_tx.into_psbt()),
|res| Message::SpendTx(SpendTxMessage::Updated(res)),
);
} else {
Expand All @@ -496,7 +498,7 @@ impl State for ManagerImportSendTransactionState {
self.view.view(
ctx,
&self.psbt_input,
self.psbt_imported.as_ref(),
self.psbt_imported.as_ref().map(|spend_tx| spend_tx.psbt()),
self.warning.as_ref(),
)
}
Expand Down
49 changes: 47 additions & 2 deletions tests/app_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use revault_gui::{
config::Config as GUIConfig,
context::{ConfigContext, Context},
menu::Menu,
message::{InputMessage, Message},
state::manager::ManagerCreateSendTransactionState,
message::{InputMessage, Message, SpendTxMessage},
state::manager::{ManagerCreateSendTransactionState, ManagerImportSendTransactionState},
},
conversion::Converter,
daemon::{
Expand All @@ -26,6 +26,7 @@ use revault_gui::{
},
revault::Role,
};
use revaultd::revault_tx::transactions::SpendTransaction;

#[tokio::test]
async fn test_manager_create_spend() {
Expand Down Expand Up @@ -145,3 +146,47 @@ async fn test_manager_create_spend() {
.await;
assert_eq!(sandbox.state().input_amount(), 119965368);
}

#[tokio::test]
async fn test_manager_import_spend() {
let psbt_string = "cHNidP8BAIkCAAAAAUeuD/NEqc88sk3DoBrKoVKjXbN2xW8Jr/4GO5q87JqJAQAAAAD9////AriGJgcAAAAAIgAgSOjPZes2prPdrcgiv+IG1sjXyTCc4KDr9+C9F+xk6LwwdQAAAAAAACIAIAjkMa8elv7dHUmYpDATWBtmMmpv9yyKFawMunvGQ1AMAAAAAAABASsADicHAAAAACIAIHXyaRd0yBZ3gxhGsCgiAOKIssWXELWPdDGD1JJVB9vFAQMEAQAAAAEFR1IhAlgt7b9E9GVk5djNsGdTbWDr40zR0YAc/1G7+desKJtDIQNHBN7LVbWqiP/R710GNmJIwTFOGWVRE2/xTquLukpJDlKuIgYCWC3tv0T0ZWTl2M2wZ1NtYOvjTNHRgBz/Ubv516wom0MI1n1/6QAAAAAiBgNHBN7LVbWqiP/R710GNmJIwTFOGWVRE2/xTquLukpJDghyqV8iAAAAAAAiAgICkzqxA36tCqSnhYxtSdZwXh+zvF9msAkYr3ufAOzVJgglHWAJAAAAACICAlgt7b9E9GVk5djNsGdTbWDr40zR0YAc/1G7+desKJtDCNZ9f+kAAAAAIgIDRwTey1W1qoj/0e9dBjZiSMExThllURNv8U6ri7pKSQ4IcqlfIgAAAAAAIgICUHL04HZXilyJ1B118e1Smr+S8c1qtja46Le7DzMCaUMI+93szQAAAAAA";
let spend = SpendTransaction::from_raw_psbt(&base64::decode(psbt_string).unwrap()).unwrap();

let daemon = Daemon::new(vec![(
Some(json!({"method": "updatespendtx", "params":
Some(vec![
psbt_string
])
})),
Ok(json!({})),
)]);

let sandbox: Sandbox<ManagerImportSendTransactionState> =
Sandbox::new(ManagerImportSendTransactionState::new());

let client = daemon.run();
let ctx = Context::new(
ConfigContext {
daemon: random_daemon_config(),
gui: GUIConfig::new(PathBuf::from_str("revault_gui.toml").unwrap()),
},
Arc::new(RevaultD::new(client)),
Converter::new(bitcoin::Network::Bitcoin),
Role::Stakeholder,
Menu::DelegateFunds,
Box::new(|| Box::pin(no_hardware_wallet())),
);
let sandbox = sandbox
.update(
&ctx,
Message::SpendTx(SpendTxMessage::PsbtEdited(psbt_string.to_string())),
)
.await;

let sandbox = sandbox
.update(&ctx, Message::SpendTx(SpendTxMessage::Import))
.await;

let state: &ManagerImportSendTransactionState = sandbox.state();
assert_eq!(state.imported_state().as_ref().unwrap(), &spend);
}

0 comments on commit 0859e52

Please sign in to comment.