This commit is contained in:
Matias Alejo Garcia 2015-03-06 12:00:10 -03:00
commit 320de62f13
348 changed files with 7745 additions and 30874 deletions

View file

@ -1,69 +0,0 @@
'use strict';
var copay = require('copay');
var _ = require('lodash');
var LS = require('../js/plugins/LocalStorage');
var ls = new LS();
// TODO move this to configService !
var config = copay.defaultConfig;
ls.getItem('config', function(err, data) {
var localConfig;
try {
localConfig = JSON.parse(data);
} catch(e) {};
if (localConfig) {
var cmv = copay.version.split('.')[1];
var lmv = localConfig.version ? localConfig.version.split('.')[1] : '-1';
if (cmv === lmv) {
_.each(localConfig, function(value, key) {
config[key] = value;
});
}
}
});
var modules = [
'ngRoute',
'angularMoment',
'mm.foundation',
'monospaced.qrcode',
'ngIdle',
'gettext',
'ui.gravatar',
'ngTouch',
'copayApp.filters',
'copayApp.services',
'copayApp.controllers',
'copayApp.directives',
];
var copayApp = window.copayApp = angular.module('copayApp', modules);
var defaults = JSON.parse(JSON.stringify(copay.defaultConfig));
copayApp.value('defaults', defaults);
copayApp.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'self',
'mailto:**'
]);
});
angular.module('ui.gravatar').config([
'gravatarServiceProvider',
function(gravatarServiceProvider) {
gravatarServiceProvider.defaults = {
size: 35
};
// Use https endpoint
gravatarServiceProvider.secure = true;
}
]);
angular.module('copayApp.filters', []);
angular.module('copayApp.services', []);
angular.module('copayApp.controllers', []);
angular.module('copayApp.directives', []);

View file

@ -1,72 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('CopayersController',
function($scope, $rootScope, $timeout, go, identityService, notification, isCordova) {
var w = $rootScope.wallet;
$scope.init = function() {
$rootScope.title = 'Share this secret with your copayers';
$scope.loading = false;
$scope.secret = $rootScope.wallet.getSecret();
$scope.isCordova = isCordova;
w.on('publicKeyRingUpdated', $scope.updateList);
w.on('ready', $scope.updateList);
$scope.updateList();
};
$scope.updateList = function() {
var w = $rootScope.wallet;
$scope.copayers = $rootScope.wallet.getRegisteredPeerIds();
if (w.isComplete()) {
w.removeListener('publicKeyRingUpdated', $scope.updateList);
w.removeListener('ready', $scope.updateList);
go.walletHome();
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
$scope.deleteWallet = function() {
$rootScope.starting = true;
$timeout(function() {
identityService.deleteWallet(w, function(err) {
$rootScope.starting = false;
if (err) {
$scope.error = err.message || err;
copay.logger.warn(err);
$timeout(function () { $scope.$digest(); });
} else {
if ($rootScope.wallet) {
go.walletHome();
}
$timeout(function() {
notification.success('Success', 'The wallet "' + (w.name || w.id) + '" was deleted');
});
}
});
}, 100);
};
$scope.copySecret = function(secret) {
if (isCordova) {
window.cordova.plugins.clipboard.copy(secret);
window.plugins.toast.showShortCenter('Copied to clipboard');
}
};
$scope.shareSecret = function(secret) {
if (isCordova) {
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
}
window.plugins.socialsharing.share(secret, null, null, null);
}
};
});

View file

@ -1,78 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('CreateController',
function($scope, $rootScope, $location, $timeout, identityService, backupService, notification, defaults, isMobile, isCordova) {
$rootScope.fromSetup = true;
$scope.loading = false;
$scope.walletPassword = $rootScope.walletPassword;
$scope.isMobile = isMobile.any();
$scope.hideAdv = true;
$scope.networkName = config.networkName;
$rootScope.title = 'Create new wallet';
$rootScope.hideWalletNavigation = true;
$scope.isWindowsPhoneApp = isMobile.Windows() && isCordova;
// ng-repeat defined number of times instead of repeating over array?
$scope.getNumber = function(num) {
return new Array(num);
}
$scope.totalCopayers = config.wallet.totalCopayers;
$scope.TCValues = _.range(1, config.limits.totalCopayers + 1);
var updateRCSelect = function(n) {
var maxReq = copay.Wallet.getMaxRequiredCopayers(n);
$scope.RCValues = _.range(1, maxReq + 1);
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
};
updateRCSelect($scope.totalCopayers);
$scope.$watch('totalCopayers', function(tc) {
updateRCSelect(tc);
});
$scope.$watch('networkName', function(tc) {
$scope.networkUrl = config.network[$scope.networkName].url;
});
$scope.showNetwork = function() {
return $scope.networkUrl != defaults.network.livenet.url && $scope.networkUrl != defaults.network.testnet.url;
};
$scope.create = function(form) {
if (form && form.$invalid) {
$scope.error = 'Please enter the required fields';
return;
}
var opts = {
requiredCopayers: $scope.requiredCopayers,
totalCopayers: $scope.totalCopayers,
name: $scope.walletName,
privateKeyHex: $scope.private,
networkName: $scope.networkName,
};
$rootScope.starting = true;
identityService.createWallet(opts, function(err, wallet){
$rootScope.starting = false;
if (err || !wallet) {
copay.logger.debug(err);
if (err.match('OVERQUOTA')){
$scope.error = 'Could not create wallet: storage limits on remove server exceeded';
} else {
$scope.error = 'Could not create wallet: ' + err;
}
}
$timeout(function(){
$rootScope.$digest();
},1);
});
};
$scope.$on("$destroy", function () {
$rootScope.hideWalletNavigation = false;
});
});

View file

@ -1,202 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, $window, notification, pluginManager, identityService, pinService, isMobile, isCordova, configService, go) {
var _credentials;
$scope.init = function() {
if ($rootScope.wallet)
go.walletHome();
$scope.isMobile = isMobile.any();
$scope.isWindowsPhoneApp = isMobile.Windows() && isCordova;
$scope.hideForWP = 0;
$scope.digits = [];
$scope.defined = [];
$scope.askForPin = 0;
$scope.createStep = 'storage';
$scope.useLocalstorage = false;
$scope.minPasswordStrength = _.isUndefined(config.minPasswordStrength) ?
4 : config.minPasswordStrength;
};
$scope.clear = function() {
pinService.clearPin($scope);
};
$scope.press = function(digit) {
pinService.pressPin($scope, digit, true);
};
$scope.skip = function () {
pinService.skipPin($scope, true);
};
$scope.formFocus = function() {
if (!$scope.isWindowsPhoneApp) return
$scope.hideForWP = true;
$timeout(function() {
$scope.$digest();
}, 1);
};
$scope.createPin = function(pin) {
preconditions.checkArgument(pin);
preconditions.checkState($rootScope.iden);
preconditions.checkState(_credentials && _credentials.email);
$rootScope.starting = true;
$timeout(function() {
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
_credentials.password = '';
_credentials = null;
$scope.askForPin = 0;
$rootScope.hasPin = true;
$scope.createDefaultWallet();
});
}, 100);
};
$scope.setStep = function(step) {
$scope.error = null;
$scope.createStep = step;
$scope.hideForWP = false;
$timeout(function() {
$scope.$digest();
}, 1);
};
$scope.selectStorage = function(storage) {
$scope.useLocalstorage = storage == 'local';
$scope.hideForWP = false;
$timeout(function() {
$scope.$digest();
}, 1);
};
$scope.goToEmail = function() {
$scope.createStep = 'email';
$scope.useEmail = !$scope.useLocalstorage;
};
$scope.setEmailOrUsername = function(form) {
$scope.userOrEmail = $scope.useLocalstorage ? form.username.$modelValue : form.email.$modelValue;
preconditions.checkState($scope.userOrEmail);
$scope.error = null;
$scope.hideForWP = false;
$scope.createStep = 'pass';
$timeout(function() {
$scope.$digest();
}, 1);
};
/* Last step. Will emit after creation so the UX gets updated */
$scope.createDefaultWallet = function() {
$rootScope.hideNavigation = false;
$rootScope.starting = true;
identityService.createDefaultWallet(function(err) {
$scope.askForPin = 0;
$rootScope.starting = null;
if (err) {
var msg = err.toString();
$scope.error = msg;
} else {
if (!$scope.useLocalstorage) {
$rootScope.pleaseConfirmEmail = true;
}
}
});
};
$scope._doCreateProfile = function(emailOrUsername, password, cb) {
preconditions.checkArgument(_.isString(emailOrUsername));
preconditions.checkArgument(_.isString(password));
$rootScope.hideNavigation = false;
identityService.create(emailOrUsername, password, function(err) {
if (err) {
var msg = err.toString();
$scope.createStep = 'email';
if (msg.indexOf('EEXIST') >= 0 || msg.indexOf('BADC') >= 0) {
msg = 'This profile already exists'
}
if (msg.indexOf('EMAILERROR') >= 0) {
msg = 'Could not send verification email. Please check your email address.';
}
return cb(msg);
} else {
// mobile
if ($scope.isMobile) {
$rootScope.starting = null;
_credentials = {
email: emailOrUsername,
password: password,
};
$scope.askForPin = 1;
$scope.hideForWP = 0;
$rootScope.hideNavigation = true;
$timeout(function() {
$rootScope.$digest();
}, 1);
return;
} else {
$scope.createDefaultWallet();
}
}
return cb();
});
};
$scope.saveSettings = function(cb) {
var plugins = config.plugins;
plugins.EncryptedLocalStorage = false;
plugins.EncryptedInsightStorage = false;
var pluginName = $scope.useLocalstorage ? 'EncryptedLocalStorage' : 'EncryptedInsightStorage';
plugins[pluginName] = true;
configService.set({
plugins: plugins
}, cb);
};
$scope.createProfile = function(form) {
if (form && form.$invalid) {
$scope.error = 'Please enter the required fields';
return;
}
$scope.saveSettings(function(err) {
preconditions.checkState(!err, err);
$rootScope.starting = true;
$scope._doCreateProfile($scope.userOrEmail, form.password.$modelValue, function(err) {
if (err) {
$scope.error = err;
$scope.passwordStrength = null;
$rootScope.starting = false;
}
form.password.$setViewValue('');
form.password.$render();
form.repeatpassword.$setViewValue('');
form.repeatpassword.$render();
form.$setPristine();
$timeout(function() {
$rootScope.$digest();
}, 1);
});
});
};
});

View file

@ -1,16 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('DevLoginController', function($scope, $rootScope, $routeParams, identityService) {
var mail = $routeParams.mail;
var password = $routeParams.password;
var form = {};
form.email = {};
form.password = {};
form.email.$modelValue = mail;
form.password.$modelValue = password;
identityService.open($scope, form);
});

View file

@ -1,6 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('EmailConfirmationController', function($scope, $rootScope, $location) {
$rootScope.fromEmailConfirmation = true;
$location.path('/');
});

View file

@ -1,204 +0,0 @@
'use strict';
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('HistoryController',
function($scope, $rootScope, $filter, $timeout, $modal, rateService, notification, go) {
var w = $rootScope.wallet;
$rootScope.title = 'History';
$scope.loading = false;
$scope.generating = false;
$scope.lastShowed = false;
$scope.currentPage = 1;
$scope.itemsPerPage = 10;
$scope.nbPages = 0;
$scope.totalItems = 0;
$scope.blockchain_txs = [];
$scope.alternativeCurrency = [];
$scope.selectPage = function(page) {
$scope.paging = true;
$scope.currentPage = page;
$scope.update();
};
$scope.downloadHistory = function() {
var w = $rootScope.wallet;
if (!w) return;
var filename = "copay_history.csv";
var descriptor = {
columns: [{
label: 'Date',
property: 'ts',
type: 'date'
}, {
label: 'Amount (' + w.settings.unitName + ')',
property: 'amount',
type: 'number'
}, {
label: 'Amount (' + w.settings.alternativeIsoCode + ')',
property: 'alternativeAmount'
}, {
label: 'Action',
property: 'action'
}, {
label: 'AddressTo',
property: 'addressTo'
}, {
label: 'Comment',
property: 'comment'
}, ],
};
if (w.isShared()) {
descriptor.columns.push({
label: 'Signers',
property: function(obj) {
if (!obj.actionList) return '';
return _.map(obj.actionList, function(action) {
return w.publicKeyRing.nicknameForCopayer(action.cId);
}).join('|');
}
});
}
$scope.generating = true;
$scope._getTransactions(w, null, function(err, res) {
if (err) {
$scope.generating = false;
logger.error(err);
notification.error('Could not get transaction history');
return;
}
$scope._addRates(w, res.items, function(err) {
copay.csv.toCsv(res.items, descriptor, function(err, res) {
if (err) {
$scope.generating = false;
logger.error(err);
notification.error('Could not generate csv file');
return;
}
var csvContent = "data:text/csv;charset=utf-8," + res;
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", filename);
link.click();
$scope.generating = false;
$scope.$digest();
});
});
});
};
$scope.update = function() {
$scope.getTransactions();
};
$scope.show = function() {
$scope.loading = true;
setTimeout(function() {
$scope.update();
}, 1);
};
$scope._getTransactions = function(w, opts, cb) {
w.getTransactionHistory(opts, function(err, res) {
if (err) return cb(err);
if (!res) return cb();
var now = new Date();
var items = res.items;
_.each(items, function(tx) {
tx.ts = tx.minedTs || tx.sentTs;
tx.rateTs = Math.floor((tx.ts || now) / 1000);
tx.amount = $filter('noFractionNumber')(tx.amount);
});
return cb(null, res);
});
};
$scope._addRates = function(w, txs, cb) {
if (!txs || txs.length == 0) return cb();
var index = _.groupBy(txs, 'rateTs');
rateService.getHistoricRates(w.settings.alternativeIsoCode, _.keys(index), function(err, res) {
if (err || !res) return cb(err);
_.each(res, function(r) {
_.each(index[r.ts], function (tx) {
var alternativeAmount = (r.rate != null ? tx.amountSat * rateService.SAT_TO_BTC * r.rate : null);
tx.alternativeAmount = alternativeAmount ? $filter('noFractionNumber')(alternativeAmount, 2) : null;
});
});
return cb();
});
};
$scope.openTxModal = function(btx) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.btx = btx;
$scope.getShortNetworkName = function() {
var w = $rootScope.wallet;
return w.getNetworkName().substring(0, 4);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
$modal.open({
templateUrl: 'views/modals/tx-details.html',
windowClass: 'medium',
controller: ModalInstanceCtrl,
});
};
$scope.getTransactions = function() {
var w = $rootScope.wallet;
if (!w) return;
$scope.blockchain_txs = w.cached_txs || [];
$scope.loading = true;
$scope._getTransactions(w, {
currentPage: $scope.currentPage,
itemsPerPage: $scope.itemsPerPage,
}, function(err, res) {
if (err) throw err;
if (!res) {
$scope.loading = false;
$scope.lastShowed = false;
return;
}
var items = res.items;
$scope._addRates(w, items, function(err) {
$timeout(function() {
$scope.$digest();
}, 1);
})
$scope.blockchain_txs = w.cached_txs = items;
$scope.nbPages = res.nbPages;
$scope.totalItems = res.nbItems;
$scope.loading = false;
$scope.paging = false;
setTimeout(function() {
$scope.$digest();
}, 1);
});
};
$scope.hasAction = function(actions, action) {
return actions.hasOwnProperty('create');
};
});

View file

@ -1,216 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $timeout, $window, go, notification, identityService, Compatibility, pinService, applicationService, isMobile, isCordova, localstorageService) {
var KEY = 'CopayDisclaimer';
var ls = localstorageService;
var _credentials;
$scope.init = function() {
$scope.isMobile = isMobile.any();
$scope.isWindowsPhoneApp = isMobile.Windows() && isCordova;
$scope.hideForWP = 0;
$scope.attempt = 0;
$scope.digits = [];
$scope.defined = [];
$scope.askForPin = 0;
// This is only for backwards compat, insight api should link to #!/confirmed directly
if (getParam('confirmed')) {
var hashIndex = window.location.href.indexOf('/?');
window.location = window.location.href.substr(0, hashIndex) + '#!/confirmed';
return;
}
if ($rootScope.fromEmailConfirmation) {
$scope.confirmedEmail = true;
$rootScope.fromEmailConfirmation = false;
}
if ($rootScope.wallet) {
go.walletHome();
}
Compatibility.check($scope);
pinService.check(function(err, value) {
$rootScope.hasPin = value;
});
$scope.usingLocalStorage = config.plugins.EncryptedLocalStorage;
if (isCordova) {
ls.getItem(KEY, function(err, value) {
$scope.showDisclaimer = value ? null : true;
});
}
};
$scope.clear = function() {
pinService.clearPin($scope);
};
$scope.press = function(digit) {
pinService.pressPin($scope, digit);
};
$scope.skip = function () {
pinService.skipPin($scope);
};
$scope.agreeDisclaimer = function() {
ls.setItem(KEY, true, function(err) {
$scope.showDisclaimer = null;
});
};
$scope.formFocus = function() {
if ($scope.isWindowsPhoneApp) {
$scope.hideForWP = true;
$timeout(function() {
$scope.$digest();
}, 1);
}
};
$scope.openWithPin = function(pin) {
if (!pin) {
$scope.error = 'Please enter the required fields';
return;
}
$rootScope.starting = true;
$timeout(function() {
var credentials = pinService.get(pin, function(err, credentials) {
if (err || !credentials) {
$rootScope.starting = null;
$scope.error = 'Wrong PIN';
$scope.clear();
$timeout(function() {
$scope.error = null;
}, 2000);
return;
}
$scope.open(credentials.email, credentials.password);
});
}, 100);
};
$scope.openWallets = function() {
preconditions.checkState($rootScope.iden);
var iden = $rootScope.iden;
$rootScope.hideNavigation = false;
$rootScope.starting = true;
iden.openWallets();
};
$scope.createPin = function(pin) {
preconditions.checkArgument(pin);
preconditions.checkState($rootScope.iden);
preconditions.checkState(_credentials && _credentials.email);
$rootScope.starting = true;
$timeout(function() {
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
_credentials.password = '';
_credentials = null;
$scope.askForPin = 0;
$rootScope.hasPin = true;
$rootScope.starting = null;
$scope.openWallets();
});
}, 100);
};
$scope.openWithCredentials = function(form) {
if (form && form.$invalid) {
$scope.error = 'Please enter the required fields';
return;
}
$timeout(function() {
$scope.open(form.email.$modelValue, form.password.$modelValue);
}, 100);
};
$scope.pinLogout = function() {
pinService.clear(function() {
copay.logger.debug('PIN erased');
delete $rootScope['hasPin'];
applicationService.restart();
});
};
$scope.open = function(email, password) {
$rootScope.starting = true;
identityService.open(email, password, function(err, iden) {
if (err) {
$rootScope.starting = false;
copay.logger.warn(err);
var identifier = $scope.usingLocalStorage ? 'username' : 'email';
if ((err.toString() || '').match('PNOTFOUND')) {
$scope.error = 'Invalid ' + identifier + ' or password';
if ($scope.attempt++ > 1) {
var storage = $scope.usingLocalStorage ? 'this device storage' : 'cloud storage';
$scope.error = 'Invalid ' + identifier + ' or password. You are trying to sign in using ' + storage + '. Change it on settings if necessary.';
};
$rootScope.hasPin = false;
pinService.clear(function() {});
} else if ((err.toString() || '').match('Connection')) {
$scope.error = 'Could not connect to Insight Server';
} else if ((err.toString() || '').match('Unable')) {
$scope.error = 'Unable to read data from the Insight Server';
} else {
$scope.error = 'Unknown error';
}
$timeout(function() {
$rootScope.$digest();
}, 1)
return;
}
// Open successfully?
if (iden) {
$scope.error = null;
$scope.confirmedEmail = false;
// mobile
if ($scope.isMobile && !$rootScope.hasPin) {
_credentials = {
email: email,
password: password,
};
$scope.askForPin = 1;
$rootScope.starting = false;
$rootScope.hideNavigation = true;
$timeout(function() {
$rootScope.$digest();
}, 1);
return;
}
// no mobile
else {
$scope.openWallets();
}
}
});
};
function getParam(sname) {
var params = location.search.substr(location.search.indexOf("?") + 1);
var sval = "";
params = params.split("&");
// split param and value into individual pieces
for (var i = 0; i < params.length; i++) {
var temp = params[i].split("=");
if ([temp[0]] == sname) {
sval = temp[1];
}
}
return sval;
}
});

View file

@ -1,103 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('HomeWalletController', function($scope, $rootScope, $timeout, $filter, $modal, rateService, notification, txStatus, identityService, isCordova) {
$scope.openTxModal = function(tx) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
var w = $rootScope.wallet;
$scope.error = null;
$scope.tx = tx;
$scope.registeredCopayerIds = w.getRegisteredCopayerIds();
$scope.loading = null;
$scope.getShortNetworkName = function() {
var w = $rootScope.wallet;
return w.getNetworkName().substring(0, 4);
};
$scope.sign = function(ntxid) {
if (isCordova) {
window.plugins.spinnerDialog.show(null, 'Signing transaction...', true);
}
$scope.loading = true;
$scope.error = null;
$timeout(function() {
w.signAndSend(ntxid, function(err, id, status) {
if (isCordova) {
window.plugins.spinnerDialog.hide();
}
$scope.loading = false;
if (err) {
$scope.error = 'Transaction could not send. Please try again.';
$scope.$digest();
}
else {
$modalInstance.close(status);
}
});
}, 100);
};
$scope.reject = function(ntxid) {
if (isCordova) {
window.plugins.spinnerDialog.show(null, 'Rejecting transaction...', true);
}
$scope.loading = true;
$scope.error = null;
$timeout(function() {
w.reject(ntxid, function(err, status) {
if (isCordova) {
window.plugins.spinnerDialog.hide();
}
$scope.loading = false;
if (err) {
$scope.error = err;
$scope.$digest();
}
else {
$modalInstance.close(status);
}
});
}, 100);
};
$scope.broadcast = function(ntxid) {
if (isCordova) {
window.plugins.spinnerDialog.show(null, 'Sending transaction...', true);
}
$scope.loading = true;
$scope.error = null;
$timeout(function() {
w.issueTx(ntxid, function(err, txid, status) {
if (isCordova) {
window.plugins.spinnerDialog.hide();
}
$scope.loading = false;
if (err) {
$scope.error = 'Transaction could not send. Please try again.';
$scope.$digest();
}
else {
$modalInstance.close(status);
}
});
}, 100);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/txp-details.html',
windowClass: 'medium',
controller: ModalInstanceCtrl,
});
modalInstance.result.then(function(status) {
txStatus.notify(status);
});
};
});

View file

@ -1,110 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('ImportController',
function($scope, $rootScope, $location, $timeout, identityService, notification, isMobile, isCordova, Compatibility) {
$rootScope.title = 'Import wallet';
$scope.importStatus = 'Importing wallet - Reading backup...';
$scope.hideAdv = true;
$scope.isSafari = isMobile.Safari();
$scope.isCordova = isCordova;
$scope.importOpts = {};
$rootScope.hideWalletNavigation = true;
window.ignoreMobilePause = true;
$scope.$on('$destroy', function() {
$timeout(function(){
window.ignoreMobilePause = false;
}, 100);
});
Compatibility.check($scope);
var reader = new FileReader();
var updateStatus = function(status) {
$scope.importStatus = status;
}
$scope.getFile = function() {
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var encryptedObj = evt.target.result;
updateStatus('Importing wallet - Procesing backup...');
identityService.importWallet(encryptedObj, $scope.password, {}, function(err) {
if (err) {
$rootScope.starting = false;
$scope.error = 'Could not read wallet. Please check your password';
$timeout(function() {
$rootScope.$digest();
}, 1);
}
});
}
}
};
$scope.import = function(form) {
if (form.$invalid) {
$scope.error = 'There is an error in the form';
return;
}
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var backupOldWallet = form.backupOldWallet.$modelValue;
var password = form.password.$modelValue;
if (backupOldWallet) {
backupText = backupOldWallet.value;
}
if (!backupFile && !backupText) {
$scope.error = 'Please, select your backup file';
return;
}
$rootScope.starting = true;
$timeout(function() {
$scope.importOpts = {};
var skipFields = [];
if ($scope.skipPublicKeyRing)
skipFields.push('publicKeyRing');
if ($scope.skipTxProposals)
skipFields.push('txProposals');
if (skipFields)
$scope.importOpts.skipFields = skipFields;
if (backupFile) {
reader.readAsBinaryString(backupFile);
} else {
updateStatus('Importing wallet - Procesing backup...');
identityService.importWallet(backupText, $scope.password, $scope.importOpts, function(err) {
if (err) {
$rootScope.starting = false;
$scope.error = 'Could not read wallet. Please check your password';
$timeout(function() {
$rootScope.$digest();
}, 1);
}
});
}
}, 100);
};
$scope.$on("$destroy", function () {
$rootScope.hideWalletNavigation = false;
});
});

View file

@ -1,82 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('ImportProfileController',
function($scope, $rootScope, $location, $timeout, notification, isMobile, isCordova, identityService) {
$scope.title = 'Import a backup';
$scope.importStatus = 'Importing profile - Reading backup...';
$scope.hideAdv = true;
$scope.isSafari = isMobile.Safari();
$scope.isCordova = isCordova;
window.ignoreMobilePause = true;
$scope.$on('$destroy', function() {
$timeout(function(){
window.ignoreMobilePause = false;
}, 100);
});
var reader = new FileReader();
var updateStatus = function(status) {
$scope.importStatus = status;
}
var _importBackup = function(str) {
var password = $scope.password;
updateStatus('Importing profile - Setting things up...');
identityService.importProfile(str,password, function(err, iden) {
if (err) {
$rootScope.starting = false;
copay.logger.warn(err);
if ((err.toString() || '').match('BADSTR')) {
$scope.error = 'Bad password or corrupt profile file';
} else if ((err.toString() || '').match('EEXISTS')) {
$scope.error = 'Profile already exists';
} else {
$scope.error = 'Unknown error';
}
$timeout(function() {
$rootScope.$digest();
}, 1);
}
});
};
$scope.getFile = function() {
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var encryptedObj = evt.target.result;
_importBackup(encryptedObj);
}
};
};
$scope.import = function(form) {
if (form.$invalid) {
$scope.error = 'Please enter the required fields';
return;
}
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var password = form.password.$modelValue;
if (!backupFile && !backupText) {
$scope.error = 'Please, select your backup file';
return;
}
$rootScope.starting = true;
$timeout(function() {
if (backupFile) {
reader.readAsBinaryString(backupFile);
} else {
_importBackup(backupText);
}
}, 100);
};
});

View file

@ -1,34 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('IndexController', function($scope, $timeout, go, isCordova, identityService, notification) {
$scope.init = function() {
};
$scope.resendVerificationEmail = function() {
$scope.loading = true;
identityService.resendVerificationEmail(function(err) {
if (err) {
notification.error('Could not send email', 'There was a problem sending the verification email.');
}
else {
notification.success('Email sent', 'Check your inbox and confirms the email');
$scope.hideReSendButton = true;
}
$scope.loading = null;
$timeout(function() {
$scope.$digest();
}, 1);
return;
});
};
$scope.openMenu = function() {
go.swipe(true);
};
$scope.closeMenu = function() {
go.swipe();
};
});

View file

@ -1,160 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('JoinController',
function($scope, $rootScope, $timeout, isMobile, notification, identityService) {
$rootScope.fromSetup = false;
$scope.loading = false;
$scope.isMobile = isMobile.any();
$rootScope.title = 'Join shared wallet';
$rootScope.hideWalletNavigation = true;
// QR code Scanner
var cameraInput;
var video;
var canvas;
var $video;
var context;
var localMediaStream;
$scope.hideAdv = true;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (!window.cordova && !navigator.getUserMedia)
$scope.disableScanner = 1;
var _scan = function(evt) {
if (localMediaStream) {
context.drawImage(video, 0, 0, 300, 225);
try {
qrcode.decode();
} catch (e) {
//qrcodeError(e);
}
}
$timeout(_scan, 500);
};
var _successCallback = function(stream) {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
localMediaStream = stream;
video.play();
$timeout(_scan, 1000);
};
var _scanStop = function() {
$scope.showScanner = false;
if (!$scope.isMobile) {
if (localMediaStream && localMediaStream.stop) localMediaStream.stop();
localMediaStream = null;
video.src = '';
}
};
var _videoError = function(err) {
_scanStop();
};
qrcode.callback = function(data) {
_scanStop();
$scope.$apply(function() {
$scope.connectionId = data;
$scope.joinForm.connectionId.$setViewValue(data);
$scope.joinForm.connectionId.$render();
});
};
$scope.cancelScanner = function() {
_scanStop();
};
$scope.openScanner = function() {
if (window.cordova) return $scope.scannerIntent();
$scope.showScanner = true;
// Wait a moment until the canvas shows
$timeout(function() {
canvas = document.getElementById('qr-canvas');
context = canvas.getContext('2d');
if ($scope.isMobile) {
cameraInput = document.getElementById('qrcode-camera');
cameraInput.addEventListener('change', _scan, false);
} else {
video = document.getElementById('qrcode-scanner-video');
$video = angular.element(video);
canvas.width = 300;
canvas.height = 225;
context.clearRect(0, 0, 300, 225);
navigator.getUserMedia({
video: true
}, _successCallback, _videoError);
}
}, 500);
};
$scope.scannerIntent = function() {
window.ignoreMobilePause = true;
cordova.plugins.barcodeScanner.scan(
function onSuccess(result) {
$timeout(function(){
window.ignoreMobilePause = false;
}, 100);
if (result.cancelled) return;
$scope.connectionId = result.text;
$rootScope.$digest();
},
function onError(error) {
$timeout(function(){
window.ignoreMobilePause = false;
}, 100);
alert('Scanning error');
});
}
$scope.join = function(form) {
if (form && form.$invalid) {
notification.error('Error', 'Please enter the required fields');
return;
}
$rootScope.starting = true;
identityService.joinWallet({
secret: $scope.connectionId,
nickname: $scope.nickname,
privateHex: $scope.private,
}, function(err) {
$rootScope.starting = false;
if (err) {
if (err === 'joinError')
notification.error('Fatal error connecting to Insight server');
else if (err === 'walletFull')
notification.error('The wallet is full');
else if (err === 'walletAlreadyExists')
notification.error('Wallet already exists', 'Cannot join again from the same profile');
else if (err === 'badNetwork')
notification.error('Network Error', 'Wallet network configuration missmatch');
else if (err === 'badSecret')
notification.error('Bad secret', 'The secret string you entered is invalid');
else {
notification.error('Error', err.message || err);
}
}
$timeout(function () { $scope.$digest(); }, 1);
});
}
$scope.$on("$destroy", function () {
$rootScope.hideWalletNavigation = false;
});
});

View file

@ -1,183 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('MoreController',
function($scope, $rootScope, $location, $filter, $timeout, balanceService, notification, rateService, backupService, identityService, isMobile, isCordova, go, pendingTxsService) {
var w = $rootScope.wallet;
var max = $rootScope.quotaPerItem;
$scope.isSafari = isMobile.Safari();
$scope.isCordova = isCordova;
$scope.wallet = w;
$scope.error = null;
$scope.success = null;
var bits = w.sizes().total;
w.kb = $filter('noFractionNumber')(bits / 1000, 1);
if (max) {
w.usage = $filter('noFractionNumber')(bits / max * 100, 0);
}
$rootScope.title = 'Settings';
$scope.unitOpts = [{
name: 'Satoshis (100,000,000 satoshis = 1BTC)',
shortName: 'SAT',
value: 1,
decimals: 0
}, {
name: 'bits (1,000,000 bits = 1BTC)',
shortName: 'bits',
value: 100,
decimals: 2
}, {
name: 'mBTC (1,000 mBTC = 1BTC)',
shortName: 'mBTC',
value: 100000,
decimals: 5
}, {
name: 'BTC',
shortName: 'BTC',
value: 100000000,
decimals: 8
}];
$scope.selectedAlternative = {
name: w.settings.alternativeName,
isoCode: w.settings.alternativeIsoCode
};
$scope.alternativeOpts = rateService.isAvailable() ?
rateService.listAlternatives() : [$scope.selectedAlternative];
rateService.whenAvailable(function() {
$scope.alternativeOpts = rateService.listAlternatives();
for (var ii in $scope.alternativeOpts) {
if (w.settings.alternativeIsoCode === $scope.alternativeOpts[ii].isoCode) {
$scope.selectedAlternative = $scope.alternativeOpts[ii];
}
}
});
for (var ii in $scope.unitOpts) {
if (w.settings.unitName === $scope.unitOpts[ii].shortName) {
$scope.selectedUnit = $scope.unitOpts[ii];
break;
}
}
$scope.hideAdv = true;
$scope.hidePriv = true;
$scope.hideSecret = true;
if (w) {
$scope.priv = w.privateKey.toObj().extendedPrivateKeyString;
$scope.secret = w.getSecret();
}
setTimeout(function() {
$scope.$digest();
}, 1);
$scope.save = function() {
var w = $rootScope.wallet;
w.changeSettings({
unitName: $scope.selectedUnit.shortName,
unitToSatoshi: $scope.selectedUnit.value,
unitDecimals: $scope.selectedUnit.decimals,
alternativeName: $scope.selectedAlternative.name,
alternativeIsoCode: $scope.selectedAlternative.isoCode,
});
notification.success('Success', $filter('translate')('settings successfully updated'));
balanceService.update(w, function() {
pendingTxsService.update();
$rootScope.$digest();
});
};
$scope.purge = function(deleteAll) {
var removed = w.purgeTxProposals(deleteAll);
if (removed) {
balanceService.update(w, function() {
$rootScope.$digest();
}, true);
}
notification.info('Transactions Proposals Purged', removed + ' ' + $filter('translate')('transaction proposal purged'));
};
$scope.updateIndexes = function() {
var w = $rootScope.wallet;
notification.info('Scaning for transactions', 'Using derived addresses from your wallet');
w.updateIndexes(function(err) {
notification.info('Scan Ended', 'Updating balance');
if (err) {
notification.error('Error', $filter('translate')('Error updating indexes: ') + err);
}
balanceService.update(w, function() {
notification.info('Finished', 'The balance is updated using the derived addresses');
w.sendIndexes();
$rootScope.$digest();
}, true);
});
};
$scope.deleteWallet = function() {
$scope.loading = true;
$timeout(function() {
identityService.deleteWallet(w, function(err) {
$scope.loading = false;
if (err) {
$scope.error = err.message || err;
copay.logger.warn(err);
$timeout(function () { $scope.$digest(); });
} else {
if ($rootScope.wallet) {
go.walletHome();
}
$timeout(function() {
notification.success('Success', 'The wallet "' + (w.name || w.id) + '" was deleted');
});
}
});
}, 100);
};
$scope.copyText = function(text) {
if (isCordova) {
window.cordova.plugins.clipboard.copy(text);
window.plugins.toast.showShortCenter('Copied to clipboard');
}
};
$scope.downloadWalletBackup = function() {
backupService.walletDownload(w);
};
$scope.viewWalletBackup = function() {
$scope.loading = true;
$timeout(function() {
$scope.backupWalletPlainText = backupService.walletEncrypted(w);
}, 100);
};
$scope.copyWalletBackup = function() {
var ew = backupService.walletEncrypted(w);
window.cordova.plugins.clipboard.copy(ew);
window.plugins.toast.showShortCenter('Copied to clipboard');
};
$scope.sendWalletBackup = function() {
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
}
window.plugins.toast.showShortCenter('Preparing backup...');
var name = (w.name || w.id);
var ew = backupService.walletEncrypted(w);
var properties = {
subject: 'Copay Wallet Backup: ' + name,
body: 'Here is the encrypted backup of the wallet '
+ name + ': \n\n' + ew
+ '\n\n To import this backup, copy all text between {...}, including the symbols {}',
isHtml: false
};
window.plugin.email.open(properties);
};
});

View file

@ -1,20 +0,0 @@
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('paymentUriController', function($rootScope, $scope, $routeParams, $location, go) {
// Build bitcoinURI with querystring
var query = [];
angular.forEach($location.search(), function(value, key) {
query.push(key + "=" + value);
});
var queryString = query ? query.join("&") : null;
var bitcoinURI = $routeParams.data + ( queryString ? '?' + queryString : '');
var uri = new bitcore.BIP21(bitcoinURI);
if (uri && uri.address && (_.isString(uri.address) || uri.address.isValid()) ) {
copay.logger.debug('Payment Intent:', bitcoinURI);
$rootScope.pendingPayment = bitcoinURI;
}
go.home();
});

View file

@ -1,69 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('ProfileController', function($scope, $rootScope, $location, $modal, $filter, $timeout, backupService, identityService, isMobile, isCordova, notification) {
$scope.username = $rootScope.iden.getName();
$scope.isSafari = isMobile.Safari();
$scope.isCordova = isCordova;
$rootScope.title = 'Profile';
$scope.hideAdv = true;
$scope.downloadProfileBackup = function() {
backupService.profileDownload($rootScope.iden);
};
$scope.viewProfileBackup = function() {
$scope.loading = true;
$timeout(function() {
$scope.backupProfilePlainText = backupService.profileEncrypted($rootScope.iden);
}, 100);
};
$scope.copyProfileBackup = function() {
var ep = backupService.profileEncrypted($rootScope.iden);
window.cordova.plugins.clipboard.copy(ep);
window.plugins.toast.showShortCenter('Copied to clipboard');
};
$scope.sendProfileBackup = function() {
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
}
window.plugins.toast.showShortCenter('Preparing backup...');
var name = $rootScope.iden.fullName;
var ep = backupService.profileEncrypted($rootScope.iden);
var properties = {
subject: 'Copay Profile Backup: ' + name,
body: 'Here is the encrypted backup of the profile '
+ name + ': \n\n' + ep
+ '\n\n To import this backup, copy all text between {...}, including the symbols {}',
isHtml: false
};
window.plugin.email.open(properties);
};
$scope.init = function() {
if ($rootScope.quotaPerItem) {
$scope.perItem = $filter('noFractionNumber')($rootScope.quotaPerItem / 1000, 1);
$scope.nrWallets = parseInt($rootScope.quotaItems) - 1;
}
// no need to add event handlers here. Wallet deletion is handle by callback.
};
$scope.deleteProfile = function() {
$scope.loading = true;
identityService.deleteProfile(function(err, res) {
$scope.loading = false;
if (err) {
log.warn(err);
notification.error('Error', 'Could not delete profile');
$timeout(function () { $scope.$digest(); });
}
else {
$location.path('/');
$timeout(function() {
notification.success('Success', 'Profile successfully deleted');
});
}
});
};
});

View file

@ -1,106 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('ReceiveController',
function($scope, $rootScope, $timeout, $modal, isCordova, isMobile) {
$scope.newAddr = function() {
var w = $rootScope.wallet;
var lastAddr = w.generateAddress(null);
$scope.setAddressList();
$scope.addr = lastAddr;
};
$scope.copyAddress = function(addr) {
if (isCordova) {
window.cordova.plugins.clipboard.copy('bitcoin:' + addr);
window.plugins.toast.showShortCenter('Copied to clipboard');
}
};
$scope.shareAddress = function(addr) {
if (isCordova) {
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
}
window.plugins.socialsharing.share('bitcoin:' + addr, null, null, null);
}
};
$scope.init = function() {
$rootScope.title = 'Receive';
$scope.showAll = false;
$scope.isCordova = isCordova;
var w = $rootScope.wallet;
var lastAddr = _.first(w.getAddressesOrdered());
var balance = w.balanceInfo.balanceByAddr;
$scope.setAddressList();
while (balance && balance[lastAddr] > 0) {
$scope.loading = true;
lastAddr = w.generateAddress(null);
};
$scope.loading = false;
$scope.addr = lastAddr;
};
$scope.openAddressModal = function(address) {
var scope = $scope;
var ModalInstanceCtrl = function($scope, $modalInstance, address) {
$scope.address = address;
$scope.isCordova = isCordova;
$scope.copyAddress = function(addr) {
scope.copyAddress(addr);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
$modal.open({
templateUrl: 'views/modals/qr-address.html',
windowClass: 'small',
controller: ModalInstanceCtrl,
resolve: {
address: function() {
return address;
}
}
});
};
$scope.toggleShowAll = function() {
$scope.showAll = !$scope.showAll;
$scope.setAddressList();
};
$scope.setAddressList = function() {
if ($scope.showAll) {
var w = $rootScope.wallet;
var balance = w.balanceInfo.balanceByAddr;
var addresses = w.getAddressesOrdered();
if (addresses) {
$scope.addrLength = addresses.length;
if (!$scope.showAll)
addresses = addresses.slice(0, 3);
var list = [];
_.each(addresses, function(address, index) {
list.push({
'index': index,
'address': address,
'balance': balance ? balance[address] : null,
'isChange': w.addressIsChange(address),
});
});
$scope.addresses = list;
}
} else {
$scope.addresses = [];
}
};
}
);

View file

@ -1,602 +0,0 @@
'use strict';
var bitcore = require('bitcore');
var preconditions = require('preconditions').singleton();
angular.module('copayApp.controllers').controller('SendController',
function($scope, $rootScope, $window, $timeout, $modal, $filter, notification, isMobile, rateService, txStatus, isCordova) {
$scope.init = function() {
var w = $rootScope.wallet;
preconditions.checkState(w);
preconditions.checkState(w.settings.unitToSatoshi);
$scope.isMobile = isMobile.any();
$scope.isWindowsPhoneApp = isMobile.Windows() && isCordova;
$rootScope.wpInputFocused = false;
$scope.isShared = w.isShared();
$scope.requiresMultipleSignatures = w.requiresMultipleSignatures();
$rootScope.title = $scope.requiresMultipleSignatures ? 'Send Proposal' : 'Send';
$scope.loading = false;
$scope.error = $scope.success = null;
$scope.alternativeName = w.settings.alternativeName;
$scope.alternativeIsoCode = w.settings.alternativeIsoCode;
$scope.isRateAvailable = false;
$scope.rateService = rateService;
$scope.showScanner = false;
$scope.myId = w.getMyCopayerId();
$scope.isMobile = isMobile.any();
if ($rootScope.pendingPayment) {
$timeout(function() {
$scope.setFromUri($rootScope.pendingPayment)
$rootScope.pendingPayment = null;
}, 100);
}
$scope.setInputs();
$scope.setScanner();
rateService.whenAvailable(function() {
$scope.isRateAvailable = true;
$scope.$digest();
});
};
if (isCordova) {
var openScannerCordova = $rootScope.$on('dataScanned', function(event, data) {
$scope.sendForm.address.$setViewValue(data);
$scope.sendForm.address.$render();
});
$scope.$on('$destroy', function() {
openScannerCordova();
});
}
$scope.formFocus = function(what) {
if (!$scope.isWindowsPhoneApp) return
if (!what) {
$rootScope.wpInputFocused = false;
$scope.hideAddress = false;
$scope.hideAmount = false;
} else {
$rootScope.wpInputFocused = true;
if (what == 'amount') {
$scope.hideAddress = true;
} else if (what == 'msg') {
$scope.hideAddress = true;
$scope.hideAmount = true;
}
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
$scope.setInputs = function() {
var w = $rootScope.wallet;
var unitToSat = w.settings.unitToSatoshi;
var satToUnit = 1 / unitToSat;
/**
* Setting the two related amounts as properties prevents an infinite
* recursion for watches while preserving the original angular updates
*
*/
Object.defineProperty($scope,
"_alternative", {
get: function() {
return this.__alternative;
},
set: function(newValue) {
this.__alternative = newValue;
if (typeof(newValue) === 'number' && $scope.isRateAvailable) {
this._amount = parseFloat(
(rateService.fromFiat(newValue, $scope.alternativeIsoCode) * satToUnit).toFixed(w.settings.unitDecimals), 10);
} else {
this._amount = 0;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty($scope,
"_amount", {
get: function() {
return this.__amount;
},
set: function(newValue) {
this.__amount = newValue;
if (typeof(newValue) === 'number' && $scope.isRateAvailable) {
this.__alternative = parseFloat(
(rateService.toFiat(newValue * unitToSat, $scope.alternativeIsoCode)).toFixed(2), 10);
} else {
this.__alternative = 0;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty($scope,
"_address", {
get: function() {
return this.__address;
},
set: function(newValue) {
this.__address = $scope.onAddressChange(newValue);
},
enumerable: true,
configurable: true
});
};
$scope.setScanner = function() {
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL ||
window.mozURL || window.msURL;
if (!window.cordova && !navigator.getUserMedia)
$scope.disableScanner = 1;
};
$scope.setError = function(err) {
var w = $rootScope.wallet;
copay.logger.warn(err);
var msg = err.toString();
if (msg.match('BIG'))
msg = 'The transaction have too many inputs. Try creating many transactions for smaller amounts'
if (msg.match('totalNeededAmount') || msg.match('unspent not set'))
msg = 'Insufficient funds'
if (msg.match('expired'))
msg = 'The payment request has expired';
if (msg.match('XMLHttpRequest'))
msg = 'Error when sending to the blockchain. Resend it from Home';
var message = 'The transaction' + ($scope.requiresMultipleSignatures ? ' proposal' : '') +
' could not be created: ' + msg;
$scope.error = message;
$timeout(function() {
$scope.$digest();
}, 1);
};
$scope.submitForm = function(form) {
var w = $rootScope.wallet;
var unitToSat = w.settings.unitToSatoshi;
if (form.$invalid) {
$scope.error = 'Unable to send transaction proposal';
return;
}
if (isCordova) {
window.plugins.spinnerDialog.show(null, 'Creating transaction...', true);
}
$scope.loading = true;
if ($scope.isWindowsPhoneApp)
$rootScope.wpInputFocused = true;
$timeout(function () {
var comment = form.comment.$modelValue;
var merchantData = $scope._merchantData;
var address, amount;
if (!merchantData) {
address = form.address.$modelValue;
amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0));
}
w.spend({
merchantData: merchantData,
toAddress: address,
amountSat: amount,
comment: comment,
}, function (err, txid, status) {
if (isCordova) {
window.plugins.spinnerDialog.hide();
}
$scope.loading = false;
if ($scope.isWindowsPhoneApp)
$rootScope.wpInputFocused = false;
if (err) {
$scope.setError(err);
}
else {
txStatus.notify(status);
$scope.resetForm();
}
});
}, 100);
};
// QR code Scanner
var cameraInput;
var video;
var canvas;
var $video;
var context;
var localMediaStream;
var _scan = function(evt) {
if ($scope.isMobile) {
$scope.scannerLoading = true;
var files = evt.target.files;
if (files.length === 1 && files[0].type.indexOf('image/') === 0) {
var file = files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var mpImg = new MegaPixImage(file);
mpImg.render(canvas, {
maxWidth: 200,
maxHeight: 200,
orientation: 6
});
$timeout(function() {
qrcode.width = canvas.width;
qrcode.height = canvas.height;
qrcode.imagedata = context.getImageData(0, 0, qrcode.width, qrcode.height);
try {
qrcode.decode();
} catch (e) {
// error decoding QR
}
}, 1500);
};
})(file);
// Read in the file as a data URL
reader.readAsDataURL(file);
}
} else {
if (localMediaStream) {
context.drawImage(video, 0, 0, 300, 225);
try {
qrcode.decode();
} catch (e) {
//qrcodeError(e);
}
}
$timeout(_scan, 500);
}
};
var _successCallback = function(stream) {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
localMediaStream = stream;
video.play();
$timeout(_scan, 1000);
};
var _scanStop = function() {
$scope.scannerLoading = false;
$scope.showScanner = false;
if (!$scope.isMobile) {
if (localMediaStream && localMediaStream.stop) localMediaStream.stop();
localMediaStream = null;
video.src = '';
}
};
var _videoError = function(err) {
_scanStop();
};
qrcode.callback = function(data) {
_scanStop();
$scope.$apply(function() {
$scope.sendForm.address.$setViewValue(data);
$scope.sendForm.address.$render();
});
};
$scope.cancelScanner = function() {
_scanStop();
};
$scope.openScanner = function() {
$scope.showScanner = true;
// Wait a moment until the canvas shows
$timeout(function() {
canvas = document.getElementById('qr-canvas');
context = canvas.getContext('2d');
if ($scope.isMobile) {
cameraInput = document.getElementById('qrcode-camera');
cameraInput.addEventListener('change', _scan, false);
} else {
video = document.getElementById('qrcode-scanner-video');
$video = angular.element(video);
canvas.width = 300;
canvas.height = 225;
context.clearRect(0, 0, 300, 225);
navigator.getUserMedia({
video: true
}, _successCallback, _videoError);
}
}, 500);
};
$scope.setTopAmount = function() {
var w = $rootScope.wallet;
var form = $scope.sendForm;
if (form) {
form.amount.$setViewValue(w.balanceInfo.topAmount);
form.amount.$render();
form.amount.$isValid = true;
}
};
$scope.setForm = function(to, amount, comment) {
var form = $scope.sendForm;
if (to) {
form.address.$setViewValue(to);
form.address.$isValid = true;
form.address.$render();
$scope.lockAddress = true;
}
if (amount) {
form.amount.$setViewValue("" + amount);
form.amount.$isValid = true;
form.amount.$render();
$scope.lockAmount = true;
}
if (comment) {
form.comment.$setViewValue(comment);
form.comment.$isValid = true;
form.comment.$render();
}
};
$scope.resetForm = function() {
var form = $scope.sendForm;
$scope.fetchingURL = null;
$scope._merchantData = $scope._domain = null;
$scope.lockAddress = false;
$scope.lockAmount = false;
$scope._amount = $scope._address = null;
form.amount.$pristine = true;
form.amount.$setViewValue('');
form.amount.$render();
form.comment.$setViewValue('');
form.comment.$render();
form.$setPristine();
if (form.address) {
form.address.$pristine = true;
form.address.$setViewValue('');
form.address.$render();
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
var $oscope = $scope;
$scope.openPPModal = function(merchantData) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
var w = $rootScope.wallet;
var satToUnit = 1 / w.settings.unitToSatoshi;
$scope.md = merchantData;
$scope.alternative = $oscope._alternative;
$scope.alternativeIsoCode = $oscope.alternativeIsoCode;
$scope.isRateAvailable = $oscope.isRateAvailable;
$scope.unitTotal = (merchantData.total * satToUnit).toFixed(w.settings.unitDecimals);
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
$modal.open({
templateUrl: 'views/modals/paypro.html',
windowClass: 'medium',
controller: ModalInstanceCtrl,
});
};
$scope.setFromPayPro = function(uri) {
var isChromeApp = window.chrome && chrome.runtime && chrome.runtime.id;
if (isChromeApp) {
$scope.error = 'Payment Protocol not yet supported on ChromeApp';
return;
}
var w = $rootScope.wallet;
var satToUnit = 1 / w.settings.unitToSatoshi;
$scope.fetchingURL = uri;
$scope.loading = true;
// Payment Protocol URI (BIP-72)
w.fetchPaymentRequest({
url: uri
}, function(err, merchantData) {
$scope.loading = false;
$scope.fetchingURL = null;
if (err) {
copay.logger.warn(err);
$scope.resetForm();
var msg = err.toString();
if (msg.match('HTTP')) {
msg = 'Could not fetch payment information';
}
$scope.error = msg;
} else {
$scope._merchantData = merchantData;
$scope._domain = merchantData.domain;
$scope.setForm(null, (merchantData.total * satToUnit).toFixed(w.settings.unitDecimals));
}
});
};
$scope.setFromUri = function(uri) {
function sanitizeUri(uri) {
// Fixes when a region uses comma to separate decimals
var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i;
var match = regex.exec(uri);
if (!match || match.length === 0) {
return uri;
}
var value = match[0].replace(',', '.');
var newUri = uri.replace(regex, value);
return newUri;
};
var w = $rootScope.wallet;
var satToUnit = 1 / w.settings.unitToSatoshi;
var form = $scope.sendForm;
uri = sanitizeUri(uri);
var parsed = new bitcore.BIP21(uri);
if (!parsed.isValid() || !parsed.address.isValid()) {
$scope.error = 'Invalid bitcoin URL';
form.address.$isValid = false;
return uri;
};
var addr = parsed.address.toString();
if (parsed.data.merchant)
return $scope.setFromPayPro(parsed.data.merchant);
var amount = (parsed.data && parsed.data.amount) ?
((parsed.data.amount * 100000000).toFixed(0) * satToUnit).toFixed(w.settings.unitDecimals) : 0;
$scope.setForm(addr, amount, parsed.data.message, true);
return addr;
};
$scope.onAddressChange = function(value) {
$scope.error = $scope.success = null;
if (!value) return '';
if (value.indexOf('bitcoin:') === 0) {
return $scope.setFromUri(value);
} else if (/^https?:\/\//.test(value)) {
return $scope.setFromPayPro(value);
}
return value;
};
$scope.openAddressBook = function() {
var w = $rootScope.wallet;
var modalInstance = $modal.open({
templateUrl: 'views/modals/address-book.html',
windowClass: 'large',
controller: function($scope, $modalInstance) {
$scope.showForm = null;
$scope.addressBook = w.addressBook;
$scope.hasEntry = function() {
return _.keys($scope.addressBook).length > 0 ? true : false;
};
$scope.toggleAddressBookEntry = function(key) {
w.toggleAddressBookEntry(key);
};
$scope.copyToSend = function(addr) {
$modalInstance.close(addr);
};
$scope.cancel = function(form) {
$scope.error = $scope.success = $scope.newaddress = $scope.newlabel = null;
clearForm(form);
$scope.toggleForm();
};
$scope.toggleForm = function() {
$scope.showForm = !$scope.showForm;
};
var clearForm = function(form) {
form.newaddress.$pristine = true;
form.newaddress.$setViewValue('');
form.newaddress.$render();
form.newlabel.$pristine = true;
form.newlabel.$setViewValue('');
form.newlabel.$render();
form.$setPristine();
};
// TODO change to modal
$scope.submitAddressBook = function(form) {
if (form.$invalid) {
return;
}
$scope.loading = true;
$timeout(function() {
var errorMsg;
var entry = {
"address": form.newaddress.$modelValue,
"label": form.newlabel.$modelValue
};
try {
w.setAddressBook(entry.address, entry.label);
} catch (e) {
copay.logger.warn(e);
errorMsg = e.message;
}
if (errorMsg) {
$scope.error = errorMsg;
} else {
clearForm(form);
$scope.toggleForm();
notification.success('Entry created', 'New addressbook entry created')
}
$scope.loading = false;
$rootScope.$digest();
}, 100);
return;
};
$scope.close = function() {
$modalInstance.dismiss('cancel');
};
},
});
modalInstance.result.then(function(addr) {
$scope.setForm(addr);
});
};
});

View file

@ -1,86 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $route, $location, notification, configService) {
$scope.title = 'Settings';
$scope.insightLivenet = config.network.livenet.url;
$scope.insightTestnet = config.network.testnet.url;
$scope.defaultLogLevel = config.logLevel || 'log';
var logLevels = copay.logger.getLevels();
$scope.availableLogLevels = [];
for (var key in logLevels) {
$scope.availableLogLevels.push({
'name': key
});
}
$scope.availableStorages = [{
name: 'In the cloud (Insight server)',
pluginName: 'EncryptedInsightStorage',
}, {
name: 'On this device (localstorage)',
pluginName: 'EncryptedLocalStorage',
},
// {
// name: 'GoogleDrive',
// pluginName: 'GoogleDrive',
// }
];
_.each($scope.availableStorages, function(v) {
if (config.plugins[v.pluginName])
$scope.selectedStorage = v;
});
for (var ii in $scope.availableLogLevels) {
if ($scope.defaultLogLevel === $scope.availableLogLevels[ii].name) {
$scope.selectedLogLevel = $scope.availableLogLevels[ii];
break;
}
}
$scope.save = function() {
$scope.insightLivenet = copay.Insight.setCompleteUrl($scope.insightLivenet);
$scope.insightTestnet = copay.Insight.setCompleteUrl($scope.insightTestnet);
var insightSettings = {
livenet: {
url: $scope.insightLivenet,
transports: ['polling'],
},
testnet: {
url: $scope.insightTestnet,
transports: ['polling'],
},
}
var plugins = {};
plugins[$scope.selectedStorage.pluginName] = true;
configService.set({
network: insightSettings,
plugins: plugins,
logLevel: $scope.selectedLogLevel.name,
EncryptedInsightStorage: _.extend(config.EncryptedInsightStorage, {
url: insightSettings.livenet.url + '/api/email'
}),
rates: _.extend(config.rates, {
url: insightSettings.livenet.url + '/api/rates'
}),
},
function() {
notification.success('Settings saved',"Settings were saved");
$location.path('/');
});
};
$scope.reset = function() {
configService.reset(function() {
notification.success('Settings reseted',"Settings were reseted");
$location.path('/');
});
};
});

View file

@ -1,121 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('SidebarController', function($scope, $rootScope, $location, $timeout, identityService, isMobile, isCordova, go) {
$scope.isMobile = isMobile.any();
$scope.isCordova = isCordova;
$scope.username = $rootScope.iden ? $rootScope.iden.getName() : '';
$scope.menu = [{
'title': 'Home',
'icon': 'icon-home',
'link': 'homeWallet'
}, {
'title': 'Receive',
'icon': 'icon-receive',
'link': 'receive'
}, {
'title': 'Send',
'icon': 'icon-paperplane',
'link': 'send'
}, {
'title': 'History',
'icon': 'icon-history',
'link': 'history'
}];
$scope.signout = function() {
identityService.signout();
};
$scope.isActive = function(item) {
return item.link && item.link == $location.path().split('/')[1];
};
$scope.switchWallet = function(wid) {
$scope.walletSelection = false;
identityService.setFocusedWallet(wid);
go.walletHome();
};
$scope.toggleWalletSelection = function() {
$scope.walletSelection = !$scope.walletSelection;
if (!$scope.walletSelection) return;
$scope.setWallets();
};
$scope.openScanner = function() {
window.ignoreMobilePause = true;
cordova.plugins.barcodeScanner.scan(
function onSuccess(result) {
$timeout(function() {
window.ignoreMobilePause = false;
}, 100);
if (result.cancelled) return;
$timeout(function() {
var data = result.text;
$scope.$apply(function() {
$rootScope.$emit('dataScanned', data);
});
}, 1000);
},
function onError(error) {
$timeout(function() {
window.ignoreMobilePause = false;
}, 100);
alert('Scanning error');
}
);
go.send();
};
$scope.init = function() {
// This should be called only once.
// focused wallet change
if ($rootScope.wallet) {
$rootScope.$watch('wallet', function() {
$scope.walletSelection = false;
$scope.setWallets();
});
}
// wallet list change
if ($rootScope.iden) {
var iden = $rootScope.iden;
iden.on('newWallet', function() {
$scope.walletSelection = false;
$scope.setWallets();
});
iden.on('walletDeleted', function(wid) {
if (wid == $rootScope.wallet.id) {
copay.logger.debug('Deleted focused wallet:', wid);
// new focus
var newWid = $rootScope.iden.getLastFocusedWalletId();
if (newWid && $rootScope.iden.getWalletById(newWid)) {
identityService.setFocusedWallet(newWid);
} else {
copay.logger.debug('No wallets');
identityService.noFocusedWallet(newWid);
}
}
$scope.walletSelection = false;
$scope.setWallets();
});
}
};
$scope.setWallets = function() {
if (!$rootScope.iden) return;
var ret = _.filter($rootScope.iden.getWallets(), function(w) {
return w;
});
$scope.wallets = _.sortBy(ret, 'name');
};
$scope.openMenu = function() {
go.swipe(true);
};
});

View file

@ -1,5 +0,0 @@
angular.module('copayApp.controllers').controller('signOutController', function(identityService) {
identityService.signout();
});

View file

@ -1,9 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('UnsupportedController',
function($scope, $location) {
if (localStorage && localStorage.length > 0) {
$location.path('/');
}
}
);

View file

@ -1,34 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('VersionController',
function($scope, $rootScope, $http, $filter, notification) {
var w = $rootScope.wallet;
$scope.version = copay.version;
$scope.commitHash = copay.commitHash;
$scope.networkName = w ? w.getNetworkName() : '';
if (_.isUndefined($rootScope.checkVersion))
$rootScope.checkVersion = true;
if ($rootScope.checkVersion) {
$rootScope.checkVersion = false;
$http.get('https://api.github.com/repos/bitpay/copay/tags').success(function(data) {
var toInt = function(s) {
return parseInt(s);
};
var latestVersion = data[0].name.replace('v', '').split('.').map(toInt);
var currentVersion = copay.version.split('.').map(toInt);
var title = 'Copay ' + data[0].name + ' ' + $filter('translate')('available.');
var content;
if (currentVersion[0] < latestVersion[0]) {
content = 'It\'s important that you update your wallet at https://copay.io';
notification.version(title, content, true);
} else if (currentVersion[0] == latestVersion[0] && currentVersion[1] < latestVersion[1]) {
var content = 'Please update your wallet at https://copay.io';
notification.version(title, content, false);
}
});
}
});

View file

@ -1,48 +0,0 @@
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('walletForPaymentController', function($rootScope, $scope, $modal, identityService, go) {
// INIT: (not it a function, since there is no associated html)
var ModalInstanceCtrl = function($scope, $modalInstance, identityService) {
$scope.loading = true;
preconditions.checkState($rootScope.iden);
var iden = $rootScope.iden;
iden.on('newWallet', function() {
$scope.setWallets();
});
$scope.setWallets = function() {
$scope.wallets = $rootScope.iden.getWallets();
};
$scope.ok = function(w) {
$modalInstance.close(w);
};
$scope.cancel = function() {
$rootScope.pendingPayment = null;
$modalInstance.close();
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/walletSelection.html',
windowClass: 'tiny',
controller: ModalInstanceCtrl,
});
modalInstance.result.then(function(w) {
if (w) {
identityService.setFocusedWallet(w);
$rootScope.walletForPaymentSet = true;
} else {
$rootScope.pendingPayment = null;
}
go.walletHome();
}, function() {
$rootScope.pendingPayment = null;
go.walletHome();
});
});

View file

@ -1,27 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('WarningController', function($scope, $rootScope, $location, identityService) {
$scope.checkLock = function() {
if (!$rootScope.tmp || !$rootScope.tmp.getLock()) {
console.log('[warning.js.7] TODO LOCK'); //TODO
}
};
$scope.signout = function() {
identityService.signout();
};
$scope.ignoreLock = function() {
var w = $rootScope.tmp;
delete $rootScope['tmp'];
if (!w) {
$location.path('/');
} else {
w.ignoreLock = 1;
$scope.loading = true;
//controllerUtils.startNetwork(w, $scope);
// TODO
}
};
});

View file

@ -1,301 +0,0 @@
'use strict';
var bitcore = require('bitcore');
var Address = bitcore.Address;
var bignum = bitcore.Bignum;
var preconditions = require('preconditions').singleton();
function selectText(element) {
var doc = document;
if (doc.body.createTextRange) { // ms
var range = doc.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = doc.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
angular.module('copayApp.directives')
.directive('validAddress', ['$rootScope',
function($rootScope) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
// Regular url
if (/^https?:\/\//.test(value)) {
ctrl.$setValidity('validAddress', true);
return value;
}
// Bip21 uri
if (/^bitcoin:/.test(value)) {
var uri = new bitcore.BIP21(value);
var hasAddress = uri.address && uri.isValid() && uri.address.network().name === $rootScope.wallet.getNetworkName();
ctrl.$setValidity('validAddress', uri.data.merchant || hasAddress);
return value;
}
if (typeof value == 'undefined') {
ctrl.$pristine = true;
return;
}
// Regular Address
var a = new Address(value);
ctrl.$setValidity('validAddress', a.isValid() && a.network().name === $rootScope.wallet.getNetworkName());
return value;
};
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
};
}
])
.directive('validUrl', [
function() {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
// Regular url
if (/^https?:\/\//.test(value)) {
ctrl.$setValidity('validUrl', true);
return value;
} else {
ctrl.$setValidity('validUrl', false);
return value;
}
};
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
};
}
])
.directive('validAmount', ['$rootScope', '$locale',
function($rootScope, locale) {
var formats = locale.NUMBER_FORMATS;
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var val = function(value) {
var w = $rootScope.wallet;
preconditions.checkState(w);
var vNum = Number((value * w.settings.unitToSatoshi).toFixed(0));
if (typeof value == 'undefined') {
ctrl.$pristine = true;
}
if (typeof vNum == "number" && vNum > 0) {
var decimals = Number(w.settings.unitDecimals);
var sep_index = ('' + value).indexOf(formats.DECIMAL_SEP);
var str_value = ('' + value).substring(sep_index + 1);
if (sep_index > 0 && str_value.length > decimals) {
ctrl.$setValidity('validAmount', false);
} else {
ctrl.$setValidity('validAmount', true);
}
} else {
ctrl.$setValidity('validAmount', false);
}
return value;
}
ctrl.$parsers.unshift(val);
ctrl.$formatters.unshift(val);
}
};
}
])
.directive('walletSecret', function() {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
var a = new Address(value);
ctrl.$setValidity('walletSecret', !a.isValid() && Boolean(copay.Wallet.decodeSecret(value)));
return value;
};
ctrl.$parsers.unshift(validator);
}
};
})
.directive('loading', function() {
return {
restrict: 'A',
link: function($scope, element, attr) {
var a = element.html();
var text = attr.loading;
element.on('click', function() {
element.html('<i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i> ' + text + '...');
});
$scope.$watch('loading', function(val) {
if (!val) {
element.html(a);
}
});
}
}
})
.directive('ngFileSelect', function() {
return {
link: function($scope, el) {
el.bind('change', function(e) {
$scope.file = (e.srcElement || e.target).files[0];
$scope.getFile();
});
}
}
})
.directive('contact', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
if (!scope.wallet) return;
var address = attrs.address;
var contact = scope.wallet.addressBook[address];
if (contact && !contact.hidden) {
element.append(contact.label);
element.attr('tooltip',attrs.address);
} else {
element.append(address);
}
element.bind('click', function() {
selectText(element[0]);
});
}
};
})
.directive('highlightOnChange', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.$watch(attrs.highlightOnChange, function(newValue, oldValue) {
element.addClass('highlight');
setTimeout(function() {
element.removeClass('highlight');
}, 500);
});
}
}
})
.directive('checkStrength', function() {
return {
replace: false,
restrict: 'EACM',
require: 'ngModel',
link: function(scope, element, attrs) {
var MIN_LENGTH = 8;
var MESSAGES = ['Very Weak', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'];
var COLOR = ['#dd514c', '#dd514c', '#faa732', '#faa732', '#16A085', '#16A085'];
function evaluateMeter(password) {
var passwordStrength = 0;
var text;
if (password.length > 0) passwordStrength = 1;
if (password.length >= MIN_LENGTH) {
if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) {
passwordStrength++;
} else {
text = ', add mixed case';
}
if (password.match(/\d+/)) {
passwordStrength++;
} else {
if (!text) text = ', add numerals';
}
if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) {
passwordStrength++;
} else {
if (!text) text = ', add punctuation';
}
if (password.length > 12) {
passwordStrength++;
} else {
if (!text) text = ', add characters';
}
} else {
text = ', that\'s short';
}
if (!text) text = '';
return {
strength: passwordStrength,
message: MESSAGES[passwordStrength] + text,
color: COLOR[passwordStrength]
}
}
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
if (newValue && newValue !== '') {
var info = evaluateMeter(newValue);
scope[attrs.checkStrength] = info;
}
});
}
};
})
.directive('showFocus', function($timeout) {
return function(scope, element, attrs) {
scope.$watch(attrs.showFocus,
function (newValue) {
$timeout(function() {
newValue && element[0].focus();
});
},true);
};
})
.directive('match', function() {
return {
require: 'ngModel',
restrict: 'A',
scope: {
match: '='
},
link: function(scope, elem, attrs, ctrl) {
scope.$watch(function() {
return (ctrl.$pristine && angular.isUndefined(ctrl.$modelValue)) || scope.match === ctrl.$modelValue;
}, function(currentValue) {
ctrl.$setValidity('match', currentValue);
});
}
};
})
.directive('clipCopy', function() {
return {
restrict: 'A',
scope: {
clipCopy: '=clipCopy'
},
link: function(scope, elm) {
// TODO this does not work (FIXME)
elm.attr('tooltip','Press Ctrl+C to Copy');
elm.attr('tooltip-placement','top');
elm.bind('click', function() {
selectText(elm[0]);
});
}
};
});

View file

@ -1,70 +0,0 @@
'use strict';
angular.module('copayApp.filters', [])
.filter('amTimeAgo', ['amMoment',
function(amMoment) {
return function(input) {
return amMoment.preprocessDate(input).fromNow();
};
}
])
.filter('paged', function() {
return function(elements) {
if (elements) {
return elements.filter(Boolean);
}
return false;
};
})
.filter('removeEmpty', function() {
return function(elements) {
elements = elements || [];
// Hide empty change addresses from other copayers
return elements.filter(function(e) {
return !e.isChange || e.balance > 0;
});
}
})
.filter('noFractionNumber', ['$filter', '$locale', '$rootScope',
function(filter, locale, $rootScope) {
var numberFilter = filter('number');
var formats = locale.NUMBER_FORMATS;
return function(amount, n) {
if (typeof(n) === 'undefined' && !$rootScope.wallet) return amount;
var fractionSize = (typeof(n) !== 'undefined') ?
n : $rootScope.wallet.settings.unitToSatoshi.toString().length - 1;
var value = numberFilter(amount, fractionSize);
var sep = value.indexOf(formats.DECIMAL_SEP);
var group = value.indexOf(formats.GROUP_SEP);
if (amount >= 0) {
if (group > 0) {
if (sep < 0) {
return value;
}
var intValue = value.substring(0, sep);
var floatValue = parseFloat(value.substring(sep));
if (floatValue === 0) {
floatValue = '';
} else {
if (floatValue % 1 === 0) {
floatValue = floatValue.toFixed(0);
}
floatValue = floatValue.toString().substring(1);
}
var finalValue = intValue + floatValue;
return finalValue;
} else {
value = parseFloat(value);
if (value % 1 === 0) {
value = value.toFixed(0);
}
return value;
}
}
return 0;
};
}
]);

View file

@ -1,53 +0,0 @@
'use strict';
angular.element(document).ready(function() {
// this is now in HTML tab, witch is compatible with Windows Phone
// var startAngular = function() {
// angular.bootstrap(document, ['copayApp']);
// };
/* Cordova specific Init */
if (window.cordova !== undefined) {
document.addEventListener('deviceready', function() {
document.addEventListener('pause', function() {
if (!window.ignoreMobilePause) {
window.location = '#!/signout';
}
}, false);
document.addEventListener('resume', function() {
setTimeout(function() {
window.ignoreMobilePause = false;
}, 100);
}, false);
document.addEventListener('backbutton', function() {
window.location = '#!/homeWallet';
}, false);
document.addEventListener("menubutton", function() {
window.location = '#!/more';
}, false);
setTimeout(function() {
navigator.splashscreen.hide();
}, 2000);
function handleBitcoinURI(url) {
if (!url) return;
window.location = '#!/uri-payment/' + url;
}
window.plugins.webintent.getUri(handleBitcoinURI);
window.plugins.webintent.onNewIntent(handleBitcoinURI);
window.handleOpenURL = handleBitcoinURI;
// startAngular();
}, false);
} else {
// startAngular();
}
});

View file

@ -1,432 +0,0 @@
'use strict';
var EventEmitter = require('events').EventEmitter;
var bitcore = require('bitcore');
var log = require('../util/log');
var AuthMessage = bitcore.AuthMessage;
var util = bitcore.util;
var nodeUtil = require('util');
var extend = nodeUtil._extend;
var io = require('socket.io-client');
var preconditions = require('preconditions').singleton();
function Network(opts) {
preconditions.checkArgument(opts);
preconditions.checkArgument(opts.url);
opts = opts || {};
this.maxPeers = opts.maxPeers || 12;
this.url = opts.url;
this.secretNumber = opts.secretNumber;
this.cleanUp();
this.socketOptions = {
reconnection: true,
'force new connection': true,
'secure': this.url.indexOf('https') === 0,
};
if (opts.transports) {
this.socketOptions['transports'] = opts.transports;
}
}
nodeUtil.inherits(Network, EventEmitter);
Network.prototype.cleanUp = function() {
this.started = false;
this.connectedPeers = [];
this.peerId = null;
this.privkey = null;
this.key = null;
this.copayerId = null;
this.allowedCopayerIds = null;
this.isInboundPeerAuth = [];
this.copayerForPeer = {};
this.criticalErr = '';
if (this.socket) {
log.info('Async DISCONNECT');
this.socket.disconnect();
this.socket.removeAllListeners();
this.socket = null;
}
this.removeAllListeners();
};
Network.parent = EventEmitter;
// Array helpers
Network._inArray = function(el, array) {
return array.indexOf(el) > -1;
};
Network._arrayPushOnce = function(el, array) {
var ret = false;
if (!Network._inArray(el, array)) {
array.push(el);
ret = true;
}
return ret;
};
Network._arrayRemove = function(el, array) {
var pos = array.indexOf(el);
if (pos >= 0) array.splice(pos, 1);
return array;
};
Network.prototype.connectedCopayers = function() {
var ret = [];
for (var i in this.connectedPeers) {
var copayerId = this.copayerForPeer[this.connectedPeers[i]];
if (copayerId) ret.push(copayerId);
}
return ret;
};
Network.prototype._sendHello = function(copayerId, secretNumber) {
this.send(copayerId, {
type: 'hello',
copayerId: this.copayerId,
secretNumber: secretNumber
});
};
Network.prototype._sendRejectConnection = function(copayerId) {
this.send(copayerId, {
type: 'rejectConnection',
copayerId: this.copayerId,
});
};
Network.prototype._deletePeer = function(peerId) {
delete this.isInboundPeerAuth[peerId];
delete this.copayerForPeer[peerId];
this.connectedPeers = Network._arrayRemove(peerId, this.connectedPeers);
};
Network.prototype._addCopayer = function(copayerId) {
var peerId = this.peerFromCopayer(copayerId);
this._addCopayerMap(peerId, copayerId);
Network._arrayPushOnce(peerId, this.connectedPeers);
};
Network.prototype._addConnectedCopayer = function(copayerId) {
this._addCopayer(copayerId);
this.emit('connect', copayerId);
};
Network.prototype.getKey = function() {
preconditions.checkState(this.privkey || this.key);
if (!this.key) {
var key = new bitcore.Key();
key.private = new Buffer(this.privkey, 'hex');
key.regenerateSync();
this.key = key;
}
return this.key;
};
//hex version of one's own nonce
Network.prototype.setHexNonce = function(networkNonce) {
if (networkNonce) {
if (networkNonce.length !== 16)
throw new Error('incorrect length of hex nonce');
this.networkNonce = new Buffer(networkNonce, 'hex');
} else
this.iterateNonce();
};
//hex version of copayers' nonces
Network.prototype.setHexNonces = function(networkNonces) {
for (var i in networkNonces) {
if (!this.networkNonces)
this.networkNonces = {};
if (networkNonces[i].length === 16)
this.networkNonces[i] = new Buffer(networkNonces[i], 'hex');
}
};
//for oneself
Network.prototype.getHexNonce = function() {
return this.networkNonce.toString('hex');
};
//for copayers
Network.prototype.getHexNonces = function() {
var networkNoncesHex = [];
for (var i in this.networkNonces) {
networkNoncesHex[i] = this.networkNonces[i].toString('hex');
}
return networkNoncesHex;
};
Network.prototype.iterateNonce = function() {
if (!this.networkNonce || this.networkNonce.length !== 8) {
this.networkNonce = new Buffer(8);
this.networkNonce.fill(0);
}
//the first 4 bytes of a nonce is a unix timestamp in seconds
//the second 4 bytes is just an iterated "sub" nonce
//the whole thing is interpreted as one big endian number
var noncep1 = this.networkNonce.slice(0, 4);
noncep1.writeUInt32BE(Math.floor(Date.now() / 1000), 0);
var noncep2uint = this.networkNonce.slice(4, 8).readUInt32BE(0);
var noncep2 = this.networkNonce.slice(4, 8);
noncep2.writeUInt32BE(noncep2uint + 1, 0);
this.networkNonce = Buffer.concat([noncep1, noncep2], 8);
return this.networkNonce;
};
Network.prototype.decode = function(enc) {
var sender = enc.pubkey;
var key = this.getKey();
var prevnonce = this.networkNonces ? this.networkNonces[sender] : undefined;
var opts = {
prevnonce: prevnonce
};
var decoded = AuthMessage.decode(key, enc, opts);
//if no error thrown in the last step, we can set the copayer's nonce
if (!this.networkNonces)
this.networkNonces = {};
this.networkNonces[sender] = decoded.nonce;
var payload = decoded.payload;
return payload;
};
Network.prototype._onMessage = function(enc) {
var sender = enc.pubkey;
try {
var payload = this.decode(enc);
} catch (e) {
this._deletePeer(sender);
return;
}
if (this.ignoreMessageFromTs && this.ignoreMessageFromTs === enc.ts) {
log.debug('Ignoring trailing message. Ts:', enc.ts);
return;
}
var self = this;
switch (payload.type) {
case 'hello':
if (typeof payload.secretNumber === 'undefined' || payload.secretNumber !== this.secretNumber) {
this._sendRejectConnection(sender);
this._deletePeer(enc.pubkey, 'incorrect secret number');
return;
}
// if we locked allowed copayers, check if it belongs
if (this.allowedCopayerIds && !this.allowedCopayerIds[payload.copayerId]) {
this._sendRejectConnection(sender);
this._deletePeer(sender);
return;
}
//ensure claimed public key is actually the public key of the peer
//e.g., their public key should hash to be their peerId
if (sender !== payload.copayerId) {
this._sendRejectConnection(sender);
this._deletePeer(enc.pubkey, 'incorrect pubkey for peerId');
return;
}
this._addConnectedCopayer(payload.copayerId);
break;
default:
this.emit('data', sender, payload, enc.ts);
}
};
Network.prototype._setupSocketHandlers = function(opts, cb) {
preconditions.checkState(this.socket);
log.debug('setting up connection', opts);
var self = this;
self.socket.on('connect_error', function(m) {
// If socket is not started, destroy it and emit and error
// If it is started, socket.io will try to reconnect.
if (!self.started) {
self.emit('connect_error');
self.cleanUp();
}
});
self.socket.on('subscribed', function(m) {
var fromTs = opts.syncedTimestamp || 0;
// We ask for this message, and then ignore it, only to see if the
// server has erased our old messages.
if (fromTs) {
self.ignoreMessageFromTs = fromTs;
}
log.info('Async: synchronizing from: ', fromTs);
self.socket.emit('sync', fromTs);
self.started = true;
});
self.socket.on('message', function(m) {
// delay execution, to improve error handling
setTimeout(function() {
self._onMessage(m);
}, 1);
});
self.socket.on('error', self._onError.bind(self));
self.socket.on('no_messages', self.emit.bind(self, 'no_messages'));
self.socket.on('no messages', self.emit.bind(self, 'no_messages'));
self.socket.on('connect', function() {
var pubkey = self.getKey().public.toString('hex');
log.debug('Async subscribing to pubkey:', pubkey);
self.socket.emit('subscribe', pubkey);
self.socket.on('disconnect', function() {
self.socket.emit('subscribe', pubkey);
});
if (typeof cb === 'function') cb();
});
};
Network.prototype._onError = function(err) {
log.debug('RECV ERROR: ', err);
log.debug(err.stack);
this.criticalError = err.message;
};
Network.prototype.greet = function(copayerId, secretNumber) {
this._sendHello(copayerId, secretNumber);
var peerId = this.peerFromCopayer(copayerId);
this._addCopayerMap(peerId, copayerId);
};
Network.prototype._addCopayerMap = function(peerId, copayerId) {
if (!this.copayerForPeer[peerId]) {
if (Object.keys(this.copayerForPeer).length < this.maxPeers) {
this.copayerForPeer[peerId] = copayerId;
}
}
};
Network.prototype._setInboundPeerAuth = function(peerId) {
this.isInboundPeerAuth[peerId] = true;
};
Network.prototype.setCopayerId = function(copayerId) {
preconditions.checkState(!this.started, 'network already started: can not change peerId');
this.copayerId = copayerId;
this.copayerIdBuf = new Buffer(copayerId, 'hex');
this.peerId = this.peerFromCopayer(this.copayerId);
this._addCopayerMap(this.peerId, copayerId);
};
// TODO cache this.
Network.prototype.peerFromCopayer = function(hex) {
var SIN = bitcore.SIN;
return new SIN(new Buffer(hex, 'hex')).toString();
};
Network.prototype.start = function(opts, openCallback) {
preconditions.checkArgument(opts);
preconditions.checkArgument(opts.privkey);
preconditions.checkArgument(opts.copayerId);
if (this.started) {
log.debug('Async: Networing already started for this wallet.')
return openCallback();
}
this.privkey = opts.privkey;
this.setCopayerId(opts.copayerId);
this.maxPeers = opts.maxPeers || this.maxPeers;
this.socket = this.createSocket();
this._setupSocketHandlers(opts, openCallback);
};
Network.prototype.createSocket = function() {
log.debug('Async: Connecting to socket:', this.url);
return io.connect(this.url, this.socketOptions);
};
Network.prototype.getOnlinePeerIDs = function() {
return this.connectedPeers;
};
Network.prototype.getCopayerIds = function() {
if (this.allowedCopayerIds) {
return Object.keys(this.allowedCopayerIds);
} else {
var copayerIds = [];
for (var peerId in this.copayerForPeer) {
copayerIds.push(this.copayerForPeer[peerId]);
}
return copayerIds;
}
};
Network.prototype.send = function(dest, payload, cb) {
preconditions.checkState(this.socket);
preconditions.checkArgument(payload);
var self = this;
if (!dest) {
dest = this.getCopayerIds();
payload.isBroadcast = 1;
}
if (typeof dest === 'string')
dest = [dest];
var l = dest.length;
var i = 0;
for (var ii in dest) {
var to = dest[ii];
if (to == this.copayerId)
continue;
var message = this.encode(to, payload);
this.socket.emit('message', message);
}
if (typeof cb === 'function') cb();
};
Network.prototype.encode = function(copayerId, payload, nonce) {
this.iterateNonce();
var opts = {
nonce: nonce || this.networkNonce
};
var copayerIdBuf = new Buffer(copayerId, 'hex');
var message = AuthMessage.encode(copayerIdBuf, this.getKey(), payload, opts);
return message;
};
Network.prototype.isOnline = function() {
return !!this.socket;
};
Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) {
this.allowedCopayerIds = {};
for (var i in allowedCopayerIdsArray) {
this.allowedCopayerIds[allowedCopayerIdsArray[i]] = true;
}
};
Network.prototype.setCopayers = function(copayersIdsArray) {
for (var i in copayersIdsArray) {
this._addCopayer(copayersIdsArray[i]);
}
};
module.exports = Network;

View file

@ -1,256 +0,0 @@
'use strict';
var Identity = require('./Identity');
var Wallet = require('./Wallet');
var cryptoUtils = require('../util/crypto');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var sjcl = require('../../lib/sjcl');
var log = require('../util/log');
var preconditions = require('preconditions').instance();
var _ = require('lodash');
var Compatibility = {};
Compatibility.iterations = 100;
Compatibility.salt = 'mjuBtGybi/4=';
/**
* Reads from localstorage wallets saved previously to 0.8
*/
Compatibility._getWalletIds = function(cb) {
preconditions.checkArgument(cb);
var walletIds = [];
var uniq = {};
var key;
for (key in localStorage) {
var split = key.split('::');
if (split.length == 2) {
var walletId = split[0];
if (!walletId || walletId === 'nameFor' || walletId === 'lock' || walletId === 'wallet') {
continue;
}
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
return cb(walletIds);
};
/**
* @param {string} encryptedWallet - base64-encoded encrypted wallet
* @param {string} password
* @returns {Object}
*/
Compatibility.importLegacy = function(encryptedWallet, password) {
var passphrase = this.kdf(password);
var ret = Compatibility._decrypt(encryptedWallet, passphrase);
if (!ret) return null;
return ret;
};
/**
* Decrypts using the CryptoJS library (unknown encryption schema)
*
* Don't use CryptoJS to encrypt. This still exists for compatibility reasons only.
*/
Compatibility._decrypt = function(base64, passphrase) {
var decryptedStr = null;
try {
var decrypted = CryptoJS.AES.decrypt(base64, passphrase);
if (decrypted)
decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
} catch (e) {
// Error while decrypting
return null;
}
return decryptedStr;
};
/**
* Reads an item from localstorage, decrypts it with passphrase
*/
Compatibility._read = function(k, passphrase, cb) {
preconditions.checkArgument(cb);
var ret = localStorage.getItem(k);
if (!ret) return cb(null);
var ret = self._decrypt(ret, passphrase);
if (!ret) return cb(null);
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
return ret;
};
Compatibility.getWallets_Old = function(cb) {
preconditions.checkArgument(cb);
var wallets = [];
var self = this;
this._getWalletIds(function(ids) {
if (!ids.length) {
return cb([]);
}
_.each(ids, function(id) {
var name = localStorage.getItem('nameFor::' + id);
if (name) {
wallets.push({
id: id,
name: name,
});
}
});
return cb(wallets);
});
};
Compatibility.getWallets2 = function(cb) {
var self = this;
var re = /wallet::([^_]+)(_?(.*))/;
var va = /^{+/;
var key;
var keys = [];
for (key in localStorage) {
keys.push(key);
}
var wallets = _.compact(_.map(keys, function(key) {
if (key.indexOf('wallet::') !== 0)
return null;
var match = key.match(re);
var matchValue = localStorage[key].match(va);
if (match.length != 4)
return null;
if (matchValue)
return null;
return {
id: match[1],
name: match[3] ? match[3] : undefined,
value: localStorage[key]
};
}));
return cb(wallets);
};
/**
* Lists all wallets in localstorage
*/
Compatibility.listWalletsPre8 = function(cb) {
var self = this;
self.getWallets2(function(wallets) {
self.getWallets_Old(function(wallets2) {
var ids = _.pluck(wallets, 'id');
_.each(wallets2, function(w) {
if (!_.contains(ids, w.id))
wallets.push(w);
});
return cb(wallets);
});
})
};
/**
* Retrieves a wallet that predates the 0.8 release
*/
Compatibility.readWalletPre8 = function(walletId, password, cb) {
var self = this;
var passphrase = cryptoUtils.kdf(password);
var obj = {};
var key;
for (key in localStorage) {
if (key.indexOf('wallet::' + walletId) !== -1) {
var ret = self._read(localStorage.getItem(key), passphrase);
if (err) return cb(err);
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
}
}
};
Compatibility.importEncryptedWallet = function(identity, cypherText, password, opts, cb) {
var crypto = (opts && opts.cryptoUtil) || cryptoUtils;
var obj = crypto.decrypt(password, cypherText);
if (!obj) {
// 0.7.3 broken KDF
log.debug('Trying legacy encryption 0.7.2...');
var passphrase = crypto.kdf(password, 'mjuBtGybi/4=', 100);
obj = crypto.decrypt(passphrase, cypherText);
}
if (!obj) {
log.info("Could not decrypt, trying legacy..");
obj = Compatibility.importLegacy(cypherText, password);
};
if (!obj) {
return cb('Could not decrypt', null);
}
try {
obj = JSON.parse(obj);
} catch (e) {
return cb('Could not read encrypted wallet', null);
}
return identity.importWalletFromObj(obj, opts, cb);
};
/**
* @desc Generate a WordArray expanding a password
*
* @param {string} password - the password to expand
* @returns WordArray 512 bits with the expanded key generated from password
*/
Compatibility.kdf = function(password) {
var hash = sjcl.hash.sha256.hash(sjcl.hash.sha256.hash(password));
var salt = sjcl.codec.base64.toBits(this.salt);
var crypto2 = function(key, salt, iterations, length, alg) {
return sjcl.codec.hex.fromBits(sjcl.misc.pbkdf2(key, salt, iterations, length * 8,
alg == 'sha1' ? function(key) {
return new sjcl.misc.hmac(key, sjcl.hash.sha1)
} : null
))
};
var key512 = crypto2(hash, salt, this.iterations, 64, 'sha1');
var sbase64 = sjcl.codec.base64.fromBits(sjcl.codec.hex.toBits(key512));
return sbase64;
};
Compatibility.deleteOldWallet = function(walletObj) {
console.log('[Compatibility.js:249]',walletObj); //TODO
localStorage.removeItem('wallet::' + walletObj.id + '_' + walletObj.name);
log.info('Old wallet ' + walletObj.name + ' deleted: ' + walletObj.id);
};
module.exports = Compatibility;

View file

@ -1,274 +0,0 @@
'use strict';
// 83.8% typed (by google's closure-compiler account)
var preconditions = require('preconditions').singleton();
var HDPath = require('./HDPath');
var _ = require('lodash');
/**
* @desc
* HDParams is a class that encapsulates information about the current indexes
* of a copayer
*
* When a copayer creates a new wallet, his receiveIndex gets updated, and an
* address is generated from everybody's public key, using this BIP32 path:
* <pre> m/copay'/{copayer}/0/{index} </pre>
*
* When a copayer generates a transaction proposal, his changeIndex gets
* updated, and all funds from that transaction proposal go to the multisig
* address generated from this BIP32 path for all the copayers:
* <pre> m/copay'/{copayer}/1/{changeIndex} </pre>
*
* There's a shared index, <tt>HDPath.SHARED_INDEX</tt>, that serves to
* generate addresses common to everybody.
*
* @TODO: Should opts.cosigner go?
*
* @constructor
*
* @param {Object} opts - options for the construction of this object
* @param {number=} opts.cosigner - backwards compatible index of a copayer
* @param {number} opts.copayerIndex - the copayer that generated this branch
* of addresses
* @param {number} opts.receiveIndex - the current index for a the last receive
* address generated for this copayer
* @param {number} opts.changeIndex - the current index for a the last change
* address generated for this copayer
*/
function HDParams(opts) {
opts = opts || {};
//opts.cosigner is for backwards compatibility only
/**
* @public
* @type number
*/
this.copayerIndex = _.isUndefined(opts.copayerIndex) ? opts.cosigner : opts.copayerIndex;
/**
* @public
* @type number
*/
this.changeIndex = opts.changeIndex || 0;
/**
* @public
* @type number
*/
this.receiveIndex = opts.receiveIndex || 0;
if (_.isUndefined(this.copayerIndex)) {
this.copayerIndex = HDPath.SHARED_INDEX;
}
}
/**
* @desc
* Creates a set of HDParams, with one HDParams structure for each copayer and
* a shared path.
*
* @static
* @param {number} totalCopayers - the number of copayers in a wallet
* @returns {HDParams[]} a list of HDParams generated for a new empty wallet
*/
HDParams.init = function(totalCopayers) {
preconditions.shouldBeNumber(totalCopayers);
var ret = [new HDParams({receiveIndex: 1})];
for (var i = 0 ; i < totalCopayers ; i++) {
ret.push(new HDParams({copayerIndex: i}));
}
return ret;
};
/**
* @desc
* Generates a set of HDParams from a list with a object-serialized version of
* HDParams.
*
* @static
* @param {Object[]} hdParams - a list with objects
* @returns {HDParams[]} builds a HDParams for each object literal found in the list
*/
HDParams.fromList = function(hdParams) {
return hdParams.map(function(i) { return HDParams.fromObj(i); });
};
/**
* @desc
* Generate a HDParams from an object.
*
* This essentialy only calls new HDParams(data), but it's the interface being
* used everywhere to encode/decode.
*
* @TODO: This should be clarified - Or abstracted away - as it's a pattern
* used in multiple places.
*
* @static
* @param {Object} data - a serialized version of HDParams
* @return {HDParams}
* @throws {BADDATA} - when the parameter <tt>data</tt> already is an instance of
* HDParams.
*/
HDParams.fromObj = function(data) {
if (data instanceof HDParams) {
throw new Error('BADDATA', 'bad data format: Did you use .toObj()?');
}
return new HDParams(data);
};
/**
* @desc
* Serializes a list of HDParams to a list of "plain" objects, according to each
* element's <tt>toObj()</tt> method.
*
* @TODO: There should be a list of Classes that share this behaviour.
*
* @static
* @param {HDParams[]} hdParams - a list of HDParams objects.
* @returns {Array} an array with the <tt>toObj()</tt> serialization of each
* element in hdParams
*/
HDParams.serialize = function(hdParams) {
return hdParams.map(function(i) { return i.toObj(); });
};
/**
* @desc
* Creates a new HDParams set with <tt>totalCopayers+1</tt> elements.
*
* Sets the first (corresponding to the parameters for the shared addresses)
* HDParams object to match <tt>shared</tt>'s values. Returns a serialized
* version of this set
*
* <pre>
* var updateResult = HDParams.update({changeIndex: 1, receiveIndex: 2}, 5);
* // All the following asserts succeed
* assert(_.isArray(updateResult));
* assert(_.all(updateResult, function (hd) { return !(hd instanceOf HDParams); }));
* assert(_.size(updateResult) === 6);
* assert(updateResult[0].changeIndex === 1);
* assert(updateResult[0].receiveIndex === 2);
* </pre>
*
* @TODO: This method is badly coded, it does something that is very specific
* and kind of strange. I couldn't figure out why would it be needed.
*
* @static
* @param {HDParams} shared - an instance of HDParams
* @param {number} totalCopayers
* @return {Object[]}
*/
HDParams.update = function(shared, totalCopayers) {
var hdParams = this.init(totalCopayers);
hdParams[0].changeIndex = shared.changeIndex;
hdParams[0].receiveIndex = shared.receiveIndex;
return this.serialize(hdParams);
};
/**
* @desc
* Serializes this object
*
* @TODO: I couldn't realize why would this be needed - calling, for example,
* JSON.stringify would have the same result on this object than on the
* original instance
*
* @returns {Object} a serialized version that should be equal (in a deep object
* comparison) to the <tt>this</tt> instance if passed to the
* <tt>HDParams()</tt> constructor.
*/
HDParams.prototype.toObj = function() {
return {
copayerIndex: this.copayerIndex,
changeIndex: this.changeIndex,
receiveIndex: this.receiveIndex
};
};
/**
* @desc
* Throws an error if a given index falls out of the range for known addresses.
*
* @TODO: This is not a good pattern, exceptions should be for exceptional things.
*
* @param {number} index the index to check for
* @param {boolean} isChange whether to check for the change index or the
* receive address index
*/
HDParams.prototype.checkRange = function(index, isChange) {
if ((isChange && index > this.changeIndex) ||
(!isChange && index > this.receiveIndex)) {
throw new Error('Out of bounds at index ' + index + ' isChange: ' + isChange);
}
};
/**
* @desc
* Return this instance's changeIndex value
*
* @TODO: Somebody did a lot of java. Not sure if we need to be so verbose. If
* anything, let's just declare changeIndex as a read only variable
* @returns {number} this HDParams current index to generate a change address
*/
HDParams.prototype.getChangeIndex = function() {
return this.changeIndex;
};
/**
* @desc
* Return this instance's receiveIndex value
*
* @TODO: Somebody did a lot of java. Not sure if we need to be so verbose. If
* anything, let's just declare changeIndex as a read only variable
* @returns {number} this HDParams current index to generate a receive address
*/
HDParams.prototype.getReceiveIndex = function() {
return this.receiveIndex;
};
/**
* @desc
* Increment this instance's changeIndex or receiveIndex value
*
* @TODO: Somebody did a lot of java. Not sure if we need to be so verbose.
*
* @param {boolean} isChange - if true, change <tt>changeIndex</tt>
*/
HDParams.prototype.increment = function(isChange) {
if (isChange) {
this.changeIndex++;
} else {
this.receiveIndex++;
}
};
/**
* @desc
* Merge this instance with another HDParams instance.
*
* @TODO: Device a general approach to merges.
*
* @param {Object} inHDParams - the object to merge to
* @param {number} inHDParams.copayerIndex - the object to merge to
* @returns {boolean} true if this object has changed
*/
HDParams.prototype.merge = function(inHDParams) {
preconditions.shouldBeObject(inHDParams);
preconditions.checkArgument(this.copayerIndex == inHDParams.copayerIndex);
var hasChanged = false;
if (inHDParams.changeIndex > this.changeIndex) {
this.changeIndex = inHDParams.changeIndex;
hasChanged = true;
}
if (inHDParams.receiveIndex > this.receiveIndex) {
this.receiveIndex = inHDParams.receiveIndex;
hasChanged = true;
}
return hasChanged;
};
module.exports = HDParams;

View file

@ -1,116 +0,0 @@
'use strict';
// 90.2% typed (by google's closure-compiler account)
var preconditions = require('preconditions').singleton();
var _ = require('lodash');
/**
* @namespace
* @desc
* HDPath contains helper functions to handle BIP32 branches as
* Copay uses them.
* Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki
* <pre>
* m / purpose' / copayerIndex / change:boolean / addressIndex
* </pre>
*/
var HDPath = {};
/**
* @desc Copay's BIP45 purpose code
* @const
* @type number
*/
HDPath.PURPOSE = 45;
/**
* @desc Maximum number for non-hardened values (BIP32)
* @const
* @type number
*/
HDPath.MAX_NON_HARDENED = 0x80000000 - 1;
/**
* @desc Shared Index: used for creating addresses for no particular purpose
* @const
* @type number
*/
HDPath.SHARED_INDEX = HDPath.MAX_NON_HARDENED - 0;
/**
* @desc ???
* @const
* @type number
*/
HDPath.ID_INDEX = HDPath.MAX_NON_HARDENED - 1;
/**
* @desc BIP45 prefix for COPAY
* @const
* @type string
*/
HDPath.BIP45_PUBLIC_PREFIX = 'm/' + HDPath.PURPOSE + '\'';
/**
* @desc Retrieve a string to be used with bitcore representing a Copay branch
* @param {number} addressIndex - the last value of the HD derivation
* @param {boolean} isChange - whether this is a change address or a receive
* @param {number} copayerIndex - the index of the copayer in the pubkeyring
* @return {string} - the path for the HD derivation
*/
HDPath.Branch = function(addressIndex, isChange, copayerIndex) {
preconditions.checkArgument(_.isNumber(addressIndex));
preconditions.checkArgument(_.isBoolean(isChange));
var ret = 'm/' +
(typeof copayerIndex !== 'undefined' ? copayerIndex : HDPath.SHARED_INDEX) + '/' +
(isChange ? 1 : 0) + '/' +
addressIndex;
return ret;
};
/**
* @desc ???
* @param {number} addressIndex - the last value of the HD derivation
* @param {boolean} isChange - whether this is a change address or a receive
* @param {number} copayerIndex - the index of the copayer in the pubkeyring
* @return {string} - the path for the HD derivation
*/
HDPath.FullBranch = function(addressIndex, isChange, copayerIndex) {
preconditions.checkArgument(_.isNumber(addressIndex));
preconditions.checkArgument(_.isBoolean(isChange));
var sub = HDPath.Branch(addressIndex, isChange, copayerIndex);
sub = sub.substring(2);
return HDPath.BIP45_PUBLIC_PREFIX + '/' + sub;
};
/**
* @desc
* Decompose a string and retrieve its arguments as if it where a Copay address.
* @param {string} path - the HD path
* @returns {Object} an object with three keys: addressIndex, isChange, and
* copayerIndex
*/
HDPath.indexesForPath = function(path) {
preconditions.checkArgument(_.isString(path));
var s = path.split('/');
return {
isChange: s[3] === '1',
addressIndex: parseInt(s[4], 10),
copayerIndex: parseInt(s[2], 10)
};
};
/**
* @desc The ID for a shared branch
*/
HDPath.IdFullBranch = HDPath.FullBranch(0, false, HDPath.ID_INDEX);
/**
* @desc Partial ID for a shared branch
*/
HDPath.IdBranch = HDPath.Branch(0, false, HDPath.ID_INDEX);
module.exports = HDPath;

View file

@ -1,905 +0,0 @@
'use strict';
var _ = require('lodash');
var preconditions = require('preconditions').singleton();
var inherits = require('inherits');
var events = require('events');
var async = require('async');
var bitcore = require('bitcore');
var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing');
var PrivateKey = require('./PrivateKey');
var Wallet = require('./Wallet');
var PluginManager = require('./PluginManager');
var Async = require('./Async');
var cryptoUtil = require('../util/crypto');
var log = require('../util/log');
var version = require('../../version').version;
/**
* @desc
* Identity - stores the state for a wallet in creation
*
* @param {Object} opts - configuration for this wallet
* @param {string} opts.fullName
* @param {string} opts.email
* @param {string} opts.password
* @param {string} opts.storage
* @param {string} opts.pluginManager
* @param {Object} opts.walletDefaults
* @param {string} opts.version
* @param {Object} opts.wallets
* @param {Object} opts.network
* @param {string} opts.network.testnet
* @param {string} opts.network.livenet
* @constructor
*/
function Identity(opts) {
preconditions.checkArgument(opts);
opts = _.extend({}, opts);
this.networkOpts = {
'livenet': opts.network.livenet,
'testnet': opts.network.testnet,
};
this.blockchainOpts = {
'livenet': opts.network.livenet,
'testnet': opts.network.testnet,
};
this.fullName = opts.fullName || opts.email;
this.email = opts.email;
this.password = opts.password;
this.storage = opts.storage || opts.pluginManager.get('DB');
this.storage.setCredentials(this.email, this.password, {});
this.walletDefaults = opts.walletDefaults || {};
this.version = opts.version || version;
this.walletIds = opts.walletIds || [];
this.wallets = opts.wallets || {};
this.focusedTimestamps = opts.focusedTimestamps || {};
this.backupNeeded = opts.backupNeeded || false;
};
inherits(Identity, events.EventEmitter);
Identity.getStoragePrefix = function() {
return 'profile::';
};
Identity.getKeyForEmail = function(email) {
return Identity.getStoragePrefix() + bitcore.util.sha256ripe160(email).toString('hex');
};
Identity.prototype.getChecksumForStorage = function() {
return JSON.stringify(_.sortBy(this.walletIds));
};
Identity.prototype.getId = function() {
return Identity.getKeyForEmail(this.email);
};
Identity.prototype.getName = function() {
return this.fullName || this.email;
};
/**
* Creates an Identity
*
* @param opts
* @param cb
* @return {undefined}
*/
Identity.create = function(opts, cb) {
opts = _.extend({
backupNeeded: true
}, opts);
var iden = new Identity(opts);
iden.store(_.extend(opts, {
failIfExists: true
}), function(err) {
if (err) return cb(err);
return cb(null, iden);
});
};
Identity.prototype.resendVerificationEmail = function(cb) {
var self = this;
preconditions.checkArgument(_.isFunction(cb));
preconditions.checkState(_.isFunction(self.storage.resendVerificationEmail));
self.storage.resendVerificationEmail(cb);
};
/**
* Open an Identity from the given storage.
*
* After opening a profile, and setting its wallet event handlers,
* the client must run .netStart on each
* (probably on iden's newWallet handler
*
* @param {Object} opts
* @param {Object} opts.storage
* @param {string} opts.email
* @param {string} opts.password
* @param {Function} cb
*/
Identity.open = function(opts, cb) {
preconditions.checkArgument(_.isObject(opts));
preconditions.checkArgument(_.isFunction(cb));
var storage = opts.storage || opts.pluginManager.get('DB');
storage.setCredentials(opts.email, opts.password, opts);
storage.getItem(Identity.getKeyForEmail(opts.email), function(err, data, headers) {
var exported;
if (err) {
return cb(err);
}
try {
exported = JSON.parse(data);
} catch (e) {
return cb(e);
}
return cb(null, new Identity(_.extend(opts, exported)), headers);
});
};
Identity.prototype.verifyChecksum = function(cb) {
var self = this;
self.storage.getItem(Identity.getKeyForEmail(self.email), function(err, data, headers) {
var iden;
if (err) return cb(err);
try {
iden = JSON.parse(data);
} catch (e) {
return cb(e);
}
return cb(null, self.getChecksumForStorage() == self.getChecksumForStorage.call(iden));
});
};
/**
* @param {string} walletId
* @returns {Wallet}
*/
Identity.prototype.getWalletById = function(walletId) {
return this.wallets[walletId];
};
/**
* @returns {Wallet[]}
*/
Identity.prototype.getWallets = function() {
return _.values(this.wallets);
};
/**
* addWallet
*
* @param w
*/
Identity.prototype.addWallet = function(w) {
this.wallets[w.getId()] = w;
this.walletIds = _.union(this.walletIds, [w.getId()]);
};
/**
* @desc Deletes a wallet. This involves removing it from the storage instance
*
* @param {string} walletId
* @callback cb
* @return {err}
*/
Identity.prototype.deleteWallet = function(walletId, cb) {
preconditions.checkArgument(_.isString(walletId));
var self = this;
self.verifyChecksum(function(err, match) {
if (err) return cb(err);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
var w = self.getWalletById(walletId);
w.close();
delete self.wallets[walletId];
delete self.focusedTimestamps[walletId];
self.walletIds = _.without(self.walletIds, walletId);
self.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
if (err) return cb(err);
self.emitAndKeepAlive('walletDeleted', walletId);
if (!self.walletIds.length) {
self.emitAndKeepAlive('noWallets')
}
self.store({
noWallets: true
}, cb);
});
});
};
/**
* readAndBindWallet
*
* @param {string} wid walletId to be readed
* @param {function} cb
*
*/
Identity.prototype.readAndBindWallet = function(walletId, cb) {
var self = this;
self.retrieveWalletFromStorage(walletId, {}, function(error, wallet) {
if (!error) {
self.addWallet(wallet);
self.bindWallet(wallet);
}
return cb(error);
});
};
Identity.prototype.emitAndKeepAlive = function(args) {
var args = Array.prototype.slice.call(arguments);
log.debug('Ident Emitting:', args);
//this.keepAlive(); // TODO
this.emit.apply(this, arguments);
};
/**
* @desc open profile's wallets. Call it AFTER setting
* the proper even listeners. no callback.
*
*/
Identity.prototype.openWallets = function() {
var self = this;
if (_.isEmpty(self.walletIds)) {
self.emitAndKeepAlive('noWallets')
return;
}
// First read the lastFocused wallet
self.walletIds.sort(function(a, b) {
var va = self.focusedTimestamps[a] || 0;
var vb = self.focusedTimestamps[b] || 0;
return va < vb ? 1 : (va === vb ? 0 : -1);
});
// opens the wallets, in the order they were last accessed. Emits open events (newWallet)
async.eachSeries(self.walletIds, function(walletId, a_cb) {
self.readAndBindWallet(walletId, a_cb);
});
};
/**
* @param {string} walletId
* @param {} opts
* opts.importWallet
* @param {Function} cb
*/
Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, cb) {
var self = this;
var importFunction = opts.importWallet || Wallet.fromUntrustedObj;
this.storage.getItem(Wallet.getStorageKey(walletId), function(error, walletData) {
if (error) {
return cb(error);
}
try {
log.info('## OPENING Wallet:', walletId);
if (_.isString(walletData)) {
walletData = JSON.parse(walletData);
}
var readOpts = {
networkOpts: self.networkOpts,
blockchainOpts: self.blockchainOpts,
skipFields: []
};
} catch (e) {
log.debug("ERROR: ", e.message);
if (e && e.message && e.message.indexOf('MISSOPTS') !== -1) {
return cb(new Error('WERROR: Could not read: ' + walletId + ': ' + e.message));
} else {
return cb(e);
}
}
return cb(null, importFunction(walletData, readOpts));
});
};
/**
* @param {Wallet} wallet
* @param {Function} cb
*/
Identity.prototype.storeWallet = function(wallet, cb) {
var self = this;
preconditions.checkArgument(wallet && _.isObject(wallet));
wallet.setVersion(this.version);
var val = wallet.toObj();
var key = wallet.getStorageKey();
log.debug('Storing wallet:' + wallet.getName());
this.storage.setItem(key, val, function(err) {
if (err) {
log.error('Wallet:' + wallet.getName() + ' could not be stored:', err);
log.error('Wallet:' + wallet.getName() + ' Size:', JSON.stringify(wallet.sizes()));
if (err.match('OVERQUOTA')) {
self.emitAndKeepAlive('walletStorageError', wallet.getId(), 'Storage limits on remote server exceeded');
} else {
self.emitAndKeepAlive('walletStorageError', wallet.getId(), err);
}
}
if (cb)
return cb(err);
});
};
/**
* @param {Identity} identity
* @param {Wallet} wallet
* @param {Function} cb
*/
Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) {
identity.storeWallet(wallet, cb);
}, 3000);
Identity.prototype.toObj = function() {
return _.pick(this, 'walletIds', 'version', 'fullName', 'password', 'email', 'backupNeeded', 'focusedTimestamps');
};
Identity.prototype.exportEncryptedWithWalletInfo = function(opts) {
var crypto = opts.cryptoUtil || cryptoUtil;
return crypto.encrypt(this.password, this.exportWithWalletInfo(opts));
};
Identity.prototype.setBackupNeeded = function(backupNeeded) {
var self = this;
self.backupNeeded = !!backupNeeded;
self.verifyChecksum(function(err, match) {
if (err) {
log.error(err);
return;
}
if (!match) {
log.error('The profile is out of sync. Please re-login to get the latest changes.');
return;
}
self.store({
noWallets: true
}, function() {});
});
}
Identity.prototype.exportWithWalletInfo = function(opts) {
return _.extend({
wallets: _.map(this.getWallets(), function(wallet) {
return wallet.toObj();
})
},
_.pick(this, 'version', 'fullName', 'password', 'email', 'backupNeeded')
);
};
/**
* @param {Object} opts
* @param {Function} cb
*/
Identity.prototype.store = function(opts, cb) {
var self = this;
opts = opts || {};
var storeFunction = opts.failIfExists ? self.storage.createItem : self.storage.setItem;
storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) {
if (err) return cb(err);
if (opts.noWallets) return cb();
async.each(self.getWallets(), function(wallet, in_cb) {
self.storeWallet(wallet, in_cb);
}, cb);
});
};
/**
* @param {Object} opts
* @param {Function} cb
*/
Identity.prototype.remove = function(opts, cb) {
log.debug('Deleting profile');
var self = this;
opts = opts || {};
async.each(self.getWallets(), function(w, cb) {
w.close();
self.storage.removeItem(Wallet.getStorageKey(w.getId()), function(err) {
if (err) return cb(err);
cb();
});
}, function(err) {
if (err) return cb(err);
self.storage.removeItem(self.getId(), function(err) {
if (err) return cb(err);
self.storage.clear(function(err) {
if (err) return cb(err);
self.emitAndKeepAlive('closed');
return cb();
});
});
});
};
Identity.prototype._cleanUp = function() {
var self = this;
_.each(this.getWallets(), function(w) {
w.close();
delete self.wallets[w.getId()];
});
};
/**
* @desc Closes the wallet and disconnects all services
*/
Identity.prototype.close = function(cb) {
var self = this;
function doClose() {
self._cleanUp();
self.emitAndKeepAlive('closed');
if (cb) return cb();
};
self.verifyChecksum(function(err, match) {
if (!err && match) {
self.store({
noWallets: true,
}, function(err) {
return doClose();
});
} else {
return doClose();
}
});
};
Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
var self = this;
preconditions.checkArgument(cb);
self.verifyChecksum(function(err, match) {
if (err) return cb(err);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
var importFunction = opts.importWallet || Wallet.fromUntrustedObj;
var readOpts = {
networkOpts: self.networkOpts,
blockchainOpts: self.blockchainOpts,
skipFields: opts.skipFields,
};
var w = importFunction(obj, readOpts);
if (!w) return cb(new Error('Could not decrypt'));
log.debug('Wallet decrypted:' + w.getName());
self._checkVersion(w.version);
log.debug('Updating Indexes for wallet:' + w.getName());
w.updateIndexes(function(err) {
log.debug('Adding wallet to profile:' + w.getName());
self.storeWallet(w, function(err) {
if (err) return cb(err);
self.addWallet(w);
self.updateFocusedTimestamp(w.getId());
self.bindWallet(w);
self.backupNeeded = true;
self.store({
noWallets: true,
}, function(err) {
return cb(err);
});
});
});
});
};
Identity.prototype.importMultipleWalletsFromObj = function(objs, opts) {
var self = this;
opts = opts || {};
async.eachSeries(objs, function(walletData, cb) {
if (!walletData)
return cb();
self.importWalletFromObj(walletData, opts, cb);
});
};
/**
* @param {Wallet} wallet
* @param {Function} cb
*/
Identity.prototype.closeWallet = function(wallet, cb) {
preconditions.checkState(wallet, 'Wallet not found');
var self = this;
wallet.close(function(err) {
return cb(err);
});
};
Identity.importFromEncryptedFullJson = function(ejson, password, opts, cb) {
var crypto = opts.cryptoUtil || cryptoUtil;
var str = crypto.decrypt(password, ejson);
if (!str) {
// 0.7.3 broken KDF
log.debug('Trying legacy encryption...');
var passphrase = crypto.kdf(password, 'mjuBtGybi/4=', 100);
str = crypto.decrypt(passphrase, ejson);
}
if (!str)
return cb('BADSTR');
return Identity.importFromFullJson(str, password, opts, cb);
};
Identity.importFromFullJson = function(str, password, opts, cb) {
preconditions.checkArgument(str);
var json;
try {
json = JSON.parse(str);
} catch (e) {
return cb('BADSTR: Unable to retrieve json from string', str);
}
var email = json.email;
opts.email = email;
opts.password = password;
if (!email)
return cb('BADSTR');
var iden = new Identity(opts);
opts.failIfExists = true;
json.wallets = json.wallets || {};
iden.store(opts, function(err) {
if (err) return cb(err); //profile already exists
return cb(null, iden, json.wallets);
});
};
/**
* @desc binds a wallet's events and emits 'newWallet'
* @param {string} walletId Wallet id to be binded
* @emits newWallet (walletId)
*/
Identity.prototype.bindWallet = function(w) {
preconditions.checkArgument(w && this.getWalletById(w.getId()));
var self = this;
log.debug('Binding wallet:' + w.getName());
w.on('txProposalsUpdated', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('paymentAck', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('newAddresses', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('settingsUpdated', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('txProposalEvent', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('addressBookUpdated', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('publicKeyRingUpdated', function() {
Identity.storeWalletDebounced(self, w);
});
w.on('ready', function() {
Identity.storeWalletDebounced(self, w);
});
this.emitAndKeepAlive('newWallet', w.getId());
};
/**
* @desc This method prepares options for a new Wallet
*
* @param {Object} opts
* @param {string} opts.id
* @param {PrivateKey=} opts.privateKey
* @param {string=} opts.privateKeyHex
* @param {number} opts.requiredCopayers
* @param {number} opts.totalCopayers
* @param {PublicKeyRing=} opts.publicKeyRing
* @param {string} opts.nickname
* @param {string} opts.password
* @param {boolean} opts.spendUnconfirmed this.walletDefaults.spendUnconfirmed
* @param {number} opts.reconnectDelay time in milliseconds
* @param {number=} opts.version
* @param {callback} opts.version
* @return {Wallet}
*/
Identity.prototype.createWallet = function(opts, cb) {
preconditions.checkArgument(cb);
var self = this;
self.verifyChecksum(function(err, match) {
if (err) return cb(err);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
opts = opts || {};
opts.networkName = opts.networkName || 'testnet';
log.debug('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID') + (opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey'));
var privOpts = {
networkName: opts.networkName,
};
if (opts.privateKeyHex && opts.privateKeyHex.length > 1) {
privOpts.extendedPrivateKeyString = opts.privateKeyHex;
}
opts.privateKey = opts.privateKey || new PrivateKey(privOpts);
var requiredCopayers = opts.requiredCopayers || self.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || self.walletDefaults.totalCopayers;
opts.lockTimeoutMin = self.walletDefaults.idleDurationMin;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: opts.networkName,
requiredCopayers: requiredCopayers,
totalCopayers: totalCopayers,
});
opts.publicKeyRing.addCopayer(
opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(),
opts.nickname || self.getName()
);
log.debug('\t### PublicKeyRing Initialized');
opts.txProposals = opts.txProposals || new TxProposals({
networkName: opts.networkName,
});
var walletClass = opts.walletClass || Wallet;
log.debug('\t### TxProposals Initialized');
opts.networkOpts = self.networkOpts;
opts.blockchainOpts = self.blockchainOpts;
opts.spendUnconfirmed = opts.spendUnconfirmed || self.walletDefaults.spendUnconfirmed;
opts.reconnectDelay = opts.reconnectDelay || self.walletDefaults.reconnectDelay;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || self.version;
var w = new walletClass(opts);
if (self.getWalletById(w.getId())) {
return cb('walletAlreadyExists');
}
self.storeWallet(w, function(err) {
if (err) return cb(err);
self.addWallet(w);
self.updateFocusedTimestamp(w.getId());
self.bindWallet(w);
self.backupNeeded = true;
self.store({
noWallets: true,
}, function(err) {
return cb(err, w);
});
});
});
};
/**
* @desc Checks if a version is compatible with the current version
* @param {string} inVersion - a version, with major, minor, and revision, period-separated (x.y.z)
* @throws {Error} if there's a major version difference
*/
Identity.prototype._checkVersion = function(inVersion) {
if (inVersion) {
var thisV = this.version.split('.');
var thisV0 = parseInt(thisV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
}
//We only check for major version differences
if (thisV0 < inV0) {
throw new Error('Major difference in software versions' +
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
}
};
/**
* @desc Pass through to {@link Wallet#secret}
*/
Identity.prototype.decodeSecret = function(secret) {
try {
return Wallet.decodeSecret(secret);
} catch (e) {
return false;
}
};
/**
* getLastFocusedWalletId
*
* @return {string} walletId
*/
Identity.prototype.getLastFocusedWalletId = function() {
if (this.walletIds.length == 0) return undefined;
var max = _.max(this.focusedTimestamps);
if (!max)
return this.walletIds[0];
return _.findKey(this.focusedTimestamps, function(ts) {
return ts == max;
}) || this.walletIds[0];
};
Identity.prototype.updateFocusedTimestamp = function(wid) {
preconditions.checkArgument(wid && this.getWalletById(wid));
this.focusedTimestamps[wid] = Date.now();
};
/**
* @callback walletCreationCallback
* @param {?} err - an error, if any, that happened during the wallet creation
* @param {Wallet=} wallet - the wallet created
*/
/**
* @desc Start the network functionality.
*
* Start up the Network instance and try to join a wallet defined by the
* parameter <tt>secret</tt> using the parameter <tt>nickname</tt>. Encode
* information locally using <tt>passphrase</tt>. <tt>privateHex</tt> is the
* private extended master key. <tt>cb</tt> has two params: error and wallet.
*
* @param {object} opts
* @param {string} opts.secret - the wallet secret
* @param {string} opts.nickname - a nickname for the current user
* @param {string} opts.privateHex - the private extended master key
* @param {walletCreationCallback} cb - a callback
*/
Identity.prototype.joinWallet = function(opts, cb) {
preconditions.checkArgument(opts);
preconditions.checkArgument(opts.secret);
preconditions.checkArgument(cb);
var self = this;
var decodedSecret = this.decodeSecret(opts.secret);
if (!decodedSecret || !decodedSecret.networkName || !decodedSecret.pubKey) {
return cb('badSecret');
}
var privOpts = {
networkName: decodedSecret.networkName,
};
if (opts.privateHex && opts.privateHex.length > 1) {
privOpts.extendedPrivateKeyString = opts.privateHex;
}
//Create our PrivateK
var privateKey = new PrivateKey(privOpts);
log.debug('\t### PrivateKey Initialized');
var joinOpts = {
copayerId: privateKey.getId(),
privkey: privateKey.getIdPriv(),
key: privateKey.getIdKey(),
secretNumber: decodedSecret.secretNumber,
};
var joinNetwork = opts.Async || new Async(this.networkOpts[decodedSecret.networkName]);
// This is a hack to reconize if the connection was rejected or the peer wasn't there.
var connectedOnce = false;
joinNetwork.on('connected', function(sender, data) {
connectedOnce = true;
});
joinNetwork.on('connect_error', function() {
return cb('connectionError');
});
joinNetwork.on('serverError', function() {
return cb('joinError');
});
joinNetwork.start(joinOpts, function() {
joinNetwork.greet(decodedSecret.pubKey, joinOpts.secretNumber);
joinNetwork.on('data', function(sender, data) {
if (data.type === 'walletId' && data.opts) {
if (!data.networkName || data.networkName !== decodedSecret.networkName) {
return cb('badNetwork');
}
data.opts.networkName = data.networkName;
var walletOpts = _.clone(data.opts);
walletOpts.id = data.walletId;
walletOpts.network = joinNetwork;
walletOpts.privateKey = privateKey;
walletOpts.nickname = opts.nickname || self.getName();
if (opts.password)
walletOpts.password = opts.password;
self.createWallet(walletOpts, function(err, w) {
if (w) {
w.sendWalletReady(decodedSecret.pubKey);
} else {
if (!err) {
err = 'walletFull';
}
}
return cb(err, w);
});
}
});
});
};
module.exports = Identity;

View file

@ -1,346 +0,0 @@
'use strict';
var util = require('util');
var async = require('async');
var request = require('request');
var io = require('socket.io-client');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var preconditions = require('preconditions').singleton();
var bitcore = require('bitcore');
var log = require('../util/log.js');
/*
This class lets interface with the blockchain, making general queries and
subscribing to transactions on addresses and blocks.
Opts:
- url
- reconnection (optional)
- reconnectionDelay (optional)
Events:
- tx: activity on subscribed address.
- block: a new block that includes a subscribed address.
- connect: the connection with the blockchain is ready.
- disconnect: the connection with the blochckain is unavailable.
*/
var Insight = function(opts) {
preconditions.checkArgument(opts)
.shouldBeObject(opts)
.checkArgument(opts.url)
this.status = this.STATUS.DISCONNECTED;
this.subscribed = {};
this.listeningBlocks = false;
this.url = opts.url;
this.opts = {
'reconnection': opts.reconnection || true,
'reconnectionDelay': opts.reconnectionDelay || 1000,
'secure': opts.url.indexOf('https') === 0
};
if (opts.transports) {
this.opts['transports'] = opts.transports;
}
this.socket = this.getSocket();
}
Insight.setCompleteUrl = function(uri) {
if (!uri) return uri;
var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
var parts = [
'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
];
function parseuri(str) {
var m = re.exec(str || ''),
uri = {},
i = 14;
while (i--) {
uri[parts[i]] = m[i] || '';
}
return uri;
};
var opts_host;
var opts_secure;
var opts_port;
var opts_protocol;
if (uri) {
uri = parseuri(uri);
opts_host = uri.host;
opts_protocol = uri.protocol;
opts_secure = uri.protocol == 'https' || uri.protocol == 'wss';
opts_port = uri.port;
}
var this_secure = null != opts_secure ? opts_secure :
('https:' == location.protocol);
var opts_hostname;
if (opts_host) {
var pieces = opts_host.split(':');
opts_hostname = pieces.shift();
if (pieces.length) opts_port = pieces.pop();
}
var this_port = opts_port ||
(this_secure ? 443 : 80);
var newUri = opts_protocol + '://' + opts_host + ':' + this_port;
return newUri;
}
util.inherits(Insight, EventEmitter);
Insight.prototype.STATUS = {
CONNECTED: 'connected',
DISCONNECTED: 'disconnected',
DESTROYED: 'destroyed'
}
/** @private */
Insight.prototype.subscribeToBlocks = function() {
var socket = this.getSocket();
if (this.listeningBlocks || !socket.connected) return;
var self = this;
socket.on('block', function(blockHash) {
self.emit('block', blockHash);
});
this.listeningBlocks = true;
}
/** @private */
Insight.prototype._getSocketIO = function(url, opts) {
log.debug('Insight: Connecting to socket:', this.url);
return io(this.url, this.opts);
};
Insight.prototype._setMainHandlers = function(url, opts) {
// Emmit connection events
var self = this;
this.socket.on('connect', function() {
self.status = self.STATUS.CONNECTED;
self.subscribeToBlocks();
self.emit('connect', 0);
});
this.socket.on('connect_error', function() {
if (self.status != self.STATUS.CONNECTED) return;
self.status = self.STATUS.DISCONNECTED;
self.emit('disconnect');
});
this.socket.on('connect_timeout', function() {
if (self.status != self.STATUS.CONNECTED) return;
self.status = self.STATUS.DISCONNECTED;
self.emit('disconnect');
});
this.socket.on('reconnect', function(attempt) {
if (self.status != self.STATUS.DISCONNECTED) return;
self.emit('reconnect', attempt);
self.reSubscribe();
self.status = self.STATUS.CONNECTED;
});
};
/** @private */
Insight.prototype.getSocket = function() {
if (!this.socket) {
this.socket = this._getSocketIO(this.url, this.opts);
this._setMainHandlers();
}
return this.socket;
}
/** @private */
Insight.prototype.request = function(path, cb) {
preconditions.checkArgument(path).shouldBeFunction(cb);
request(this.url + path, cb);
}
/** @private */
Insight.prototype.requestPost = function(path, data, cb) {
preconditions.checkArgument(path).checkArgument(data).shouldBeFunction(cb);
request({
method: "POST",
url: this.url + path,
json: data
}, cb);
}
Insight.prototype.destroy = function() {
var socket = this.getSocket();
this.socket.disconnect();
this.socket.removeAllListeners();
this.socket = null;
this.subscribed = {};
this.status = this.STATUS.DESTROYED;
this.removeAllListeners();
};
Insight.prototype.subscribe = function(addresses) {
addresses = Array.isArray(addresses) ? addresses : [addresses];
var self = this;
function handlerFor(self, address) {
return function(txid) {
// verify the address is still subscribed
if (!self.subscribed[address]) return;
self.emit('tx', {
address: address,
txid: txid
});
}
}
var s = self.getSocket();
addresses.forEach(function(address) {
preconditions.checkArgument(new bitcore.Address(address).isValid());
// skip already subscibed
if (!self.subscribed[address]) {
var handler = handlerFor(self, address);
self.subscribed[address] = handler;
// log.debug('Subscribe to: ', address);
s.emit('subscribe', address);
s.on(address, handler);
}
});
};
Insight.prototype.getSubscriptions = function(addresses) {
return this.subscribed;
}
Insight.prototype.reSubscribe = function() {
log.debug('insight reSubscribe');
var allAddresses = Object.keys(this.subscribed);
this.subscribed = {};
var s = this.socket;
if (s) {
s.removeAllListeners();
this._setMainHandlers();
this.subscribe(allAddresses);
this.subscribeToBlocks();
}
};
Insight.prototype.broadcast = function(rawtx, cb) {
preconditions.checkArgument(rawtx);
preconditions.shouldBeFunction(cb);
this.requestPost('/api/tx/send', {
rawtx: rawtx
}, function(err, res, body) {
if (err || res.statusCode != 200) return cb(err || body);
return cb(null, body ? body.txid : null);
});
};
Insight.prototype.getTransaction = function(txid, cb) {
preconditions.shouldBeFunction(cb);
this.request('/api/tx/' + txid, function(err, res, body) {
if (err || res.statusCode != 200 || !body) return cb(err || res);
cb(null, JSON.parse(body));
});
};
Insight.prototype.getTransactions = function(addresses, from, to, cb) {
preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb);
var qs = '';
if (_.isNumber(from)) {
qs += '?from=' + from;
if (_.isNumber(to)) {
qs += '&to=' + to;
}
}
this.requestPost('/api/addrs/txs' + qs, {
addrs: addresses.join(',')
}, function(err, res, txs) {
if (err || res.statusCode != 200) return cb(err || res);
cb(null, txs);
});
};
Insight.prototype.getUnspent = function(addresses, cb) {
preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb);
this.requestPost('/api/addrs/utxo', {
addrs: addresses.join(',')
}, function(err, res, unspentRaw) {
if (err || res.statusCode != 200) return cb(err || res);
// This filter out possible broken unspent, as reported on
// https://github.com/bitpay/copay/issues/1585
// and later gitter conversation.
var unspent = _.filter(unspentRaw, 'scriptPubKey');
cb(null, unspent);
});
};
Insight.prototype.getActivity = function(addresses, cb) {
preconditions.shouldBeArray(addresses);
this.getTransactions(addresses, null, null, function then(err, txs) {
if (err) return cb(err);
var flatArray = function(xss) {
return xss.reduce(function(r, xs) {
return r.concat(xs);
}, []);
};
var getInputs = function(t) {
return t.vin.map(function(vin) {
return vin.addr
});
};
var getOutputs = function(t) {
return flatArray(
t.vout.map(function(vout) {
return vout.scriptPubKey.addresses;
})
);
};
var activityMap = new Array(addresses.length);
var activeAddress = flatArray(txs.map(function(t) {
return getInputs(t).concat(getOutputs(t));
}));
activeAddress.forEach(function(addr) {
var index = addresses.indexOf(addr);
if (index != -1) activityMap[index] = true;
});
cb(null, activityMap);
});
};
module.exports = Insight;

View file

@ -1,57 +0,0 @@
'use strict';
var preconditions = require('preconditions').singleton();
var log = require('../util/log');
function PluginManager(config) {
this.registered = {};
this.scripts = [];
for (var ii in config.plugins) {
var pluginName = ii;
if (!config.plugins[pluginName])
continue;
log.info('Loading plugin: ' + pluginName);
var pluginClass;
if(config.pluginsPath){
pluginClass = require(config.pluginsPath + pluginName);
} else {
pluginClass = require('../js/plugins/' + pluginName);
}
var pluginObj = new pluginClass(config[pluginName]);
pluginObj.init();
this._register(pluginObj, pluginName);
}
};
var KIND_UNIQUE = PluginManager.KIND_UNIQUE = 1;
var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2;
PluginManager.TYPE = {};
PluginManager.TYPE['DB'] = KIND_UNIQUE;
PluginManager.prototype._register = function(obj, name) {
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
var type = obj.type;
var kind = PluginManager.TYPE[type];
preconditions.checkArgument(kind, 'Unknown plugin type:' + name);
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered:' + name);
if (kind === PluginManager.KIND_UNIQUE) {
this.registered[type] = obj;
} else {
this.registered[type] = this.registered[type] || [];
this.registered[type].push(obj);
}
this.scripts = this.scripts.concat(obj.scripts || []);
};
PluginManager.prototype.get = function(type) {
return this.registered[type];
};
module.exports = PluginManager;

View file

@ -1,264 +0,0 @@
'use strict';
// 62.9% typed (by google's closure-compiler account)
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
var util = bitcore.util;
var _ = require('lodash');
var preconditions = require('preconditions').instance();
var HDPath = require('./HDPath');
/**
* @desc
* Wrapper for bitcore.HierarchicalKey to be used inside of Copay.
*
* @param {Object} opts
* @param {string} opts.networkName if set to 'testnet', use the test3 bitcoin
* network constants (livenet otherwise)
* @param {string} opts.extendedPrivateKeyString if set, use this private key
* string, othewise create a new
* private key
* @constructor
*/
function PrivateKey(opts) {
opts = opts || {};
this.network = opts.networkName === 'testnet' ? networks.testnet : networks.livenet;
var init = opts.extendedPrivateKeyString || this.network.name;
this.bip = new HK(init);
this.privateKeyCache = {};
this.publicHex = this.deriveBIP45Branch().eckey.public.toString('hex');
};
/**
* @desc Retrieve this derivated private key's public key in hexa format
*
* The value returned is calculated using the path from PrivateKey's
* <tt>HDParams.IdFullBranch</tt>. This key is used to identify the copayer
* (signing messages mostly).
*
* @returns {string} the public key in a hexadecimal string
*/
PrivateKey.prototype.getId = function() {
if (!this.id) {
this.cacheId();
}
return this.id;
};
/**
* @desc Retrieve this private key's private key in hex format
*
* The value returned is calculated using the path from PrivateKey's
* <tt>HDParams.IdFullBranch</tt>. This key is used to identify the copayer
* (signing messages mostly).
*
* @returns {string} the private key in a hexadecimal string
*/
PrivateKey.prototype.getIdPriv = function() {
if (!this.idpriv) {
this.cacheId();
}
return this.idpriv;
};
/**
* @desc Retrieve this private key's private key
*
* The value returned is calculated using the path from PrivateKey's
* <tt>HDParams.IdFullBranch</tt>. This key is used to identify the copayer
* (signing messages mostly).
*
* @returns {bitcore.PrivateKey} the private key
*/
PrivateKey.prototype.getIdKey = function() {
if (!this.idkey) {
this.cacheId();
}
return this.idkey;
};
/**
* @desc Caches the result of deriving IdFullBranch
*
* @private
*/
PrivateKey.prototype.cacheId = function() {
var path = HDPath.IdFullBranch;
var idhk = this.bip.derive(path);
this.idkey = idhk.eckey;
this.id = idhk.eckey.public.toString('hex');
this.idpriv = idhk.eckey.private.toString('hex');
};
/**
* @desc Derive the master branch for Copay.
*/
PrivateKey.prototype.deriveBIP45Branch = function() {
if (!this.bip45Branch) {
this.bip45Branch = this.bip.derive(HDPath.BIP45_PUBLIC_PREFIX);
}
return this.bip45Branch;
};
/**
* @desc Returns an object with information needed to rebuild a PrivateKey
* (as most of its properties are derived from the extended private key).
*
* @TODO: Figure out if this is the correct pattern
* This is a static method and is probably used for serialization.
*
* @static
* @param {Object} data
* @param {*} data.networkName - a name for a bitcoin network
* @param {*} data.extendedPrivateKeyString - a bip32 extended private key
* @returns {Object} an object with two properties: networkName and
* extendedPrivateKeyString, taken from the <tt>data</tt>
* parameter.
*/
PrivateKey.trim = function(data) {
var opts = {};
['networkName', 'extendedPrivateKeyString'].forEach(function(k){
opts[k] = data[k];
});
return opts
};
/**
* @desc Generate a private Key from a serialized object
*
* @TODO: This method uses PrivateKey.trim but it's actually not needed...
*
* @param {Object} data
* @param {*} data.networkName - a name for a bitcoin network
* @param {*} data.extendedPrivateKeyString - a bip32 extended private key
* @returns {PrivateKey}
*/
PrivateKey.fromObj = function(obj) {
return new PrivateKey(PrivateKey.trim(obj));
};
/**
* @desc Serialize a private key, keeping only the data necessary to rebuild it
*
* @returns {Object}
*/
PrivateKey.prototype.toObj = function() {
return {
extendedPrivateKeyString: this.getExtendedPrivateKeyString(),
networkName: this.network.name
};
};
/**
* @desc Retrieve a BIP32 extended public key as generated by bitcore
*
* @returns {string}
*/
PrivateKey.prototype.getExtendedPublicKeyString = function() {
return this.bip.extendedPublicKeyString();
};
/**
* @desc Retrieve a BIP32 extended private key as generated by bitcore
*
* @returns {string}
*/
PrivateKey.prototype.getExtendedPrivateKeyString = function() {
return this.bip.extendedPrivateKeyString();
};
/**
* @desc
* Retrieve a HierarchicalKey derived from the given path as generated by
* bitcore
* @param {string} path - a string for derivation (something like "m/234'/1/2")
* @returns {bitcore.HierarchicalKey}
*/
PrivateKey.prototype._getHK = function(path) {
if (_.isUndefined(path)) {
return this.bip;
}
var ret = this.bip.derive(path);
return ret;
};
/**
* @desc
* Retrieve an array of WalletKey derived from given paths. {@see PrivateKey#getForPath}
*
* @param {string[]} paths - the paths to derive
* @returns {bitcore.WalletKey[]} - the derived keys
*/
PrivateKey.prototype.getForPaths = function(paths) {
return paths.map(this.getForPath.bind(this));
};
/**
* @desc
* Retrieve a WalletKey derived from a path.
*
* @param {string} paths - the path to derive
* @returns {bitcore.WalletKey} - the derived key
*/
PrivateKey.prototype.getForPath = function(path) {
var pk = this.privateKeyCache[path];
if (!pk) {
var derivedHK = this._getHK(path);
pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex');
}
var wk = new WalletKey({
network: this.network
});
wk.fromObj({
priv: pk
});
return wk;
};
/**
* @desc
* Retrieve a Branch for Copay using the given path
*
* @TODO: Investigate when is this called and if this is really needed
*
* @param {number} index - the index of the key to generate
* @param {boolean} isChange - whether this is a change adderess or a receive
* @param {number} cosigner - the cosigner index
* @return {bitcore.HierarchicalKey}
*/
PrivateKey.prototype.get = function(index, isChange, cosigner) {
// TODO: Add parameter validation?
var path = HDPath.FullBranch(index, isChange, cosigner);
return this.getForPath(path);
};
/**
* @desc
* Retrieve multiple branches for Copay up to the received indexes
*
* @TODO: Investigate when is this called and if this is really needed
*
* @param {number} receiveIndex - the number of receive addresses to generate
* @param {number} changeIndex - the number of change addresses to generate
* @param {number} cosigner - the cosigner index
* @return {bitcore.HierarchicalKey}
*/
PrivateKey.prototype.getAll = function(receiveIndex, changeIndex, cosigner) {
preconditions.checkArgument(!_.isUndefined(receiveIndex) && !_.isUndefined(changeIndex));
var ret = [];
for (var i = 0; i < receiveIndex; i++) {
ret.push(this.get(i, false, cosigner));
}
for (var i = 0; i < changeIndex; i++) {
ret.push(this.get(i, true, cosigner));
}
return ret;
};
module.exports = PrivateKey;

View file

@ -1,803 +0,0 @@
'use strict';
var preconditions = require('preconditions').instance();
var _ = require('lodash');
var log = require('../util/log');
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var Address = bitcore.Address;
var Script = bitcore.Script;
var PrivateKey = require('./PrivateKey');
var HDPath = require('./HDPath');
var HDParams = require('./HDParams');
/**
* @desc Represents a public key ring, the set of all public keys and the used indexes
*
* @constructor
* @param {Object} opts
* @param {string} opts.walletId
* @param {string} opts.network 'livenet' to signal the bitcoin main network, all others are testnet
* @param {number=} opts.requiredCopayers - defaults to 3
* @param {number=} opts.totalCopayers - defaults to 5
* @param {Object[]} [opts.indexes] - an array to be deserialized using {@link HDParams#fromList}
* (defaults to all indexes in zero)
* @param {Object=} opts.nicknameFor - nicknames for other copayers
*/
function PublicKeyRing(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.requiredCopayers = opts.requiredCopayers || 3;
this.totalCopayers = opts.totalCopayers || 5;
this.copayersHK = [];
this.indexes = opts.indexes ? HDParams.fromList(opts.indexes) : HDParams.init(this.totalCopayers);
this.publicKeysCache = {};
this.nicknameFor = opts.nicknameFor || {};
this.copayerIds = [];
this.resetCache();
};
PublicKeyRing.prototype.resetCache = function() {
this.cache = {};
this.cache.addressToPath = {};
this.cache.pathToAddress = {};
// Non persistent cache
this._isChange = {};
};
/**
* @desc Returns an object with only the keys needed to rebuild a PublicKeyRing
*
* @TODO: Figure out if this is the correct pattern
* This is a static method and is probably used for serialization.
*
* @static
* @param {Object} data
* @param {string} data.walletId - a string to identify a wallet
* @param {string} data.networkName - the name of the bitcoin network
* @param {number} data.requiredCopayers - the number of required copayers
* @param {number} data.totalCopayers - the number of copayers in the ring
* @param {Object[]} data.indexes - an array of objects that can be turned into
* an array of HDParams
* @param {Object} data.nicknameFor - a registry of nicknames for other copayers
* @param {string[]} data.copayersExtPubKeys - the extended public keys of copayers
* @returns {Object} a trimmed down version of PublicKeyRing that can be used
* as a parameter
*/
PublicKeyRing.trim = function(data) {
preconditions.checkArgument(data);
var opts = {};
['walletId', 'networkName', 'requiredCopayers', 'totalCopayers',
'indexes', 'nicknameFor', 'copayersExtPubKeys'
].forEach(function(k) {
opts[k] = data[k];
});
return opts;
};
/**
* @desc Deserializes a PublicKeyRing from a plain object
*
* If the <tt>data</tt> parameter is an instance of PublicKeyRing already,
* it will fail, throwing an assertion error.
*
* @static
* @param {object} data - a serialized version of PublicKeyRing {@see PublicKeyRing#trim}
* @return {PublicKeyRing} - the deserialized object
*/
PublicKeyRing.fromObj = function(opts) {
preconditions.checkArgument(!(opts instanceof PublicKeyRing), 'bad opts format: Did you use .toObj()?');
// Support old indexes schema
if (!Array.isArray(opts.indexes)) {
opts.indexes = HDParams.update(opts.indexes, opts.totalCopayers);
}
var pkr = new PublicKeyRing(opts);
for (var k in opts.copayersExtPubKeys) {
pkr.addCopayer(opts.copayersExtPubKeys[k]);
}
if (opts.cache && opts.cache.addressToPath) {
log.debug('PublicKeyRing: Using address cache');
pkr.cache.addressToPath = opts.cache.addressToPath;
pkr.rebuildCache();
}
return pkr;
};
PublicKeyRing.prototype.rebuildCache = function() {
if (!this.cache.addressToPath)
return;
var self = this;
_.each(this.cache.addressToPath, function(path, address) {
self.cache.pathToAddress[path] = address;
});
};
PublicKeyRing.fromUntrustedObj = function(opts) {
return PublicKeyRing.fromObj(PublicKeyRing.trim(opts));
};
/**
* @desc Serialize this object to a plain object with all the data needed to
* rebuild it
*
* @return {Object} a serialized version of a PublicKeyRing
*/
PublicKeyRing.prototype.toObj = function() {
return {
walletId: this.walletId,
networkName: this.network.name,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
indexes: HDParams.serialize(this.indexes),
copayersExtPubKeys: this.copayersHK.map(function(b) {
return b.extendedPublicKeyString();
}),
nicknameFor: this.nicknameFor,
// We only store addressToPath and derive the reset from it
cache: {
addressToPath: this.cache.addressToPath
},
};
};
PublicKeyRing.prototype.toTrimmedObj = function() {
return PublicKeyRing.trim(this.toObj());
}
/**
* @desc
* Retrieve a copayer's public key as a hexadecimal encoded string
*
* @param {number} copayerId - the copayer id
* @returns {string} the extended public key of the i-th copayer
*/
PublicKeyRing.prototype.getCopayerId = function(copayerId) {
preconditions.checkArgument(!_.isUndefined(copayerId))
preconditions.checkArgument(_.isNumber(copayerId));
return this.copayerIds[copayerId];
};
/**
* @desc
* Get the amount of registered copayers in this PubKeyRing
*
* @returns {number} amount of copayers present
*/
PublicKeyRing.prototype.registeredCopayers = function() {
return this.copayersHK.length;
};
/**
* @desc
* Returns true if all the needed copayers have joined the public key ring
*
* @returns {boolean}
*/
PublicKeyRing.prototype.isComplete = function() {
return this.remainingCopayers() == 0;
};
/**
* @desc
* Returns the number of copayers yet to join to make the public key ring complete
*
* @returns {number}
*/
PublicKeyRing.prototype.remainingCopayers = function() {
return this.totalCopayers - this.registeredCopayers();
};
/**
* @desc
* Returns an array of copayer's public keys
*
* @returns {string[]} a list of hexadecimal strings with the public keys for
* the copayers in this ring
*/
PublicKeyRing.prototype.getAllCopayerIds = function() {
return this.copayerIds;
};
/**
* @desc
* Gets the current user's copayerId
*
* @returns {string} the extended public key hexadecimal-encoded
*/
PublicKeyRing.prototype.myCopayerId = function() {
return this.getCopayerId(0);
};
/**
* @desc Throws an error if the public key ring isn't complete
*/
PublicKeyRing.prototype._checkKeys = function() {
if (!this.isComplete()) throw new Error('dont have required keys yet');
};
/**
* @desc
* Updates the internal register of the public hex string for a copayer, based
* on the value of the hierarchical key stored in copayersHK
*
* @private
* @param {number} index - the index of the copayer to update
*/
PublicKeyRing.prototype._updateBip = function(index) {
var hk = this.copayersHK[index].derive(HDPath.IdBranch);
this.copayerIds[index] = hk.eckey.public.toString('hex');
};
/**
* @desc
* Sets a nickname for one of the copayers
*
* @private
* @param {number} index - the index of the copayer to update
* @param {string} nickname - the new nickname for that copayer
*/
PublicKeyRing.prototype._setNicknameForIndex = function(index, nickname) {
this.nicknameFor[this.copayerIds[index]] = nickname;
};
/**
* @desc
* Fetch the name of a copayer
*
* @param {number} index - the index of the copayer
* @return {string} the nickname of the index-th copayer
*/
PublicKeyRing.prototype.nicknameForIndex = function(index) {
return this.nicknameFor[this.copayerIds[index]];
};
/**
* @desc
* Fetch the name of a copayer using its public key
*
* @param {string} copayerId - the public key ring of a copayer, hex encoded
* @return {string} the nickname of the copayer with such pubkey
*/
PublicKeyRing.prototype.nicknameForCopayer = function(copayerId) {
return this.nicknameFor[copayerId] || 'NN';
};
/**
* @desc
* Add a copayer into the public key ring.
*
* @param {string} newHexaExtendedPublicKey - an hex encoded string with the copayer's pubkey
* @param {string} nickname - a nickname for this copayer
* @return {string} the newHexaExtendedPublicKey parameter
*/
PublicKeyRing.prototype.addCopayer = function(newHexaExtendedPublicKey, nickname) {
preconditions.checkArgument(newHexaExtendedPublicKey && _.isString(newHexaExtendedPublicKey));
preconditions.checkArgument(!this.isComplete());
preconditions.checkArgument(!nickname || _.isString(nickname));
preconditions.checkArgument(!_.any(this.copayersHK,
function(copayer) {
return copayer.extendedPublicKeyString === newHexaExtendedPublicKey;
}
));
var newCopayerIndex = this.copayersHK.length;
var hierarchicalKey = new HK(newHexaExtendedPublicKey);
this.copayersHK.push(hierarchicalKey);
this._updateBip(newCopayerIndex);
if (nickname) {
this._setNicknameForIndex(newCopayerIndex, nickname);
}
return newHexaExtendedPublicKey;
};
/**
* @desc
* Get all the public keys for the copayers in this ring, for a given branch of Copay
*
* @param {number} index - the index for the shared address
* @param {boolean} isChange - whether to derive a change address o receive address
* @param {number} copayerIndex - the index of the copayer that requested the derivation
* @return {Buffer[]} an array of derived public keys in hexa format
*/
PublicKeyRing.prototype.getPubKeys = function(index, isChange, copayerIndex) {
this._checkKeys();
log.warn('Slow pubkey derivation...');
var path = HDPath.Branch(index, isChange, copayerIndex);
var pubKeys = _.map(this.copayersHK, function(hdKey) {
return hdKey.derive(path).eckey.public;
});
return pubKeys;
};
/**
* @desc
* Generate a new Script for a copay address generated by index, isChange, and copayerIndex
*
* @TODO this could be cached
*
* @param {number} index - the index for the shared address
* @param {boolean} isChange - whether to derive a change address o receive address
* @param {number} copayerIndex - the index of the copayer that requested the derivation
* @returns {bitcore.Script}
*/
PublicKeyRing.prototype.getRedeemScript = function(index, isChange, copayerIndex) {
var pubKeys = this.getPubKeys(index, isChange, copayerIndex);
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
return script;
};
/**
* @desc
* Get the address for a multisig based on the given params.
*
* Caches the address to the branch in the member addressToPath
*
* @param {number} index - the index for the shared address
* @param {boolean} isChange - whether to derive a change address o receive address
* @param {number} copayerIndex - the index of the copayer that requested the derivation
* @returns {bitcore.Address}
*/
PublicKeyRing.prototype._getAddress = function(index, isChange, id) {
var copayerIndex = this.getCosigner(id);
var path = HDPath.FullBranch(index, isChange, copayerIndex);
if (this.cache.pathToAddress[path])
return this.cache.pathToAddress[path];
log.info('Generating Address:', index, isChange, copayerIndex);
var script = this.getRedeemScript(index, isChange, copayerIndex);
var address = Address.fromScript(script, this.network.name).toString();
this._cacheAddress(address, path, isChange);
return address;
};
PublicKeyRing.prototype._cacheAddress = function(address, path, isChange) {
this.cache.addressToPath[address] = path;
this.cache.pathToAddress[path] = address;
};
/**
* @desc
* Get the parameters used to derive a pubkey or a cosigner index
*
* Overloaded to receive a PubkeyString or a consigner index
*
* @param {number|string} id public key in hex format, or the copayer's index
* @return ????
*/
PublicKeyRing.prototype.getHDParams = function(id) {
var copayerIndex = this.getCosigner(id);
var index = this.indexes.filter(function(i) {
return i.copayerIndex == copayerIndex
});
if (index.length != 1) throw new Error('no index for copayerIndex');
return index[0];
};
/**
* @desc
* Get the path used to derive a pubkey or a cosigner index for an address
*
* @param {string} address a multisig p2sh address
* @return {HDPath}
*/
PublicKeyRing.prototype.pathForAddress = function(address) {
this._checkCache();
var path = this.cache.addressToPath[address];
if (!path) throw new Error('Couldn\'t find path for address ' + address);
return path;
};
/**
* @desc
* Generates a new address and updates the last index used
*
* @param {truthy} isChange - generate a change address if true, otherwise
* generates a receive
* @param {number|string} pubkey - the pubkey for the copayer that generates the
* address (or index in the keyring)
* @returns {bitpay.Address}
*/
PublicKeyRing.prototype.generateAddress = function(isChange, pubkey) {
isChange = !!isChange;
var hdParams = this.getHDParams(pubkey);
var index = isChange ? hdParams.getChangeIndex() : hdParams.getReceiveIndex();
var ret = this._getAddress(index, isChange, hdParams.copayerIndex);
hdParams.increment(isChange);
return ret;
};
/**
* @desc Is an address is from this wallet?
*
* @param {string} address
* @return {boolean}
*/
PublicKeyRing.prototype.addressIsOwn = function(address) {
return !!this.cache.addressToPath[address];
};
/**
* @desc Is an address is a change address?
*
* @param {string} address
* @return {boolean}
*/
PublicKeyRing.prototype.addressIsChange = function(address) {
this._checkCache();
var path = this.cache.addressToPath[address];
if (!path)
return null;
var p = HDPath.indexesForPath(path);
//Memoization Only, never stored.
this._isChange[address] = p.isChange;
return !!this._isChange[address];
};
/**
* @desc
* Maps a copayer's public key to his index in the keyring
*
* @param {number|string|undefined} pubKey - if undefined, returns the SHARED_INDEX
* - if a number, just return it
* - if a string, assume is the hex encoded public key
* @returns {number} the index of the copayer with the given pubkey
*/
PublicKeyRing.prototype.getCosigner = function(pubKey) {
if (_.isUndefined(pubKey)) return HDPath.SHARED_INDEX;
if (_.isNumber(pubKey)) return pubKey;
var sorted = this.copayersHK.map(function(h, i) {
return h.eckey.public.toString('hex');
}).sort(function(h1, h2) {
return h1.localeCompare(h2);
});
var index = sorted.indexOf(pubKey);
if (index == -1) throw new Error('public key is not on the ring');
return index;
};
PublicKeyRing.prototype.buildAddressCache = function() {
var ret = [];
var self = this;
_.each(this.indexes, function(index) {
for (var i = 0; i < index.receiveIndex; i++) {
self._getAddress(i, false, index.copayerIndex);
}
for (var i = 0; i < index.changeIndex; i++) {
self._getAddress(i, true, index.copayerIndex);
}
});
};
PublicKeyRing.prototype.size = function(opts) {
var self = this;
return _.reduce(this.indexes, function(sum, index) {
return sum + index.receiveIndex + index.changeIndex
}, 0);
};
PublicKeyRing.prototype._checkCache = function(opts) {
if (_.isEmpty(this.cache.addressToPath)) {
this.buildAddressCache();
}
if (_.size(this.cache.addressToPath) !== this.size()) {
this.buildAddressCache();
}
};
/**
* @desc
* Gets information about addresses for a copayer
*
* @param {Object} opts
* @returns {AddressInfo[]}
*/
PublicKeyRing.prototype.getAddresses = function() {
this._checkCache();
return _.keys(this.cache.addressToPath);
};
/**
* getAddressesOrdered
* {@link Wallet#getAddressesOrdered}
*
* @param pubkey
* @return {string[]}
*/
PublicKeyRing.prototype.getAddressesOrdered = function(pubkey) {
this._checkCache();
var info = _.map(this.cache.addressToPath, function(path, addr) {
var p = HDPath.indexesForPath(path);
p.address = addr;
return p;
});
var copayerIndex = this.getCosigner(pubkey);
var l = info.length;
var sortedInfo = _.sortBy(info, function(i) {
var goodness = ((i.copayerIndex !== copayerIndex) ? 2 * l : 0) + (i.isChange ? l : 0) + l - i.addressIndex;
return goodness;
});
return _.pluck(sortedInfo, 'address');
};
/**
* @desc
* Gets information about addresses for a copayer
*
* @param {Object} opts
* @returns {AddressInfo[]}
*/
PublicKeyRing.prototype.getReceiveAddresses = function() {
this._checkCache();
var self = this;
return _.filter(this.getAddresses(), function(addr) {
return !self.addressIsChange(addr);
});
};
/**
* @desc
* Retrieve the public keys for all cosigners for a given path
*
* @param {string} path - the BIP32 path
* @return {Buffer[]} the public keys, in buffer format
*/
PublicKeyRing.prototype._getForPath = function(path) {
var p = HDPath.indexesForPath(path);
return this.getPubKeys(p.addressIndex, p.isChange, p.copayerIndex);
};
/**
* @desc
* Retrieve the public keys for derived addresses and the public keys for copayers
*
* @TODO: Should this exist? A user should just call _getForPath(paths)
*
* @param {string[]} paths - the paths to be derived
* @return {Object} with keys pubKeys and copayerIds
*/
PublicKeyRing.prototype.forPaths = function(paths) {
return {
pubKeys: paths.map(this._getForPath.bind(this)),
copayerIds: this.copayerIds,
}
};
/**
* @desc
* Retrieve the public keys for all cosigners for multiple paths
*
* @param {string[]} paths - the BIP32 paths
* @return {Array[]} the public keys, in buffer format (matrix of Buffer, Buffer[][])
*/
PublicKeyRing.prototype._getForPaths = function(paths) {
preconditions.checkArgument(!_.isUndefined(paths));
preconditions.checkArgument(_.isArray(paths));
preconditions.checkArgument(_.all(paths, _.isString));
return paths.map(this._getForPath.bind(this));
};
/**
* @desc
* Returns a map from a pubkey of an address to the id that generated it
*
* @param {string[]} pubkeys - the pubkeys to query
* @param {string[]} paths - the paths to query
*/
PublicKeyRing.prototype.copayersForPubkeys = function(pubkeys, paths) {
preconditions.checkArgument(pubkeys);
preconditions.checkArgument(paths);
var inKeyMap = {},
ret = {};
for (var i in pubkeys) {
inKeyMap[pubkeys[i]] = 1;
};
var keys = this._getForPaths(paths);
for (var i in keys) {
for (var copayerIndex in keys[i]) {
var kHex = keys[i][copayerIndex].toString('hex');
if (inKeyMap[kHex]) {
ret[kHex] = this.copayerIds[copayerIndex];
delete inKeyMap[kHex];
}
}
}
if (_.size(inKeyMap)) {
for (var i in inKeyMap) {
log.error('Pubkey ' + i + ' not identified');
}
throw new Error('Pubkeys not identified');
}
return ret;
};
/**
* @desc
* Returns a map from address -> public key needed
*
* @param {HDPath[]} paths - paths to be solved
* @returns {Object} a map from addresses to Buffer with the hex pubkeys
*/
PublicKeyRing.prototype.getRedeemScriptMap = function(paths) {
var ret = {};
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
var p = HDPath.indexesForPath(path);
var script = this.getRedeemScript(p.addressIndex, p.isChange, p.copayerIndex);
ret[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
}
return ret;
};
/**
* @desc
* Check if another PubKeyRing is similar to this one (checks network name,
* requiredCopayers, and totalCopayers). If ignoreId is falsy, also check that
* both walletIds match.
*
* @private
* @param {PubKeyRing} inPKR - the other PubKeyRing
* @param {boolean} ignoreId - whether to ignore checking for equal walletId
* @throws {Error} if the wallets mismatch
* @return true
*/
PublicKeyRing.prototype._checkInPKR = function(inPKR, ignoreId) {
preconditions.checkArgument(_.isObject(inPKR));
if (!ignoreId && this.walletId !== inPKR.walletId)
throw new Error('inPKR walletId mismatch');
if (this.network.name !== inPKR.network.name)
throw new Error('Network mismatch. Should be ' + this.network.name +
' and found ' + inPKR.network.name);
if (this.requiredCopayers && inPKR.requiredCopayers &&
(this.requiredCopayers !== inPKR.requiredCopayers))
throw new Error('inPKR requiredCopayers mismatch ' + this.requiredCopayers +
'!=' + inPKR.requiredCopayers);
if (this.totalCopayers && inPKR.totalCopayers &&
this.totalCopayers !== inPKR.totalCopayers)
throw new Error('inPKR totalCopayers mismatch' + this.totalCopayers +
'!=' + inPKR.requiredCopayers);
return true;
};
/**
* @desc
* Merges the public keys of the wallet passed in as a parameter with ours.
*
* @param {PublicKeyRing} inPKR
* @return {boolean} true if there where changes in our internal state
*/
PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
var self = this;
var hasChanged = false;
if (self.isComplete())
return;
inPKR.copayersHK.forEach(function(b) {
var epk = b.extendedPublicKeyString();
var haveIt = _.any(self.copayersHK, function(hk) {
return hk.extendedPublicKeyString() === epk;
});
if (!haveIt) {
if (self.isComplete()) {
throw new Error('trying to add more pubkeys, when PKR isComplete at merge');
}
var l2 = self.copayersHK.length;
self.copayersHK.push(new HK(epk));
self._updateBip(l2);
if (inPKR.nicknameFor[self.getCopayerId(l2)])
self._setNicknameForIndex(l2, inPKR.nicknameFor[self.getCopayerId(l2)]);
hasChanged = true;
}
});
return hasChanged;
};
/**
* @desc
* Merges the indexes for addresses generated with another copy of a list of
* HDParams
*
* @param {HDParams[]} indexes - indexes as received from another sources
* @return {boolean} true if the internal state has changed
*/
PublicKeyRing.prototype.mergeIndexes = function(indexes) {
var self = this;
var hasChanged = false;
indexes.forEach(function(theirs) {
var mine = self.getHDParams(theirs.copayerIndex);
hasChanged |= mine.merge(theirs);
});
return !!hasChanged
}
/**
* @desc
* Merges this public key ring with another one, optionally ignoring the
* wallet id
*
* @param {PublicKeyRing} inPkr
* @param {boolean} ignoreId
* @return {boolean} true if the internal state has changed
*/
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
this._checkInPKR(inPKR, ignoreId);
var hasChanged = false;
hasChanged |= this.mergeIndexes(inPKR.indexes);
hasChanged |= this._mergePubkeys(inPKR);
return !!hasChanged;
};
module.exports = PublicKeyRing;

View file

@ -1,171 +0,0 @@
'use strict';
var util = require('util');
var _ = require('lodash');
var log = require('../util/log');
var preconditions = require('preconditions').singleton();
var request = require('request');
/*
This class lets interfaces with BitPay's exchange rate API.
*/
var RateService = function(opts) {
var self = this;
opts = opts || {};
self.request = opts.request || request;
self.SAT_TO_BTC = 1 / 1e8;
self.BTC_TO_SAT = 1e8;
self.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
self.UNSUPPORTED_CURRENCY_ERROR = 'Currency not supported';
self._url = opts.url || 'https://insight.bitpay.com:443/api/rates';
self._isAvailable = false;
self._rates = {};
self._alternatives = [];
self._queued = [];
self._fetchCurrencies();
};
var _instance;
RateService.singleton = function(opts) {
if (!_instance) {
_instance = new RateService(opts);
}
return _instance;
};
RateService.prototype._fetchCurrencies = function() {
var self = this;
var backoffSeconds = 5;
var updateFrequencySeconds = 3600;
var rateServiceUrl = 'https://bitpay.com/api/rates';
var retrieve = function() {
log.info('Fetching exchange rates');
self.request.get({
url: rateServiceUrl,
json: true
}, function(err, res, body) {
if (err || !body) {
log.debug('Error fetching exchange rates', err);
setTimeout(function() {
backoffSeconds *= 1.5;
retrieve();
}, backoffSeconds * 1000);
return;
}
_.each(body, function(currency) {
self._rates[currency.code] = currency.rate;
self._alternatives.push({
name: currency.name,
isoCode: currency.code,
rate: currency.rate
});
});
self._isAvailable = true;
_.each(self._queued, function(callback) {
setTimeout(callback, 1);
});
setTimeout(retrieve, updateFrequencySeconds * 1000);
});
};
retrieve();
};
RateService.prototype.getRate = function(code) {
return this._rates[code];
};
RateService.prototype.getHistoricRate = function(code, date, cb) {
var self = this;
self.request.get({
url: self._url + '/' + code + '?ts=' + date,
json: true
}, function(err, res, body) {
if (err || res.statusCode != 200 || !body) return cb(err || res);
return cb(null, body.rate);
});
};
RateService.prototype.getHistoricRates = function(code, dates, cb) {
var self = this;
var tsList = dates.join(',');
self.request.get({
url: self._url + '/' + code + '?ts=' + tsList,
json: true
}, function(err, res, body) {
if (err || res.statusCode != 200 || !body) return cb(err || res);
if (!_.isArray(body)) {
body = [{
ts: dates[0],
rate: body.rate
}];
}
return cb(null, body);
});
};
RateService.prototype.getAlternatives = function() {
return this._alternatives;
};
RateService.prototype.isAvailable = function() {
return this._isAvailable;
};
RateService.prototype.whenAvailable = function(callback) {
if (this.isAvailable()) {
setTimeout(callback, 1);
} else {
this._queued.push(callback);
}
};
RateService.prototype.toFiat = function(satoshis, code) {
if (!this.isAvailable()) {
throw new Error(this.UNAVAILABLE_ERROR);
}
return satoshis * this.SAT_TO_BTC * this.getRate(code);
};
RateService.prototype.toFiatHistoric = function(satoshis, code, date, cb) {
var self = this;
self.getHistoricRate(code, date, function(err, rate) {
if (err) return cb(err);
return cb(null, satoshis * self.SAT_TO_BTC * rate);
});
};
RateService.prototype.fromFiat = function(amount, code) {
if (!this.isAvailable()) {
throw new Error(this.UNAVAILABLE_ERROR);
}
return amount / this.getRate(code) * this.BTC_TO_SAT;
};
RateService.prototype.listAlternatives = function() {
if (!this.isAvailable()) {
throw new Error(this.UNAVAILABLE_ERROR);
}
return _.map(this.getAlternatives(), function(item) {
return {
name: item.name,
isoCode: item.isoCode
}
});
};
module.exports = RateService;

View file

@ -1,624 +0,0 @@
'use strict';
var _ = require('lodash');
var preconditions = require('preconditions').singleton();
var bitcore = require('bitcore');
var util = bitcore.util;
var Transaction = bitcore.Transaction;
var TransactionBuilder = bitcore.TransactionBuilder;
var Script = bitcore.Script;
var Key = bitcore.Key;
var log = require('../util/log');
var TX_MAX_SIZE_KB = 50;
var VERSION = 1;
var CORE_FIELDS = ['builderObj', 'inputChainPaths', 'version', 'comment', 'paymentProtocolURL', 'paymentAckMemo'];
function TxProposal(opts) {
preconditions.checkArgument(opts);
preconditions.checkArgument(opts.inputChainPaths, 'no inputChainPaths');
preconditions.checkArgument(opts.builder, 'no builder');
preconditions.checkArgument(opts.inputChainPaths, 'no inputChainPaths');
this.inputChainPaths = opts.inputChainPaths;
this.version = opts.version;
this.builder = opts.builder;
this.createdTs = opts.createdTs;
// Copayer Actions ( copayerId: timeStamp )
this.signedBy = opts.signedBy || {};
this.seenBy = opts.seenBy || {};
this.rejectedBy = opts.rejectedBy || {};
this.sentTs = opts.sentTs || null;
this.sentTxid = opts.sentTxid || null;
this.comment = opts.comment || null;
this.readonly = opts.readonly || null;
this.merchant = opts.merchant || null;
this.paymentAckMemo = opts.paymentAckMemo || null;
this.paymentProtocolURL = opts.paymentProtocolURL || null;
this.resetCache();
// New Tx Proposal
if (_.isEmpty(this.seenBy) && opts.creator) {
var now = Date.now();
var me = {};
me[opts.creator] = now;
this.seenBy = me;
this.signedBy = {};
this.creator = opts.creator;
this.createdTs = now;
if (opts.signWith) {
if (!this.sign(opts.signWith, opts.creator))
throw new Error('Could not sign generated tx');
}
}
}
TxProposal.prototype._checkPayPro = function() {
if (!this.merchant) return;
if (this.paymentProtocolURL !== this.merchant.request_url)
throw new Error('PayPro: Mismatch on Payment URLs');
if (!this.merchant.outs || this.merchant.outs.length !== 1)
throw new Error('PayPro: Unsopported number of outputs');
if (this.merchant.expires < (this.getSent() || Date.now() / 1000.))
throw new Error('PayPro: Request expired');
if (!this.merchant.total || !this.merchant.outs[0].amountSatStr || !this.merchant.outs[0].address)
throw new Error('PayPro: Missing amount');
var outs = JSON.parse(this.builder.vanilla.outs);
if (_.size(outs) != 1)
throw new Error('PayPro: Wrong outs in Tx');
var ppOut = this.merchant.outs[0];
var txOut = outs[0];
if (ppOut.address !== txOut.address)
throw new Error('PayPro: Wrong out address in Tx');
if (ppOut.amountSatStr !== txOut.amountSatStr + '')
throw new Error('PayPro: Wrong amount in Tx');
};
TxProposal.prototype.isFullySigned = function() {
return this.builder && this.builder.isFullySigned();
};
TxProposal.prototype.getMySignatures = function() {
preconditions.checkState(this._mySignatures, 'Still no signatures from us');
return _.clone(this._mySignatures);
};
TxProposal.prototype._setMySignatures = function(signaturesBefore) {
var mySigs = [];
_.each(this.getSignatures(), function(signatures, index) {
var diff = _.difference(signatures, signaturesBefore[index]);
preconditions.checkState(diff.length == 1, 'more that one signature added!');
mySigs.push(diff[0].toString('hex'));
})
this._mySignatures = mySigs;
return;
};
TxProposal.prototype.sign = function(keys, signerId) {
var before = this.countSignatures();
var signaturesBefore = this.getSignatures();
this.builder.sign(keys);
var signaturesAdded = this.countSignatures() > before;
if (signaturesAdded) {
this.signedBy[signerId] = Date.now();
this._setMySignatures(signaturesBefore);
}
return signaturesAdded;
};
TxProposal.prototype._check = function() {
if (this.builder.signhash && this.builder.signhash !== Transaction.SIGHASH_ALL) {
throw new Error('Invalid tx proposal');
}
// Should be able to build
var tx = this.builder.build();
var txSize = tx.getSize();
if (txSize / 1024 > TX_MAX_SIZE_KB)
throw new Error('BIG: Invalid TX proposal. Too big: ' + txSize + ' bytes');
if (!tx.ins.length)
throw new Error('Invalid tx proposal: no ins');
_.each(tx.ins, function(value, index) {
var scriptSig = value.s;
if (!scriptSig || !scriptSig.length) {
throw new Error('Invalid tx proposal: no signatures');
}
var hashType = tx.getHashType(index);
if (hashType && hashType !== Transaction.SIGHASH_ALL)
throw new Error('Invalid tx proposal: bad signatures');
});
this._checkPayPro();
};
TxProposal.prototype.addMerchantData = function(merchantData) {
preconditions.checkArgument(merchantData.pr);
preconditions.checkArgument(merchantData.request_url);
var m = _.clone(merchantData);
if (!this.paymentProtocolURL)
this.paymentProtocolURL = m.request_url;
// remove unneeded data
m.raw = m.pr.pki_data = m.pr.signature = undefined;
this.merchant = m;
this._checkPayPro();
};
TxProposal.prototype.getSignatures = function() {
var ins = this.builder.build().ins;
var sigs = _.map(ins, function(value) {
var script = new bitcore.Script(value.s);
var nchunks = script.chunks.length;
return _.map(script.chunks.slice(1, nchunks - 1), function(buffer) {
return buffer.toString('hex');
});
});
return sigs;
};
TxProposal.prototype.rejectCount = function() {
return _.size(this.rejectedBy);
};
TxProposal.prototype.isFinallyRejected = function(maxRejectCount) {
return this.rejectCount() > maxRejectCount;
};
TxProposal.prototype.isPending = function(maxRejectCount) {
preconditions.checkArgument(_.isNumber(maxRejectCount));
if (this.isFinallyRejected(maxRejectCount) || this.sentTxid)
return false;
return true;
};
TxProposal.prototype._setSigned = function(copayerId) {
// Sign powns rejected
if (this.rejectedBy[copayerId]) {
log.info("WARN: a previously rejected transaction was signed by:", copayerId);
delete this.rejectedBy[copayerId];
}
this.signedBy[copayerId] = Date.now();
return this;
};
/**
*
* @desc verify signatures of ONE copayer, using an array of signatures for each input
*
* @param {string[]} signatures, of the same copayer, one for each input
* @return {string[]} array for signing pubkeys for each input
*/
TxProposal.prototype._addSignatureAndVerify = function(signatures) {
var self = this;
var ret = [];
var tx = self.builder.build();
var inputsFullySigned = 0;
var newScriptSigs = [];
this.resetCache();
_.each(tx.ins, function(input, index) {
var scriptSig = new Script(input.s);
var info = TxProposal.infoFromRedeemScript(scriptSig);
var txSigHash = tx.hashForSignature(info.script, parseInt(index), Transaction.SIGHASH_ALL);
var keys = TxProposal.formatKeys(info.keys);
var sig = new Buffer(signatures[index], 'hex');
var hashType = sig[sig.length - 1];
if (hashType !== Transaction.SIGHASH_ALL)
throw new Error('BADSIG: Invalid signature: Bad hash type');
var sigRaw = new Buffer(sig.slice(0, sig.length - 1));
var signingPubKeyHex = self._verifyOneSignature(keys, sigRaw, txSigHash);
if (!signingPubKeyHex)
throw new Error('BADSIG: Invalid signatures: invalid for input:' + index);
// now insert it
var keysHex = _.pluck(keys, 'keyHex');
var prio = _.indexOf(keysHex, signingPubKeyHex);
preconditions.checkState(prio >= 0);
var currentKeys = self.getSignersPubKeys()[index];
if (_.indexOf(currentKeys, signingPubKeyHex) >= 0)
throw new Error('BADSIG: Already have this signature');
var currentPrios = _.map(currentKeys, function(key) {
var prio = _.indexOf(keysHex, key);
preconditions.checkState(prio >= 0);
return prio;
});
var insertAt = 0;
while (!_.isUndefined(currentPrios[insertAt]) && prio > currentPrios[insertAt])
insertAt++;
// Insert it! (1 is OP_0!)
scriptSig.chunks.splice(1 + insertAt, 0, sig);
scriptSig.updateBuffer();
if (info.nreq == currentKeys.length + 1) {
inputsFullySigned++;
}
newScriptSigs.push(scriptSig.buffer);
});
preconditions.checkState(newScriptSigs.length === tx.ins.length);
// If we reach here, all signatures are OK, let's update the TX.
_.each(tx.ins, function(input, index) {
input.s = newScriptSigs[index];
// just to keep TransactionBuilder updated
if (tx.ins.length == inputsFullySigned)
self.builder.inputsSigned++;
});
};
TxProposal.prototype.resetCache = function() {
this.cache = {
pubkeysForScript: {},
};
};
/**
* addSignature
*
* @param {string[]} signatures from *ONE* copayer, one signature for each TX input.
* @return {boolean} true = signatures added
*/
TxProposal.prototype.addSignature = function(copayerId, signatures) {
preconditions.checkArgument(_.isArray(signatures));
if (this.isFullySigned())
return false;
var tx = this.builder.build();
preconditions.checkArgument(signatures.length === tx.ins.length, 'Wrong number of signatures given');
this._addSignatureAndVerify(signatures);
this._setSigned(copayerId);
return true;
};
/**
*
* getSignersPubKey
* @desc get Pubkeys of signers, for each input. this is CPU intensive
*
* @return {string[][]} array of hashes for signing pubkeys for each input
*/
TxProposal.prototype.getSignersPubKeys = function(forceUpdate) {
var self = this;
var signersPubKey = [];
if (!self.cache.signersPubKey || forceUpdate) {
log.debug('PERFORMANCE WARN: Verifying *all* TX signatures:', self.getId());
var tx = self.builder.build();
_.each(tx.ins, function(input, index) {
if (!self.cache.pubkeysForScript[input.s]) {
var scriptSig = new Script(input.s);
var signatureCount = scriptSig.countSignatures();
var info = TxProposal.infoFromRedeemScript(scriptSig);
var txSigHash = tx.hashForSignature(info.script, parseInt(index), Transaction.SIGHASH_ALL);
var inputSignersPubKey = self.verifySignatures(info.keys, scriptSig, txSigHash);
// Does scriptSig has strings that are not signatures?
if (inputSignersPubKey.length !== signatureCount)
throw new Error('Invalid signature');
self.cache.pubkeysForScript[input.s] = inputSignersPubKey;
}
signersPubKey[index] = self.cache.pubkeysForScript[input.s];
});
self.cache.signersPubKey = signersPubKey;
} else {
log.debug('Using signatures verification cache')
}
return self.cache.signersPubKey;
};
TxProposal.prototype.getId = function() {
preconditions.checkState(this.builder);
if (!this.ntxid) {
this.ntxid = this.builder.build().getNormalizedHash().toString('hex');
}
return this.ntxid;
};
TxProposal.prototype.toObj = function() {
var o = JSON.parse(JSON.stringify(this));
delete o['builder'];
delete o['cache'];
o.builderObj = this.builder.toObj();
return o;
};
TxProposal._trim = function(o) {
var ret = {};
CORE_FIELDS.forEach(function(k) {
ret[k] = o[k];
});
return ret;
};
TxProposal.fromObj = function(o, forceOpts) {
preconditions.checkArgument(o.builderObj);
delete o['builder'];
forceOpts = forceOpts || {};
o.builderObj.opts = o.builderObj.opts || {};
// force opts is requested.
_.each(forceOpts, function(value, key) {
o.builderObj.opts[key] = value;
});
// Handle undef fee options
if (_.isUndefined(forceOpts.fee) && _.isUndefined(forceOpts.feeSat)) {
o.builderObj.opts.fee = undefined;
o.builderObj.opts.feeSat = undefined;
}
try {
o.builder = TransactionBuilder.fromObj(o.builderObj);
} catch (e) {
throw new Error(e);
return null;
}
return new TxProposal(o);
};
TxProposal.fromUntrustedObj = function(o, forceOpts) {
var trimmed = TxProposal._trim(o);
var txp = TxProposal.fromObj(trimmed, forceOpts);
if (!txp)
throw new Error('Invalid Transaction');
txp._check();
return txp;
};
TxProposal.prototype.toObjTrim = function() {
return TxProposal._trim(this.toObj());
};
TxProposal.formatKeys = function(keys) {
var ret = [];
for (var i in keys) {
if (!Buffer.isBuffer(keys[i]))
throw new Error('keys must be buffers');
var k = new Key();
k.public = keys[i];
ret.push({
keyObj: k,
keyHex: keys[i].toString('hex'),
});
}
return ret;
};
/**
* @desc Verify a single signature, for a given hash, tested against a given list of public keys.
* @param keys
* @param sigRaw
* @param txSigHash
* @return {string?} on valid signature, return the signing public key hex representation
*/
TxProposal.prototype._verifyOneSignature = function(keys, sigRaw, txSigHash) {
preconditions.checkArgument(Buffer.isBuffer(txSigHash));
preconditions.checkArgument(Buffer.isBuffer(sigRaw));
preconditions.checkArgument(_.isArray(keys));
preconditions.checkArgument(keys[0].keyObj);
var signingKey = _.find(keys, function(key) {
var ret = false;
try {
ret = key.keyObj.verifySignatureSync(txSigHash, sigRaw);
} catch (e) {};
return ret;
});
return signingKey ? signingKey.keyHex : null;
};
/**
* @desc verify transaction signatures
*
* @param inKeys
* @param scriptSig
* @param txSigHash
* @return {string[]} signing pubkeys, in order of apperance
*/
TxProposal.prototype.verifySignatures = function(inKeys, scriptSig, txSigHash) {
preconditions.checkArgument(Buffer.isBuffer(txSigHash));
preconditions.checkArgument(inKeys);
preconditions.checkState(Buffer.isBuffer(inKeys[0]));
var self = this;
if (scriptSig.chunks[0] !== 0)
throw new Error('Invalid scriptSig');
var keys = TxProposal.formatKeys(inKeys);
var ret = [];
for (var i = 1; i <= scriptSig.countSignatures(); i++) {
var chunk = scriptSig.chunks[i];
log.debug('\t Verifying CHUNK:', i);
var sigRaw = new Buffer(chunk.slice(0, chunk.length - 1));
var signingPubKeyHex = self._verifyOneSignature(keys, sigRaw, txSigHash);
if (!signingPubKeyHex)
throw new Error('Found a signature that is invalid');
ret.push(signingPubKeyHex);
}
return ret;
};
TxProposal.infoFromRedeemScript = function(s) {
var redeemScript = new Script(s.chunks[s.chunks.length - 1]);
if (!redeemScript)
throw new Error('Bad scriptSig (no redeemscript)');
var nreq = nreq = redeemScript.chunks[0] - 80; //see OP_2-OP_16
var pubkeys = redeemScript.capture();
if (!pubkeys || !pubkeys.length)
throw new Error('Bad scriptSig (no pubkeys)');
return {
nreq: nreq,
keys: pubkeys,
script: redeemScript,
};
};
TxProposal.prototype.getSeen = function(copayerId) {
return this.seenBy[copayerId];
};
TxProposal.prototype.setSeen = function(copayerId) {
if (!this.seenBy[copayerId])
this.seenBy[copayerId] = Date.now();
};
TxProposal.prototype.setRejected = function(copayerId) {
if (this.signedBy[copayerId])
throw new Error('Can not reject a signed TX');
if (!this.rejectedBy[copayerId])
this.rejectedBy[copayerId] = Date.now();
return this;
};
TxProposal.prototype.setSent = function(sentTxid) {
this.sentTxid = sentTxid;
this.sentTs = Date.now();
return this;
};
TxProposal.prototype.getSent = function() {
return this.sentTs;
}
TxProposal.prototype.setCopayers = function(pubkeyToCopayerMap) {
var newCopayer = {},
oldCopayers = {},
newSignedBy = {},
readOnlyPeers = {},
isNew = 1;
for (var k in this.signedBy) {
oldCopayers[k] = 1;
isNew = 0;
};
if (isNew == 0) {
if (!this.creator || !this.createdTs)
throw new Error('Existing TX has no creator');
if (!this.signedBy[this.creator])
throw new Error('Existing TX is not signed by creator');
if (Object.keys(this.signedBy).length === 0)
throw new Error('Existing TX has no signatures');
}
var iSig = this.getSignersPubKeys();
for (var i in iSig) {
var copayerId = pubkeyToCopayerMap[iSig[i]];
if (!copayerId)
throw new Error('Found unknown signature')
if (oldCopayers[copayerId]) {
//Already have it. Do nothing
} else {
newCopayer[copayerId] = Date.now();
delete oldCopayers[i];
}
}
if (Object.keys(newCopayer).length > 1)
throw new Error('New TX must have only 1 new signature');
// Handler creator / createdTs.
// from senderId, and must be signed by senderId * DISABLED*
//
if (isNew) {
this.creator = Object.keys(newCopayer)[0];
this.seenBy[this.creator] = this.createdTs = Date.now();
}
//Ended. Update this
_.extend(this.signedBy, newCopayer);
// signedBy has preference over rejectedBy
for (var i in this.signedBy) {
delete this.rejectedBy[i];
}
return Object.keys(newCopayer);
};
//This should be on bitcore / Transaction
TxProposal.prototype.countSignatures = function() {
var tx = this.builder.build();
var ret = 0;
for (var i in tx.ins) {
ret += tx.countInputSignatures(i);
}
return ret;
};
module.exports = TxProposal;

View file

@ -1,156 +0,0 @@
'use strict';
var _ = require('lodash');
var preconditions = require('preconditions').singleton();
var bitcore = require('bitcore');
var util = bitcore.util;
var Transaction = bitcore.Transaction;
var Script = bitcore.Script;
var Key = bitcore.Key;
var buffertools = bitcore.buffertools;
var log = require('../util/log');
var TxProposal = require('./TxProposal');;
function TxProposals(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.txps = {};
}
// fromObj => from a trusted source
TxProposals.fromObj = function(o, forceOpts) {
var ret = new TxProposals({
networkName: o.networkName,
walletId: o.walletId,
});
o.txps.forEach(function(o2) {
try {
var t = TxProposal.fromObj(o2, forceOpts);
} catch (e) {
log.info('Ignoring corrupted TxProposal:', o2, e);
}
if (t && t.builder) {
var id = t.getId();
ret.txps[id] = t;
}
});
return ret;
};
TxProposals.prototype.length = function() {
return Object.keys(this.txps).length;
};
TxProposals.prototype.getNtxidsSince = function(sinceTs) {
preconditions.checkArgument(sinceTs);
var ret = [];
for (var ii in this.txps) {
var txp = this.txps[ii];
if (txp.createdTs >= sinceTs)
ret.push(ii);
}
return ret;
};
TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
TxProposals.prototype.deleteOne = function(ntxid) {
preconditions.checkState(this.txps[ntxid], 'Unknown TXP: ' + ntxid);
delete this.txps[ntxid];
};
TxProposals.prototype.deleteAll = function() {
this.txps = {};
};
TxProposals.prototype.deletePending = function(maxRejectCount) {
for (var ntxid in this.txps) {
if (this.txps[ntxid].isPending(maxRejectCount))
delete this.txps[ntxid];
};
};
TxProposals.prototype.toObj = function() {
var ret = [];
for (var id in this.txps) {
var t = this.txps[id];
if (!t.sent)
ret.push(t.toObj());
}
return {
txps: ret,
walletId: this.walletId,
networkName: this.network.name,
};
};
// Add a LOCALLY CREATED (trusted) tx proposal
TxProposals.prototype.add = function(txp) {
var ntxid = txp.getId();
this.txps[ntxid] = txp;
return ntxid;
};
TxProposals.prototype.exist = function(ntxid) {
return this.txps[ntxid] ? true : false;
};
TxProposals.prototype.get = function(ntxid) {
var ret = this.txps[ntxid];
if (!ret)
throw new Error('Unknown TXP: ' + ntxid);
return ret;
};
//returns the unspent txid-vout used in PENDING Txs
TxProposals.prototype.getUsedUnspent = function(maxRejectCount) {
var ret = {};
var self = this;
_.each(this.txps, function(txp) {
if (!txp.isPending(maxRejectCount))
return
_.each(txp.builder.getSelectedUnspent(), function(u) {
ret[u.txid + ',' + u.vout] = 1;
});
});
return ret;
};
/**
* purge
*
* @param deleteAll
* @return {undefined}
*/
TxProposals.prototype.purge = function(deleteAll, maxRejectCount) {
var m = _.size(this.txps);
if (deleteAll) {
this.deleteAll();
} else {
this.deletePending(maxRejectCount);
}
var n = _.size(this.txps);
return m - n;
};
module.exports = TxProposals;

File diff suppressed because it is too large Load diff

View file

@ -1,116 +0,0 @@
'use strict';
var preconditions = require('preconditions').singleton();
function WalletLock(storage, walletId, timeoutMin) {
preconditions.checkArgument(storage);
preconditions.checkArgument(walletId);
this.storage = storage;
this.timeoutMin = timeoutMin || 5;
this.key = WalletLock._keyFor(walletId);
}
WalletLock.prototype.getSessionId = function(cb) {
preconditions.checkArgument(cb);
var self = this;
self.sessionStorage.getItem('sessionId', function(sessionId) {
if (sessionId)
return cb(sessionId);
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
self.sessionStorage.setItem('sessionId', sessionId, function() {
return cb(sessionId);
});
});
};
WalletLock.prototype.init = function(cb) {
preconditions.checkArgument(cb);
var self = this;
self.storage.getSessionId(function(sid) {
preconditions.checkState(sid);
self.sessionId = sid;
cb();
});
};
WalletLock._keyFor = function(walletId) {
return 'lock' + '::' + walletId;
};
WalletLock.prototype._isLockedByOther = function(cb) {
var self = this;
this.storage.getGlobal(this.key, function(json) {
var wl = json ? JSON.parse(json) : null;
if (!wl || !wl.expireTs)
return cb(false);
var expiredSince = Date.now() - wl.expireTs;
if (expiredSince >= 0)
return cb(false);
var isMyself = wl.sessionId === self.sessionId;
if (isMyself)
return cb(false);
// Seconds remainding
return cb(parseInt(-expiredSince / 1000));
});
};
WalletLock.prototype._setLock = function(cb) {
preconditions.checkArgument(cb);
preconditions.checkState(this.sessionId);
var self = this;
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
}, function() {
cb(null);
});
};
WalletLock.prototype._doKeepAlive = function(cb) {
preconditions.checkArgument(cb);
preconditions.checkState(this.sessionId);
var self = this;
this._isLockedByOther(function(t) {
if (t)
return cb(new Error('LOCKED: Wallet is locked for ' + t + ' srcs'));
self._setLock(cb);
});
};
WalletLock.prototype.keepAlive = function(cb) {
var self = this;
if (!self.sessionId) {
return self.init(self._doKeepAlive.bind(self, cb));
};
return this._doKeepAlive(cb);
};
WalletLock.prototype.release = function(cb) {
this.storage.removeGlobal(this.key, cb);
};
module.exports = WalletLock;

View file

@ -1,59 +0,0 @@
var cryptoUtil = require('../util/crypto');
var InsightStorage = require('./InsightStorage');
var inherits = require('inherits');
var log = require('../util/log');
var SEPARATOR = '%^#@';
function EncryptedInsightStorage(config) {
InsightStorage.apply(this, [config]);
}
inherits(EncryptedInsightStorage, InsightStorage);
EncryptedInsightStorage.prototype._brokenDecrypt = function(body) {
var key = cryptoUtil.kdf(this.password + this.email, 'mjuBtGybi/4=', 100);
log.debug('Trying legacy decrypt')
var decryptedJson = cryptoUtil.decrypt(key, body);
return decryptedJson;
};
EncryptedInsightStorage.prototype.resendVerificationEmail = function(callback) {
InsightStorage.prototype.resendVerificationEmail.apply(this, [callback]);
};
EncryptedInsightStorage.prototype.getItem = function(name, callback) {
var self = this;
InsightStorage.prototype.getItem.apply(this, [name,
function(err, body, headers) {
if (err) {
return callback(err);
}
var decryptedJson = cryptoUtil.decrypt(self.email + SEPARATOR + self.password, body);
if (!decryptedJson) {
log.debug('Could not decrypt value using current decryption schema');
decryptedJson = self._brokenDecrypt(body);
}
if (!decryptedJson) {
log.debug('Could not decrypt value.');
return callback('PNOTFOUND');
}
return callback(null, decryptedJson, headers);
}
]);
};
EncryptedInsightStorage.prototype.setItem = function(name, value, callback) {
var record = cryptoUtil.encrypt(this.email + SEPARATOR + this.password, value);
InsightStorage.prototype.setItem.apply(this, [name, record, callback]);
};
EncryptedInsightStorage.prototype.removeItem = function(name, callback) {
InsightStorage.prototype.removeItem.apply(this, [name, callback]);
};
EncryptedInsightStorage.prototype.clear = function(callback) {
InsightStorage.prototype.clear.apply(this, [callback]);
};
module.exports = EncryptedInsightStorage;

View file

@ -1,77 +0,0 @@
var cryptoUtil = require('../util/crypto');
var log = require('../util/log');
var LocalStorage = require('./LocalStorage');
var inherits = require('inherits');
var preconditions = require('preconditions').singleton();
var SEPARATOR = '@#$';
function EncryptedLocalStorage(config) {
LocalStorage.apply(this, [config]);
}
inherits(EncryptedLocalStorage, LocalStorage);
EncryptedLocalStorage.prototype._brokenDecrypt = function(body) {
var key = cryptoUtil.kdf(this.password + this.email, 'mjuBtGybi/4=', 100);
log.debug('Trying legacy decrypt')
var decryptedJson = cryptoUtil.decrypt(key, body);
return decryptedJson;
};
EncryptedLocalStorage.prototype._brokenDecryptUndef = function(body) {
var badkey = undefined + SEPARATOR + undefined;
return cryptoUtil.decrypt(badkey, body);
};
EncryptedLocalStorage.prototype.getItem = function(name, callback) {
var self = this;
preconditions.checkState(self.email);
LocalStorage.prototype.getItem.apply(this, [name,
function(err, body) {
var decryptedJson = cryptoUtil.decrypt(self.email + SEPARATOR + self.password, body);
if (!decryptedJson) {
log.debug('Could not decrypt value using current decryption schema');
decryptedJson = self._brokenDecrypt(body);
}
if (!decryptedJson) {
decryptedJson = self._brokenDecryptUndef(body);
}
if (!decryptedJson) {
log.debug('Could not decrypt value.');
return callback('PNOTFOUND');
}
return callback(null, decryptedJson);
}
]);
};
EncryptedLocalStorage.prototype.setItem = function(name, value, callback) {
if (!_.isString(value)) {
value = JSON.stringify(value);
}
var record = cryptoUtil.encrypt(this.email + SEPARATOR + this.password, value);
LocalStorage.prototype.setItem.apply(this, [name, record, callback]);
};
EncryptedLocalStorage.prototype.removeItem = function(name, callback) {
LocalStorage.prototype.removeItem.apply(this, [name, callback]);
};
EncryptedLocalStorage.prototype.clear = function(callback) {
LocalStorage.prototype.clear.apply(this, [callback]);
};
module.exports = EncryptedLocalStorage;

View file

@ -1,301 +0,0 @@
'use strict';
var preconditions = require('preconditions').singleton();
var loaded = 0;
var SCOPES = 'https://www.googleapis.com/auth/drive';
var log = require('../util/log');
function GoogleDrive(config) {
preconditions.checkArgument(config && config.clientId, 'No clientId at GoogleDrive config');
this.clientId = config.clientId;
this.home = config.home || 'copay';
this.idCache = {};
this.type = 'DB';
this.scripts = [{
then: this.initLoaded.bind(this),
src: 'https://apis.google.com/js/client.js?onload=InitGoogleDrive'
}];
this.isReady = false;
this.useImmediate = true;
this.ts = 100;
};
window.InitGoogleDrive = function() {
log.debug('googleDrive loadeded'); //TODO
loaded = 1;
};
GoogleDrive.prototype.init = function() {};
/**
* Called when the client library is loaded to start the auth flow.
*/
GoogleDrive.prototype.initLoaded = function() {
if (!loaded) {
window.setTimeout(this.initLoaded.bind(this), 500);
} else {
window.setTimeout(this.checkAuth.bind(this), 1);
}
}
/**
* Check if the current user has authorized the application.
*/
GoogleDrive.prototype.checkAuth = function() {
log.debug('Google Drive: Checking Auth');
gapi.auth.authorize({
'client_id': this.clientId,
'scope': SCOPES,
'immediate': this.useImmediate,
},
this.handleAuthResult.bind(this));
};
GoogleDrive.prototype.setCredentils = function(email, password, opts, callback) {
};
/**
* Called when authorization server replies.
*/
GoogleDrive.prototype.handleAuthResult = function(authResult) {
var self = this;
log.debug('Google Drive: authResult', authResult); //TODO
if (authResult.error) {
if (authResult.error) {
self.useImmediate = false;
return this.checkAuth();
};
throw new Error(authResult.error);
}
gapi.client.load('drive', 'v2', function() {
self.isReady = true;
});
}
GoogleDrive.prototype.checkReady = function() {
if (!this.isReady)
throw new Error('goggle drive is not ready!');
};
GoogleDrive.prototype._httpGet = function(theUrl) {
var accessToken = gapi.auth.getToken().access_token;
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xmlHttp.send(null);
return xmlHttp.responseText;
}
GoogleDrive.prototype.createItem = function(name, value, callback) {
this.getItem(name, function(err, retrieved) {
if (err || !retrieved) {
return this.setItem(name, value, callback);
} else {
return callback('EEXISTS');
}
});
};
GoogleDrive.prototype.getItem = function(k, cb) {
//console.log('[googleDrive.js.95:getItem:]', k); //TODO
var self = this;
self.checkReady();
self._idForName(k, function(kId) {
// console.log('[googleDrive.js.89:kId:]', kId); //TODO
if (!kId)
return cb(null);
var args = {
'path': '/drive/v2/files/' + kId,
'method': 'GET',
};
// console.log('[googleDrive.js.95:args:]', args); //TODO
var request = gapi.client.request(args);
request.execute(function(res) {
// console.log('[googleDrive.js.175:res:]', res); //TODO
if (!res || !res.downloadUrl)
return cb(null);
return cb(self._httpGet(res.downloadUrl));
});
});
};
GoogleDrive.prototype.setItem = function(k, v, cb) {
// console.log('[googleDrive.js.111:setItem:]', k, v); //TODO
var self = this;
self.checkReady();
self._idForName(this.home, function(parentId) {
preconditions.checkState(parentId);
// console.log('[googleDrive.js.118:parentId:]', parentId); //TODO
self._idForName(k, function(kId) {
// console.log('[googleDrive.js.105]', parentId, kId); //TODO
var boundary = '-------314159265358979323846';
var delimiter = "\r\n--" + boundary + "\r\n";
var close_delim = "\r\n--" + boundary + "--";
var metadata = {
'title': k,
'mimeType': 'application/octet-stream',
'parents': [{
'id': parentId
}],
};
var base64Data = btoa(v);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: application/octet-stream \r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var args = {
'path': '/upload/drive/v2/files' + (kId ? '/' + kId : ''),
'method': kId ? 'PUT' : 'POST',
'params': {
'uploadType': 'multipart',
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
}
// console.log('[googleDrive.js.148:args:]', args); //TODO
var request = gapi.client.request(args);
request.execute(function(ret) {
return cb(ret.kind === 'drive#file' ? null : new Error('error saving file on drive'));
});
});
});
};
GoogleDrive.prototype.removeItem = function(k, cb) {
var self = this;
self.checkReady();
self._idForName(this.home, function(parentId) {
preconditions.checkState(parentId);
self._idForName(k, function(kId) {
var args = {
'path': '/drive/v2/files/' + kId,
'method': 'DELETE',
};
var request = gapi.client.request(args);
request.execute(function() {
if (cb)
cb();
});
});
});
};
GoogleDrive.prototype.clear = function() {
this.checkReady();
throw new Error('clear not implemented');
};
GoogleDrive.prototype._mkdir = function(cb) {
preconditions.checkArgument(cb);
var self = this;
log.debug('Creating drive folder ' + this.home);
var request = gapi.client.request({
'path': '/drive/v2/files',
'method': 'POST',
'body': JSON.stringify({
'title': this.home,
'mimeType': "application/vnd.google-apps.folder",
}),
});
request.execute(function() {
self._idForName(self.home, cb);
});
};
GoogleDrive.prototype._idForName = function(name, cb) {
// console.log('[googleDrive.js.199:_idForName:]', name); //TODO
preconditions.checkArgument(name);
preconditions.checkArgument(cb);
var self = this;
if (!self.isReady) {
log.debug('Waiting for Google Drive');
self.ts = self.ts * 1.5;
return setTimeout(self._idForName.bind(self, name, cb), self.ts);
}
if (self.idCache[name]) {
// console.log('[googleDrive.js.212:] FROM CACHE', name, self.idCache[name]); //TODO
return cb(self.idCache[name]);
}
log.debug('GoogleDrive Querying for: ', name); //TODO
var args;
var idParent = name == this.home ? 'root' : self.idCache[this.home];
if (!idParent) {
return self._mkdir(function() {
self._idForName(name, cb);
});
}
// console.log('[googleDrive.js.177:idParent:]', idParent); //TODO
preconditions.checkState(idParent);
args = {
'path': '/drive/v2/files',
'method': 'GET',
'params': {
'q': "title='" + name + "' and trashed = false and '" + idParent + "' in parents",
}
};
var request = gapi.client.request(args);
request.execute(function(res) {
var i = res.items && res.items[0] ? res.items[0].id : false;
if (i)
self.idCache[name] = i;
// console.log('[googleDrive.js.238] CACHING ' + name + ':' + i); //TODO
return cb(self.idCache[name]);
});
};
GoogleDrive.prototype._checkHomeDir = function(cb) {
var self = this;
this._idForName(this.home, function(homeId) {
if (!homeId)
return self._mkdir(cb);
return cb(homeId);
});
};
module.exports = GoogleDrive;

View file

@ -1,287 +0,0 @@
var request = require('request');
var cryptoUtil = require('../util/crypto');
var bitcore = require('bitcore');
var buffers = require('buffer');
var querystring = require('querystring');
var Identity = require('../models/Identity');
var log = require('../util/log');
var SEPARATOR = '|';
function InsightStorage(config) {
this.type = 'DB';
this.storeUrl = config.url || 'https://insight.bitpay.com:443/api/email',
this.request = config.request || request;
this.iterations = config.iterations || 1000;
this.salt = config.salt || 'jBbYTj8zTrOt6V';
}
InsightStorage.prototype.init = function() {};
InsightStorage.prototype.setCredentials = function(email, password, opts) {
this.email = email;
this.password = password;
this._cachedKey = null;
};
InsightStorage.prototype.createItem = function(name, value, callback) {
var self = this;
this.getItem(name, function(err, retrieved) {
if (err || !retrieved) {
return self.setItem(name, value, callback);
} else {
return callback('EEXISTS');
}
});
};
InsightStorage.prototype.resendVerificationEmail = function (callback) {
var passphrase = this.getPassphrase();
var authHeader = new buffers.Buffer(this.email + ':' + passphrase).toString('base64');
var resendUrl = this.storeUrl + '/resend_email';
log.debug('Resending verification email: ' + this.email);
this.request.get({
url: resendUrl,
headers: {
'Authorization': authHeader
},
body: null,
}, function(err, response, body) {
if (err) {
return callback('Connection error');
}
if (response.statusCode === 409) {
return callback('BADCREDENTIALS: Invalid username or password');
} else if (response.statusCode !== 200) {
return callback('Unable to process the request');
}
return callback();
});
};
function mayBeOldPassword(password) {
// Test for base64
return /^[a-zA-Z0-9\/=\+]+$/.test(password);
}
InsightStorage.prototype.getItem = function(name, callback) {
var passphrase = this.getPassphrase();
var self = this;
this._makeGetRequest(passphrase, name, function(err, body, headers) {
if (err) log.warn(err);
if (err && err.indexOf('PNOTFOUND') !== -1 && mayBeOldPassword(self.password)) {
return self._brokenGetItem(name, callback);
}
return callback(err, body, headers);
});
};
/* This key need to have DIFFERENT
* settings(salt,iterations) than the kdf for wallet/profile encryption
* in Encrpted*Storage. The user should be able
* to change the settings on config.js to modify salt / iterations
* for encryption, but
* mantain the same key & passphrase. This is why those settings are
* not shared with encryption
*/
InsightStorage.prototype.getKey = function() {
if (!this._cachedKey) {
this._cachedKey = cryptoUtil.kdf(this.password + SEPARATOR + this.email, this.salt, this.iterations);
}
return this._cachedKey;
};
InsightStorage.prototype.getPassphrase = function() {
return bitcore.util.twoSha256(this.getKey()).toString('base64');
};
/**
* XmlHttpRequest's getAllResponseHeaders() method returns a string of response
* headers according to the format described here:
* http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method
* This method parses that string into a user-friendly key/value pair object.
*/
InsightStorage.parseResponseHeaders = function (headerStr) {
var headers = {};
if (!headerStr) {
return headers;
}
var headerPairs = headerStr.split('\u000d\u000a');
for (var i = 0, len = headerPairs.length; i < len; i++) {
var headerPair = headerPairs[i];
var index = headerPair.indexOf('\u003a\u0020');
if (index > 0) {
var key = headerPair.substring(0, index);
var val = headerPair.substring(index + 2);
headers[key] = val;
}
}
return headers;
}
InsightStorage.prototype._makeGetRequest = function(passphrase, key, callback) {
var authHeader = new buffers.Buffer(this.email + ':' + passphrase).toString('base64');
var retrieveUrl = this.storeUrl + '/retrieve';
var getParams = {
url: retrieveUrl + '?' + querystring.encode({
key: key,
rand: Math.random() // prevent cache
}),
headers: {
'Authorization': authHeader
}
};
this.request.get(getParams,
function(err, response, body) {
if (err) {
return callback('Connection error');
}
if (response.statusCode === 403) {
return callback('PNOTFOUND: Profile not found');
}
if (response.statusCode !== 200) {
return callback('Unable to read item from insight');
}
return callback(null, body, InsightStorage.parseResponseHeaders(response.getAllResponseHeaders()));
}
);
};
InsightStorage.prototype._brokenGetItem = function(name, callback) {
var passphrase = this._makeBrokenSecret();
var self = this;
log.debug('using legacy get');
this._makeGetRequest(passphrase, name, function(err, body) {
if (!err) {
return self._changePassphrase(function(err) {
if (err) {
return callback(err);
}
return callback(null, body);
});
}
return callback(err);
});
};
InsightStorage.prototype._makeBrokenSecret = function() {
var key = cryptoUtil.kdf(this.password + this.email, 'mjuBtGybi/4=', 100);
return cryptoUtil.kdf(key, this.password, 100);
};
InsightStorage.prototype._changePassphrase = function(callback) {
var passphrase = this._makeBrokenSecret();
var newPassphrase = this.getPassphrase();
var authHeader = new buffers.Buffer(this.email + ':' + passphrase).toString('base64');
var url = this.storeUrl + '/change_passphrase';
this.request.post({
url: url,
headers: {
'Authorization': authHeader
},
body: querystring.encode({
newPassphrase: newPassphrase
})
}, function(err, response, body) {
if (err) {
return callback('Connection error');
}
if (response.statusCode === 409) {
return callback('BADCREDENTIALS: Invalid username or password');
}
if (response.statusCode !== 200) {
return callback('Unable to store data on insight');
}
return callback();
});
};
InsightStorage.prototype.setItem = function(name, value, callback) {
var passphrase = this.getPassphrase();
var authHeader = new buffers.Buffer(this.email + ':' + passphrase).toString('base64');
var registerUrl = this.storeUrl + '/save';
log.debug('setItem ' + name + ' size:'+ (value.length/1024).toFixed(1) + 'kb' );
this.request.post({
url: registerUrl,
headers: {
'Authorization': authHeader
},
body: querystring.encode({
key: name,
record: value
})
}, function(err, response, body) {
if (err) {
return callback('Connection error');
}
if (response.statusCode === 409) {
return callback('BADCREDENTIALS: Invalid username or password');
} else if (response.statusCode === 406) {
return callback('OVERQUOTA: Quota exceeded');
} else if (response.statusCode === 501) {
return callback('EMAILERROR: Error sending verification email');
} else if (response.statusCode !== 200) {
return callback('Unable to store data on insight');
}
return callback();
});
};
InsightStorage.prototype.removeItem = function(key, callback) {
var passphrase = this.getPassphrase();
var authHeader = new buffers.Buffer(this.email + ':' + passphrase).toString('base64');
var deleteUrl = this.storeUrl + '/delete/item';
var getParams = {
url: deleteUrl + '?' + querystring.encode({
key: key
}),
headers: {
'Authorization': authHeader
}
};
log.debug('Erasing: ' + key);
this.request.get(getParams, function(err, response, body) {
if (err) {
return callback('Connection error');
}
if (response.statusCode === 409) {
return callback('BADCREDENTIALS: Invalid username or password');
} else if (response.statusCode !== 200) {
return callback('Unable to remove data on insight');
}
return callback();
});
};
InsightStorage.prototype.clear = function(callback) {
var passphrase = this.getPassphrase();
var authHeader = new buffers.Buffer(this.email + ':' + passphrase).toString('base64');
var deleteUrl = this.storeUrl + '/delete/profile';
log.debug('Clearing storage for: ' + this.email);
this.request.post({
url: deleteUrl,
headers: {
'Authorization': authHeader
},
body: null,
}, function(err, response, body) {
if (err) {
return callback('Connection error');
}
if (response.statusCode === 409) {
return callback('BADCREDENTIALS: Invalid username or password');
} else if (response.statusCode !== 200) {
return callback('Unable to remove data on insight');
}
return callback();
});
};
module.exports = InsightStorage;

View file

@ -1,106 +0,0 @@
'use strict';
var _ = require('lodash');
var preconditions = require('preconditions').singleton();
var isChromeApp = typeof window !== "undefined" && window.chrome && chrome.runtime && chrome.runtime.id;
function LocalStorage(opts) {
this.type = 'DB';
opts = opts || {};
this.ls = opts.ls ||
((typeof localStorage !== "undefined") ? localStorage : null);
if (isChromeApp && !this.ls) {
this.ls = localStorage = chrome.storage.local;
window.localStorage = chrome.storage.local;
}
preconditions.checkState(this.ls,
'localstorage not available, cannot run plugin');
};
LocalStorage.prototype.init = function() {};
LocalStorage.prototype.setCredentials = function(email, password, opts) {
this.email = email;
this.password = password;
};
LocalStorage.prototype.getItem = function(k, cb) {
if (isChromeApp) {
chrome.storage.local.get(k,
function(data) {
//TODO check for errors
return cb(null, data[k]);
});
} else {
return cb(null, this.ls.getItem(k));
}
};
/**
* Same as setItem, but fails if an item already exists
*/
LocalStorage.prototype.createItem = function(name, value, callback) {
var self = this;
self.getItem(name,
function(err, data) {
if (data) {
return callback('EEXISTS');
} else {
return self.setItem(name, value, callback);
}
});
};
LocalStorage.prototype.setItem = function(k, v, cb) {
if (isChromeApp) {
var obj = {};
obj[k] = v;
chrome.storage.local.set(obj, cb);
} else {
this.ls.setItem(k, v);
return cb();
}
};
LocalStorage.prototype.removeItem = function(k, cb) {
if (isChromeApp) {
chrome.storage.local.remove(k, cb);
} else {
this.ls.removeItem(k);
return cb();
}
};
LocalStorage.prototype.clear = function(cb) {
// NOP
return cb();
};
LocalStorage.prototype.allKeys = function(cb) {
if (isChromeApp) {
chrome.storage.local.get(null, function(items) {
return cb(null, _.keys(items));
});
} else {
var ret = [];
var l = this.ls.length;
for (var i = 0; i < l; i++)
ret.push(this.ls.key(i));
return cb(null, ret);
}
};
module.exports = LocalStorage;

View file

@ -1,28 +0,0 @@
'use strict';
angular.module('copayApp.services')
.factory('applicationService', function($rootScope, $location, $timeout, go, isCordova) {
var root = {};
var isChromeApp = window.chrome && chrome.runtime && chrome.runtime.id;
root.restart = function() {
if (isCordova) {
$rootScope.iden = $rootScope.wallet = undefined;
go.path('/');
$timeout(function(){
$rootScope.$digest();
},1);
} else {
// Go home reloading the application
var hashIndex = window.location.href.indexOf('#!/');
if (isChromeApp) {
chrome.runtime.reload();
} else {
window.location = window.location.href.substr(0, hashIndex);
}
}
};
return root;
});

View file

@ -1,86 +0,0 @@
'use strict';
var BackupService = function($rootScope, notification) {
this.$rootScope = $rootScope;
this.notifications = notification;
};
BackupService.prototype.getCopayer = function(wallet) {
return wallet.totalCopayers > 1 ? wallet.getMyCopayerNickname() : '';
};
BackupService.prototype._download = function(ew, walletName, filename) {
var NewBlob = function(data, datatype) {
var out;
try {
out = new Blob([data], {
type: datatype
});
console.debug("case 1");
} catch (e) {
window.BlobBuilder = window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
if (e.name == 'TypeError' && window.BlobBuilder) {
var bb = new BlobBuilder();
bb.append(data);
out = bb.getBlob(datatype);
console.debug("case 2");
} else if (e.name == "InvalidStateError") {
// InvalidStateError (tested on FF13 WinXP)
out = new Blob([data], {
type: datatype
});
console.debug("case 3");
} else {
// We're screwed, blob constructor unsupported entirely
console.debug("Errore");
}
}
return out;
};
var blob;
blob = new NewBlob(ew, 'text/plain;charset=utf-8');
this.notifications.success('Backup created', 'Encrypted backup file saved');
// otherwise lean on the browser implementation
saveAs(blob, filename);
};
BackupService.prototype.walletEncrypted = function(wallet) {
return wallet.exportEncrypted(this.$rootScope.iden.password);
}
BackupService.prototype.walletDownload = function(wallet) {
var ew = this.walletEncrypted(wallet);
var walletName = wallet.getName();
var copayerName = this.getCopayer(wallet);
var filename = (copayerName ? copayerName + '-' : '') + walletName + '-keybackup.json.aes';
this._download(ew, walletName, filename)
};
BackupService.prototype.profileEncrypted = function(iden) {
iden.setBackupNeeded(false);
return iden.exportEncryptedWithWalletInfo(iden.password);
}
BackupService.prototype.profileDownload = function(iden) {
var ew = this.profileEncrypted(iden);
var name = iden.fullName;
var filename = name + '-profile.json';
this._download(ew, name, filename)
};
angular.module('copayApp.services').service('backupService', BackupService);

View file

@ -1,99 +0,0 @@
'use strict';
var bitcore = require('bitcore');
angular.module('copayApp.services')
.factory('balanceService', function($rootScope, $filter, $timeout, rateService) {
var root = {};
var _balanceCache = {};
root.clearBalanceCache = function(w) {
w.clearUnspentCache();
delete _balanceCache[w.getId()];
};
root._fetchBalance = function(w, cb) {
cb = cb || function() {};
var satToUnit = 1 / w.settings.unitToSatoshi;
var COIN = bitcore.util.COIN;
w.getBalance(function(err, balanceSat, balanceByAddrSat, safeBalanceSat, safeUnspentCount) {
if (err) return cb(err);
var r = {};
r.totalBalance = $filter('noFractionNumber')(balanceSat * satToUnit);
r.totalBalanceBTC = (balanceSat / COIN);
var availableBalanceNr = safeBalanceSat * satToUnit;
r.availableBalance = $filter('noFractionNumber')(safeBalanceSat * satToUnit);
r.availableBalanceBTC = (safeBalanceSat / COIN);
r.safeUnspentCount = safeUnspentCount;
var lockedBalance = (balanceSat - safeBalanceSat) * satToUnit;
r.lockedBalance = lockedBalance ? $filter('noFractionNumber')(lockedBalance) : null;
r.lockedBalanceBTC = (balanceSat - safeBalanceSat) / COIN;
if (r.safeUnspentCount) {
var estimatedFee = copay.Wallet.estimatedFee(r.safeUnspentCount);
r.topAmount = (((availableBalanceNr * w.settings.unitToSatoshi).toFixed(0) - estimatedFee) / w.settings.unitToSatoshi);
}
var balanceByAddr = {};
for (var ii in balanceByAddrSat) {
balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit;
}
r.balanceByAddr = balanceByAddr;
if (rateService.isAvailable()) {
var totalBalanceAlternative = rateService.toFiat(balanceSat, w.settings.alternativeIsoCode);
var lockedBalanceAlternative = rateService.toFiat(balanceSat - safeBalanceSat, w.settings.alternativeIsoCode);
var alternativeConversionRate = rateService.toFiat(100000000, w.settings.alternativeIsoCode);
r.totalBalanceAlternative = $filter('noFractionNumber')(totalBalanceAlternative, 2);
r.lockedBalanceAlternative = $filter('noFractionNumber')(lockedBalanceAlternative, 2);
r.alternativeConversionRate = $filter('noFractionNumber')(alternativeConversionRate, 2);
r.alternativeBalanceAvailable = true;
r.alternativeIsoCode = w.settings.alternativeIsoCode;
};
r.updatingBalance = false;
return cb(null, r)
});
};
root.update = function(w, cb, isFocused) {
w = w || $rootScope.wallet;
if (!w || !w.isComplete()) return;
copay.logger.debug('Updating balance of:', w.getName(), isFocused);
var wid = w.getId();
// cache available? Set the cached values until we updated them
if (_balanceCache[wid]) {
w.balanceInfo = _balanceCache[wid];
} else {
if (isFocused)
$rootScope.updatingBalance = true;
}
w.balanceInfo = w.balanceInfo || {};
w.balanceInfo.updating = true;
root._fetchBalance(w, function(err, res) {
if (err) throw err;
w.balanceInfo = _balanceCache[wid] = res;
w.balanceInfo.updating = false;
if (isFocused) {
$rootScope.updatingBalance = false;
}
// we alwalys calltimeout because if balance is cached, we are still on the same
// execution path
if (cb) $timeout(function() {
return cb();
}, 1);
});
};
return root;
});

View file

@ -1,13 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('Compatibility', function($rootScope) {
var root = {};
root.check = function (scope) {
copay.Compatibility.listWalletsPre8(function(wallets) {
scope.anyWallet = wallets.length > 0 ? true : false;
scope.oldWallets = wallets;
});
};
return root;
});

View file

@ -1,36 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('configService', function($timeout, localstorageService, gettextCatalog, defaults) {
var root = {};
root.set = function(opts, cb) {
// Options that have runtime effects
if (opts.logLevel)
copay.logger.setLevel(opts.logLevel);
// Set current version
opts.version = copay.version;
localstorageService.getItem('config', function(err, oldOpsStr) {
var oldOpts = {};
try {
oldOpts = JSON.parse(oldOpsStr);
} catch (e) {};
var newOpts = {};
_.extend(newOpts, copay.defaultConfig, oldOpts, opts);
// TODO remove this global variable.
config = newOpts;
localstorageService.setItem('config', JSON.stringify(newOpts), cb);
});
};
root.reset = function(cb) {
config = defaults;
localstorageService.removeItem('config', cb);
};
return root;
});

View file

@ -1,98 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('go', function($window, $rootScope, $location) {
var root = {};
var hideSidebars = function() {
if (typeof document === 'undefined')
return;
// hack to hide sidebars and use ng-click (no href=)
var win = angular.element($window);
var elem = angular.element(document.querySelector('#off-canvas-wrap'))
elem.removeClass('move-right');
elem.removeClass('move-left');
};
var toggleSidebar = function(invert) {
if (typeof document === 'undefined')
return;
var elem = angular.element(document.querySelector('#off-canvas-wrap'));
var leftbarActive = angular.element(document.getElementsByClassName('move-right')).length;
if (invert) {
if ($rootScope.iden && !$rootScope.hideNavigation) {
elem.addClass('move-right');
}
}
else {
if (leftbarActive) {
hideSidebars();
}
}
};
root.openExternalLink = function(url) {
var ref = window.open(url, '_blank', 'location=no');
};
root.path = function(path) {
var parts = path.split('#');
$location.path(parts[0]);
if (parts[1])
$location.hash(parts[1]);
hideSidebars();
};
root.swipe = function(invert) {
toggleSidebar(invert);
};
root.walletHome = function() {
var w = $rootScope.wallet;
preconditions.checkState(w);
$rootScope.starting = false;
if (!w.isComplete()) {
root.path('copayers');
} else {
if ($rootScope.pendingPayment) {
if ($rootScope.walletForPaymentSet) {
root.path('send');
} else {
root.path('selectWalletForPayment');
}
} else {
root.path('homeWallet');
}
}
};
root.home = function() {
if ($rootScope.iden)
root.walletHome();
else
root.path('/');
};
root.send = function() {
$location.path('send');
};
// Global go. This should be in a better place TODO
// We dont do a 'go' directive, to use the benefits of ng-touch with ng-click
$rootScope.go = function (path) {
root.path(path);
};
$rootScope.openExternalLink = function (url) {
root.openExternalLink(url);
};
return root;
});

View file

@ -1,363 +0,0 @@
'use strict';
angular.module('copayApp.services')
.factory('identityService', function($rootScope, $location, $timeout, $filter, pluginManager, notification, pendingTxsService, balanceService, applicationService, go) {
// TODO:
// * remove iden from rootScope
// * remove wallet from rootScope
// * create walletService
var root = {};
root.check = function(scope) {
copay.Identity.checkIfExistsAny({
pluginManager: pluginManager.getInstance(config),
}, function(anyProfile) {
copay.Wallet.checkIfExistsAny({
pluginManager: pluginManager.getInstance(config),
}, function(anyWallet) {
scope.loading = false;
scope.anyProfile = anyProfile ? true : false;
scope.anyWallet = anyWallet ? true : false;
if (!scope.anyProfile) {
$location.path('/createProfile');
}
});
});
};
root.create = function(email, password, cb) {
copay.Identity.create({
email: email,
password: password,
pluginManager: pluginManager.getInstance(config),
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
failIfExists: true,
}, function(err, iden) {
if (err) return cb(err);
preconditions.checkState(iden);
root.bind(iden);
return cb(null);
});
};
root.createDefaultWallet = function(cb) {
var iden = $rootScope.iden;
var walletOptions = {
nickname: iden.fullName,
networkName: config.networkName,
requiredCopayers: 1,
totalCopayers: 1,
password: iden.password,
name: 'My wallet',
};
iden.createWallet(walletOptions, function(err, wallet) {
return cb(err);
});
};
root.resendVerificationEmail = function(cb) {
var iden = $rootScope.iden;
iden.resendVerificationEmail(cb);
};
root.setServerStatus = function(headers) {
if (!headers)
return;
var customHeaders = {};
_.each(_.keys(headers), function(headerKey) {
var hk = headerKey.toLowerCase();
if (hk.indexOf('x-') === 0) {
customHeaders[hk] = headers[headerKey];
}
});
if (customHeaders['x-email-needs-validation'])
$rootScope.needsEmailConfirmation = true;
else
$rootScope.needsEmailConfirmation = null;
if (customHeaders['x-quota-per-item'])
$rootScope.quotaPerItem = parseInt(customHeaders['x-quota-per-item']);
if (customHeaders['x-quota-items-limit'])
$rootScope.quotaItems = parseInt(customHeaders['x-quota-items-limit']);
};
root.open = function(email, password, cb) {
var opts = {
email: email,
password: password,
pluginManager: pluginManager.getInstance(config),
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
};
copay.Identity.open(opts, function(err, iden, headers) {
if (err) return cb(err);
root.setServerStatus(headers);
root.bind(iden);
return cb(null, iden);
});
};
root.deleteProfile = function(cb) {
$rootScope.iden.remove(null, cb);
};
root.deleteWallet = function(w, cb) {
$rootScope.iden.deleteWallet(w.getId(), cb);
};
root.isFocused = function(wid) {
return $rootScope.wallet && wid === $rootScope.wallet.getId();
};
root.setupGlobalVariables = function(iden) {
$rootScope.reconnecting = false;
$rootScope.iden = iden;
};
root.noFocusedWallet = function() {
$rootScope.wallet = null;
$timeout(function() {
$rootScope.$digest();
})
};
root.setFocusedWallet = function(w, dontUpdateIt) {
if (!_.isObject(w))
w = $rootScope.iden.getWalletById(w);
preconditions.checkState(w && _.isObject(w));
copay.logger.debug('Set focus:', w.getName());
$rootScope.wallet = w;
if (!dontUpdateIt)
$rootScope.iden.updateFocusedTimestamp(w.getId());
pendingTxsService.update();
$timeout(function() {
$rootScope.$digest();
}, 1);
};
root.notifyTxProposalEvent = function(w, e) {
if (e.cId == w.getMyCopayerId())
return;
var user = w.publicKeyRing.nicknameForCopayer(e.cId);
var name = w.getName();
switch (e.type) {
case copay.Wallet.TX_NEW:
notification.info('[' + name + '] New Transaction',
$filter('translate')('You received a transaction proposal from') + ' ' + user);
break;
case copay.Wallet.TX_SIGNED:
notification.success('[' + name + '] Transaction Signed',
$filter('translate')('A transaction was signed by') + ' ' + user);
break;
case copay.Wallet.TX_BROADCASTED:
notification.success('[' + name + '] Transaction Approved',
$filter('translate')('A transaction was broadcasted by') + ' ' + user);
break;
case copay.Wallet.TX_REJECTED:
notification.warning('[' + name + '] Transaction Rejected',
$filter('translate')('A transaction was rejected by') + ' ' + user);
break;
}
};
root.installWalletHandlers = function(w) {
var wid = w.getId();
w.on('connectionError', function() {
if (root.isFocused(wid)) {
var message = "Could not connect to the Insight server. Check your settings and network configuration";
notification.error('Networking Error', message);
}
});
w.on('corrupt', function(peerId) {
copay.logger.warn('Received corrupt message from ' + peerId);
});
w.on('publicKeyRingUpdated', function() {
$rootScope.$digest();
});
w.on('ready', function() {
var isFocused = root.isFocused(wid);
copay.logger.debug('Wallet:' + w.getName() +
' is ready. Focused:', isFocused);
balanceService.update(w, function() {
$rootScope.$digest();
}, isFocused);
});
w.on('tx', function(address, isChange) {
if (!isChange) {
notification.funds('Funds received on ' + w.getName(), address);
}
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
});
w.on('balanceUpdated', function() {
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
});
w.on('insightReconnected', function() {
$rootScope.reconnecting = false;
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
});
w.on('insightError', function() {
if (root.isFocused(wid)) {
$rootScope.reconnecting = true;
$rootScope.$digest();
}
});
w.on('newAddresses', function() {
// Nothing yet
});
// Disabled for now, does not seens to have much value for the user
// w.on('paymentACK', function(memo) {
// notification.success('Payment Acknowledged', memo);
// });
w.on('txProposalEvent', function(ev) {
if (root.isFocused(wid)) {
pendingTxsService.update();
}
// TODO aqui lo unico que cambia son los locked
// se puede optimizar
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
root.notifyTxProposalEvent(w, ev);
$timeout(function() {
$rootScope.$digest();
});
});
w.on('addressBookUpdated', function(dontDigest) {
if (root.isFocused(wid)) {
if (!dontDigest) {
$rootScope.$digest();
}
}
});
w.on('connect', function(peerID) {
$rootScope.$digest();
});
// TODO?
// w.on('close', );
// w.on('locked',);
};
root.bind = function(iden) {
preconditions.checkArgument(_.isObject(iden));
copay.logger.debug('Binding profile...');
var self = this;
root.setupGlobalVariables(iden);
iden.on('newWallet', function(wid) {
var w = iden.getWalletById(wid);
copay.logger.debug('newWallet:',
w.getName(), wid, iden.getLastFocusedWalletId());
root.installWalletHandlers(w);
if (wid == iden.getLastFocusedWalletId()) {
copay.logger.debug('GOT Focused wallet:', w.getName());
root.setFocusedWallet(w, true);
go.walletHome();
}
// At the end (after all handlers are in place)...start the wallet.
w.netStart();
});
iden.on('noWallets', function() {
notification.warning('No Wallets', 'Your profile has no wallets. Create one here');
$rootScope.starting = false;
$location.path('/add');
$timeout(function() {
$rootScope.$digest();
}, 1);
});
iden.on('walletDeleted', function(wid) {
// do nothing. this is handled 'on sync' on controller.
});
iden.on('walletStorageError', function(wid, message) {
notification.error('Error storing wallet', message);
});
iden.on('closed', function() {
delete $rootScope['wallet'];
delete $rootScope['iden'];
applicationService.restart();
});
};
root.signout = function() {
if ($rootScope.iden) {
$rootScope.signingOut = true;
$rootScope.iden.close(function() { // Will trigger 'closed'
$timeout(function() {
$rootScope.signingOut = null;
}, 100);
}); // Will trigger 'closed'
}
};
root.createWallet = function(opts, cb) {
$rootScope.iden.createWallet(opts, cb);
};
root.importWallet = function(encryptedObj, pass, opts, cb) {
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, pass, opts, cb);
};
root.joinWallet = function(opts, cb) {
$rootScope.iden.joinWallet(opts, function(err, w) {
return cb(err);
});
};
root.importProfile = function(str, password, cb) {
copay.Identity.importFromEncryptedFullJson(str, password, {
pluginManager: pluginManager.getInstance(config),
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
}, function(err, iden, walletObjs) {
if (err) return cb(err);
root.bind(iden);
iden.importMultipleWalletsFromObj(walletObjs);
return cb();
});
};
return root;
});

View file

@ -1,3 +0,0 @@
'use strict';
angular.module('copayApp.services').value('isCordova', window.cordova ? true : false);

View file

@ -1,29 +0,0 @@
'use strict';
// Detect mobile devices
var isMobile = {
Android: function() {
return !!navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return !!navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return !!navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return !!navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return !!navigator.userAgent.match(/IEMobile/i);
},
Safari: function() {
return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
angular.module('copayApp.services').value('isMobile', isMobile);

View file

@ -1,9 +0,0 @@
'use strict';
angular.module('copayApp.services')
.factory('localstorageService', function($rootScope) {
var LS = require('../js/plugins/LocalStorage');
var ls = new LS();
return ls;
});

View file

@ -1,288 +0,0 @@
'use strict';
var LS = require('../js/plugins/LocalStorage');
var ls = new LS();
angular.module('copayApp.services').
factory('notification', ['$timeout',
function($timeout) {
var notifications = [];
ls.getItem('notifications', function(err, data) {
if (data) {
notifications = JSON.parse(data);
}
});
var queue = [];
var settings = {
info: {
duration: 6000,
enabled: true
},
funds: {
duration: 7000,
enabled: true
},
version: {
duration: 60000,
enabled: true
},
warning: {
duration: 7000,
enabled: true
},
error: {
duration: 7000,
enabled: true
},
success: {
duration: 5000,
enabled: true
},
progress: {
duration: 0,
enabled: true
},
custom: {
duration: 35000,
enabled: true
},
details: true,
localStorage: false,
html5Mode: false,
html5DefaultIcon: 'img/favicon.ico'
};
function html5Notify(icon, title, content, ondisplay, onclose) {
if (window.webkitNotifications && window.webkitNotifications.checkPermission() === 0) {
if (!icon) {
icon = 'img/favicon.ico';
}
var noti = window.webkitNotifications.createNotification(icon, title, content);
if (typeof ondisplay === 'function') {
noti.ondisplay = ondisplay;
}
if (typeof onclose === 'function') {
noti.onclose = onclose;
}
noti.show();
} else {
settings.html5Mode = false;
}
}
return {
/* ========== SETTINGS RELATED METHODS =============*/
disableHtml5Mode: function() {
settings.html5Mode = false;
},
disableType: function(notificationType) {
settings[notificationType].enabled = false;
},
enableHtml5Mode: function() {
// settings.html5Mode = true;
settings.html5Mode = this.requestHtml5ModePermissions();
},
enableType: function(notificationType) {
settings[notificationType].enabled = true;
},
getSettings: function() {
return settings;
},
toggleType: function(notificationType) {
settings[notificationType].enabled = !settings[notificationType].enabled;
},
toggleHtml5Mode: function() {
settings.html5Mode = !settings.html5Mode;
},
requestHtml5ModePermissions: function() {
if (window.webkitNotifications) {
if (window.webkitNotifications.checkPermission() === 0) {
return true;
} else {
window.webkitNotifications.requestPermission(function() {
if (window.webkitNotifications.checkPermission() === 0) {
settings.html5Mode = true;
} else {
settings.html5Mode = false;
}
});
return false;
}
} else {
return false;
}
},
/* ============ QUERYING RELATED METHODS ============*/
getAll: function() {
// Returns all notifications that are currently stored
return notifications;
},
getQueue: function() {
return queue;
},
/* ============== NOTIFICATION METHODS ==============*/
info: function(title, content, userData) {
return this.awesomeNotify('info', 'info', title, content, userData);
},
funds: function(title, content, userData) {
return this.awesomeNotify('funds', 'bitcoin-circle', title, content, userData);
},
version: function(title, content, severe) {
return this.awesomeNotify('version', severe ? 'alert' : 'flag', title, content);
},
error: function(title, content, userData) {
return this.awesomeNotify('error', 'x', title, content, userData);
},
success: function(title, content, userData) {
return this.awesomeNotify('success', 'check', title, content, userData);
},
warning: function(title, content, userData) {
return this.awesomeNotify('warning', 'alert', title, content, userData);
},
awesomeNotify: function(type, icon, title, content, userData) {
/**
* Supposed to wrap the makeNotification method for drawing icons using font-awesome
* rather than an image.
*
* Need to find out how I'm going to make the API take either an image
* resource, or a font-awesome icon and then display either of them.
* Also should probably provide some bits of color, could do the coloring
* through classes.
*/
// image = '<i class="icon-' + image + '"></i>';
return this.makeNotification(type, false, icon, title, content, userData);
},
notify: function(image, title, content, userData) {
// Wraps the makeNotification method for displaying notifications with images
// rather than icons
return this.makeNotification('custom', image, true, title, content, userData);
},
makeNotification: function(type, image, icon, title, content, userData) {
var notification = {
'type': type,
'image': image,
'icon': icon,
'title': title,
'content': content,
'timestamp': +new Date(),
'userData': userData
};
notifications.push(notification);
if (settings.html5Mode) {
html5Notify(image, title, content, function() {
// inner on display function
}, function() {
// inner on close function
});
}
//this is done because html5Notify() changes the variable settings.html5Mode
if (!settings.html5Mode) {
queue.push(notification);
$timeout(function removeFromQueueTimeout() {
queue.splice(queue.indexOf(notification), 1);
}, settings[type].duration);
}
// Mobile notification
if (window && window.navigator && window.navigator.vibrate) {
window.navigator.vibrate([200, 100, 200]);
};
if (document.hidden && (type == 'info' || type == 'funds')) {
new window.Notification(title, {
body: content,
icon: 'img/notification.png'
});
}
this.save();
return notification;
},
/* ============ PERSISTENCE METHODS ============ */
save: function() {
// Save all the notifications into localStorage
if (settings.localStorage) {
localStorage.setItem('notifications', JSON.stringify(notifications));
}
},
restore: function() {
// Load all notifications from localStorage
},
clear: function() {
notifications = [];
this.save();
}
};
}
]).directive('notifications', function(notification, $compile) {
/**
*
* It should also parse the arguments passed to it that specify
* its position on the screen like "bottom right" and apply those
* positions as a class to the container element
*
* Finally, the directive should have its own controller for
* handling all of the notifications from the notification service
*/
function link(scope, element, attrs) {
var position = attrs.notifications;
position = position.split(' ');
element.addClass('dr-notification-container');
for (var i = 0; i < position.length; i++) {
element.addClass(position[i]);
}
}
return {
restrict: 'A',
scope: {},
templateUrl: 'views/includes/notifications.html',
link: link,
controller: ['$scope',
function NotificationsCtrl($scope) {
$scope.queue = notification.getQueue();
$scope.removeNotification = function(noti) {
$scope.queue.splice($scope.queue.indexOf(noti), 1);
};
}
]
};
});

View file

@ -1,51 +0,0 @@
'use strict';
angular.module('copayApp.services')
.factory('pendingTxsService', function($rootScope, $filter, rateService) {
var root = {};
root.setAlternativeAmount = function(w, tx, cb) {
var alternativeIsoCode = w.settings.alternativeIsoCode;
rateService.whenAvailable(function() {
_.each(tx.outs, function(out) {
var valueSat = out.valueSat * w.settings.unitToSatoshi;
out.alternativeAmount = $filter('noFractionNumber')(
rateService.toFiat(valueSat, alternativeIsoCode), 2);
out.alternativeIsoCode = alternativeIsoCode;
});
if (cb) return cb(tx);
});
};
root.getDecoratedTxProposals = function(w) {
var txps = w.getPendingTxProposals();
_.each(txps, function(tx) {
root.setAlternativeAmount(w, tx);
if (tx.outs) {
_.each(tx.outs, function(out) {
out.valueSat = out.value;
out.value = $filter('noFractionNumber')(out.value);
});
}
});
return txps;
};
/**
* @desc adds 2 fields to wallet: pendingTxProposalsCountForUs, pendingTxProposals.
*
* @param w wallet
*/
root.update = function(w) {
var w = $rootScope.wallet;
if (!w) return;
//pendingTxCount
var ret = w.getPendingTxProposalsCount();
w.pendingTxProposalsCountForUs = ret.pendingForUs;
w.pendingTxProposals = root.getDecoratedTxProposals(w);
};
return root;
});

View file

@ -1,115 +0,0 @@
'use strict';
angular.module('copayApp.services')
.factory('pinService', function($rootScope, $timeout, localstorageService) {
var KEY = 'pinDATA';
var SALT = '4gllotIKguqi0EkIslC0';
var ITER = 5000;
var ls = localstorageService;
var root = {};
var _firstpin;
root.check = function(cb) {
ls.getItem(KEY, function(err, value) {
return cb(err, value ? true : false);
});
};
root.get = function(pin, cb) {
ls.getItem(KEY, function(err, value) {
if (!value) return cb(null);
var enc = value;
var data = copay.crypto.decrypt('' + parseInt(pin), enc);
var err = new Error('Could not decrypt');
if (data) {
var obj;
try {
obj = JSON.parse(data);
err = null;
} catch (e) {};
}
return cb(err, obj);
});
};
root.save = function(pin, email, password, cb) {
var credentials = {
email: email,
password: password,
};
var enc = copay.crypto.encrypt('' + parseInt(pin), credentials, SALT, ITER);
ls.setItem(KEY, enc, function(err) {
return cb(err);
});
};
root.clear = function(cb) {
ls.removeItem(KEY, cb);
};
root.clearPin = function(scope) {
scope.digits = [];
scope.defined = [];
};
root.pressPin = function(scope, digit, skipOpenWithPin) {
scope.error = null;
scope.digits.push(digit);
scope.defined.push(true);
if (scope.digits.length == 4) {
var pin = scope.digits.join('');
if (!$rootScope.hasPin) {
if (!_firstpin) {
_firstpin = pin;
scope.askForPin = 2;
$timeout(function() {
scope.clear();
}, 100);
return;
}
else {
if (pin === _firstpin) {
_firstpin = null;
scope.askForPin = null;
scope.createPin(pin);
return;
}
else {
_firstpin = null;
scope.askForPin = 1;
$timeout(function() {
scope.clear();
scope.error = 'Entered PINs were not equal. Try again';
$timeout(function() {
scope.error = null;
}, 2000);
}, 100);
return;
}
}
}
if (!skipOpenWithPin) {
scope.openWithPin(pin);
}
}
};
root.skipPin = function(scope, creatingProfile) {
if (!$rootScope.hasPin) {
if (!creatingProfile) {
scope.openWallets();
}
else {
scope.createDefaultWallet()
}
}
else {
scope.pinLogout();
}
};
return root;
});

View file

@ -1,10 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('pluginManager', function() {
var root = {};
root.getInstance = function(config){
return new copay.PluginManager(config);
};
return root;
});

View file

@ -1,8 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('rateService', function(request) {
var cfg = _.extend(config.rates, {
request: request
});
return copay.RateService.singleton(cfg);
});

View file

@ -1,6 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('request', function() {
return require('request');
});

View file

@ -1,36 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('txStatus', function($modal) {
var root = {};
root.notify = function(status) {
var msg;
if (status == copay.Wallet.TX_BROADCASTED)
msg = 'Transaction broadcasted';
else if (status == copay.Wallet.TX_PROPOSAL_SENT)
msg = 'Transaction proposal created';
else if (status == copay.Wallet.TX_SIGNED)
msg = 'Transaction proposal signed';
else if (status == copay.Wallet.TX_REJECTED)
msg = 'Transaction was rejected';
if (msg)
root.openModal(msg);
};
root.openModal = function(statusStr) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.statusStr = statusStr;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
$modal.open({
templateUrl: 'views/modals/tx-status.html',
windowClass: 'tiny',
controller: ModalInstanceCtrl,
});
};
return root;
});

View file

@ -1,14 +0,0 @@
'use strict';
var UriHandler = function() {};
UriHandler.prototype.register = function() {
var base = window.location.origin + '/';
var url = base + '#!/uri-payment/%s';
if(navigator.registerProtocolHandler) {
navigator.registerProtocolHandler('bitcoin', url, 'Copay');
}
};
angular.module('copayApp.services').value('uriHandler', new UriHandler());

View file

@ -1,122 +0,0 @@
/*
** copay-shell integration
*/
(function() {
/*
** This is a monkey patch for when Copay is running from
** within Copay-Shell (atom-shell). Since the renderer (the frontend)
** receives context from Node.js, we get a `module.exports` contruct
** available to us. Because of this, some libs (specifically Moment.js)
** attempt to assume their CommonJS form and bind to this. This causes
** there to be no references in the window to these libs, so let's trick
** the renderer into thinking that we are _not_ in a CommonJS environment.
*/
if (typeof module !== 'undefined') module = {
exports: false
};
// are we running in copay shell?
if (window.process && process.type === 'renderer') {
window.cshell = initCopayShellBindings();
} else {
return;
}
function controller(name) {
return angular.element(
document.querySelectorAll(
'[ng-controller="' + name + '"], [data-ng-controller="' + name + '"]'
)
).scope();
};
function needsWalletLogin(ipc) {
ipc.send('alert', 'info', 'Please select a wallet.');
};
function initCopayShellBindings() {
var ipc = require('ipc');
var clipb = require('clipboard');
// atom shell forces to implement the clipboard (on osx) on our own - thanks obama.
Mousetrap.stopCallback = function() {
return false
};
Mousetrap.bind('command+c', function(e) {
clipb.writeText(window.getSelection().toString());
});
Mousetrap.bind('command+v', function(e) {
if (document.activeElement) {
document.activeElement.value = clipb.readText();
document.activeElement.dispatchEvent(new Event('change'));
}
});
// handle messages
ipc.on('address:create', function(data) {
location.href = '#/addresses';
var ctrl = controller('AddressesController');
if (!ctrl) return needsWalletLogin(ipc);
ctrl.newAddr();
});
ipc.on('transactions:send', function(data) {
location.href = '#/send';
var ctrl = controller('SendController');
if (!ctrl) return needsWalletLogin(ipc);
});
ipc.on('transactions:all', function(data) {
location.href = '#/transactions';
var ctrl = controller('TransactionsController');
if (!ctrl) return needsWalletLogin(ipc);
ctrl.show();
});
ipc.on('transactions:pending', function(data) {
location.href = '#/transactions';
var ctrl = controller('TransactionsController');
if (!ctrl) return needsWalletLogin(ipc);
ctrl.show(true);
});
ipc.on('backup:download', function(data) {
location.href = '#/backup';
var ctrl = controller('BackupController');
if (!ctrl) return needsWalletLogin(ipc);
ctrl.download();
});
ipc.on('backup:email', function(data) {
location.href = '#/backup';
var ctrl = controller('BackupController');
if (!ctrl) return needsWalletLogin(ipc);
ctrl.openModal();
});
ipc.on('backup:import:data', function(data) {
location.href = '#/import';
var ctrl = controller('ImportController');
if (!ctrl) return;
ctrl.backupText = data;
ctrl.openPasteArea();
ctrl.$apply();
});
ipc.on('backup:import', function(data) {
location.href = '#/import';
});
// let the shell know when an error occurs
window.onerror = function(err) {
ipc.send('error', err);
};
return ipc;
};
})();

View file

@ -1,79 +0,0 @@
var preconditions = require('preconditions').singleton();
module.exports = {
request: function(options, callback) {
preconditions.checkArgument(_.isObject(options));
options.method = options.method || 'GET';
options.headers = options.headers || {};
var ret = {
success: function(cb) {
this._success = cb;
return this;
},
error: function(cb) {
this._error = cb;
return this;
},
_success: function() {;
},
_error: function(_, err) {
console.trace(err);
throw err;
}
};
var method = (options.method || 'GET').toUpperCase();
var url = options.url;
var req = options;
req.headers = req.headers || {};
req.body = req.body || req.data || '';
var xhr = options.xhr || new XMLHttpRequest();
xhr.open(method, url, true);
Object.keys(req.headers).forEach(function(key) {
var val = req.headers[key];
if (key === 'Content-Length') return;
if (key === 'Content-Transfer-Encoding') return;
xhr.setRequestHeader(key, val);
});
if (req.responseType) {
xhr.responseType = req.responseType;
}
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, options);
};
xhr.onerror = function(event) {
var status;
if (xhr.status === 0 || !xhr.statusText) {
status = 'HTTP Request Error: This endpoint likely does not support cross-origin requests.';
} else {
status = xhr.statusText;
}
return ret._error(null, status, null, options);
};
if (req.body) {
xhr.send(req.body);
} else {
xhr.send(null);
}
return ret;
},
};

View file

@ -1,83 +0,0 @@
/**
* Small module for some helpers that wrap sjcl with some good practices.
*/
var sjcl = require('sjcl');
var _ = require('lodash');
var log = require('../util/log.js');
var config = require('../../config');
var defaultSalt = (config && config.passphraseConfig && config.passphraseConfig.storageSalt) || 'mjuBtGybi/4=';
var defaultIterations = (config && config.passphraseConfig && config.passphraseConfig.iterations) || 1000;
module.exports = {
/**
* @param {string} password
* @param {string} salt - base64 encoded, defaults to 'mjuBtGybi/4='
* @param {number} iterations - defaults to 100
* @param {number} length - bits, defaults to 512 bits
* @returns {string} base64 encoded pbkdf2 derivation using sha1 for hmac
*/
kdf: function(password, salt, iterations, length) {
return sjcl.codec.base64.fromBits(
this.kdfbinary(password, salt, iterations, length)
);
},
/**
* @param {string} key
* @param {string} data
* @return {string} base64 encoded hmac
*/
hmac: function(key, data) {
return sjcl.codec.base64.fromBits(
new sjcl.misc.hmac(key, sjcl.hash.sha256).encrypt(data)
);
},
/**
* @param {string} password
* @param {string} salt - base64 encoded, defaults to 'mjuBtGybi/4='
* @param {number} iterations - defaults to 100
* @param {number} length - bits, defaults to 512 bits
* @returns {string} base64 encoded pbkdf2 derivation using sha1 for hmac
*/
kdfbinary: function(password, salt, iterations, length) {
iterations = iterations || defaultIterations;
length = length || 512;
salt = sjcl.codec.base64.toBits(salt || defaultSalt);
var hash = sjcl.hash.sha256.hash(sjcl.hash.sha256.hash(password));
var prff = function(key) {
return new sjcl.misc.hmac(hash, sjcl.hash.sha1);
};
return sjcl.misc.pbkdf2(hash, salt, iterations, length, prff);
},
/**
* Encrypts symmetrically using a passphrase
*/
encrypt: function(key, message, salt, iter) {
if (!_.isString(message)) {
message = JSON.stringify(message);
}
sjcl.json.defaults.salt = salt || defaultSalt;
sjcl.json.defaults.iter = iter || defaultIterations;
return sjcl.encrypt(key, message);
},
/**
* Decrypts symmetrically using a passphrase
*/
decrypt: function(key, sjclEncryptedJson) {
var output = {};
try {
return sjcl.decrypt(key, sjclEncryptedJson);
} catch (e) {
log.debug('Decryption failed due to error: ' + e.message);
return null;
}
}
};

View file

@ -1,95 +0,0 @@
/**
* Small module for exporting data to CSV.
*/
var _ = require('lodash');
var preconditions = require('preconditions').singleton();
var moment = require('moment');
var logger = require('../util/log.js');
var config = require('../../config');
var COL_DELIMITER = ',';
var ROW_DELIMITER = '\r\n';
function getValue(obj, property) {
if (_.isFunction(property)) {
try {
return property(obj);
} catch (err) {
if (_.isString(err)) return err;
return undefined;
}
}
if (!_.isObject(obj)) return undefined;
return obj.hasOwnProperty(property) ? obj[property] : undefined;
};
function formatValue(value, type, format) {
if (_.isUndefined(value) || _.isNull(value)) return '';
var r;
switch (type) {
default:
case 'string':
r = value.toString();
r.replace('"', '\\"');
break;
case 'date':
r = moment(value).format(format);
break;
case 'number':
r = value.toString();
break;
}
// escape when commas in values
if (r.indexOf(',') !== -1) {
r = '"' + r + '"';
}
return r;
};
function getHeader(descriptor) {
return _.map(descriptor.columns, function (col) {
return col.label || (_.isString(col.property) ? col.property : '') || '';
});
};
function processDataRow(data, descriptor) {
return _.map(descriptor.columns, function (col) {
var value = getValue(data, col.property);
var formatted = formatValue(value, col.type, col.format);
return formatted;
});
};
/**
* @desc Convert json object to csv based on a descriptor
*
* @param {array} data - the array of json objects to convert to csv
* @param {object} descriptor - an object that parameterizes the conversion
* @param {function} cb - called with the resulting csv
*/
module.exports.toCsv = function(data, descriptor, cb) {
preconditions.shouldBeArray(data);
preconditions.shouldBeObject(descriptor);
preconditions.shouldBeArray(descriptor.columns);
preconditions.shouldBeFunction(cb);
var colDelimiter = descriptor.colDelimiter || COL_DELIMITER;
var rowDelimiter = descriptor.rowDelimiter || ROW_DELIMITER;
var rows = _.map(data, function (dataRow) {
return processDataRow(dataRow, descriptor);
});
var header = getHeader(descriptor);
rows.unshift(header);
var csv = _.reduce(rows, function (memo, row) {
return memo + row.join(colDelimiter) + rowDelimiter;
}, '');
return cb(null, csv);
};

View file

@ -1,150 +0,0 @@
var config = config || require('../../config');
var _ = require('lodash');
var ls;
try {
var LS = require('../js/plugins/LocalStorage');
ls = new LS();
} catch (e) {};
/**
* @desc
* A simple logger that wraps the <tt>console.log</tt> methods when available.
*
* Usage:
* <pre>
* log = new Logger('copay');
* log.setLevel('info');
* log.debug('Message!'); // won't show
* log.setLevel('debug');
* log.debug('Message!', 1); // will show '[debug] copay: Message!, 1'
* </pre>
*
* @param {string} name - a name for the logger. This will show up on every log call
* @constructor
*/
var Logger = function(name) {
this.name = name || 'log';
this.level = 2;
};
Logger.prototype.getLevels = function() {
return levels;
};
var levels = {
'debug': 0,
'info': 1,
'log': 2,
'warn': 3,
'error': 4,
'fatal': 5
};
_.each(levels, function(level, levelName) {
Logger.prototype[levelName] = function() {
if (level >= levels[this.level]) {
if (Error.stackTraceLimit && this.level == 'debug') {
var old = Error.stackTraceLimit;
Error.stackTraceLimit = 2;
var stack;
// this hack is to be compatible with IE11
try {
anerror();
} catch (e) {
stack = e.stack;
}
var lines = stack.split('\n');
var caller = lines[2];
caller = ':' + caller.substr(6);
Error.stackTraceLimit = old;
}
var str = '[' + levelName + (caller || '') + '] ' + arguments[0],
extraArgs,
extraArgs = [].slice.call(arguments, 1);
if (console[levelName]) {
extraArgs.unshift(str);
console[levelName].apply(console, extraArgs);
} else {
if (extraArgs.length) {
str += JSON.stringify(extraArgs);
}
console.log(str);
}
}
};
});
/**
* @desc
* Sets the level of a logger. A level can be any bewteen: 'debug', 'info', 'log',
* 'warn', 'error', and 'fatal'. That order matters: if a logger's level is set to
* 'warn', calling <tt>level.debug</tt> won't have any effect.
*
* @param {number} level - the name of the logging level
*/
Logger.prototype.setLevel = function(level) {
this.level = level;
};
/**
* @class Logger
* @method debug
* @desc Log messages at the debug level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method info
* @desc Log messages at the info level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method log
* @desc Log messages at an intermediary level called 'log'.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method warn
* @desc Log messages at the warn level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method error
* @desc Log messages at the error level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method fatal
* @desc Log messages at the fatal level.
* @param {*} args - the arguments to be logged.
*/
var logger = new Logger('copay');
var error = new Error();
var logLevel = config.logLevel || 'info';
if (ls && ls.getItem) {
ls.getItem("config", function(err, value) {
if (err) return;
var localConfig = JSON.parse(value);
if (localConfig && localConfig.logLevel)
logLevel = localConfig.logLevel;
logger.setLevel(logLevel);
});
} else {
logger.setLevel(logLevel);
}
module.exports = logger;