Skip to content

Commit af28952

Browse files
authored
Swapd: Complete swap key manager refactor
<p dir="auto">This is the last big swap &lt;-&gt; wallet refactor - mostly because the wallet does not exist anymore after this.</p> <p dir="auto">Here is a very basic overview of changes:</p> <ul dir="auto"> <li>Move any mutating state from the wallet (so fields that we Optional before) into the swap state machine</li> <li>Pass in Event and Runtime into the wallet functions</li> <li>Use the farcaster core types instead of the wrapped p2p types for increased type safety</li> <li>Move functions that functionally belong into the wallet from the swapd Runtime into the wallet</li> <li>Thanks to these changes remove much of the complicated Error handling and checking from the wallet</li> <li>The remote proof is now just verified, but not persisted anymore. It's therefore no longer part of the checkpoint!</li> <li>Rename the <code class="notranslate">swapd/wallet.rs</code> to <code class="notranslate">swapd/swap_key_manager.rs</code></li> </ul>
2 parents 3cd62d0 + 1ffd2e9 commit af28952

6 files changed

Lines changed: 1509 additions & 1659 deletions

File tree

src/swapd/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
mod opts;
99
mod runtime;
1010
mod state_report;
11+
mod swap_key_manager;
1112
mod swap_state;
1213
mod syncer_client;
1314
mod temporal_safety;
14-
mod wallet;
1515

1616
#[cfg(feature = "shell")]
1717
pub use opts::Opts;

src/swapd/runtime.rs

Lines changed: 3 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use crate::swapd::Opts;
1414
use crate::syncerd::bitcoin_syncer::p2wpkh_signed_tx_fee;
1515
use crate::syncerd::types::{Event, TransactionConfirmations};
1616
use crate::{
17-
bus::ctl::{BitcoinFundingInfo, Checkpoint, CtlMsg, FundingInfo, Params},
17+
bus::ctl::{BitcoinFundingInfo, Checkpoint, CtlMsg, FundingInfo},
1818
bus::info::{InfoMsg, SwapInfo},
19-
bus::p2p::{Commit, PeerMsg, Reveal},
19+
bus::p2p::PeerMsg,
2020
bus::sync::SyncMsg,
2121
bus::{BusMsg, Outcome, ServiceBus},
2222
syncerd::{HeightChanged, Task, TransactionRetrieved, XmrAddressAddendum},
@@ -34,7 +34,7 @@ use bitcoin::Txid;
3434
use colored::ColoredString;
3535
use farcaster_core::{
3636
blockchain::Blockchain,
37-
crypto::{CommitmentEngine, SharedKeyId},
37+
crypto::SharedKeyId,
3838
monero::SHARED_VIEW_KEY_ID,
3939
role::{SwapRole, TradeRole},
4040
swap::btcxmr::{Deal, DealParameters, Parameters},
@@ -786,70 +786,6 @@ impl Runtime {
786786
Ok(amount)
787787
}
788788

789-
pub fn taker_commit(
790-
&mut self,
791-
endpoints: &mut Endpoints,
792-
params: Params,
793-
) -> Result<Commit, Error> {
794-
self.log_info(format!(
795-
"{} to Maker remote peer",
796-
"Proposing to take swap".bright_white_bold(),
797-
));
798-
799-
let msg = format!(
800-
"Proposing to take swap {} to Maker remote peer",
801-
self.swap_id()
802-
);
803-
// Ignoring possible reporting errors here and after: do not want to
804-
// halt the swap just because the client disconnected
805-
let _ = self.report_progress_message(endpoints, msg);
806-
807-
let engine = CommitmentEngine;
808-
let commitment = match params {
809-
Params::Bob(params) => {
810-
Commit::BobParameters(params.commit_bob(self.swap_id(), &engine))
811-
}
812-
Params::Alice(params) => {
813-
Commit::AliceParameters(params.commit_alice(self.swap_id(), &engine))
814-
}
815-
};
816-
817-
Ok(commitment)
818-
}
819-
820-
pub fn maker_commit(
821-
&mut self,
822-
endpoints: &mut Endpoints,
823-
swap_id: SwapId,
824-
params: Params,
825-
) -> Result<Commit, Error> {
826-
self.log_info(format!(
827-
"{} as Maker from Taker through peerd {}",
828-
"Accepting swap".bright_white_bold(),
829-
self.peer_service.bright_blue_italic()
830-
));
831-
832-
let msg = format!(
833-
"Accepting swap {} as Maker from Taker through peerd {}",
834-
swap_id, self.peer_service
835-
);
836-
// Ignoring possible reporting errors here and after: do not want to
837-
// halt the swap just because the enquirer (farcasterd) disconnected
838-
let _ = self.report_progress_message(endpoints, msg);
839-
840-
let engine = CommitmentEngine;
841-
let commitment = match params {
842-
Params::Bob(params) => {
843-
Commit::BobParameters(params.commit_bob(self.swap_id(), &engine))
844-
}
845-
Params::Alice(params) => {
846-
Commit::AliceParameters(params.commit_alice(self.swap_id(), &engine))
847-
}
848-
};
849-
850-
Ok(commitment)
851-
}
852-
853789
pub fn abort_swap(&mut self, endpoints: &mut Endpoints) -> Result<(), Error> {
854790
let swap_success_req = BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::FailureAbort));
855791
self.send_ctl(endpoints, ServiceId::Farcasterd, swap_success_req)?;
@@ -992,39 +928,3 @@ pub fn aggregate_xmr_spend_view(
992928
.elem();
993929
(alice_params.spend + bob_params.spend, alice_view + bob_view)
994930
}
995-
996-
/// Parameter processing irrespective of maker & taker role. Return [`Params`] if the commit/reveal
997-
/// matches.
998-
pub fn validate_reveal(reveal: &Reveal, remote_commit: Commit) -> Result<Params, Error> {
999-
let core_wallet = CommitmentEngine;
1000-
match reveal {
1001-
Reveal::Alice { parameters, .. } => match &remote_commit {
1002-
Commit::AliceParameters(commit) => {
1003-
commit.verify_with_reveal(&core_wallet, parameters.clone())?;
1004-
Ok(Params::Alice(parameters.clone().into_parameters()))
1005-
}
1006-
_ => {
1007-
let err_msg = format!(
1008-
"expected Some(Commit::Alice(commit)), found {}",
1009-
remote_commit
1010-
);
1011-
error!("{}", err_msg);
1012-
Err(Error::Farcaster(err_msg))
1013-
}
1014-
},
1015-
Reveal::Bob { parameters, .. } => match &remote_commit {
1016-
Commit::BobParameters(commit) => {
1017-
commit.verify_with_reveal(&core_wallet, parameters.clone())?;
1018-
Ok(Params::Bob(parameters.clone().into_parameters()))
1019-
}
1020-
_ => {
1021-
let err_msg = format!(
1022-
"expected Some(Commit::Bob(commit)), found {}",
1023-
remote_commit
1024-
);
1025-
error!("{}", err_msg);
1026-
Err(Error::Farcaster(err_msg))
1027-
}
1028-
},
1029-
}
1030-
}

0 commit comments

Comments
 (0)