You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7 KiB

11 years ago
var Privkey = require('./privkey');
var Pubkey = require('./pubkey');
var BN = require('./bn');
11 years ago
var point = require('./point');
var Keypair = function Keypair(obj) {
if (!(this instanceof Keypair))
return new Keypair(obj);
11 years ago
if (obj)
this.set(obj);
};
Keypair.prototype.set = function(obj) {
11 years ago
this.privkey = obj.privkey || this.privkey || undefined;
this.pubkey = obj.pubkey || this.pubkey || undefined;
return this;
11 years ago
};
Keypair.prototype.fromJSON = function(json) {
if (json.privkey)
this.set({privkey: Privkey().fromJSON(json.privkey)});
if (json.pubkey)
this.set({pubkey: Pubkey().fromJSON(json.pubkey)});
return this;
};
Keypair.prototype.toJSON = function() {
var json = {};
if (this.privkey)
json.privkey = this.privkey.toJSON();
if (this.pubkey)
json.pubkey = this.pubkey.toJSON();
return json;
};
Keypair.prototype.fromPrivkey = function(privkey) {
this.privkey = privkey;
this.privkey2pubkey();
return this;
};
Keypair.prototype.fromRandom = function() {
this.privkey = Privkey().fromRandom();
11 years ago
this.privkey2pubkey();
return this;
11 years ago
};
Keypair.prototype.fromString = function(str) {
11 years ago
var obj = JSON.parse(str);
if (obj.privkey) {
11 years ago
this.privkey = new Privkey();
this.privkey.fromString(obj.privkey);
11 years ago
}
if (obj.pubkey) {
11 years ago
this.pubkey = new Pubkey();
this.pubkey.fromString(obj.pubkey);
11 years ago
}
};
Keypair.prototype.privkey2pubkey = function() {
this.pubkey = Pubkey().fromPrivkey(this.privkey);
11 years ago
};
Keypair.prototype.toString = function() {
11 years ago
var obj = {};
11 years ago
if (this.privkey)
obj.privkey = this.privkey.toString();
11 years ago
if (this.pubkey)
obj.pubkey = this.pubkey.toString();
11 years ago
return JSON.stringify(obj);
};
module.exports = Keypair;