From f8fc3812f078ff732168867ecd9cd9092fd6acbd Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 13 Aug 2014 19:23:45 -0400 Subject: [PATCH] add BN.prototype.fromBuffer --- lib/bn.js | 7 +++++++ test/test.bn.js | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/lib/bn.js b/lib/bn.js index ad1ba05..a4eeea0 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -31,6 +31,13 @@ bnjs.fromBuffer = function(buf, opts) { return bn; }; +bnjs.prototype.fromBuffer = function(buf, opts) { + var bn = bnjs.fromBuffer(buf, opts); + bn.copy(this); + + return this; +}; + bnjs.prototype.toBuffer = function(opts) { var buf; if (opts && opts.size) { diff --git a/test/test.bn.js b/test/test.bn.js index fd19344..da7226a 100644 --- a/test/test.bn.js +++ b/test/test.bn.js @@ -83,6 +83,11 @@ describe('bn', function() { bn.toString().should.equal('1'); }); + it('should work as a prototype method', function() { + var bn = BN().fromBuffer(new Buffer('0100', 'hex'), {size: 2, endian: 'little'}); + bn.toString().should.equal('1'); + }); + }); describe('#toBuffer', function() {