Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to new multiformats #42

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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