bitcore/lib/keypair.js

57 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-08-07 21:31:36 -07:00
var Privkey = require('./privkey');
var Pubkey = require('./pubkey');
2014-08-28 19:27:22 -07:00
var BN = require('./bn');
2014-08-07 21:31:36 -07:00
var point = require('./point');
2014-08-28 16:18:36 -07:00
var Key = function Key(obj) {
if (!(this instanceof Key))
2014-08-28 16:18:36 -07:00
return new Key(obj);
if (obj)
this.set(obj);
};
Key.prototype.set = function(obj) {
this.privkey = obj.privkey || this.privkey || undefined;
this.pubkey = obj.pubkey || this.pubkey || undefined;
return this;
2014-08-07 21:31:36 -07:00
};
2014-08-29 12:43:55 -07:00
Key.prototype.fromPrivkey = function(privkey) {
this.privkey = privkey;
this.privkey2pubkey();
return this;
};
2014-08-07 21:31:36 -07:00
Key.prototype.fromRandom = function() {
2014-08-28 19:27:22 -07:00
this.privkey = Privkey().fromRandom();
2014-08-09 17:43:24 -07:00
this.privkey2pubkey();
return this;
2014-08-07 21:31:36 -07:00
};
Key.prototype.fromString = function(str) {
var obj = JSON.parse(str);
2014-08-13 12:23:06 -07:00
if (obj.privkey) {
2014-08-09 17:43:24 -07:00
this.privkey = new Privkey();
2014-08-13 12:23:06 -07:00
this.privkey.fromString(obj.privkey);
2014-08-07 21:31:36 -07:00
}
2014-08-13 12:23:06 -07:00
if (obj.pubkey) {
2014-08-09 17:43:24 -07:00
this.pubkey = new Pubkey();
2014-08-13 12:23:06 -07:00
this.pubkey.fromString(obj.pubkey);
2014-08-07 21:31:36 -07:00
}
};
2014-08-09 17:43:24 -07:00
Key.prototype.privkey2pubkey = function() {
2014-08-28 20:19:30 -07:00
this.pubkey = Pubkey().fromPrivkey(this.privkey);
2014-08-07 21:31:36 -07:00
};
Key.prototype.toString = function() {
var obj = {};
2014-08-09 17:43:24 -07:00
if (this.privkey)
2014-08-13 12:23:06 -07:00
obj.privkey = this.privkey.toString();
2014-08-09 17:43:24 -07:00
if (this.pubkey)
2014-08-13 12:23:06 -07:00
obj.pubkey = this.pubkey.toString();
2014-08-07 21:31:36 -07:00
return JSON.stringify(obj);
};
module.exports = Key;