Skip to content

Commit

Permalink
Merge branch 'feature/WTCH-78' into 'dev'
Browse files Browse the repository at this point in the history
[WTCH-78] Edit SignCredentialsProvider for use thread local

Closes WTCH-78

See merge request waves-enterprise/java-sdk/we-node-client!12
  • Loading branch information
Daniil Georgiev committed Sep 5, 2022
2 parents a2f62f9 + f95ee59 commit 0170ebc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 18 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Tx> sign(signRequest: SignRequest<T>): T = txService.sign(
signRequest
.withAddress(signCredentialsService.senderAddress())
.withPassword(signCredentialsService.password())
.withAddress(signCredentialsProvider.credentials().senderAddress)
.withPassword(signCredentialsProvider.credentials().password)
)
}
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wavesenterprise.sdk.tx.signer.node.credentials

class DefaultSignCredentialsProvider(
private val credentials: Credentials,
) : SignCredentialsProvider {

override fun credentials(): Credentials = credentials
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wavesenterprise.sdk.tx.signer.node.credentials

interface SignCredentialsProvider {
fun credentials(): Credentials
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wavesenterprise.sdk.tx.signer.node.credentials

interface SpecifiedCredentials {
fun <T> withCredentials(creds: Credentials, fn: () -> T): T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.wavesenterprise.sdk.tx.signer.node.credentials

class ThreadLocalSignCredentialsProvider : SignCredentialsProvider, SpecifiedCredentials {
private val threadLocal: ThreadLocal<Credentials> = ThreadLocal()

override fun <T> 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" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
}

Expand All @@ -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)
}
}

0 comments on commit 0170ebc

Please sign in to comment.