forked from driftluo/tentacle
-
Notifications
You must be signed in to change notification settings - Fork 26
multiaddr: Support Onion Protocol #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| use std::{borrow::Cow, fmt}; | ||
|
|
||
| /// Represents an Onion v3 address | ||
| #[derive(Clone)] | ||
| pub struct Onion3Addr<'a>(Cow<'a, [u8; 35]>, u16); | ||
|
|
||
| impl<'a> Onion3Addr<'a> { | ||
| /// Return the hash of the public key as bytes | ||
| pub fn hash(&self) -> &[u8; 35] { | ||
| self.0.as_ref() | ||
| } | ||
|
|
||
| /// Return the port | ||
| pub fn port(&self) -> u16 { | ||
| self.1 | ||
| } | ||
|
|
||
| /// Consume this instance and create an owned version containing the same address | ||
| pub fn acquire<'b>(self) -> Onion3Addr<'b> { | ||
| Onion3Addr(Cow::Owned(self.0.into_owned()), self.1) | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq for Onion3Addr<'_> { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.1 == other.1 && self.0[..] == other.0[..] | ||
| } | ||
| } | ||
|
|
||
| impl Eq for Onion3Addr<'_> {} | ||
|
|
||
| impl From<([u8; 35], u16)> for Onion3Addr<'_> { | ||
| fn from(parts: ([u8; 35], u16)) -> Self { | ||
| Self(Cow::Owned(parts.0), parts.1) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<(&'a [u8; 35], u16)> for Onion3Addr<'a> { | ||
| fn from(parts: (&'a [u8; 35], u16)) -> Self { | ||
| Self(Cow::Borrowed(parts.0), parts.1) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Debug for Onion3Addr<'_> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | ||
| f.debug_tuple("Onion3Addr") | ||
| .field(&format!("{:02x?}", &self.0[..])) | ||
| .field(&self.1) | ||
| .finish() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| mod onion; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| use data_encoding::HEXUPPER; | ||
| use std::convert::TryFrom; | ||
| use tentacle_multiaddr::*; | ||
|
|
||
| fn ma_valid(source: &str, target: &str, protocols: Vec<Protocol<'_>>) { | ||
| let parsed = source.parse::<Multiaddr>().unwrap(); | ||
| assert_eq!(HEXUPPER.encode(&parsed.to_vec()[..]), target); | ||
| assert_eq!(parsed.iter().collect::<Vec<_>>(), protocols); | ||
| assert_eq!(source.parse::<Multiaddr>().unwrap().to_string(), source); | ||
| assert_eq!( | ||
| Multiaddr::try_from(HEXUPPER.decode(target.as_bytes()).unwrap()).unwrap(), | ||
| parsed | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn construct_success() { | ||
| use Protocol::*; | ||
|
|
||
| ma_valid( | ||
| "/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234", | ||
| "BD03ADADEC040BE047F9658668B11A504F3155001F231A37F54C4476C07FB4CC139ED7E30304D2", | ||
| vec![Onion3( | ||
| ( | ||
| [ | ||
| 173, 173, 236, 4, 11, 224, 71, 249, 101, 134, 104, 177, 26, 80, 79, 49, 85, 0, | ||
| 31, 35, 26, 55, 245, 76, 68, 118, 192, 127, 180, 204, 19, 158, 215, 227, 3, | ||
| ], | ||
| 1234, | ||
| ) | ||
| .into(), | ||
| )], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn construct_fail() { | ||
| let addresses = [ | ||
| "/onion3/9ww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:80", | ||
| "/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd7:80", | ||
| "/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:0", | ||
| "/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:-1", | ||
| "/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd", | ||
| "/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy@:666", | ||
| ]; | ||
|
|
||
| for address in &addresses { | ||
| assert!( | ||
| address.parse::<Multiaddr>().is_err(), | ||
| "{}", | ||
| address.to_string() | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this fn is same as
split_header, why not reuse it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove
split_aton next release pr