Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

feat: allow array-like objects as input #78

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,22 @@ class CID {
return
}

if (Buffer.isBuffer(version)) {
const firstByte = version.slice(0, 1)
const v = parseInt(firstByte.toString('hex'), 16)
// It is an array-like object, e.g. Arraym, Buffer or TypedArray
if (version.length) {
// The first byte is the actual version
const v = version[0]
if (v === 0 || v === 1) {
// version is a CID buffer
const cid = version
this.version = v
this.codec = multicodec.getCodec(cid.slice(1))
this.multihash = multicodec.rmPrefix(cid.slice(1))
this.multihash = Buffer.from(multicodec.rmPrefix(cid.slice(1)))
this.multibaseName = (v === 0) ? 'base58btc' : multibaseName
} else {
// version is a raw multihash buffer, so v0
this.version = 0
this.codec = 'dag-pb'
this.multihash = version
this.multihash = Buffer.from(version)
this.multibaseName = 'base58btc'
}
CID.validateCID(this)
Expand Down
62 changes: 62 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,40 @@ describe('CID', () => {
})
})

it('handles Uint8Array multihash', (done) => {
multihashing(Buffer.from('hello world'), 'sha2-256', (err, mh) => {
expect(err).to.not.exist()
const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
const mhUint8 = Uint8Array.from(mh)

const cid = new CID(mhUint8)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash').that.eql(mh)

expect(cid.toBaseEncodedString()).to.eql(mhStr)
done()
})
})

it('handles Array multihash', (done) => {
multihashing(Buffer.from('hello world'), 'sha2-256', (err, mh) => {
expect(err).to.not.exist()
const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
const mhArray = Uint8Array.from(mh)

const cid = new CID(mhArray)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash').that.eql(mh)

expect(cid.toBaseEncodedString()).to.eql(mhStr)
done()
})
})

it('create by parts', () => {
const cid = new CID(0, 'dag-pb', hash)

Expand Down Expand Up @@ -125,6 +159,34 @@ describe('CID', () => {
expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
})

it('handles CID by Uint8Array', () => {
const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
const cidBuf = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')
const cidUint8 = Uint8Array.from(cidBuf)

const cid = new CID(cidUint8)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')

expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
})

it('handles CID by Array', () => {
const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
const cidBuf = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')
const cidArray = Array.from(cidBuf)

const cid = new CID(cidArray)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')

expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
})

it('create by parts', () => {
const cid = new CID(1, 'dag-cbor', hash)

Expand Down