feat: remove toBuffer from number

This commit is contained in:
nanov 2019-11-07 10:30:03 +02:00
parent 0efc46b014
commit 4496cbf560
3 changed files with 12 additions and 12 deletions

View File

@ -25,17 +25,18 @@ class BigNumber {
throw new TypeError('Unsupported value type.'); throw new TypeError('Unsupported value type.');
} }
toBEBuffer(length = undefined) { toBEBuffer(length) {
const arr = this._n.toByteArray(); const arr = this._n.toByteArray();
if (length && arr.length > length) if (length === undefined)
throw new Error('Number out of range.'); return Buffer.from(arr);
while (length && arr.length < length)
arr.unshift(0);
return Buffer.from(arr);
}
toBuffer() { if (arr.length > length)
return Buffer.from(this._n.toByteArray()); throw new Error('Number out of range.');
while (arr.length < length)
arr.unshift(0);
return Buffer.from(arr);
} }
toString(radix = 10) { toString(radix = 10) {

View File

@ -33,7 +33,7 @@ const emsaPSS = (msg, salt) => {
maskedDbMsb = `0${maskedDbMsb.substr(1)}`; maskedDbMsb = `0${maskedDbMsb.substr(1)}`;
// console.log((new BN(maskedDbMsb, 2).toBuffer())[0], new BigNumber(maskedDbMsb, 2).toBuffer()[0]); // 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 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')]); return Buffer.concat([maskedDb, mTickHash, Buffer.from('BC', 'hex')]);
}; };
@ -72,7 +72,7 @@ module.exports = class Crypto {
const power = new BigNumber(key.d()); const power = new BigNumber(key.d());
const mod = new BigNumber(key.n()); 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) { static pad(d) {

View File

@ -29,7 +29,6 @@ describe('BigNumber', () => {
})); }));
it('toString with radix 10', () => assert.equal(instance.toString(), '11')); it('toString with radix 10', () => assert.equal(instance.toString(), '11'));
it('toString with radix 16', () => assert.equal(instance.toString(16), '0b')); 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 without length', () => assert.equal(instance.toBEBuffer().toString('hex'), '0b'));
it('toBEBuffer with length', () => assert.equal(instance.toBEBuffer(4).toString('hex'), '0000000b')); it('toBEBuffer with length', () => assert.equal(instance.toBEBuffer(4).toString('hex'), '0000000b'));
}); });