Skip to content

Commit 911b18a

Browse files
authored
src/protocol.rs: Add /webrtc and /certhash (#59)
1 parent 8a4bd49 commit 911b18a

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# 0.15.0 [unreleased]
22

3+
- Add `WebRTC` instance for `Multiaddr`. See [PR 59].
4+
- Add `Certhash` instance for `Multiaddr`. See [PR 59].
5+
36
- Add support for Noise protocol. See [PR 53].
47

58
- Use `multibase` instead of `bs58` for base58 encoding. See [PR 56].
69

710
[PR 53]: https://github.com/multiformats/rust-multiaddr/pull/53
811
[PR 56]: https://github.com/multiformats/rust-multiaddr/pull/56
12+
[PR 59]: https://github.com/multiformats/rust-multiaddr/pull/59
913

1014
# 0.14.0 [2022-02-02]
1115

src/protocol.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const IP4: u32 = 4;
2828
const IP6: u32 = 41;
2929
const P2P_WEBRTC_DIRECT: u32 = 276;
3030
const P2P_WEBRTC_STAR: u32 = 275;
31+
const WEBRTC: u32 = 280;
32+
const CERTHASH: u32 = 466;
3133
const P2P_WEBSOCKET_STAR: u32 = 479;
3234
const MEMORY: u32 = 777;
3335
const ONION: u32 = 444;
@@ -80,6 +82,8 @@ pub enum Protocol<'a> {
8082
Ip6(Ipv6Addr),
8183
P2pWebRtcDirect,
8284
P2pWebRtcStar,
85+
WebRTC,
86+
Certhash(Multihash),
8387
P2pWebSocketStar,
8488
/// Contains the "port" to contact. Similar to TCP or UDP, 0 means "assign me a port".
8589
Memory(u64),
@@ -192,6 +196,12 @@ impl<'a> Protocol<'a> {
192196
}
193197
"p2p-websocket-star" => Ok(Protocol::P2pWebSocketStar),
194198
"p2p-webrtc-star" => Ok(Protocol::P2pWebRtcStar),
199+
"webrtc" => Ok(Protocol::WebRTC),
200+
"certhash" => {
201+
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
202+
let (_base, decoded) = multibase::decode(s)?;
203+
Ok(Protocol::Certhash(Multihash::from_bytes(&decoded)?))
204+
}
195205
"p2p-webrtc-direct" => Ok(Protocol::P2pWebRtcDirect),
196206
"p2p-circuit" => Ok(Protocol::P2pCircuit),
197207
"memory" => {
@@ -268,6 +278,12 @@ impl<'a> Protocol<'a> {
268278
}
269279
P2P_WEBRTC_DIRECT => Ok((Protocol::P2pWebRtcDirect, input)),
270280
P2P_WEBRTC_STAR => Ok((Protocol::P2pWebRtcStar, input)),
281+
WEBRTC => Ok((Protocol::WebRTC, input)),
282+
CERTHASH => {
283+
let (n, input) = decode::usize(input)?;
284+
let (data, rest) = split_at(n, input)?;
285+
Ok((Protocol::Certhash(Multihash::from_bytes(data)?), rest))
286+
}
271287
P2P_WEBSOCKET_STAR => Ok((Protocol::P2pWebSocketStar, input)),
272288
MEMORY => {
273289
let (data, rest) = split_at(8, input)?;
@@ -441,6 +457,13 @@ impl<'a> Protocol<'a> {
441457
}
442458
Protocol::P2pWebSocketStar => w.write_all(encode::u32(P2P_WEBSOCKET_STAR, &mut buf))?,
443459
Protocol::P2pWebRtcStar => w.write_all(encode::u32(P2P_WEBRTC_STAR, &mut buf))?,
460+
Protocol::WebRTC => w.write_all(encode::u32(WEBRTC, &mut buf))?,
461+
Protocol::Certhash(hash) => {
462+
w.write_all(encode::u32(CERTHASH, &mut buf))?;
463+
let bytes = hash.to_bytes();
464+
w.write_all(encode::usize(bytes.len(), &mut encode::usize_buffer()))?;
465+
w.write_all(&bytes)?
466+
}
444467
Protocol::P2pWebRtcDirect => w.write_all(encode::u32(P2P_WEBRTC_DIRECT, &mut buf))?,
445468
Protocol::P2pCircuit => w.write_all(encode::u32(P2P_CIRCUIT, &mut buf))?,
446469
Protocol::Memory(port) => {
@@ -466,6 +489,8 @@ impl<'a> Protocol<'a> {
466489
Ip6(a) => Ip6(a),
467490
P2pWebRtcDirect => P2pWebRtcDirect,
468491
P2pWebRtcStar => P2pWebRtcStar,
492+
WebRTC => WebRTC,
493+
Certhash(hash) => Certhash(hash),
469494
P2pWebSocketStar => P2pWebSocketStar,
470495
Memory(a) => Memory(a),
471496
Onion(addr, port) => Onion(Cow::Owned(addr.into_owned()), port),
@@ -502,6 +527,12 @@ impl<'a> fmt::Display for Protocol<'a> {
502527
Ip6(addr) => write!(f, "/ip6/{}", addr),
503528
P2pWebRtcDirect => f.write_str("/p2p-webrtc-direct"),
504529
P2pWebRtcStar => f.write_str("/p2p-webrtc-star"),
530+
WebRTC => f.write_str("/webrtc"),
531+
Certhash(hash) => write!(
532+
f,
533+
"/certhash/{}",
534+
multibase::encode(multibase::Base::Base64Url, hash.to_bytes())
535+
),
505536
P2pWebSocketStar => f.write_str("/p2p-websocket-star"),
506537
Memory(port) => write!(f, "/memory/{}", port),
507538
Onion(addr, port) => {

tests/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ fn construct_success() {
338338
"047F00000106007FC603",
339339
vec![Ip4(local), Tcp(127), Noise],
340340
);
341+
342+
ma_valid(
343+
"/ip4/127.0.0.1/udp/1234/webrtc",
344+
"047F000001910204D29802",
345+
vec![Ip4(local), Udp(1234), WebRTC],
346+
);
347+
348+
let (_base, decoded) =
349+
multibase::decode("uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g").unwrap();
350+
ma_valid(
351+
"/ip4/127.0.0.1/udp/1234/webrtc/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
352+
"047F000001910204D29802D203221220C3AB8FF13720E8AD9047DD39466B3C8974E592C2FA383D4A3960714CAEF0C4F2",
353+
vec![
354+
Ip4(local),
355+
Udp(1234),
356+
WebRTC,
357+
Certhash(Multihash::from_bytes(&decoded).unwrap()),
358+
],
359+
);
341360
}
342361

343362
#[test]
@@ -374,6 +393,8 @@ fn construct_fail() {
374393
"/ip4/127.0.0.1/p2p",
375394
"/ip4/127.0.0.1/p2p/tcp",
376395
"/p2p-circuit/50",
396+
"/ip4/127.0.0.1/udp/1234/webrtc/certhash",
397+
"/ip4/127.0.0.1/udp/1234/webrtc/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp", // 1 character missing from certhash
377398
];
378399

379400
for address in &addresses {

0 commit comments

Comments
 (0)