bitcore/lib/privkey.js

81 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-08-06 22:47:10 -07:00
var bn = require('./bn');
var point = require('./point');
var constants = require('./constants');
var base58check = require('./base58check');
var Privkey = function(n, network, compressed) {
2014-08-07 21:31:36 -07:00
if (typeof n !== 'undefined')
this.setNumber(n);
if (typeof network !== 'undefined')
this.setNetwork(network);
if (typeof compressed !== 'undefined')
this.setCompressed(compressed);
2014-08-06 22:47:10 -07:00
};
Privkey.prototype.setNumber = function(n) {
if (!n.lt(point.getN()))
throw new Error('privkey: Number must be less than N');
this.n = n;
};
Privkey.prototype.setNetwork = function(network) {
if (typeof constants[network] === undefined)
throw new Error('privkey: Must specify the network ("mainnet" or "testnet")');
this.network = network;
};
Privkey.prototype.setCompressed = function(compressed) {
if (typeof compressed !== 'boolean')
throw new Error('privkey: Must specify whether the corresponding public key is compressed or not (true or false)');
this.compressed = compressed;
};
Privkey.prototype.toWIF = function() {
var network = this.network;
var compressed = this.compressed;
2014-08-07 21:31:36 -07:00
if (typeof this.network === 'undefined')
network = 'mainnet';
if (typeof this.compressed === 'undefined')
compressed = true;
2014-08-06 22:47:10 -07:00
var privbuf = this.n.toBuffer({size: 32});
var buf;
if (compressed)
buf = Buffer.concat([new Buffer([constants[network].privkey]), this.n.toBuffer({size: 32}), new Buffer([0x01])]);
else
buf = Buffer.concat([new Buffer([constants[network].privkey]), this.n.toBuffer({size: 32})]);
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('privkey: Length of buffer must be 33 (uncompressed) or 34 (compressed)');
if (buf[0] === constants.mainnet.privkey)
this.network = 'mainnet';
else if (buf[0] === constants.testnet.privkey)
this.network = 'testnet';
else
throw new Error('privkey: Invalid network');
this.n = bn.fromBuffer(buf.slice(1, 32 + 1));
};
Privkey.prototype.toString = function() {
return this.toWIF();
};
Privkey.prototype.fromString = function(str) {
this.fromWIF(str);
};
module.exports = Privkey;