change name to bitcore.HierarchicalKey

This commit is contained in:
Matias Alejo Garcia 2014-05-14 20:55:34 -03:00
commit 432f74488a
2 changed files with 24 additions and 24 deletions

View file

@ -3,7 +3,7 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32; var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey; var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks; var networks = bitcore.networks;
var util = bitcore.util; var util = bitcore.util;
@ -14,7 +14,7 @@ function PrivateKey(opts) {
this.network = opts.networkName === 'testnet' ? this.network = opts.networkName === 'testnet' ?
networks.testnet : networks.livenet; networks.testnet : networks.livenet;
var init = opts.extendedPrivateKeyString || this.network.name; var init = opts.extendedPrivateKeyString || this.network.name;
this.bip = opts.BIP32 || new BIP32(init); this.bip = opts.HK || new HK(init);
this.privateKeyCache = opts.privateKeyCache || {}; this.privateKeyCache = opts.privateKeyCache || {};
}; };
@ -47,7 +47,7 @@ PrivateKey.prototype.getExtendedPrivateKeyString = function() {
return this.bip.extendedPrivateKeyString(); return this.bip.extendedPrivateKeyString();
}; };
PrivateKey.prototype._getBIP32 = function(path) { PrivateKey.prototype._getHK = function(path) {
if (typeof path === 'undefined') { if (typeof path === 'undefined') {
return this.bip; return this.bip;
} }
@ -58,8 +58,8 @@ PrivateKey.prototype.get = function(index,isChange) {
var path = PublicKeyRing.Branch(index, isChange); var path = PublicKeyRing.Branch(index, isChange);
var pk = this.privateKeyCache[path]; var pk = this.privateKeyCache[path];
if (!pk) { if (!pk) {
var derivedBIP32 = this._getBIP32(path); var derivedHK = this._getHK(path);
pk = this.privateKeyCache[path] = derivedBIP32.eckey.private.toString('hex'); pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex');
} else { } else {
//console.log('cache hit!'); //console.log('cache hit!');
} }

View file

@ -4,7 +4,7 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32; var HK = bitcore.HierarchicalKey;
var Address = bitcore.Address; var Address = bitcore.Address;
var Script = bitcore.Script; var Script = bitcore.Script;
var coinUtil = bitcore.util; var coinUtil = bitcore.util;
@ -26,7 +26,7 @@ function PublicKeyRing(opts) {
this.requiredCopayers = opts.requiredCopayers || 3; this.requiredCopayers = opts.requiredCopayers || 3;
this.totalCopayers = opts.totalCopayers || 5; this.totalCopayers = opts.totalCopayers || 5;
this.copayersBIP32 = opts.copayersBIP32 || []; this.copayersHK = opts.copayersHK || [];
this.changeAddressIndex= opts.changeAddressIndex || 0; this.changeAddressIndex= opts.changeAddressIndex || 0;
this.addressIndex= opts.addressIndex || 0; this.addressIndex= opts.addressIndex || 0;
@ -74,7 +74,7 @@ PublicKeyRing.prototype.toObj = function() {
changeAddressIndex: this.changeAddressIndex, changeAddressIndex: this.changeAddressIndex,
addressIndex: this.addressIndex, addressIndex: this.addressIndex,
copayersExtPubKeys: this.copayersBIP32.map( function (b) { copayersExtPubKeys: this.copayersHK.map( function (b) {
return b.extendedPublicKeyString(); return b.extendedPublicKeyString();
}), }),
nicknameFor: this.nicknameFor, nicknameFor: this.nicknameFor,
@ -87,7 +87,7 @@ PublicKeyRing.prototype.getCopayerId = function(i) {
}; };
PublicKeyRing.prototype.registeredCopayers = function () { PublicKeyRing.prototype.registeredCopayers = function () {
return this.copayersBIP32.length; return this.copayersHK.length;
}; };
PublicKeyRing.prototype.isComplete = function () { PublicKeyRing.prototype.isComplete = function () {
@ -109,14 +109,14 @@ PublicKeyRing.prototype._checkKeys = function() {
}; };
PublicKeyRing.prototype._newExtendedPublicKey = function () { PublicKeyRing.prototype._newExtendedPublicKey = function () {
return new BIP32(this.network.name) return new HK(this.network.name)
.extendedPublicKeyString(); .extendedPublicKeyString();
}; };
PublicKeyRing.prototype._updateBip = function (index) { PublicKeyRing.prototype._updateBip = function (index) {
var path = PublicKeyRing.ID_BRANCH; var path = PublicKeyRing.ID_BRANCH;
var bip32 = this.copayersBIP32[index].derive(path); var hk = this.copayersHK[index].derive(path);
this.copayerIds[index]= bip32.eckey.public.toString('hex'); this.copayerIds[index]= hk.eckey.public.toString('hex');
}; };
PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) { PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) {
@ -139,14 +139,14 @@ PublicKeyRing.prototype.addCopayer = function (newEpk, nickname) {
newEpk = this._newExtendedPublicKey(); newEpk = this._newExtendedPublicKey();
} }
this.copayersBIP32.forEach(function(b){ this.copayersHK.forEach(function(b){
if (b.extendedPublicKeyString() === newEpk) if (b.extendedPublicKeyString() === newEpk)
throw new Error('already have that key'); throw new Error('already have that key');
}); });
var i=this.copayersBIP32.length; var i=this.copayersHK.length;
var bip = new BIP32(newEpk); var bip = new HK(newEpk);
this.copayersBIP32.push(bip); this.copayersHK.push(bip);
this._updateBip(i); this._updateBip(i);
if (nickname) { if (nickname) {
this._setNicknameForIndex(i,nickname); this._setNicknameForIndex(i,nickname);
@ -161,10 +161,10 @@ PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
var pubKeys = this.publicKeysCache[path]; var pubKeys = this.publicKeysCache[path];
if (!pubKeys) { if (!pubKeys) {
pubKeys = []; pubKeys = [];
var l = this.copayersBIP32.length; var l = this.copayersHK.length;
for(var i=0; i<l; i++) { for(var i=0; i<l; i++) {
var bip32 = this.copayersBIP32[i].derive(path); var hk = this.copayersHK[i].derive(path);
pubKeys[i] = bip32.eckey.public; pubKeys[i] = hk.eckey.public;
} }
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');}); this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');});
} }
@ -307,15 +307,15 @@ PublicKeyRing.prototype._mergeIndexes = function(inPKR) {
PublicKeyRing.prototype._mergePubkeys = function(inPKR) { PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
var self = this; var self = this;
var hasChanged = false; var hasChanged = false;
var l= self.copayersBIP32.length; var l= self.copayersHK.length;
if (self.isComplete()) if (self.isComplete())
return; return;
inPKR.copayersBIP32.forEach( function(b) { inPKR.copayersHK.forEach( function(b) {
var haveIt = false; var haveIt = false;
var epk = b.extendedPublicKeyString(); var epk = b.extendedPublicKeyString();
for(var j=0; j<l; j++) { for(var j=0; j<l; j++) {
if (self.copayersBIP32[j].extendedPublicKeyString() === epk) { if (self.copayersHK[j].extendedPublicKeyString() === epk) {
haveIt=true; haveIt=true;
break; break;
} }
@ -324,8 +324,8 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
if (self.isComplete()) { if (self.isComplete()) {
throw new Error('trying to add more pubkeys, when PKR isComplete at merge'); throw new Error('trying to add more pubkeys, when PKR isComplete at merge');
} }
var l2 = self.copayersBIP32.length; var l2 = self.copayersHK.length;
self.copayersBIP32.push(new BIP32(epk)); self.copayersHK.push(new HK(epk));
self._updateBip(l2); self._updateBip(l2);
if (inPKR.nicknameFor[self.getCopayerId(l2)]) if (inPKR.nicknameFor[self.getCopayerId(l2)])
self._setNicknameForIndex(l2,inPKR.nicknameFor[self.getCopayerId(l2)]); self._setNicknameForIndex(l2,inPKR.nicknameFor[self.getCopayerId(l2)]);