Skip to content

Commit

Permalink
Use bean qualifiers to prevent type mismatches during service injection
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperschwartz committed Sep 24, 2022
1 parent 309d592 commit 77a88ba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.provenance.scope.encryption.util.toJavaPrivateKey
import io.provenance.scope.encryption.util.toKeyPair
import io.provenance.scope.objectstore.client.CachedOsClient
import io.provenance.scope.objectstore.client.OsClient
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

Expand All @@ -21,14 +22,14 @@ class AppConfig {
return CachedOsClient(osClient, objectStoreProperties.decryptionWorkerThreads, objectStoreProperties.concurrencySize, objectStoreProperties.cacheRecordSizeBytes)
}

@Bean
@Bean(BeanQualifiers.OBJECTSTORE_ENCRYPTION_KEYS)
fun encryptionKeys(provenanceProperties: ProvenanceProperties, objectStoreProperties: ObjectStoreProperties): Map<String, KeyRef> = objectStoreProperties.privateKeys.map {
it.toJavaPrivateKey().toKeyPair().let { keyPair ->
keyPair.public.getAddress(provenanceProperties.mainNet) to DirectKeyRef(keyPair)
}
}.toMap()

@Bean
@Bean(BeanQualifiers.OBJECTSTORE_MASTER_KEY)
fun masterKey(objectStoreProperties: ObjectStoreProperties): KeyRef = objectStoreProperties.masterKey.toJavaPrivateKey().toKeyPair().let(::DirectKeyRef)

@Bean
Expand All @@ -38,6 +39,8 @@ class AppConfig {
gasEstimationMethod = GasEstimationMethod.MSG_FEE_CALCULATION,
)

@Bean
fun accountAddresses(encryptionKeys: Map<String, KeyRef>): Set<String> = encryptionKeys.keys
@Bean(BeanQualifiers.OBJECTSTORE_PRIVATE_KEYS)
fun accountAddresses(
@Qualifier(BeanQualifiers.OBJECTSTORE_ENCRYPTION_KEYS) encryptionKeys: Map<String, KeyRef>,
): Set<String> = encryptionKeys.keys
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package tech.figure.objectstore.gateway.configuration

object BeanQualifiers {
const val EVENT_STREAM_COROUTINE_SCOPE_QUALIFIER = "eventStreamCoroutineScopeBean"
const val OBJECTSTORE_ENCRYPTION_KEYS: String = "objectStoreEncryptionKeys"
const val OBJECTSTORE_PRIVATE_KEYS: String = "objectStorePrivateKeys"
const val OBJECTSTORE_MASTER_KEY: String = "objectStoreMasterKey"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package tech.figure.objectstore.gateway.service

import io.provenance.metadata.v1.ScopeResponse
import mu.KLogging
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import tech.figure.objectstore.gateway.configuration.BeanQualifiers
import tech.figure.objectstore.gateway.repository.ScopePermissionsRepository

@Service
class ScopePermissionsService(
private val accountAddresses: Set<String>,
@Qualifier(BeanQualifiers.OBJECTSTORE_PRIVATE_KEYS) private val accountAddresses: Set<String>,
private val scopeFetchService: ScopeFetchService,
private val scopePermissionsRepository: ScopePermissionsRepository,
) {
Expand Down

0 comments on commit 77a88ba

Please sign in to comment.