Skip to content

Commit df6ea8a

Browse files
committed
Removes toPaddedBuffer and extends toBuffer
1 parent d8883e8 commit df6ea8a

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/bigi.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ BigInteger.fromBuffer = function(buffer) {
2121
}
2222

2323
// Export operations
24-
BigInteger.prototype.toBuffer = function() {
25-
return new Buffer(this.toByteArrayUnsigned())
26-
}
24+
BigInteger.prototype.toBuffer = function(s) {
25+
if (s != undefined) assert(Number.isFinite(s))
2726

28-
BigInteger.prototype.toHex = function() {
29-
return this.toBuffer().toString('hex')
30-
}
27+
var buffer = new Buffer(this.toByteArrayUnsigned())
28+
var padded = new Buffer(s - buffer.length)
29+
padded.fill(0)
3130

32-
BigInteger.prototype.toPaddedBuffer = function(s) {
33-
var buffer = this.toBuffer()
34-
var padded = new Buffer(s - buffer.length)
35-
padded.fill(0)
31+
return Buffer.concat([padded, buffer], s)
32+
}
3633

37-
return Buffer.concat([padded, buffer], s)
34+
BigInteger.prototype.toHex = function(s) {
35+
return this.toBuffer(s).toString('hex')
3836
}
3937

4038
module.exports = BigInteger

src/eckey.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ECKey.prototype.sign = function(hash) {
6565

6666
// Export functions
6767
ECKey.prototype.toBuffer = function() {
68-
return this.D.toPaddedBuffer(32)
68+
return this.D.toBuffer(32)
6969
}
7070
ECKey.prototype.toHex = function() {
7171
return this.toBuffer().toString('hex')

src/message.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function sign(key, message) {
3333
i += 4
3434
}
3535

36-
var rB = sig.r.toPaddedBuffer(32)
37-
var sB = sig.s.toPaddedBuffer(32)
36+
var rB = sig.r.toBuffer(32)
37+
var sB = sig.s.toBuffer(32)
3838

3939
return Buffer.concat([new Buffer([i]), rB, sB], 65)
4040
}

test/bigi.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ describe('BigInteger', function() {
77
describe('fromBuffer/fromHex', function() {
88
it('should match the test vectors', function() {
99
fixtures.valid.forEach(function(f) {
10-
assert.deepEqual(BigInteger.fromHex(f.hex).toString(), f.dec)
11-
assert.deepEqual(BigInteger.fromHex(f.hexPadded).toString(), f.dec)
10+
assert.equal(BigInteger.fromHex(f.hex).toString(), f.dec)
11+
assert.equal(BigInteger.fromHex(f.hexPadded).toString(), f.dec)
1212
})
1313
})
1414

@@ -24,21 +24,21 @@ describe('BigInteger', function() {
2424
describe('toBuffer/toHex', function() {
2525
it('should match the test vectors', function() {
2626
fixtures.valid.forEach(function(f) {
27-
var actualHex = new BigInteger(f.dec).toHex()
27+
var bi = new BigInteger(f.dec)
2828

29-
assert.equal(actualHex, f.hex)
29+
assert.equal(bi.toHex(), f.hex)
30+
assert.equal(bi.toHex(32), f.hexPadded)
3031
})
3132
})
32-
})
3333

34-
describe('toPaddedBuffer', function() {
35-
it('should match the test vectors', function() {
36-
fixtures.valid.forEach(function(f) {
37-
var actualBuf = new BigInteger(f.dec).toPaddedBuffer(32)
34+
it('throws on non-finite padding value', function() {
35+
var bi = new BigInteger('1')
3836

39-
assert.equal(actualBuf.length, 32)
40-
assert.equal(actualBuf.toString('hex'), f.hexPadded)
41-
})
37+
assert.throws(function() { bi.toHex({}) })
38+
assert.throws(function() { bi.toHex([]) })
39+
assert.throws(function() { bi.toHex('') })
40+
assert.throws(function() { bi.toHex(0 / 0) })
41+
assert.throws(function() { bi.toHex(1 / 0) })
4242
})
4343
})
4444
})

0 commit comments

Comments
 (0)