From 4ede2cd4c9667fc5de45f68c0edf8661889e3253 Mon Sep 17 00:00:00 2001 From: Vladimir Lebedev Date: Thu, 3 Oct 2024 16:39:10 +0700 Subject: [PATCH] Add GetLibraries and custom liteserver opts --- Cargo.lock | 23 +---------------------- cli/Cargo.toml | 2 +- cli/src/arg_parsers.rs | 8 ++++++-- cli/src/main.rs | 37 +++++++++++++++++++++++++++++-------- liteapi/src/tl/common.rs | 7 +++++++ network-config/Cargo.toml | 7 +------ network-config/src/lib.rs | 4 ---- 7 files changed, 45 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index be535b3..c1adc64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,20 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "adnl" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02b9094e4e606e5b8b3c3a6b37cee520e8d9bf327375549a14718a93af84f2d3" -dependencies = [ - "aes", - "ciborium-io", - "ctr", - "log", - "rand_core", - "sha2", -] - [[package]] name = "adnl" version = "2.0.0" @@ -232,12 +218,6 @@ dependencies = [ "windows-targets 0.52.4", ] -[[package]] -name = "ciborium-io" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" - [[package]] name = "cipher" version = "0.4.4" @@ -1519,7 +1499,7 @@ dependencies = [ name = "ton_liteapi" version = "0.1.0" dependencies = [ - "adnl 2.0.0", + "adnl", "base64 0.13.1", "derivative", "env_logger 0.11.3", @@ -1542,7 +1522,6 @@ dependencies = [ name = "ton_networkconfig" version = "0.1.0" dependencies = [ - "adnl 0.1.0", "serde", "serde_json", "serde_with", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 77e2e4b..8ad7fa0 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -14,6 +14,6 @@ hex = "0.4.3" ureq = "2.4.0" regex = "1" ton_liteapi = { path = "../liteapi" } -ton_networkconfig = { path = "../network-config", features = ["adnl"] } +ton_networkconfig = { path = "../network-config" } rand = "0.8.5" tokio = { version = "1.36", features = ["full"] } \ No newline at end of file diff --git a/cli/src/arg_parsers.rs b/cli/src/arg_parsers.rs index be58238..b9dacc5 100644 --- a/cli/src/arg_parsers.rs +++ b/cli/src/arg_parsers.rs @@ -46,11 +46,15 @@ fn parse_account_base64(s: &str) -> std::result::Result std::result::Result> { let (workchain, account) = s.split_once(":").ok_or_else(|| format!("can't parse {}: wrong address format, must be :", s))?; - let workchain = workchain.parse::().map_err(|e| format!("wrong workchain {}", workchain))?; - let id = account.parse::().map_err(|e| format!("wrong account id {}", account))?; + let workchain = workchain.parse::().map_err(|_e| format!("wrong workchain {}", workchain))?; + let id = account.parse::().map_err(|_e| format!("wrong account id {}", account))?; Ok(AccountId { workchain, id }) } pub fn parse_account_id(s: &str) -> std::result::Result { parse_account_base64(s).or_else(|e| parse_account_raw(s).map_err(|e2| format!("Can't parse account as base64 ({}) or as raw ({}))", e, e2))) } + +pub fn parse_key(s: &str) -> std::result::Result<[u8; 32], Box> { + Ok(base64::decode(s).or_else(|_e| hex::decode(s)).map_err(|_e| "can't parse key")?.as_slice().try_into()?) +} \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index f0789a2..eebb41d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -10,11 +10,12 @@ use ton_networkconfig::ConfigGlobal; use std::error::Error; use std::fs::{read_to_string, File}; use std::io::{stdin, Read}; +use std::net::SocketAddr; use std::path::PathBuf; use std::str::FromStr; use std::time::{Duration, UNIX_EPOCH}; -use crate::arg_parsers::{parse_account_id, parse_block_id_ext}; +use crate::arg_parsers::{parse_account_id, parse_block_id_ext, parse_key}; type Result = std::result::Result>; @@ -27,6 +28,12 @@ struct Args { /// Use testnet config, if not provided use mainnet config #[clap(short, long, parse(from_flag), group = "config-group")] testnet: bool, + /// Liteserver address (IP:PORT) + #[clap(long, group = "config-group")] + address: Option, + /// Liteserver public key (hex-encoded) + #[clap(long, value_parser = parse_key, requires = "address")] + public_key: Option<[u8; 32]>, #[clap(subcommand)] command: Commands, } @@ -215,21 +222,31 @@ enum Commands { start_after: Option, modified_after: Option, }, + GetLibraries { + library_list: Vec, + }, } #[tokio::main] async fn main() -> Result<()> { env_logger::init(); let args = Args::parse(); - let config_json = if let Some(config) = args.config { - read_to_string(config)? + + let client = if let (Some(address), Some(public_key)) = (&args.address, &args.public_key) { + LiteClient::connect(address, public_key).await? } else { - download_config(args.testnet).await? + let config_json = if let Some(config) = args.config { + read_to_string(config)? + } else { + download_config(args.testnet).await? + }; + let config: ConfigGlobal = ConfigGlobal::from_str(&config_json)?; + let ls = config.liteservers.choose(&mut rand::thread_rng()).unwrap(); + let public_key: [u8; 32] = ls.id.clone().into(); + LiteClient::connect(ls.socket_addr(), public_key).await? }; - let config: ConfigGlobal = ConfigGlobal::from_str(&config_json)?; - let ls = config.liteservers.choose(&mut rand::thread_rng()).unwrap(); - let public_key: [u8; 32] = ls.id.clone().into(); - let mut client = LiteClient::connect(ls.socket_addr(), public_key).await?; + + let mut client = client; if let Err(e) = execute_command(&mut client, &args.command).await { println!("[ERROR] {:?}", e); @@ -393,6 +410,10 @@ async fn execute_command(client: &mut LiteClient, command: &Commands) -> Result< ).await?; println!("{:#?}", result); } + Commands::GetLibraries { library_list } => { + let result = client.get_libraries(library_list.clone()).await?; + println!("{:#?}", result); + } }; Ok(()) } diff --git a/liteapi/src/tl/common.rs b/liteapi/src/tl/common.rs index fef82ce..b81474a 100644 --- a/liteapi/src/tl/common.rs +++ b/liteapi/src/tl/common.rs @@ -119,6 +119,7 @@ pub struct TransactionId3 { // #[tl(boxed, id = "liteServer.signature", scheme_inline = r##"liteServer.signature node_id_short:int256 signature:bytes = liteServer.Signature;"##)] pub struct Signature { pub node_id_short: Int256, + #[derivative(Debug(format_with = "fmt_bytes"))] pub signature: Vec, } @@ -150,8 +151,11 @@ pub enum BlockLink { to_key_block: bool, from: BlockIdExt, to: BlockIdExt, + #[derivative(Debug(format_with = "fmt_bytes"))] dest_proof: Vec, + #[derivative(Debug(format_with = "fmt_bytes"))] proof: Vec, + #[derivative(Debug(format_with = "fmt_bytes"))] state_proof: Vec, }, /// liteServer.blockLinkForward to_key_block:Bool from:tonNode.blockIdExt to:tonNode.blockIdExt dest_proof:bytes config_proof:bytes signatures:liteServer.SignatureSet = liteServer.BlockLink; @@ -160,7 +164,9 @@ pub enum BlockLink { to_key_block: bool, from: BlockIdExt, to: BlockIdExt, + #[derivative(Debug(format_with = "fmt_bytes"))] dest_proof: Vec, + #[derivative(Debug(format_with = "fmt_bytes"))] config_proof: Vec, signatures: SignatureSet, }, @@ -196,5 +202,6 @@ pub struct TransactionId { #[derivative(Debug, Clone, PartialEq)] pub struct LibraryEntry { pub hash: Int256, + #[derivative(Debug(format_with = "fmt_bytes"))] pub data: Vec, } diff --git a/network-config/Cargo.toml b/network-config/Cargo.toml index cd926a0..1c1607a 100644 --- a/network-config/Cargo.toml +++ b/network-config/Cargo.toml @@ -6,9 +6,4 @@ edition = "2021" [dependencies] serde = { version = "1.0.136", features = ["derive"] } serde_json = "1.0.79" -serde_with = { version = "1.12.0", features = ["base64"] } -adnl = { version = "0.1.0", optional = true } - -[features] -default = [] -adnl = ["dep:adnl"] \ No newline at end of file +serde_with = { version = "1.12.0", features = ["base64"] } \ No newline at end of file diff --git a/network-config/src/lib.rs b/network-config/src/lib.rs index 711aeac..b1ad0c1 100644 --- a/network-config/src/lib.rs +++ b/network-config/src/lib.rs @@ -2,10 +2,6 @@ use serde::{Deserialize, Serialize}; use std::net::{Ipv4Addr, SocketAddrV4}; use std::ops::{Deref, DerefMut}; use std::str::FromStr; -#[cfg(feature = "dalek")] -use x25519_dalek::PublicKey; -#[cfg(feature = "adnl")] -use adnl::AdnlPublicKey; #[serde_with::serde_as] #[derive(Serialize, Deserialize, Debug, Clone)]