diff --git a/package.json b/package.json index fa9687b..62cd997 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "npm": ">=3.0.0" }, "devDependencies": { - "aegir": "^20.0.0", + "aegir": "^20.3.1", "chai": "^4.2.0", "dirty-chai": "^2.0.1", "interface-transport": "^0.7.0", @@ -44,7 +44,7 @@ "class-is": "^1.1.0", "debug": "^4.1.1", "err-code": "^2.0.0", - "ip-address": "^6.1.0", + "libp2p-utils": "~0.1.0", "mafmt": "^7.0.0", "multiaddr": "^7.1.0", "stream-to-it": "^0.1.1" diff --git a/src/ip-port-to-multiaddr.js b/src/ip-port-to-multiaddr.js deleted file mode 100644 index 6d980c4..0000000 --- a/src/ip-port-to-multiaddr.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const multiaddr = require('multiaddr') -const { Address4, Address6 } = require('ip-address') - -module.exports = (ip, port) => { - if (typeof ip !== 'string') { - throw new Error('invalid ip') - } - - port = parseInt(port) - - if (isNaN(port)) { - throw new Error('invalid port') - } - - if (new Address4(ip).isValid()) { - return multiaddr(`/ip4/${ip}/tcp/${port}`) - } - - const ip6 = new Address6(ip) - - if (ip6.isValid()) { - return ip6.is4() - ? multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`) - : multiaddr(`/ip6/${ip}/tcp/${port}`) - } - - throw new Error('invalid ip') -} diff --git a/src/socket-to-conn.js b/src/socket-to-conn.js index c1dccaf..2cdab2d 100644 --- a/src/socket-to-conn.js +++ b/src/socket-to-conn.js @@ -3,7 +3,7 @@ const abortable = require('abortable-iterator') const log = require('debug')('libp2p:tcp:socket') const toIterable = require('stream-to-it') -const toMultiaddr = require('./ip-port-to-multiaddr') +const toMultiaddr = require('libp2p-utils/src/ip-port-to-multiaddr') const { CLOSE_TIMEOUT } = require('./constants') // Convert a socket into a MultiaddrConnection diff --git a/test/ip-port-to-multiaddr.spec.js b/test/ip-port-to-multiaddr.spec.js deleted file mode 100644 index 657e569..0000000 --- a/test/ip-port-to-multiaddr.spec.js +++ /dev/null @@ -1,50 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) -const toMultiaddr = require('../src/ip-port-to-multiaddr') - -describe('IP and port to Multiaddr', () => { - it('creates multiaddr from valid IPv4 IP and port', () => { - const ip = '127.0.0.1' - const port = '9090' - expect(toMultiaddr(ip, port).toString()).to.equal(`/ip4/${ip}/tcp/${port}`) - }) - - it('creates multiaddr from valid IPv4 IP and numeric port', () => { - const ip = '127.0.0.1' - const port = 9090 - expect(toMultiaddr(ip, port).toString()).to.equal(`/ip4/${ip}/tcp/${port}`) - }) - - it('creates multiaddr from valid IPv4 in IPv6 IP and port', () => { - const ip = '0:0:0:0:0:0:101.45.75.219' - const port = '9090' - expect(toMultiaddr(ip, port).toString()).to.equal(`/ip4/101.45.75.219/tcp/${port}`) - }) - - it('creates multiaddr from valid IPv6 IP and port', () => { - const ip = '::1' - const port = '9090' - expect(toMultiaddr(ip, port).toString()).to.equal(`/ip6/${ip}/tcp/${port}`) - }) - - it('throws for missing IP address', () => { - expect(() => toMultiaddr()).to.throw('invalid ip') - }) - - it('throws for invalid IP address', () => { - const ip = 'aewmrn4awoew' - const port = '234' - expect(() => toMultiaddr(ip, port)).to.throw('invalid ip') - }) - - it('throws for invalid port', () => { - const ip = '127.0.0.1' - const port = 'garbage' - expect(() => toMultiaddr(ip, port)).to.throw('invalid port') - }) -})