bitcore/lib/privkey.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-08-28 19:27:22 -07:00
var BN = require('./bn');
2014-08-28 20:19:30 -07:00
var Point = require('./point');
2014-08-06 22:47:10 -07:00
var constants = require('./constants');
var base58check = require('./base58check');
2014-08-28 19:27:22 -07:00
var Random = require('./random');
2014-08-06 22:47:10 -07:00
2014-09-01 21:13:44 -07:00
var Privkey = function Privkey(bn) {
if (!(this instanceof Privkey))
2014-09-02 12:07:18 -07:00
return new Privkey(bn);
2014-09-01 21:13:44 -07:00
if (bn instanceof BN)
this.bn = bn;
else if (bn) {
var obj = bn;
2014-08-28 17:26:56 -07:00
this.set(obj);
2014-09-01 21:13:44 -07:00
}
2014-08-28 17:26:56 -07:00
};
Privkey.prototype.set = function(obj) {
this.bn = obj.bn || this.bn;
this.networkstr = obj.networkstr || this.networkstr;
2014-08-28 17:26:56 -07:00
this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed;
return this;
2014-08-06 22:47:10 -07:00
};
2014-08-28 19:27:22 -07:00
Privkey.prototype.fromRandom = function() {
do {
var privbuf = Random.getRandomBuffer(32);
var bn = BN().fromBuffer(privbuf);
2014-08-28 20:19:30 -07:00
var condition = bn.lt(Point.getN());
2014-08-28 19:27:22 -07:00
} while (!condition);
2014-08-29 12:38:43 -07:00
this.set({
bn: bn,
networkstr: 'mainnet',
compressed: true
});
2014-08-28 19:27:22 -07:00
return this;
};
2014-08-09 19:58:48 -07:00
Privkey.prototype.validate = function() {
2014-08-28 20:19:30 -07:00
if (!this.bn.lt(Point.getN()))
throw new Error('Number must be less than N');
if (typeof constants[this.networkstr] === undefined)
throw new Error('Must specify the networkstr ("mainnet" or "testnet")');
2014-08-09 19:58:48 -07:00
if (typeof this.compressed !== 'boolean')
throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)');
2014-08-06 22:47:10 -07:00
};
Privkey.prototype.toWIF = function() {
var networkstr = this.networkstr;
2014-08-06 22:47:10 -07:00
var compressed = this.compressed;
if (typeof this.networkstr === 'undefined')
networkstr = 'mainnet';
2014-08-07 21:31:36 -07:00
if (typeof this.compressed === 'undefined')
compressed = true;
2014-08-13 12:23:06 -07:00
var privbuf = this.bn.toBuffer({size: 32});
2014-08-06 22:47:10 -07:00
var buf;
if (compressed)
buf = Buffer.concat([new Buffer([constants[networkstr].privkey]), this.bn.toBuffer({size: 32}), new Buffer([0x01])]);
2014-08-06 22:47:10 -07:00
else
buf = Buffer.concat([new Buffer([constants[networkstr].privkey]), this.bn.toBuffer({size: 32})]);
2014-08-06 22:47:10 -07:00
return base58check.encode(buf);
};
Privkey.prototype.fromWIF = function(str) {
var buf = base58check.decode(str);
if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] == 1)
this.compressed = true;
else if (buf.length === 1 + 32)
this.compressed = false;
else
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
2014-08-06 22:47:10 -07:00
if (buf[0] === constants.mainnet.privkey)
this.networkstr = 'mainnet';
2014-08-06 22:47:10 -07:00
else if (buf[0] === constants.testnet.privkey)
this.networkstr = 'testnet';
2014-08-06 22:47:10 -07:00
else
throw new Error('Invalid networkstr');
2014-08-06 22:47:10 -07:00
2014-08-28 19:27:22 -07:00
this.bn = BN.fromBuffer(buf.slice(1, 32 + 1));
2014-08-06 22:47:10 -07:00
};
Privkey.prototype.toString = function() {
return this.toWIF();
};
Privkey.prototype.fromString = function(str) {
this.fromWIF(str);
};
module.exports = Privkey;