From 933119445fa9ca3faa6d745b91cc639a13cdc325 Mon Sep 17 00:00:00 2001 From: nikuda Date: Sat, 3 Dec 2016 19:32:07 +1100 Subject: [PATCH] fix(utils): make `util.toBase64` browserify compatible `bn.toArrayLike` is used instead of `bn.toBuffer`, to ensure compatibility with browserify. --- src/crypto/util.js | 2 +- test/util.spec.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/util.spec.js diff --git a/src/crypto/util.js b/src/crypto/util.js index a641b82..a33efd9 100644 --- a/src/crypto/util.js +++ b/src/crypto/util.js @@ -5,7 +5,7 @@ const BN = require('asn1.js').bignum // 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 exports.toBase64 = function toBase64 (bn) { - let s = bn.toBuffer('be').toString('base64') + let s = bn.toArrayLike(Buffer, 'be').toString('base64') return s .replace(/(=*)$/, '') // Remove any trailing '='s diff --git a/test/util.spec.js b/test/util.spec.js new file mode 100644 index 0000000..31b2434 --- /dev/null +++ b/test/util.spec.js @@ -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() + }) +})