Skip to content

Commit

Permalink
Added key tool and docker file (MystenLabs#1465)
Browse files Browse the repository at this point in the history
* Added key tool and docker file
  • Loading branch information
oxade authored Apr 26, 2022
1 parent 59a491c commit ec5ecd1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions faucet/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub async fn start_test_network(
db_path: info.db_path.clone(),
stake: info.stake,
consensus_address: info.consensus_address,
address: SuiAddress::from(info.key_pair.public_key_bytes()),
})
.collect();
genesis_config.authorities = authorities;
Expand Down
2 changes: 1 addition & 1 deletion sui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ jsonrpsee-proc-macros = "0.10.1"
tracing-test = "0.2.1"

[features]
benchmark = ["narwhal-node/benchmark"]
benchmark = ["narwhal-node/benchmark"]
39 changes: 39 additions & 0 deletions sui/src/bin/key_tool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use clap::*;
use std::{fs, path::Path};
use sui_types::{
base_types::SuiAddress,
crypto::{get_key_pair, KeyPair},
};

#[allow(clippy::large_enum_variant)]
#[derive(Parser)]
#[clap(
name = "Sui Key Tool",
about = "Utility For Generating Keys and Addresses Encoded as Base64 Bytes",
rename_all = "kebab-case"
)]
enum KeyToolOpt {
/// Generate a keypair
Generate {},

/// Extract components
Unpack { keypair: KeyPair },
}

fn main() {
let res = match KeyToolOpt::parse() {
KeyToolOpt::Generate {} => get_key_pair(),
KeyToolOpt::Unpack { keypair } => (SuiAddress::from(keypair.public_key_bytes()), keypair),
};
let path_str = format!("{}.key", res.0).to_lowercase();
let path = Path::new(&path_str);
let address = format!("{}", res.0);
let kp = serde_json::to_string(&res.1).unwrap();
let kp = &kp[1..kp.len() - 1];
let out_str = format!("address: {}\nkeypair: {}", address, kp);
fs::write(path, out_str).unwrap();
println!("Address and keypair written to {}", path.to_str().unwrap());
}
2 changes: 2 additions & 0 deletions sui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct AuthorityInfo {

#[derive(Serialize, Debug)]
pub struct AuthorityPrivateInfo {
pub address: SuiAddress,
pub key_pair: KeyPair,
pub host: String,
pub port: u16,
Expand Down Expand Up @@ -107,6 +108,7 @@ impl<'de> Deserialize<'de> for AuthorityPrivateInfo {
};

Ok(AuthorityPrivateInfo {
address: SuiAddress::from(key_pair.public_key_bytes()),
key_pair,
host,
port,
Expand Down
4 changes: 2 additions & 2 deletions sui/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async fn main() -> Result<(), anyhow::Error> {
json_log_output: std::env::var("SUI_JSON_SPAN_LOGS").is_ok(),
..Default::default()
};
#[allow(unused)]
let guard = telemetry_subscribers::init(config);

let _guard = telemetry_subscribers::init(config);

let cfg = ValidatorOpt::parse();

Expand Down
15 changes: 15 additions & 0 deletions sui_types/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::committee::EpochId;
use crate::error::{SuiError, SuiResult};
use crate::readable_serde::encoding::Base64;
use crate::readable_serde::Readable;
use anyhow::anyhow;
use anyhow::Error;
use base64ct::Encoding;
use digest::Digest;
Expand All @@ -21,6 +22,7 @@ use serde_with::Bytes;
use sha3::Sha3_256;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::str::FromStr;

// TODO: Make sure secrets are not copyable and movable to control where they are in memory
#[derive(Debug)]
Expand Down Expand Up @@ -95,6 +97,19 @@ impl<'de> Deserialize<'de> for KeyPair {
}
}

impl FromStr for KeyPair {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = base64ct::Base64::decode_vec(s).map_err(|e| anyhow!("{}", e.to_string()))?;
let key = dalek::Keypair::from_bytes(&value).map_err(|e| anyhow!("{}", e.to_string()))?;
Ok(KeyPair {
key_pair: key,
public_key_cell: OnceCell::new(),
})
}
}

impl signature::Signer<Signature> for KeyPair {
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> {
let signature_bytes = self.key_pair.try_sign(msg)?;
Expand Down

0 comments on commit ec5ecd1

Please sign in to comment.