Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Add toBufferNoPadding method to private key
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Sep 16, 2016
1 parent d0e3f84 commit e9d1237
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/privatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ PrivateKey.prototype.toBuffer = function(){
return this.bn.toBuffer({ size: 32 });
};

/**
* Will return the private key as a BN buffer without leading zero padding
*
* @returns {Buffer} A buffer of the private key
*/
PrivateKey.prototype.toBufferNoPadding = function() {
return this.bn.toBuffer();
};

/**
* Will return the corresponding public key
*
Expand Down
14 changes: 14 additions & 0 deletions test/privatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@ describe('PrivateKey', function() {
fromBuffer.toString().should.equal(privkey.toString());
});

it('will output a 32 byte buffer', function() {
var bn = BN.fromBuffer(new Buffer('9b5a0e8fee1835e21170ce1431f9b6f19b487e67748ed70d8a4462bc031915', 'hex'));
var privkey = new PrivateKey(bn);
var buffer = privkey.toBuffer();
buffer.length.should.equal(32);
});

it('will output a 31 byte buffer', function() {
var bn = BN.fromBuffer(new Buffer('9b5a0e8fee1835e21170ce1431f9b6f19b487e67748ed70d8a4462bc031915', 'hex'));
var privkey = new PrivateKey(bn);
var buffer = privkey.toBufferNoPadding();
buffer.length.should.equal(31);
});

it('should return buffer with length equal 32', function() {
var bn = BN.fromBuffer(buf.slice(0, 31));
var privkey = new PrivateKey(bn, 'livenet');
Expand Down

0 comments on commit e9d1237

Please sign in to comment.