Skip to content

Commit

Permalink
try to fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
navichok26 committed Feb 22, 2024
1 parent 96e4391 commit 9af0e62
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
7 changes: 4 additions & 3 deletions examples/echo_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use adnl::{AdnlPeer, AdnlRawPublicKey};
use std::{env, error::Error};
use x25519_dalek::PublicKey;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -14,14 +15,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
let remote_public = AdnlRawPublicKey::try_from(&*hex::decode(public_key_hex)?)?;

// act as a client: connect to ADNL server and perform handshake
let mut client = AdnlPeer::connect(&remote_public, addr).await?;
let mut client = AdnlPeer::connect(&remote_public, addr).await.unwrap();

// send over ADNL
client.send(&mut "hello".as_bytes().to_vec()).await?;
client.send(&mut "hello".as_bytes().to_vec()).await.unwrap();

// receive result into vector
let mut result = Vec::<u8>::new();
client.receive(&mut result).await?;
client.receive(&mut result).await.unwrap();

println!("received: {}", String::from_utf8(result).unwrap());
Ok(())
Expand Down
4 changes: 3 additions & 1 deletion examples/echo_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
let listener = TcpListener::bind(&addr).await?;
println!("Listening on: {}", addr);

//приватный 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
// публичный 8f40c5adb68f25624ae5b214ea767a6ec94d829d3d7b5e1ad1ba6f3e2138285f
// ADNL: print public key and adnl address associated with given private key
println!(
"Public key is: {}",
hex::encode(private_key.public().as_bytes())
hex::encode(private_key.public().key_to_bytes())
);
println!(
"Address is: {}",
Expand Down
6 changes: 3 additions & 3 deletions src/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ pub trait AdnlPublicKey {
fn address(&self) -> AdnlAddress {
let mut hasher = Sha256::new();
hasher.update([0xc6, 0xb4, 0x13, 0x48]); // type id - always ed25519
hasher.update(self.to_bytes());
hasher.update(self.key_to_bytes());
AdnlAddress(hasher.finalize().into())
}

fn to_bytes(&self) -> [u8; 32];
fn key_to_bytes(&self) -> [u8; 32];
}

/// Public key can be provided using raw slice
#[derive(Clone)]
pub struct AdnlRawPublicKey([u8; 32]);

impl AdnlPublicKey for AdnlRawPublicKey {
fn to_bytes(&self) -> [u8; 32] {
fn key_to_bytes(&self) -> [u8; 32] {
self.0
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/integrations/dalek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use curve25519_dalek::montgomery::MontgomeryPoint;
use x25519_dalek::{PublicKey, StaticSecret};

impl AdnlPublicKey for PublicKey {
fn to_bytes(&self) -> [u8; 32] {
fn key_to_bytes(&self) -> [u8; 32] {
MontgomeryPoint(self.to_bytes())
.to_edwards(0)
.unwrap()
Expand All @@ -18,7 +18,7 @@ impl AdnlPublicKey for PublicKey {

fn edwards_to_montgomery<P: AdnlPublicKey>(public_key: &P) -> PublicKey {
PublicKey::from(
CompressedEdwardsY::from_slice(&public_key.to_bytes())
CompressedEdwardsY::from_slice(&public_key.key_to_bytes())
.unwrap()
.decompress()
.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion src/primitives/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
};
use aes::cipher::KeyIvInit;
use ctr::cipher::StreamCipher;
use curve25519_dalek::edwards::CompressedEdwardsY;

Check failure on line 8 in src/primitives/handshake.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `curve25519_dalek::edwards::CompressedEdwardsY`

Check warning on line 8 in src/primitives/handshake.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `curve25519_dalek::edwards::CompressedEdwardsY`

Check warning on line 8 in src/primitives/handshake.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `curve25519_dalek::edwards::CompressedEdwardsY`
use sha2::{Digest, Sha256};
use tokio::io::{AsyncReadExt, AsyncWriteExt};

Expand Down Expand Up @@ -57,7 +58,7 @@ impl<P: AdnlPublicKey> AdnlHandshake<P> {

let mut packet = [0u8; 256];
packet[..32].copy_from_slice(self.receiver.as_bytes());
packet[32..64].copy_from_slice(&self.sender.to_bytes());
packet[32..64].copy_from_slice(&self.sender.key_to_bytes());
packet[64..96].copy_from_slice(&hash);
packet[96..256].copy_from_slice(&raw_params);
packet
Expand Down
6 changes: 4 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ extern crate alloc;

use super::*;
use alloc::vec::Vec;
use curve25519_dalek::edwards::CompressedEdwardsY;

Check warning on line 5 in src/tests.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `curve25519_dalek::edwards::CompressedEdwardsY`
use x25519_dalek::StaticSecret;

Check warning on line 6 in src/tests.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `x25519_dalek::StaticSecret`

#[test]
fn test_handshake_1() {
Expand Down Expand Up @@ -103,8 +105,8 @@ fn test_handshake(
"receiver mismatch"
);
assert_eq!(
handshake2.sender().to_bytes(),
local_public.to_bytes(),
handshake2.sender().key_to_bytes(),
local_public.key_to_bytes(),
"sender mismatch"
);
assert_eq!(
Expand Down

0 comments on commit 9af0e62

Please sign in to comment.