Skip to content

Commit c66bb64

Browse files
committed
feat: CID Support
Closes #12
1 parent c1e342d commit c66bb64

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
"author": "Francisco Dias <francisco@baiodias.com> (http://franciscodias.net/)",
2929
"license": "MIT",
3030
"dependencies": {
31-
"bs58": "^4.0.0",
32-
"multihashes": "^0.3.2"
31+
"cids": "^0.5.1",
32+
"bs58": "^4.0.1",
33+
"multihashes": "^0.4.9"
3334
},
3435
"devDependencies": {
3536
"aegir": "^9.4.0",
@@ -50,4 +51,4 @@
5051
"Marcin Rataj <lidel@lidel.org>",
5152
"nginnever <ginneversource@gmail.com>"
5253
]
53-
}
54+
}

src/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const base58 = require('bs58')
44
const multihash = require('multihashes')
5+
const CID = require('cids')
56

67
const urlPattern = /^https?:\/\/[^/]+\/(ip(f|n)s)\/((\w+).*)/
78
const pathPattern = /^\/(ip(f|n)s)\/((\w+).*)/
@@ -17,6 +18,14 @@ function isMultihash (hash) {
1718
}
1819
}
1920

21+
function isCID (hash) {
22+
try {
23+
return CID.isCID(new CID(hash))
24+
} catch (e) {
25+
return false
26+
}
27+
}
28+
2029
function isIpfs (input, pattern) {
2130
const formatted = convertToString(input)
2231
if (!formatted) {
@@ -33,7 +42,7 @@ function isIpfs (input, pattern) {
3342
}
3443

3544
const hash = match[4]
36-
return isMultihash(hash)
45+
return isCID(hash)
3746
}
3847

3948
function isIpns (input, pattern) {
@@ -67,6 +76,7 @@ function convertToString (input) {
6776

6877
module.exports = {
6978
multihash: isMultihash,
79+
cid: isCID,
7080
ipfsUrl: (url) => isIpfs(url, urlPattern),
7181
ipnsUrl: (url) => isIpns(url, urlPattern),
7282
url: (url) => (isIpfs(url, urlPattern) || isIpns(url, urlPattern)),

test/test-cid.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const base58 = require('bs58')
5+
const expect = require('chai').expect
6+
const isIPFS = require('../src/index')
7+
8+
describe('ipfs cid', () => {
9+
it('isIPFS.cid should match a valid CIDv0 (multihash)', (done) => {
10+
const actual = isIPFS.cid('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')
11+
expect(actual).to.equal(true)
12+
done()
13+
})
14+
15+
it('isIPFS.cid should match a valid CIDv0 (multihash) buffer', (done) => {
16+
const actual = isIPFS.cid(new Buffer(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
17+
expect(actual).to.equal(true)
18+
done()
19+
})
20+
21+
it('isIPFS.cid should not match a broken CIDv0 buffer', (done) => {
22+
const actual = isIPFS.cid(new Buffer('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70'))
23+
expect(actual).to.equal(false)
24+
done()
25+
})
26+
27+
it('isIPFS.cid should not match an invalid CIDv0 (multihash with a typo)', (done) => {
28+
const actual = isIPFS.cid('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70')
29+
expect(actual).to.equal(false)
30+
done()
31+
})
32+
33+
it('isIPFS.cid should match a valid CIDv1', (done) => {
34+
const actual = isIPFS.cid('zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ7')
35+
expect(actual).to.equal(true)
36+
done()
37+
})
38+
39+
it('isIPFS.cid should not match an invalid CIDv1 (with a typo)', (done) => {
40+
const actual = isIPFS.cid('zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ')
41+
expect(actual).to.equal(false)
42+
done()
43+
})
44+
45+
it('isIPFS.cid should not match an invalid CID', (done) => {
46+
const actual = isIPFS.cid('noop')
47+
expect(actual).to.equal(false)
48+
done()
49+
})
50+
51+
it('isIPFS.cid should not match an invalid CID data type', (done) => {
52+
const actual = isIPFS.cid(4)
53+
expect(actual).to.equal(false)
54+
done()
55+
})
56+
})

0 commit comments

Comments
 (0)