Deterministic peer id from seed for bob

This commit is contained in:
Daniel Karzel 2021-01-08 12:13:07 +11:00
parent 0e09c08af5
commit d80cc393cf
3 changed files with 11 additions and 5 deletions

View file

@ -5,7 +5,7 @@ use crate::{
network::{ network::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
transport::SwapTransport, transport::SwapTransport,
TokioExecutor, Seed, TokioExecutor,
}, },
SwapAmounts, SwapAmounts,
}; };
@ -121,8 +121,8 @@ pub struct Behaviour {
} }
impl Behaviour { impl Behaviour {
pub fn identity(&self) -> Keypair { pub fn identity(&self, seed: Seed) -> Keypair {
self.identity.clone() seed.derive_libp2p_identity()
} }
pub fn peer_id(&self) -> PeerId { pub fn peer_id(&self) -> PeerId {

View file

@ -170,6 +170,7 @@ async fn main() -> Result<()> {
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
config, config,
&seed,
) )
.await?; .await?;
} }
@ -247,6 +248,7 @@ async fn main() -> Result<()> {
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
config, config,
&seed,
) )
.await?; .await?;
} }
@ -325,9 +327,12 @@ async fn bob_swap(
alice_peer_id: PeerId, alice_peer_id: PeerId,
alice_addr: Multiaddr, alice_addr: Multiaddr,
config: Config, config: Config,
seed: &Seed,
) -> Result<BobState> { ) -> Result<BobState> {
let bob_behaviour = bob::Behaviour::default(); 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) = let (event_loop, handle) =
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)?;

View file

@ -188,8 +188,9 @@ 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 seed = Seed::random().unwrap();
let bob_behaviour = bob::Behaviour::default(); 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) bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)
.unwrap() .unwrap()
} }