/// This function handles the server side of a `ConvertTo` command.
/// It returns the result of the proof verification.
This function handles the server side of a `ConvertTo` command.
It returns the result of the proof verification.
This function handles the server side of a ` ConvertTo ` command . It returns the result of the proof verification .
pub fn c_to(
    stream: TcpStream,
    blockchain: Arc<RwLock<BlockChain>>,
    server_data: Arc<ServerData>,
) -> std::io::Result<bool> {
    let mut stream_reader = BufReader::new(&stream);
    let mut stream_writer = BufWriter::new(&stream);
    let mut rng = OsRng;
    let mut pk_addr: AddressPublicKey = [0; 32];
    let mut pk_enc = [0; 32];
    let mut v_note = [0; 8];
    let mut message = vec![];
    stream_reader.read_exact(&mut pk_addr)?;
    stream_reader.read_exact(&mut pk_enc)?;
    let pk_enc = UnparsedPublicKey::new(&X25519, pk_enc);
    stream_reader.read_exact(&mut v_note)?;
    let note_new = Note::generate_random_from_pk_and_v(pk_addr, v_note, &mut rng);
    let time_start = Instant::now();
    let proof = convert_to::prover::create_proof(&note_new, &constants::C_TO_PARAMS);
    let duration = time_start.elapsed();
    println!("Proof generation took {} seconds.", duration.as_secs_f64());
    let cm_note_new = note_new.comm_note();
    stream_writer.write_all(&cm_note_new.to_bytes())?;
    proof.write(stream_writer.by_ref())?;
    stream_writer.flush()?;
    let mut result = [0; 1];
    stream_reader.read_exact(&mut result)?;
    match result[0] {
        1 => {
            let mut data = vec![];
            data.extend(&note_new.s_note.to_bytes());
            data.extend(&note_new.v_note);
            let mut context = vec![];
            proof.write(context.by_ref())?;
            let epk = encrypt(&mut data, &context, &pk_enc).unwrap();
            message.extend_from_slice(&cm_note_new.to_bytes());
            message.extend_from_slice(data.as_slice());
            message.extend_from_slice(epk.bytes().as_ref());
            proof.write(message.by_ref())?;
            let signature = sign(message.as_slice(), &server_data.signature_keypair)
                .expect("Signing should not fail.")
                .as_ref()
                .to_vec();
            stream_writer.write_all(&u64_array_to_u8_vector(&[signature.len() as u64]))?;
            stream_writer.write_all(&signature)?;
            stream_writer.flush()?;
            let mut blockchain = blockchain.write().unwrap();
            blockchain
                .new_block
                .as_mut()
                .expect("A new block should be ready.")
                .add_transaction(Box::new(CToTransaction {
                    cm_note_new,
                    t_new: TransactionTime::current(),
                    data,
                    epk,
                    proof,
                    signature,
                }));
            Ok(true)
        }
        _ => Ok(false),
    }
}
pub fn c_to ( stream : TcpStream , blockchain : Arc < RwLock < BlockChain > > , server_data : Arc < ServerData > , ) -> std :: io :: Result < bool > { let mut stream_reader = BufReader :: new ( & stream ) ; let mut stream_writer = BufWriter :: new ( & stream ) ; let mut rng = OsRng ; let mut pk_addr : AddressPublicKey = [ 0 ; 32 ] ; let mut pk_enc = [ 0 ; 32 ] ; let mut v_note = [ 0 ; 8 ] ; let mut message = vec ! [ ] ; stream_reader . read_exact ( & mut pk_addr ) ? ; stream_reader . read_exact ( & mut pk_enc ) ? ; let pk_enc = UnparsedPublicKey :: new ( & X25519 , pk_enc ) ; stream_reader . read_exact ( & mut v_note ) ? ; let note_new = Note :: generate_random_from_pk_and_v ( pk_addr , v_note , & mut rng ) ; let time_start = Instant :: now ( ) ; let proof = convert_to :: prover :: create_proof ( & note_new , & constants :: C_TO_PARAMS ) ; let duration = time_start . elapsed ( ) ; println ! ( " " duration as_secs_f64 ( ) ) ; let cm_note_new = note_new . comm_note ( ) ; stream_writer . write_all ( & cm_note_new . to_bytes ( ) ) ? ; proof . write ( stream_writer . by_ref ( ) ) ? ; stream_writer . flush ( ) ? ; let mut result = [ 0 ; 1 ] ; stream_reader . read_exact ( & mut result ) ? ; match result [ 0 ] { 1 => { let mut data = vec ! [ ] ; data . extend ( & note_new . s_note . to_bytes ( ) ) ; data . extend ( & note_new . v_note ) ; let mut context = vec ! [ ] ; proof . write ( context . by_ref ( ) ) ? ; let epk = encrypt ( & mut data , & context , & pk_enc ) . unwrap ( ) ; message . extend_from_slice ( & cm_note_new . to_bytes ( ) ) ; message . extend_from_slice ( data . as_slice ( ) ) ; message . extend_from_slice ( epk . bytes ( ) . as_ref ( ) ) ; proof . write ( message . by_ref ( ) ) ? ; let signature = sign ( message . as_slice ( ) , & server_data . signature_keypair ) . expect ( " " ) . as_ref ( ) . to_vec ( ) ; stream_writer . write_all ( & u64_array_to_u8_vector ( & [ signature . len ( ) as u64 ] ) ) ? ; stream_writer . write_all ( & signature ) ? ; stream_writer . flush ( ) ? ; let mut blockchain = blockchain . write ( ) . unwrap ( ) ; blockchain . new_block . as_mut ( ) . expect ( " " ) . add_transaction ( Box :: new ( CToTransaction { cm_note_new , t_new : TransactionTime :: current ( ) , data , epk , proof , signature , } ) ) ; Ok ( true ) } _ => Ok ( false ) , } }
This function handles the server side of a `ConvertTo` command.
This function handles the server side of a ` ConvertTo ` command .
stream
TcpStream
blockchain
Arc<RwLock<BlockChain>>
server_data
Arc<ServerData>