From 703befe20027c8c0243f92d88039e34cdf60c658 Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Tue, 30 Jul 2024 14:02:26 +0200 Subject: [PATCH] store seed credential Signed-off-by: Berend Sliedrecht --- apps/funke/constants.ts | 1 + apps/funke/storage/index.ts | 1 + apps/funke/storage/seedCredential.ts | 18 ++++++++++++ packages/agent/src/index.ts | 1 + packages/agent/src/storage/index.ts | 1 + packages/agent/src/storage/walletJsonStore.ts | 28 +++++++++++++++++++ 6 files changed, 50 insertions(+) create mode 100644 apps/funke/storage/index.ts create mode 100644 apps/funke/storage/seedCredential.ts create mode 100644 packages/agent/src/storage/index.ts create mode 100644 packages/agent/src/storage/walletJsonStore.ts diff --git a/apps/funke/constants.ts b/apps/funke/constants.ts index 699748e7..a2ec6df5 100644 --- a/apps/funke/constants.ts +++ b/apps/funke/constants.ts @@ -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 diff --git a/apps/funke/storage/index.ts b/apps/funke/storage/index.ts new file mode 100644 index 00000000..840978b8 --- /dev/null +++ b/apps/funke/storage/index.ts @@ -0,0 +1 @@ +export * from './seedCredential' diff --git a/apps/funke/storage/seedCredential.ts b/apps/funke/storage/seedCredential.ts new file mode 100644 index 00000000..a85a7fc1 --- /dev/null +++ b/apps/funke/storage/seedCredential.ts @@ -0,0 +1,18 @@ +import type { Agent } from '@credo-ts/core' +import { walletJsonStore } from '@package/agent' +import { FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID } from '../constants' + +export const seedCredentialStorage = { + store: async (agent: Agent, seedCredential: string) => + walletJsonStore.store(agent, FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID, { + seedCredential, + }), + + update: async (agent: Agent, seedCredential: string) => + walletJsonStore.update(agent, FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID, { + seedCredential, + }), + + getById: async (agent: Agent) => + walletJsonStore.getById<{ seedCredential: string }>(agent, FUNKE_WALLET_SEED_CREDENTIAL_RECORD_ID), +} diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.ts index e41c7375..a72c4925 100644 --- a/packages/agent/src/index.ts +++ b/packages/agent/src/index.ts @@ -23,3 +23,4 @@ export { } from './format/formatPresentation' export * from './mediation' export * from './crypto' +export * from './storage' diff --git a/packages/agent/src/storage/index.ts b/packages/agent/src/storage/index.ts new file mode 100644 index 00000000..1ce2db56 --- /dev/null +++ b/packages/agent/src/storage/index.ts @@ -0,0 +1 @@ +export * from './walletJsonStore' diff --git a/packages/agent/src/storage/walletJsonStore.ts b/packages/agent/src/storage/walletJsonStore.ts new file mode 100644 index 00000000..9b8c41ae --- /dev/null +++ b/packages/agent/src/storage/walletJsonStore.ts @@ -0,0 +1,28 @@ +import type { Agent } from '@credo-ts/core' +import { GenericRecord } from '@credo-ts/core/build/modules/generic-records/repository/GenericRecord' + +const store = async (agent: Agent, id: string, value: Record) => { + const record = new GenericRecord({ id, content: value }) + await agent.genericRecords.save(record) +} + +const update = async (agent: Agent, id: string, value: Record) => { + const record = new GenericRecord({ id, content: value }) + await agent.genericRecords.update(record) +} + +const getById = async >(agent: Agent, id: string): Promise => { + const record = await agent.genericRecords.findById(id) + + if (!record) { + throw Error(`Record with id: ${id} not found`) + } + + return record.content as T +} + +export const walletJsonStore = { + store, + getById, + update, +}