Skip to content

Commit

Permalink
Merge schema and type files
Browse files Browse the repository at this point in the history
Rename signer config for better reusability.
  • Loading branch information
wcalderipe committed Apr 2, 2024
1 parent d36d3cf commit f53dffd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConfigService } from '@narval/config-module'
import { Injectable } from '@nestjs/common'
import { Config } from '../../../policy-engine.config'
import { EngineSignerConfig } from '../../../shared/type/domain.type'
import { SignerConfig } from '../../../shared/type/domain.type'
import { EngineSignerConfigRepository } from '../../persistence/repository/engine-signer-config.repository'
import { EngineSignerNotFoundException } from '../exception/engine-signer-not-found.exception'

Expand All @@ -12,11 +12,11 @@ export class EngineSignerConfigService {
private engineSignerConfigRepository: EngineSignerConfigRepository
) {}

async save(signerConfig: EngineSignerConfig): Promise<boolean> {
async save(signerConfig: SignerConfig): Promise<boolean> {
return this.engineSignerConfigRepository.save(this.getEngineId(), signerConfig)
}

async getSignerConfigOrThrow(): Promise<EngineSignerConfig> {
async getSignerConfigOrThrow(): Promise<SignerConfig> {
const signerConfig = await this.getSignerConfig()

if (signerConfig) {
Expand All @@ -26,7 +26,7 @@ export class EngineSignerConfigService {
throw new EngineSignerNotFoundException()
}

async getSignerConfig(): Promise<EngineSignerConfig | null> {
async getSignerConfig(): Promise<SignerConfig | null> {
return this.engineSignerConfigRepository.findByEngineId(this.getEngineId())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { KeyValueRepository } from '../../../../../shared/module/key-value/core/
import { EncryptKeyValueService } from '../../../../../shared/module/key-value/core/service/encrypt-key-value.service'
import { InMemoryKeyValueRepository } from '../../../../../shared/module/key-value/persistence/repository/in-memory-key-value.repository'
import { getTestRawAesKeyring } from '../../../../../shared/testing/encryption.testing'
import { EngineSignerConfig } from '../../../../../shared/type/domain.type'
import { SignerConfig } from '../../../../../shared/type/domain.type'
import { EngineSignerConfigRepository } from '../../engine-signer-config.repository'

describe(EngineSignerConfigRepository.name, () => {
Expand Down Expand Up @@ -38,7 +38,7 @@ describe(EngineSignerConfigRepository.name, () => {

describe('save', () => {
it('saves encrypted signer configuration', async () => {
const signerConfig: EngineSignerConfig = {
const signerConfig: SignerConfig = {
type: 'PRIVATE_KEY',
key: secp256k1PrivateKeyToJwk('0x44c75d8485a564a3c8d60ed23e7524f77bba719372f1c05807d88af6d3c09f55')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, Logger } from '@nestjs/common'
import { EncryptKeyValueService } from '../../../shared/module/key-value/core/service/encrypt-key-value.service'
import { decode, encode } from '../../../shared/module/key-value/core/util/coercion.util'
import { EngineSignerConfig } from '../../../shared/type/domain.type'
import { SignerConfig } from '../../../shared/type/domain.type'
import { EngineSignerConfigService } from '../../core/service/engine-signer-config.service'

@Injectable()
Expand All @@ -10,9 +10,9 @@ export class EngineSignerConfigRepository {

constructor(private encryptKeyValueService: EncryptKeyValueService) {}

async save(engineId: string, signerConfig: EngineSignerConfig): Promise<boolean> {
async save(engineId: string, signerConfig: SignerConfig): Promise<boolean> {
try {
await this.encryptKeyValueService.set(this.getKey(engineId), this.encode(signerConfig))
await this.encryptKeyValueService.set(this.getKey(engineId), encode(SignerConfig, signerConfig))

return true
} catch (error) {
Expand All @@ -25,11 +25,11 @@ export class EngineSignerConfigRepository {
}
}

async findByEngineId(engineId: string): Promise<EngineSignerConfig | null> {
async findByEngineId(engineId: string): Promise<SignerConfig | null> {
const value = await this.encryptKeyValueService.get(this.getKey(engineId))

if (value) {
return this.decode(value)
return decode(SignerConfig, value)
}

return null
Expand All @@ -38,12 +38,4 @@ export class EngineSignerConfigRepository {
getKey(id: string): string {
return `engine:${id}:signer-config`
}

private encode(engine: EngineSignerConfig): string {
return encode(EngineSignerConfig, engine)
}

private decode(value: string): EngineSignerConfig {
return decode(EngineSignerConfig, value)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'
import { KeyValueService } from '../../../shared/module/key-value/core/service/key-value.service'
import { engineSchema } from '../../../shared/schema/engine.schema'
import { Engine, EngineSignerConfig } from '../../../shared/type/domain.type'
import { decode, encode } from '../../../shared/module/key-value/core/util/coercion.util'
import { Engine } from '../../../shared/type/domain.type'

@Injectable()
export class EngineRepository {
Expand All @@ -11,27 +11,19 @@ export class EngineRepository {
const value = await this.keyValueService.get(this.getEngineKey(id))

if (value) {
return this.decode(value)
return decode(Engine, value)
}

return null
}

async save(engine: Engine): Promise<Engine> {
await this.keyValueService.set(this.getEngineKey(engine.id), this.encode(engine))
await this.keyValueService.set(this.getEngineKey(engine.id), encode(Engine, engine))

return engine
}

getEngineKey(id: string): string {
return `engine:${id}`
}

private decode(value: string): Engine {
return engineSchema.parse(JSON.parse(value))
}

private encode(engine: Engine | EngineSignerConfig): string {
return JSON.stringify(engine)
}
}
7 changes: 0 additions & 7 deletions apps/policy-engine/src/shared/schema/engine.schema.ts

This file was deleted.

16 changes: 9 additions & 7 deletions apps/policy-engine/src/shared/type/domain.type.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { privateKeySchema } from '@narval/signature'
import { dataStoreConfigurationSchema } from 'packages/policy-engine-shared/src/lib/schema/data-store.schema'
import { z } from 'zod'
import { engineSchema } from '../schema/engine.schema'

export const EngineSignerConfig = z.object({
export const SignerConfig = z.object({
type: z.literal('PRIVATE_KEY'),
key: privateKeySchema
})

export type EngineSignerConfig = z.infer<typeof EngineSignerConfig>
export type SignerConfig = z.infer<typeof SignerConfig>

export const Client = z.object({
clientId: z.string(),
Expand All @@ -17,11 +15,15 @@ export const Client = z.object({
entity: dataStoreConfigurationSchema,
policy: dataStoreConfigurationSchema
}),
signer: EngineSignerConfig,
signer: SignerConfig,
createdAt: z.coerce.date(),
updatedAt: z.coerce.date()
})

export type Client = z.infer<typeof Client>

export type Engine = z.infer<typeof engineSchema>
export const Engine = z.object({
id: z.string().min(1),
adminApiKey: z.string().min(1),
masterKey: z.string().min(1).optional()
})
export type Engine = z.infer<typeof Engine>

0 comments on commit f53dffd

Please sign in to comment.