This repository was archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Feature complete #4
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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