use of com.quorum.tessera.api.SendResponse 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();
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class BesuTransactionResourceTest method send.
@Test
public void send() {
final Base64.Encoder base64Encoder = Base64.getEncoder();
final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
sendRequest.setTo(base64Key);
final PublicKey sender = mock(PublicKey.class);
when(transactionManager.defaultPublicKey()).thenReturn(sender);
final byte[] recipientKeyBytes = Base64.getDecoder().decode(base64Key);
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);
PrivacyGroup legacy = mock(PrivacyGroup.class);
when(legacy.getId()).thenReturn(PrivacyGroup.Id.fromBytes("group".getBytes()));
when(privacyGroupManager.createLegacyPrivacyGroup(eq(sender), eq(List.of(PublicKey.from(recipientKeyBytes))))).thenReturn(legacy);
final Response result = besuTransactionResource.send(sendRequest);
// jersey.target("send").request().post(Entity.entity(sendRequest,
// MediaType.APPLICATION_JSON));
assertThat(result.getStatus()).isEqualTo(200);
assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
SendResponse resultSendResponse = (SendResponse) result.getEntity();
assertThat(resultSendResponse.getKey()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
verify(transactionManager).send(argumentCaptor.capture());
verify(transactionManager).defaultPublicKey();
verify(privacyGroupManager).createLegacyPrivacyGroup(eq(sender), eq(List.of(PublicKey.from(recipientKeyBytes))));
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.STANDARD_PRIVATE);
assertThat(businessObject.getAffectedContractTransactions()).isEmpty();
assertThat(businessObject.getExecHash()).isEmpty();
assertThat(businessObject.getPrivacyGroupId()).isPresent().get().isEqualTo(PrivacyGroup.Id.fromBytes("group".getBytes()));
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource3Test method sendSignedTransactionWithPrivacy.
@Test
public void sendSignedTransactionWithPrivacy() {
final PublicKey sender = PublicKey.from(Base64.getDecoder().decode("QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc="));
com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
byte[] transactionHashData = "I Love Sparrows".getBytes();
final String base64EncodedTransactionHAshData = Base64.getEncoder().encodeToString(transactionHashData);
MessageHash transactionHash = mock(MessageHash.class);
when(transactionHash.getHashBytes()).thenReturn(transactionHashData);
when(sendResponse.getTransactionHash()).thenReturn(transactionHash);
when(sendResponse.getSender()).thenReturn(sender);
when(transactionManager.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
final String base64AffectedHash1 = Base64.getEncoder().encodeToString("aHash1".getBytes());
final String base64AffectedHash2 = Base64.getEncoder().encodeToString("aHash2".getBytes());
SendSignedRequest sendSignedRequest = new SendSignedRequest();
sendSignedRequest.setHash("SOMEDATA".getBytes());
sendSignedRequest.setTo("recipient1", "recipient2");
sendSignedRequest.setPrivacyFlag(3);
sendSignedRequest.setAffectedContractTransactions(base64AffectedHash1, base64AffectedHash2);
sendSignedRequest.setExecHash("execHash");
Response result = transactionResource.sendSignedTransaction(sendSignedRequest);
// jersey.target("sendsignedtx")
// .request()
// .post(Entity.entity(sendSignedRequest,
// "application/vnd.tessera-2.1+json"));
assertThat(result.getStatus()).isEqualTo(201);
SendResponse resultResponse = (SendResponse) result.getEntity();
assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
assertThat(resultResponse.getSenderKey()).isEqualTo(sender.encodeToBase64());
assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
verify(transactionManager).sendSignedTransaction(argumentCaptor.capture());
com.quorum.tessera.transaction.SendSignedRequest obj = argumentCaptor.getValue();
assertThat(obj).isNotNull();
assertThat(obj.getSignedData()).isEqualTo("SOMEDATA".getBytes());
assertThat(obj.getRecipients()).hasSize(2);
assertThat(obj.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
assertThat(obj.getAffectedContractTransactions().stream().map(MessageHash::toString)).hasSize(2).containsExactlyInAnyOrder(base64AffectedHash1, base64AffectedHash2);
assertThat(obj.getExecHash()).isEqualTo("execHash".getBytes());
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource3Test method sendWithPrivacy.
@Test
public void sendWithPrivacy() {
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(3);
sendRequest.setAffectedContractTransactions(base64Hash);
sendRequest.setExecHash("executionHash");
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.PRIVATE_STATE_VALIDATION);
assertThat(businessObject.getAffectedContractTransactions()).hasSize(1);
final MessageHash hash = businessObject.getAffectedContractTransactions().iterator().next();
assertThat(Base64.getEncoder().encodeToString(hash.getHashBytes())).isEqualTo(base64Hash);
assertThat(businessObject.getExecHash()).isEqualTo("executionHash".getBytes());
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource3Test method sendSignedTransaction.
@Test
public void sendSignedTransaction() {
final PublicKey sender = PublicKey.from(Base64.getDecoder().decode("QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc="));
com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
byte[] transactionHashData = "I Love Sparrows".getBytes();
final String base64EncodedTransactionHAshData = Base64.getEncoder().encodeToString(transactionHashData);
MessageHash transactionHash = mock(MessageHash.class);
when(transactionHash.getHashBytes()).thenReturn(transactionHashData);
when(sendResponse.getTransactionHash()).thenReturn(transactionHash);
when(sendResponse.getManagedParties()).thenReturn(Set.of(sender));
when(sendResponse.getSender()).thenReturn(sender);
when(transactionManager.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
SendSignedRequest sendSignedRequest = new SendSignedRequest();
sendSignedRequest.setHash("SOMEDATA".getBytes());
sendSignedRequest.setTo("recipient1", "recipient2");
Response result = transactionResource.sendSignedTransaction(sendSignedRequest);
// jersey.target("sendsignedtx")
// .request()
// .post(Entity.entity(sendSignedRequest,
// "application/vnd.tessera-2.1+json"));
assertThat(result.getStatus()).isEqualTo(201);
SendResponse resultResponse = (SendResponse) result.getEntity();
assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
assertThat(resultResponse.getManagedParties()).containsExactlyInAnyOrder(sender.encodeToBase64());
assertThat(resultResponse.getSenderKey()).isEqualTo(sender.encodeToBase64());
assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
verify(transactionManager).sendSignedTransaction(argumentCaptor.capture());
com.quorum.tessera.transaction.SendSignedRequest obj = argumentCaptor.getValue();
assertThat(obj).isNotNull();
assertThat(obj.getSignedData()).isEqualTo("SOMEDATA".getBytes());
assertThat(obj.getRecipients()).hasSize(2);
assertThat(obj.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
assertThat(obj.getAffectedContractTransactions()).isEmpty();
assertThat(obj.getExecHash()).isEmpty();
}
Aggregations