2014-04-09 16:37:14 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
var imports = require('soop').imports();
|
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var BIP32 = bitcore.BIP32;
|
|
|
|
var WalletKey = bitcore.WalletKey;
|
|
|
|
var networks = bitcore.networks;
|
2014-04-09 22:16:57 -07:00
|
|
|
var util = bitcore.util;
|
2014-04-09 16:37:14 -07:00
|
|
|
var PublicKeyRing = require('./PublicKeyRing');
|
|
|
|
|
|
|
|
function PrivateKey(opts) {
|
2014-04-17 13:01:31 -07:00
|
|
|
opts = opts || {};
|
2014-04-09 16:37:14 -07:00
|
|
|
this.network = opts.networkName === 'testnet' ?
|
|
|
|
networks.testnet : networks.livenet;
|
2014-04-11 09:26:36 -07:00
|
|
|
var init = opts.extendedPrivateKeyString || this.network.name;
|
2014-04-17 13:01:31 -07:00
|
|
|
this.bip = opts.BIP32 || new BIP32(init);
|
|
|
|
this.privateKeyCache = opts.privateKeyCache || {};
|
2014-04-09 16:37:14 -07:00
|
|
|
};
|
|
|
|
|
2014-04-18 10:40:16 -07:00
|
|
|
PrivateKey.prototype.getId = function(prefix) {
|
|
|
|
var buf = this.bip.extendedPublicKey;
|
|
|
|
if (prefix) {
|
2014-04-18 14:25:51 -07:00
|
|
|
buf = Buffer.concat([prefix, buf]);
|
2014-04-18 10:40:16 -07:00
|
|
|
}
|
2014-04-21 08:10:34 -07:00
|
|
|
var hash = util.sha256(buf).toString('hex');
|
|
|
|
return hash.substring(0, hash.length/2);
|
2014-04-09 22:16:57 -07:00
|
|
|
};
|
2014-04-09 16:37:14 -07:00
|
|
|
|
2014-04-17 13:01:31 -07:00
|
|
|
PrivateKey.fromObj = function(obj) {
|
|
|
|
return new PrivateKey(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
|
|
|
privateKeyCache: this.privateKeyCache
|
2014-04-09 22:16:57 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-04-17 13:01:31 -07:00
|
|
|
PrivateKey.prototype.getExtendedPublicKeyString = function() {
|
|
|
|
return this.bip.extendedPublicKeyString();
|
|
|
|
};
|
|
|
|
|
|
|
|
PrivateKey.prototype.getExtendedPrivateKeyString = function() {
|
|
|
|
return this.bip.extendedPrivateKeyString();
|
|
|
|
};
|
|
|
|
|
|
|
|
PrivateKey.prototype._getBIP32 = function(path) {
|
|
|
|
if (typeof path === 'undefined') {
|
|
|
|
return this.bip;
|
|
|
|
}
|
|
|
|
return this.bip.derive(path);
|
|
|
|
};
|
|
|
|
|
2014-04-09 16:37:14 -07:00
|
|
|
PrivateKey.prototype.get = function(index,isChange) {
|
2014-04-17 13:01:31 -07:00
|
|
|
var path = PublicKeyRing.Branch(index, isChange);
|
|
|
|
var pk = this.privateKeyCache[path];
|
|
|
|
if (!pk) {
|
|
|
|
var derivedBIP32 = this._getBIP32(path);
|
|
|
|
pk = this.privateKeyCache[path] = derivedBIP32.eckey.private.toString('hex');
|
|
|
|
} else {
|
|
|
|
//console.log('cache hit!');
|
|
|
|
}
|
2014-04-09 16:37:14 -07:00
|
|
|
var wk = new WalletKey({network: this.network});
|
2014-04-17 13:01:31 -07:00
|
|
|
wk.fromObj({priv: pk});
|
2014-04-09 16:37:14 -07:00
|
|
|
return wk;
|
|
|
|
};
|
|
|
|
|
2014-04-09 19:04:22 -07: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 16:37:14 -07:00
|
|
|
module.exports = require('soop')(PrivateKey);
|