use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.
the class TransactionResource4Test method sendWithMandatoryRecipients.
@Test
public void sendWithMandatoryRecipients() {
final Base64.Encoder base64Encoder = Base64.getEncoder();
final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
final String base64Hash = "yKNxAAPdBMiEZFkyQifH1PShwHTHTdE92T3hAfSQ3RtGce9IB8jrsrXxGuCe+Vu3Wyv2zgSbUnt+QBN2Rf48qQ==";
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
sendRequest.setTo(base64Key);
sendRequest.setPrivacyFlag(2);
sendRequest.setMandatoryRecipients(new String[] { base64Key });
sendRequest.setAffectedContractTransactions(base64Hash);
final PublicKey sender = mock(PublicKey.class);
when(transactionManager.defaultPublicKey()).thenReturn(sender);
final com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
final MessageHash messageHash = mock(MessageHash.class);
final byte[] txnData = "TxnData".getBytes();
when(messageHash.getHashBytes()).thenReturn(txnData);
when(sendResponse.getTransactionHash()).thenReturn(messageHash);
when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
final Response result = transactionResource.send(sendRequest);
assertThat(result.getStatus()).isEqualTo(201);
assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
SendResponse resultSendResponse = (SendResponse) result.getEntity();
assertThat(resultSendResponse.getKey());
ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
verify(transactionManager).send(argumentCaptor.capture());
verify(transactionManager).defaultPublicKey();
com.quorum.tessera.transaction.SendRequest businessObject = argumentCaptor.getValue();
assertThat(businessObject).isNotNull();
assertThat(businessObject.getPayload()).isEqualTo(sendRequest.getPayload());
assertThat(businessObject.getSender()).isEqualTo(sender);
assertThat(businessObject.getRecipients()).hasSize(1);
assertThat(businessObject.getRecipients().get(0).encodeToBase64()).isEqualTo(base64Key);
assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.MANDATORY_RECIPIENTS);
assertThat(businessObject.getAffectedContractTransactions()).hasSize(1);
final MessageHash hash = businessObject.getAffectedContractTransactions().iterator().next();
assertThat(Base64.getEncoder().encodeToString(hash.getHashBytes())).isEqualTo(base64Hash);
assertThat(businessObject.getMandatoryRecipients().iterator().next().encodeToBase64()).isEqualTo(base64Key);
}
use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.
the class TransactionResourceTest method sendSignedTransactionNullRecipients.
@Test
public void sendSignedTransactionNullRecipients() 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.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
Response result = transactionResource.sendSignedTransactionStandard(null, "signedTxData".getBytes());
// jersey.target("sendsignedtx")
// .request()
// .header("c11n-to", null)
// .post(Entity.entity("signedTxData".getBytes(),
// MediaType.APPLICATION_OCTET_STREAM_TYPE));
assertThat(result.getStatus()).isEqualTo(200);
assertThat(result.getEntity()).isEqualTo(Base64.getEncoder().encodeToString("KEY".getBytes()));
verify(transactionManager).sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class));
}
use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.
the class BesuTransactionResource method send.
@Hidden
@POST
@Path("send")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
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> optionalPrivacyGroup = Optional.ofNullable(sendRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
final List<PublicKey> recipientList = optionalPrivacyGroup.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 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);
optionalPrivacyGroup.ifPresentOrElse(requestBuilder::withPrivacyGroupId, () -> {
PrivacyGroup legacyGroup = privacyGroupManager.createLegacyPrivacyGroup(sender, recipientList);
requestBuilder.withPrivacyGroupId(legacyGroup.getId());
});
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 SendResponse sendResponse = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).map(messageHash -> new SendResponse(messageHash, null, null)).get();
final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedKey, StandardCharsets.UTF_8)).build();
return Response.status(Response.Status.OK).type(APPLICATION_JSON).location(location).entity(sendResponse).build();
}
use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.
the class EncodedPayloadResource method decryptEncodedPayload.
// hide this operation from swagger generation; the /encodedpayload/decrypt operation is
// overloaded and must be documented in a single place
@Hidden
@POST
@Path("decrypt")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response decryptEncodedPayload(@Valid @NotNull final PayloadDecryptRequest request) {
LOGGER.info("Received request to decrypt custom transaction");
final Base64.Decoder decoder = Base64.getDecoder();
final Map<TxHash, byte[]> affectedTxns = request.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> TxHash.from(decoder.decode(e.getKey())), e -> decoder.decode(e.getValue())));
final EncodedPayload requestAsPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(request.getSenderKey())).withCipherText(request.getCipherText()).withCipherTextNonce(request.getCipherTextNonce()).withRecipientBoxes(request.getRecipientBoxes()).withRecipientNonce(request.getRecipientNonce()).withRecipientKeys(request.getRecipientKeys().stream().map(PublicKey::from).collect(Collectors.toList())).withPrivacyFlag(request.getPrivacyMode()).withAffectedContractTransactions(affectedTxns).withExecHash(request.getExecHash()).build();
final com.quorum.tessera.transaction.ReceiveResponse response = encodedPayloadManager.decrypt(requestAsPayload, null);
final ReceiveResponse receiveResponse = new ReceiveResponse();
receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
receiveResponse.setPayload(response.getUnencryptedTransactionData());
receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(Base64.getEncoder()::encodeToString).toArray(String[]::new));
Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
return Response.ok(receiveResponse).type(APPLICATION_JSON).build();
}
use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.
the class EncodedPayloadResource method receive21.
// path /encodedpayload/decrypt is overloaded (application/json and
// application/vnd.tessera-2.1+json); swagger annotations cannot handle situations like this so
// this operation documents both
@POST
@Path("decrypt")
@Operation(summary = "/encodedpayload/decrypt", operationId = "decrypt", description = "decrypt an encrypted payload and return the result; does not store to the database or push to peers", requestBody = @RequestBody(content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PayloadDecryptRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = PayloadDecryptRequest.class)) }))
@ApiResponse(responseCode = "200", description = "decrypted payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ReceiveResponse.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = ReceiveResponse.class)) })
@Consumes(MIME_TYPE_JSON_2_1)
@Produces(MIME_TYPE_JSON_2_1)
public Response receive21(@Valid @NotNull final PayloadDecryptRequest request) {
LOGGER.info("Received request to decrypt custom transaction");
final Base64.Decoder decoder = Base64.getDecoder();
final Map<TxHash, byte[]> affectedTxns = request.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> TxHash.from(decoder.decode(e.getKey())), e -> decoder.decode(e.getValue())));
final EncodedPayload requestAsPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(request.getSenderKey())).withCipherText(request.getCipherText()).withCipherTextNonce(request.getCipherTextNonce()).withRecipientBoxes(request.getRecipientBoxes()).withRecipientNonce(request.getRecipientNonce()).withRecipientKeys(request.getRecipientKeys().stream().map(PublicKey::from).collect(Collectors.toList())).withPrivacyFlag(request.getPrivacyMode()).withAffectedContractTransactions(affectedTxns).withExecHash(request.getExecHash()).build();
final com.quorum.tessera.transaction.ReceiveResponse response = encodedPayloadManager.decrypt(requestAsPayload, null);
final ReceiveResponse receiveResponse = new ReceiveResponse();
receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
receiveResponse.setPayload(response.getUnencryptedTransactionData());
receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(Base64.getEncoder()::encodeToString).toArray(String[]::new));
Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
return Response.ok(receiveResponse).type(MIME_TYPE_JSON_2_1).build();
}
Aggregations