From d80cc393cf04ff5dc15c8d755a20662d8b8f38f7 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Fri, 8 Jan 2021 12:13:07 +1100 Subject: [PATCH] Deterministic peer id from seed for bob --- swap/src/bob.rs | 6 +++--- swap/src/main.rs | 7 ++++++- swap/tests/testutils/mod.rs | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/swap/src/bob.rs b/swap/src/bob.rs index 33917b57..e58a5d68 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -5,7 +5,7 @@ use crate::{ network::{ peer_tracker::{self, PeerTracker}, transport::SwapTransport, - TokioExecutor, + Seed, TokioExecutor, }, SwapAmounts, }; @@ -121,8 +121,8 @@ pub struct Behaviour { } impl Behaviour { - pub fn identity(&self) -> Keypair { - self.identity.clone() + pub fn identity(&self, seed: Seed) -> Keypair { + seed.derive_libp2p_identity() } pub fn peer_id(&self) -> PeerId { diff --git a/swap/src/main.rs b/swap/src/main.rs index c2edb55f..9f7171ca 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -170,6 +170,7 @@ async fn main() -> Result<()> { alice_peer_id, alice_addr, config, + &seed, ) .await?; } @@ -247,6 +248,7 @@ async fn main() -> Result<()> { alice_peer_id, alice_addr, config, + &seed, ) .await?; } @@ -325,9 +327,12 @@ async fn bob_swap( alice_peer_id: PeerId, alice_addr: Multiaddr, config: Config, + seed: &Seed, ) -> Result { let bob_behaviour = bob::Behaviour::default(); - let bob_transport = build(bob_behaviour.identity())?; + + let identity = bob_behaviour.identity(network::Seed::new(seed.bytes())); + let bob_transport = build(identity)?; let (event_loop, handle) = bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)?; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 562b21d7..70017069 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -188,8 +188,9 @@ pub fn init_bob_event_loop( alice_peer_id: PeerId, alice_addr: Multiaddr, ) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { + let seed = Seed::random().unwrap(); let bob_behaviour = bob::Behaviour::default(); - let bob_transport = build(bob_behaviour.identity()).unwrap(); + let bob_transport = build(bob_behaviour.identity(network::Seed::new(seed.bytes()))).unwrap(); bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr) .unwrap() }