Skip to content

Commit

Permalink
fix: Add support for webrtc and certhash (multiformats#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Aug 9, 2022
1 parent e87460e commit 5ea7b52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"dns-over-http-resolver": "^2.1.0",
"err-code": "^3.0.1",
"is-ip": "^4.0.0",
"multibase": "^4.0.6",
"multiformats": "^9.4.5",
"uint8arrays": "^3.0.0",
"varint": "^6.0.0"
Expand Down
11 changes: 11 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

/**
* Converts bytes to bas58btc string
*/
Expand Down
2 changes: 2 additions & 0 deletions src/protocols-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const table: Array<[number, number, string, boolean?, boolean?]> = [
[275, 0, 'p2p-webrtc-star'],
[276, 0, 'p2p-webrtc-direct'],
[277, 0, 'p2p-stardust'],
[280, 0, 'webrtc'],
[290, 0, 'p2p-circuit'],
[301, 0, 'udt'],
[302, 0, 'utp'],
Expand All @@ -40,6 +41,7 @@ export const table: Array<[number, number, string, boolean?, boolean?]> = [
[445, 296, 'onion3'],
[446, V, 'garlic64'],
[460, 0, 'quic'],
[466, V, 'certhash'],
[477, 0, 'ws'],
[478, 0, 'wss'],
[479, 0, 'p2p-websocket-star'],
Expand Down

0 comments on commit 5ea7b52

Please sign in to comment.