Kaynağa Gözat

create a function for the process of building slab from transaction

ghassmo 5 yıl önce
ebeveyn
işleme
39fabd79c9
2 değiştirilmiş dosya ile 61 ekleme ve 37 silme
  1. 12 35
      src/bin/darkfid.rs
  2. 49 2
      src/util.rs

+ 12 - 35
src/bin/darkfid.rs

@@ -14,7 +14,7 @@ use drk::rpc::jsonserver;
 use drk::serial::{deserialize, Decodable, Encodable};
 use drk::serial::{deserialize, Decodable, Encodable};
 use drk::service::{CashierClient, GatewayClient, GatewaySlabsSubscriber};
 use drk::service::{CashierClient, GatewayClient, GatewaySlabsSubscriber};
 use drk::state::{state_transition, ProgramState, StateUpdate};
 use drk::state::{state_transition, ProgramState, StateUpdate};
-use drk::util::join_config_path;
+use drk::util::{join_config_path, prepare_transaction};
 use drk::wallet::{WalletDb, WalletPtr};
 use drk::wallet::{WalletDb, WalletPtr};
 use drk::{tx, Result};
 use drk::{tx, Result};
 
 
@@ -176,43 +176,20 @@ pub async fn futures_broker(
             transfer_params = publish_tx_recv.recv().fuse() => {
             transfer_params = publish_tx_recv.recv().fuse() => {
                 let transfer_params = transfer_params?;
                 let transfer_params = transfer_params?;
 
 
-                let merkle_path = {
-                    let (_coin, _, _, witness) = &mut state.wallet.get_own_coins()?[0];
-
-                    let merkle_path = witness.path().unwrap();
-
-                    merkle_path
-                };
-
                 let address = bs58::decode(transfer_params.pub_key).into_vec()?;
                 let address = bs58::decode(transfer_params.pub_key).into_vec()?;
                 let address: jubjub::SubgroupPoint = deserialize(&address)?;
                 let address: jubjub::SubgroupPoint = deserialize(&address)?;
 
 
-                // Make a spend tx
-
-                // Construct a new tx spending the coin
-                let builder = tx::TransactionBuilder {
-                    clear_inputs: vec![],
-                    inputs: vec![tx::TransactionBuilderInputInfo {
-                        merkle_path,
-                        secret: secret.clone(),
-                        note: state.wallet.get_own_coins()?[0].1.clone(),
-                    }],
-                    // We can add more outputs to this list.
-                    // The only constraint is that sum(value in) == sum(value out)
-                    outputs: vec![tx::TransactionBuilderOutputInfo {
-                        value: transfer_params.amount as u64,
-                        asset_id: 1,
-                        public: address,
-                    }],
-                };
-                // Build the tx
-                let mut tx_data = vec![];
-                {
-                    let tx = builder.build(&mint_params, &spend_params);
-                    tx.encode(&mut tx_data).expect("encode tx");
-                }
-
-                let slab = Slab::new(tx_data);
+                let own_coins = state.wallet.get_own_coins()?;
+
+                let slab = prepare_transaction(
+                    state,
+                    secret.clone(),
+                    mint_params.clone(),
+                    spend_params.clone(),
+                    address,
+                    transfer_params.amount,
+                    own_coins
+                )?;
 
 
                 client.put_slab(slab).await.expect("put slab");
                 client.put_slab(slab).await.expect("put slab");
             }
             }

+ 49 - 2
src/util.rs

@@ -1,6 +1,13 @@
-use std::path::{Path, PathBuf};
-
+use crate::blockchain::Slab;
+use crate::state::ProgramState;
 use crate::Result;
 use crate::Result;
+use crate::tx;
+use crate::serial::Encodable;
+use crate::crypto::OwnCoins;
+
+use bls12_381::Bls12;
+
+use std::path::{Path, PathBuf};
 
 
 pub fn join_config_path(file: &PathBuf) -> Result<PathBuf> {
 pub fn join_config_path(file: &PathBuf) -> Result<PathBuf> {
     let mut path = PathBuf::new();
     let mut path = PathBuf::new();
@@ -17,3 +24,43 @@ pub fn join_config_path(file: &PathBuf) -> Result<PathBuf> {
 
 
     Ok(path)
     Ok(path)
 }
 }
+
+pub fn prepare_transaction(
+    state: &dyn ProgramState,
+    secret: jubjub::Fr,
+    mint_params: bellman::groth16::Parameters<Bls12>,
+    spend_params: bellman::groth16::Parameters<Bls12>,
+    address: jubjub::SubgroupPoint,
+    amount: f64,
+    own_coins: OwnCoins,
+) -> Result<Slab> {
+    let witness = &own_coins[0].3;
+    let merkle_path = witness.path().unwrap();
+
+    // Construct a new tx spending the coin
+    let builder = tx::TransactionBuilder {
+        clear_inputs: vec![],
+        inputs: vec![tx::TransactionBuilderInputInfo {
+            merkle_path,
+            secret: secret.clone(),
+            note: own_coins[0].1.clone(),
+        }],
+        // We can add more outputs to this list.
+        // The only constraint is that sum(value in) == sum(value out)
+        outputs: vec![tx::TransactionBuilderOutputInfo {
+            value: amount as u64,
+            asset_id: 1,
+            public: address,
+        }],
+    };
+    // Build the tx
+    let mut tx_data = vec![];
+    {
+        let tx = builder.build(&mint_params, &spend_params);
+        tx.encode(&mut tx_data).expect("encode tx");
+    }
+
+    // build slab from the transaction
+    let slab = Slab::new(tx_data);
+    return Ok(slab);
+}