Skip to content

Commit

Permalink
feat: use storage wrapper (#2137)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Leach <[email protected]>
  • Loading branch information
jleach authored Sep 27, 2024
1 parent bcd763f commit 7b8a677
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 71 deletions.
11 changes: 7 additions & 4 deletions app/container-imp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import {
Onboarding,
PINRules,
testIdWithKey,
PersistentStorage,
PersistentState,
} from '@hyperledger/aries-bifold-core'
import { RemoteLogger, RemoteLoggerOptions } from '@hyperledger/aries-bifold-remote-logs'
import { getProofRequestTemplates } from '@hyperledger/aries-bifold-verifier'
import { BrandingOverlayType, RemoteOCABundleResolver } from '@hyperledger/aries-oca/build/legacy'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { NavigationProp } from '@react-navigation/native'
import { TFunction } from 'react-i18next'
import { Linking } from 'react-native'
Expand Down Expand Up @@ -78,6 +79,7 @@ export class AppContainer implements Container {
private navigate: (stack: never, params: never) => void
private setAppState: React.Dispatch<React.SetStateAction<AppState>>
private appState: AppState
private storage: PersistentStorage<PersistentState>

public constructor(
bifoldContainer: Container,
Expand All @@ -93,6 +95,7 @@ export class AppContainer implements Container {
const [appState, setAppState] = useState
this.setAppState = setAppState
this.appState = appState
this.storage = new PersistentStorage(log)
}

public get container(): DependencyContainer {
Expand Down Expand Up @@ -294,6 +297,7 @@ export class AppContainer implements Container {

const resolver = new RemoteOCABundleResolver(Config.OCA_URL ?? '', {
brandingOverlayType: BrandingOverlayType.Branding10,
verifyCacheIntegrity: true,
})
resolver.log = logger
this._container.registerInstance(TOKENS.UTIL_OCA_RESOLVER, resolver)
Expand Down Expand Up @@ -327,10 +331,9 @@ export class AppContainer implements Container {
this._container.registerInstance(TOKENS.UTIL_PROOF_TEMPLATE, getProofRequestTemplates)
this._container.registerInstance(TOKENS.LOAD_STATE, async (dispatch: React.Dispatch<ReducerAction<unknown>>) => {
const loadState = async <Type>(key: LocalStorageKeys | BCLocalStorageKeys, updateVal: (val: Type) => void) => {
const data = await AsyncStorage.getItem(key)
const data = (await this.storage.getValueForKey(key)) as Type
if (data) {
const dataAsJSON = JSON.parse(data) as Type
updateVal(dataAsJSON)
updateVal(data)
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ PODS:
- React-jsinspector (0.72.5)
- React-logger (0.72.5):
- glog
- "react-native-attestation (1.0.0-alpha.319+36d26768)":
- "react-native-attestation (1.0.0-alpha.330+a657da40)":
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-config (1.5.0):
Expand Down Expand Up @@ -931,7 +931,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: ff70a72027dea5cc7d71cfcc6fad7f599f63987a
React-jsinspector: aef73cbd43b70675f572214d10fa438c89bf11ba
React-logger: 2e4aee3e11b3ec4fa6cfd8004610bbb3b8d6cca4
react-native-attestation: f8a438d7f607001e3d2ebf39f33eb801c49e014d
react-native-attestation: 1d88e562ea25bffab15e9950c113e83a4490918c
react-native-config: 5330c8258265c1e5fdb8c009d2cabd6badd96727
react-native-encrypted-storage: db300a3f2f0aba1e818417c1c0a6be549038deb7
react-native-get-random-values: a6ea6a8a65dc93e96e24a11105b1a9c8cfe1d72a
Expand Down
10 changes: 5 additions & 5 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@
"@formatjs/intl-relativetimeformat": "9.3.1",
"@hyperledger/anoncreds-react-native": "0.2.2",
"@hyperledger/aries-askar-react-native": "0.2.3",
"@hyperledger/aries-bifold-core": "1.0.0-alpha.319",
"@hyperledger/aries-bifold-remote-logs": "1.0.0-alpha.319",
"@hyperledger/aries-bifold-verifier": "1.0.0-alpha.319",
"@hyperledger/aries-oca": "1.0.0-alpha.319",
"@hyperledger/aries-react-native-attestation": "1.0.0-alpha.319",
"@hyperledger/aries-bifold-core": "1.0.0-alpha.330",
"@hyperledger/aries-bifold-remote-logs": "1.0.0-alpha.330",
"@hyperledger/aries-bifold-verifier": "1.0.0-alpha.330",
"@hyperledger/aries-oca": "1.0.0-alpha.330",
"@hyperledger/aries-react-native-attestation": "1.0.0-alpha.330",
"@hyperledger/indy-vdr-react-native": "0.2.2",
"@hyperledger/indy-vdr-shared": "0.2.2",
"@react-native-async-storage/async-storage": "1.15.11",
Expand Down
18 changes: 11 additions & 7 deletions app/src/helpers/PushNotificationsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Agent, ConnectionRecord, ConnectionType } from '@credo-ts/core'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { PersistentStorage } from '@hyperledger/aries-bifold-core'
import messaging, { FirebaseMessagingTypes } from '@react-native-firebase/messaging'
import { Platform } from 'react-native'
import { Config } from 'react-native-config'
import { request, check, PERMISSIONS, RESULTS, PermissionStatus } from 'react-native-permissions'

const TOKEN_STORAGE_KEY = 'deviceToken'
import { BCLocalStorageKeys } from '../store'

const enum NotificationPermissionStatus {
DENIED = 'denied',
Expand Down Expand Up @@ -117,7 +117,7 @@ const getMediatorConnection = async (agent: Agent): Promise<ConnectionRecord | u
* @returns {Promise<boolean>}
*/
const isUserDenied = async (): Promise<boolean> => {
return (await AsyncStorage.getItem('userDeniedPushNotifications')) === 'true'
return (await PersistentStorage.fetchValueForKey<boolean>(BCLocalStorageKeys.UserDeniedPushNotifications)) ?? false
}

/**
Expand Down Expand Up @@ -157,13 +157,14 @@ const isMediatorCapable = async (agent: Agent): Promise<boolean | undefined> =>
*/
const isRegistered = async (): Promise<boolean> => {
const authorized = (await messaging().hasPermission()) === messaging.AuthorizationStatus.AUTHORIZED
const tokenValue = await PersistentStorage.fetchValueForKey<string>(BCLocalStorageKeys.DeviceToken)

// Need to register for push notification capability on iOS
if (Platform.OS === 'ios' && !messaging().isDeviceRegisteredForRemoteMessages) {
await messaging().registerDeviceForRemoteMessages()
}

if (authorized && (await AsyncStorage.getItem(TOKEN_STORAGE_KEY)) !== null) {
if (authorized && tokenValue !== null) {
return true
}
return false
Expand All @@ -175,7 +176,10 @@ const isRegistered = async (): Promise<boolean> => {
*/
const isEnabled = async (): Promise<boolean> => {
try {
return (await messaging().getToken()) === (await AsyncStorage.getItem(TOKEN_STORAGE_KEY))
const deviceTokenValue = await PersistentStorage.fetchValueForKey<string>(BCLocalStorageKeys.DeviceToken)
const messageTokenValue = await messaging().getToken()

return messageTokenValue === deviceTokenValue
} catch (error) {
return false
}
Expand Down Expand Up @@ -207,9 +211,9 @@ const setDeviceInfo = async (agent: Agent, blankDeviceToken = false): Promise<vo
devicePlatform: Platform.OS,
})
if (blankDeviceToken) {
AsyncStorage.setItem(TOKEN_STORAGE_KEY, 'blank')
await PersistentStorage.storeValueForKey<string>(BCLocalStorageKeys.DeviceToken, 'blank')
} else {
AsyncStorage.setItem(TOKEN_STORAGE_KEY, token)
await PersistentStorage.storeValueForKey<string>(BCLocalStorageKeys.DeviceToken, token)
}
} catch (error) {
agent.config.logger.error('Error sending device token info to mediator agent')
Expand Down
5 changes: 2 additions & 3 deletions app/src/screens/PersonCredentialLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CredentialState, ProofState } from '@credo-ts/core'
import { useAgent, useCredentialByState, useProofByState } from '@credo-ts/react-hooks'
import { CredentialState } from '@credo-ts/core'
import { useAgent, useCredentialByState } from '@credo-ts/react-hooks'
import {
Button,
ButtonType,
Expand Down Expand Up @@ -37,7 +37,6 @@ const PersonCredentialLoading: React.FC<PersonProps> = ({ navigation }) => {
const { ColorPallet, TextTheme } = useTheme()
const [store] = useStore<BCState>()
const [remoteAgentDetails, setRemoteAgentDetails] = useState<WellKnownAgentDetails | undefined>()
const receivedProofRequests = useProofByState(ProofState.RequestReceived)
const timer = useRef<NodeJS.Timeout>()
const [logger] = useServices([TOKENS.UTIL_LOGGER])
const receivedCredentialOffers = useCredentialByState(CredentialState.OfferReceived)
Expand Down
26 changes: 8 additions & 18 deletions app/src/screens/Splash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
createLinkSecretIfRequired,
TOKENS,
useServices,
PersistentStorage,
} from '@hyperledger/aries-bifold-core'
import { RemoteOCABundleResolver } from '@hyperledger/aries-oca/build/legacy'
import { GetCredentialDefinitionRequest, GetSchemaRequest } from '@hyperledger/indy-vdr-shared'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { CommonActions, useNavigation } from '@react-navigation/native'
import moment from 'moment'
import React, { useEffect, useState } from 'react'
Expand Down Expand Up @@ -206,20 +206,9 @@ const Splash = () => {
setMounted(true)
}, [])

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const loadObjectFromStorage = async (key: string): Promise<undefined | any> => {
try {
const data = await AsyncStorage.getItem(key)
if (data) {
return JSON.parse(data)
}
} catch {
return
}
}

const loadCachedLedgers = async (): Promise<IndyVdrPoolConfig[] | undefined> => {
const cachedTransactions = await loadObjectFromStorage(BCLocalStorageKeys.GenesisTransactions)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cachedTransactions = await PersistentStorage.fetchValueForKey<any>(BCLocalStorageKeys.GenesisTransactions)
if (cachedTransactions) {
const { timestamp, transactions } = cachedTransactions
return moment().diff(moment(timestamp), 'days') >= 1 ? undefined : transactions
Expand Down Expand Up @@ -442,10 +431,11 @@ const Splash = () => {
}, ''),
}))
if (transactions) {
await AsyncStorage.setItem(
BCLocalStorageKeys.GenesisTransactions,
JSON.stringify({ timestamp: moment().toISOString(), transactions })
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await PersistentStorage.storeValueForKey<any>(BCLocalStorageKeys.GenesisTransactions, {
timestamp: moment().toISOString(),
transactions,
})
}
}

Expand Down
19 changes: 12 additions & 7 deletions app/src/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
reducer as bifoldReducer,
defaultState,
ReducerAction,
PersistentStorage,
} from '@hyperledger/aries-bifold-core'
import AsyncStorage from '@react-native-async-storage/async-storage'

export interface IASEnvironment {
name: string
Expand Down Expand Up @@ -98,6 +98,8 @@ export enum BCLocalStorageKeys {
GenesisTransactions = 'GenesisTransactions',
RemoteDebugging = 'RemoteDebugging',
EnableProxy = 'EnableProxy',
UserDeniedPushNotifications = 'userDeniedPushNotifications',
DeviceToken = 'deviceToken',
}

export const initialState: BCState = {
Expand All @@ -115,9 +117,9 @@ const bcReducer = (state: BCState, action: ReducerAction<BCDispatchAction>): BCS
const newState = { ...state, developer }

if (enabledAt) {
AsyncStorage.setItem(BCLocalStorageKeys.RemoteDebugging, JSON.stringify(developer.remoteDebugging))
PersistentStorage.storeValueForKey<boolean>(BCLocalStorageKeys.RemoteDebugging, developer.remoteDebugging)
} else {
AsyncStorage.removeItem(BCLocalStorageKeys.RemoteDebugging)
PersistentStorage.removeValueForKey(BCLocalStorageKeys.RemoteDebugging)
}

return newState
Expand All @@ -127,14 +129,16 @@ const bcReducer = (state: BCState, action: ReducerAction<BCDispatchAction>): BCS
const developer = { ...state.developer, environment }

// Persist IAS environment between app restarts
AsyncStorage.setItem(BCLocalStorageKeys.Environment, JSON.stringify(developer.environment))
PersistentStorage.storeValueForKey<IASEnvironment>(BCLocalStorageKeys.Environment, developer.environment)

return { ...state, developer }
}
case DeveloperDispatchAction.TOGGLE_PROXY: {
const enableProxy: boolean = (action?.payload || []).pop() || false
const developer = { ...state.developer, enableProxy }

AsyncStorage.setItem(BCLocalStorageKeys.EnableProxy, JSON.stringify(developer.enableProxy))
PersistentStorage.storeValueForKey<boolean>(BCLocalStorageKeys.EnableProxy, developer.enableProxy)

return { ...state, developer }
}
case DismissPersonCredentialOfferDispatchAction.PERSON_CREDENTIAL_OFFER_DISMISSED: {
Expand All @@ -143,10 +147,11 @@ const bcReducer = (state: BCState, action: ReducerAction<BCDispatchAction>): BCS
const newState = { ...state, dismissPersonCredentialOffer }

// save to storage so notification doesn't reapper on app restart
AsyncStorage.setItem(
PersistentStorage.storeValueForKey<DismissPersonCredentialOffer>(
BCLocalStorageKeys.PersonCredentialOfferDismissed,
JSON.stringify(newState.dismissPersonCredentialOffer)
newState.dismissPersonCredentialOffer
)

return newState
}
default:
Expand Down
50 changes: 25 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3037,9 +3037,9 @@ __metadata:
languageName: node
linkType: hard

"@hyperledger/aries-bifold-core@npm:1.0.0-alpha.319":
version: 1.0.0-alpha.319
resolution: "@hyperledger/aries-bifold-core@npm:1.0.0-alpha.319"
"@hyperledger/aries-bifold-core@npm:1.0.0-alpha.330":
version: 1.0.0-alpha.330
resolution: "@hyperledger/aries-bifold-core@npm:1.0.0-alpha.330"
peerDependencies:
"@credo-ts/anoncreds": 0.5.9
"@credo-ts/askar": 0.5.9
Expand Down Expand Up @@ -3114,57 +3114,57 @@ __metadata:
uuid: ^9.0.0
bin:
bifold: bin/bifold
checksum: da78b122099ba85883ea1517fb979faf595b24784bc3c91f9f68a5185c38b059b7548f320c3be2f1f6411fb4e6bf463989c609c1d22dfb204a63777ea10f16b4
checksum: ccbb107d1b87c3a13466e50616191eaf69478f26908348aae33f95b9e5712b907f338baf11932184f854e4b39b0082611da7d94175cb731d42a488a2d1c90ab2
languageName: node
linkType: hard

"@hyperledger/aries-bifold-remote-logs@npm:1.0.0-alpha.319":
version: 1.0.0-alpha.319
resolution: "@hyperledger/aries-bifold-remote-logs@npm:1.0.0-alpha.319"
"@hyperledger/aries-bifold-remote-logs@npm:1.0.0-alpha.330":
version: 1.0.0-alpha.330
resolution: "@hyperledger/aries-bifold-remote-logs@npm:1.0.0-alpha.330"
peerDependencies:
"@credo-ts/core": 0.5.9
axios: ^1.4.0
buffer: ^6.0.3
react: ^18.2.0
react-native: ^0.72.5
react-native-logs: ^5.1.0
checksum: d0813a73b3f6fca1840c7a6614fee0afaf5d1cc42a367c53d2428175d7b9fc685895e9403cd91eb7be8b87feeec6e7476c5ffa2abd6834b52054403e2d1cf2c4
checksum: 04b431d62f75b96b5d5a6b2c8983960bee81c73257fc5f344b54b97ea68f7f2f7672098da6eea672939e2af1a79bc0ff3560eca9b40ecf6fc0f966be61f2826a
languageName: node
linkType: hard

"@hyperledger/aries-bifold-verifier@npm:1.0.0-alpha.319":
version: 1.0.0-alpha.319
resolution: "@hyperledger/aries-bifold-verifier@npm:1.0.0-alpha.319"
"@hyperledger/aries-bifold-verifier@npm:1.0.0-alpha.330":
version: 1.0.0-alpha.330
resolution: "@hyperledger/aries-bifold-verifier@npm:1.0.0-alpha.330"
peerDependencies:
"@credo-ts/anoncreds": 0.5.9
"@credo-ts/core": 0.5.9
"@credo-ts/react-hooks": ^0.6.0
"@hyperledger/anoncreds-shared": 0.2.2
react: ^18.2.0
checksum: df868e83a05c7aeb9f5e123ccb07dc583637c1f6ffc413546d62dc52d35aed8453bc02e49124572093fab2a14510fa7f1f16da2e9c9206bdca41c42270eedc3b
checksum: 756ac3b87385b04aa490ada53e64d4c9100b5d8c40afc3bd151916256098ea21d1997bfaa7067be3dd1027e047114b11a3189139f3a582880fd7162008e40cc4
languageName: node
linkType: hard

"@hyperledger/aries-oca@npm:1.0.0-alpha.319":
version: 1.0.0-alpha.319
resolution: "@hyperledger/aries-oca@npm:1.0.0-alpha.319"
"@hyperledger/aries-oca@npm:1.0.0-alpha.330":
version: 1.0.0-alpha.330
resolution: "@hyperledger/aries-oca@npm:1.0.0-alpha.330"
dependencies:
"@credo-ts/anoncreds": "npm:0.5.9"
"@credo-ts/core": "npm:0.5.9"
axios: "npm:^1.4.0"
lodash.startcase: "npm:^4.4.0"
react-native-fs: "npm:^2.16.6"
checksum: a3f8d96ac6ca2d5662a0fee9d4103f74407d3094a0f9d563c0796a9eb2b69f4c67cfd4fff614dce9c3a37ece6ce1b94c68e1df7181036b247600571bed1b3778
checksum: 6d058522de31144fc320730f1cf2656424a90dbaf1d2475012022276ee467d9ce923dc96896359bfb4bdc2025dd3b629cc0104d069b16c5c8499060287e7a4c3
languageName: node
linkType: hard

"@hyperledger/aries-react-native-attestation@npm:1.0.0-alpha.319":
version: 1.0.0-alpha.319
resolution: "@hyperledger/aries-react-native-attestation@npm:1.0.0-alpha.319"
"@hyperledger/aries-react-native-attestation@npm:1.0.0-alpha.330":
version: 1.0.0-alpha.330
resolution: "@hyperledger/aries-react-native-attestation@npm:1.0.0-alpha.330"
peerDependencies:
react: "*"
react-native: "*"
checksum: 48e5b1242b9843d01f5813825e26f90125eabebac41aa62f6a922b0ff5b73409d7dc6383ad65f0c662d82e023bbce4600f69096579c7e6756cc49d81416ceb6f
checksum: de9bdf5a0413362b9970f59eeb87a5037e5ad23e467ef808d98faeda88a9c9bb3480279d4bb377e4fd9f01ba75f0d795c70d98a0d69cae31deca0f96d187dd71
languageName: node
linkType: hard

Expand Down Expand Up @@ -7864,11 +7864,11 @@ __metadata:
"@formatjs/intl-relativetimeformat": "npm:9.3.1"
"@hyperledger/anoncreds-react-native": "npm:0.2.2"
"@hyperledger/aries-askar-react-native": "npm:0.2.3"
"@hyperledger/aries-bifold-core": "npm:1.0.0-alpha.319"
"@hyperledger/aries-bifold-remote-logs": "npm:1.0.0-alpha.319"
"@hyperledger/aries-bifold-verifier": "npm:1.0.0-alpha.319"
"@hyperledger/aries-oca": "npm:1.0.0-alpha.319"
"@hyperledger/aries-react-native-attestation": "npm:1.0.0-alpha.319"
"@hyperledger/aries-bifold-core": "npm:1.0.0-alpha.330"
"@hyperledger/aries-bifold-remote-logs": "npm:1.0.0-alpha.330"
"@hyperledger/aries-bifold-verifier": "npm:1.0.0-alpha.330"
"@hyperledger/aries-oca": "npm:1.0.0-alpha.330"
"@hyperledger/aries-react-native-attestation": "npm:1.0.0-alpha.330"
"@hyperledger/indy-vdr-react-native": "npm:0.2.2"
"@hyperledger/indy-vdr-shared": "npm:0.2.2"
"@react-native-async-storage/async-storage": "npm:1.15.11"
Expand Down

0 comments on commit 7b8a677

Please sign in to comment.