Skip to content

Commit

Permalink
store seed credential
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
Berend Sliedrecht committed Jul 30, 2024
1 parent fadad1a commit 7a63e71
Show file tree
Hide file tree
Showing 7 changed files with 3,052 additions and 2,941 deletions.
1 change: 1 addition & 0 deletions apps/funke/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ExpoConstants from 'expo-constants'

export const FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID = 'FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID '
export const FUNKE_WALLET_INSTANCE_LONG_TERM_AES_KEY_ID = 'FUNKE_WALLET_INSTANCE_LONG_TERM_AES_KEY_ID'

const TRUSTED_CERTIFICATES = ExpoConstants.expoConfig?.extra?.trustedCertificates as [string, ...string[]] | undefined
Expand Down
1 change: 1 addition & 0 deletions apps/funke/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './seedCredential'
18 changes: 18 additions & 0 deletions apps/funke/storage/seedCredential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { AgentContext } from '@credo-ts/core'
import { walletJsonStore } from '@package/agent'
import { FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID } from '../constants'

export const seedCredentialStorage = {
store: async (context: AgentContext, seedCredential: string) =>
walletJsonStore.store(context, FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID, {
seedCredential,
}),

replace: async (context: AgentContext, seedCredential: string) =>
walletJsonStore.replace(context, FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID, {
seedCredential,
}),

getById: async (context: AgentContext) =>
walletJsonStore.getById<{ seedCredential: string }>(context, FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID),
}
1 change: 1 addition & 0 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export {
} from './format/formatPresentation'
export * from './mediation'
export * from './crypto'
export * from './storage'
1 change: 1 addition & 0 deletions packages/agent/src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './walletJsonStore'
54 changes: 54 additions & 0 deletions packages/agent/src/storage/walletJsonStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { assertAskarWallet } from '@credo-ts/askar/build/utils/assertAskarWallet'
import type { AgentContext } from '@credo-ts/core'

const store = async (context: AgentContext, id: string, value: Record<string, unknown>) => {
const wallet = context.wallet
assertAskarWallet(wallet)

wallet.withSession(async (session) => {
session.insert({
name: id,
value: JSON.stringify(value),
category: 'genericStorageRecord',
})
})
}

const replace = async (context: AgentContext, id: string, value: Record<string, unknown>) => {
const wallet = context.wallet
assertAskarWallet(wallet)

wallet.withSession(async (session) => {
session.replace({
name: id,
value: JSON.stringify(value),
category: 'genericStorageRecord',
})
})
}

const getById = async <T extends Record<string, unknown>>(context: AgentContext, id: string): Promise<T> => {
const wallet = context.wallet
assertAskarWallet(wallet)

const item = await wallet.withSession(async (session) =>
session.fetch({
name: id,
category: 'genericStorageRecord',
})
)

if (!item) {
throw new Error(`No generic item with id: ${id} found`)
}

const transformedValue = item.value === 'string' ? JSON.parse(item.value) : item.value

return transformedValue as T
}

export const walletJsonStore = {
store,
getById,
replace,
}
5,917 changes: 2,976 additions & 2,941 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit 7a63e71

Please sign in to comment.