New pairing request

This commit is contained in:
Gustavo Maximiliano Cortez 2016-10-11 14:50:35 -03:00
parent 756e1aa489
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;
$scope.dateRange = 'last30Days';
bitpayCardService.getEnvironment(function(err, network) {
$scope.network = network;
});
$scope.network = bitpayCardService.getEnvironment();
var getFromCache = function(cb) {
bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) {
@ -49,8 +47,8 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.loadingHistory = true;
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
$scope.loadingHistory = false;
if (err || history.error) {
$log.error(err || history.error);
if (err) {
$log.error(err);
$scope.error = gettextCatalog.getString('Could not get transactions');
return;
}

View File

@ -42,31 +42,22 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
disableAnimate: true
});
$state.go('tabs.home');
if (data.cards[0]) {
$timeout(function() {
$state.transitionTo('tabs.bitpayCard', {id: data.cards[0].id});
}, 100);
}
});
}
});
});
});
} else {
// TEST TODO
bitpayCardService.testSession(function(err, session) {
bitpayCardService.getCredentials(function(err, credentials) {
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() {

View File

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

View File

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