Skip to content

Commit

Permalink
Add ledgers in cached memory (#85)
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Drouin <[email protected]>
  • Loading branch information
jcdrouin21 authored Feb 22, 2024
1 parent 8e0157e commit ea311d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
40 changes: 39 additions & 1 deletion app/src/screens/Splash.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Agent, HttpOutboundTransport, MediatorPickupStrategy, WsOutboundTransport } from '@aries-framework/core'
import { IndyVdrPoolConfig, IndyVdrPoolService } from '@aries-framework/indy-vdr/build/pool'
import { useAgent } from '@aries-framework/react-hooks'
import { agentDependencies } from '@aries-framework/react-native'
import {
Expand All @@ -25,6 +26,7 @@ import {
} from '@hyperledger/aries-bifold-core'
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'
import { useTranslation } from 'react-i18next'
import { StyleSheet, View, Text, Image } from 'react-native'
Expand Down Expand Up @@ -218,6 +220,14 @@ const Splash: React.FC = () => {
return environment?.iasAgentInviteUrl ?? Config.MCN_MEDIATOR_URL
}

const loadCachedLedgers = async (): Promise<IndyVdrPoolConfig[] | undefined> => {
const cachedTransactions = await loadObjectFromStorage(BCLocalStorageKeys.GenesisTransactions)
if (cachedTransactions) {
const { timestamp, transactions } = cachedTransactions
return moment().diff(moment(timestamp), 'days') >= 1 ? undefined : transactions
}
}

useEffect(() => {
const initOnboarding = async (): Promise<void> => {
try {
Expand Down Expand Up @@ -344,6 +354,8 @@ const Splash: React.FC = () => {
}

setStep(5)
const cachedLedgers = await loadCachedLedgers()
const ledgers = cachedLedgers ?? indyLedgers

const options = {
config: {
Expand All @@ -358,7 +370,7 @@ const Splash: React.FC = () => {
},
dependencies: agentDependencies,
modules: getAgentModules({
indyNetworks: indyLedgers,
indyNetworks: ledgers,
mediatorInvitationUrl: environment,
}),
}
Expand All @@ -385,6 +397,32 @@ const Splash: React.FC = () => {

setStep(6)
await newAgent.initialize()
const poolService = newAgent.dependencyManager.resolve(IndyVdrPoolService)
if (!cachedLedgers) {
// these escapes can be removed once Indy VDR has been upgraded and the patch is no longer needed
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:next-line
await poolService.refreshPoolConnections()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:next-line
const raw_transactions = await poolService.getPoolTransactions()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:next-line
const transactions = raw_transactions.map(({ config, transactions }) => ({
...config,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:next-line
genesisTransactions: transactions.reduce((prev, curr) => {
return prev + JSON.stringify(curr)
}, ''),
}))
if (transactions) {
await AsyncStorage.setItem(
BCLocalStorageKeys.GenesisTransactions,
JSON.stringify({ timestamp: moment().toISOString(), transactions })
)
}
}

setStep(7)
await createLinkSecretIfRequired(newAgent)
Expand Down
1 change: 1 addition & 0 deletions app/src/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export enum BCLocalStorageKeys {
PersonCredentialOfferDismissed = 'PersonCredentialOfferDismissed',
Environment = 'Environment',
Attestation = 'Attestation',
GenesisTransactions = 'GenesisTransactions',
}

export const initialState: BCState = {
Expand Down

0 comments on commit ea311d7

Please sign in to comment.