"Keypair" is a more explanatory name, and also should be less confused with other kinds of keys (particularly "cipher keys", which are the keys used in symmetric block ciphers, especially AES).
62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
var Address = require('../lib/address');
|
|
var Privkey = require('./privkey');
|
|
var Pubkey = require('./pubkey');
|
|
var BN = require('./bn');
|
|
var point = require('./point');
|
|
|
|
var Key = function Key(obj) {
|
|
if (!(this instanceof Key))
|
|
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;
|
|
};
|
|
|
|
Key.prototype.fromPrivkey = function(privkey) {
|
|
this.privkey = privkey;
|
|
this.privkey2pubkey();
|
|
return this;
|
|
};
|
|
|
|
Key.prototype.fromRandom = function() {
|
|
this.privkey = Privkey().fromRandom();
|
|
this.privkey2pubkey();
|
|
return this;
|
|
};
|
|
|
|
Key.prototype.fromString = function(str) {
|
|
var obj = JSON.parse(str);
|
|
if (obj.privkey) {
|
|
this.privkey = new Privkey();
|
|
this.privkey.fromString(obj.privkey);
|
|
}
|
|
if (obj.pubkey) {
|
|
this.pubkey = new Pubkey();
|
|
this.pubkey.fromString(obj.pubkey);
|
|
}
|
|
};
|
|
|
|
Key.prototype.getAddress = function(networkstr) {
|
|
return Address().fromPubkey(this.pubkey, networkstr);
|
|
};
|
|
|
|
Key.prototype.privkey2pubkey = function() {
|
|
this.pubkey = Pubkey().fromPrivkey(this.privkey);
|
|
};
|
|
|
|
Key.prototype.toString = function() {
|
|
var obj = {};
|
|
if (this.privkey)
|
|
obj.privkey = this.privkey.toString();
|
|
if (this.pubkey)
|
|
obj.pubkey = this.pubkey.toString();
|
|
return JSON.stringify(obj);
|
|
};
|
|
|
|
module.exports = Key;
|