working towards new AddressIndex obj

This commit is contained in:
Manuel Araoz 2014-06-04 12:28:26 -03:00
commit 88df346c51
3 changed files with 94 additions and 44 deletions

View file

@ -0,0 +1,79 @@
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var PrivateKey = require('./PrivateKey');
var Structure = require('./Structure');
var Address = bitcore.Address;
var Script = bitcore.Script;
var coinUtil = bitcore.util;
var Transaction = bitcore.Transaction
var util = bitcore.util;
function AddressIndex(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.changeIndex = opts.changeIndex || 0;
this.receiveIndex = opts.receiveIndex || 0;
}
AddressIndex.fromObj = function(data) {
if (data instanceof AddressIndex) {
throw new Error('bad data format: Did you use .toObj()?');
}
var ret = new AddressIndex(data);
return ret;
};
AddressIndex.prototype.toObj = function() {
return {
walletId: this.walletId,
changeIndex: this.changeIndex,
receiveIndex: this.receiveIndex,
};
};
AddressIndex.prototype._checkIndexRange = function(index, isChange) {
if ((isChange && index > this.changeIndex) ||
(!isChange && index > this.receiveIndex)) {
throw new Error('Out of bounds at index %d isChange: %d', index, isChange);
}
};
AddressIndex.prorotype.getChangeIndex = function() {
return this.changeIndex;
};
AddressIndex.prorotype.getReceiveIndex = function() {
return this.receiveIndex;
};
AddressIndex.prototype.increment = function(isChange) {
if (isChange) {
this.changeIndex++;
} else {
this.receiveIndex++;
}
};
AddressIndex.prototype.merge = function(inAddressIndex) {
var hasChanged = false;
// Indexes
if (inAddressIndex.changeIndex > this.changeIndex) {
this.changeIndex = inAddressIndex.changeIndex;
hasChanged = true;
}
if (inAddressIndex.receiveIndex > this.receiveIndex) {
this.receiveIndex = inAddressIndex.receiveIndex;
hasChanged = true;
}
return hasChanged;
};
module.exports = require('soop')(AddressIndex);

View file

@ -81,12 +81,12 @@ PrivateKey.prototype.get = function(index,isChange) {
return this.getForPath(path);
};
PrivateKey.prototype.getAll = function(addressIndex, changeAddressIndex) {
PrivateKey.prototype.getAll = function(receiveIndex, changeIndex) {
var ret = [];
for(var i=0;i<addressIndex; i++) {
for(var i=0;i<receiveIndex; i++) {
ret.push(this.get(i,false));
}
for(var i=0; i<changeAddressIndex; i++) {
for(var i=0; i<changeIndex; i++) {
ret.push(this.get(i,true));
}
return ret;

View file

@ -6,7 +6,8 @@ var imports = require('soop').imports();
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var PrivateKey = require('./PrivateKey');
var Structure = require('./Structure');
var Structure = require('./Structure');
var AddressIndex= require('./AddressIndex');
var Address = bitcore.Address;
var Script = bitcore.Script;
var coinUtil = bitcore.util;
@ -26,8 +27,7 @@ function PublicKeyRing(opts) {
this.copayersHK = opts.copayersHK || [];
this.changeAddressIndex= opts.changeAddressIndex || 0;
this.addressIndex= opts.addressIndex || 0;
this.indexes = opts.indexes || new ;
this.publicKeysCache = opts.publicKeysCache || {};
this.nicknameFor = opts.nicknameFor || {};
@ -54,9 +54,8 @@ PublicKeyRing.prototype.toObj = function() {
networkName: this.network.name,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
indexes: this.indexes.toObj(),
changeAddressIndex: this.changeAddressIndex,
addressIndex: this.addressIndex,
copayersExtPubKeys: this.copayersHK.map( function (b) {
return b.extendedPublicKeyString();
}),
@ -158,13 +157,6 @@ PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
return pubKeys;
};
PublicKeyRing.prototype._checkIndexRange = function (index, isChange) {
if ( (isChange && index > this.changeAddressIndex) ||
(!isChange && index > this.addressIndex)) {
throw new Error('Out of bounds at getAddress: Index %d isChange: %d', index, isChange);
}
};
// TODO this could be cached
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
this._checkIndexRange(index, isChange);
@ -197,14 +189,9 @@ PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
//generate a new address, update index.
PublicKeyRing.prototype.generateAddress = function(isChange) {
var ret =
this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange);
if (isChange) {
this.changeAddressIndex++;
} else {
this.addressIndex++;
}
var index = isChange ? this.indexes.getChangeIndex() : this.indexes.getReceiveIndex();
var ret = this.getAddress(index, isChange);
this.indexes.increment(isChange);
return ret;
};
@ -219,7 +206,7 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
var ret = [];
if (!opts.excludeChange) {
for (var i=0; i<this.changeAddressIndex; i++) {
for (var i=0; i<this.indexes.getChangeIndex(); i++) {
ret.unshift({
address: this.getAddress(i,true),
isChange: true
@ -228,7 +215,7 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
}
if (!opts.excludeMain) {
for (var i=0; i<this.addressIndex; i++) {
for (var i=0; i<this.indexes.getReceiveIndex(); i++) {
ret.unshift({
address: this.getAddress(i,false),
isChange: false
@ -248,10 +235,10 @@ PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) {
PublicKeyRing.prototype.getRedeemScriptMap = function () {
var ret = {};
for (var i=0; i<this.changeAddressIndex; i++) {
for (var i=0; i<this.indexes.getChangeIndex(); i++) {
this._addScriptMap(ret,i,true);
}
for (var i=0; i<this.addressIndex; i++) {
for (var i=0; i<this.indexes.getReceiveIndex(); i++) {
this._addScriptMap(ret,i,false);
}
return ret;
@ -278,22 +265,6 @@ PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
};
PublicKeyRing.prototype._mergeIndexes = function(inPKR) {
var hasChanged = false;
// Indexes
if (inPKR.changeAddressIndex > this.changeAddressIndex) {
this.changeAddressIndex = inPKR.changeAddressIndex;
hasChanged = true;
}
if (inPKR.addressIndex > this.addressIndex) {
this.addressIndex = inPKR.addressIndex;
hasChanged = true;
}
return hasChanged;
};
PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
var self = this;
var hasChanged = false;
@ -330,7 +301,7 @@ PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
this._checkInPRK(inPKR, ignoreId);
if (this._mergeIndexes(inPKR))
if (this.indexes.merge(inPKR.indexes))
hasChanged = true;
if (this._mergePubkeys(inPKR))