Improved bitpay account pairing and management of paired state and data.
This commit is contained in:
parent
15d12823ab
commit
63bc3d8f63
23 changed files with 691 additions and 208 deletions
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('bitpayService', function($log, $http, platformInfo, appIdentityService, bitauthService, storageService, gettextCatalog, popupService, ongoingProcess) {
|
||||
angular.module('copayApp.services').factory('bitpayService', function($log, $http, appIdentityService, bitauthService) {
|
||||
var root = {};
|
||||
|
||||
var NETWORK = 'livenet';
|
||||
|
|
@ -12,99 +12,6 @@ angular.module('copayApp.services').factory('bitpayService', function($log, $htt
|
|||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* Pair this app with the bitpay server using the specified pairing data.
|
||||
* An app identity will be created if one does not already exist.
|
||||
* Pairing data is provided by an input URI provided by the bitpay server.
|
||||
*
|
||||
* pairData - data needed to complete the pairing process
|
||||
* {
|
||||
* secret: shared pairing secret
|
||||
* email: email address associated with bitpay account
|
||||
* otp: two-factor one-time use password
|
||||
* }
|
||||
*
|
||||
* pairingReason - text string to be embedded into popup message. If `null` then the reason
|
||||
* message is not shown to the UI.
|
||||
* "To {{reason}} you must pair this app with your BitPay account ({{email}})."
|
||||
*
|
||||
* cb - callback after completion
|
||||
* callback(err, paired, apiContext)
|
||||
*
|
||||
* err - something unexpected happened which prevented the pairing
|
||||
*
|
||||
* paired - boolean indicating whether the pairing was compledted by the user
|
||||
*
|
||||
* apiContext - the context needed for making future api calls
|
||||
* {
|
||||
* token: api token for use in future calls
|
||||
* pairData: the input pair data
|
||||
* appIdentity: the identity of this app
|
||||
* }
|
||||
*/
|
||||
root.pair = function(pairData, pairingReason, cb) {
|
||||
checkOtp(pairData, function(otp) {
|
||||
pairData.otp = otp;
|
||||
var deviceName = 'Unknown device';
|
||||
if (platformInfo.isNW) {
|
||||
deviceName = require('os').platform();
|
||||
} else if (platformInfo.isCordova) {
|
||||
deviceName = device.model;
|
||||
}
|
||||
var json = {
|
||||
method: 'createToken',
|
||||
params: {
|
||||
secret: pairData.secret,
|
||||
version: 2,
|
||||
deviceName: deviceName,
|
||||
code: pairData.otp
|
||||
}
|
||||
};
|
||||
appIdentityService.getIdentity(root.getEnvironment().network, function(err, appIdentity) {
|
||||
if (err) return cb(err);
|
||||
ongoingProcess.set('fetchingBitPayAccount', true);
|
||||
$http(_postAuth('/api/v2/', json, appIdentity)).then(function(data) {
|
||||
ongoingProcess.set('fetchingBitPayAccount', false);
|
||||
|
||||
if (data && data.data.error) return cb(data.data.error);
|
||||
$log.info('BitPay service BitAuth create token: SUCCESS');
|
||||
var title = gettextCatalog.getString('Link BitPay Account?');
|
||||
var msgDetail = 'Link BitPay account ({{email}})?';
|
||||
if (pairingReason) {
|
||||
msgDetail = 'To add your {{reason}} please link your BitPay account {{email}}';
|
||||
}
|
||||
var msg = gettextCatalog.getString(msgDetail, {
|
||||
reason: pairingReason,
|
||||
email: pairData.email
|
||||
});
|
||||
var ok = gettextCatalog.getString('Confirm');
|
||||
var cancel = gettextCatalog.getString('Cancel');
|
||||
popupService.showConfirm(title, msg, ok, cancel, function(res) {
|
||||
if (res) {
|
||||
var acctData = {
|
||||
token: data.data.data,
|
||||
email: pairData.email
|
||||
};
|
||||
setBitpayAccount(acctData, function(err) {
|
||||
if (err) return cb(err);
|
||||
return cb(null, true, {
|
||||
token: acctData.token,
|
||||
pairData: pairData,
|
||||
appIdentity: appIdentity
|
||||
});
|
||||
});
|
||||
} else {
|
||||
$log.info('User cancelled BitPay pairing process');
|
||||
return cb(null, false);
|
||||
}
|
||||
});
|
||||
}, function(data) {
|
||||
return cb(_setError('BitPay service BitAuth create token: ERROR ', data));
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.get = function(endpoint, successCallback, errorCallback) {
|
||||
$http(_get(endpoint)).then(function(data) {
|
||||
successCallback(data);
|
||||
|
|
@ -115,7 +22,9 @@ angular.module('copayApp.services').factory('bitpayService', function($log, $htt
|
|||
|
||||
root.post = function(endpoint, json, successCallback, errorCallback) {
|
||||
appIdentityService.getIdentity(root.getEnvironment().network, function(err, appIdentity) {
|
||||
if (err) return errorCallback(err);
|
||||
if (err) {
|
||||
return errorCallback(err);
|
||||
}
|
||||
$http(_post(endpoint, json, appIdentity)).then(function(data) {
|
||||
successCallback(data);
|
||||
}, function(data) {
|
||||
|
|
@ -124,22 +33,20 @@ angular.module('copayApp.services').factory('bitpayService', function($log, $htt
|
|||
});
|
||||
};
|
||||
|
||||
var checkOtp = function(pairData, cb) {
|
||||
if (pairData.otp) {
|
||||
var msg = gettextCatalog.getString('Enter Two Factor for your BitPay account');
|
||||
popupService.showPrompt(null, msg, null, function(res) {
|
||||
cb(res);
|
||||
root.postAuth = function(json, successCallback, errorCallback) {
|
||||
appIdentityService.getIdentity(root.getEnvironment().network, function(err, appIdentity) {
|
||||
if (err) {
|
||||
return errorCallback(err);
|
||||
}
|
||||
$http(_postAuth('/api/v2/', json, appIdentity)).then(function(data) {
|
||||
data.appIdentity = appIdentity;
|
||||
successCallback(data);
|
||||
}, function(data) {
|
||||
errorCallback(data);
|
||||
});
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var setBitpayAccount = function(accountData, cb) {
|
||||
storageService.setBitpayAccount(root.getEnvironment().network, accountData, cb);
|
||||
};
|
||||
|
||||
|
||||
var _get = function(endpoint) {
|
||||
return {
|
||||
method: 'GET',
|
||||
|
|
@ -184,12 +91,6 @@ angular.module('copayApp.services').factory('bitpayService', function($log, $htt
|
|||
return ret;
|
||||
};
|
||||
|
||||
var _setError = function(msg, e) {
|
||||
$log.error(msg);
|
||||
var error = (e && e.data && e.data.error) ? e.data.error : msg;
|
||||
return error;
|
||||
};
|
||||
|
||||
return root;
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue