Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
feat: support identity hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Apr 4, 2019
1 parent 443a8eb commit ba36bbc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict'

exports.names = Object.freeze({
'id': 0x0,
'identity': 0x0,
'sha1': 0x11,
'sha2-256': 0x12,
'sha2-512': 0x13,
Expand Down Expand Up @@ -343,6 +343,8 @@ exports.names = Object.freeze({
})

exports.codes = Object.freeze({
0x0: 'identity',

0x11: 'sha1',
0x12: 'sha2-256',
0x13: 'sha2-512',
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ exports.decode = function decode (buf) {
* @returns {Buffer}
*/
exports.encode = function encode (digest, code, length) {
if (!digest || !code) {
if (!digest || code == undefined) {
throw new Error('multihash encode requires at least two args: digest, code')
}

Expand Down Expand Up @@ -154,7 +154,7 @@ exports.coerceCode = function coerceCode (name) {
let code = name

if (typeof name === 'string') {
if (!cs.names[name]) {
if (cs.names[name] === undefined) {
throw new Error(`Unrecognized hash function named: ${name}`)
}
code = cs.names[name]
Expand All @@ -164,7 +164,7 @@ exports.coerceCode = function coerceCode (name) {
throw new Error(`Hash function code should be a number. Got: ${code}`)
}

if (!cs.codes[code] && !exports.isAppCode(code)) {
if (cs.codes[code] === undefined && !exports.isAppCode(code)) {
throw new Error(`Unrecognized function code: ${code}`)
}

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ module.exports = [{
},
hex: '2c26b46b',
size: 4
}, {
encoding: {
code: 0x0,
name: 'identity'
},
hex: '7465737420737472696e6720f09f918d',
size: 16
}]

0 comments on commit ba36bbc

Please sign in to comment.