Skip to content

Commit f193d46

Browse files
committed
feat: Add support for multiaddr node with dns
1 parent 1291a2f commit f193d46

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

src/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const Multiaddr = withIs.proto(function (addr) {
3737
*/
3838
this.buffer = codec.fromBuffer(addr)
3939
} else if (typeof addr === 'string' || addr instanceof String) {
40+
if (addr.length > 0 && addr.charAt(0) !== '/') {
41+
throw new Error(`multiaddr "${addr}" must start with a "/"`)
42+
}
4043
this.buffer = codec.fromString(addr)
4144
} else if (addr.buffer && addr.protos && addr.protoCodes) { // Multiaddr
4245
this.buffer = codec.fromBuffer(addr.buffer) // validate + copy buffer
@@ -297,14 +300,20 @@ Multiaddr.prototype.equals = function equals (addr) {
297300
* // {family: 'IPv4', address: '127.0.0.1', port: '4001'}
298301
*/
299302
Multiaddr.prototype.nodeAddress = function nodeAddress () {
300-
if (!this.isThinWaistAddress()) {
301-
throw new Error('Multiaddr must be "thin waist" address for nodeAddress.')
302-
}
303-
304303
const codes = this.protoCodes()
304+
const names = this.protoNames()
305305
const parts = this.toString().split('/').slice(1)
306+
307+
if (parts.length < 4) {
308+
throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6}/{address}/{tcp, udp}/{port}".')
309+
} else if (codes[0] !== 4 && codes[0] !== 41 && codes[0] !== 54 && codes[0] !== 55) {
310+
throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns4, dns6}".`)
311+
} else if (parts[2] !== 'tcp' && parts[2] !== 'udp') {
312+
throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`)
313+
}
314+
306315
return {
307-
family: (codes[0] === 41) ? 'IPv6' : 'IPv4',
316+
family: (codes[0] === 41 || codes[0] === 55) ? 6 : 4,
308317
address: parts[1], // ip addr
309318
port: parts[3] // tcp or udp port
310319
}

test/index.spec.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ describe('helpers', () => {
443443
})
444444

445445
describe('.nodeAddress', () => {
446-
it('throws on non thinWaistAddress', () => {
446+
it('throws on an invalid node address', () => {
447447
expect(
448448
() => multiaddr('/ip4/192.168.0.1/utp').nodeAddress()
449449
).to.throw(
450-
/thin waist/
450+
/multiaddr must have a valid format/
451451
)
452452
})
453453

@@ -456,10 +456,44 @@ describe('helpers', () => {
456456
multiaddr('/ip4/192.168.0.1/tcp/1234').nodeAddress()
457457
).to.be.eql({
458458
address: '192.168.0.1',
459-
family: 'IPv4',
459+
family: 4,
460460
port: '1234'
461461
})
462462
})
463+
464+
it('returns a node friendly address with dns', () => {
465+
expect(
466+
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress()
467+
).to.be.eql({
468+
address: 'wss0.bootstrap.libp2p.io',
469+
family: 4,
470+
port: '443'
471+
})
472+
})
473+
474+
it('throws on an invalid format address when the addr is not prefixed with a /', () => {
475+
expect(
476+
() => multiaddr('ip4/192.168.0.1/udp').nodeAddress()
477+
).to.throw(
478+
/must start with a/
479+
)
480+
})
481+
482+
it('throws on an invalid protocol name when the addr has an invalid one', () => {
483+
expect(
484+
() => multiaddr('/ip5/127.0.0.1/udp/5000')
485+
).to.throw(
486+
/no protocol with name/
487+
)
488+
})
489+
490+
it('throws on an invalid protocol name when the transport protocol is not valid', () => {
491+
expect(
492+
() => multiaddr('/ip4/127.0.0.1/utp/5000')
493+
).to.throw(
494+
/no protocol with name/
495+
)
496+
})
463497
})
464498

465499
describe('.fromNodeAddress', () => {
@@ -501,10 +535,10 @@ describe('helpers', () => {
501535
}
502536
families.forEach((family) => {
503537
transports.forEach((transport) => {
504-
it(`returns true for ${family}-${transport}`, () => {
538+
it(`returns true for /${family}-${transport}`, () => {
505539
expect(
506540
multiaddr(
507-
`${family}/${addresses[family]}/${transport}/1234`
541+
`/${family}/${addresses[family]}/${transport}/1234`
508542
).isThinWaistAddress()
509543
).to.equal(true)
510544
})

0 commit comments

Comments
 (0)