Skip to content

Commit

Permalink
fix: getNumber varint decode bug
Browse files Browse the repository at this point in the history
This fixes #50 by removing the varintUint8ArrayDecode function and using varint.decode directly instead.

Co-authored-by: Jonas Kruckenberg <[email protected]>
  • Loading branch information
JonasKruckenberg and Jonas Kruckenberg authored Sep 7, 2020
1 parent 86548cd commit ce93ceb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ exports.getNumber = (name) => {
if (code === undefined) {
throw new Error('Codec `' + name + '` not found')
}
return util.varintUint8ArrayDecode(code)[0]
return varint.decode(code)
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
numberToUint8Array,
uint8ArrayToNumber,
varintUint8ArrayEncode,
varintUint8ArrayDecode,
varintEncode
}

Expand All @@ -28,10 +27,6 @@ function varintUint8ArrayEncode (input) {
return Uint8Array.from(varint.encode(uint8ArrayToNumber(input)))
}

function varintUint8ArrayDecode (input) {
return numberToUint8Array(varint.decode(input))
}

function varintEncode (num) {
return Uint8Array.from(varint.encode(num))
}
11 changes: 8 additions & 3 deletions test/multicodec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { expect } = require('aegir/utils/chai')
const multicodec = require('../src')
const uint8ArrayFromString = require('uint8arrays/from-string')
const baseTable = require('../src/base-table.json')

describe('multicodec', () => {
it('add prefix through multicodec (string)', () => {
Expand Down Expand Up @@ -53,9 +54,13 @@ describe('multicodec', () => {
it('returns the codec number from name', () => {
expect(multicodec.getNumber('eth-block')).to.eql(144)
expect(multicodec.getNumber('dag-pb')).to.eql(112)
// NOTE vmx 2019-09019: Uncomment once
// https://github.com/multiformats/js-multicodec/issues/50 is fixed
// expect(multicodec.getNumber('blake2b-8')).to.eql(0xb201)
expect(multicodec.getNumber('blake2b-8')).to.eql(0xb201)
})

it('returns all codec numbers from names', () => {
for (const name in baseTable) {
expect(multicodec.getNumber(name)).to.eql(baseTable[name])
}
})

it('returns the codec number from constant', () => {
Expand Down

0 comments on commit ce93ceb

Please sign in to comment.