Search in sources :

Example 26 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class TransactionResource4 method getMandatoryRecipients.

@Operation(summary = "/transaction/{hash}/mandatory", operationId = "getMandatoryRecipients", description = "get list of mandatory recipient public keys for a transaction")
@ApiResponse(responseCode = "200", description = "comma-separated list of mandatory recipients", content = @Content(schema = @Schema(type = "string", description = "comma-separated list of mandatory recipients"), examples = @ExampleObject("ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc=,BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=")))
@GET
@Path("/transaction/{hash}/mandatory")
@Produces(TEXT_PLAIN)
public Response getMandatoryRecipients(@Parameter(description = "hash indicating encrypted payload to get mandatory recipients for", schema = @Schema(format = "base64")) @PathParam("hash") final String ptmHash) {
    LOGGER.debug("Received mandatory recipients list API request for key {}", ptmHash);
    MessageHash transactionHash = Optional.of(ptmHash).map(Base64.getDecoder()::decode).map(MessageHash::new).get();
    final String mandatoryRecipients = transactionManager.getMandatoryRecipients(transactionHash).stream().map(PublicKey::encodeToBase64).collect(Collectors.joining(","));
    return Response.ok(mandatoryRecipients).build();
}
Also used : MessageHash(com.quorum.tessera.data.MessageHash) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 27 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class TransactionResource4 method sendSignedTransaction.

@POST
@Path("sendsignedtx")
@Consumes({ MIME_TYPE_JSON_4 })
@Produces({ MIME_TYPE_JSON_4 })
public Response sendSignedTransaction(@NotNull @Valid @PrivacyValid final SendSignedRequest sendSignedRequest) {
    final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendSignedRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipients = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Optional.ofNullable(sendSignedRequest.getTo()).stream().flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
    final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendSignedRequest.getPrivacyFlag());
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendSignedRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendSignedRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final Set<PublicKey> mandatoryRecipients = Stream.ofNullable(sendSignedRequest.getMandatoryRecipients()).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toUnmodifiableSet());
    final com.quorum.tessera.transaction.SendSignedRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendSignedRequest.Builder.create().withSignedData(sendSignedRequest.getHash()).withRecipients(recipients).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withExecHash(execHash).withMandatoryRecipients(mandatoryRecipients);
    privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
    final com.quorum.tessera.transaction.SendResponse response = transactionManager.sendSignedTransaction(requestBuilder.build());
    final String encodedTransactionHash = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedTransactionHash, StandardCharsets.UTF_8)).build();
    final String[] managedParties = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getManagedParties).orElse(Collections.emptySet()).stream().map(PublicKey::encodeToBase64).toArray(String[]::new);
    final SendResponse responseEntity = new SendResponse();
    responseEntity.setKey(encodedTransactionHash);
    responseEntity.setManagedParties(managedParties);
    responseEntity.setSenderKey(response.getSender().encodeToBase64());
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    return Response.created(location).entity(responseEntity).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest)

Example 28 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class TransactionResource4 method send.

@POST
@Path("send")
@Consumes({ MIME_TYPE_JSON_4 })
@Produces({ MIME_TYPE_JSON_4 })
public Response send(@NotNull @Valid @PrivacyValid final SendRequest sendRequest) {
    final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
    final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipientList = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Stream.of(sendRequest).filter(sr -> Objects.nonNull(sr.getTo())).flatMap(s -> Stream.of(s.getTo())).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendRequest.getPrivacyFlag());
    final Set<PublicKey> mandatoryRecipients = Stream.ofNullable(sendRequest.getMandatoryRecipients()).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toUnmodifiableSet());
    final com.quorum.tessera.transaction.SendRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withMandatoryRecipients(mandatoryRecipients);
    privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
    final com.quorum.tessera.transaction.SendResponse response = transactionManager.send(requestBuilder.build());
    final String encodedKey = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    final String[] managedParties = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getManagedParties).orElse(Collections.emptySet()).stream().map(PublicKey::encodeToBase64).toArray(String[]::new);
    final SendResponse sendResponse = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).map(messageHash -> new SendResponse(messageHash, managedParties, sender.encodeToBase64())).get();
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedKey, StandardCharsets.UTF_8)).build();
    return Response.created(location).entity(sendResponse).build();
}
Also used : SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) SendResponse(com.quorum.tessera.api.SendResponse) LoggerFactory(org.slf4j.LoggerFactory) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) TEXT_PLAIN(jakarta.ws.rs.core.MediaType.TEXT_PLAIN) PrivacyValid(com.quorum.tessera.api.constraint.PrivacyValid) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) MIME_TYPE_JSON_4(com.quorum.tessera.version.MandatoryRecipientsVersion.MIME_TYPE_JSON_4) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) URI(java.net.URI) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Logger(org.slf4j.Logger) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) TransactionManager(com.quorum.tessera.transaction.TransactionManager) PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Parameter(io.swagger.v3.oas.annotations.Parameter) URLEncoder(java.net.URLEncoder) SendRequest(com.quorum.tessera.api.SendRequest) Stream(java.util.stream.Stream) UriBuilder(jakarta.ws.rs.core.UriBuilder) Tag(io.swagger.v3.oas.annotations.tags.Tag) SendRequest(com.quorum.tessera.api.SendRequest) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode)

Example 29 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class TransactionResourceTest method sendRawNullRecipient.

@Test
public void sendRawNullRecipient() throws UnsupportedEncodingException {
    byte[] txnData = "KEY".getBytes();
    com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
    MessageHash messageHash = mock(MessageHash.class);
    when(messageHash.getHashBytes()).thenReturn(txnData);
    when(sendResponse.getTransactionHash()).thenReturn(messageHash);
    when(transactionManager.defaultPublicKey()).thenReturn(mock(PublicKey.class));
    when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
    Response result = transactionResource.sendRaw("", "", "foo".getBytes());
    // jersey.target("sendraw")
    // .request()
    // .header("c11n-from", "")
    // .header("c11n-to", null)
    // .post(Entity.entity("foo".getBytes(),
    // MediaType.APPLICATION_OCTET_STREAM_TYPE));
    assertThat(result.getStatus()).isEqualTo(200);
    assertThat(result.getEntity()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
    verify(transactionManager).send(any(com.quorum.tessera.transaction.SendRequest.class));
    verify(transactionManager).defaultPublicKey();
}
Also used : Response(jakarta.ws.rs.core.Response) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 30 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class TransactionResourceTest method sendRaw.

@Test
public void sendRaw() throws UnsupportedEncodingException {
    byte[] txnData = "KEY".getBytes();
    com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
    MessageHash messageHash = mock(MessageHash.class);
    when(messageHash.getHashBytes()).thenReturn(txnData);
    when(sendResponse.getTransactionHash()).thenReturn(messageHash);
    when(transactionManager.defaultPublicKey()).thenReturn(mock(PublicKey.class));
    when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
    Response result = transactionResource.sendRaw("", "someone", "foo".getBytes());
    // jersey.target("sendraw")
    // .request()
    // .header("c11n-from", "")
    // .header("c11n-to", "someone")
    // .post(Entity.entity("foo".getBytes(),
    // MediaType.APPLICATION_OCTET_STREAM_TYPE));
    assertThat(result.getStatus()).isEqualTo(200);
    assertThat(result.getEntity()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
    verify(transactionManager).send(any(com.quorum.tessera.transaction.SendRequest.class));
    verify(transactionManager).defaultPublicKey();
}
Also used : Response(jakarta.ws.rs.core.Response) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Aggregations

MessageHash (com.quorum.tessera.data.MessageHash)81 PublicKey (com.quorum.tessera.encryption.PublicKey)56 Test (org.junit.Test)51 Response (jakarta.ws.rs.core.Response)44 Operation (io.swagger.v3.oas.annotations.Operation)21 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)21 SendResponse (com.quorum.tessera.api.SendResponse)17 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)16 SendRequest (com.quorum.tessera.api.SendRequest)15 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)15 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)15 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)13 java.util (java.util)13 Collectors (java.util.stream.Collectors)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)12 TransactionManager (com.quorum.tessera.transaction.TransactionManager)12 Tag (io.swagger.v3.oas.annotations.tags.Tag)11 Valid (jakarta.validation.Valid)11