diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/SignCredentialsService.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/SignCredentialsService.kt deleted file mode 100644 index 5b1955db..00000000 --- a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/SignCredentialsService.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.wavesenterprise.sdk.tx.signer.node - -import com.wavesenterprise.sdk.node.domain.Address -import com.wavesenterprise.sdk.node.domain.Password - -interface SignCredentialsService { - fun senderAddress(): Address - fun password(): Password -} diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSigner.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSigner.kt index ffbd4105..3b3e380c 100644 --- a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSigner.kt +++ b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSigner.kt @@ -4,14 +4,15 @@ import com.wavesenterprise.sdk.node.domain.blocking.tx.TxService import com.wavesenterprise.sdk.node.domain.sign.SignRequest import com.wavesenterprise.sdk.node.domain.tx.Tx import com.wavesenterprise.sdk.tx.signer.TxSigner +import com.wavesenterprise.sdk.tx.signer.node.credentials.SignCredentialsProvider class TxServiceTxSigner( private val txService: TxService, - private val signCredentialsService: SignCredentialsService, + private val signCredentialsProvider: SignCredentialsProvider, ) : TxSigner { override fun sign(signRequest: SignRequest): T = txService.sign( signRequest - .withAddress(signCredentialsService.senderAddress()) - .withPassword(signCredentialsService.password()) + .withAddress(signCredentialsProvider.credentials().senderAddress) + .withPassword(signCredentialsProvider.credentials().password) ) } diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/Credentials.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/Credentials.kt new file mode 100644 index 00000000..099ffdcb --- /dev/null +++ b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/Credentials.kt @@ -0,0 +1,9 @@ +package com.wavesenterprise.sdk.tx.signer.node.credentials + +import com.wavesenterprise.sdk.node.domain.Address +import com.wavesenterprise.sdk.node.domain.Password + +data class Credentials( + val senderAddress: Address, + val password: Password, +) diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/DefaultSignCredentialsProvider.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/DefaultSignCredentialsProvider.kt new file mode 100644 index 00000000..11638a47 --- /dev/null +++ b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/DefaultSignCredentialsProvider.kt @@ -0,0 +1,8 @@ +package com.wavesenterprise.sdk.tx.signer.node.credentials + +class DefaultSignCredentialsProvider( + private val credentials: Credentials, +) : SignCredentialsProvider { + + override fun credentials(): Credentials = credentials +} diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/SignCredentialsProvider.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/SignCredentialsProvider.kt new file mode 100644 index 00000000..dc0eb311 --- /dev/null +++ b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/SignCredentialsProvider.kt @@ -0,0 +1,5 @@ +package com.wavesenterprise.sdk.tx.signer.node.credentials + +interface SignCredentialsProvider { + fun credentials(): Credentials +} diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/SpecifiedCredentials.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/SpecifiedCredentials.kt new file mode 100644 index 00000000..a6fad8a1 --- /dev/null +++ b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/SpecifiedCredentials.kt @@ -0,0 +1,5 @@ +package com.wavesenterprise.sdk.tx.signer.node.credentials + +interface SpecifiedCredentials { + fun withCredentials(creds: Credentials, fn: () -> T): T +} diff --git a/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/ThreadLocalSignCredentialsProvider.kt b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/ThreadLocalSignCredentialsProvider.kt new file mode 100644 index 00000000..fd67afdf --- /dev/null +++ b/we-tx-signer/we-tx-signer-node/src/main/kotlin/com/wavesenterprise/sdk/tx/signer/node/credentials/ThreadLocalSignCredentialsProvider.kt @@ -0,0 +1,15 @@ +package com.wavesenterprise.sdk.tx.signer.node.credentials + +class ThreadLocalSignCredentialsProvider : SignCredentialsProvider, SpecifiedCredentials { + private val threadLocal: ThreadLocal = ThreadLocal() + + override fun withCredentials(creds: Credentials, fn: () -> T): T = + try { + threadLocal.set(creds) + fn() + } finally { + threadLocal.remove() + } + + override fun credentials(): Credentials = requireNotNull(threadLocal.get()) { "Credentials haven't been set" } +} diff --git a/we-tx-signer/we-tx-signer-node/src/test/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSignerTest.kt b/we-tx-signer/we-tx-signer-node/src/test/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSignerTest.kt index 6cc42903..2d704d0c 100644 --- a/we-tx-signer/we-tx-signer-node/src/test/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSignerTest.kt +++ b/we-tx-signer/we-tx-signer-node/src/test/kotlin/com/wavesenterprise/sdk/tx/signer/node/TxServiceTxSignerTest.kt @@ -6,6 +6,8 @@ import com.wavesenterprise.sdk.node.test.data.TestDataFactory.Companion.address import com.wavesenterprise.sdk.node.test.data.TestDataFactory.Companion.createContractSignRequest import com.wavesenterprise.sdk.node.test.data.TestDataFactory.Companion.createContractTx import com.wavesenterprise.sdk.node.test.data.TestDataFactory.Companion.password +import com.wavesenterprise.sdk.tx.signer.node.credentials.Credentials +import com.wavesenterprise.sdk.tx.signer.node.credentials.SignCredentialsProvider import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.junit5.MockKExtension @@ -21,19 +23,18 @@ internal class TxServiceTxSignerTest { lateinit var txServiceTxSigner: TxServiceTxSigner @MockK - lateinit var signCredentialsService: SignCredentialsService + lateinit var signCredentialsProvider: SignCredentialsProvider @MockK lateinit var txService: TxService @BeforeEach fun initMocks() { - every { signCredentialsService.senderAddress() } returns senderAddress - every { signCredentialsService.password() } returns password + every { signCredentialsProvider.credentials() } returns credentials txServiceTxSigner = TxServiceTxSigner( txService = txService, - signCredentialsService = signCredentialsService, + signCredentialsProvider = signCredentialsProvider, ) } @@ -51,7 +52,9 @@ internal class TxServiceTxSignerTest { } companion object { - val senderAddress = address() - val password = password() + private val senderAddress = address() + private val password = password() + + val credentials = Credentials(senderAddress, password) } }