Skip to content

Commit

Permalink
Merge pull request #21 from tonlabs/nodeid-calc
Browse files Browse the repository at this point in the history
Node ID calculation
  • Loading branch information
AlexeyVavilin committed Jun 3, 2020
2 parents 5f473de + ee2425c commit b0fc1a7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"
license = "Apache-2.0"
keywords = ["TON", "SDK", "smart contract", "tonlabs"]
edition = "2018"
version = "0.1.6"
version = "0.1.7"

[dependencies]
base64 = "0.10.1"
Expand All @@ -23,6 +23,7 @@ qr2term = "0.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_derive = "1.0.91"
sha2 = "0.8"

ton_abi = { git = "https://github.com/tonlabs/ton-labs-abi.git" }
ton-client-rs = { git = 'https://github.com/tonlabs/ton-client-rs.git', default-features = false, branch = "0.23.0-rc" }
Expand Down
18 changes: 17 additions & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* See the License for the specific TON DEV software governing permissions and
* limitations under the License.
*/

use sha2::{Sha256, Digest};

pub fn convert_token(amount: &str) -> Result<String, String> {
let parts: Vec<&str> = amount.split(".").collect();
if parts.len() >= 1 && parts.len() <= 2 {
Expand All @@ -30,4 +33,17 @@ pub fn convert_token(amount: &str) -> Result<String, String> {
return Ok(result);
}
Err("Invalid amout value".to_string())
}
}

pub fn nodeid_from_pubkey(key: &[u8]) -> Result<String, String> {
if key.len() != 32 {
return Err("Public key must be 32 byte long".to_owned());
}
let mut hasher = Sha256::new();
// node id magic
hasher.input(&[0xc6, 0xb4, 0x13, 0x48]);
//key
hasher.input(key);

Ok(hex::encode(&hasher.result()))
}
28 changes: 27 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ fn main_internal() -> Result <(), String> {
(about: "Reads global configuration parameter with defined index.")
(@arg INDEX: +required +takes_value "Parameter index.")
)
(@subcommand nodeid =>
(about: "Calculates node ID from validator public key")
(@arg KEY: --pubkey +takes_value "Validator public key.")
(@arg KEY_PAIR: --keypair +takes_value "Validator key pair as 12 words mnemonic or file path.")
)
(@setting SubcommandRequired)
).get_matches();

Expand Down Expand Up @@ -320,6 +325,9 @@ fn main_internal() -> Result <(), String> {
if let Some(m) = matches.subcommand_matches("getconfig") {
return getconfig_command(m, conf);
}
if let Some(m) = matches.subcommand_matches("nodeid") {
return nodeid_command(m);
}
if let Some(_) = matches.subcommand_matches("version") {
println!(
"tonlabs-cli {}\nCOMMIT_ID: {}\nBUILD_DATE: {}\nCOMMIT_DATE: {}\nGIT_BRANCH: {}",
Expand Down Expand Up @@ -594,4 +602,22 @@ fn getconfig_command(matches: &ArgMatches, config: Config) -> Result<(), String>
let index = matches.value_of("INDEX");
print_args!(matches, index);
query_global_config(config, index.unwrap())
}
}

fn nodeid_command(matches: &ArgMatches) -> Result<(), String> {
let key = matches.value_of("KEY");
let keypair = matches.value_of("KEY_PAIR");
print_args!(matches, key, keypair);
let nodeid = if let Some(key) = key {
let vec = hex::decode(key)
.map_err(|e| format!("failed to decode public key: {}", e))?;
convert::nodeid_from_pubkey(&vec)?
} else if let Some(pair) = keypair {
let pair = crypto::load_keypair(pair)?;
convert::nodeid_from_pubkey(&pair.public.0)?
} else {
return Err("Either public key or key pair parameter should be provided".to_owned());
};
println!("{}", nodeid);
Ok(())
}
31 changes: 30 additions & 1 deletion tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,33 @@ fn test_callex() -> Result<(), Box<dyn std::error::Error>> {
.stdout(predicate::str::contains("Succeeded"));

Ok(())
}
}

#[test]
fn test_nodeid() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("nodeid")
.arg("--pubkey")
.arg("cde8fbf86c44e4ed2095f83b6f3c97b7aec55a77e06e843f8b9ffeab66ad4b32");
cmd.assert()
.success()
.stdout(predicate::str::contains("cdae19f3d5a96d016e74d656ef15e35839b554ae2590bec0dce5e6608cb7f837"));

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("nodeid")
.arg("--keypair")
.arg("tests/samples/exp.json");
cmd.assert()
.success()
.stdout(predicate::str::contains("e8c5df53b6205e8db639629d2cd2552b354501021a9f223bb72e81e75f37f64a"));

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("nodeid")
.arg("--keypair")
.arg("ghost frost pool buzz rival mad naive rare shell tooth smart praise");
cmd.assert()
.success()
.stdout(predicate::str::contains("e8c5df53b6205e8db639629d2cd2552b354501021a9f223bb72e81e75f37f64a"));

Ok(())
}

0 comments on commit b0fc1a7

Please sign in to comment.