copay/js/controllers/transactions.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-03-26 05:18:42 -07:00
'use strict';
2014-04-18 15:28:28 -07:00
var bitcore = require('bitcore');
2014-03-26 05:18:42 -07:00
angular.module('copay.transactions').controller('TransactionsController',
2014-04-18 15:08:01 -07:00
function($scope, $rootScope, $location) {
2014-03-26 05:18:42 -07:00
$scope.title = 'Transactions';
var _updateTxs = function() {
2014-04-20 16:24:24 -07:00
console.log('[transactions.js.10:_updateTxs:]'); //TODO
2014-04-18 07:19:39 -07:00
var w =$rootScope.wallet;
var inT = w.getTxProposals();
2014-04-18 15:28:28 -07:00
var txs = [];
2014-04-18 07:19:39 -07:00
inT.forEach(function(i){
2014-04-21 03:27:45 -07:00
var tx = i.builder.build();
var outs = [];
2014-04-18 15:28:28 -07:00
tx.outs.forEach(function(o) {
2014-04-18 15:28:28 -07:00
var addr = bitcore.Address.fromScriptPubKey(o.getScript(), config.networkName)[0].toString();
if (!w.addressIsOwn(addr)) {
2014-04-18 07:33:49 -07:00
outs.push({
2014-04-18 15:28:28 -07:00
address: addr,
2014-04-18 07:33:49 -07:00
value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.COIN,
});
2014-04-18 15:28:28 -07:00
}
});
2014-04-21 03:27:45 -07:00
// extra fields
i.outs = outs;
i.fee = i.feeSat/bitcore.util.COIN;
i.missingSignatures = tx.countInputMissingSignatures(0);
txs.push(i);
});
2014-04-21 09:16:15 -07:00
console.log('[transactions.js.35:txs:]',txs); //TODO
2014-04-18 15:28:28 -07:00
$scope.txs = txs;
2014-04-20 17:53:54 -07:00
w.removeListener('txProposalsUpdated',_updateTxs)
2014-04-20 16:24:24 -07:00
w.once('txProposalsUpdated',_updateTxs);
};
2014-04-20 17:53:54 -07:00
$scope.send = function (ntxid) {
var w = $rootScope.wallet;
w.sendTx(ntxid, function(txid) {
console.log('[transactions.js.68:txid:] SENTTX CALLBACK',txid); //TODO
$rootScope.flashMessage = txid
? {type:'success', message: 'Transactions SENT! txid:' + txid}
: {type:'error', message: 'There was an error sending the Transaction'}
;
_updateTxs();
$rootScope.$digest();
});
};
2014-04-16 13:50:10 -07:00
$scope.sign = function (ntxid) {
var w = $rootScope.wallet;
var ret = w.sign(ntxid);
2014-04-18 15:28:28 -07:00
_updateTxs();
var p = w.getTxProposal(ntxid);
if (p.txp.builder.isFullySigned()) {
2014-04-20 17:53:54 -07:00
$scope.send(ntxid);
2014-04-18 15:28:28 -07:00
}
else {
$rootScope.flashMessage = ret
? {type:'success', message: 'Transactions signed'}
: {type:'error', message: 'There was an error signing the Transaction'}
;
2014-04-20 17:53:54 -07:00
_updateTxs();
$rootScope.$digest();
2014-04-18 15:28:28 -07:00
}
2014-04-16 13:50:10 -07:00
};
2014-04-20 17:53:54 -07:00
2014-04-21 09:16:15 -07:00
_updateTxs();
2014-03-26 05:18:42 -07:00
});