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

feat: support identity hashes #66

Merged
merged 1 commit into from
Apr 24, 2019
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
6 changes: 5 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,9 @@ exports.names = Object.freeze({
})

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

// sha family
0x11: 'sha1',
0x12: 'sha2-256',
0x13: 'sha2-512',
Expand All @@ -357,6 +360,7 @@ exports.codes = Object.freeze({
0x1B: 'keccak-256',
0x1C: 'keccak-384',
0x1D: 'keccak-512',

0x22: 'murmur3-128',
0x23: 'murmur3-32',

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
Copy link
Member

Choose a reason for hiding this comment

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

Was this test fixture taken from a go-ipfs/go-multiaddr example/output?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, I just made one, should I fetch one from there?

> Buffer.from('7465737420737472696e6720f09f918d', 'hex').toString()
'test string 👍'

}]