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

Feature complete #4

Merged
merged 1 commit into from
Oct 24, 2016
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
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ const cid = new CID(base58Multihash)

## API

### `new CID(codec[, version, hash])`
### Constructor

- `new CID(<version>, <codec>, <multihash>)`
- `new CID(<cidStr>)`
- `new CID(<cid.buffer>)`
- `new CID(<multihash>)`
- `new CID(<bs58 encoded multihash>)`

### `.codec`

Expand All @@ -85,7 +91,13 @@ const cid = new CID(base58Multihash)

### `.buffer`

### `.toString()`
### `.toV0()`

### `.toV1()`

### `.toBaseEncodedString(base)`

Defaults to 'base58btc'

### `.toJSON()`

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"multibase": "^0.2.0",
"multicodec": "0.1.0",
"multihashes": "^0.2.2"
},
"devDependencies": {
Expand All @@ -50,4 +51,4 @@
"contributors": [
"Friedel Ziegelmayer <dignifiedquire@gmail.com>"
]
}
}
75 changes: 0 additions & 75 deletions src/cid.js

This file was deleted.

13 changes: 9 additions & 4 deletions src/codecs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict'

/*
* Consult table at: https://github.com/multiformats/multicodec
*/

module.exports = {
protobuf: 0,
raw: 1,
json: 2,
cbor: 3
raw: new Buffer('00', 'hex'),
'dag-pb': new Buffer('70', 'hex'),
'dag-cbor': new Buffer('71', 'hex'),
'eth-block': new Buffer('90', 'hex'),
'eth-tx': new Buffer('91', 'hex')
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if we could just pull this directly off of js-multicodec

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Or even a multicodec-table module that gets published directly from the spec repo, this way, all the other multi* can require it and use always the latest.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you plan on doing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have multiformats/multicodec#16 finished, otherwise, instead of having codes floating in two places, we would have it floating in three

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note here: multiformats/multicodec#20

117 changes: 116 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,120 @@
'use strict'

const CID = require('./cid')
const mh = require('multihashes')
const multibase = require('multibase')
const multicodec = require('multicodec')

const codecs = require('./codecs')

// CID: <mbase><version><mcodec><mhash>

class CID {
/*
* if (str)
* if (1st char is on multibase table) -> CID String
* else -> bs58 encoded multihash
* else if (Buffer)
* if (0 or 1) -> CID
* else -> multihash
* else if (Number)
* -> construct CID by parts
*
* ..if only JS had traits..
*/
constructor (version, codec, multihash) {
if (typeof version === 'string') {
if (multibase.isEncoded(version)) { // CID String (encoded with multibase)
const cid = multibase.decode(version)
this.version = parseInt(cid.slice(0, 1).toString('hex'), 16)
this.codec = multicodec.getCodec(cid.slice(1))
this.multihash = multicodec.rmPrefix(cid.slice(1))
} else { // bs58 string encoded multihash
this.codec = 'dag-pb'
this.multihash = mh.fromB58String(version)
this.version = 0
}
} else if (Buffer.isBuffer(version)) {
const firstByte = version.slice(0, 1)
const v = parseInt(firstByte.toString('hex'), 16)
if (v === 0 || v === 1) { // CID
const cid = version
this.version = v
this.codec = multicodec.getCodec(cid.slice(1))
this.multihash = multicodec.rmPrefix(cid.slice(1))
} else { // multihash
this.codec = 'dag-pb'
this.multihash = version
this.version = 0
}
} else if (typeof version === 'number') {
if (typeof codec !== 'string') {
throw new Error('codec must be string')
}
if (!(version === 0 || version === 1)) {
throw new Error('version must be a number equal to 0 or 1')
}
mh.validate(multihash)
this.codec = codec
this.version = version
this.multihash = multihash
}
}

get buffer () {
switch (this.version) {
case 0:
return this.multihash
case 1:
return Buffer.concat([
Buffer('01', 'hex'),
Buffer(codecs[this.codec]),
this.multihash
])
default:
throw new Error('unsupported version')
}
}

toV0 () {
return this.multihash
}

toV1 () {
return this.buffer
}

/* defaults to base58btc */
toBaseEncodedString (base) {
base = base || 'base58btc'

switch (this.version) {
case 0:
return mh.toB58String(this.multihash)
case 1:
return multibase.encode(base, this.buffer).toString()
default:
throw new Error('Unsupported version')
}
}

toJSON () {
return {
codec: this.codec,
version: this.version,
hash: this.multihash
}
}

equals (other) {
return this.codec === other.codec &&
this.version === other.version &&
this.multihash.equals(other.multihash)
}
}

CID.codecs = codecs
CID.isCID = (other) => {
return other.constructor.name === 'CID'
}

module.exports = CID
21 changes: 21 additions & 0 deletions test/helpers/gen-cid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

// CID String: <mbase><version><mcodec><mhash>

const multibase = require('multibase')
const codecs = require('../../src').codecs
const multihashing = require('multihashing')

const mh = multihashing(Buffer('oh, hey!'), 'sha2-256')
const cid = Buffer.concat([
new Buffer('01', 'hex'),
codecs.dagPB,
mh
])

const cidStr = multibase.encode('base58btc', cid).toString()

console.log('CID String (multibase included)')
console.log(cidStr)
console.log('CID in hex (multibase not included)')
console.log(cid.toString('hex'))
Loading