Skip to content

Commit

Permalink
Added usage notes to README
Browse files Browse the repository at this point in the history
  • Loading branch information
tinybike committed Oct 25, 2015
1 parent 995f016 commit 3a19aec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Multi-hash
[![Coverage Status](https://coveralls.io/repos/tinybike/multi-hash/badge.svg?branch=master&service=github)](https://coveralls.io/github/tinybike/multi-hash?branch=master)
[![npm version](https://badge.fury.io/js/multi-hash.svg)](http://badge.fury.io/js/multi-hash)

Multi-hash encoder/decoder.
Multi-hash encoder/decoder. Only supports sha256 multi-hash (the [IPFS](https://ipfs.io/) default) for now; will add more soon.

Usage
-----
Expand All @@ -20,6 +20,19 @@ A minified, browserified file `dist/multihash.min.js` is included for use in the
```html
<script src="dist/multihash.min.js" type="text/javascript"></script>
```
Multi-hash includes `encode` and `decode` functions. `encode` accepts a 32-byte buffer or hex-encoded string, and converts it to a base58-encoded multi-hash string.
```javascript
var hex = "4afeb08a2bf63b8e42f4b67bd92dbf7e4a23f991c7acf0236a9d1c04462db278";

var ipfsHash = multihash.encode(hex);
// ipfsHash: QmPH4nmLYxgWyq9FqpzvxAEPZ5ZdwGZjmvusLqPDCk7mu1
```
`decode` accepts a base58-encoded multi-hash string, and converts it to a 32-byte buffer with the multi-hash prefix removed.
```javascript
var buf = multihash.decode(ipfsHash);
// buf: <Buffer 4a fe b0 8a 2b f6 3b 8e 42 f4 b6 7b d9 2d bf 7e 4a 23 f9 91 c7
// ac f0 23 6a 9d 1c 04 46 2d b2 78>
```

Tests
-----
Expand Down
36 changes: 18 additions & 18 deletions dist/multi-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ var bs58 = require("bs58");

module.exports = {

/**
* Remove multi-hash tag (assuming sha256, the IPFS default):
* 1. Remove leading two characters (Qm)
* 2. Decode base58 string to 33 byte array
* 3. Remove the leading byte, which is part of the hash tag
* @param {string|Buffer} hash Base58-encoded sha256 hash digest.
* @return {Buffer} 32-byte array with multihash tag removed.
*/
decode: function (hash) {
if (hash && hash.constructor === String && hash.slice(0, 2) === "Qm") {
if (hash.length !== 46) {
throw new Error("length error: expected hash+tag length of 46");
}
return new Buffer(bs58.decode(hash.slice(2)).splice(1));
}
throw new Error("unsupported format: expected base58 string");
},

/**
* Add sha256 multi-hash tag to hex-encoded hash and convert to base58.
* (Prefix 1 if leading byte > 60; prefix 2 otherwise.)
Expand All @@ -56,6 +38,24 @@ module.exports = {
return "Qm" + bs58.encode(new Buffer(hex, "hex"));
}
throw new Error("unsupported format: expected hex string");
},

/**
* Remove multi-hash tag (assuming sha256, the IPFS default):
* 1. Remove leading two characters (Qm)
* 2. Decode base58 string to 33 byte array
* 3. Remove the leading byte, which is part of the hash tag
* @param {string|Buffer} hash Base58-encoded sha256 hash digest.
* @return {Buffer} 32-byte array with multihash tag removed.
*/
decode: function (hash) {
if (hash && hash.constructor === String && hash.slice(0, 2) === "Qm") {
if (hash.length !== 46) {
throw new Error("length error: expected hash+tag length of 46");
}
return new Buffer(bs58.decode(hash.slice(2)).splice(1));
}
throw new Error("unsupported format: expected base58 string");
}

};
Expand Down
2 changes: 1 addition & 1 deletion dist/multi-hash.min.js

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@ var bs58 = require("bs58");

module.exports = {

/**
* Remove multi-hash tag (assuming sha256, the IPFS default):
* 1. Remove leading two characters (Qm)
* 2. Decode base58 string to 33 byte array
* 3. Remove the leading byte, which is part of the hash tag
* @param {string|Buffer} hash Base58-encoded sha256 hash digest.
* @return {Buffer} 32-byte array with multihash tag removed.
*/
decode: function (hash) {
if (hash && hash.constructor === String && hash.slice(0, 2) === "Qm") {
if (hash.length !== 46) {
throw new Error("length error: expected hash+tag length of 46");
}
return new Buffer(bs58.decode(hash.slice(2)).splice(1));
}
throw new Error("unsupported format: expected base58 string");
},

/**
* Add sha256 multi-hash tag to hex-encoded hash and convert to base58.
* (Prefix 1 if leading byte > 60; prefix 2 otherwise.)
Expand All @@ -48,6 +30,24 @@ module.exports = {
return "Qm" + bs58.encode(new Buffer(hex, "hex"));
}
throw new Error("unsupported format: expected hex string");
},

/**
* Remove multi-hash tag (assuming sha256, the IPFS default):
* 1. Remove leading two characters (Qm)
* 2. Decode base58 string to 33 byte array
* 3. Remove the leading byte, which is part of the hash tag
* @param {string|Buffer} hash Base58-encoded sha256 hash digest.
* @return {Buffer} 32-byte array with multihash tag removed.
*/
decode: function (hash) {
if (hash && hash.constructor === String && hash.slice(0, 2) === "Qm") {
if (hash.length !== 46) {
throw new Error("length error: expected hash+tag length of 46");
}
return new Buffer(bs58.decode(hash.slice(2)).splice(1));
}
throw new Error("unsupported format: expected base58 string");
}

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multi-hash",
"version": "0.1.0",
"version": "0.1.1",
"description": "Multi-hash encoder/decoder",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 3a19aec

Please sign in to comment.