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

Commit 08e4787

Browse files
authored
fix: fix base32pad, cleanup
2 parents 9e80d3b + bd02451 commit 08e4787

File tree

8 files changed

+29
-48
lines changed

8 files changed

+29
-48
lines changed

.npmignore

Lines changed: 0 additions & 36 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ stages:
77

88
node_js:
99
- '10'
10+
- '12'
1011

1112
os:
1213
- linux

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"coverage": "aegir coverage",
1818
"coverage-publish": "aegir coverage publish"
1919
},
20+
"files": [
21+
"src",
22+
"dist"
23+
],
2024
"repository": {
2125
"type": "git",
2226
"url": "git+https://github.com/multiformats/js-multibase.git"
@@ -30,13 +34,14 @@
3034
"formats"
3135
],
3236
"devDependencies": {
33-
"aegir": "^18.1.1",
37+
"aegir": "^21.3.0",
3438
"chai": "^4.1.2",
3539
"dirty-chai": "^2.0.1",
3640
"pre-commit": "^1.2.2"
3741
},
3842
"dependencies": {
39-
"base-x": "3.0.4"
43+
"base-x": "^3.0.8",
44+
"buffer": "^5.5.0"
4045
},
4146
"license": "MIT",
4247
"bugs": {

src/base16.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const { Buffer } = require('buffer')
23

34
module.exports = function base16 (alphabet) {
45
return {
@@ -9,7 +10,7 @@ module.exports = function base16 (alphabet) {
910
return input.toString('hex')
1011
},
1112
decode (input) {
12-
for (let char of input) {
13+
for (const char of input) {
1314
if (alphabet.indexOf(char) < 0) {
1415
throw new Error('invalid base16 character')
1516
}

src/base32.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
function decode (input, alphabet) {
44
input = input.replace(new RegExp('=', 'g'), '')
5-
let length = input.length
5+
const length = input.length
66

77
let bits = 0
88
let value = 0
99

1010
let index = 0
11-
let output = new Uint8Array((length * 5 / 8) | 0)
11+
const output = new Uint8Array((length * 5 / 8) | 0)
1212

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

2626
function encode (buffer, alphabet) {
27-
let length = buffer.byteLength
28-
let view = new Uint8Array(buffer)
29-
let padding = alphabet.indexOf('=') === alphabet.length - 1
27+
const length = buffer.byteLength
28+
const view = new Uint8Array(buffer)
29+
const padding = alphabet.indexOf('=') === alphabet.length - 1
3030

3131
if (padding) {
32-
alphabet = alphabet.substring(0, alphabet.length - 2)
32+
alphabet = alphabet.substring(0, alphabet.length - 1)
3333
}
3434

3535
let bits = 0
@@ -63,13 +63,13 @@ module.exports = function base32 (alphabet) {
6363
return {
6464
encode (input) {
6565
if (typeof input === 'string') {
66-
return encode(Buffer.from(input), alphabet)
66+
return encode(Uint8Array.from(input), alphabet)
6767
}
6868

6969
return encode(input, alphabet)
7070
},
7171
decode (input) {
72-
for (let char of input) {
72+
for (const char of input) {
7373
if (alphabet.indexOf(char) < 0) {
7474
throw new Error('invalid base32 character')
7575
}

src/base64.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const { Buffer } = require('buffer')
23

34
module.exports = function base64 (alphabet) {
45
// The alphabet is only used to know:
@@ -31,7 +32,7 @@ module.exports = function base64 (alphabet) {
3132
return output
3233
},
3334
decode (input) {
34-
for (let char of input) {
35+
for (const char of input) {
3536
if (alphabet.indexOf(char) < 0) {
3637
throw new Error('invalid base64 character')
3738
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
'use strict'
66

7+
const { Buffer } = require('buffer')
78
const constants = require('./constants')
89

910
exports = module.exports = multibase

test/multibase.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ describe('multibase.encode ', () => {
176176
})
177177
})
178178
}
179+
180+
it('should allow base32pad full alphabet', () => {
181+
const encodedStr = 'ctimaq4ygg2iegci7'
182+
const decoded = multibase.decode(encodedStr)
183+
184+
const encoded = multibase.encode('c', decoded)
185+
expect(encodedStr).to.be.eq(encoded.toString())
186+
})
179187
})
180188

181189
describe('multibase.decode', () => {

0 commit comments

Comments
 (0)