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

fix: fix base32pad, cleanup #51

Merged
merged 1 commit into from
Mar 16, 2020
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
36 changes: 0 additions & 36 deletions .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ stages:

node_js:
- '10'
- '12'

os:
- linux
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage publish"
},
"files": [
"src",
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/multiformats/js-multibase.git"
Expand All @@ -30,13 +34,14 @@
"formats"
],
"devDependencies": {
"aegir": "^18.1.1",
"aegir": "^21.3.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
},
"dependencies": {
"base-x": "3.0.4"
"base-x": "^3.0.8",
"buffer": "^5.5.0"
},
"license": "MIT",
"bugs": {
Expand Down
3 changes: 2 additions & 1 deletion src/base16.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const { Buffer } = require('buffer')

module.exports = function base16 (alphabet) {
return {
Expand All @@ -9,7 +10,7 @@ module.exports = function base16 (alphabet) {
return input.toString('hex')
},
decode (input) {
for (let char of input) {
for (const char of input) {
if (alphabet.indexOf(char) < 0) {
throw new Error('invalid base16 character')
}
Expand Down
16 changes: 8 additions & 8 deletions src/base32.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

function decode (input, alphabet) {
input = input.replace(new RegExp('=', 'g'), '')
let length = input.length
const length = input.length

let bits = 0
let value = 0

let index = 0
let output = new Uint8Array((length * 5 / 8) | 0)
const output = new Uint8Array((length * 5 / 8) | 0)

for (let i = 0; i < length; i++) {
value = (value << 5) | alphabet.indexOf(input[i])
Expand All @@ -24,12 +24,12 @@ function decode (input, alphabet) {
}

function encode (buffer, alphabet) {
let length = buffer.byteLength
let view = new Uint8Array(buffer)
let padding = alphabet.indexOf('=') === alphabet.length - 1
const length = buffer.byteLength
const view = new Uint8Array(buffer)
const padding = alphabet.indexOf('=') === alphabet.length - 1

if (padding) {
alphabet = alphabet.substring(0, alphabet.length - 2)
alphabet = alphabet.substring(0, alphabet.length - 1)
}

let bits = 0
Expand Down Expand Up @@ -63,13 +63,13 @@ module.exports = function base32 (alphabet) {
return {
encode (input) {
if (typeof input === 'string') {
return encode(Buffer.from(input), alphabet)
return encode(Uint8Array.from(input), alphabet)
}

return encode(input, alphabet)
},
decode (input) {
for (let char of input) {
for (const char of input) {
if (alphabet.indexOf(char) < 0) {
throw new Error('invalid base32 character')
}
Expand Down
3 changes: 2 additions & 1 deletion src/base64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const { Buffer } = require('buffer')

module.exports = function base64 (alphabet) {
// The alphabet is only used to know:
Expand Down Expand Up @@ -31,7 +32,7 @@ module.exports = function base64 (alphabet) {
return output
},
decode (input) {
for (let char of input) {
for (const char of input) {
if (alphabet.indexOf(char) < 0) {
throw new Error('invalid base64 character')
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
'use strict'

const { Buffer } = require('buffer')
const constants = require('./constants')

exports = module.exports = multibase
Expand Down
8 changes: 8 additions & 0 deletions test/multibase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ describe('multibase.encode ', () => {
})
})
}

it('should allow base32pad full alphabet', () => {
const encodedStr = 'ctimaq4ygg2iegci7'
const decoded = multibase.decode(encodedStr)

const encoded = multibase.encode('c', decoded)
expect(encodedStr).to.be.eq(encoded.toString())
})
})

describe('multibase.decode', () => {
Expand Down