retrieving label from address book

This commit is contained in:
Ivan Socolsky 2014-10-23 11:38:56 -03:00 committed by Matias Alejo Garcia
commit b6f5b342f2
3 changed files with 54 additions and 11 deletions

View file

@ -82,16 +82,7 @@ angular.module('copayApp.controllers').controller('SendController',
} }
$scope.showAddressBook = function() { $scope.showAddressBook = function() {
var flag; return w && _.keys(w.addressBook).length > 0;
if (w) {
for (var k in w.addressBook) {
if (w.addressBook[k]) {
flag = true;
break;
}
}
}
return flag;
}; };
if ($rootScope.pendingPayment) { if ($rootScope.pendingPayment) {

View file

@ -2943,11 +2943,13 @@ Wallet.prototype.getTransactionHistory = function(cb) {
addressStr: itemAddr, addressStr: itemAddr,
}); });
} }
return { return {
type: 'out', type: 'out',
address: addr ? addr : itemAddr, address: addr ? addr : itemAddr,
isMine: !_.isUndefined(addr), isMine: !_.isUndefined(addr),
isChange: addr ? !!addr.isChange : false, isChange: addr ? !!addr.isChange : false,
label: self.addressBook[itemAddr] ? self.addressBook[itemAddr].label : undefined,
amountSat: parseInt((item.value * bitcore.util.COIN).toFixed(0)), amountSat: parseInt((item.value * bitcore.util.COIN).toFixed(0)),
} }
}); });
@ -2986,13 +2988,16 @@ Wallet.prototype.getTransactionHistory = function(cb) {
var amount; var amount;
if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? fees : 0))) { if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? fees : 0))) {
tx.action = 'moved'; tx.action = 'moved';
// TODO: subtract amount from change addresses
amount = amountOut; amount = amountOut;
} else { } else {
amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? fees : 0); amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? fees : 0);
tx.action = amount > 0 ? 'sent' : 'received'; tx.action = amount > 0 ? 'sent' : 'received';
} }
var firstOut = _.findWhere(items, {
type: 'out'
});
tx.labelTo = firstOut ? firstOut.label : undefined;
tx.amountSat = Math.abs(amount); tx.amountSat = Math.abs(amount);
tx.amount = tx.amountSat * satToUnit; tx.amount = tx.amountSat * satToUnit;
}; };

View file

@ -2081,6 +2081,53 @@ describe('Wallet model', function() {
done(); done();
}); });
}); });
it('should assign label when address in address book', function(done) {
var w = cachedCreateW2();
var txs = [{
vin: [{
addr: 'addr_in_1',
valueSat: 3000
}, {
addr: 'addr_in_2',
valueSat: 2000
}],
vout: [{
scriptPubKey: {
addresses: ['addr_out_1'],
},
value: '0.00003900',
}, {
scriptPubKey: {
addresses: ['change'],
},
value: '0.00001000',
}],
fees: 0.00000100
}];
w.blockchain.getTransactions = sinon.stub().yields(null, txs);
w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1'
}, {
addressStr: 'addr_in_2'
}, {
addressStr: 'change',
isChange: true,
}]);
w.addressBook = {
'addr_out_1': {
label: 'Address out one'
},
};
w.getTransactionHistory(function(err, res) {
res.should.exist;
res[0].labelTo.should.equal('Address out one');
done();
});
});
}); });