Skip to content

Commit 88c156b

Browse files
authored
feat: use static certificate and gen deterministic peer id (#38)
* use static certificate * deterministic peer id * use 127 addr
1 parent 2940b4c commit 88c156b

File tree

2 files changed

+85
-42
lines changed

2 files changed

+85
-42
lines changed

packages/frontend/src/lib/libp2p.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ export async function startLibp2p(options: {} = {}) {
5656
}),],
5757
connectionEncryption: [noise()],
5858
streamMuxers: [yamux()],
59-
// peerDiscovery: [
60-
// bootstrap({
61-
// list: [
62-
// '/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiAYv08CIdaiohT6PVzWkv0-grXYo2VTAy9YltZ47C7VXg/p2p/12D3KooWRGSEnLtsbcuWS7RWZhXA8mxkMPtyKGsCwwwYRiexk2Kx',
63-
// ],
64-
// }),
65-
// ],
59+
peerDiscovery: [
60+
bootstrap({
61+
list: [
62+
'/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiBy_U1UNQ0IDvot_PKlQM_QeU3yx-zCAVaMxxVm2JxWBg/p2p/12D3KooWGTDZj1zAjMCJ8XXx9Z88zAAd6vn3krQYLwZ67S4vMUxz',
63+
],
64+
}),
65+
],
6666
pubsub: gossipsub({
6767
allowPublishToZeroPeers: true,
6868
msgIdFn: msgIdFnStrictNoSign,

rust-peer/src/main.rs

+78-35
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,57 @@ use libp2p::{
1515
};
1616
use libp2p_webrtc as webrtc;
1717
use log::{debug, error, info, warn};
18-
use rand::thread_rng;
19-
use std::time::Instant;
2018
use std::{
2119
borrow::Cow,
20+
collections::hash_map::DefaultHasher,
21+
fs::File,
2222
hash::{Hash, Hasher},
23+
io::{BufReader, Read},
24+
time::{Duration, Instant},
2325
};
24-
use std::{collections::hash_map::DefaultHasher, time::Duration};
25-
26-
// TODO: replace with our private bootstrap node
27-
// const BOOTNODES: [&str; 4] = [
28-
// "QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
29-
// "QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
30-
// "QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
31-
// "QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
32-
// ];
3326

3427
const TICK_INTERVAL: Duration = Duration::from_secs(15);
3528
const KADEMLIA_PROTOCOL_NAME: &'static [u8] = b"/universal-connectivity/lan/kad/1.0.0";
29+
const STATIC_CERTIFICATE: &'static [u8] = &[
30+
45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 69, 88, 80, 73, 82, 69, 83, 45, 45, 45, 45, 45, 10,
31+
65, 80, 102, 104, 110, 103, 56, 65, 65, 65, 65, 61, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 69,
32+
88, 80, 73, 82, 69, 83, 45, 45, 45, 45, 45, 10, 10, 45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32,
33+
80, 82, 73, 86, 65, 84, 69, 95, 75, 69, 89, 45, 45, 45, 45, 45, 10, 77, 73, 71, 72, 65, 103,
34+
69, 65, 77, 66, 77, 71, 66, 121, 113, 71, 83, 77, 52, 57, 65, 103, 69, 71, 67, 67, 113, 71, 83,
35+
77, 52, 57, 65, 119, 69, 72, 66, 71, 48, 119, 97, 119, 73, 66, 65, 81, 81, 103, 105, 86, 56,
36+
82, 72, 69, 100, 118, 85, 101, 48, 57, 108, 80, 57, 83, 10, 104, 52, 78, 70, 73, 74, 69, 70,
37+
113, 88, 104, 88, 43, 68, 85, 97, 51, 76, 109, 88, 112, 79, 119, 115, 117, 76, 71, 104, 82, 65,
38+
78, 67, 65, 65, 84, 115, 69, 73, 65, 53, 121, 113, 67, 119, 56, 78, 122, 73, 87, 73, 114, 117,
39+
111, 86, 87, 43, 109, 116, 83, 113, 105, 70, 122, 106, 10, 50, 51, 77, 84, 43, 107, 88, 73, 52,
40+
77, 77, 49, 115, 52, 109, 65, 121, 122, 100, 82, 57, 90, 113, 115, 81, 70, 72, 43, 104, 55, 86,
41+
75, 106, 70, 67, 86, 122, 73, 86, 70, 52, 76, 47, 49, 97, 54, 81, 75, 47, 86, 81, 47, 53, 120,
42+
102, 66, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 80, 82, 73, 86, 65, 84, 69, 95, 75, 69, 89,
43+
45, 45, 45, 45, 45, 10, 10, 45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 67, 69, 82, 84, 73, 70,
44+
73, 67, 65, 84, 69, 45, 45, 45, 45, 45, 10, 77, 73, 73, 66, 87, 84, 67, 66, 47, 54, 65, 68, 65,
45+
103, 69, 67, 65, 103, 104, 111, 117, 49, 43, 70, 77, 118, 103, 76, 88, 106, 65, 75, 66, 103,
46+
103, 113, 104, 107, 106, 79, 80, 81, 81, 68, 65, 106, 65, 104, 77, 82, 56, 119, 72, 81, 89, 68,
47+
86, 81, 81, 68, 68, 66, 90, 121, 10, 89, 50, 100, 108, 98, 105, 66, 122, 90, 87, 120, 109, 73,
48+
72, 78, 112, 90, 50, 53, 108, 90, 67, 66, 106, 90, 88, 74, 48, 77, 67, 65, 88, 68, 84, 99, 49,
49+
77, 68, 69, 119, 77, 84, 65, 119, 77, 68, 65, 119, 77, 70, 111, 89, 68, 122, 81, 119, 79, 84,
50+
89, 119, 77, 84, 65, 120, 10, 77, 68, 65, 119, 77, 68, 65, 119, 87, 106, 65, 104, 77, 82, 56,
51+
119, 72, 81, 89, 68, 86, 81, 81, 68, 68, 66, 90, 121, 89, 50, 100, 108, 98, 105, 66, 122, 90,
52+
87, 120, 109, 73, 72, 78, 112, 90, 50, 53, 108, 90, 67, 66, 106, 90, 88, 74, 48, 77, 70, 107,
53+
119, 69, 119, 89, 72, 10, 75, 111, 90, 73, 122, 106, 48, 67, 65, 81, 89, 73, 75, 111, 90, 73,
54+
122, 106, 48, 68, 65, 81, 99, 68, 81, 103, 65, 69, 55, 66, 67, 65, 79, 99, 113, 103, 115, 80,
55+
68, 99, 121, 70, 105, 75, 55, 113, 70, 86, 118, 112, 114, 85, 113, 111, 104, 99, 52, 57, 116,
56+
122, 69, 47, 112, 70, 10, 121, 79, 68, 68, 78, 98, 79, 74, 103, 77, 115, 51, 85, 102, 87, 97,
57+
114, 69, 66, 82, 47, 111, 101, 49, 83, 111, 120, 81, 108, 99, 121, 70, 82, 101, 67, 47, 57, 87,
58+
117, 107, 67, 118, 49, 85, 80, 43, 99, 88, 119, 97, 77, 102, 77, 66, 48, 119, 71, 119, 89, 68,
59+
86, 82, 48, 82, 10, 66, 66, 81, 119, 69, 111, 73, 81, 101, 85, 57, 76, 97, 122, 90, 84, 77,
60+
107, 70, 87, 97, 70, 108, 120, 86, 106, 86, 70, 89, 84, 65, 75, 66, 103, 103, 113, 104, 107,
61+
106, 79, 80, 81, 81, 68, 65, 103, 78, 74, 65, 68, 66, 71, 65, 105, 69, 65, 119, 65, 109, 109,
62+
47, 114, 103, 112, 10, 84, 80, 48, 57, 88, 77, 102, 83, 49, 118, 70, 79, 84, 65, 48, 122, 79,
63+
66, 75, 103, 53, 97, 56, 111, 86, 106, 83, 111, 89, 67, 48, 80, 83, 103, 52, 67, 73, 81, 68,
64+
54, 73, 121, 73, 115, 56, 76, 111, 117, 106, 109, 82, 102, 78, 102, 53, 115, 57, 106, 97, 121,
65+
72, 102, 107, 112, 10, 80, 67, 106, 106, 111, 104, 68, 112, 114, 122, 49, 67, 78, 75, 79, 66,
66+
53, 81, 61, 61, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 67, 69, 82, 84, 73, 70, 73, 67, 65, 84,
67+
69, 45, 45, 45, 45, 45, 10,
68+
];
3669

3770
#[derive(Debug, Parser)]
3871
#[clap(name = "universal connectivity rust peer")]
@@ -136,8 +169,15 @@ async fn main() -> Result<()> {
136169
// TODO (fixme): the below doesn't work because the address is still missing /webrtc/p2p even after https://github.com/libp2p/js-libp2p-webrtc/pull/121
137170
// swarm.behaviour_mut().kademlia.add_address(&peer_id, addr);
138171

139-
let webrtc_address = Multiaddr::try_from(addr.to_string() + "/webrtc/p2p/" + &peer_id.clone().to_string())?;
140-
swarm.behaviour_mut().kademlia.add_address(&peer_id, webrtc_address);
172+
let webrtc_address = Multiaddr::try_from(
173+
addr.to_string()
174+
+ "/webrtc/p2p/"
175+
+ &peer_id.clone().to_string(),
176+
)?;
177+
swarm
178+
.behaviour_mut()
179+
.kademlia
180+
.add_address(&peer_id, webrtc_address);
141181

142182
// TODO: below is how we should be constructing the address (not string manipulation)
143183
// let webrtc_address = addr.with(Protocol::WebRTC(peer_id.clone().into()));
@@ -160,11 +200,14 @@ async fn main() -> Result<()> {
160200
swarm.external_addresses().collect::<Vec<&AddressRecord>>()
161201
);
162202

163-
// if let Err(e) = swarm.behaviour_mut().kademlia.bootstrap() {
164-
// error!("Failed to run Kademlia bootstrap: {e:?}");
165-
// }
203+
if let Err(e) = swarm.behaviour_mut().kademlia.bootstrap() {
204+
debug!("Failed to run Kademlia bootstrap: {e:?}");
205+
}
166206

167-
let message = format!("Hello world! Sent from the rust-peer at: {:4}s", now.elapsed().as_secs_f64());
207+
let message = format!(
208+
"Hello world! Sent from the rust-peer at: {:4}s",
209+
now.elapsed().as_secs_f64()
210+
);
168211

169212
if let Err(err) = swarm.behaviour_mut().gossipsub.publish(
170213
gossipsub::IdentTopic::new("universal-connectivity"),
@@ -188,7 +231,14 @@ struct Behaviour {
188231
}
189232

190233
fn create_swarm() -> Result<Swarm<Behaviour>> {
191-
let local_key = identity::Keypair::generate_ed25519();
234+
let f = File::open("private_key")?;
235+
let mut reader = BufReader::new(f);
236+
let mut buffer = Vec::new();
237+
238+
reader.read_to_end(&mut buffer)?;
239+
240+
let local_key = identity::Keypair::ed25519_from_bytes(&mut buffer)?;
241+
192242
let local_peer_id = PeerId::from(local_key.public());
193243
debug!("Local peer id: {local_peer_id}");
194244

@@ -222,33 +272,26 @@ fn create_swarm() -> Result<Swarm<Behaviour>> {
222272
// subscribes to our topic
223273
gossipsub.subscribe(&topic)?;
224274

275+
let pem_str = std::str::from_utf8(STATIC_CERTIFICATE).unwrap();
276+
225277
let transport = webrtc::tokio::Transport::new(
226278
local_key.clone(),
227-
webrtc::tokio::Certificate::generate(&mut thread_rng())?,
228-
);
279+
webrtc::tokio::Certificate::from_pem(pem_str)?,
280+
)
281+
.map(|(local_peer_id, conn), _| (local_peer_id, StreamMuxerBox::new(conn)))
282+
.boxed();
229283

230-
let identify_config = identify::Behaviour::new(
231-
identify::Config::new("/ipfs/0.1.0".into(), local_key.public().clone())
232-
);
284+
let identify_config = identify::Behaviour::new(identify::Config::new(
285+
"/ipfs/0.1.0".into(),
286+
local_key.public().clone(),
287+
));
233288

234289
// Create a Kademlia behaviour.
235290
let mut cfg = KademliaConfig::default();
236291
cfg.set_protocol_names(vec![Cow::Owned(KADEMLIA_PROTOCOL_NAME.to_vec())]);
237292
let store = MemoryStore::new(local_peer_id);
238293
let kad_behaviour = Kademlia::with_config(local_peer_id, store, cfg);
239294

240-
// Add the bootnodes to the local routing table. `libp2p-dns` built
241-
// into the `transport` resolves the `dnsaddr` when Kademlia tries
242-
// to dial these nodes.
243-
// for peer in &BOOTNODES {
244-
// // TODO: update this
245-
// kad_behaviour.add_address(&peer.parse()?, "/dnsaddr/bootstrap.libp2p.io".parse()?);
246-
// }
247-
248-
let transport = transport
249-
.map(|(local_peer_id, conn), _| (local_peer_id, StreamMuxerBox::new(conn)))
250-
.boxed();
251-
252295
let behaviour = Behaviour {
253296
gossipsub,
254297
identify: identify_config,

0 commit comments

Comments
 (0)