-
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
Changes from 1 commit
5ea7b52
f351942
cab5d25
178b660
344e8f8
33e5b30
561bf9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { getProtocol } from './protocols-table.js' | |
import { CID } from 'multiformats/cid' | ||
import { base32 } from 'multiformats/bases/base32' | ||
import { base58btc } from 'multiformats/bases/base58' | ||
import * as MB from 'multibase' | ||
import * as Digest from 'multiformats/hashes/digest' | ||
import varint from 'varint' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
|
@@ -51,6 +52,8 @@ export function convertToString (proto: number | string, buf: Uint8Array) { | |
return bytes2onion(buf) | ||
case 445: // onion3 | ||
return bytes2onion(buf) | ||
case 466: //certhash | ||
return bytes2mh(buf) | ||
default: | ||
return uint8ArrayToString(buf, 'base16') // no clue. convert to hex | ||
} | ||
|
@@ -84,6 +87,8 @@ export function convertToBytes (proto: string | number, str: string) { | |
return onion2bytes(str) | ||
case 445: // onion3 | ||
return onion32bytes(str) | ||
case 466: //certhash | ||
return mb2bytes(str) | ||
default: | ||
return uint8ArrayFromString(str, 'base16') // no clue. convert from hex | ||
} | ||
|
@@ -148,6 +153,12 @@ function mh2bytes (hash: string) { | |
return uint8ArrayConcat([size, mh], size.length + mh.length) | ||
} | ||
|
||
function mb2bytes(mbstr: string) { | ||
let mb = MB.decode(mbstr) | ||
const size = Uint8Array.from(varint.encode(mb.length)) | ||
return uint8ArrayConcat([size, mb], size.length + mb.length) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any verification required that the decoded multibase contains a valid multihash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure there's an easy way to verify that. The existing multihash function does one validation I did not replicate here, https://github.com/multiformats/js-multiaddr/blob/master/src/convert.ts#L158-L160 That it reports a size that is correct. I could (maybe should) do that. But to go beyond that I think we'd have to be aware of all the supported encryption schemes, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @achingbrain - thoughts on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine, otherwise as you say it would have to know about all the available hash algorithms. |
||
} | ||
|
||
/** | ||
* Converts bytes to bas58btc string | ||
*/ | ||
|
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.
do we need to handle the case where decoding the multibase fails?
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.
decode
would throw in some failure caseshttps://multiformats.github.io/js-multibase/#decode
https://github.com/multiformats/js-multibase/blob/dde00682ed1a2539677c1e2b17dc6a3c112c8e3a/src/base.js#L40-L47
That said I don't think multibase is aware of what kind of data it's decoding, so it should not be able to detect things like the field being truncated. I don't think the existing analogous multihash functions do validation of this kind in all cases either, though.
https://github.com/multiformats/js-multiaddr/blob/master/src/convert.ts#L140-L142