Skip to content

Commit

Permalink
Add LiteClient
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker-volodya committed Oct 3, 2024
1 parent b1242da commit 58f5443
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 47 deletions.
24 changes: 19 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion liteapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
adnl = { git = "https://github.com/tonstack/adnl-rs", branch = "server" }
adnl = "2.0.0"
tl-proto = { version = "0.4", features = ["bytes"] }
derivative = "2.2.0"
log = { version = "0.4.14", features = ["max_level_trace"] }
Expand Down
25 changes: 7 additions & 18 deletions liteapi/examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
use adnl::{AdnlPeer, AdnlRawPublicKey};
use tokio_tower::multiplex;
use ton_liteapi::layers::WrapMessagesLayer;
use ton_liteapi::peer::LitePeer;
use ton_liteapi::tl::request::{Request, WrappedRequest};
use tower::{Service, ServiceBuilder, ServiceExt};
use ton_liteapi::client::LiteClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let server_public = AdnlRawPublicKey::try_from(&*hex::decode("691a14528fb2911839649c489cb4cbec1f4aa126c244c0ea2ac294eb568a7037")?)?;
let ls_ip = "127.0.0.1";
let ls_port: u16 = 8080;
let adnl = AdnlPeer::connect(&server_public, (ls_ip, ls_port)).await?;
let lite = LitePeer::new(adnl);
let mut service = ServiceBuilder::new()
.layer(WrapMessagesLayer)
.service(multiplex::Client::<_, Box<dyn std::error::Error + Send + Sync + 'static>, _>::new(lite));
let message = WrappedRequest { wait_masterchain_seqno: None, request: Request::GetTime };
let result = service.ready().await?.call(message.clone()).await?;
let server_public = hex::decode("9f69357376ad875d1543faea6f0bb9fbcd283521b743b1c0d2d432587fe9dbae")?;
let server_address = ("127.0.0.1", 8080);
let mut liteclient = LiteClient::connect(server_address, server_public).await?;
let result = liteclient.get_time().await?;
println!("{:?}", result);
let result = service.ready().await?.call(message.clone()).await?;
let result = liteclient.get_time().await?;
println!("{:?}", result);
let result = service.ready().await?.call(message.clone()).await?;
let result = liteclient.get_time().await?;
println!("{:?}", result);
Ok(())
}
18 changes: 12 additions & 6 deletions liteapi/examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::env;
use std::error::Error;

use adnl::{AdnlPrivateKey, AdnlPublicKey};
use adnl::crypto::{KeyPair, SecretKey};
use adnl::AdnlAddress;
use ton_liteapi::layers::{UnwrapMessagesLayer, WrapErrorLayer};
use ton_liteapi::server::serve;
use ton_liteapi::tl::request::{Request, WrappedRequest};
use ton_liteapi::types::LiteError;
use ton_liteapi::tl::response::CurrentTime;
use ton_liteapi::tl::response::Response;
use tower::{make::Shared, ServiceBuilder};
use x25519_dalek::StaticSecret;

async fn handler(req: WrappedRequest) -> Result<Response, LiteError> {
println!("Received frame: {:?}", &req);
Expand All @@ -25,18 +25,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
// ADNL: get private key from environment variable KEY or use default insecure one
let private_key_hex = env::var("KEY").unwrap_or_else(|_| "f0971651aec4bb0d65ec3861c597687fda9c1e7d2ee8a93acb9a131aa9f3aee7".to_string());
let private_key_bytes: [u8; 32] = hex::decode(private_key_hex)?.try_into().unwrap();
let private_key = StaticSecret::from(private_key_bytes);
let keypair = KeyPair::from(&SecretKey::from_bytes(private_key_bytes));

// ADNL: print public key and adnl address associated with given private key
println!("Public key is: {}", hex::encode(private_key.public().edwards_repr()));
println!("Address is: {}", hex::encode(private_key.public().address().as_bytes()));
println!(
"Public key is: {}",
hex::encode(keypair.public_key.as_bytes())
);
println!(
"Address is: {}",
hex::encode(AdnlAddress::from(&keypair.public_key).as_bytes())
);

let service = ServiceBuilder::new()
.buffer(100)
.layer(UnwrapMessagesLayer)
.layer(WrapErrorLayer)
.service_fn(handler);

serve(&("127.0.0.1", 8080), private_key, Shared::new(service)).await?;
serve(&("127.0.0.1", 8080), keypair, Shared::new(service)).await?;
Ok(())
}
Loading

0 comments on commit 58f5443

Please sign in to comment.