From 4496cbf560fc20a86596979c04be8122635ea4e8 Mon Sep 17 00:00:00 2001 From: nanov Date: Thu, 7 Nov 2019 10:30:03 +0200 Subject: [PATCH] feat: remove toBuffer from number --- lib/crypto/BigNumber.js | 19 ++++++++++--------- lib/crypto/Crypto.js | 4 ++-- test/unit/BigNumber.js | 1 - 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/crypto/BigNumber.js b/lib/crypto/BigNumber.js index fda2e84..ce25340 100644 --- a/lib/crypto/BigNumber.js +++ b/lib/crypto/BigNumber.js @@ -25,17 +25,18 @@ class BigNumber { throw new TypeError('Unsupported value type.'); } - toBEBuffer(length = undefined) { + toBEBuffer(length) { const arr = this._n.toByteArray(); - if (length && arr.length > length) - throw new Error('Number out of range.'); - while (length && arr.length < length) - arr.unshift(0); - return Buffer.from(arr); - } + if (length === undefined) + return Buffer.from(arr); - toBuffer() { - return Buffer.from(this._n.toByteArray()); + if (arr.length > length) + throw new Error('Number out of range.'); + + while (arr.length < length) + arr.unshift(0); + + return Buffer.from(arr); } toString(radix = 10) { diff --git a/lib/crypto/Crypto.js b/lib/crypto/Crypto.js index a39fd75..445088c 100644 --- a/lib/crypto/Crypto.js +++ b/lib/crypto/Crypto.js @@ -33,7 +33,7 @@ const emsaPSS = (msg, salt) => { maskedDbMsb = `0${maskedDbMsb.substr(1)}`; // console.log((new BN(maskedDbMsb, 2).toBuffer())[0], new BigNumber(maskedDbMsb, 2).toBuffer()[0]); // maskedDb[0] = (new BN(maskedDbMsb, 2).toBuffer())[0]; // eslint-disable-line - maskedDb[0] = new BigNumber(maskedDbMsb, 2).toBuffer()[0]; // eslint-disable-line + maskedDb[0] = new BigNumber(maskedDbMsb, 2).toBEBuffer()[0]; // eslint-disable-line return Buffer.concat([maskedDb, mTickHash, Buffer.from('BC', 'hex')]); }; @@ -72,7 +72,7 @@ module.exports = class Crypto { const power = new BigNumber(key.d()); const mod = new BigNumber(key.n()); - return (modPow(base, power, mod)).toBuffer().toString('base64'); + return (modPow(base, power, mod)).toBEBuffer().toString('base64'); } static pad(d) { diff --git a/test/unit/BigNumber.js b/test/unit/BigNumber.js index 0b95bca..ff725c7 100644 --- a/test/unit/BigNumber.js +++ b/test/unit/BigNumber.js @@ -29,7 +29,6 @@ describe('BigNumber', () => { })); it('toString with radix 10', () => assert.equal(instance.toString(), '11')); it('toString with radix 16', () => assert.equal(instance.toString(16), '0b')); - it('toBuffer', () => assert.equal(instance.toBuffer().toString('hex'), '0b')); it('toBEBuffer without length', () => assert.equal(instance.toBEBuffer().toString('hex'), '0b')); it('toBEBuffer with length', () => assert.equal(instance.toBEBuffer(4).toString('hex'), '0000000b')); });