Skip to content

Commit

Permalink
feat: isIPFS.multiaddr(input)
Browse files Browse the repository at this point in the history
This adds `multiaddr` check if input is a valid multiaddr.
A separate check for IPFS peer multiaddr will be added in next commit.

License: MIT
Signed-off-by: Marcin Rataj <[email protected]>
  • Loading branch information
lidel committed Feb 24, 2019
1 parent 9fe8a59 commit 820d475
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ isIPFS.ipnsSubdomain('http://bafybeiabc2xofh6tdi6vutusorpumwcikw3hf3st4ecjugo6j5
isIPFS.ipnsSubdomain('http://bafybeiabc2xofh6tdi6vutusorpumwcikw3hf3st4ecjugo6j52f6xwc6q.dweb.link') // false
isIPFS.ipnsSubdomain('http://QmcNioXSC1bfJj1dcFErhUfyjFzoX2HodkRccsFFVJJvg8.ipns.dweb.link') // false
isIPFS.ipnsSubdomain('http://foo-bar.ipns.dweb.link') // false (not a PeerID)

isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234') // true
isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234/http') // true
isIPFS.multiaddr('/ip6/::1/udp/1234') // true
isIPFS.multiaddr('ip6/::1/udp/1234') // false
isIPFS.multiaddr('/yoloinvalid/::1/udp/1234') // false
```

# API
Expand All @@ -115,7 +121,7 @@ Returns `true` if the provided string is a valid `multihash` or `false` otherwis

### `isIPFS.cid(hash)`

Returns `true` if the provided string is a valid `CID` or `false` otherwise.
Returns `true` if the provided string or [`CID`](https://github.com/ipld/js-cid) is a valid [CID](https://docs.ipfs.io/guides/concepts/cid/) or `false` otherwise.

### `isIPFS.base32cid(hash)`

Expand Down Expand Up @@ -172,6 +178,11 @@ Returns `true` if the provided string includes a valid IPFS subdomain or `false`

Returns `true` if the provided string includes a valid IPNS subdomain or `false` otherwise.

## Multiaddrs

### `isIPFS.multiaddr(addr)`

Returns `true` if the provided `string`, [`Multiaddr`](https://github.com/multiformats/js-multiaddr) or `Buffer` includes a valid [multiaddr](https://multiformats.io/multiaddr/) or `false` otherwise.

# License

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"bs58": "4.0.1",
"cids": "~0.5.6",
"multiaddr": "6.0.4",
"multibase": "~0.6.0",
"multihashes": "~0.4.13"
},
Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const base58 = require('bs58')
const multihash = require('multihashes')
const multibase = require('multibase')
const Multiaddr = require('multiaddr')
const CID = require('cids')

const urlPattern = /^https?:\/\/[^/]+\/(ip(f|n)s)\/((\w+).*)/
Expand Down Expand Up @@ -42,6 +43,20 @@ function isCID (hash) {
}
}

function isMultiaddr (input) {
if (!input) return false
if (isString(input) || input instanceof Buffer) {
try {
new Multiaddr(input) // eslint-disable-line no-new
return true
} catch (e) {
return false
}
}
if (Multiaddr.isMultiaddr(input)) return true
return false
}

function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch = defaultHashMath) {
const formatted = convertToString(input)
if (!formatted) {
Expand Down Expand Up @@ -116,6 +131,7 @@ const ipnsSubdomain = (url) => isIpns(url, fqdnPattern, fqdnProtocolMatch, fqdnH

module.exports = {
multihash: isMultihash,
multiaddr: isMultiaddr,
cid: isCID,
base32cid: (cid) => (isMultibase(cid) === 'base32' && isCID(cid)),
ipfsSubdomain: ipfsSubdomain,
Expand Down
8 changes: 8 additions & 0 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
const base58 = require('bs58')
const expect = require('chai').expect
const isIPFS = require('../src/index')
const CID = require('cids')

describe('ipfs cid', () => {
it('isIPFS.cid should match a valid CID instance', (done) => {
const cid = new CID('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')
const actual = isIPFS.cid(cid)
expect(actual).to.equal(true)
done()
})

it('isIPFS.cid should match a valid CIDv0 (multihash)', (done) => {
const actual = isIPFS.cid('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')
expect(actual).to.equal(true)
Expand Down
64 changes: 64 additions & 0 deletions test/test-multiaddr.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const Multiaddr = require('multiaddr')
const isIPFS = require('../src/index')

describe('ipfs multiaddr', () => {
it('isIPFS.multiaddr should match a string with valid ip4 multiaddr', (done) => {
const actual = isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234/http')
expect(actual).to.equal(true)
done()
})

it('isIPFS.multiaddr should match a string with valid ip6 multiaddr', (done) => {
const actual = isIPFS.multiaddr('/ip6/::1/udp/1234/http')
expect(actual).to.equal(true)
done()
})

it('isIPFS.multiaddr should match a valid Multiaddr instance', (done) => {
const ma = new Multiaddr('/ip6/::1/udp/1234/http')
const actual = isIPFS.multiaddr(ma)
expect(actual).to.equal(true)
done()
})

it('isIPFS.multiaddr should match a Buffer with multiaddr', (done) => {
const ma = new Multiaddr('/ip6/::1/udp/1234/http')
const actual = isIPFS.multiaddr(Buffer.from(ma.buffer))
expect(actual).to.equal(true)
done()
})

it('isIPFS.multiaddr should not match random Buffer', (done) => {
const actual = isIPFS.multiaddr(Buffer.from('randombuffer'))
expect(actual).to.equal(false)
done()
})

it('isIPFS.multiaddr should not match an invalid multiaddr (no initial slash)', (done) => {
const actual = isIPFS.multiaddr('ip4/127.0.0.1/udp/1234/http')
expect(actual).to.equal(false)
done()
})

it('isIPFS.multiaddr should not match an invalid multiaddr (unknown namespace)', (done) => {
const actual = isIPFS.multiaddr('/yoloinvalid/127.0.0.1/udp/1234/http')
expect(actual).to.equal(false)
done()
})

it('isIPFS.multiaddr should not match an invalid multiaddr', (done) => {
const actual = isIPFS.multiaddr('noop')
expect(actual).to.equal(false)
done()
})

it('isIPFS.multiaddr should not match an invalid multiaddr data type', (done) => {
const actual = isIPFS.multiaddr(4)
expect(actual).to.equal(false)
done()
})
})

0 comments on commit 820d475

Please sign in to comment.