From b4119a3703b0e2ea887ff2879719b5b780bb1342 Mon Sep 17 00:00:00 2001 From: parazyd Date: Fri, 24 Feb 2023 19:32:40 +0100 Subject: [PATCH] drk: Basic inspection of PartialSwapData. Needs more love. --- bin/drk/src/cli_util.rs | 2 +- bin/drk/src/rpc_swap.rs | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bin/drk/src/cli_util.rs b/bin/drk/src/cli_util.rs index 3bdbc4a7e..0e0e90353 100644 --- a/bin/drk/src/cli_util.rs +++ b/bin/drk/src/cli_util.rs @@ -21,7 +21,7 @@ use async_std::{fs::File, io::WriteExt}; use darkfi::{util::parse::decode_base10, Result}; use darkfi_sdk::crypto::TokenId; -use crate::Drk; +use super::Drk; pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> { let v: Vec<&str> = s.split(':').collect(); diff --git a/bin/drk/src/rpc_swap.rs b/bin/drk/src/rpc_swap.rs index 0b91e7c38..7ee2ec2e7 100644 --- a/bin/drk/src/rpc_swap.rs +++ b/bin/drk/src/rpc_swap.rs @@ -15,6 +15,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +use std::fmt; use anyhow::{anyhow, Result}; use darkfi::{ @@ -42,7 +43,7 @@ use rand::rngs::OsRng; use super::Drk; -#[derive(SerialEncodable, SerialDecodable)] +#[derive(Debug, Clone, SerialEncodable, SerialDecodable)] /// Half of the swap data, includes the coin that is supposed to be sent, /// and the coin that is supposed to be received. pub struct PartialSwapData { @@ -54,6 +55,19 @@ pub struct PartialSwapData { token_blinds: Vec, } +impl fmt::Display for PartialSwapData { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = + format!( + "{:#?}\nValue pair: {}:{}\nToken pair: {}:{}\nValue blinds: {:?}\nToken blinds: {:?}\n", + self.params, self.value_pair.0, self.value_pair.1, self.token_pair.0, self.token_pair.1, + self.value_blinds, self.token_blinds, + ); + + write!(f, "{}", s) + } +} + impl Drk { /// Initialize the first half of an atomic swap pub async fn init_swap( @@ -173,7 +187,7 @@ impl Drk { // If there are any, we'll just spend the first one we see. let burn_coin = owncoins[0].0.clone(); - // Fetch our default address + // Fetch our default address // FIXME: Should actually be getting is_default let address = self.wallet_address(1).await?; // We'll also need our Merkle tree @@ -257,14 +271,14 @@ impl Drk { /// Inspect and verify a given swap (half or full) transaction pub async fn inspect_swap(&self, bytes: Vec) -> Result<()> { let mut full: Option = None; - let mut _half: Option = None; + let mut half: Option = None; if let Ok(v) = deserialize(&bytes) { full = Some(v) }; match deserialize(&bytes) { - Ok(v) => _half = Some(v), + Ok(v) => half = Some(v), Err(_) => { if full.is_none() { return Err(anyhow!("Failed to deserialize to Transaction or PartialSwapData")) @@ -401,8 +415,11 @@ impl Drk { return Ok(()) } - // TODO: Inspect PartialSwapData - todo!("Inspect PartialSwapData"); + // Inspect PartialSwapData + let partial = half.unwrap(); + eprintln!("{}", partial); + + Ok(()) } /// Sign a given transaction by retrieving the secret key from the encrypted