|
@ -12,7 +12,7 @@ var Privkey = function Privkey(obj) { |
|
|
|
|
|
|
|
|
Privkey.prototype.set = function(obj) { |
|
|
Privkey.prototype.set = function(obj) { |
|
|
this.bn = obj.bn || this.bn; |
|
|
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; |
|
|
this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed; |
|
|
return this; |
|
|
return this; |
|
|
}; |
|
|
}; |
|
@ -20,27 +20,27 @@ Privkey.prototype.set = function(obj) { |
|
|
Privkey.prototype.validate = function() { |
|
|
Privkey.prototype.validate = function() { |
|
|
if (!this.bn.lt(point.getN())) |
|
|
if (!this.bn.lt(point.getN())) |
|
|
throw new Error('Number must be less than N'); |
|
|
throw new Error('Number must be less than N'); |
|
|
if (typeof constants[this.network] === undefined) |
|
|
if (typeof constants[this.networkstr] === undefined) |
|
|
throw new Error('Must specify the network ("mainnet" or "testnet")'); |
|
|
throw new Error('Must specify the networkstr ("mainnet" or "testnet")'); |
|
|
if (typeof this.compressed !== 'boolean') |
|
|
if (typeof this.compressed !== 'boolean') |
|
|
throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)'); |
|
|
throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)'); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Privkey.prototype.toWIF = function() { |
|
|
Privkey.prototype.toWIF = function() { |
|
|
var network = this.network; |
|
|
var networkstr = this.networkstr; |
|
|
var compressed = this.compressed; |
|
|
var compressed = this.compressed; |
|
|
|
|
|
|
|
|
if (typeof this.network === 'undefined') |
|
|
if (typeof this.networkstr === 'undefined') |
|
|
network = 'mainnet'; |
|
|
networkstr = 'mainnet'; |
|
|
if (typeof this.compressed === 'undefined') |
|
|
if (typeof this.compressed === 'undefined') |
|
|
compressed = true; |
|
|
compressed = true; |
|
|
|
|
|
|
|
|
var privbuf = this.bn.toBuffer({size: 32}); |
|
|
var privbuf = this.bn.toBuffer({size: 32}); |
|
|
var buf; |
|
|
var buf; |
|
|
if (compressed) |
|
|
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 |
|
|
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); |
|
|
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)'); |
|
|
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)'); |
|
|
|
|
|
|
|
|
if (buf[0] === constants.mainnet.privkey) |
|
|
if (buf[0] === constants.mainnet.privkey) |
|
|
this.network = 'mainnet'; |
|
|
this.networkstr = 'mainnet'; |
|
|
else if (buf[0] === constants.testnet.privkey) |
|
|
else if (buf[0] === constants.testnet.privkey) |
|
|
this.network = 'testnet'; |
|
|
this.networkstr = 'testnet'; |
|
|
else |
|
|
else |
|
|
throw new Error('Invalid network'); |
|
|
throw new Error('Invalid networkstr'); |
|
|
|
|
|
|
|
|
this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1)); |
|
|
this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1)); |
|
|
}; |
|
|
}; |
|
|