From 8460a46f317b8d86972f4b364e69aa953d09d2f3 Mon Sep 17 00:00:00 2001 From: dgeorgiev Date: Fri, 14 Jun 2024 15:41:04 +0000 Subject: [PATCH] [WTCH-328] Add default bean of SenderAddressProvider --- .../contract/ContractAutoConfiguration.kt | 2 + ...BlockingServiceFactoryAutoConfiguration.kt | 5 +-- .../NodeCredentialsProviderConfiguration.kt | 29 ------------- ...sConfiguration.kt => NodeConfiguration.kt} | 21 +++++++++- .../WeTxServiceTxSignerAutoConfiguration.kt | 42 +++++++++++++++++++ .../src/test/resources/application-test.yml | 16 ++++++- .../build.gradle.kts | 1 - 7 files changed, 81 insertions(+), 35 deletions(-) delete mode 100644 we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeCredentialsProviderConfiguration.kt rename we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/{NodePropertiesConfiguration.kt => NodeConfiguration.kt} (69%) diff --git a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/contract/ContractAutoConfiguration.kt b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/contract/ContractAutoConfiguration.kt index 7b08c7a..e59c7fb 100644 --- a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/contract/ContractAutoConfiguration.kt +++ b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/contract/ContractAutoConfiguration.kt @@ -6,6 +6,7 @@ import com.wavesenterprise.sdk.spring.autoconfigure.contract.properties.Contract import com.wavesenterprise.sdk.spring.autoconfigure.contract.properties.LegacyContractsPropertiesConfiguration import com.wavesenterprise.sdk.spring.autoconfigure.contract.update.ContractsUpdateConfig import com.wavesenterprise.sdk.spring.autoconfigure.node.service.NodeServicesAutoConfiguration +import com.wavesenterprise.sdk.spring.autoconfigure.signer.WeTxServiceTxSignerAutoConfiguration import org.springframework.boot.autoconfigure.AutoConfigureAfter import org.springframework.boot.autoconfigure.condition.ConditionalOnClass import org.springframework.context.annotation.Configuration @@ -22,6 +23,7 @@ import org.springframework.context.annotation.Import ) @AutoConfigureAfter( NodeServicesAutoConfiguration::class, + WeTxServiceTxSignerAutoConfiguration::class, ) @ConditionalOnClass( ContractBlockingClientFactory::class, diff --git a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeBlockingServiceFactoryAutoConfiguration.kt b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeBlockingServiceFactoryAutoConfiguration.kt index 2113f14..6329ca6 100644 --- a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeBlockingServiceFactoryAutoConfiguration.kt +++ b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeBlockingServiceFactoryAutoConfiguration.kt @@ -21,8 +21,8 @@ import com.wavesenterprise.sdk.spring.autoconfigure.node.holder.NodeBlockingServ import com.wavesenterprise.sdk.spring.autoconfigure.node.holder.NodeClient import com.wavesenterprise.sdk.spring.autoconfigure.node.legacy.LegacyNodeConfiguration import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.CacheProperties +import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.NodeConfiguration import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.NodeProperties -import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.NodePropertiesConfiguration import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.RateLimiterProperties import com.wavesenterprise.sdk.spring.autoconfigure.node.service.NodeServicesAutoConfiguration import org.springframework.boot.autoconfigure.AutoConfigureBefore @@ -40,8 +40,7 @@ import org.springframework.context.annotation.Import ) @Import( LegacyNodeConfiguration::class, - NodePropertiesConfiguration::class, - NodeCredentialsProviderConfiguration::class, + NodeConfiguration::class, ) @ConditionalOnClass( NodeBlockingServiceFactory::class, diff --git a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeCredentialsProviderConfiguration.kt b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeCredentialsProviderConfiguration.kt deleted file mode 100644 index b63e576..0000000 --- a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/NodeCredentialsProviderConfiguration.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.wavesenterprise.sdk.spring.autoconfigure.node - -import com.wavesenterprise.sdk.node.client.blocking.credentials.DefaultNodeCredentialsProvider -import com.wavesenterprise.sdk.node.client.blocking.credentials.NodeCredentialsProvider -import com.wavesenterprise.sdk.node.domain.Address -import com.wavesenterprise.sdk.node.domain.Password -import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.NodeCredentialsProperties -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean -import org.springframework.boot.context.properties.EnableConfigurationProperties -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -@Configuration -@EnableConfigurationProperties( - NodeCredentialsProperties::class, -) -class NodeCredentialsProviderConfiguration { - - @Bean - @ConditionalOnMissingBean - fun nodeCredentialsProvider( - nodeCredentialsProperties: NodeCredentialsProperties, - ): NodeCredentialsProvider = - DefaultNodeCredentialsProvider( - credentialsMap = nodeCredentialsProperties.addresses.map { (address, password) -> - Address.fromBase58(address) to password?.let { Password(password) } - }.toMap() - ) -} diff --git a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/NodePropertiesConfiguration.kt b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/NodeConfiguration.kt similarity index 69% rename from we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/NodePropertiesConfiguration.kt rename to we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/NodeConfiguration.kt index eb41349..da4754f 100644 --- a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/NodePropertiesConfiguration.kt +++ b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/node/properties/NodeConfiguration.kt @@ -1,19 +1,38 @@ package com.wavesenterprise.sdk.spring.autoconfigure.node.properties +import com.wavesenterprise.sdk.node.client.blocking.credentials.DefaultNodeCredentialsProvider +import com.wavesenterprise.sdk.node.client.blocking.credentials.NodeCredentialsProvider +import com.wavesenterprise.sdk.node.domain.Address +import com.wavesenterprise.sdk.node.domain.Password import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @Configuration +@EnableConfigurationProperties( + NodeCredentialsProperties::class, +) @ConditionalOnProperty(value = ["node.legacy-mode"], havingValue = "false", matchIfMissing = true) -class NodePropertiesConfiguration { +class NodeConfiguration { @Bean @ConfigurationProperties(prefix = "node") fun nodeConfigurationProperties() = NodeConfigurationProperties() + @Bean + @ConditionalOnMissingBean + fun nodeCredentialsProvider( + nodeCredentialsProperties: NodeCredentialsProperties, + ): NodeCredentialsProvider = + DefaultNodeCredentialsProvider( + credentialsMap = nodeCredentialsProperties.addresses.map { (address, password) -> + Address.fromBase58(address) to password?.let { Password(password) } + }.toMap() + ) + @Bean @ConditionalOnMissingBean fun nodeProperties( diff --git a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/signer/WeTxServiceTxSignerAutoConfiguration.kt b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/signer/WeTxServiceTxSignerAutoConfiguration.kt index a68d1f0..26effca 100644 --- a/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/signer/WeTxServiceTxSignerAutoConfiguration.kt +++ b/we-autoconfigure/src/main/kotlin/com/wavesenterprise/sdk/spring/autoconfigure/signer/WeTxServiceTxSignerAutoConfiguration.kt @@ -1,15 +1,25 @@ package com.wavesenterprise.sdk.spring.autoconfigure.signer +import com.wavesenterprise.sdk.node.client.blocking.credentials.DefaultSenderAddressProvider import com.wavesenterprise.sdk.node.client.blocking.credentials.NodeCredentialsProvider import com.wavesenterprise.sdk.node.client.blocking.credentials.SenderAddressProvider import com.wavesenterprise.sdk.node.client.blocking.node.NodeBlockingServiceFactory import com.wavesenterprise.sdk.node.client.blocking.tx.TxService +import com.wavesenterprise.sdk.node.domain.Address +import com.wavesenterprise.sdk.node.domain.Password +import com.wavesenterprise.sdk.spring.autoconfigure.contract.ContractAutoConfiguration +import com.wavesenterprise.sdk.spring.autoconfigure.node.legacy.LegacyNodeConfigurationProperties +import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.NodeConfigurationProperties +import com.wavesenterprise.sdk.spring.autoconfigure.node.properties.NodeCredentialsProperties import com.wavesenterprise.sdk.spring.autoconfigure.node.service.NodeServicesAutoConfiguration import com.wavesenterprise.sdk.tx.signer.TxSigner import com.wavesenterprise.sdk.tx.signer.node.TxServiceTxSigner import com.wavesenterprise.sdk.tx.signer.node.credentials.Credentials import com.wavesenterprise.sdk.tx.signer.node.credentials.SignCredentialsProvider +import com.wavesenterprise.sdk.tx.signer.node.factory.TxServiceTxSignerFactory import org.springframework.boot.autoconfigure.AutoConfigureAfter +import org.springframework.boot.autoconfigure.AutoConfigureBefore +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean import org.springframework.boot.autoconfigure.condition.ConditionalOnClass import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.context.annotation.Bean @@ -21,8 +31,40 @@ import org.springframework.context.annotation.Configuration TxSigner::class, ) @AutoConfigureAfter(NodeServicesAutoConfiguration::class) +@AutoConfigureBefore(ContractAutoConfiguration::class) class WeTxServiceTxSignerAutoConfiguration { + @Bean + @ConditionalOnMissingBean + @ConditionalOnBean(LegacyNodeConfigurationProperties::class) + fun defaultLegacySenderAddressProvider( + txServiceTxSignerFactory: TxServiceTxSignerFactory, + legacyNodeConfigurationProperties: LegacyNodeConfigurationProperties, + ): SenderAddressProvider { + val firstNodeCredentials = legacyNodeConfigurationProperties.getConfigForUsage() + .filter { it.value.nodeOwnerAddress != null } + .map { entry -> + Address.fromBase58(entry.value.nodeOwnerAddress!!) to + entry.value.keyStorePassword?.let { Password(it) } + }.toMap().entries.firstOrNull() + ?: throw IllegalStateException("No credentials found in 'node' properties in legacy mode") + val senderAddress = firstNodeCredentials.key + return DefaultSenderAddressProvider(senderAddress) + } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnBean(NodeConfigurationProperties::class) + fun defaultSenderAddressProvider( + txServiceTxSignerFactory: TxServiceTxSignerFactory, + nodeCredentialsProperties: NodeCredentialsProperties, + ): SenderAddressProvider { + val firstNodeCredentials = nodeCredentialsProperties.addresses.entries.firstOrNull() + ?: throw IllegalStateException("No credentials found in 'node' properties") + val senderAddress = firstNodeCredentials.key + return DefaultSenderAddressProvider(Address.fromBase58(senderAddress)) + } + @Bean @ConditionalOnMissingBean fun txSigner( diff --git a/we-autoconfigure/src/test/resources/application-test.yml b/we-autoconfigure/src/test/resources/application-test.yml index 299aa65..bbf3034 100644 --- a/we-autoconfigure/src/test/resources/application-test.yml +++ b/we-autoconfigure/src/test/resources/application-test.yml @@ -1,5 +1,4 @@ contracts: - is-enabled: false config: test-one: validation-enabled: false @@ -22,4 +21,19 @@ contracts: enabled: false contractCreatorAddress: 4L4XEpNpesX9r6rVJ8hW1TrMiNCZ6SMvRuWPKB7T47wKfnp4D84XBUv7xsa36CGwoyK3fzfojivwonHNrsX2fLBL +node: + credentials-provider: + addresses: + 3NqmRauaV87hhJPz1wzS6wx8kqWD5i7coCM: password + 3NqNVU8XpEWLR86zvGAyZ6QL4xSse1EDb7K: password + 3NoqQ88SuVBYuUimRWp3zeKLAYT66xLn5s3: password + config: + node-0: + http: + xApiKey: xApiKey + xPrivacyApiKey: xPrivacyApiKey + url: https://localhost.com + loggerLevel: FULL + read-timeout: 30000 + debug: true diff --git a/we-starters/we-starter-contract-client/build.gradle.kts b/we-starters/we-starter-contract-client/build.gradle.kts index 25db25d..4e5bd12 100644 --- a/we-starters/we-starter-contract-client/build.gradle.kts +++ b/we-starters/we-starter-contract-client/build.gradle.kts @@ -4,7 +4,6 @@ dependencies { api(project(":we-autoconfigure")) api("com.wavesenterprise:we-node-client-grpc-blocking-client") api("com.wavesenterprise:we-contract-sdk-blocking-client") - api("com.wavesenterprise:we-tx-signer-node") api("io.github.openfeign:feign-core") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm") }