Skip to content

Commit

Permalink
chore: update to new multiformats (#42)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses the CID class from the new multiformats module

Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
achingbrain and lidel authored Jul 7, 2021
1 parent f7fe63c commit 22bf9b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@
"release-major": "aegir release --type major"
},
"dependencies": {
"cids": "^1.1.6",
"iso-url": "^1.1.3",
"mafmt": "^9.0.0",
"multiaddr": "^9.0.1",
"multibase": "^4.0.2",
"multihashes": "^4.0.2",
"multiaddr": "^10.0.0",
"multiformats": "^9.0.0",
"uint8arrays": "^2.1.3"
},
"devDependencies": {
Expand Down
35 changes: 23 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'

const multihash = require('multihashes')
const multibase = require('multibase')
const { base58btc } = require('multiformats/bases/base58')
const { base32 } = require('multiformats/bases/base32')
const Digest = require('multiformats/hashes/digest')
const { Multiaddr } = require('multiaddr')
const mafmt = require('mafmt')
const CID = require('cids')
const { CID } = require('multiformats/cid')
const { URL } = require('iso-url')
const uint8ArrayToString = require('uint8arrays/to-string')

Expand All @@ -27,31 +28,41 @@ const fqdnWithTld = /^(([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\.)+([a-z0-9]|[a-z0-
function isMultihash (hash) {
const formatted = convertToString(hash)
try {
multihash.decode(multibase.decode('z' + formatted))
return true
} catch (e) {
Digest.decode(base58btc.decode('z' + formatted))
} catch {
return false
}

return true
}

/**
* @param {*} hash
*/
function isMultibase (hash) {
function isBase32EncodedMultibase (hash) {
try {
return multibase.isEncoded(hash)
} catch (e) {
base32.decode(hash)
} catch {
return false
}

return true
}

/**
* @param {*} hash
*/
function isCID (hash) {
try {
new CID(hash) // eslint-disable-line no-new
return true
if (typeof hash === 'string') {
return Boolean(CID.parse(hash))
}

if (hash instanceof Uint8Array) {
return Boolean(CID.decode(hash))
}

return Boolean(CID.asCID(hash)) // eslint-disable-line no-new
} catch (e) {
return false
}
Expand Down Expand Up @@ -222,7 +233,7 @@ module.exports = {
/**
* @param {CID | string | Uint8Array} cid
*/
base32cid: (cid) => (isMultibase(cid) === 'base32' && isCID(cid)),
base32cid: (cid) => (isBase32EncodedMultibase(cid) && isCID(cid)),
ipfsSubdomain,
ipnsSubdomain,
subdomain,
Expand Down
4 changes: 2 additions & 2 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

const { expect } = require('aegir/utils/chai')
const isIPFS = require('../src/index')
const CID = require('cids')
const { CID } = require('multiformats/cid')
const uint8ArrayFromString = require('uint8arrays/from-string')

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

0 comments on commit 22bf9b7

Please sign in to comment.