Skip to content

Commit

Permalink
Merge pull request #35 from njgheorghita/private-key-cli-flag
Browse files Browse the repository at this point in the history
Add private key cli arg
  • Loading branch information
njgheorghita authored Jul 15, 2021
2 parents d8ce930 + ea4c672 commit ea546e5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ env_logger = "0.8.2"
eth2_ssz = "0.1.2"
eth2_ssz_derive = "0.1.0"
futures = "0.3.13"
hex = "0.4.3"
interfaces = "0.0.7"
lazy_static = "1.4.0"
log = "0.4.14"
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ FLAGS:
-V, --version Prints version information

OPTIONS:
--pool-size <pool_size> max size of threadpool [default: 2]
--web3-http-port <web3_http_port> port to accept json-rpc http connections [default: 8545]
--web3-ipc-path <web3_ipc_path> path to json-rpc endpoint over IPC [default: /tmp/trin-jsonrpc.ipc]
--web3-transport <web3_transport> select transport protocol to serve json-rpc endpoint [default: ipc]
[possible values: http, ipc]
--bootnodes <bootnodes> One or more comma-delimited base64-encoded ENR's or multiaddr strings of
peers to initially add to the local routing table [default: ]
--discovery-port <discovery_port> The UDP port to listen on. [default: 9000]
--external-address <external_addr> The public IP address and port under which this node is accessible
--pool-size <pool_size> max size of threadpool [default: 2]
--unsafe-private-key <private_key> Hex encoded 32 byte private key (considered unsafe to pass in pk as cli
arg, as it's stored in terminal history - keyfile support coming soon)
--web3-http-port <web3_http_port> port to accept json-rpc http connections [default: 8545]
--web3-ipc-path <web3_ipc_path> path to json-rpc endpoint over IPC [default: /tmp/trin-jsonrpc.ipc]
--web3-transport <web3_transport> select transport protocol to serve json-rpc endpoint [default: ipc]
[possible values: http, ipc]
```
## Gotchas
Expand Down
88 changes: 85 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct TrinConfig {
pub discovery_port: u16,
pub bootnodes: Vec<String>,
pub external_addr: Option<SocketAddr>,
pub private_key: Option<Vec<u8>>,
}

const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/trin-jsonrpc.ipc";
Expand Down Expand Up @@ -78,9 +79,15 @@ impl TrinConfig {
)
.arg(
Arg::with_name("external_addr")
.long("external-address")
.help("The public IP address and port under which this node is accessible")
.takes_value(true),
.long("external-address")
.help("The public IP address and port under which this node is accessible")
.takes_value(true),
)
.arg(
Arg::with_name("private_key")
.long("unsafe-private-key")
.help("Hex encoded 32 byte private key (considered unsafe to pass in pk as cli arg, as it's stored in terminal history - keyfile support coming soon)")
.takes_value(true),
)
.get_matches_from(args);

Expand All @@ -96,6 +103,19 @@ impl TrinConfig {
} else {
None
};
let private_key = if matches.is_present("private_key") {
let hex_private_key = value_t!(matches.value_of("private_key"), String)?;
match hex_private_key.len() {
64 => (),
val => panic!(
"Invalid private key length: {}, expected 32 byte hexstring",
val
),
}
Some(hex::decode(&hex_private_key).unwrap())
} else {
None
};

match web3_transport.as_str() {
"http" => match &web3_ipc_path[..] {
Expand Down Expand Up @@ -137,6 +157,7 @@ impl TrinConfig {
discovery_port,
bootnodes,
external_addr,
private_key,
})
}
}
Expand Down Expand Up @@ -164,6 +185,7 @@ mod test {
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec![],
external_addr: None,
private_key: None,
};
let actual_config = TrinConfig::new_from(["trin"].iter()).unwrap();
assert_eq!(actual_config.web3_transport, expected_config.web3_transport);
Expand All @@ -177,6 +199,7 @@ mod test {
assert!(env_is_set());
let expected_config = TrinConfig {
external_addr: None,
private_key: None,
web3_http_port: 8080,
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 3,
Expand Down Expand Up @@ -209,6 +232,7 @@ mod test {
TrinConfig::new_from(["trin", "--web3-transport", "ipc"].iter()).unwrap();
let expected_config = TrinConfig {
external_addr: None,
private_key: None,
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
Expand Down Expand Up @@ -236,6 +260,7 @@ mod test {
)
.unwrap();
let expected_config = TrinConfig {
private_key: None,
external_addr: None,
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: "/path/test.ipc".to_string(),
Expand Down Expand Up @@ -289,6 +314,7 @@ mod test {
assert!(env_is_set());
let expected_config = TrinConfig {
external_addr: None,
private_key: None,
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
Expand All @@ -306,6 +332,7 @@ mod test {
assert!(env_is_set());
let expected_config = TrinConfig {
external_addr: None,
private_key: None,
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
Expand Down Expand Up @@ -339,4 +366,59 @@ mod test {
Some(SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 1], 1234)))
);
}

#[test]
fn test_custom_private_key() {
assert!(env_is_set());
let expected_config = TrinConfig {
external_addr: None,
private_key: Some(vec![1; 32]),
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
web3_transport: "ipc".to_string(),
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec![],
};
let actual_config = TrinConfig::new_from(
[
"trin",
"--unsafe-private-key",
"0101010101010101010101010101010101010101010101010101010101010101",
]
.iter(),
)
.unwrap();
assert_eq!(actual_config.private_key, expected_config.private_key);
}

#[test]
#[should_panic(expected = "Invalid private key length")]
fn test_custom_private_key_odd_length() {
assert!(env_is_set());
TrinConfig::new_from(
[
"trin",
"--unsafe-private-key",
"010101010101010101010101010101010101010101010101010101010101010",
]
.iter(),
)
.unwrap_err();
}

#[test]
#[should_panic(expected = "Invalid private key length")]
fn test_custom_private_key_requires_32_bytes() {
assert!(env_is_set());
TrinConfig::new_from(
[
"trin",
"--unsafe-private-key",
"01010101010101010101010101010101010101010101010101010101010101",
]
.iter(),
)
.unwrap_err();
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let portalnet_config = PortalnetConfig {
external_addr: trin_config.external_addr,
private_key: trin_config.private_key.clone(),
listen_port: trin_config.discovery_port,
bootnode_enrs,
..Default::default()
Expand Down
7 changes: 6 additions & 1 deletion src/portalnet/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Config {
pub listen_port: u16,
pub discv5_config: Discv5Config,
pub bootnode_enrs: Vec<Enr>,
pub private_key: Option<Vec<u8>>,
}

impl Default for Config {
Expand All @@ -23,6 +24,7 @@ impl Default for Config {
listen_port: 4242,
discv5_config: Discv5Config::default(),
bootnode_enrs: vec![],
private_key: None,
}
}
}
Expand All @@ -37,7 +39,10 @@ pub struct Discovery {

impl Discovery {
pub fn new(config: Config) -> Result<Self, String> {
let enr_key = CombinedKey::generate_secp256k1();
let enr_key = match config.private_key {
Some(val) => CombinedKey::secp256k1_from_bytes(val.clone().as_mut_slice()).unwrap(),
None => CombinedKey::generate_secp256k1(),
};

let enr = {
let mut builder = EnrBuilder::new("v4");
Expand Down
3 changes: 3 additions & 0 deletions src/portalnet/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct PortalEndpoint {
#[derive(Clone)]
pub struct PortalnetConfig {
pub external_addr: Option<SocketAddr>,
pub private_key: Option<Vec<u8>>,
pub listen_port: u16,
pub bootnode_enrs: Vec<Enr>,
pub data_radius: U256,
Expand All @@ -44,6 +45,7 @@ impl Default for PortalnetConfig {
fn default() -> Self {
Self {
external_addr: None,
private_key: None,
listen_port: 4242,
bootnode_enrs: Vec::<Enr>::new(),
data_radius: U256::from(u64::MAX), //TODO better data_radius default?
Expand Down Expand Up @@ -198,6 +200,7 @@ impl PortalnetProtocol {
listen_port: external_addr.port(),
listen_address: external_addr.ip(),
bootnode_enrs: portal_config.bootnode_enrs,
private_key: portal_config.private_key,
..Default::default()
};

Expand Down

0 comments on commit ea546e5

Please # to comment.