network -> networkstr

...for compatibility with address, and to make the types obvious
This commit is contained in:
Ryan X. Charles 2014-08-28 17:53:11 -07:00
parent e2824035bb
commit 280578d641
2 changed files with 14 additions and 14 deletions

View File

@ -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));
};

View File

@ -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);
});