New pairing request

This commit is contained in:
Gustavo Maximiliano Cortez 2016-10-11 14:50:35 -03:00
commit d4ace8ab2c
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
4 changed files with 130 additions and 179 deletions

View file

@ -4,9 +4,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
var self = this; var self = this;
$scope.dateRange = 'last30Days'; $scope.dateRange = 'last30Days';
bitpayCardService.getEnvironment(function(err, network) { $scope.network = bitpayCardService.getEnvironment();
$scope.network = network;
});
var getFromCache = function(cb) { var getFromCache = function(cb) {
bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) { bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) {
@ -49,8 +47,8 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.loadingHistory = true; $scope.loadingHistory = true;
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) { bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
$scope.loadingHistory = false; $scope.loadingHistory = false;
if (err || history.error) { if (err) {
$log.error(err || history.error); $log.error(err);
$scope.error = gettextCatalog.getString('Could not get transactions'); $scope.error = gettextCatalog.getString('Could not get transactions');
return; return;
} }

View file

@ -42,31 +42,22 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
disableAnimate: true disableAnimate: true
}); });
$state.go('tabs.home'); $state.go('tabs.home');
if (data.cards[0]) {
$timeout(function() {
$state.transitionTo('tabs.bitpayCard', {id: data.cards[0].id});
}, 100);
}
}); });
} }
}); });
}); });
}); });
} else { } else {
// TEST TODO bitpayCardService.getCredentials(function(err, credentials) {
bitpayCardService.testSession(function(err, session) {
if (err) popupService.showAlert(null, err); if (err) popupService.showAlert(null, err);
else $log.info('BitPay Debit Card Credentials: Ok.');
}); });
} }
/*
storageService.getNextStep('BitpayCard', function(err, value) {
if (value) {
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home');
$timeout(function() {
$state.transitionTo('tabs.bitpayCard');
}, 100);
}
});
*/
}); });
$scope.orderBitPayCard = function() { $scope.orderBitPayCard = function() {

View file

@ -2,41 +2,32 @@
angular.module('copayApp.services').factory('bitpayCardService', function($http, $log, $window, lodash, storageService, bitauthService, platformInfo) { angular.module('copayApp.services').factory('bitpayCardService', function($http, $log, $window, lodash, storageService, bitauthService, platformInfo) {
var root = {}; var root = {};
var credentials = {}; var BITPAY_CARD_NETWORK = 'livenet';
var bpSession = {}; var BITPAY_CARD_API_URL = BITPAY_CARD_NETWORK == 'livenet' ? 'https://bitpay.com' : 'https://test.bitpay.com';
var pubkey, sin;
var _setCredentials = function(cb) { var _getCredentials = function(cb) {
/* var pubkey, sin, isNew;
* Development: 'testnet' storageService.getBitpayCardCredentials(BITPAY_CARD_NETWORK, function(err, data) {
* Production: 'livenet'
*/
credentials.NETWORK = 'livenet';
if (credentials.NETWORK == 'testnet') {
credentials.BITPAY_API_URL = 'https://test.bitpay.com';
}
else {
credentials.BITPAY_API_URL = 'https://bitpay.com';
}
storageService.getBitpayDebitCardSin(credentials.NETWORK, function(err, data) {
if (err) return cb(err); if (err) return cb(err);
if (lodash.isString(data)) { if (lodash.isString(data)) {
data = JSON.parse(data); data = JSON.parse(data);
} }
data = data || {}; var credentials = data || {};
if (lodash.isEmpty(data) || (data && !data.priv)) { if (lodash.isEmpty(credentials) || (credentials && !credentials.priv)) {
data = bitauthService.generateSin(); isNew = true;
credentials = bitauthService.generateSin();
} }
try { try {
credentials.BITPAY_PRIV_KEY = data.priv pubkey = bitauthService.getPublicKeyFromPrivateKey(credentials.priv);
pubkey = bitauthService.getPublicKeyFromPrivateKey(credentials.BITPAY_PRIV_KEY);
sin = bitauthService.getSinFromPublicKey(pubkey); sin = bitauthService.getSinFromPublicKey(pubkey);
storageService.setBitpayDebitCardSin(credentials.NETWORK, JSON.stringify(data), function(err) {}); if (isNew)
storageService.setBitpayCardCredentials(BITPAY_CARD_NETWORK, JSON.stringify(credentials), function(err) {});
} }
catch (e) { catch (e) {
$log.error(e); $log.error(e);
return cb(e); return cb(e);
}; };
return cb(null, credentials);
}); });
}; };
@ -46,74 +37,48 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
return error; return error;
}; };
var _getSession = function(cb) { var _get = function(endpoint) {
_setCredentials(cb); return {
$http({
method: 'GET', method: 'GET',
url: credentials.BITPAY_API_URL + '/api/session', url: BITPAY_CARD_API_URL + endpoint,
headers: { headers: {
'content-type': 'application/json' 'content-type': 'application/json'
} }
}).then(function(data) {
$log.info('BitPay Get Session: SUCCESS');
return cb(data.data.error, data.data.data);
}, function(data) {
return cb(_setError('BitPay Card Error: Get Session', data));
});
};
var _getBitPay = function(endpoint) {
return {
method: 'GET',
url: credentials.BITPAY_API_URL + endpoint,
headers: {
'content-type': 'application/json',
'x-csrf-token': bpSession.csrfToken
}
}; };
}; };
root.getEnvironment = function(cb) {
_setCredentials(cb);
return cb(null, credentials.NETWORK);
};
root.testSession = function(cb) { var _post = function(endpoint, json, credentials) {
_getSession(cb); var dataToSign = BITPAY_CARD_API_URL + endpoint + JSON.stringify(json);
}; var signedData = bitauthService.sign(dataToSign, credentials.priv);
var _postBitAuth = function(endpoint, data) {
var dataToSign = credentials.BITPAY_API_URL + endpoint + JSON.stringify(data);
var signedData = bitauthService.sign(dataToSign, credentials.BITPAY_PRIV_KEY);
return { return {
method: 'POST', method: 'POST',
url: credentials.BITPAY_API_URL + endpoint, url: BITPAY_CARD_API_URL + endpoint,
headers: { headers: {
'content-type': 'application/json', 'content-type': 'application/json',
'x-identity': pubkey, 'x-identity': credentials.pub,
'x-signature': signedData 'x-signature': signedData
}, },
data: data data: json
}; };
}; };
var _afterBitAuthSuccess = function(obj, cb) { var _afterBitAuthSuccess = function(obj, credentials, cb) {
var data = { var json = {
method: 'getTokens' method: 'getTokens'
}; };
// Get tokens // Get tokens
$http(_postBitAuth('/api/v2/', data)).then(function(data) { $http(_post('/api/v2/', json, credentials)).then(function(data) {
$log.info('BitPay Get Tokens: SUCCESS'); $log.info('BitPay Get Tokens: SUCCESS');
var token = lodash.find(data.data.data, 'visaUser'); var token = lodash.find(data.data.data, 'visaUser');
if (lodash.isEmpty(token)) return cb(_setError('No token for visaUser')); if (lodash.isEmpty(token)) return cb(_setError('No token for visaUser'));
token = token.visaUser; token = token.visaUser;
data['method'] = 'getDebitCards'; json['method'] = 'getDebitCards';
// Get Debit Cards // Get Debit Cards
$http(_postBitAuth('/api/v2/' + token, data)).then(function(data) { $http(_post('/api/v2/' + token, json, credentials)).then(function(data) {
$log.info('BitPay Get Debit Cards: SUCCESS'); $log.info('BitPay Get Debit Cards: SUCCESS');
var cards = data.data.data; return cb(data.data.error, {token: token, cards: data.data.data, email: obj.email});
return cb(null, {token: token, cards: cards, email: obj.email});
}, function(data) { }, function(data) {
return cb(_setError('BitPay Card Error: Get Debit Cards', data)); return cb(_setError('BitPay Card Error: Get Debit Cards', data));
}); });
@ -122,46 +87,6 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
}); });
}; };
root.bitAuthPair = function(obj, cb) {
_getSession(function(err, session) {
if (err) return cb(err);
var deviceName = 'Unknow device';
if (platformInfo.isNW) {
deviceName = require('os').platform();
} else if (platformInfo.isCordova) {
deviceName = device.model;
}
var userData = {
csrf: session.csrfToken,
secret: obj.secret,
deviceName: deviceName,
code: obj.otp
};
var dataToSign = credentials.BITPAY_API_URL + '/api/validateBitAuthPairingCode' + JSON.stringify(userData);
var signedData = bitauthService.sign(dataToSign, credentials.BITPAY_PRIV_KEY);
$http({
method: 'POST',
url: credentials.BITPAY_API_URL + '/api/validateBitAuthPairingCode',
headers: {
'content-type': 'application/json',
'x-csrf-token': session.csrfToken,
'x-identity': pubkey,
'x-signature': signedData
},
data: userData
}).then(function(data) {
$log.info('BitPay Card BitAuth: SUCCESS');
// Get cards
_afterBitAuthSuccess(obj, cb);
}, function(data) {
return cb(_setError('BitPay Card Error: BitAuth', data));
});
});
};
var _processTransactions = function(invoices, history) { var _processTransactions = function(invoices, history) {
invoices = invoices || []; invoices = invoices || [];
for (var i = 0; i < invoices.length; i++) { for (var i = 0; i < invoices.length; i++) {
@ -186,72 +111,113 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
return history; return history;
}; };
root.getEnvironment = function() {
return BITPAY_CARD_NETWORK;
};
root.getCredentials = function(cb) {
_getCredentials(function(err, credentials) {
return cb(err, credentials);
});
};
root.bitAuthPair = function(obj, cb) {
var deviceName = 'Unknow device';
if (platformInfo.isNW) {
deviceName = require('os').platform();
} else if (platformInfo.isCordova) {
deviceName = device.model;
}
var json = {
method: 'registerSinWithSecret',
params: {
secret: obj.secret,
deviceName: deviceName,
code: obj.otp
}
};
_getCredentials(function(err, credentials) {
if (err) return cb(err);
$http(_post('/api/v2/', json, credentials)).then(function(data) {
$log.info('BitPay Card BitAuth: SUCCESS');
_afterBitAuthSuccess(obj, credentials, cb);
}, function(data) {
return cb(_setError('BitPay Card Error: BitAuth', data));
});
});
};
root.getHistory = function(cardId, params, cb) { root.getHistory = function(cardId, params, cb) {
var invoices, transactions;
params = params || {}; params = params || {};
var json = {}; var json = {
json = {
method: 'getInvoiceHistory', method: 'getInvoiceHistory',
params: JSON.stringify(params) params: JSON.stringify(params)
}; };
_setCredentials(cb); _getCredentials(function(err, credentials) {
root.getBitpayDebitCards(function(err, data) { if (err) return cb(err);
var card = lodash.find(data.cards, {id : cardId}); root.getBitpayDebitCards(function(err, data) {
if (!card) return cb(_setError('No card available')); if (err) return cb(err);
// Get invoices var card = lodash.find(data.cards, {id : cardId});
$http(_postBitAuth('/api/v2/' + card.token, json)).then(function(data) { if (!card) return cb(_setError('Not card found'));
$log.info('BitPay Get Invoices: SUCCESS'); // Get invoices
var invoices = data.data.data; $http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) {
json = { $log.info('BitPay Get Invoices: SUCCESS');
method: 'getTransactionHistory', invoices = data.data.data;
params: JSON.stringify(params) if (lodash.isEmpty(invoices)) $log.info('No invoices');
}; json = {
// Get transactions list method: 'getTransactionHistory',
$http(_postBitAuth('/api/v2/' + card.token, json)).then(function(data) { params: JSON.stringify(params)
$log.info('BitPay Get Transactions: SUCCESS'); };
var history = data.data.data || data.data; // Get transactions list
history['txs'] = _processTransactions(invoices, history.transactionList); $http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) {
return cb(null, history); $log.info('BitPay Get Transactions: SUCCESS');
transactions = data.data.data || {};
transactions['txs'] = _processTransactions(invoices, transactions.transactionList);
return cb(data.data.error, transactions);
}, function(data) {
return cb(_setError('BitPay Card Error: Get Transactions', data));
});
}, function(data) { }, function(data) {
return cb(_setError('BitPay Card Error: Get Transactions', data)); return cb(_setError('BitPay Card Error: Get Invoices', data));
}); });
}, function(data) {
return cb(_setError('BitPay Card Error: Get Invoices', data));
}); });
}); });
}; };
root.topUp = function(cardId, params, cb) { root.topUp = function(cardId, params, cb) {
params = params || {};
var json = { var json = {
method: 'generateTopUpInvoice', method: 'generateTopUpInvoice',
params: JSON.stringify(params) params: JSON.stringify(params)
}; };
_setCredentials(cb); _getCredentials(function(err, credentials) {
root.getBitpayDebitCards(function(err, data) { if (err) return cb(err);
var card = lodash.find(data.cards, {id : cardId}); root.getBitpayDebitCards(function(err, data) {
if (!card) return cb(_setError('No card available')); if (err) return cb(err);
$http(_postBitAuth('/api/v2/' + card.token, json)).then(function(data) { var card = lodash.find(data.cards, {id : cardId});
$log.info('BitPay TopUp: SUCCESS'); if (!card) return cb(_setError('Not card found'));
var invoiceId = data.data.data.invoice; $http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) {
return cb(null, invoiceId); $log.info('BitPay TopUp: SUCCESS');
}, function(data) { return cb(data.data.error, data.data.data.invoice);
return cb(_setError('BitPay Card Error: TopUp', data)); }, function(data) {
return cb(_setError('BitPay Card Error: TopUp', data));
});
}); });
}); });
}; };
root.getInvoice = function(id, cb) { root.getInvoice = function(id, cb) {
_setCredentials(cb); $http(_get('/invoices/' + id)).then(function(data) {
$http(_getBitPay('/invoices/' + id)).then(function(data) {
$log.info('BitPay Get Invoice: SUCCESS'); $log.info('BitPay Get Invoice: SUCCESS');
return cb(null, data.data.data); return cb(data.data.error, data.data.data);
}, function(data) { }, function(data) {
return cb(_setError('BitPay Card Error: Get Invoice', data)); return cb(_setError('BitPay Card Error: Get Invoice', data));
}); });
}; };
root.getBitpayDebitCards = function(cb) { root.getBitpayDebitCards = function(cb) {
_setCredentials(cb); storageService.getBitpayDebitCards(BITPAY_CARD_NETWORK, function(err, data) {
storageService.getBitpayDebitCards(credentials.NETWORK, function(err, data) {
if (err) return cb(err); if (err) return cb(err);
if (lodash.isString(data)) { if (lodash.isString(data)) {
data = JSON.parse(data); data = JSON.parse(data);
@ -262,17 +228,15 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
}; };
root.setBitpayDebitCards = function(data, cb) { root.setBitpayDebitCards = function(data, cb) {
_setCredentials(cb);
data = JSON.stringify(data); data = JSON.stringify(data);
storageService.setBitpayDebitCards(credentials.NETWORK, data, function(err) { storageService.setBitpayDebitCards(BITPAY_CARD_NETWORK, data, function(err) {
if (err) return cb(err); if (err) return cb(err);
return cb(); return cb();
}); });
}; };
root.getBitpayDebitCardsHistory = function(cardId, cb) { root.getBitpayDebitCardsHistory = function(cardId, cb) {
_setCredentials(cb); storageService.getBitpayDebitCardsHistory(BITPAY_CARD_NETWORK, function(err, data) {
storageService.getBitpayDebitCardsHistory(credentials.NETWORK, function(err, data) {
if (err) return cb(err); if (err) return cb(err);
if (lodash.isString(data)) { if (lodash.isString(data)) {
data = JSON.parse(data); data = JSON.parse(data);
@ -284,8 +248,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
}; };
root.setBitpayDebitCardsHistory = function(cardId, data, opts, cb) { root.setBitpayDebitCardsHistory = function(cardId, data, opts, cb) {
_setCredentials(cb); storageService.getBitpayDebitCardsHistory(BITPAY_CARD_NETWORK, function(err, oldData) {
storageService.getBitpayDebitCardsHistory(credentials.NETWORK, function(err, oldData) {
if (lodash.isString(oldData)) { if (lodash.isString(oldData)) {
oldData = JSON.parse(oldData); oldData = JSON.parse(oldData);
} }
@ -299,17 +262,16 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
} }
inv = JSON.stringify(inv); inv = JSON.stringify(inv);
storageService.setBitpayDebitCardsHistory(credentials.NETWORK, inv, function(err) { storageService.setBitpayDebitCardsHistory(BITPAY_CARD_NETWORK, inv, function(err) {
return cb(err); return cb(err);
}); });
}); });
}; };
root.remove = function(cb) { root.remove = function(cb) {
_setCredentials(cb); storageService.removeBitpayCardCredentials(BITPAY_CARD_NETWORK, function(err) {
storageService.removeBitpayDebitCardSin(credentials.NETWORK, function(err) { storageService.removeBitpayDebitCards(BITPAY_CARD_NETWORK, function(err) {
storageService.removeBitpayDebitCards(credentials.NETWORK, function(err) { storageService.removeBitpayDebitCardsHistory(BITPAY_CARD_NETWORK, function(err) {
storageService.removeBitpayDebitCardsHistory(credentials.NETWORK, function(err) {
$log.info('BitPay Debit Cards Removed: SUCCESS'); $log.info('BitPay Debit Cards Removed: SUCCESS');
return cb(); return cb();
}); });

View file

@ -349,16 +349,16 @@ angular.module('copayApp.services')
storage.remove('bitpayDebitCards-' + network, cb); storage.remove('bitpayDebitCards-' + network, cb);
}; };
root.setBitpayDebitCardSin = function(network, data, cb) { root.setBitpayCardCredentials = function(network, data, cb) {
storage.set('bitpayDebitCardSin-' + network, data, cb); storage.set('bitpayCardCredentials-' + network, data, cb);
}; };
root.getBitpayDebitCardSin = function(network, cb) { root.getBitpayCardCredentials = function(network, cb) {
storage.get('bitpayDebitCardSin-' + network, cb); storage.get('bitpayCardCredentials-' + network, cb);
}; };
root.removeBitpayDebitCardSin = function(network, cb) { root.removeBitpayCardCredentials = function(network, cb) {
storage.remove('bitpayDebitCardSin-' + network, cb); storage.remove('bitpayCardCredentials-' + network, cb);
}; };
root.removeAllWalletData = function(walletId, cb) { root.removeAllWalletData = function(walletId, cb) {