Wallet/js/models/core/PrivateKey.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-04-09 20:37:14 -03:00
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
2014-05-29 17:18:55 -03:00
var HK = bitcore.HierarchicalKey;
2014-04-09 20:37:14 -03:00
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
2014-04-10 02:16:57 -03:00
var util = bitcore.util;
2014-05-29 17:18:55 -03:00
var Structure = require('./Structure');
2014-04-09 20:37:14 -03:00
function PrivateKey(opts) {
2014-04-17 17:01:31 -03:00
opts = opts || {};
2014-04-09 20:37:14 -03:00
this.network = opts.networkName === 'testnet' ?
networks.testnet : networks.livenet;
var init = opts.extendedPrivateKeyString || this.network.name;
2014-05-14 20:55:34 -03:00
this.bip = opts.HK || new HK(init);
2014-04-17 17:01:31 -03:00
this.privateKeyCache = opts.privateKeyCache || {};
2014-04-09 20:37:14 -03:00
};
2014-04-23 22:43:17 -03:00
PrivateKey.prototype.getId = function() {
if (!this.id) {
2014-05-29 17:18:55 -03:00
var path = Structure.IdFullBranch;
var idhk = this.bip.derive(path);
this.id= idhk.eckey.public.toString('hex');
2014-04-18 14:40:16 -03:00
}
2014-04-23 22:43:17 -03:00
return this.id;
};
PrivateKey.prototype.deriveBIP45Branch = function() {
if (!this.bip45Branch) {
2014-05-29 17:18:55 -03:00
this.bip45Branch = this.bip.derive(Structure.BIP45_PUBLIC_PREFIX);
}
return this.bip45Branch;
}
2014-04-17 17:01:31 -03:00
PrivateKey.fromObj = function(obj) {
return new PrivateKey(obj);
2014-04-10 02:16:57 -03:00
};
PrivateKey.prototype.toObj = function() {
return {
2014-04-17 17:01:31 -03:00
extendedPrivateKeyString: this.getExtendedPrivateKeyString(),
2014-04-10 02:16:57 -03:00
networkName: this.network.name,
2014-04-17 17:01:31 -03:00
privateKeyCache: this.privateKeyCache
2014-04-10 02:16:57 -03:00
};
};
2014-04-17 17:01:31 -03:00
PrivateKey.prototype.getExtendedPublicKeyString = function() {
return this.bip.extendedPublicKeyString();
};
PrivateKey.prototype.getExtendedPrivateKeyString = function() {
return this.bip.extendedPrivateKeyString();
};
2014-05-14 20:55:34 -03:00
PrivateKey.prototype._getHK = function(path) {
2014-04-17 17:01:31 -03:00
if (typeof path === 'undefined') {
return this.bip;
}
return this.bip.derive(path);
};
2014-04-09 20:37:14 -03:00
PrivateKey.prototype.get = function(index,isChange) {
2014-05-29 17:18:55 -03:00
var path = Structure.FullBranch(index, isChange);
2014-04-17 17:01:31 -03:00
var pk = this.privateKeyCache[path];
if (!pk) {
2014-05-14 20:55:34 -03:00
var derivedHK = this._getHK(path);
pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex');
2014-04-17 17:01:31 -03:00
}
2014-04-09 20:37:14 -03:00
var wk = new WalletKey({network: this.network});
2014-04-17 17:01:31 -03:00
wk.fromObj({priv: pk});
2014-04-09 20:37:14 -03:00
return wk;
};
2014-04-09 23:04:22 -03:00
PrivateKey.prototype.getAll = function(addressIndex, changeAddressIndex) {
var ret = [];
for(var i=0;i<addressIndex; i++) {
ret.push(this.get(i,false));
}
for(var i=0; i<changeAddressIndex; i++) {
ret.push(this.get(i,true));
}
return ret;
};
2014-04-09 20:37:14 -03:00
module.exports = require('soop')(PrivateKey);