Removes toPaddedBuffer and extends toBuffer
This commit is contained in:
parent
d8883e8d32
commit
df6ea8aea2
4 changed files with 25 additions and 27 deletions
22
src/bigi.js
22
src/bigi.js
|
@ -21,20 +21,18 @@ BigInteger.fromBuffer = function(buffer) {
|
|||
}
|
||||
|
||||
// Export operations
|
||||
BigInteger.prototype.toBuffer = function() {
|
||||
return new Buffer(this.toByteArrayUnsigned())
|
||||
BigInteger.prototype.toBuffer = function(s) {
|
||||
if (s != undefined) assert(Number.isFinite(s))
|
||||
|
||||
var buffer = new Buffer(this.toByteArrayUnsigned())
|
||||
var padded = new Buffer(s - buffer.length)
|
||||
padded.fill(0)
|
||||
|
||||
return Buffer.concat([padded, buffer], s)
|
||||
}
|
||||
|
||||
BigInteger.prototype.toHex = function() {
|
||||
return this.toBuffer().toString('hex')
|
||||
}
|
||||
|
||||
BigInteger.prototype.toPaddedBuffer = function(s) {
|
||||
var buffer = this.toBuffer()
|
||||
var padded = new Buffer(s - buffer.length)
|
||||
padded.fill(0)
|
||||
|
||||
return Buffer.concat([padded, buffer], s)
|
||||
BigInteger.prototype.toHex = function(s) {
|
||||
return this.toBuffer(s).toString('hex')
|
||||
}
|
||||
|
||||
module.exports = BigInteger
|
||||
|
|
|
@ -65,7 +65,7 @@ ECKey.prototype.sign = function(hash) {
|
|||
|
||||
// Export functions
|
||||
ECKey.prototype.toBuffer = function() {
|
||||
return this.D.toPaddedBuffer(32)
|
||||
return this.D.toBuffer(32)
|
||||
}
|
||||
ECKey.prototype.toHex = function() {
|
||||
return this.toBuffer().toString('hex')
|
||||
|
|
|
@ -33,8 +33,8 @@ function sign(key, message) {
|
|||
i += 4
|
||||
}
|
||||
|
||||
var rB = sig.r.toPaddedBuffer(32)
|
||||
var sB = sig.s.toPaddedBuffer(32)
|
||||
var rB = sig.r.toBuffer(32)
|
||||
var sB = sig.s.toBuffer(32)
|
||||
|
||||
return Buffer.concat([new Buffer([i]), rB, sB], 65)
|
||||
}
|
||||
|
|
24
test/bigi.js
24
test/bigi.js
|
@ -7,8 +7,8 @@ describe('BigInteger', function() {
|
|||
describe('fromBuffer/fromHex', function() {
|
||||
it('should match the test vectors', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
assert.deepEqual(BigInteger.fromHex(f.hex).toString(), f.dec)
|
||||
assert.deepEqual(BigInteger.fromHex(f.hexPadded).toString(), f.dec)
|
||||
assert.equal(BigInteger.fromHex(f.hex).toString(), f.dec)
|
||||
assert.equal(BigInteger.fromHex(f.hexPadded).toString(), f.dec)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -24,21 +24,21 @@ describe('BigInteger', function() {
|
|||
describe('toBuffer/toHex', function() {
|
||||
it('should match the test vectors', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
var actualHex = new BigInteger(f.dec).toHex()
|
||||
var bi = new BigInteger(f.dec)
|
||||
|
||||
assert.equal(actualHex, f.hex)
|
||||
assert.equal(bi.toHex(), f.hex)
|
||||
assert.equal(bi.toHex(32), f.hexPadded)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('toPaddedBuffer', function() {
|
||||
it('should match the test vectors', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
var actualBuf = new BigInteger(f.dec).toPaddedBuffer(32)
|
||||
it('throws on non-finite padding value', function() {
|
||||
var bi = new BigInteger('1')
|
||||
|
||||
assert.equal(actualBuf.length, 32)
|
||||
assert.equal(actualBuf.toString('hex'), f.hexPadded)
|
||||
})
|
||||
assert.throws(function() { bi.toHex({}) })
|
||||
assert.throws(function() { bi.toHex([]) })
|
||||
assert.throws(function() { bi.toHex('') })
|
||||
assert.throws(function() { bi.toHex(0 / 0) })
|
||||
assert.throws(function() { bi.toHex(1 / 0) })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue