From 280578d64120238b6fd3111e20fab6c7b747881f Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Thu, 28 Aug 2014 17:53:11 -0700 Subject: [PATCH] network -> networkstr ...for compatibility with address, and to make the types obvious --- lib/privkey.js | 22 +++++++++++----------- test/test.privkey.js | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/privkey.js b/lib/privkey.js index 9fde9d8..d012fa8 100644 --- a/lib/privkey.js +++ b/lib/privkey.js @@ -12,7 +12,7 @@ var Privkey = function Privkey(obj) { Privkey.prototype.set = function(obj) { this.bn = obj.bn || this.bn; - this.network = obj.network || this.network; + this.networkstr = obj.networkstr || this.networkstr; this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed; return this; }; @@ -20,27 +20,27 @@ Privkey.prototype.set = function(obj) { Privkey.prototype.validate = function() { if (!this.bn.lt(point.getN())) throw new Error('Number must be less than N'); - if (typeof constants[this.network] === undefined) - throw new Error('Must specify the network ("mainnet" or "testnet")'); + if (typeof constants[this.networkstr] === undefined) + throw new Error('Must specify the networkstr ("mainnet" or "testnet")'); if (typeof this.compressed !== 'boolean') throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)'); }; Privkey.prototype.toWIF = function() { - var network = this.network; + var networkstr = this.networkstr; var compressed = this.compressed; - if (typeof this.network === 'undefined') - network = 'mainnet'; + if (typeof this.networkstr === 'undefined') + networkstr = 'mainnet'; if (typeof this.compressed === 'undefined') compressed = true; var privbuf = this.bn.toBuffer({size: 32}); var buf; if (compressed) - buf = Buffer.concat([new Buffer([constants[network].privkey]), this.bn.toBuffer({size: 32}), new Buffer([0x01])]); + buf = Buffer.concat([new Buffer([constants[networkstr].privkey]), this.bn.toBuffer({size: 32}), new Buffer([0x01])]); else - buf = Buffer.concat([new Buffer([constants[network].privkey]), this.bn.toBuffer({size: 32})]); + buf = Buffer.concat([new Buffer([constants[networkstr].privkey]), this.bn.toBuffer({size: 32})]); return base58check.encode(buf); }; @@ -56,11 +56,11 @@ Privkey.prototype.fromWIF = function(str) { throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)'); if (buf[0] === constants.mainnet.privkey) - this.network = 'mainnet'; + this.networkstr = 'mainnet'; else if (buf[0] === constants.testnet.privkey) - this.network = 'testnet'; + this.networkstr = 'testnet'; else - throw new Error('Invalid network'); + throw new Error('Invalid networkstr'); this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1)); }; diff --git a/test/test.privkey.js b/test/test.privkey.js index 26872a3..ed0a4ce 100644 --- a/test/test.privkey.js +++ b/test/test.privkey.js @@ -17,17 +17,17 @@ describe('Privkey', function() { }); it('should create a mainnet private key', function() { - var privkey = new Privkey({bn: Bn.fromBuffer(buf), network: 'mainnet', compressed: true}); + var privkey = new Privkey({bn: Bn.fromBuffer(buf), networkstr: 'mainnet', compressed: true}); privkey.toString().should.equal(encmainnet); }); it('should create an uncompressed testnet private key', function() { - var privkey = new Privkey({bn: Bn.fromBuffer(buf), network: 'testnet', compressed: false}); + var privkey = new Privkey({bn: Bn.fromBuffer(buf), networkstr: 'testnet', compressed: false}); privkey.toString().should.equal(enctu); }); it('should create an uncompressed mainnet private key', function() { - var privkey = new Privkey({bn: Bn.fromBuffer(buf), network: 'mainnet', compressed: false}); + var privkey = new Privkey({bn: Bn.fromBuffer(buf), networkstr: 'mainnet', compressed: false}); privkey.toString().should.equal(encmu); });