Skip to content

Commit

Permalink
extra debug logging and handle session token failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore3 committed May 31, 2017
1 parent b8a4bcd commit 3fc9828
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/error-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ module.exports = {
ERR_IS_HD: 'Current wallet is already an HD wallet. To see your accounts, call `/merchant/:guid/accounts`',
ERR_ACCT_IDX: 'Account nonexistent, check that your account xpub or index is correct',
ERR_SYNC: 'Attempt to save wallet to server failed',
ERR_SESSION: 'Unable to establish session',
ERR_AUTH: 'Wallets that require email authorization are currently not supported in the Wallet API. Please disable this in your wallet settings, or add the IP address of this server to your wallet IP whitelist.'
}
40 changes: 33 additions & 7 deletions src/wallet-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,48 @@ WalletCache.prototype.login = function (guid, options) {
var done = clearTimeout.bind(null, timeout)
var remove = function () { this.instanceStore[guid] = undefined }.bind(this)

var handleMainPwError = function (error) {
var message = error.indexOf('Error decrypting wallet') > -1 ? 'ERR_PASSWORD' : error
return q.reject(message)
}
var handleLoginError = function (error) {
if (error.indexOf('Error decrypting wallet') > -1) {
return q.reject('ERR_PASSWORD')
}

if (error.indexOf('Unable to establish session') > -1 && !options.sessionToken) {
winston.debug('Failed to establish session, retrying...')
return instance.WalletNetwork.establishSession().then(function (token) {
winston.debug('Established session, retrying login...')
return this.login(guid, Object.assign({}, options, { sessionToken: token }))
}.bind(this), function (error) {
winston.debug('Failed to establish session, reason: ' + error)
return q.reject('ERR_SESSION')
})
}

return q.reject(error)
}.bind(this)

instance.API.API_CODE = options.api_code
instance.WalletStore.isLogoutDisabled = function () { return true }
overrides.handleSocketErrors(instance.MyWallet.ws)
overrides.substituteWithCryptoRNG(instance.RNG)

var callbacks = { authorizationRequired: needsAuth, needsTwoFactorCode: needs2FA }
var loginP = instance.MyWallet.login(guid, options.password, { twoFactor: null }, callbacks)
var callbacks = {
authorizationRequired: needsAuth,
needsTwoFactorCode: needs2FA,
newSessionToken: function () { winston.debug('Created new session token') },
didFetch: function () { winston.debug('Fetched wallet') },
didDecrypt: function () { winston.debug('Decrypted wallet') }
}

var credentials = {
twoFactor: null,
sessionToken: options.sessionToken
}

var loginP = instance.MyWallet.login(guid, options.password, credentials, callbacks)

var startupPromise = q.race([
deferred.promise,
loginP.then(function () { return instance }).catch(handleMainPwError)
loginP.then(function () { return instance }).catch(handleLoginError)
])

this.instanceStore[guid] = startupPromise
Expand Down

0 comments on commit 3fc9828

Please sign in to comment.