working towards new AddressIndex obj
This commit is contained in:
parent
cf5e61cfcb
commit
88df346c51
3 changed files with 94 additions and 44 deletions
79
js/models/core/AddressIndex.js
Normal file
79
js/models/core/AddressIndex.js
Normal 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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue