Bladeren bron

drk: Basic inspection of PartialSwapData.

Needs more love.
parazyd 3 jaren geleden
bovenliggende
commit
b4119a3703
2 gewijzigde bestanden met toevoegingen van 24 en 7 verwijderingen
  1. 1 1
      bin/drk/src/cli_util.rs
  2. 23 6
      bin/drk/src/rpc_swap.rs

+ 1 - 1
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::{util::parse::decode_base10, Result};
 use darkfi_sdk::crypto::TokenId;
 use darkfi_sdk::crypto::TokenId;
 
 
-use crate::Drk;
+use super::Drk;
 
 
 pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> {
 pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> {
     let v: Vec<&str> = s.split(':').collect();
     let v: Vec<&str> = s.split(':').collect();

+ 23 - 6
bin/drk/src/rpc_swap.rs

@@ -15,6 +15,7 @@
  * You should have received a copy of the GNU Affero General Public License
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
  */
+use std::fmt;
 
 
 use anyhow::{anyhow, Result};
 use anyhow::{anyhow, Result};
 use darkfi::{
 use darkfi::{
@@ -42,7 +43,7 @@ use rand::rngs::OsRng;
 
 
 use super::Drk;
 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,
 /// Half of the swap data, includes the coin that is supposed to be sent,
 /// and the coin that is supposed to be received.
 /// and the coin that is supposed to be received.
 pub struct PartialSwapData {
 pub struct PartialSwapData {
@@ -54,6 +55,19 @@ pub struct PartialSwapData {
     token_blinds: Vec<pallas::Scalar>,
     token_blinds: Vec<pallas::Scalar>,
 }
 }
 
 
+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 {
 impl Drk {
     /// Initialize the first half of an atomic swap
     /// Initialize the first half of an atomic swap
     pub async fn init_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.
         // If there are any, we'll just spend the first one we see.
         let burn_coin = owncoins[0].0.clone();
         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?;
         let address = self.wallet_address(1).await?;
 
 
         // We'll also need our Merkle tree
         // We'll also need our Merkle tree
@@ -257,14 +271,14 @@ impl Drk {
     /// Inspect and verify a given swap (half or full) transaction
     /// Inspect and verify a given swap (half or full) transaction
     pub async fn inspect_swap(&self, bytes: Vec<u8>) -> Result<()> {
     pub async fn inspect_swap(&self, bytes: Vec<u8>) -> Result<()> {
         let mut full: Option<Transaction> = None;
         let mut full: Option<Transaction> = None;
-        let mut _half: Option<PartialSwapData> = None;
+        let mut half: Option<PartialSwapData> = None;
 
 
         if let Ok(v) = deserialize(&bytes) {
         if let Ok(v) = deserialize(&bytes) {
             full = Some(v)
             full = Some(v)
         };
         };
 
 
         match deserialize(&bytes) {
         match deserialize(&bytes) {
-            Ok(v) => _half = Some(v),
+            Ok(v) => half = Some(v),
             Err(_) => {
             Err(_) => {
                 if full.is_none() {
                 if full.is_none() {
                     return Err(anyhow!("Failed to deserialize to Transaction or PartialSwapData"))
                     return Err(anyhow!("Failed to deserialize to Transaction or PartialSwapData"))
@@ -401,8 +415,11 @@ impl Drk {
             return Ok(())
             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
     /// Sign a given transaction by retrieving the secret key from the encrypted