differentiate change addresses from regular addresses
This commit is contained in:
parent
de5772112f
commit
6e2ade54bc
2 changed files with 64 additions and 27 deletions
|
|
@ -2916,44 +2916,80 @@ Wallet.prototype.read_Old = function(walletId, skipFields, cb) {
|
||||||
Wallet.prototype.getTransactionHistory = function(cb) {
|
Wallet.prototype.getTransactionHistory = function(cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
var addresses = self.getAddressesInfo();
|
||||||
var satToUnit = 1 / self.settings.unitToSatoshi;
|
var satToUnit = 1 / self.settings.unitToSatoshi;
|
||||||
var addresses = _.pluck(self.getAddressesInfo(), 'addressStr');
|
|
||||||
|
|
||||||
if (addresses.length == 0) return cb();
|
function extractInsOuts(tx) {
|
||||||
|
// Inputs
|
||||||
function computeAmountIn(items) {
|
var inputs = _.map(tx.vin, function(item) {
|
||||||
var mine = _.filter(items, function(item) {
|
var addr = _.findWhere(addresses, {
|
||||||
return _.contains(addresses, item.addr);
|
addressStr: item.addr
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
type: 'in',
|
||||||
|
address: addr ? addr.addressStr : item.addr,
|
||||||
|
isMine: !_.isUndefined(addr),
|
||||||
|
isChange: addr ? !!addr.isChange : false,
|
||||||
|
amountSat: item.valueSat,
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return _.reduce(mine, function(memo, item) {
|
var outputs = _.map(tx.vout, function(item) {
|
||||||
return memo + item.valueSat;
|
var addr;
|
||||||
}, 0);
|
var itemAddr;
|
||||||
|
// If classic multisig, ignore
|
||||||
|
if (item.scriptPubKey && item.scriptPubKey.addresses.length == 1) {
|
||||||
|
itemAddr = item.scriptPubKey.addresses[0];
|
||||||
|
addr = _.findWhere(addresses, {
|
||||||
|
addressStr: itemAddr,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
type: 'out',
|
||||||
|
address: addr ? addr : itemAddr,
|
||||||
|
isMine: !_.isUndefined(addr),
|
||||||
|
isChange: addr ? !!addr.isChange : false,
|
||||||
|
amountSat: parseInt((item.value * bitcore.util.COIN).toFixed(0)),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return inputs.concat(outputs);
|
||||||
};
|
};
|
||||||
|
|
||||||
function computeAmountOut(items) {
|
function sum(items, filter) {
|
||||||
var mine = _.filter(items, function(item) {
|
return _.reduce(_.where(items, filter),
|
||||||
if (!item.scriptPubKey) return false;
|
function(memo, item) {
|
||||||
|
return memo + item.amountSat;
|
||||||
// If classic multisig, ignore
|
}, 0);
|
||||||
if (item.scriptPubKey.addresses.length > 1) return false;
|
|
||||||
return _.contains(addresses, item.scriptPubKey.addresses[0]);
|
|
||||||
});
|
|
||||||
return _.reduce(mine, function(memo, item) {
|
|
||||||
return memo + parseInt((item.value * bitcore.util.COIN).toFixed(0));
|
|
||||||
}, 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function decorateTx(tx) {
|
function decorateTx(tx) {
|
||||||
var amountIn = computeAmountIn(tx.vin);
|
var items = extractInsOuts(tx);
|
||||||
var amountOut = computeAmountOut(tx.vout);
|
|
||||||
|
var amountIn = sum(items, {
|
||||||
|
type: 'in',
|
||||||
|
isMine: true
|
||||||
|
});
|
||||||
|
|
||||||
|
var amountOut = sum(items, {
|
||||||
|
type: 'out',
|
||||||
|
isMine: true,
|
||||||
|
isChange: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
var amountOutChange = sum(items, {
|
||||||
|
type: 'out',
|
||||||
|
isMine: true,
|
||||||
|
isChange: true,
|
||||||
|
});
|
||||||
|
|
||||||
var fees = parseInt((tx.fees * bitcore.util.COIN).toFixed(0));
|
var fees = parseInt((tx.fees * bitcore.util.COIN).toFixed(0));
|
||||||
var amount;
|
var amount;
|
||||||
if (amountIn == (amountOut + (amountIn > 0 ? fees : 0))) {
|
if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? fees : 0))) {
|
||||||
tx.action = 'moved';
|
tx.action = 'moved';
|
||||||
// TODO: subtract amount from change addresses
|
// TODO: subtract amount from change addresses
|
||||||
amount = amountOut;
|
amount = amountOut;
|
||||||
} else {
|
} else {
|
||||||
amount = amountIn - amountOut - (amountIn > 0 ? fees : 0);
|
amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? fees : 0);
|
||||||
tx.action = amount > 0 ? 'sent' : 'received';
|
tx.action = amount > 0 ? 'sent' : 'received';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1990,7 +1990,6 @@ describe('Wallet model', function() {
|
||||||
w.getTransactionHistory(function(err, res) {
|
w.getTransactionHistory(function(err, res) {
|
||||||
res.should.exist;
|
res.should.exist;
|
||||||
res.length.should.equal(3);
|
res.length.should.equal(3);
|
||||||
console.log('res', res);
|
|
||||||
res[0].action.should.equal('sent');
|
res[0].action.should.equal('sent');
|
||||||
res[0].amountSat.should.equal(900);
|
res[0].amountSat.should.equal(900);
|
||||||
res[1].action.should.equal('received');
|
res[1].action.should.equal('received');
|
||||||
|
|
@ -2030,7 +2029,8 @@ describe('Wallet model', function() {
|
||||||
}, {
|
}, {
|
||||||
addressStr: 'addr_in_2'
|
addressStr: 'addr_in_2'
|
||||||
}, {
|
}, {
|
||||||
addressStr: 'change'
|
addressStr: 'change',
|
||||||
|
isChange: true,
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
w.getTransactionHistory(function(err, res) {
|
w.getTransactionHistory(function(err, res) {
|
||||||
|
|
@ -2070,7 +2070,8 @@ describe('Wallet model', function() {
|
||||||
}, {
|
}, {
|
||||||
addressStr: 'addr_2'
|
addressStr: 'addr_2'
|
||||||
}, {
|
}, {
|
||||||
addressStr: 'change'
|
addressStr: 'change',
|
||||||
|
isChange: true,
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
w.getTransactionHistory(function(err, res) {
|
w.getTransactionHistory(function(err, res) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue