diff --git a/bin/darkotc/src/main.rs b/bin/darkotc/src/main.rs index 8f8347c9d..d0d88faf5 100644 --- a/bin/darkotc/src/main.rs +++ b/bin/darkotc/src/main.rs @@ -1,50 +1,16 @@ use std::{ io::{stdin, Read}, process::exit, - /* - <<<<<<< HEAD - str::FromStr, - }; - - use clap::{Parser, Subcommand}; - use darkfi::crypto::proof::VerifyingKey; - use halo2_proofs::{arithmetic::Field, pasta::group::ff::PrimeField}; - use rand::rngs::OsRng; - use serde_json::json; - use termion::color; - ======= - */ }; use clap::{Parser, Subcommand}; use halo2_proofs::{arithmetic::Field, pasta::group::ff::PrimeField}; use rand::rngs::OsRng; - use url::Url; use darkfi::{ cli_desc, crypto::{ - /* - <<<<<<< HEAD - address::Address, - burn_proof::{create_burn_proof, verify_burn_proof}, - keypair::{PublicKey, SecretKey}, - merkle_node::MerkleNode, - mint_proof::{create_mint_proof, verify_mint_proof}, - proof::ProvingKey, - token_id, - types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValueBlind}, - util::{pedersen_commitment_base, pedersen_commitment_u64}, - BurnRevealedValues, MintRevealedValues, OwnCoin, Proof, - }, - rpc::{client::RpcClient, jsonrpc::JsonRequest}, - util::{ - cli::progress_bar, - encode_base10, - serial::{deserialize, serialize, SerialDecodable, SerialEncodable}, - ======= - */ burn_proof::{create_burn_proof, verify_burn_proof}, keypair::{PublicKey, SecretKey}, mint_proof::{create_mint_proof, verify_mint_proof}, @@ -76,7 +42,6 @@ use darkfi::{ mod cli_util; use cli_util::{parse_token_pair, parse_value_pair}; - mod rpc; use rpc::Rpc; @@ -101,145 +66,6 @@ enum Subcmd { /// Initialize an atomic swap Init { #[clap(short, long)] - /* - <<<<<<< HEAD - /// Pair of token IDs to swap: e.g. token_to_send:token_to_recv - token_pair: String, - - #[clap(short, long)] - /// Pair of values to swap: e.g. value_to_send:value_to_recv - value_pair: String, - }, - - /// Inspect swap data from stdin or file. - Inspect, - } - - struct Rpc { - pub rpc_client: RpcClient, - } - - impl Rpc { - async fn balance_of(&self, token_id: &str) -> Result { - let req = JsonRequest::new("wallet.get_balances", json!([])); - let rep = self.rpc_client.request(req).await?; - - if !rep.is_object() { - eprintln!("Error: Invalid balance data received from darkfid RPC endpoint."); - exit(1); - } - - for i in rep.as_object().unwrap().keys() { - if i == &token_id { - if let Some(balance) = rep[i].as_u64() { - return Ok(balance) - } - - eprintln!("Error: Invalid balance data received from darkfid RPC endpoint."); - exit(1); - } - } - - Ok(0) - } - - async fn wallet_address(&self) -> Result
{ - let req = JsonRequest::new("wallet.get_addrs", json!([0_i64])); - let rep = self.rpc_client.request(req).await?; - - if !rep.is_array() || !rep.as_array().unwrap()[0].is_string() { - eprintln!("Error: Invalid wallet address received from darkfid RPC endpoint."); - exit(1); - } - - Address::from_str(rep[0].as_str().unwrap()) - } - - async fn get_coins_valtok(&self, value: u64, token_id: &str) -> Result> { - let req = JsonRequest::new("wallet.get_coins_valtok", json!([value, token_id, true])); - let rep = self.rpc_client.request(req).await?; - - if !rep.is_array() { - eprintln!("Error: Invalid coin data received from darkfid RPC endpoint."); - exit(1); - } - - let mut ret = vec![]; - let rep = rep.as_array().unwrap(); - - for i in rep { - if !i.is_string() { - eprintln!("Error: Invalid base58 data for OwnCoin"); - exit(1); - } - - let data = match bs58::decode(i.as_str().unwrap()).into_vec() { - Ok(v) => v, - Err(e) => { - eprintln!("Error: Failed decoding base58 for OwnCoin: {}", e); - exit(1); - } - }; - - let oc = match deserialize(&data) { - Ok(v) => v, - Err(e) => { - eprintln!("Error: Failed deserializing OwnCoin: {}", e); - exit(1); - } - }; - - ret.push(oc); - } - - Ok(ret) - } - - async fn get_merkle_path(&self, leaf_pos: usize) -> Result> { - let req = JsonRequest::new("wallet.get_merkle_path", json!([leaf_pos as u64])); - let rep = self.rpc_client.request(req).await?; - - if !rep.is_array() { - eprintln!("Error: Invalid merkle path data received from darkfid RPC endpoint."); - exit(1); - } - - let mut ret = vec![]; - let rep = rep.as_array().unwrap(); - - for i in rep { - if !i.is_string() { - eprintln!("Error: Invalid base58 data for MerkleNode"); - exit(1); - } - - let n = i.as_str().unwrap(); - let n = match bs58::decode(n).into_vec() { - Ok(v) => v, - Err(e) => { - eprintln!("Error: Failed decoding base58 for MerkleNode: {}", e); - exit(1); - } - }; - - if n.len() != 32 { - eprintln!("Error: MerkleNode byte length is not 32"); - exit(1); - } - - let n = MerkleNode::from_bytes(&n.try_into().unwrap()); - if n.is_some().unwrap_u8() == 0 { - eprintln!("Error: Noncanonical bytes of MerkleNode"); - exit(1); - } - - ret.push(n.unwrap()); - } - - Ok(ret) - } - ======= - */ /// Pair of token IDs to swap: token_to_send:token_to_recv token_pair: String, @@ -302,18 +128,6 @@ async fn init_swap( endpoint: Url, token_pair: (String, String), value_pair: (u64, u64), - /* - <<<<<<< HEAD - ) -> Result<()> { - let rpc_client = RpcClient::new(endpoint).await?; - let rpc = Rpc { rpc_client }; - - // TODO: Think about decimals, there has to be some metadata to keep track. - let tp = (token_id::parse_b58(&token_pair.0)?, token_id::parse_b58(&token_pair.1)?); - let vp: (u64, u64) = - (value_pair.0.clone().try_into().unwrap(), value_pair.1.clone().try_into().unwrap()); - ======= - */ ) -> Result { let rpc_client = RpcClient::new(endpoint).await?; let rpc = Rpc { rpc_client }; @@ -333,20 +147,6 @@ async fn init_swap( exit(1); } - /* - <<<<<<< HEAD - // If not enough funds in a single coin, mint a single new coin - // with the funds. We do this to minimize the size of the swap - // transaction, i.e. 2 inputs and 2 outputs. - // TODO: Implement ^ - // TODO: Maybe this should be done by the user beforehand? - - // Find a coin to spend - let coins = rpc.get_coins_valtok(vp.0, &token_pair.0).await?; - if coins.is_empty() { - eprintln!("Error: Did not manage to find a coin with enough value to spend"); - ======= - */ // If there's not enough funds in a single coin, mint a single new coin // with the funds. We do this to minimize the size of the swap transaction. // i.e. 2 inputs and 2 outputs. @@ -361,16 +161,6 @@ async fn init_swap( } eprintln!("Initializing swap data for:"); - /* - <<<<<<< HEAD - eprintln!("Send: {} {} tokens", encode_base10(value_pair.0, 8), token_pair.0); - eprintln!("Recv: {} {} tokens", encode_base10(value_pair.1, 8), token_pair.1); - - // Fetch our default address - let our_address = rpc.wallet_address().await?; - let our_publickey = match PublicKey::try_from(our_address) { - ======= - */ eprintln!("Send: {} {} tokens", encode_base10(vp.0, 8), token_pair.0); eprintln!("Recv: {} {} tokens", encode_base10(vp.1, 8), token_pair.1); @@ -384,20 +174,6 @@ async fn init_swap( } }; - /* - <<<<<<< HEAD - // Build proving keys - let pb = progress_bar("Building proving key for the mint contract"); - let mint_pk = ProvingKey::build(8, &MintContract::default()); - pb.finish(); - - let pb = progress_bar("Building proving key for the burn contract"); - let burn_pk = ProvingKey::build(11, &BurnContract::default()); - pb.finish(); - - // The coin we want to receive. - ======= - */ // Build ZK proving keys let pb = progress_bar("Building proving key for the Mint contract"); let mint_pk = ProvingKey::build(11, &MintContract::default()); @@ -408,22 +184,16 @@ async fn init_swap( pb.finish(); // The coin we want to receive - let recv_value_blind = DrkValueBlind::random(&mut OsRng); let recv_token_blind = DrkValueBlind::random(&mut OsRng); let recv_coin_blind = DrkCoinBlind::random(&mut OsRng); let recv_serial = DrkSerial::random(&mut OsRng); - /* - <<<<<<< HEAD - let pb = progress_bar("Building mint proof for receiving coin"); - ======= - */ + // Spend hook and user data disabled let spend_hook = DrkSpendHook::from(0); let user_data = DrkUserData::from(0); let pb = progress_bar("Building Mint proof for the receiving coin"); - let (mint_proof, mint_revealed) = create_mint_proof( &mint_pk, vp.1, @@ -431,12 +201,6 @@ async fn init_swap( recv_value_blind, recv_token_blind, recv_serial, - /* - <<<<<<< HEAD - recv_coin_blind, - our_publickey, - ======= - */ spend_hook, user_data, recv_coin_blind, @@ -445,18 +209,9 @@ async fn init_swap( pb.finish(); // The coin we are spending. - /* - <<<<<<< HEAD - // We'll spend the first one we've found. - let coin = coins[0]; - - let pb = progress_bar("Building burn proof for spending coin"); - ======= - */ let coin = coins[0].clone(); let pb = progress_bar("Building Burn proof for the spending coin"); - let signature_secret = SecretKey::random(&mut OsRng); let merkle_path = match rpc.get_merkle_path(usize::from(coin.leaf_position)).await { Ok(v) => v, @@ -489,13 +244,6 @@ async fn init_swap( )?; pb.finish(); - /* - <<<<<<< HEAD - // Pack proofs together with pedersen commitment openings so - // counterparty can verify correctness. - let swap_data = SwapData { - ======= - */ // Create encrypted note let note = Note { serial: recv_serial, @@ -524,19 +272,6 @@ async fn init_swap( burn_revealed, burn_value_blind: coin.note.value_blind, burn_token_blind: coin.note.token_blind, - /* - <<<<<<< HEAD - }; - - // Print encoded data. - println!("{}", bs58::encode(serialize(&swap_data)).into_string()); - - Ok(()) - } - - fn inspect(data: &str) -> Result<()> { - ======= - */ encrypted_note, }; @@ -559,39 +294,14 @@ fn inspect_partial(data: &str) -> Result<()> { } }; - /* - <<<<<<< HEAD - let sd: SwapData = match deserialize(&bytes) { - Ok(v) => v, - Err(e) => { - eprintln!("Error: Failed to deserialize swap data into struct: {}", e); - ======= - */ let sd: PartialSwapData = match deserialize(&bytes) { Ok(v) => v, Err(e) => { eprintln!("Error deserializing partial swap data into struct: {}", e); - exit(1); } }; - /* - <<<<<<< HEAD - eprintln!("Successfully decoded data into SwapData struct"); - - // Build verifying keys - let pb = progress_bar("Building verifying key for the mint contract"); - let mint_vk = VerifyingKey::build(8, &MintContract::default()); - pb.finish(); - - let pb = progress_bar("Building verifying key for the burn contract"); - let burn_vk = VerifyingKey::build(11, &BurnContract::default()); - pb.finish(); - - let pb = progress_bar("Verifying burn proof"); - ======= - */ eprintln!("Successfully decoded partial swap data"); // Build ZK verifying keys @@ -604,29 +314,17 @@ fn inspect_partial(data: &str) -> Result<()> { pb.finish(); let pb = progress_bar("Verifying Burn proof"); - if verify_burn_proof(&burn_vk, &sd.burn_proof, &sd.burn_revealed).is_ok() { burn_valid = true; } pb.finish(); - /* - <<<<<<< HEAD - let pb = progress_bar("Verifying mint proof"); - ======= - */ let pb = progress_bar("Verifying Mint proof"); - if verify_mint_proof(&mint_vk, &sd.mint_proof, &sd.mint_revealed).is_ok() { mint_valid = true; } pb.finish(); - /* - <<<<<<< HEAD - eprintln!(" Verifying pedersen commitments"); - ======= - */ eprintln!(" Verifying Pedersen commitments"); if pedersen_commitment_u64(sd.burn_value, sd.burn_value_blind) == sd.burn_revealed.value_commit @@ -713,23 +411,6 @@ fn inspect_partial(data: &str) -> Result<()> { bs58::encode(sd.burn_token.to_repr()).into_string() ); - /* - <<<<<<< HEAD - if !valid { - eprintln!( - "\nThe ZK proofs and commitments inspected are {}NOT VALID{}", - color::Fg(color::Red), - color::Fg(color::Reset) - ); - exit(1); - } else { - eprintln!( - "\nThe ZK proofs and commitments inspected are {}VALID{}", - color::Fg(color::Green), - color::Fg(color::Reset) - ); - ======= - */ eprint!("\nThe ZK proofs and commitments inspected are "); if !valid { println!("{}", fg_red("NOT VALID")); @@ -741,24 +422,6 @@ fn inspect_partial(data: &str) -> Result<()> { Ok(()) } -/* -<<<<<<< HEAD -#[derive(SerialEncodable, SerialDecodable)] -struct SwapData { - mint_proof: Proof, - mint_revealed: MintRevealedValues, - mint_value: u64, - mint_token: DrkTokenId, - mint_value_blind: DrkValueBlind, - mint_token_blind: DrkValueBlind, - burn_proof: Proof, - burn_revealed: BurnRevealedValues, - burn_value: u64, - burn_token: DrkTokenId, - burn_value_blind: DrkValueBlind, - burn_token_blind: DrkValueBlind, -======= -*/ async fn join(endpoint: Url, d0: PartialSwapData, d1: PartialSwapData) -> Result { eprintln!("Joining data into a transaction"); @@ -931,17 +594,6 @@ async fn main() -> Result<()> { Subcmd::Init { token_pair, value_pair } => { let token_pair = parse_token_pair(&token_pair)?; let value_pair = parse_value_pair(&value_pair)?; - /* - <<<<<<< HEAD - - init_swap(args.endpoint, token_pair, value_pair).await - } - Subcmd::Inspect => { - let mut buf = String::new(); - stdin().read_to_string(&mut buf)?; - inspect(&buf.trim()) - ======= - */ let swap_data = init_swap(args.endpoint, token_pair, value_pair).await?; println!("{}", bs58::encode(serialize(&swap_data)).into_string());