mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 19:50:58 +00:00
fix(utils): make util.toBase64
browserify compatible
`bn.toArrayLike` is used instead of `bn.toBuffer`, to ensure compatibility with browserify.
This commit is contained in:
parent
a4e6f9dd83
commit
933119445f
@ -5,7 +5,7 @@ const BN = require('asn1.js').bignum
|
|||||||
// Convert a BN.js instance to a base64 encoded string without padding
|
// Convert a BN.js instance to a base64 encoded string without padding
|
||||||
// Adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C
|
// Adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C
|
||||||
exports.toBase64 = function toBase64 (bn) {
|
exports.toBase64 = function toBase64 (bn) {
|
||||||
let s = bn.toBuffer('be').toString('base64')
|
let s = bn.toArrayLike(Buffer, 'be').toString('base64')
|
||||||
|
|
||||||
return s
|
return s
|
||||||
.replace(/(=*)$/, '') // Remove any trailing '='s
|
.replace(/(=*)$/, '') // Remove any trailing '='s
|
||||||
|
21
test/util.spec.js
Normal file
21
test/util.spec.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const util = require('../src/crypto/util')
|
||||||
|
const BN = require('bn.js')
|
||||||
|
|
||||||
|
describe('Util', () => {
|
||||||
|
let bn
|
||||||
|
|
||||||
|
before((done) => {
|
||||||
|
bn = new BN('dead', 16)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('toBase64', (done) => {
|
||||||
|
expect(util.toBase64(bn)).to.be.eql('3q0')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user