aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2017-12-07 23:27:41 -0800
committerBryan Newbold <bnewbold@robocracy.org>2017-12-07 23:27:41 -0800
commit044d3de4981af84a7e6caab2d79ac273f77d7124 (patch)
tree9478bc79eca60635272417390703d21744b210de /src
parentd542e4e12df6031139f5ff9c8cc607b8524cf948 (diff)
downloadgeniza-044d3de4981af84a7e6caab2d79ac273f77d7124.tar.gz
geniza-044d3de4981af84a7e6caab2d79ac273f77d7124.zip
parse_dat_address() helper
Diffstat (limited to 'src')
-rw-r--r--src/bin/geniza-net.rs28
-rw-r--r--src/lib.rs43
2 files changed, 49 insertions, 22 deletions
diff --git a/src/bin/geniza-net.rs b/src/bin/geniza-net.rs
index 6d38eae..0c82e6d 100644
--- a/src/bin/geniza-net.rs
+++ b/src/bin/geniza-net.rs
@@ -12,22 +12,6 @@ use geniza::*;
use std::path::Path;
use clap::{App, SubCommand, Arg};
-fn parse_dat_key(raw_key: &str) -> Result<Vec<u8>> {
-
- if raw_key.len() != 32 * 2 {
- bail!("dat key not correct length");
- }
- let mut key_bytes = vec![];
- for i in 0..32 {
- let r = u8::from_str_radix(&raw_key[2 * i..2 * i + 2], 16);
- match r {
- Ok(b) => key_bytes.push(b),
- Err(e) => bail!("Problem with hex: {}", e),
- };
- }
- Ok(key_bytes)
-}
-
fn run() -> Result<()> {
env_logger::init().unwrap();
@@ -81,7 +65,7 @@ fn run() -> Result<()> {
("connect", Some(subm)) => {
let host_port = subm.value_of("host_port").unwrap();
let dat_key = subm.value_of("dat_key").unwrap();
- let key_bytes = parse_dat_key(&dat_key)?;
+ let key_bytes = parse_dat_address(&dat_key)?;
DatConnection::connect(host_port, &key_bytes, false)?;
println!("Done!");
}
@@ -89,7 +73,7 @@ fn run() -> Result<()> {
let host_port = subm.value_of("host_port").unwrap();
let dat_key = subm.value_of("dat_key").unwrap();
let count: u64 = subm.value_of("count").unwrap().parse().unwrap();
- let key_bytes = parse_dat_key(&dat_key)?;
+ let key_bytes = parse_dat_address(&dat_key)?;
let mut dc = DatConnection::connect(host_port, &key_bytes, false)?;
dc.receive_some(false, count)?;
dc.receive_some(true, count)?;
@@ -97,7 +81,7 @@ fn run() -> Result<()> {
}
("discovery-key", Some(subm)) => {
let dat_key = subm.value_of("dat_key").unwrap();
- let key_bytes = parse_dat_key(&dat_key)?;
+ let key_bytes = parse_dat_address(&dat_key)?;
let disc_key = make_discovery_key(&key_bytes);
for b in disc_key {
print!("{:02x}", b);
@@ -106,7 +90,7 @@ fn run() -> Result<()> {
}
("discovery-dns-name", Some(subm)) => {
let dat_key = subm.value_of("dat_key").unwrap();
- let key_bytes = parse_dat_key(&dat_key)?;
+ let key_bytes = parse_dat_address(&dat_key)?;
let disc_key = make_discovery_key(&key_bytes);
for b in 0..20 {
print!("{:02x}", disc_key[b]);
@@ -115,7 +99,7 @@ fn run() -> Result<()> {
}
("discover-dns", Some(subm)) => {
let dat_key = subm.value_of("dat_key").unwrap();
- let key_bytes = parse_dat_key(&dat_key)?;
+ let key_bytes = parse_dat_address(&dat_key)?;
let peers = discover_peers_dns(&key_bytes)?;
if peers.len() == 0 {
println!("No peers found!");
@@ -128,7 +112,7 @@ fn run() -> Result<()> {
("naive-clone", Some(subm)) => {
let host_port = subm.value_of("host_port").unwrap();
let dat_key = subm.value_of("dat_key").unwrap();
- let key_bytes = parse_dat_key(&dat_key)?;
+ let key_bytes = parse_dat_address(&dat_key)?;
let dir = Path::new(subm.value_of("dat-dir").unwrap());
let mut metadata = SleepDirRegister::create(&dir, "metadata")?;
node_simple_clone(host_port, &key_bytes, &mut metadata, false)?;
diff --git a/src/lib.rs b/src/lib.rs
index bc62d78..63000f7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -78,3 +78,46 @@ pub fn make_discovery_key(key: &[u8]) -> Vec<u8> {
discovery_key.to_vec()
}
+/// Helper to parse a dat address (aka, public key) in string format.
+///
+/// Address can start with 'dat://'. It should contain 64 hexadecimal characters.
+pub fn parse_dat_address(input: &str) -> Result<Vec<u8>> {
+
+ let raw_key = if input.starts_with("dat://") {
+ &input[6..]
+ } else {
+ input
+ };
+ if raw_key.len() != 32 * 2 {
+ bail!("dat key not correct length");
+ }
+ let mut key_bytes = vec![];
+ for i in 0..32 {
+ let r = u8::from_str_radix(&raw_key[2 * i..2 * i + 2], 16);
+ match r {
+ Ok(b) => key_bytes.push(b),
+ Err(e) => bail!("Problem with hex: {}", e),
+ };
+ }
+ Ok(key_bytes)
+}
+
+#[test]
+fn test_parse_dat_address() {
+
+ assert!(parse_dat_address(
+ "c7638882870abd4044d6467b0738f15e3a36f57c3a7f7f3417fd7e4e0841d597").is_ok());
+ assert!(parse_dat_address(
+ "C7638882870ABD4044D6467B0738F15E3A36F57C3A7F7F3417FD7E4E0841D597").is_ok());
+ assert!(parse_dat_address(
+ "dat://c7638882870abd4044d6467b0738f15e3a36f57c3a7f7f3417fd7e4e0841d597").is_ok());
+
+ assert!(parse_dat_address(
+ "c7638882870ab").is_err());
+ assert!(parse_dat_address(
+ "g7638882870abd4044d6467b0738f15e3a36f57c3a7f7f3417fd7e4e0841d597").is_err());
+ assert!(parse_dat_address(
+ "dat://c7638882870abd4044d6467b0738f15e3a36f57c3a7f7f3417fd7e4e0841d5970").is_err());
+ assert!(parse_dat_address(
+ "dat://c7638882870abd4044d6467b0738f15e3a36f57c3a7f7f3417fd7e4e0841d59").is_err());
+}