aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2017-10-26 00:29:19 -0700
committerBryan Newbold <bnewbold@robocracy.org>2017-10-26 00:29:19 -0700
commitffd90794391678a25d794ed10c50c7e376cadb04 (patch)
tree574f8f7549650ca8dc6f2ef62dee832efcb63d81
parent6910602ac2e7bc7afa8a362438bcd448855520cb (diff)
downloadgeniza-ffd90794391678a25d794ed10c50c7e376cadb04.tar.gz
geniza-ffd90794391678a25d794ed10c50c7e376cadb04.zip
fold TcpSodiumReader/Writer into DatConnection itself
-rw-r--r--src/sync.rs23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/sync.rs b/src/sync.rs
index 7dcf8e3..3a7c6f7 100644
--- a/src/sync.rs
+++ b/src/sync.rs
@@ -141,6 +141,9 @@ pub struct DatConnection {
tcp: TcpStream,
live: bool,
key: Key,
+ discovery_key: [u8; 32],
+ data_key: [u8; 32],
+ data_discovery_key: [u8; 32],
tx_nonce: Nonce,
tx_offset: u64,
rx_nonce: Nonce,
@@ -180,6 +183,15 @@ impl Write for DatConnection {
}
}
+fn discovery_key(key: &Key) => Vec<u8> {
+ // calculate discovery key
+ let mut discovery_key = [0; 32];
+ let mut hash = Blake2b::new_keyed(32, key);
+ hash.input(&"hypercore".as_bytes());
+ hash.result(&mut discovery_key);
+ discovery_key.to_vec()
+}
+
impl DatConnection {
pub fn connect(host_port: &str, key: &[u8], live: bool) -> Result<DatConnection> {
@@ -190,6 +202,9 @@ impl DatConnection {
let mut rng = OsRng::new()?;
rng.fill_bytes(&mut local_id);
+ let dk = [0; 32];
+ dk[0..32] = discovery_key(key)[0..32];
+
// Connect to server
info!("Connecting to {}", host_port);
// TODO: timeout on connect (socketaddr dance)
@@ -203,6 +218,9 @@ impl DatConnection {
live,
remote_id: [0; 32],
key: Key::from_slice(key).unwrap(), // TODO:
+ discovery_key,
+ data_key: [0; 32],
+ data_discovery_key: [0; 32],
tx_nonce: tx_nonce,
tx_offset: 0,
rx_nonce: gen_nonce(), // dummy
@@ -211,11 +229,6 @@ impl DatConnection {
// Exchange register/feed
dc.tcp.set_nodelay(true)?; // Faster handshake
- // calculate discovery key
- let mut discovery_key = [0; 32];
- let mut hash = Blake2b::new_keyed(32, key);
- hash.input(&"hypercore".as_bytes());
- hash.result(&mut discovery_key);
// send register
let mut register_msg = Feed::new();
register_msg.set_discoveryKey(discovery_key.to_vec());