Search in sources :

Example 61 with MessageHash

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

the class RawTransactionResourceTest method storeUsingDefaultKey.

@Test
public void storeUsingDefaultKey() {
    com.quorum.tessera.transaction.StoreRawResponse response = mock(com.quorum.tessera.transaction.StoreRawResponse.class);
    MessageHash transactionHash = mock(MessageHash.class);
    when(transactionHash.getHashBytes()).thenReturn("TXN".getBytes());
    when(response.getHash()).thenReturn(transactionHash);
    when(transactionManager.store(any())).thenReturn(response);
    when(transactionManager.defaultPublicKey()).thenReturn(PublicKey.from("SENDER".getBytes()));
    final StoreRawRequest storeRawRequest = new StoreRawRequest();
    storeRawRequest.setPayload("PAYLOAD".getBytes());
    final Response result = transactionResource.store(storeRawRequest);
    assertThat(result.getStatus()).isEqualTo(200);
    verify(transactionManager).store(any());
    verify(transactionManager).defaultPublicKey();
}
Also used : Response(jakarta.ws.rs.core.Response) StoreRawRequest(com.quorum.tessera.api.StoreRawRequest) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 62 with MessageHash

use of com.quorum.tessera.data.MessageHash 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()));
}
Also used : Base64(java.util.Base64) SendRequest(com.quorum.tessera.api.SendRequest) 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) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 63 with MessageHash

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

the class EncodedPayloadResourceTest method createPayload.

@Test
public void createPayload() {
    final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(Base64.getEncoder().encode("PAYLOAD".getBytes()));
    sendRequest.setTo(base64Key);
    sendRequest.setAffectedContractTransactions("dHgx");
    final PublicKey sender = PublicKey.from(Base64.getDecoder().decode("oNspPPgszVUFw0qmGFfWwh1uxVUXgvBxleXORHj07g8="));
    when(transactionManager.defaultPublicKey()).thenReturn(sender);
    final EncodedPayload samplePayload = EncodedPayload.Builder.create().withSenderKey(sender).withRecipientKeys(List.of(PublicKey.from(Base64.getDecoder().decode(base64Key)))).withRecipientBoxes(List.of("boxOne".getBytes())).withRecipientNonce("recipientNonce".getBytes()).withCipherText("testPayload".getBytes()).withCipherTextNonce("cipherTextNonce".getBytes()).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(Map.of(TxHash.from("tx1".getBytes()), "tx1val".getBytes())).withExecHash(new byte[0]).build();
    when(encodedPayloadManager.create(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(samplePayload);
    final Response result = encodedPayloadResource.createEncodedPayload(sendRequest);
    assertThat(result.getStatus()).isEqualTo(200);
    final PayloadEncryptResponse payloadEncryptResponse = PayloadEncryptResponse.class.cast(result.getEntity());
    assertThat(PublicKey.from(payloadEncryptResponse.getSenderKey())).isEqualTo(sender);
    assertThat(payloadEncryptResponse.getCipherText()).isEqualTo("testPayload".getBytes());
    assertThat(payloadEncryptResponse.getCipherTextNonce()).isEqualTo("cipherTextNonce".getBytes());
    assertThat(payloadEncryptResponse.getRecipientBoxes()).hasSize(1).containsExactly("boxOne".getBytes());
    assertThat(payloadEncryptResponse.getRecipientNonce()).isEqualTo("recipientNonce".getBytes());
    assertThat(payloadEncryptResponse.getRecipientKeys()).hasSize(1);
    assertThat(payloadEncryptResponse.getRecipientKeys().get(0)).isEqualTo(Base64.getDecoder().decode(base64Key));
    assertThat(payloadEncryptResponse.getPrivacyMode()).isEqualTo(0);
    assertThat(payloadEncryptResponse.getAffectedContractTransactions()).contains(entry("dHgx", "dHgxdmFs"));
    assertThat(payloadEncryptResponse.getExecHash()).isEmpty();
    final ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
    verify(encodedPayloadManager).create(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.STANDARD_PRIVATE);
    assertThat(businessObject.getAffectedContractTransactions()).containsExactly(new MessageHash("tx1".getBytes()));
    assertThat(businessObject.getExecHash()).isEmpty();
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 64 with MessageHash

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

the class EncodedPayloadResourceTest method decryptPayload.

@Test
public void decryptPayload() {
    final PrivacyMode privacyMode = PrivacyMode.PRIVATE_STATE_VALIDATION;
    final Base64.Decoder decoder = Base64.getDecoder();
    final PayloadDecryptRequest request = new PayloadDecryptRequest();
    request.setSenderKey(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo="));
    request.setCipherText(decoder.decode("h7av/vhPlaPFECB1K30hNWugv/Bu"));
    request.setCipherTextNonce(decoder.decode("8MVXAESCQuRHWxrQ6b5MXuYApjia+2h0"));
    request.setRecipientBoxes(List.of(decoder.decode("FNirZRc2ayMaYopCBaWQ/1I7VWFiCM0lNw533Hckzxb+qpvngdWVVzJlsE05dbxl")));
    request.setRecipientNonce(decoder.decode("p9gYDJlEoBvLdUQ+ZoONl2Jl9AirV1en"));
    request.setRecipientKeys(List.of(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=")));
    request.setPrivacyMode(privacyMode.getPrivacyFlag());
    request.setAffectedContractTransactions(Map.of("dHgx", "dHgxdmFs", "dHgy", "dHgydmFs"));
    request.setExecHash("execHash".getBytes());
    final ReceiveResponse response = mock(ReceiveResponse.class);
    when(response.getPrivacyMode()).thenReturn(privacyMode);
    when(response.getUnencryptedTransactionData()).thenReturn("decryptedData".getBytes());
    when(response.getExecHash()).thenReturn("I Love sparrows".getBytes());
    MessageHash messageHash = mock(MessageHash.class);
    when(messageHash.getHashBytes()).thenReturn("SomeMessageHashBytes".getBytes());
    when(response.getAffectedTransactions()).thenReturn(Set.of(messageHash));
    when(encodedPayloadManager.decrypt(any(), eq(null))).thenReturn(response);
    final Response result = encodedPayloadResource.decryptEncodedPayload(request);
    assertThat(result.getStatus()).isEqualTo(200);
    final com.quorum.tessera.api.ReceiveResponse payloadEncryptResponse = com.quorum.tessera.api.ReceiveResponse.class.cast(result.getEntity());
    assertThat(payloadEncryptResponse.getPayload()).isEqualTo("decryptedData".getBytes());
    assertThat(payloadEncryptResponse.getPrivacyFlag()).isEqualTo(privacyMode.getPrivacyFlag());
    assertThat(payloadEncryptResponse.getAffectedContractTransactions()).contains(Base64.getEncoder().encodeToString("SomeMessageHashBytes".getBytes()));
    assertThat(payloadEncryptResponse.getExecHash()).isEqualTo("I Love sparrows");
    verify(encodedPayloadManager).decrypt(any(), eq(null));
}
Also used : PayloadDecryptRequest(com.quorum.tessera.api.PayloadDecryptRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Base64(java.util.Base64) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 65 with MessageHash

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

the class EncodedPayloadResourceTest method createPayloadVersion21.

@Test
public void createPayloadVersion21() {
    final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(Base64.getEncoder().encode("PAYLOAD".getBytes()));
    sendRequest.setTo(base64Key);
    sendRequest.setAffectedContractTransactions("dHgx");
    final PublicKey sender = PublicKey.from(Base64.getDecoder().decode("oNspPPgszVUFw0qmGFfWwh1uxVUXgvBxleXORHj07g8="));
    when(transactionManager.defaultPublicKey()).thenReturn(sender);
    final EncodedPayload samplePayload = EncodedPayload.Builder.create().withSenderKey(sender).withRecipientKeys(List.of(PublicKey.from(Base64.getDecoder().decode(base64Key)))).withRecipientBoxes(List.of("boxOne".getBytes())).withRecipientNonce("recipientNonce".getBytes()).withCipherText("testPayload".getBytes()).withCipherTextNonce("cipherTextNonce".getBytes()).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(Map.of(TxHash.from("tx1".getBytes()), "tx1val".getBytes())).withExecHash(new byte[0]).build();
    when(encodedPayloadManager.create(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(samplePayload);
    final Response result = encodedPayloadResource.createEncodedPayload21(sendRequest);
    assertThat(result.getStatus()).isEqualTo(200);
    final PayloadEncryptResponse payloadEncryptResponse = Optional.of(result).map(Response::getEntity).map(PayloadEncryptResponse.class::cast).get();
    assertThat(PublicKey.from(payloadEncryptResponse.getSenderKey())).isEqualTo(sender);
    assertThat(payloadEncryptResponse.getCipherText()).isEqualTo("testPayload".getBytes());
    assertThat(payloadEncryptResponse.getCipherTextNonce()).isEqualTo("cipherTextNonce".getBytes());
    assertThat(payloadEncryptResponse.getRecipientBoxes()).hasSize(1).containsExactly("boxOne".getBytes());
    assertThat(payloadEncryptResponse.getRecipientNonce()).isEqualTo("recipientNonce".getBytes());
    assertThat(payloadEncryptResponse.getRecipientKeys()).hasSize(1);
    assertThat(payloadEncryptResponse.getRecipientKeys().get(0)).isEqualTo(Base64.getDecoder().decode(base64Key));
    assertThat(payloadEncryptResponse.getPrivacyMode()).isEqualTo(0);
    assertThat(payloadEncryptResponse.getAffectedContractTransactions()).contains(entry("dHgx", "dHgxdmFs"));
    assertThat(payloadEncryptResponse.getExecHash()).isEmpty();
    final ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
    verify(encodedPayloadManager).create(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.STANDARD_PRIVATE);
    assertThat(businessObject.getAffectedContractTransactions()).containsExactly(new MessageHash("tx1".getBytes()));
    assertThat(businessObject.getExecHash()).isEmpty();
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) 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