Merge pull request #121 from matiu/feature/ux13

Feature/ux13
This commit is contained in:
Manuel Aráoz 2014-04-21 13:18:16 -03:00
commit 5f66a179c9
9 changed files with 84 additions and 43 deletions

View File

@ -18,8 +18,10 @@
</figure>
<div class="text-right" ng-show="$root.wallet">
<h5 ng-show="$root.wallet.id">Wallet ID: {{$root.wallet.id}}</h5>
<p ng-show="totalBalance">{{totalBalance}} <i class="fi-bitcoin"></i></p>
<p ng-show="!totalBalance">0 <i class="fi-bitcoin"></i></p>
<p>
Balance: {{totalBalance || 0}} <i class="fi-bitcoin"></i><br>
Available to Spend:{{availableBalance || 0}} <i class="fi-bitcoin"></i>
</p>
</div>
</div>
@ -214,13 +216,13 @@
<script type="text/ng-template" id="addresses.html">
<div class="addresses" data-ng-controller="AddressesController">
<div ng-show='$root.wallet.publicKeyRing.isComplete()'>
<h3>Address</h3>
<h3>Addresses (available to spend)</h3>
<div class="row">
<div class="large-6 columns">
<a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">{{addr}}
<span ng-if="!isMain[addr]">(change)</span>
<span class="right">{{balanceByAddr[addr] || 0}} <i class="fi-bitcoin"></i></span></a>
<span class="right">({{balanceByAddr[addr] || 0}} <i class="fi-bitcoin"></i>)</span></a>
</div>
<div class="large-3 columns line-dashed-v text-center">

View File

@ -10,8 +10,7 @@ angular.module('copay.addresses').controller('AddressesController',
var _updateBalance = function () {
controllerUtils.setSocketHandlers();
w.getBalance(function (balance, balanceByAddr, isMain) {
w.getBalance(true, function (balance, balanceByAddr, isMain) {
if (balanceByAddr && Object.keys(balanceByAddr).length) {
$rootScope.balanceByAddr = balanceByAddr;
$scope.isMain = isMain;

View File

@ -27,11 +27,7 @@ angular.module('copay.header').controller('HeaderController',
$rootScope.$watch('wallet', function(wallet) {
if (wallet) {
controllerUtils.setSocketHandlers();
$rootScope.wallet.getBalance(function(balance) {
$rootScope.$apply(function() {
$rootScope.totalBalance = balance;
});
});
controllerUtils.updateBalance();
}
});

View File

@ -4,9 +4,6 @@ var bitcore = require('bitcore');
angular.module('copay.transactions').controller('TransactionsController',
function($scope, $rootScope, $location) {
$scope.title = 'Transactions';
$scope.oneAtATime = true;
var _updateTxs = function() {
console.log('[transactions.js.10:_updateTxs:]'); //TODO
var w =$rootScope.wallet;
@ -32,6 +29,7 @@ console.log('[transactions.js.10:_updateTxs:]'); //TODO
i.missingSignatures = tx.countInputMissingSignatures(0);
txs.push(i);
});
console.log('[transactions.js.35:txs:]',txs); //TODO
$scope.txs = txs;
w.removeListener('txProposalsUpdated',_updateTxs)
w.once('txProposalsUpdated',_updateTxs);
@ -69,4 +67,5 @@ console.log('[transactions.js.68:txid:] SENTTX CALLBACK',txid); //TODO
}
};
_updateTxs();
});

View File

@ -213,7 +213,6 @@ PublicKeyRing.prototype.getAddresses = function(onlyMain) {
PublicKeyRing.prototype.getRedeemScriptMap = function () {
var ret = {};
console.log('[PublicKeyRing.js.216]', this.changeAddressIndex, this.addressIndex); //TODO
for (var i=0; i<this.changeAddressIndex; i++) {
ret[this.getAddress(i,true)] = this.getRedeemScript(i,true).getBuffer().toString('hex');

View File

@ -181,6 +181,16 @@ TxProposals.prototype.setSent = function(ntxid,txid) {
this.txps[ntxid].setSent(txid);
};
TxProposals.prototype.getUsedUnspent = function() {
var ret = [];
for(var i in this.txps) {
var u = this.txps[i].builder.getSelectedUnspent();
for (var j in u){
ret.push(u[j].txid);
}
}
return ret;
};
TxProposals.prototype.merge = function(t) {
if (this.network.name !== t.network.name)

View File

@ -400,7 +400,7 @@ Wallet.prototype.addressIsOwn = function(addrStr) {
return ret;
};
Wallet.prototype.getBalance = function(cb) {
Wallet.prototype.getBalance = function(safe, cb) {
var balance = 0;
var balanceByAddr = {};
var isMain = {};
@ -414,7 +414,8 @@ Wallet.prototype.getBalance = function(cb) {
balanceByAddr[a]=0;
isMain[a]=1;
});
this.getUnspent(function(utxos) {
var f = safe ? this.getSafeUnspent.bind(this):this.getUnspent.bind(this);
f(function(utxos) {
for(var i=0;i<utxos.length; i++) {
var u= utxos[i];
var amt = u.amount * COIN;
@ -423,7 +424,7 @@ Wallet.prototype.getBalance = function(cb) {
}
for(var a in balanceByAddr){
balanceByAddr[a] = balanceByAddr[a]/COIN;
};
}
return cb(balance / COIN, balanceByAddr, isMain);
});
};
@ -434,8 +435,24 @@ Wallet.prototype.getUnspent = function(cb) {
});
};
Wallet.prototype.getSafeUnspent = function(cb) {
var self = this;
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
var ret=[];
var uu = self.txProposals.getUsedUnspent();
for(var i in unspentList){
if (uu.indexOf(unspentList[i].txid) === -1)
ret.push(unspentList[i]);
}
return cb(ret);
});
};
Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
console.log('[Wallet.js.447:createTx:]'); //TODO
var self = this;
if (typeof opts === 'function') {
cb = opts;
@ -446,7 +463,8 @@ console.log('[Wallet.js.447:createTx:]'); //TODO
if (typeof opts.spendUnconfirmed === 'undefined') {
opts.spendUnconfirmed = this.spendUnconfirmed;
}
self.getUnspent(function(unspentList) {
self.getSafeUnspent(function(unspentList) {
// TODO check enough funds, etc.
self.createTxSync(toAddress, amountSatStr, unspentList, opts);
self.sendPublicKeyRing(); // Change Address
@ -470,7 +488,6 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
if (!opts.remainderOut) {
opts.remainderOut ={ address: this.generateAddress(true).toString() };
}
console.log('[Wallet.js.480:opts: CREATETXSYNC]',opts); //TODO
var b = new Builder(opts)
.setUnspent(utxos)

View File

@ -27,27 +27,33 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
});
w.on('created', function() {
console.log('[controllerUtils.js.30:created:] RECV '); //TODO
$location.path('peer');
$rootScope.wallet = w;
$rootScope.wallet.getBalance(function(balance) {
$rootScope.totalBalance = balance;
});
root.updateBalance();
});
w.on('refresh', function() {
console.log('[controllerUtils.js] Refreshing'); //TODO
$rootScope.$digest();
root.updateBalance();
});
w.on('openError', root.onErrorDigest);
w.on('close', root.onErrorDigest);
console.log('[controllerUtils.js.45] CALLING NETSTART FROM setupUxHandlers'); //TODO
w.netStart();
console.log('[controllerUtils.js.45] setupUxHandlers END'); //TODO
};
root.setSocketHandlers = function(cb) {
root.updateBalance = function() {
var w = $rootScope.wallet;
w.getBalance(false,function(balance, balanceByAddr) {
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
console.log('New balance:', balance);
w.getBalance(true,function(balance) {
$rootScope.availableBalance = balance;
$rootScope.$digest();
});
});
};
root.setSocketHandlers = function() {
Socket.removeAllListeners();
var addrs = $rootScope.wallet.getAddressesStr();
@ -59,15 +65,7 @@ console.log('[controllerUtils.js.45] setupUxHandlers END'); //TODO
addrs.forEach(function(addr) {
Socket.on(addr, function(txid) {
console.log('Received!', txid);
$rootScope.wallet.getBalance(function(balance, balanceByAddr) {
$rootScope.$apply(function() {
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
});
console.log('New balance:', balance);
if (typeof cb === 'function') return cb();
});
root.updateBalance();
});
});
};

View File

@ -104,6 +104,30 @@ describe('TxProposals model', function() {
};
it('#getUsedUnspend', function () {
var priv = new PrivateKey(config);
var w = new TxProposals({
networkName: config.networkName,
});
var start = new Date().getTime();
var pkr=createPKR([priv]);
var ts = Date.now();
var isChange=0;
var index=0;
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
unspentTest,
{},
priv,
pkr
));
w.getUsedUnspent().length.should.equal(1);
w.getUsedUnspent()[0].should.equal(unspentTest[0].txid);
});
it('#merge with self', function () {
var priv = new PrivateKey(config);
var w = new TxProposals({
@ -472,8 +496,5 @@ var _dumpChunks = function (scriptSig, label) {
w2.merge(w);
Object.keys(w2.txps).length.should.equal(1);
});
});