-
Notifications
You must be signed in to change notification settings - Fork 64
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
feat: add support for webrtc and certhash (#261) #262
Merged
achingbrain
merged 7 commits into
multiformats:master
from
little-bear-labs:jt/261-webrtc
Aug 30, 2022
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ea7b52
fix: Add support for webrtc and certhash (#261)
John-LittleBearLabs f351942
Added a test for certhash encoding.
John-LittleBearLabs cab5d25
Updated to reduce size of dependency.
John-LittleBearLabs 178b660
Revert "Updated to reduce size of dependency."
John-LittleBearLabs 344e8f8
Supporting the others, removed custom proto-specific codecs.
John-LittleBearLabs 33e5b30
Back to base64url as the default base for ToString
John-LittleBearLabs 561bf9b
Updated for PR comments.
John-LittleBearLabs 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 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 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,45 @@ | ||
import { ParseError } from './codec.js' | ||
import type { Codec } from './protocols-table' | ||
import { concat as uint8ArrayConcat } from 'uint8arrays/concat' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
import varint from 'varint' | ||
import { base16 } from 'multiformats/bases/base16' | ||
import { base58btc } from 'multiformats/bases/base58' | ||
import { base64url } from 'multiformats/bases/base64' | ||
import type { BaseDecoder } from 'multiformats/bases/interface' | ||
|
||
const encodings: {[prefix: string]: BaseDecoder} = { | ||
'f': base16, | ||
'u': base64url, | ||
'z': base58btc, | ||
}; | ||
|
||
export class DefaultCerthashCodec implements Codec { | ||
stringToBytes(stringRepresentation: string): Uint8Array { | ||
if (stringRepresentation.length < 2) { | ||
throw ParseError('Not enough length to be a multibase: ' + stringRepresentation); | ||
} | ||
let prefix = stringRepresentation[0]; | ||
let encoded = stringRepresentation.slice(1); | ||
let encoding = encodings[prefix]; | ||
let decoded: Uint8Array; | ||
if (encoding) { | ||
decoded = encoding.baseDecode(encoded); | ||
} else { | ||
throw new Error('certhash is in a multibase encoding that is not supported by default. Please provide a custom decoder: '+stringRepresentation); | ||
} | ||
const size = Uint8Array.from(varint.encode(decoded.length)) | ||
return uint8ArrayConcat([size, decoded], size.length + decoded.length) | ||
} | ||
bytesToString(byteRepresentation: Uint8Array): string { | ||
const size = varint.decode(byteRepresentation) | ||
const hash = byteRepresentation.slice(varint.decode.bytes) | ||
|
||
if (hash.length !== size) { | ||
throw new Error('inconsistent lengths') | ||
} | ||
|
||
return 'z' + uint8ArrayToString(hash, 'base58btc') | ||
} | ||
|
||
} |
This file contains 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 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 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 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
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.
I am not familiar with the JS multiformats stack. I am surprised that this mapping needs to be defined. I would expect this to be a general multihash and not a certhash specific mapping is.
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.
You can compose a decoder from several other decoders, though the types are a little wonky.
Do you mean not limited to the
f
,u
orz
prefixes?multiformat
s follows a pattern where you only include the codecs you need instead of the kitchen-sink approach.You can, of course, compose your codec out of all available codecs.
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.
@mxinden I believe my first pass supported all the encodings for a "general multihash", and I may have read too much into @achingbrain 's criticism of that (that the dependency I was using was too large because it supported too much). That's why I limited what I'm bringing in by default and provided the mechanism for the caller to dependency-inject a broader codec if wished.
Will look into using
or
to compose decoders. And to be clear, we're saying we want to support all of them, to take the draft spec as currently written literally, regardless of who is using this library? Except, perhaps, encodings with '/' in their alphabet?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 was not referring to supporting all multihash variants, but instead to the fact that the mapping between identifier (e.g.
f
) and its multihash variant (herebase16
) needs to be written by the user. That seems error prone to me. I might just be missing something.That is how we implement it in Rust multiformats/rust-multiaddr#59 and Golang multiformats/go-multiaddr#176. That said, both encode with
Base64Url
. Thus I think it is fine to support only a minimal set of bases for now.We might require users to use sha256. In the same vein, we could as well require users to use
Base64Url
for now. Thoughts?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 put all in quotes because of the '/' issue. I didn't see an easy way to detect the alphabets from the dictionary provided by the
multiformats
library so I didn't filter them out. I would just guess that a base64-encoded certhash that happens to have a / in it, if placed in a Multiaddr, would cause problems at runtime.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.
For the record libp2p/specs@7009f94 specifies the requirement of supporting
sha256
andbase64url
. I don't have an opinion on whether js-multiaddr should support any other algorithms / encodings beyond that.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.
Well-spotted. If the other side is only required to accept those two, then base58btc was a very poor choice of encoding for convertToString(). I'll fix that.
I think we can afford to leave the convertToBytes as supporting a bunch of 'em, since I'm not adding anything new to package.json to get it. But I'll certainly remove those others if advised to do so.