implement BITS + tests

This commit is contained in:
Matias Alejo Garcia 2014-06-12 17:42:26 -03:00
commit 727bf8524a
14 changed files with 213 additions and 79 deletions

View file

@ -20,15 +20,7 @@ angular.module('copayApp.controllers').controller('ImportController',
}
$rootScope.wallet = w;
controllerUtils.startNetwork($rootScope.wallet);
$rootScope.wallet.on('connectionError', function() {
var message = "Looks like you are already connected to this wallet, please logout from it and try importing it again.";
$rootScope.$flashMessage = { message: message, type: 'error'};
});
$rootScope.wallet.on('serverError', function() {
$rootScope.$flashMessage = { message: 'The PeerJS server is not responding, please try again', type: 'error'};
controllerUtils.onErrorDigest();
});
controllerUtils.startNetwork($rootScope.wallet, $scope);
});
};

View file

@ -1,29 +1,32 @@
'use strict';
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('SendController',
function($scope, $rootScope, $window, $location, $timeout) {
$scope.title = 'Send';
$scope.loading = false;
$scope.defaultFee = bitcore.TransactionBuilder.FEE_PER_1000B_SAT / bitcore.util.BIT;
// TODO this shouldnt be on a particular controller.
// Detect mobile devices
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
@ -32,27 +35,29 @@ angular.module('copayApp.controllers').controller('SendController',
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
$scope.isMobile = isMobile.any();
$scope.unitIds = ['BTC','mBTC'];
$scope.selectedUnit = $scope.unitIds[0];
$scope.submitForm = function(form) {
if (form.$invalid) {
$rootScope.$flashMessage = { message: 'You can not send a proposal transaction. Please, try again', type: 'error'};
$rootScope.$flashMessage = {
message: 'You can not send a proposal transaction. Please, try again',
type: 'error'
};
return;
}
$scope.loading = true;
var address = form.address.$modelValue;
var amount = (form.amount.$modelValue * 100000000).toFixed(); // satoshi to string
var comment = form.comment.$modelValue;
var amount = (form.amount.$modelValue * 100) | 0;
var w = $rootScope.wallet;
w.createTx(address, amount, comment, function() {
$scope.loading = false;
$rootScope.$flashMessage = { message: 'The transaction proposal has been created', type: 'success'};
$rootScope.$flashMessage = {
message: 'The transaction proposal has been created',
type: 'success'
};
$rootScope.$digest();
});
@ -81,7 +86,11 @@ angular.module('copayApp.controllers').controller('SendController',
reader.onload = (function(theFile) {
return function(e) {
var mpImg = new MegaPixImage(file);
mpImg.render(canvas, { maxWidth: 200, maxHeight: 200, orientation: 6 });
mpImg.render(canvas, {
maxWidth: 200,
maxHeight: 200,
orientation: 6
});
$timeout(function() {
qrcode.width = canvas.width;
@ -107,7 +116,7 @@ angular.module('copayApp.controllers').controller('SendController',
try {
qrcode.decode();
} catch(e) {
} catch (e) {
//qrcodeError(e);
}
}
@ -168,7 +177,9 @@ angular.module('copayApp.controllers').controller('SendController',
canvas.height = 225;
context.clearRect(0, 0, 300, 225);
navigator.getUserMedia({video: true}, _successCallback, _videoError);
navigator.getUserMedia({
video: true
}, _successCallback, _videoError);
}
}, 500);
};

View file

@ -1,4 +1,5 @@
'use strict';
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('TransactionsController',
function($scope, $rootScope, $timeout, controllerUtils) {
@ -10,8 +11,6 @@ angular.module('copayApp.controllers').controller('TransactionsController',
$scope.txpCurrentPage = 1;
$scope.txpItemsPerPage = 4;
var COIN = 100000000;
$scope.blockchain_txs = [];
$scope.update = function () {
@ -81,14 +80,14 @@ angular.module('copayApp.controllers').controller('TransactionsController',
tmp[addr].doubleSpentIndex = tmp[addr].doubleSpentIndex || items[i].doubleSpentIndex;
tmp[addr].unconfirmedInput += items[i].unconfirmedInput;
tmp[addr].dbError = tmp[addr].dbError || items[i].dbError;
tmp[addr].valueSat += Math.round(items[i].value * COIN);
tmp[addr].valueSat += (items[i].value * bitcore.util.COIN)|0;
tmp[addr].items.push(items[i]);
tmp[addr].notAddr = notAddr;
tmp[addr].count++;
}
angular.forEach(tmp, function(v) {
v.value = v.value || parseInt(v.valueSat) / COIN;
v.value = (v.valueSat|0) / bitcore.util.BIT;
ret.push(v);
});
return ret;
@ -150,6 +149,8 @@ angular.module('copayApp.controllers').controller('TransactionsController',
for (var i=0; i<txs.length;i++) {
txs[i].vinSimple = _aggregateItems(txs[i].vin);
txs[i].voutSimple = _aggregateItems(txs[i].vout);
txs[i].valueOut = ((txs[i].valueOut * bitcore.util.COIN)|0) / bitcore.util.BIT;
txs[i].fees = ((txs[i].fees * bitcore.util.COIN)|0) / bitcore.util.BIT;
$scope.blockchain_txs.push(txs[i]);
}
$scope.loading = false;

View file

@ -566,7 +566,8 @@ Wallet.prototype.getBalance = function(cb) {
var balance = 0;
var safeBalance = 0;
var balanceByAddr = {};
var COIN = bitcore.util.COIN;
var BIT = coinUtil.BIT;
var COIN = coinUtil.COIN;
this.getUnspent(function(err, safeUnspent, unspent) {
if (err) {
@ -580,11 +581,11 @@ Wallet.prototype.getBalance = function(cb) {
balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt;
}
// we multiply and divide by COIN to avoid rounding errors when adding
// we multiply and divide by BIT to avoid rounding errors when adding
for (var a in balanceByAddr) {
balanceByAddr[a] = balanceByAddr[a].toFixed(0) / COIN;
balanceByAddr[a] = balanceByAddr[a].toFixed(0) / BIT;
}
balance = balance / COIN;
balance = balance.toFixed(0) / BIT;
for (var i = 0; i < safeUnspent.length; i++) {
var u = safeUnspent[i];
@ -592,7 +593,7 @@ Wallet.prototype.getBalance = function(cb) {
safeBalance += amt;
}
safeBalance = safeBalance.toFixed(0) / COIN;
safeBalance = safeBalance.toFixed(0) /BIT ;
return cb(null, balance, balanceByAddr, safeBalance);
});
};

View file

@ -1,9 +1,9 @@
'use strict';
var bitcore = require('bitcore');
angular.module('copayApp.services')
.factory('controllerUtils', function($rootScope, $sce, $location, $notification, Socket, video) {
.factory('controllerUtils', function($rootScope, $sce, $location, $notification, $timeout, Socket, video) {
var root = {};
var bitcore = require('bitcore');
root.getVideoMutedStatus = function(copayer) {
var vi = $rootScope.videoInfo[copayer]
@ -106,11 +106,14 @@ angular.module('copayApp.services')
});
w.on('txProposalsUpdated', function(dontDigest) {
root.updateTxs({onlyPending:true});
root.updateBalance(function(){
if (!dontDigest) {
$rootScope.$digest();
}
});
// give sometime to the tx to propagate.
$timeout(function() {
root.updateBalance(function(){
if (!dontDigest) {
$rootScope.$digest();
}
});
},3000);
});
w.on('connectionError', function(msg) {
root.onErrorDigest(null, msg);
@ -137,7 +140,6 @@ angular.module('copayApp.services')
var w = $rootScope.wallet;
if (!w) return root.onErrorDigest();
$rootScope.balanceByAddr = {};
$rootScope.updatingBalance = true;
w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
@ -151,8 +153,10 @@ angular.module('copayApp.services')
}
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
$rootScope.totalBalanceBTC = (balance / 1e6).toFixed(3) ;
$rootScope.availableBalance = safeBalance;
$rootScope.availableBalanceBTC = (safeBalance / 1e6).toFixed(3);
$rootScope.balanceByAddr = balanceByAddr;
root.updateAddressList();
$rootScope.updatingBalance = false;
return cb?cb():null;
@ -188,13 +192,13 @@ angular.module('copayApp.services')
if (!w.addressIsOwn(addr, {excludeMain:true})) {
outs.push({
address: addr,
value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.COIN,
value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.BIT,
});
}
});
// extra fields
i.outs = outs;
i.fee = i.builder.feeSat/bitcore.util.COIN;
i.fee = i.builder.feeSat/bitcore.util.BIT;
i.missingSignatures = tx.countInputMissingSignatures(0);
txs.push(i);
}