Skip to content

Commit 820d475

Browse files
committed
feat: isIPFS.multiaddr(input)
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 <lidel@lidel.org>
1 parent 9fe8a59 commit 820d475

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ isIPFS.ipnsSubdomain('http://bafybeiabc2xofh6tdi6vutusorpumwcikw3hf3st4ecjugo6j5
9696
isIPFS.ipnsSubdomain('http://bafybeiabc2xofh6tdi6vutusorpumwcikw3hf3st4ecjugo6j52f6xwc6q.dweb.link') // false
9797
isIPFS.ipnsSubdomain('http://QmcNioXSC1bfJj1dcFErhUfyjFzoX2HodkRccsFFVJJvg8.ipns.dweb.link') // false
9898
isIPFS.ipnsSubdomain('http://foo-bar.ipns.dweb.link') // false (not a PeerID)
99+
100+
isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234') // true
101+
isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234/http') // true
102+
isIPFS.multiaddr('/ip6/::1/udp/1234') // true
103+
isIPFS.multiaddr('ip6/::1/udp/1234') // false
104+
isIPFS.multiaddr('/yoloinvalid/::1/udp/1234') // false
99105
```
100106

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

116122
### `isIPFS.cid(hash)`
117123

118-
Returns `true` if the provided string is a valid `CID` or `false` otherwise.
124+
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.
119125

120126
### `isIPFS.base32cid(hash)`
121127

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

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

181+
## Multiaddrs
182+
183+
### `isIPFS.multiaddr(addr)`
184+
185+
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.
175186

176187
# License
177188

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dependencies": {
3333
"bs58": "4.0.1",
3434
"cids": "~0.5.6",
35+
"multiaddr": "6.0.4",
3536
"multibase": "~0.6.0",
3637
"multihashes": "~0.4.13"
3738
},

src/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const base58 = require('bs58')
44
const multihash = require('multihashes')
55
const multibase = require('multibase')
6+
const Multiaddr = require('multiaddr')
67
const CID = require('cids')
78

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

46+
function isMultiaddr (input) {
47+
if (!input) return false
48+
if (isString(input) || input instanceof Buffer) {
49+
try {
50+
new Multiaddr(input) // eslint-disable-line no-new
51+
return true
52+
} catch (e) {
53+
return false
54+
}
55+
}
56+
if (Multiaddr.isMultiaddr(input)) return true
57+
return false
58+
}
59+
4560
function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch = defaultHashMath) {
4661
const formatted = convertToString(input)
4762
if (!formatted) {
@@ -116,6 +131,7 @@ const ipnsSubdomain = (url) => isIpns(url, fqdnPattern, fqdnProtocolMatch, fqdnH
116131

117132
module.exports = {
118133
multihash: isMultihash,
134+
multiaddr: isMultiaddr,
119135
cid: isCID,
120136
base32cid: (cid) => (isMultibase(cid) === 'base32' && isCID(cid)),
121137
ipfsSubdomain: ipfsSubdomain,

test/test-cid.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
const base58 = require('bs58')
55
const expect = require('chai').expect
66
const isIPFS = require('../src/index')
7+
const CID = require('cids')
78

89
describe('ipfs cid', () => {
10+
it('isIPFS.cid should match a valid CID instance', (done) => {
11+
const cid = new CID('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')
12+
const actual = isIPFS.cid(cid)
13+
expect(actual).to.equal(true)
14+
done()
15+
})
16+
917
it('isIPFS.cid should match a valid CIDv0 (multihash)', (done) => {
1018
const actual = isIPFS.cid('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')
1119
expect(actual).to.equal(true)

test/test-multiaddr.spec.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const Multiaddr = require('multiaddr')
6+
const isIPFS = require('../src/index')
7+
8+
describe('ipfs multiaddr', () => {
9+
it('isIPFS.multiaddr should match a string with valid ip4 multiaddr', (done) => {
10+
const actual = isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234/http')
11+
expect(actual).to.equal(true)
12+
done()
13+
})
14+
15+
it('isIPFS.multiaddr should match a string with valid ip6 multiaddr', (done) => {
16+
const actual = isIPFS.multiaddr('/ip6/::1/udp/1234/http')
17+
expect(actual).to.equal(true)
18+
done()
19+
})
20+
21+
it('isIPFS.multiaddr should match a valid Multiaddr instance', (done) => {
22+
const ma = new Multiaddr('/ip6/::1/udp/1234/http')
23+
const actual = isIPFS.multiaddr(ma)
24+
expect(actual).to.equal(true)
25+
done()
26+
})
27+
28+
it('isIPFS.multiaddr should match a Buffer with multiaddr', (done) => {
29+
const ma = new Multiaddr('/ip6/::1/udp/1234/http')
30+
const actual = isIPFS.multiaddr(Buffer.from(ma.buffer))
31+
expect(actual).to.equal(true)
32+
done()
33+
})
34+
35+
it('isIPFS.multiaddr should not match random Buffer', (done) => {
36+
const actual = isIPFS.multiaddr(Buffer.from('randombuffer'))
37+
expect(actual).to.equal(false)
38+
done()
39+
})
40+
41+
it('isIPFS.multiaddr should not match an invalid multiaddr (no initial slash)', (done) => {
42+
const actual = isIPFS.multiaddr('ip4/127.0.0.1/udp/1234/http')
43+
expect(actual).to.equal(false)
44+
done()
45+
})
46+
47+
it('isIPFS.multiaddr should not match an invalid multiaddr (unknown namespace)', (done) => {
48+
const actual = isIPFS.multiaddr('/yoloinvalid/127.0.0.1/udp/1234/http')
49+
expect(actual).to.equal(false)
50+
done()
51+
})
52+
53+
it('isIPFS.multiaddr should not match an invalid multiaddr', (done) => {
54+
const actual = isIPFS.multiaddr('noop')
55+
expect(actual).to.equal(false)
56+
done()
57+
})
58+
59+
it('isIPFS.multiaddr should not match an invalid multiaddr data type', (done) => {
60+
const actual = isIPFS.multiaddr(4)
61+
expect(actual).to.equal(false)
62+
done()
63+
})
64+
})

0 commit comments

Comments
 (0)