Skip to content

Commit

Permalink
chore: update to new multiformats
Browse files Browse the repository at this point in the history
Depends on:

- [ ] multiformats/js-multiformats#91

BREAKING CHANGE: uses the CID class from the new multiformats module
  • Loading branch information
achingbrain committed Jun 2, 2021
1 parent f7fe63c commit 374cac4
Show file tree
Hide file tree
Showing 3 changed files with 23 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": "multiformats/js-multiaddr#chore/update-to-new-multiformats",
"multiformats": "^9.0.0",
"uint8arrays": "^2.1.3"
},
"devDependencies": {
Expand Down
31 changes: 19 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const multihash = require('multihashes')
const multibase = require('multibase')
const { base58btc } = require('multiformats/bases/base58')
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,7 +27,7 @@ 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))
Digest.decode(base58btc.decode('z' + formatted))
return true
} catch (e) {
return false
Expand All @@ -37,21 +37,28 @@ function isMultihash (hash) {
/**
* @param {*} hash
*/
function isMultibase (hash) {
try {
return multibase.isEncoded(hash)
} catch (e) {
return false
function isBase32EncodedMultihash (hash) {
if (typeof hash === 'string') {
return hash[0] === 'b'
}

return false
}

/**
* @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 +229,7 @@ module.exports = {
/**
* @param {CID | string | Uint8Array} cid
*/
base32cid: (cid) => (isMultibase(cid) === 'base32' && isCID(cid)),
base32cid: (cid) => (isBase32EncodedMultihash(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 374cac4

Please sign in to comment.