Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #124 from shocknet/bug/caching-encryption-errors
Browse files Browse the repository at this point in the history
Fixed encryption errors caused by caching
  • Loading branch information
shocknet-justin authored May 24, 2020
2 parents 8ea9456 + ff8a72b commit e1d3db2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
27 changes: 22 additions & 5 deletions app/screens/WalletOverview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1211,17 +1211,37 @@ class WalletOverview extends Component {
fetchRecentInvoices,
loadNewInvoice,
loadNewTransaction,
navigation,
} = this.props

this.didFocus = this.props.navigation.addListener('didFocus', () => {
this.didFocus = navigation.addListener('didFocus', () => {
StatusBar.setBackgroundColor(CSS.Colors.TRANSPARENT)
StatusBar.setBarStyle('light-content')
StatusBar.setTranslucent(true)

this.balanceIntervalID = setInterval(getWalletBalance, 4000)
this.exchangeRateIntervalID = setInterval(getUSDRate, 4000)
this.recentPaymentsIntervalID = setInterval(fetchRecentPayments, 4000)
})

navigation.addListener('didBlur', () => {
if (this.balanceIntervalID) {
clearInterval(this.balanceIntervalID)
}

if (this.exchangeRateIntervalID) {
clearInterval(this.exchangeRateIntervalID)
}

if (this.exchangeRateIntervalID) {
clearInterval(this.exchangeRateIntervalID)
}
})

//Check if this screen has a pending protocol link
//to be processed, This will happen if the app was in background
//on a screen different from WALLET_OVERVIEW
const { params } = this.props.navigation.state
const { params } = navigation.state

if (params && params.lnurl) {
const { lnurl } = params
Expand All @@ -1247,9 +1267,6 @@ class WalletOverview extends Component {
await SocketManager.connectSocket()
}

this.balanceIntervalID = setInterval(getWalletBalance, 4000)
this.exchangeRateIntervalID = setInterval(getUSDRate, 4000)
this.recentPaymentsIntervalID = setInterval(fetchRecentPayments, 4000)
subscribeOnChats()
await Promise.all([
fetchRecentInvoices(),
Expand Down
8 changes: 1 addition & 7 deletions app/services/encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,7 @@ export const decryptKey = async (encryptedKey, sessionId) => {
const decryptedKey = await RSAKeychain.decrypt(encryptedKey, keyTag)
return decryptedKey
} catch (err) {
Logger.log(
'[ENCRYPTION] Could not decrypt key:',
err,
encryptedKey,
'Session ID:',
sessionId,
)
Logger.log('[ENCRYPTION] Could not decrypt key:', err)
throw err
}
}
Expand Down
31 changes: 20 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export default class ShockWallet extends React.Component {
}
}

const cache = new Map()

// Adds an Authorization token to the header before sending any request
Http.interceptors.request.use(async config => {
try {
Expand Down Expand Up @@ -215,7 +217,7 @@ Http.interceptors.request.use(async config => {
if (cache.has(path)) {
const cachedData = cache.get(path)
// eslint-disable-next-line require-atomic-updates
config.headers.common.ETag = cachedData.hash
config.headers.common['shock-cache-hash'] = cachedData.hash
}

if (
Expand Down Expand Up @@ -271,8 +273,6 @@ Http.interceptors.request.use(async config => {
}
})

const cache = new Map()

/**
* @param {import('axios').AxiosResponse<any>} response
* @returns {Promise<import('axios').AxiosResponse<any>>}
Expand All @@ -284,14 +284,14 @@ const decryptResponse = async response => {
const path = url.parse(response?.config.url).pathname
Logger.log('[ENCRYPTION] Decrypting Path:', path)

if (response.status === 304) {
Logger.log('Using cached response for: ', path)
const cachedData = cache.get(path)
// if (response.status === 304) {
// Logger.log('Using cached response for: ', path)
// const cachedData = cache.get(path)

if (cachedData?.response) {
return { ...cachedData.response, status: 304 }
}
}
// if (cachedData?.response) {
// return { ...cachedData.response, status: 304 }
// }
// }

if (
connection.APIPublicKey &&
Expand Down Expand Up @@ -324,13 +324,20 @@ const decryptResponse = async response => {
path !== '/api/security/exchangeKeys' &&
response.headers['x-session-id']
) {
Logger.log('[HTTP] Exchanging Keys...')
Logger.log(
'[HTTP] Exchanging Keys...',
!!connection.APIPublicKey,
!!response.data.encryptedData,
!!connection.sessionId,
)
cache.clear()
await throttledExchangeKeyPair({
deviceId: connection.deviceId,
sessionId: response.headers['x-session-id'],
cachedSessionId: connection.sessionId,
baseURL: response.config.baseURL,
})(store.dispatch, store.getState, {})
cache.clear()
}

return response
Expand Down Expand Up @@ -397,12 +404,14 @@ Http.interceptors.response.use(
}

if (encryptionErrors.includes(decryptedResponse.data.field)) {
cache.clear()
await throttledExchangeKeyPair({
deviceId: connection.deviceId,
sessionId: decryptedResponse.headers['x-session-id'],
cachedSessionId: connection.sessionId,
baseURL: decryptedResponse.config.baseURL,
})(store.dispatch, store.getState, {})
cache.clear()
// eslint-disable-next-line require-atomic-updates
decryptedResponse.config.headers['x-shockwallet-device-id'] =
connection.deviceId
Expand Down

0 comments on commit e1d3db2

Please sign in to comment.