Skip to content

Commit

Permalink
Merge branch 'feature/WTCH-185' into 'dev'
Browse files Browse the repository at this point in the history
[WTCH-185] Add signMessage, verifyMessageSignature to AddressService

Closes WTCH-185

See merge request development/we/java-sdk/we-node-client!81
  • Loading branch information
ashalaginov committed Nov 14, 2023
2 parents 9bdf4af + ee407ef commit 873e524
Show file tree
Hide file tree
Showing 35 changed files with 845 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package com.wavesenterprise.sdk.node.client.blocking.address

import com.wavesenterprise.sdk.node.domain.Address
import com.wavesenterprise.sdk.node.domain.DataEntry
import com.wavesenterprise.sdk.node.domain.DataKey
import com.wavesenterprise.sdk.node.domain.PublicKey
import com.wavesenterprise.sdk.node.domain.address.SignMessageRequest
import com.wavesenterprise.sdk.node.domain.address.SignMessageResponse
import com.wavesenterprise.sdk.node.domain.address.VerifyMessageSignatureRequest
import com.wavesenterprise.sdk.node.domain.address.VerifyMessageSignatureResponse
import java.util.Optional

interface AddressService {
fun getAddresses(): List<Address>
fun getAddressByPublicKey(publicKey: String): Address
fun getAddressValue(address: String, key: String): DataEntry?
fun getAddressValues(address: String): List<DataEntry>
fun getAddressByPublicKey(publicKey: PublicKey): Address
fun getAddressValue(address: Address, key: DataKey): Optional<DataEntry>
fun getAddressValues(address: Address): List<DataEntry>
fun signMessage(address: Address, request: SignMessageRequest): SignMessageResponse
fun verifyMessageSignature(address: Address, request: VerifyMessageSignatureRequest): VerifyMessageSignatureResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.wavesenterprise.sdk.node.exception.NodeInternalServerErrorException
import com.wavesenterprise.sdk.node.exception.NodeServiceUnavailableException
import com.wavesenterprise.sdk.node.exception.specific.ContractNotFoundException
import com.wavesenterprise.sdk.node.exception.specific.PolicyItemDataIsMissingException
import com.wavesenterprise.sdk.node.test.data.TestDataFactory.Companion.address
import io.mockk.Called
import io.mockk.every
import io.mockk.verify
Expand Down Expand Up @@ -355,7 +356,7 @@ class LoadBalancingServiceFactoryTest {
mockkAddressService2.getAddressValues(any())
} throws NodeServiceUnavailableException(cause = Exception())

val address = "address"
val address = address()
repeat(TESTS) { lb.addressService().getAddressValues(address) }
verify(atLeast = 50) { mockkAddressService1.getAddressValues(address) }
verify(atLeast = 1) { mockkAddressService2.getAddressValues(address) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wavesenterprise.sdk.node.domain.address

data class Message(val value: String) {
companion object {
@JvmStatic
fun fromString(value: String): Message =
Message(value)

inline val String.message: Message get() = Message(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wavesenterprise.sdk.node.domain.address

import com.wavesenterprise.sdk.node.domain.Password

data class SignMessageRequest(
val message: Message,
val password: Password,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wavesenterprise.sdk.node.domain.address

import com.wavesenterprise.sdk.node.domain.PublicKey
import com.wavesenterprise.sdk.node.domain.Signature

data class SignMessageResponse(
val message: Message,
val publicKey: PublicKey,
val signature: Signature,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wavesenterprise.sdk.node.domain.address

import com.wavesenterprise.sdk.node.domain.PublicKey
import com.wavesenterprise.sdk.node.domain.Signature

data class VerifyMessageSignatureRequest(
val message: Message,
val publicKey: PublicKey,
val signature: Signature,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wavesenterprise.sdk.node.domain.address

data class VerifyMessageSignatureResponse(
val valid: Boolean,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.wavesenterprise.sdk.node.exception

@Suppress("MagicNumber")
enum class NodeErrorCode(val code: Int) {
INVALID_SIGNATURE(101),
INVALID_ADDRESS(102),
INVALID_PUBLIC_KEY(108),
INVALID_PASSWORD(115),
CUSTOM_VALIDATION_ERROR(199),
DATA_KEY_NOT_EXISTS(304),
CONTRACT_NOT_FOUND(600),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wavesenterprise.sdk.node.exception.specific

import com.wavesenterprise.sdk.node.exception.NodeError
import com.wavesenterprise.sdk.node.exception.NodeErrorInfoHolder
import com.wavesenterprise.sdk.node.exception.NodeException

class InvalidPasswordException(
override val nodeError: NodeError,
override val cause: Exception,
) : NodeException(cause), NodeErrorInfoHolder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wavesenterprise.sdk.node.exception.specific

import com.wavesenterprise.sdk.node.exception.NodeError
import com.wavesenterprise.sdk.node.exception.NodeErrorInfoHolder
import com.wavesenterprise.sdk.node.exception.NodeException

class InvalidPublicKeyException(
override val nodeError: NodeError,
override val cause: Exception,
) : NodeException(cause), NodeErrorInfoHolder
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.wavesenterprise.sdk.node.exception.NodeErrorCode.CONTRACT_NOT_FOUND
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.CUSTOM_VALIDATION_ERROR
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.DATA_KEY_NOT_EXISTS
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.INVALID_ADDRESS
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.INVALID_PASSWORD
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.INVALID_PUBLIC_KEY
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.INVALID_SIGNATURE
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.POLICY_DOES_NOT_EXIST
import com.wavesenterprise.sdk.node.exception.NodeErrorCode.POLICY_ITEM_DATA_IS_MISSING
Expand All @@ -21,6 +23,8 @@ object SpecificExceptionMapper {
when (code) {
INVALID_SIGNATURE -> InvalidSignatureException(nodeError, cause = exception)
INVALID_ADDRESS -> InvalidAddressException(nodeError, cause = exception)
INVALID_PUBLIC_KEY -> InvalidPublicKeyException(nodeError, cause = exception)
INVALID_PASSWORD -> InvalidPasswordException(nodeError, cause = exception)
CUSTOM_VALIDATION_ERROR -> CustomValidationErrorException(nodeError, cause = exception)
DATA_KEY_NOT_EXISTS -> DataKeyNotExistsException(nodeError, cause = exception)
CONTRACT_NOT_FOUND -> ContractNotFoundException(nodeError, cause = exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@ package com.wavesenterprise.sdk.node.client.grpc.blocking.node
import com.wavesenterprise.sdk.node.client.blocking.address.AddressService
import com.wavesenterprise.sdk.node.domain.Address
import com.wavesenterprise.sdk.node.domain.DataEntry
import com.wavesenterprise.sdk.node.domain.DataKey
import com.wavesenterprise.sdk.node.domain.PublicKey
import com.wavesenterprise.sdk.node.domain.address.SignMessageRequest
import com.wavesenterprise.sdk.node.domain.address.SignMessageResponse
import com.wavesenterprise.sdk.node.domain.address.VerifyMessageSignatureRequest
import com.wavesenterprise.sdk.node.domain.address.VerifyMessageSignatureResponse
import java.util.Optional

class AddressGrpcBlockingService : AddressService {
override fun getAddresses(): List<Address> {
TODO("Not yet implemented")
}

override fun getAddressByPublicKey(publicKey: String): Address {
override fun getAddressByPublicKey(publicKey: PublicKey): Address {
TODO("Not yet implemented")
}

override fun getAddressValue(address: String, key: String): DataEntry? {
override fun getAddressValue(address: Address, key: DataKey): Optional<DataEntry> {
TODO("Not yet implemented")
}

override fun getAddressValues(address: String): List<DataEntry> {
override fun getAddressValues(address: Address): List<DataEntry> {
TODO("Not yet implemented")
}

override fun signMessage(
address: Address,
request: SignMessageRequest
): SignMessageResponse {
TODO("Not yet implemented")
}

override fun verifyMessageSignature(
address: Address,
request: VerifyMessageSignatureRequest
): VerifyMessageSignatureResponse {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.wavesenterprise.sdk.node.client.feign.address

import com.wavesenterprise.sdk.node.client.blocking.address.AddressService
import com.wavesenterprise.sdk.node.client.http.DataEntryDto.Companion.toDomain
import com.wavesenterprise.sdk.node.client.http.address.AddressDto.Companion.toDomain
import com.wavesenterprise.sdk.node.client.http.address.SignMessageRequestDto.Companion.toDto
import com.wavesenterprise.sdk.node.client.http.address.SignMessageResponseDto.Companion.toDomain
import com.wavesenterprise.sdk.node.client.http.address.VerifyMessageSignatureRequestDto.Companion.toDto
import com.wavesenterprise.sdk.node.client.http.address.VerifyMessageSignatureResponseDto.Companion.toDomain
import com.wavesenterprise.sdk.node.domain.Address
import com.wavesenterprise.sdk.node.domain.DataEntry
import com.wavesenterprise.sdk.node.domain.DataKey
import com.wavesenterprise.sdk.node.domain.PublicKey
import com.wavesenterprise.sdk.node.domain.address.SignMessageRequest
import com.wavesenterprise.sdk.node.domain.address.SignMessageResponse
import com.wavesenterprise.sdk.node.domain.address.VerifyMessageSignatureRequest
import com.wavesenterprise.sdk.node.domain.address.VerifyMessageSignatureResponse
import com.wavesenterprise.sdk.node.exception.specific.DataKeyNotExistsException
import java.util.Optional

class FeignAddressService(
private val weAddressServiceApiFeign: WeAddressServiceApiFeign,
) : AddressService {
override fun getAddresses(): List<Address> =
weAddressServiceApiFeign.getAddresses().map { it.toDomain() }

override fun getAddressByPublicKey(publicKey: PublicKey): Address =
weAddressServiceApiFeign.getAddressByPublicKey(publicKey.asBase58String()).toDomain()

override fun getAddressValue(address: Address, key: DataKey): Optional<DataEntry> =
try {
weAddressServiceApiFeign.getAddressValue(
address = address.asBase58String(),
key = key.value,
).map { it.toDomain() }
} catch (ex: DataKeyNotExistsException) {
Optional.empty()
}

override fun getAddressValues(address: Address): List<DataEntry> =
weAddressServiceApiFeign.getAddressValues(address.asBase58String()).map { it.toDomain() }

override fun signMessage(
address: Address,
request: SignMessageRequest
): SignMessageResponse =
weAddressServiceApiFeign.signMessage(
address = address.asBase58String(),
request = request.toDto(),
).toDomain()

override fun verifyMessageSignature(
address: Address,
request: VerifyMessageSignatureRequest
): VerifyMessageSignatureResponse =
weAddressServiceApiFeign.verifyMessageSignature(
address = address.asBase58String(),
request = request.toDto(),
).toDomain()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.wavesenterprise.sdk.node.client.feign.address

import com.wavesenterprise.sdk.node.client.http.DataEntryDto
import com.wavesenterprise.sdk.node.client.http.address.AddressDto
import com.wavesenterprise.sdk.node.client.http.address.SignMessageRequestDto
import com.wavesenterprise.sdk.node.client.http.address.SignMessageResponseDto
import com.wavesenterprise.sdk.node.client.http.address.VerifyMessageSignatureRequestDto
import com.wavesenterprise.sdk.node.client.http.address.VerifyMessageSignatureResponseDto
import feign.Headers
import feign.Param
import feign.RequestLine
import java.util.Optional

interface WeAddressServiceApiFeign {

@RequestLine("GET /addresses")
fun getAddresses(): List<String>

@RequestLine("GET /addresses/publicKey/{publicKey}")
fun getAddressByPublicKey(@Param("publicKey") publicKey: String): AddressDto

@RequestLine("GET /addresses/data/{address}/{key}")
fun getAddressValue(
@Param("address") address: String,
@Param("key") key: String
): Optional<DataEntryDto>

@RequestLine("GET /addresses/data/{address}")
fun getAddressValues(@Param("address") address: String): List<DataEntryDto>

@Headers("Content-Type: application/json")
@RequestLine("POST /addresses/sign/{address}")
fun signMessage(
@Param("address") address: String,
request: SignMessageRequestDto,
): SignMessageResponseDto

@Headers("Content-Type: application/json")
@RequestLine("POST /addresses/verify/{address}")
fun verifyMessageSignature(
@Param("address") address: String,
request: VerifyMessageSignatureRequestDto,
): VerifyMessageSignatureResponseDto
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import com.wavesenterprise.sdk.node.client.blocking.tx.TxService
import com.wavesenterprise.sdk.node.client.blocking.util.NodeUtilsService
import com.wavesenterprise.sdk.node.client.feign.FeignNodeClientParams
import com.wavesenterprise.sdk.node.client.feign.FeignWeApiFactory
import com.wavesenterprise.sdk.node.client.feign.address.FeignAddressService
import com.wavesenterprise.sdk.node.client.feign.address.WeAddressServiceApiFeign
import com.wavesenterprise.sdk.node.client.feign.blocks.FeignBlocksService
import com.wavesenterprise.sdk.node.client.feign.blocks.WeBlocksServiceApiFeign
import com.wavesenterprise.sdk.node.client.feign.contract.FeignContractService
import com.wavesenterprise.sdk.node.client.feign.contract.WeContractServiceApiFeign
import com.wavesenterprise.sdk.node.client.feign.node.FeignAddressService
import com.wavesenterprise.sdk.node.client.feign.node.FeignNodeInfoService
import com.wavesenterprise.sdk.node.client.feign.node.WeAddressServiceApiFeign
import com.wavesenterprise.sdk.node.client.feign.node.WeNodeInfoServiceApiFeign
import com.wavesenterprise.sdk.node.client.feign.pki.FeignPkiService
import com.wavesenterprise.sdk.node.client.feign.pki.WePkiServiceApiFeign
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 873e524

Please sign in to comment.