Skip to content

Commit

Permalink
feat: CID Support
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Sep 10, 2017
1 parent c1e342d commit c66bb64
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
"author": "Francisco Dias <francisco@baiodias.com> (http://franciscodias.net/)",
"license": "MIT",
"dependencies": {
"bs58": "^4.0.0",
"multihashes": "^0.3.2"
"cids": "^0.5.1",
"bs58": "^4.0.1",
"multihashes": "^0.4.9"
},
"devDependencies": {
"aegir": "^9.4.0",
Expand All @@ -50,4 +51,4 @@
"Marcin Rataj <lidel@lidel.org>",
"nginnever <ginneversource@gmail.com>"
]
}
}
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const base58 = require('bs58')
const multihash = require('multihashes')
const CID = require('cids')

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

function isCID (hash) {
try {
return CID.isCID(new CID(hash))
} catch (e) {
return false
}
}

function isIpfs (input, pattern) {
const formatted = convertToString(input)
if (!formatted) {
Expand All @@ -33,7 +42,7 @@ function isIpfs (input, pattern) {
}

const hash = match[4]
return isMultihash(hash)
return isCID(hash)
}

function isIpns (input, pattern) {
Expand Down Expand Up @@ -67,6 +76,7 @@ function convertToString (input) {

module.exports = {
multihash: isMultihash,
cid: isCID,
ipfsUrl: (url) => isIpfs(url, urlPattern),
ipnsUrl: (url) => isIpns(url, urlPattern),
url: (url) => (isIpfs(url, urlPattern) || isIpns(url, urlPattern)),
Expand Down
56 changes: 56 additions & 0 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-env mocha */
'use strict'

const base58 = require('bs58')
const expect = require('chai').expect
const isIPFS = require('../src/index')

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

it('isIPFS.cid should match a valid CIDv0 (multihash) buffer', (done) => {
const actual = isIPFS.cid(new Buffer(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
expect(actual).to.equal(true)
done()
})

it('isIPFS.cid should not match a broken CIDv0 buffer', (done) => {
const actual = isIPFS.cid(new Buffer('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70'))
expect(actual).to.equal(false)
done()
})

it('isIPFS.cid should not match an invalid CIDv0 (multihash with a typo)', (done) => {
const actual = isIPFS.cid('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70')
expect(actual).to.equal(false)
done()
})

it('isIPFS.cid should match a valid CIDv1', (done) => {
const actual = isIPFS.cid('zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ7')
expect(actual).to.equal(true)
done()
})

it('isIPFS.cid should not match an invalid CIDv1 (with a typo)', (done) => {
const actual = isIPFS.cid('zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ')
expect(actual).to.equal(false)
done()
})

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

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

0 comments on commit c66bb64

Please sign in to comment.