2021-01-08 01:04:48 +00:00
|
|
|
use crate::seed::SEED_LENGTH;
|
|
|
|
use bitcoin::hashes::{sha256, Hash, HashEngine};
|
2020-10-15 22:14:39 +00:00
|
|
|
use futures::prelude::*;
|
2021-01-08 01:04:48 +00:00
|
|
|
use libp2p::{core::Executor, identity::ed25519};
|
2020-10-15 22:14:39 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use tokio::runtime::Handle;
|
|
|
|
|
|
|
|
pub mod peer_tracker;
|
|
|
|
pub mod request_response;
|
|
|
|
pub mod transport;
|
|
|
|
|
2020-12-23 03:33:29 +00:00
|
|
|
#[allow(missing_debug_implementations)]
|
2020-10-15 22:14:39 +00:00
|
|
|
pub struct TokioExecutor {
|
|
|
|
pub handle: Handle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Executor for TokioExecutor {
|
|
|
|
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
|
|
|
|
let _ = self.handle.spawn(future);
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 01:04:48 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub struct Seed([u8; SEED_LENGTH]);
|
|
|
|
|
|
|
|
impl Seed {
|
|
|
|
/// prefix "NETWORK" to the provided seed and apply sha256
|
2021-01-08 03:52:29 +00:00
|
|
|
pub fn new(seed: crate::seed::Seed) -> Self {
|
2021-01-08 01:04:48 +00:00
|
|
|
let mut engine = sha256::HashEngine::default();
|
|
|
|
|
2021-01-08 03:52:29 +00:00
|
|
|
engine.input(&seed.bytes());
|
2021-01-08 01:04:48 +00:00
|
|
|
engine.input(b"NETWORK");
|
|
|
|
|
|
|
|
let hash = sha256::Hash::from_engine(engine);
|
|
|
|
Self(hash.into_inner())
|
|
|
|
}
|
|
|
|
|
2021-01-08 03:52:29 +00:00
|
|
|
fn bytes(&self) -> [u8; SEED_LENGTH] {
|
2021-01-08 01:04:48 +00:00
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn derive_libp2p_identity(&self) -> libp2p::identity::Keypair {
|
|
|
|
let mut engine = sha256::HashEngine::default();
|
|
|
|
|
|
|
|
engine.input(&self.bytes());
|
|
|
|
engine.input(b"LIBP2P_IDENTITY");
|
|
|
|
|
|
|
|
let hash = sha256::Hash::from_engine(engine);
|
|
|
|
let key =
|
|
|
|
ed25519::SecretKey::from_bytes(hash.into_inner()).expect("we always pass 32 bytes");
|
|
|
|
libp2p::identity::Keypair::Ed25519(key.into())
|
|
|
|
}
|
|
|
|
}
|