copay/js/models/core/PrivateKey.js

135 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-04-09 16:37:14 -07:00
'use strict';
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
var util = bitcore.util;
2014-07-29 07:23:58 -07:00
var HDPath = require('./HDPath');
2014-04-09 16:37:14 -07:00
function PrivateKey(opts) {
2014-04-17 13:01:31 -07:00
opts = opts || {};
this.network = opts.networkName === 'testnet' ?
2014-04-09 16:37:14 -07:00
networks.testnet : networks.livenet;
var init = opts.extendedPrivateKeyString || this.network.name;
this.bip = new HK(init);
this.privateKeyCache = {};
this.publicHex = this.deriveBIP45Branch().eckey.public.toString('hex');
2014-04-09 16:37:14 -07:00
};
2014-04-23 18:43:17 -07:00
PrivateKey.prototype.getId = function() {
if (!this.id) {
this.cacheId();
2014-04-18 10:40:16 -07:00
}
2014-04-23 18:43:17 -07:00
return this.id;
};
PrivateKey.prototype.getIdPriv = function() {
if (!this.idpriv) {
this.cacheId();
}
return this.idpriv;
};
PrivateKey.prototype.getIdKey = function() {
if (!this.idkey) {
this.cacheId();
}
return this.idkey;
};
PrivateKey.prototype.cacheId = function() {
2014-07-29 07:23:58 -07:00
var path = HDPath.IdFullBranch;
var idhk = this.bip.derive(path);
this.idkey = idhk.eckey;
this.id = idhk.eckey.public.toString('hex');
this.idpriv = idhk.eckey.private.toString('hex');
};
PrivateKey.prototype.deriveBIP45Branch = function() {
if (!this.bip45Branch) {
2014-07-29 07:23:58 -07:00
this.bip45Branch = this.bip.derive(HDPath.BIP45_PUBLIC_PREFIX);
}
return this.bip45Branch;
}
PrivateKey.trim = function(data) {
var opts = {};
['networkName', 'extendedPrivateKeyString'].forEach(function(k){
opts[k] = data[k];
});
return opts;
};
2014-04-17 13:01:31 -07:00
PrivateKey.fromObj = function(obj) {
return new PrivateKey(PrivateKey.trim(obj));
2014-04-09 22:16:57 -07:00
};
PrivateKey.prototype.toObj = function() {
return {
2014-04-17 13:01:31 -07:00
extendedPrivateKeyString: this.getExtendedPrivateKeyString(),
2014-04-09 22:16:57 -07:00
networkName: this.network.name,
};
};
2014-04-17 13:01:31 -07:00
PrivateKey.prototype.getExtendedPublicKeyString = function() {
return this.bip.extendedPublicKeyString();
};
PrivateKey.prototype.getExtendedPrivateKeyString = function() {
return this.bip.extendedPrivateKeyString();
};
2014-05-14 16:55:34 -07:00
PrivateKey.prototype._getHK = function(path) {
2014-04-17 13:01:31 -07:00
if (typeof path === 'undefined') {
return this.bip;
}
2014-06-12 10:27:53 -07:00
var ret = this.bip.derive(path);
return ret;
2014-04-17 13:01:31 -07:00
};
2014-05-30 11:07:52 -07:00
PrivateKey.prototype.getForPaths = function(paths) {
return paths.map(this.getForPath.bind(this));
};
PrivateKey.prototype.getForPath = function(path) {
2014-04-17 13:01:31 -07:00
var pk = this.privateKeyCache[path];
if (!pk) {
var derivedHK = this._getHK(path);
2014-05-14 16:55:34 -07:00
pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex');
2014-04-17 13:01:31 -07:00
}
var wk = new WalletKey({
network: this.network
});
wk.fromObj({
priv: pk
});
2014-04-09 16:37:14 -07:00
return wk;
};
PrivateKey.prototype.get = function(index, isChange, cosigner) {
2014-07-29 07:23:58 -07:00
var path = HDPath.FullBranch(index, isChange, cosigner);
2014-05-30 11:07:52 -07:00
return this.getForPath(path);
};
PrivateKey.prototype.getAll = function(receiveIndex, changeIndex, cosigner) {
2014-06-04 09:44:32 -07:00
if (typeof receiveIndex === 'undefined' || typeof changeIndex === 'undefined')
throw new Error('Invalid parameters');
2014-04-09 19:04:22 -07:00
var ret = [];
for (var i = 0; i < receiveIndex; i++) {
ret.push(this.get(i, false, cosigner));
2014-04-09 19:04:22 -07:00
}
for (var i = 0; i < changeIndex; i++) {
ret.push(this.get(i, true, cosigner));
2014-04-09 19:04:22 -07:00
}
return ret;
};
2014-07-29 07:23:58 -07:00
module.exports = PrivateKey;