use of com.quorum.tessera.enclave.PrivacyGroup 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.enclave.PrivacyGroup in project tessera by ConsenSys.
the class TransactionResource3Test method sendSignedTransactionToPrivacyGroup.
@Test
public void sendSignedTransactionToPrivacyGroup() {
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);
PrivacyGroup.Id groupId = PrivacyGroup.Id.fromBytes("groupId".getBytes());
SendSignedRequest sendSignedRequest = new SendSignedRequest();
sendSignedRequest.setHash("SOMEDATA".getBytes());
sendSignedRequest.setPrivacyGroupId(groupId.getBase64());
final PrivacyGroup pg = mock(PrivacyGroup.class);
when(pg.getMembers()).thenReturn(List.of(PublicKey.from("r1".getBytes()), PublicKey.from("r2".getBytes())));
when(pg.getId()).thenReturn(PrivacyGroup.Id.fromBytes("groupId".getBytes()));
when(privacyGroupManager.retrievePrivacyGroup(groupId)).thenReturn(pg);
Response result = transactionResource.sendSignedTransaction(sendSignedRequest);
// jersey.target("sendsignedtx")
// .request()
// .post(Entity.entity(sendSignedRequest,
// "application/vnd.tessera-3.0+json"));
assertThat(result.getStatus()).isEqualTo(201);
SendResponse resultResponse = (SendResponse) result.getEntity();
assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
verify(privacyGroupManager).retrievePrivacyGroup(groupId);
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();
assertThat(obj.getPrivacyGroupId()).isPresent().get().isEqualTo(groupId);
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class TransactionResource3Test method sendToPrivacyGroup.
@Test
public void sendToPrivacyGroup() {
final Base64.Encoder base64Encoder = Base64.getEncoder();
final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
sendRequest.setPrivacyGroupId(base64Key);
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);
PrivacyGroup retrieved = mock(PrivacyGroup.class);
PrivacyGroup.Id groupId = PrivacyGroup.Id.fromBase64String(base64Key);
PublicKey member = PublicKey.from("member".getBytes());
when(retrieved.getId()).thenReturn(groupId);
when(retrieved.getMembers()).thenReturn(List.of(member));
when(privacyGroupManager.retrievePrivacyGroup(groupId)).thenReturn(retrieved);
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()).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).retrievePrivacyGroup(groupId);
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)).isEqualTo(member);
assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
assertThat(businessObject.getAffectedContractTransactions()).isEmpty();
assertThat(businessObject.getExecHash()).isEmpty();
assertThat(businessObject.getPrivacyGroupId()).isPresent().get().isEqualTo(groupId);
}
use of com.quorum.tessera.enclave.PrivacyGroup 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.enclave.PrivacyGroup in project tessera by ConsenSys.
the class PrivacyGroupResource method getPrivacyGroups.
@GET
@Path("groups/{type}")
@Produces(APPLICATION_JSON)
public Response getPrivacyGroups(@PathParam("type") String type) {
final PrivacyGroup.Type queryType = PrivacyGroup.Type.valueOf(type.toUpperCase());
final RuntimeContext runtimeContext = RuntimeContext.getInstance();
if (queryType == PrivacyGroup.Type.RESIDENT && !runtimeContext.isMultiplePrivateStates()) {
return Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Multiple private state feature is not available on this privacy manager").build();
}
final List<PrivacyGroup> privacyGroups = privacyGroupManager.findPrivacyGroupByType(queryType);
final PrivacyGroupResponse[] results = privacyGroups.stream().map(this::toResponseObject).toArray(PrivacyGroupResponse[]::new);
return Response.status(Response.Status.OK).type(APPLICATION_JSON).entity(results).build();
}
Aggregations