Deterministic peer id from seed for bob

pull/127/head
Daniel Karzel 4 years ago
parent 0a21040e08
commit f18d01dfaf

@ -167,6 +167,7 @@ async fn main() -> Result<()> {
db, db,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
&seed,
) )
.await?; .await?;
} }
@ -243,6 +244,7 @@ async fn main() -> Result<()> {
db, db,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
&seed,
) )
.await?; .await?;
} }
@ -319,8 +321,9 @@ async fn bob_swap(
db: Database, db: Database,
alice_peer_id: PeerId, alice_peer_id: PeerId,
alice_addr: Multiaddr, alice_addr: Multiaddr,
seed: &Seed,
) -> Result<BobState> { ) -> Result<BobState> {
let bob_behaviour = bob::Behaviour::default(); let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed.bytes()));
let bob_transport = build(bob_behaviour.identity())?; let bob_transport = build(bob_behaviour.identity())?;
let (event_loop, handle) = let (event_loop, handle) =

@ -12,7 +12,7 @@ use crate::{
network::{ network::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
transport::SwapTransport, transport::SwapTransport,
TokioExecutor, Seed, TokioExecutor,
}, },
protocol::{alice, bob}, protocol::{alice, bob},
SwapAmounts, SwapAmounts,
@ -124,6 +124,20 @@ pub struct Behaviour {
} }
impl Behaviour { impl Behaviour {
pub fn new(seed: Seed) -> Self {
let identity = seed.derive_libp2p_identity();
Self {
pt: PeerTracker::default(),
amounts: Amounts::default(),
message0: message0::Behaviour::default(),
message1: message1::Behaviour::default(),
message2: message2::Behaviour::default(),
message3: message3::Behaviour::default(),
identity,
}
}
pub fn identity(&self) -> Keypair { pub fn identity(&self) -> Keypair {
self.identity.clone() self.identity.clone()
} }

@ -192,7 +192,8 @@ pub fn init_bob_event_loop(
alice_peer_id: PeerId, alice_peer_id: PeerId,
alice_addr: Multiaddr, alice_addr: Multiaddr,
) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { ) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) {
let bob_behaviour = bob::Behaviour::default(); let seed = Seed::random().unwrap();
let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed.bytes()));
let bob_transport = build(bob_behaviour.identity()).unwrap(); let bob_transport = build(bob_behaviour.identity()).unwrap();
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr) bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)
.unwrap() .unwrap()

Loading…
Cancel
Save