diff --git a/js/models/core/AddressIndex.js b/js/models/core/AddressIndex.js new file mode 100644 index 000000000..b5a4aab68 --- /dev/null +++ b/js/models/core/AddressIndex.js @@ -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); diff --git a/js/models/core/PrivateKey.js b/js/models/core/PrivateKey.js index cc2a4d715..d324f1831 100644 --- a/js/models/core/PrivateKey.js +++ b/js/models/core/PrivateKey.js @@ -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 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) { - 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))