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.

56 lines
1.3 KiB

var Address = require('../lib/address');
11 years ago
var Privkey = require('./privkey');
var Pubkey = require('./pubkey');
var BN = require('./bn');
11 years ago
var point = require('./point');
11 years ago
var Key = function Key(obj) {
if (!(this instanceof Key))
11 years ago
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;
11 years ago
};
Key.prototype.fromRandom = function() {
this.privkey = Privkey().fromRandom();
11 years ago
this.privkey2pubkey();
return this;
11 years ago
};
Key.prototype.fromString = function(str) {
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
}
};
Key.prototype.getAddress = function(networkstr) {
return Address().fromPubkey(this.pubkey, networkstr);
};
11 years ago
Key.prototype.privkey2pubkey = function() {
this.pubkey = Pubkey().fromPrivkey(this.privkey);
11 years ago
};
Key.prototype.toString = function() {
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 = Key;