@@ -21,6 +21,9 @@ const isLegacyEncryptedValue = (value: string) =>
2121const isEncryptedValue = ( value : string ) =>
2222 isModernEncryptedValue ( value ) || isLegacyEncryptedValue ( value ) ;
2323
24+ const hasProtectedValue = ( value : unknown ) : boolean =>
25+ value !== undefined && value !== null && value !== '' ;
26+
2427const buildAesKey = ( secretKey : string ) : Buffer => {
2528 return crypto . createHash ( 'sha256' ) . update ( secretKey ) . digest ( ) ;
2629} ;
@@ -155,10 +158,14 @@ export const decryptValue = (
155158 secretKey : string ,
156159 context = defaultFieldContext ,
157160) : any => {
158- if ( typeof value !== 'string' || ! isEncryptedValue ( value ) ) {
161+ if ( ! hasProtectedValue ( value ) ) {
159162 return value ;
160163 }
161164
165+ if ( typeof value !== 'string' || ! isEncryptedValue ( value ) ) {
166+ throw new Error ( `Expected encrypted protected value at ${ context } ` ) ;
167+ }
168+
162169 if ( value . startsWith ( modernEncryptedPrefix ) ) {
163170 return decryptWithAesGcm ( value , secretKey , modernEncryptedPrefix , context ) ;
164171 }
@@ -224,7 +231,7 @@ const transformWalletStore = (
224231 state : any ,
225232 secretKey : string ,
226233 transformer : ( value : any , secretKey : string , context : string ) => any ,
227- checkCondition : ( value : string ) => boolean ,
234+ checkCondition : ( value : any ) => boolean ,
228235) : any => {
229236 if ( ! state || ! state . keys ) {
230237 return state ;
@@ -250,7 +257,7 @@ const transformWalletStore = (
250257 const updatedProperties = fieldsToTransform . reduce (
251258 ( latestProperties , field ) => {
252259 const value = properties [ field ] ;
253- if ( value && typeof value === 'string' && checkCondition ( value ) ) {
260+ if ( hasProtectedValue ( value ) && checkCondition ( value ) ) {
254261 latestProperties [ field ] = transformer (
255262 value ,
256263 secretKey ,
@@ -278,34 +285,32 @@ export const encryptWalletStore = (state: any, secretKey: string): any => {
278285 state ,
279286 secretKey ,
280287 encryptValue ,
281- value => ! isEncryptedValue ( value ) ,
288+ value => typeof value === 'string' && ! isEncryptedValue ( value ) ,
282289 ) ;
283290} ;
284291
285292export const decryptWalletStore = ( state : any , secretKey : string ) : any => {
286- return transformWalletStore ( state , secretKey , decryptValue , value =>
287- isEncryptedValue ( value ) ,
288- ) ;
293+ return transformWalletStore ( state , secretKey , decryptValue , ( ) => true ) ;
289294} ;
290295
291296// Generic function to transform app store (encrypt or decrypt)
292297const transformAppStore = (
293298 state : any ,
294299 secretKey : string ,
295300 transformer : ( value : any , secretKey : string , context : string ) => any ,
296- checkCondition : ( value : string ) => boolean ,
301+ checkCondition : ( value : any ) => boolean ,
297302) : any => {
298303 if ( ! state || ! state . identity ) {
299304 return state ;
300305 }
301306
302307 const identity = state . identity [ Network . mainnet ] ;
303- if ( ! identity || ! identity . priv ) {
308+ if ( ! identity ) {
304309 return state ;
305310 }
306311
307312 const privValue = identity . priv ;
308- if ( privValue && typeof privValue === 'string' && checkCondition ( privValue ) ) {
313+ if ( hasProtectedValue ( privValue ) && checkCondition ( privValue ) ) {
309314 return {
310315 ...state ,
311316 identity : {
@@ -329,22 +334,20 @@ export const encryptAppStore = (state: any, secretKey: string): any => {
329334 state ,
330335 secretKey ,
331336 encryptValue ,
332- value => ! isEncryptedValue ( value ) ,
337+ value => typeof value === 'string' && ! isEncryptedValue ( value ) ,
333338 ) ;
334339} ;
335340
336341export const decryptAppStore = ( state : any , secretKey : string ) : any => {
337- return transformAppStore ( state , secretKey , decryptValue , value =>
338- isEncryptedValue ( value ) ,
339- ) ;
342+ return transformAppStore ( state , secretKey , decryptValue , ( ) => true ) ;
340343} ;
341344
342345// Generic function to transform shop store (encrypt or decrypt)
343346const transformShopStore = (
344347 state : any ,
345348 secretKey : string ,
346349 transformer : ( value : any , secretKey : string , context : string ) => any ,
347- checkCondition : ( value : string ) => boolean ,
350+ checkCondition : ( value : any ) => boolean ,
348351) : any => {
349352 if ( ! state || ! state . giftCards || ! state . giftCards [ Network . mainnet ] ) {
350353 return state ;
@@ -369,7 +372,7 @@ const transformShopStore = (
369372 const updatedCard = { ...card } ;
370373 fieldsToTransform . forEach ( field => {
371374 const value = card [ field ] ;
372- if ( value && typeof value === 'string' && checkCondition ( value ) ) {
375+ if ( hasProtectedValue ( value ) && checkCondition ( value ) ) {
373376 updatedCard [ field ] = transformer (
374377 value ,
375378 secretKey ,
@@ -396,12 +399,10 @@ export const encryptShopStore = (state: any, secretKey: string): any => {
396399 state ,
397400 secretKey ,
398401 encryptValue ,
399- value => ! isEncryptedValue ( value ) ,
402+ value => typeof value === 'string' && ! isEncryptedValue ( value ) ,
400403 ) ;
401404} ;
402405
403406export const decryptShopStore = ( state : any , secretKey : string ) : any => {
404- return transformShopStore ( state , secretKey , decryptValue , value =>
405- isEncryptedValue ( value ) ,
406- ) ;
407+ return transformShopStore ( state , secretKey , decryptValue , ( ) => true ) ;
407408} ;
0 commit comments