-
Notifications
You must be signed in to change notification settings - Fork 30
Add SNI, NOISE and CERTHASH protocol in py-multiaddr #97
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
Open
lla-dane
wants to merge
5
commits into
multiformats:master
Choose a base branch
from
lla-dane:sni
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from typing import Any | ||
|
||
import multibase | ||
import multihash | ||
|
||
from ..codecs import CodecBase | ||
|
||
SIZE = -1 | ||
IS_PATH = False | ||
|
||
|
||
class Codec(CodecBase): | ||
""" | ||
Codec for certificate hashes (certhash). | ||
|
||
A certhash is a multihash of a certificate, encoded as a multibase string | ||
using the 'base64url' encoding. | ||
""" | ||
|
||
SIZE = SIZE | ||
IS_PATH = IS_PATH | ||
|
||
def validate(self, b: bytes) -> None: | ||
""" | ||
Validates that the byte representation is a valid multihash. | ||
|
||
Args: | ||
b: The bytes to validate. | ||
|
||
Raises: | ||
ValueError: If the bytes cannot be decoded as a multihash. | ||
""" | ||
try: | ||
multihash.decode(b) | ||
except Exception as e: | ||
raise ValueError("Invalid certhash: not a valid multihash") from e | ||
|
||
def to_bytes(self, proto: Any, string: str) -> bytes: | ||
""" | ||
Converts the multibase string representation of a certhash to bytes. | ||
|
||
This involves decoding the multibase string and then validating that | ||
the resulting bytes are a valid multihash. | ||
|
||
Args: | ||
proto: The multiaddr protocol code (unused). | ||
string: The string representation of the certhash. | ||
|
||
Returns: | ||
The raw multihash bytes. | ||
|
||
Raises: | ||
ValueError: If the string is not valid multibase or not a multihash. | ||
""" | ||
try: | ||
# Decode the multibase string to get the raw multihash bytes. | ||
decoded_bytes = multibase.decode(string) | ||
except Exception as e: | ||
raise ValueError(f"Failed to decode multibase string: {string}") from e | ||
|
||
# Validate that the decoded bytes are a valid multihash. | ||
self.validate(decoded_bytes) | ||
return decoded_bytes | ||
|
||
def to_string(self, proto: Any, buf: bytes) -> str: | ||
""" | ||
Converts the raw multihash bytes of a certhash to its string form. | ||
|
||
This involves validating the bytes first and then encoding them as a | ||
'base64url' multibase string. | ||
|
||
Args: | ||
proto: The multiaddr protocol code (unused). | ||
buf: The raw multihash bytes. | ||
|
||
Returns: | ||
The multibase string representation of the certhash. | ||
""" | ||
# Validate the bytes before encoding. | ||
self.validate(buf) | ||
|
||
# Encode the bytes using base64url, which is standard for certhash. | ||
# The result from `multibase.encode` is bytes, so we decode to a string. | ||
encoded_string = multibase.encode("base64url", buf) | ||
return encoded_string.decode("utf-8") |
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 @@ | ||
Add the sni protocol support in py-multiaddr in reference with go-multiaddr |
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
Oops, something went wrong.
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.
Also added a test case for
sni
multiaddr as given in go-multiaddr tests