diff --git a/app/scripts/lib/crypto/util.js b/app/scripts/lib/crypto/util.js index d51c478ba..31beacdb5 100644 --- a/app/scripts/lib/crypto/util.js +++ b/app/scripts/lib/crypto/util.js @@ -3,11 +3,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** - * Converts base64url to an ArrayBuffer + * Converts base64url to a Uint8Array * @param {String} base64 - * @returns {ArrayBufferLike} + * @returns {Uint8Array} */ -function base64urlToArrayBuffer(base64 = '') { +function base64urlToUint8Array(base64 = '') { base64 = base64.replace(/-/g, '+'); // 62nd char of encoding base64 = base64.replace(/_/g, '/'); // 63rd char of encoding const binaryString = atob(base64); @@ -16,9 +16,9 @@ function base64urlToArrayBuffer(base64 = '') { for (let i = 0; i < len; i++){ bytes[i] = binaryString.charCodeAt(i); } - return bytes.buffer; + return bytes; } module.exports = { - base64urlToArrayBuffer + base64urlToUint8Array }; diff --git a/app/scripts/lib/pairing-channel-client.js b/app/scripts/lib/pairing-channel-client.js index c03a75a42..2ba707a37 100644 --- a/app/scripts/lib/pairing-channel-client.js +++ b/app/scripts/lib/pairing-channel-client.js @@ -5,7 +5,7 @@ import PairingChannelClientErrors from './pairing-channel-client-errors'; import { Model } from 'backbone'; import { pick } from 'underscore'; -import { base64urlToArrayBuffer } from './crypto/util'; +import { base64urlToUint8Array } from './crypto/util'; import Raven from 'raven'; import Vat from 'lib/vat'; @@ -61,7 +61,7 @@ export default class PairingChannelClient extends Model { throw PairingChannelClientErrors.toError('INVALID_CONFIGURATION'); } - const psk = base64urlToArrayBuffer(channelKey); + const psk = base64urlToUint8Array(channelKey); return FxAccountsPairingChannel.PairingChannel.connect(channelServerUri, channelId, psk).then((channel) => { this.channel = channel; diff --git a/app/tests/spec/lib/crypto/util.js b/app/tests/spec/lib/crypto/util.js index b2b0dd38a..ac5caa4d5 100644 --- a/app/tests/spec/lib/crypto/util.js +++ b/app/tests/spec/lib/crypto/util.js @@ -5,25 +5,25 @@ 'use strict'; import {assert} from 'chai'; -import {base64urlToArrayBuffer} from 'lib/crypto/util'; +import {base64urlToUint8Array} from 'lib/crypto/util'; -describe('lib/crypto/util/base64urlToArrayBuffer', () => { +describe('lib/crypto/util/base64urlToUint8Array', () => { it('should decode base64url undefined as empty string', () => { - const arrayBuffer = base64urlToArrayBuffer(); - assert.equal(arrayBuffer.constructor.name, 'ArrayBuffer', 'is an array buffer'); - assert.equal(new Int8Array(arrayBuffer)[0], undefined, 'is correct value for empty string'); + const uint8array = base64urlToUint8Array(); + assert.equal(uint8array.constructor.name, 'Uint8Array', 'is a Uint8Array'); + assert.equal(new Int8Array(uint8array)[0], undefined, 'is correct value for empty string'); }); it('should decode base64url empty string', () => { - const arrayBuffer = base64urlToArrayBuffer(''); - assert.equal(arrayBuffer.constructor.name, 'ArrayBuffer', 'is an array buffer'); - assert.equal(new Int8Array(arrayBuffer)[0], undefined, 'is correct value for empty string'); + const uint8array = base64urlToUint8Array(''); + assert.equal(uint8array.constructor.name, 'Uint8Array', 'is a Uint8Array'); + assert.equal(new Int8Array(uint8array)[0], undefined, 'is correct value for empty string'); }); it('should decode base64url test', () => { - const arrayBuffer = base64urlToArrayBuffer('dGVzdA=='); - assert.equal(arrayBuffer.constructor.name, 'ArrayBuffer', 'is an array buffer'); - const int8Array = new Int8Array(arrayBuffer); + const uint8array = base64urlToUint8Array('dGVzdA=='); + assert.equal(uint8array.constructor.name, 'Uint8Array', 'is a Uint8Array'); + const int8Array = new Int8Array(uint8array); assert.equal(int8Array[0], 116); assert.equal(int8Array[1], 101); assert.equal(int8Array[2], 115);