Skip to content
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

fix: use codes instead of strings for dnsaddr detection #259

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ const DNS_CODES = [
getProtocol('dnsaddr').code
]

const P2P_CODES = [
getProtocol('p2p').code,
getProtocol('ipfs').code
]

const TCP_UDP_CODES = [
getProtocol('tcp').code,
getProtocol('udp').code
]

export interface Protocol {
code: number
size: number
Expand Down Expand Up @@ -459,20 +469,20 @@ export class Multiaddr {
const codes = this.protoCodes()
const names = this.protoNames()
const parts = this.toString().split('/').slice(1)
let protocol = parts[2]
let protocol = getProtocol(parts[2]).code
let port = parseInt(parts[3])

// default to https when protocol & port are omitted from DNS addrs
if (DNS_CODES.includes(codes[0]) && protocol === 'p2p') {
protocol = 'tcp'
if (DNS_CODES.includes(codes[0]) && P2P_CODES.includes(codes[1])) {
protocol = getProtocol('tcp').code
port = 443
}

if (parts.length < 4) {
throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6, dnsaddr}/{address}/{tcp, udp}/{port}".')
} else if (!IP_CODES.includes(codes[0]) && !DNS_CODES.includes(codes[0])) {
throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns, dns4, dns6, dnsaddr}".`)
} else if (protocol !== 'tcp' && protocol !== 'udp') {
} else if (!TCP_UDP_CODES.includes(protocol)) {
throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`)
}

Expand Down