Wallet/js/models/core/AddressIndex.js

99 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-06-04 12:28:26 -03:00
'use strict';
var imports = require('soop').imports();
2014-06-27 15:06:39 -03:00
var preconditions = require('preconditions').singleton();
var Structure = require('./Structure');
2014-06-04 12:28:26 -03:00
function AddressIndex(opts) {
opts = opts || {};
2014-07-01 12:49:50 -03:00
this.cosigner = opts.cosigner
2014-06-04 12:28:26 -03:00
this.changeIndex = opts.changeIndex || 0;
this.receiveIndex = opts.receiveIndex || 0;
2014-07-01 12:49:50 -03:00
if (typeof this.cosigner === 'undefined') {
this.cosigner = Structure.SHARED_INDEX;
}
}
AddressIndex.init = function(totalCopayers) {
preconditions.shouldBeNumber(totalCopayers);
var indexes = [new AddressIndex()];
for (var i = 0 ; i < totalCopayers ; i++) {
indexes.push(new AddressIndex({cosigner: i}));
}
return indexes;
2014-06-04 12:28:26 -03:00
}
AddressIndex.fromList = function(indexes) {
return indexes.map(function(i) { return AddressIndex.fromObj(i); });
}
2014-06-04 12:28:26 -03:00
AddressIndex.fromObj = function(data) {
if (data instanceof AddressIndex) {
throw new Error('bad data format: Did you use .toObj()?');
}
return new AddressIndex(data);
2014-06-04 12:28:26 -03:00
};
AddressIndex.serialize = function(indexes) {
return indexes.map(function(i) { return i.toObj(); });
}
AddressIndex.update = function(shared, totalCopayers) {
var indexes = this.init(totalCopayers);
indexes[0].changeIndex = shared.changeIndex;
indexes[0].receiveIndex = shared.receiveIndex;
return this.serialize(indexes);
};
2014-06-04 12:28:26 -03:00
AddressIndex.prototype.toObj = function() {
return {
2014-06-27 15:06:39 -03:00
cosigner: this.cosigner,
2014-06-04 12:28:26 -03:00
changeIndex: this.changeIndex,
2014-06-27 15:06:39 -03:00
receiveIndex: this.receiveIndex
2014-06-04 12:28:26 -03:00
};
};
2014-06-04 13:44:32 -03:00
AddressIndex.prototype.checkRange = function(index, isChange) {
2014-06-04 12:28:26 -03:00
if ((isChange && index > this.changeIndex) ||
(!isChange && index > this.receiveIndex)) {
throw new Error('Out of bounds at index ' + index + ' isChange: ' + isChange);
2014-06-04 12:28:26 -03:00
}
};
2014-06-04 13:44:32 -03:00
AddressIndex.prototype.getChangeIndex = function() {
2014-06-04 12:28:26 -03:00
return this.changeIndex;
};
2014-06-27 15:06:39 -03:00
2014-06-04 13:44:32 -03:00
AddressIndex.prototype.getReceiveIndex = function() {
2014-06-04 12:28:26 -03:00
return this.receiveIndex;
};
AddressIndex.prototype.increment = function(isChange) {
if (isChange) {
this.changeIndex++;
} else {
this.receiveIndex++;
}
};
AddressIndex.prototype.merge = function(inAddressIndex) {
2014-06-27 15:06:39 -03:00
preconditions.shouldBeObject(inAddressIndex)
.checkArgument(this.cosigner == inAddressIndex.cosigner);
2014-06-04 12:28:26 -03:00
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);