Search in sources :

Example 6 with SendSignedRequest

use of com.quorum.tessera.api.SendSignedRequest in project tessera by ConsenSys.

the class RequestPrivacyValidator method isValid.

@Override
public boolean isValid(Object request, ConstraintValidatorContext context) {
    PrivacyMode privacyMode;
    String execHash;
    if (request instanceof SendRequest) {
        privacyMode = PrivacyMode.fromFlag(((SendRequest) request).getPrivacyFlag());
        execHash = ((SendRequest) request).getExecHash();
    } else if (request instanceof SendSignedRequest) {
        privacyMode = PrivacyMode.fromFlag(((SendSignedRequest) request).getPrivacyFlag());
        execHash = ((SendSignedRequest) request).getExecHash();
    } else {
        context.buildConstraintViolationWithTemplate("Invalid usage. This validator can only be apply to SendRequest or SendSignedRequest").addConstraintViolation();
        return false;
    }
    if (PrivacyMode.PRIVATE_STATE_VALIDATION == privacyMode) {
        if (Objects.isNull(execHash) || execHash.length() == 0) {
            context.buildConstraintViolationWithTemplate("Exec hash missing").addConstraintViolation();
            return false;
        }
    }
    return true;
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest)

Example 7 with SendSignedRequest

use of com.quorum.tessera.api.SendSignedRequest 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());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) Test(org.junit.Test)

Example 8 with SendSignedRequest

use of com.quorum.tessera.api.SendSignedRequest 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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) Test(org.junit.Test)

Example 9 with SendSignedRequest

use of com.quorum.tessera.api.SendSignedRequest 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);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) Test(org.junit.Test)

Aggregations

SendSignedRequest (com.quorum.tessera.api.SendSignedRequest)9 SendResponse (com.quorum.tessera.api.SendResponse)7 MessageHash (com.quorum.tessera.data.MessageHash)7 PublicKey (com.quorum.tessera.encryption.PublicKey)7 Test (org.junit.Test)6 Response (jakarta.ws.rs.core.Response)5 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)4 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)3 URI (java.net.URI)2 SendRequest (com.quorum.tessera.api.SendRequest)1 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)1 Operation (io.swagger.v3.oas.annotations.Operation)1 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)1