From bfb9b4de4676aade75f8212762b195b517b54df8 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Thu, 19 Dec 2019 15:26:55 +1000 Subject: [PATCH 01/13] wire enclave public key into the privacy controller methods Signed-off-by: Jason Frame --- .../methods/PrivacySendTransaction.java | 5 +- .../methods/eea/EeaSendRawTransaction.java | 6 +- .../methods/priv/PrivCreatePrivacyGroup.java | 6 +- .../methods/priv/PrivDeletePrivacyGroup.java | 5 +- .../priv/PrivDistributeRawTransaction.java | 6 +- .../methods/priv/PrivFindPrivacyGroup.java | 5 +- .../priv/PrivGetEeaTransactionCount.java | 5 +- .../priv/PrivGetPrivateTransaction.java | 4 +- .../methods/priv/PrivGetTransactionCount.java | 6 +- .../priv/PrivGetTransactionReceipt.java | 4 +- .../eea/PrivGetEeaTransactionCountTest.java | 9 ++- .../eea/EeaSendRawTransactionTest.java | 36 +++++---- .../priv/PrivCreatePrivacyGroupTest.java | 17 ++-- .../PrivDistributeRawTransactionTest.java | 11 ++- .../priv/PrivGetPrivateTransactionTest.java | 6 +- .../priv/PrivGetTransactionCountTest.java | 4 +- .../priv/PrivGetTransactionReceiptTest.java | 11 ++- .../ethereum/privacy/PrivacyController.java | 48 ++++++++---- .../privacy/PrivacyControllerTest.java | 78 ++++++++++++++----- 19 files changed, 193 insertions(+), 79 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java index 184cf438e9a..93bf667646d 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; + import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException; @@ -74,7 +76,8 @@ public JsonRpcResponse validateAndExecute( final String privacyGroupId, final Supplier successfulJsonRpcResponse) { return privacyController - .validatePrivateTransaction(privateTransaction, privacyGroupId) + .validatePrivateTransaction( + privateTransaction, privacyGroupId, enclavePublicKey(request.getUser())) .either( successfulJsonRpcResponse, (errorReason) -> diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java index 85d8a698464..d8e138c583b 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.eea; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; + import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; @@ -59,7 +61,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final SendTransactionResponse sendTransactionResponse; try { - sendTransactionResponse = privacyController.sendTransaction(privateTransaction); + sendTransactionResponse = + privacyController.sendTransaction( + privateTransaction, enclavePublicKey(requestContext.getUser())); } catch (final Exception e) { return new JsonRpcErrorResponse( requestContext.getRequest().getId(), diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java index dce56fc558e..8b1095fb1d6 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.types.PrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter; @@ -59,7 +60,10 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { response = privacyController.createPrivacyGroup( - parameter.getAddresses(), parameter.getName(), parameter.getDescription()); + parameter.getAddresses(), + parameter.getName(), + parameter.getDescription(), + enclavePublicKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to create privacy group", e); return new JsonRpcErrorResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java index 7f98a8c70c1..75b4854faf5 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; @@ -48,7 +49,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final String response; try { - response = privacyController.deletePrivacyGroup(privacyGroupId); + response = + privacyController.deletePrivacyGroup( + privacyGroupId, enclavePublicKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to fetch transaction", e); return new JsonRpcSuccessResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java index 96c66ba1701..544533f79d7 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; + import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; @@ -54,7 +56,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final SendTransactionResponse sendTransactionResponse; try { - sendTransactionResponse = privacyController.sendTransaction(privateTransaction); + sendTransactionResponse = + privacyController.sendTransaction( + privateTransaction, enclavePublicKey(requestContext.getUser())); } catch (final Exception e) { return new JsonRpcErrorResponse( requestContext.getRequest().getId(), diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java index 4b0900a6982..6e55023dc87 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.types.PrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; @@ -54,7 +55,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { PrivacyGroup[] response; try { - response = privacyController.findPrivacyGroup(Arrays.asList(addresses)); + response = + privacyController.findPrivacyGroup( + Arrays.asList(addresses), enclavePublicKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to fetch privacy group", e); return new JsonRpcErrorResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java index 358fa666e1d..c48ed8ac533 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; @@ -56,7 +57,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final String[] privateFor = requestContext.getRequiredParameter(2, String[].class); try { - final long nonce = privacyController.determineNonce(privateFrom, privateFor, address); + final long nonce = + privacyController.determineNonce( + privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser())); return new JsonRpcSuccessResponse( requestContext.getRequest().getId(), Quantity.create(nonce)); } catch (final Exception e) { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java index 9636faa737e..2fb8a629503 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.types.ReceiveResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; @@ -67,7 +68,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { LOG.trace("Fetching transaction information"); final ReceiveResponse receiveResponse = privacyController.retrieveTransaction( - BytesValues.asBase64String(resultTransaction.getTransaction().getPayload())); + BytesValues.asBase64String(resultTransaction.getTransaction().getPayload()), + enclavePublicKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput bytesValueRLPInput = diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java index 34284f19276..ee4cb856091 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; + import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -48,7 +50,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final Address address = requestContext.getRequiredParameter(0, Address.class); final String privacyGroupId = requestContext.getRequiredParameter(1, String.class); - final long nonce = privacyController.determineNonce(address, privacyGroupId); + final long nonce = + privacyController.determineNonce( + address, privacyGroupId, enclavePublicKey(requestContext.getUser())); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), Quantity.create(nonce)); } } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java index 87096ca21fc..915bbe6b228 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.EnclaveException; import org.hyperledger.besu.enclave.types.ReceiveResponse; @@ -93,7 +94,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { final ReceiveResponse receiveResponse = privacyController.retrieveTransaction( - BytesValues.asBase64String(transaction.getPayload())); + BytesValues.asBase64String(transaction.getPayload()), + enclavePublicKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput bytesValueRLPInput = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java index 08b22dde297..3b4869fc3cb 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java @@ -56,7 +56,8 @@ public void validRequestProducesExpectedNonce() { final long reportedNonce = 8L; final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); - when(privacyController.determineNonce(privateFrom, privateFor, address)) + when(privacyController.determineNonce( + privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) .thenReturn(reportedNonce); final JsonRpcResponse response = method.response(request); @@ -71,7 +72,8 @@ public void validRequestProducesExpectedNonce() { public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() { final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); - when(privacyController.determineNonce(privateFrom, privateFor, address)) + when(privacyController.determineNonce( + privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) .thenThrow(RuntimeException.class); final JsonRpcResponse response = method.response(request); @@ -86,7 +88,8 @@ public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() { public void nonceProviderThrowsAnExceptionProducesErrorResponse() { final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); - when(privacyController.determineNonce(privateFrom, privateFor, address)) + when(privacyController.determineNonce( + privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) .thenThrow(RuntimeException.class); final JsonRpcResponse response = method.response(request); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java index e16cef8f39e..e16b46a59e5 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java @@ -195,10 +195,11 @@ public void valueNonZeroTransaction() { @Test public void validTransactionIsSentToTransactionPool() { - when(privacyController.sendTransaction(any(PrivateTransaction.class))) + when(privacyController.sendTransaction( + any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) .thenReturn(new SendTransactionResponse(MOCK_ORION_KEY, MOCK_PRIVACY_GROUP)); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class))) + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) .thenReturn(ValidationResult.valid()); when(privacyController.createPrivacyMarkerTransaction( any(String.class), any(PrivateTransaction.class))) @@ -218,9 +219,11 @@ public void validTransactionIsSentToTransactionPool() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); - verify(privacyController).sendTransaction(any(PrivateTransaction.class)); verify(privacyController) - .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class)); + .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); + verify(privacyController) + .validatePrivateTransaction( + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); @@ -228,10 +231,11 @@ public void validTransactionIsSentToTransactionPool() { @Test public void validTransactionPrivacyGroupIsSentToTransactionPool() { - when(privacyController.sendTransaction(any(PrivateTransaction.class))) + when(privacyController.sendTransaction( + any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) .thenReturn(new SendTransactionResponse(MOCK_ORION_KEY, MOCK_PRIVACY_GROUP)); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class))) + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) .thenReturn(ValidationResult.valid()); when(privacyController.createPrivacyMarkerTransaction( any(String.class), any(PrivateTransaction.class))) @@ -254,9 +258,11 @@ public void validTransactionPrivacyGroupIsSentToTransactionPool() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); - verify(privacyController).sendTransaction(any(PrivateTransaction.class)); verify(privacyController) - .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class)); + .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); + verify(privacyController) + .validatePrivateTransaction( + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); @@ -282,7 +288,8 @@ public void invalidTransactionWithoutPrivateFromFieldFailsWithDecodeError() { @Test public void invalidTransactionIsNotSentToTransactionPool() { - when(privacyController.sendTransaction(any(PrivateTransaction.class))) + when(privacyController.sendTransaction( + any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) .thenThrow(new EnclaveException("enclave failed to execute")); final JsonRpcRequestContext request = @@ -346,10 +353,11 @@ public void transactionWithNotWhitelistedSenderAccountIsRejected() { private void verifyErrorForInvalidTransaction( final TransactionInvalidReason transactionInvalidReason, final JsonRpcError expectedError) { - when(privacyController.sendTransaction(any(PrivateTransaction.class))) + when(privacyController.sendTransaction( + any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) .thenReturn(new SendTransactionResponse(MOCK_ORION_KEY, MOCK_PRIVACY_GROUP)); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class))) + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) .thenReturn(ValidationResult.valid()); when(privacyController.createPrivacyMarkerTransaction( any(String.class), any(PrivateTransaction.class))) @@ -367,9 +375,11 @@ private void verifyErrorForInvalidTransaction( final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); - verify(privacyController).sendTransaction(any(PrivateTransaction.class)); verify(privacyController) - .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class)); + .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); + verify(privacyController) + .validatePrivateTransaction( + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java index eff3f67436f..4c3556652af 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java @@ -61,7 +61,8 @@ public void verifyCreatePrivacyGroup() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION)) + when(privacyController.createPrivacyGroup( + ADDRESSES, NAME, DESCRIPTION, enclavePublicKey(requestContext.getUser()))) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -89,7 +90,9 @@ public void verifyCreatePrivacyGroupWithoutDescription() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, NAME, null)).thenReturn(privacyGroup); + when(privacyController.createPrivacyGroup( + ADDRESSES, NAME, null, enclavePublicKey(requestContext.getUser()))) + .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = @@ -124,7 +127,8 @@ public void verifyCreatePrivacyGroupWithoutName() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, null, DESCRIPTION)) + when(privacyController.createPrivacyGroup( + ADDRESSES, null, DESCRIPTION, enclavePublicKey(requestContext.getUser()))) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -160,7 +164,9 @@ public void verifyCreatePrivacyGroupWithoutOptionalParams() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, null, null)).thenReturn(privacyGroup); + when(privacyController.createPrivacyGroup( + ADDRESSES, null, null, enclavePublicKey(requestContext.getUser()))) + .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = @@ -241,7 +247,8 @@ public void returnsCorrectExceptionMissingParam() { @Test public void returnsCorrectErrorEnclaveError() { - when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION)) + when(privacyController.createPrivacyGroup( + ADDRESSES, NAME, DESCRIPTION, enclavePublicKey(requestContext.getUser()))) .thenThrow(new EnclaveException("")); final PrivCreatePrivacyGroup privCreatePrivacyGroup = new PrivCreatePrivacyGroup(privacyController); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java index 9a23ac79da2..d231cc8c2c8 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java @@ -58,10 +58,11 @@ public void before() { @Test public void validTransactionHashReturnedAfterDistribute() { final String enclavePublicKey = "93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng="; - when(privacyController.sendTransaction(any(PrivateTransaction.class))) + when(privacyController.sendTransaction( + any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) .thenReturn(new SendTransactionResponse(enclavePublicKey, "")); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class))) + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) .thenReturn(ValidationResult.valid()); final JsonRpcRequestContext request = @@ -78,8 +79,10 @@ public void validTransactionHashReturnedAfterDistribute() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); - verify(privacyController).sendTransaction(any(PrivateTransaction.class)); verify(privacyController) - .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class)); + .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); + verify(privacyController) + .validatePrivateTransaction( + any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index e574c38d8f1..ec3502e5a92 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -130,7 +130,8 @@ public void returnsPrivateTransactionLegacy() { final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); - when(privacyController.retrieveTransaction(anyString())) + when(privacyController.retrieveTransaction( + anyString(), enclavePublicKey(requestContext.getUser()))) .thenReturn( new ReceiveResponse( Base64.getEncoder().encodeToString(bvrlp.encoded().extractArray()).getBytes(UTF_8), @@ -165,7 +166,8 @@ public void returnsPrivateTransactionGroup() { final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); - when(privacyController.retrieveTransaction(anyString())) + when(privacyController.retrieveTransaction( + anyString(), enclavePublicKey(requestContext.getUser()))) .thenReturn( new ReceiveResponse( Base64.getEncoder().encodeToString(bvrlp.encoded().extractArray()).getBytes(UTF_8), diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java index 144a4398644..aebbd1bdd23 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java @@ -46,7 +46,9 @@ public class PrivGetTransactionCountTest { @Before public void before() { when(privacyParameters.isEnabled()).thenReturn(true); - when(privacyController.determineNonce(senderAddress, privacyGroupId)).thenReturn(NONCE); + when(privacyController.determineNonce( + senderAddress, privacyGroupId, enclavePublicKey(requestContext.getUser()))) + .thenReturn(NONCE); } @Test diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java index 2b74a6cfd8b..af396c40851 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java @@ -147,7 +147,8 @@ public class PrivGetTransactionReceiptTest { @Before public void setUp() { - when(privacyController.retrieveTransaction(anyString())) + when(privacyController.retrieveTransaction( + anyString(), enclavePublicKey(requestContext.getUser()))) .thenReturn( new ReceiveResponse( Base64.getEncoder().encode(RLP.encode(privateTransaction::writeTo).extractArray()), @@ -192,7 +193,8 @@ public void returnReceiptIfTransactionExists() { @Test public void enclavePayloadNotFoundResultsInSuccessButNullResponse() { - when(failingPrivacyController.retrieveTransaction(anyString())) + when(failingPrivacyController.retrieveTransaction( + anyString(), enclavePublicKey(requestContext.getUser()))) .thenThrow(new EnclaveException("EnclavePayloadNotFound")); final PrivGetTransactionReceipt privGetTransactionReceipt = @@ -231,7 +233,8 @@ public void markerTransactionNotAvailableResultsInNullResponse() { @Test public void enclaveConnectionIssueThrowsRuntimeException() { - when(failingPrivacyController.retrieveTransaction(anyString())) + when(failingPrivacyController.retrieveTransaction( + anyString(), enclavePublicKey(requestContext.getUser()))) .thenThrow(EnclaveException.class); final PrivGetTransactionReceipt privGetTransactionReceipt = new PrivGetTransactionReceipt( @@ -267,7 +270,7 @@ public void transactionReceiptContainsRevertReasonWhenInvalidTransactionOccurs() public void enclaveKeysCannotDecryptPayloadThrowsRuntimeException() { final String keysCannotDecryptPayloadMsg = "EnclaveKeysCannotDecryptPayload"; when(privacyParameters.getEnclave()).thenReturn(enclave); - when(privacyController.retrieveTransaction(any())) + when(privacyController.retrieveTransaction(any(), enclavePublicKey(requestContext.getUser()))) .thenThrow(new EnclaveException(keysCannotDecryptPayloadMsg)); final PrivGetTransactionReceipt privGetTransactionReceipt = diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java index 6e9496fcd14..baee5cc858d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java @@ -46,7 +46,7 @@ public class PrivacyController { private static final Logger LOG = LogManager.getLogger(); private final Enclave enclave; - private final String enclavePublicKey; + private final String defaultEnclavePublicKey; private final PrivateStateStorage privateStateStorage; private final WorldStateArchive privateWorldStateArchive; private final PrivateTransactionValidator privateTransactionValidator; @@ -67,20 +67,21 @@ public PrivacyController( public PrivacyController( final Enclave enclave, - final String enclavePublicKey, + final String defaultEnclavePublicKey, final PrivateStateStorage privateStateStorage, final WorldStateArchive privateWorldStateArchive, final PrivateTransactionValidator privateTransactionValidator, final PrivateMarkerTransactionFactory privateMarkerTransactionFactory) { this.enclave = enclave; - this.enclavePublicKey = enclavePublicKey; + this.defaultEnclavePublicKey = defaultEnclavePublicKey; this.privateStateStorage = privateStateStorage; this.privateWorldStateArchive = privateWorldStateArchive; this.privateTransactionValidator = privateTransactionValidator; this.privateMarkerTransactionFactory = privateMarkerTransactionFactory; } - public SendTransactionResponse sendTransaction(final PrivateTransaction privateTransaction) { + public SendTransactionResponse sendTransaction( + final PrivateTransaction privateTransaction, final Optional enclavePublicKey) { try { LOG.trace("Storing private transaction in enclave"); final SendResponse sendResponse = sendRequest(privateTransaction); @@ -100,20 +101,26 @@ public SendTransactionResponse sendTransaction(final PrivateTransaction privateT } } - public ReceiveResponse retrieveTransaction(final String enclaveKey) { - return enclave.receive(enclaveKey, enclavePublicKey); + public ReceiveResponse retrieveTransaction( + final String enclaveKey, final Optional enclavePublicKey) { + return enclave.receive(enclaveKey, this.defaultEnclavePublicKey); } public PrivacyGroup createPrivacyGroup( - final List addresses, final String name, final String description) { - return enclave.createPrivacyGroup(addresses, enclavePublicKey, name, description); + final List addresses, + final String name, + final String description, + final Optional enclavePublicKey) { + return enclave.createPrivacyGroup(addresses, defaultEnclavePublicKey, name, description); } - public String deletePrivacyGroup(final String privacyGroupId) { - return enclave.deletePrivacyGroup(privacyGroupId, enclavePublicKey); + public String deletePrivacyGroup( + final String privacyGroupId, final Optional enclavePublicKey) { + return enclave.deletePrivacyGroup(privacyGroupId, defaultEnclavePublicKey); } - public PrivacyGroup[] findPrivacyGroup(final List addresses) { + public PrivacyGroup[] findPrivacyGroup( + final List addresses, final Optional enclavePublicKey) { return enclave.findPrivacyGroup(addresses); } @@ -123,13 +130,19 @@ public Transaction createPrivacyMarkerTransaction( } public ValidationResult validatePrivateTransaction( - final PrivateTransaction privateTransaction, final String privacyGroupId) { + final PrivateTransaction privateTransaction, + final String privacyGroupId, + final Optional enclavePublicKey) { return privateTransactionValidator.validate( - privateTransaction, determineNonce(privateTransaction.getSender(), privacyGroupId)); + privateTransaction, + determineNonce(privateTransaction.getSender(), privacyGroupId, enclavePublicKey)); } public long determineNonce( - final String privateFrom, final String[] privateFor, final Address address) { + final String privateFrom, + final String[] privateFor, + final Address address, + final Optional enclavePublicKey) { final List groupMembers = Lists.asList(privateFrom, privateFor); final List matchingGroups = @@ -151,10 +164,11 @@ public long determineNonce( final String privacyGroupId = legacyGroups.get(0).getPrivacyGroupId(); - return determineNonce(address, privacyGroupId); + return determineNonce(address, privacyGroupId, enclavePublicKey); } - public long determineNonce(final Address sender, final String privacyGroupId) { + public long determineNonce( + final Address sender, final String privacyGroupId, final Optional enclavePublicKey) { return privateStateStorage .getLatestStateRoot(BytesValues.fromBase64(privacyGroupId)) .map( @@ -186,7 +200,7 @@ private SendResponse sendRequest(final PrivateTransaction privateTransaction) { if (privateTransaction.getPrivacyGroupId().isPresent()) { return enclave.send( payload, - enclavePublicKey, + defaultEnclavePublicKey, BytesValues.asBase64String(privateTransaction.getPrivacyGroupId().get())); } else { final List privateFor = diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java index 51cabb095d4..b42a287f42f 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java @@ -163,11 +163,13 @@ public void sendsValidLegacyTransaction() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(1); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction); + privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, sendTransactionResponse.getPrivacyGroupId()); + transaction, + sendTransactionResponse.getPrivacyGroupId(), + enclavePublicKey(request.getUser())); final Transaction markerTransaction = privacyController.createPrivacyMarkerTransaction( @@ -187,11 +189,13 @@ public void sendValidBesuTransaction() { final PrivateTransaction transaction = buildBesuPrivateTransaction(1); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction); + privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, transaction.getPrivacyGroupId().get().toString()); + transaction, + transaction.getPrivacyGroupId().get().toString(), + enclavePublicKey(request.getUser())); final Transaction markerTransaction = privacyController.createPrivacyMarkerTransaction( @@ -208,7 +212,10 @@ public void sendValidBesuTransaction() { @Test public void sendTransactionWhenEnclaveFailsThrowsEnclaveError() { assertThatExceptionOfType(EnclaveException.class) - .isThrownBy(() -> brokenPrivacyController.sendTransaction(buildLegacyPrivateTransaction())); + .isThrownBy( + () -> + brokenPrivacyController.sendTransaction( + buildLegacyPrivateTransaction(), enclavePublicKey(requestContext.getUser()))); } @Test @@ -218,10 +225,12 @@ public void validateTransactionWithTooLowNonceReturnsError() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(0); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction); + privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, sendTransactionResponse.getPrivacyGroupId()); + transaction, + sendTransactionResponse.getPrivacyGroupId(), + enclavePublicKey(request.getUser())); assertThat(validationResult).isEqualTo(ValidationResult.invalid(PRIVATE_NONCE_TOO_LOW)); } @@ -233,10 +242,12 @@ public void validateTransactionWithIncorrectNonceReturnsError() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(2); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction); + privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, sendTransactionResponse.getPrivacyGroupId()); + transaction, + sendTransactionResponse.getPrivacyGroupId(), + enclavePublicKey(request.getUser())); assertThat(validationResult).isEqualTo(ValidationResult.invalid(INCORRECT_PRIVATE_NONCE)); } @@ -245,7 +256,9 @@ public void retrievesTransaction() { when(enclave.receive(anyString(), anyString())) .thenReturn(new ReceiveResponse(PAYLOAD, PRIVACY_GROUP_ID)); - final ReceiveResponse receiveResponse = privacyController.retrieveTransaction(TRANSACTION_KEY); + final ReceiveResponse receiveResponse = + privacyController.retrieveTransaction( + TRANSACTION_KEY, enclavePublicKey(requestContext.getUser())); assertThat(receiveResponse.getPayload()).isEqualTo(PAYLOAD); assertThat(receiveResponse.getPrivacyGroupId()).isEqualTo(PRIVACY_GROUP_ID); @@ -266,7 +279,10 @@ public void createsPrivacyGroup() { final PrivacyGroup privacyGroup = privacyController.createPrivacyGroup( - PRIVACY_GROUP_ADDRESSES, PRIVACY_GROUP_NAME, PRIVACY_GROUP_DESCRIPTION); + PRIVACY_GROUP_ADDRESSES, + PRIVACY_GROUP_NAME, + PRIVACY_GROUP_DESCRIPTION, + enclavePublicKey(requestContext.getUser())); assertThat(privacyGroup).isEqualToComparingFieldByField(enclavePrivacyGroupResponse); verify(enclave) @@ -281,7 +297,9 @@ public void createsPrivacyGroup() { public void deletesPrivacyGroup() { when(enclave.deletePrivacyGroup(anyString(), anyString())).thenReturn(PRIVACY_GROUP_ID); - final String deletedPrivacyGroupId = privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID); + final String deletedPrivacyGroupId = + privacyController.deletePrivacyGroup( + PRIVACY_GROUP_ID, enclavePublicKey(requestContext.getUser())); assertThat(deletedPrivacyGroupId).isEqualTo(PRIVACY_GROUP_ID); verify(enclave).deletePrivacyGroup(PRIVACY_GROUP_ID, enclavePublicKey); @@ -299,7 +317,8 @@ public void findsPrivacyGroup() { when(enclave.findPrivacyGroup(any())).thenReturn(new PrivacyGroup[] {privacyGroup}); final PrivacyGroup[] privacyGroups = - privacyController.findPrivacyGroup(PRIVACY_GROUP_ADDRESSES); + privacyController.findPrivacyGroup( + PRIVACY_GROUP_ADDRESSES, enclavePublicKey(requestContext.getUser())); assertThat(privacyGroups).hasSize(1); assertThat(privacyGroups[0]).isEqualToComparingFieldByField(privacyGroup); verify(enclave).findPrivacyGroup(PRIVACY_GROUP_ADDRESSES); @@ -318,7 +337,11 @@ public void determinesNonceForEeaRequest() { when(account.getNonce()).thenReturn(8L); final long nonce = - privacyController.determineNonce("privateFrom", new String[] {"first", "second"}, address); + privacyController.determineNonce( + "privateFrom", + new String[] {"first", "second"}, + address, + enclavePublicKey(requestContext.getUser())); assertThat(nonce).isEqualTo(reportedNonce); verify(enclave) @@ -335,7 +358,11 @@ public void determineNonceForEeaRequestWithNoMatchingGroupReturnsZero() { when(enclave.findPrivacyGroup(any())).thenReturn(returnedGroups); final long nonce = - privacyController.determineNonce("privateFrom", new String[] {"first", "second"}, address); + privacyController.determineNonce( + "privateFrom", + new String[] {"first", "second"}, + address, + enclavePublicKey(requestContext.getUser())); assertThat(nonce).isEqualTo(reportedNonce); verify(enclave) @@ -358,7 +385,10 @@ public void determineNonceForEeaRequestWithMoreThanOneMatchingGroupThrowsExcepti .isThrownBy( () -> privacyController.determineNonce( - "privateFrom", new String[] {"first", "second"}, address)); + "privateFrom", + new String[] {"first", "second"}, + address, + enclavePublicKey(requestContext.getUser()))); } @Test @@ -367,7 +397,9 @@ public void determineNonceForPrivacyGroupRequestWhenAccountExists() { when(account.getNonce()).thenReturn(4L); - final long nonce = privacyController.determineNonce(address, "Group1"); + final long nonce = + privacyController.determineNonce( + address, "Group1", enclavePublicKey(requestContext.getUser())); assertThat(nonce).isEqualTo(4L); verify(privateStateStorage).getLatestStateRoot(BytesValues.fromBase64("Group1")); @@ -382,7 +414,9 @@ public void determineNonceForPrivacyGroupRequestWhenPrivateStateDoesNotExist() { when(privateStateStorage.getLatestStateRoot(BytesValues.fromBase64("Group1"))) .thenReturn(Optional.empty()); - final long nonce = privacyController.determineNonce(address, "Group1"); + final long nonce = + privacyController.determineNonce( + address, "Group1", enclavePublicKey(requestContext.getUser())); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(worldStateArchive, mutableWorldState, account); @@ -396,7 +430,9 @@ public void determineNonceForPrivacyGroupRequestWhenWorldStateDoesNotExist() { .thenReturn(Optional.of(hash)); when(worldStateArchive.getMutable(hash)).thenReturn(Optional.empty()); - final long nonce = privacyController.determineNonce(address, "Group1"); + final long nonce = + privacyController.determineNonce( + address, "Group1", enclavePublicKey(requestContext.getUser())); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(mutableWorldState, account); @@ -411,7 +447,9 @@ public void determineNonceForPrivacyGroupRequestWhenAccountDoesNotExist() { when(worldStateArchive.getMutable(hash)).thenReturn(Optional.of(mutableWorldState)); when(mutableWorldState.get(address)).thenReturn(null); - final long nonce = privacyController.determineNonce(address, "Group1"); + final long nonce = + privacyController.determineNonce( + address, "Group1", enclavePublicKey(requestContext.getUser())); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(account); From 1814ae740cb987ac3b62ff1518dffd901f64f395 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Thu, 19 Dec 2019 17:10:16 +1000 Subject: [PATCH 02/13] unit tests Signed-off-by: Jason Frame --- .../eea/PrivGetEeaTransactionCountTest.java | 11 ++- .../eea/EeaSendRawTransactionTest.java | 36 ++++------ .../priv/PrivCreatePrivacyGroupTest.java | 16 ++--- .../PrivDistributeRawTransactionTest.java | 11 ++- .../priv/PrivGetPrivateTransactionTest.java | 6 +- .../priv/PrivGetTransactionCountTest.java | 5 +- .../priv/PrivGetTransactionReceiptTest.java | 11 ++- .../privacy/PrivacyControllerTest.java | 68 ++++++------------- 8 files changed, 57 insertions(+), 107 deletions(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java index 3b4869fc3cb..4a342303297 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java @@ -29,6 +29,8 @@ import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.privacy.PrivacyController; +import java.util.Optional; + import org.junit.Before; import org.junit.Test; @@ -56,8 +58,7 @@ public void validRequestProducesExpectedNonce() { final long reportedNonce = 8L; final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); - when(privacyController.determineNonce( - privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) + when(privacyController.determineNonce(privateFrom, privateFor, address, Optional.empty())) .thenReturn(reportedNonce); final JsonRpcResponse response = method.response(request); @@ -72,8 +73,7 @@ privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() { final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); - when(privacyController.determineNonce( - privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) + when(privacyController.determineNonce(privateFrom, privateFor, address, Optional.empty())) .thenThrow(RuntimeException.class); final JsonRpcResponse response = method.response(request); @@ -88,8 +88,7 @@ privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) public void nonceProviderThrowsAnExceptionProducesErrorResponse() { final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); - when(privacyController.determineNonce( - privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser()))) + when(privacyController.determineNonce(privateFrom, privateFor, address, Optional.empty())) .thenThrow(RuntimeException.class); final JsonRpcResponse response = method.response(request); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java index e16b46a59e5..e258a07c417 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java @@ -195,11 +195,10 @@ public void valueNonZeroTransaction() { @Test public void validTransactionIsSentToTransactionPool() { - when(privacyController.sendTransaction( - any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) + when(privacyController.sendTransaction(any(PrivateTransaction.class), any())) .thenReturn(new SendTransactionResponse(MOCK_ORION_KEY, MOCK_PRIVACY_GROUP)); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) + any(PrivateTransaction.class), any(String.class), any())) .thenReturn(ValidationResult.valid()); when(privacyController.createPrivacyMarkerTransaction( any(String.class), any(PrivateTransaction.class))) @@ -219,11 +218,9 @@ public void validTransactionIsSentToTransactionPool() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); + verify(privacyController).sendTransaction(any(PrivateTransaction.class), any()); verify(privacyController) - .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); - verify(privacyController) - .validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); + .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class), any()); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); @@ -231,11 +228,10 @@ public void validTransactionIsSentToTransactionPool() { @Test public void validTransactionPrivacyGroupIsSentToTransactionPool() { - when(privacyController.sendTransaction( - any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) + when(privacyController.sendTransaction(any(PrivateTransaction.class), any())) .thenReturn(new SendTransactionResponse(MOCK_ORION_KEY, MOCK_PRIVACY_GROUP)); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) + any(PrivateTransaction.class), any(String.class), any())) .thenReturn(ValidationResult.valid()); when(privacyController.createPrivacyMarkerTransaction( any(String.class), any(PrivateTransaction.class))) @@ -258,11 +254,9 @@ public void validTransactionPrivacyGroupIsSentToTransactionPool() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); + verify(privacyController).sendTransaction(any(PrivateTransaction.class), any()); verify(privacyController) - .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); - verify(privacyController) - .validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); + .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class), any()); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); @@ -288,8 +282,7 @@ public void invalidTransactionWithoutPrivateFromFieldFailsWithDecodeError() { @Test public void invalidTransactionIsNotSentToTransactionPool() { - when(privacyController.sendTransaction( - any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) + when(privacyController.sendTransaction(any(PrivateTransaction.class), any())) .thenThrow(new EnclaveException("enclave failed to execute")); final JsonRpcRequestContext request = @@ -353,11 +346,10 @@ public void transactionWithNotWhitelistedSenderAccountIsRejected() { private void verifyErrorForInvalidTransaction( final TransactionInvalidReason transactionInvalidReason, final JsonRpcError expectedError) { - when(privacyController.sendTransaction( - any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) + when(privacyController.sendTransaction(any(PrivateTransaction.class), any())) .thenReturn(new SendTransactionResponse(MOCK_ORION_KEY, MOCK_PRIVACY_GROUP)); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) + any(PrivateTransaction.class), any(String.class), any())) .thenReturn(ValidationResult.valid()); when(privacyController.createPrivacyMarkerTransaction( any(String.class), any(PrivateTransaction.class))) @@ -375,11 +367,9 @@ private void verifyErrorForInvalidTransaction( final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); + verify(privacyController).sendTransaction(any(PrivateTransaction.class), any()); verify(privacyController) - .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); - verify(privacyController) - .validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); + .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class), any()); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java index 4c3556652af..1fdadf15183 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java @@ -34,6 +34,7 @@ import org.hyperledger.besu.ethereum.privacy.PrivacyController; import java.util.List; +import java.util.Optional; import org.assertj.core.util.Lists; import org.junit.Before; @@ -61,8 +62,7 @@ public void verifyCreatePrivacyGroup() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup( - ADDRESSES, NAME, DESCRIPTION, enclavePublicKey(requestContext.getUser()))) + when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, Optional.empty())) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -90,8 +90,7 @@ public void verifyCreatePrivacyGroupWithoutDescription() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup( - ADDRESSES, NAME, null, enclavePublicKey(requestContext.getUser()))) + when(privacyController.createPrivacyGroup(ADDRESSES, NAME, null, Optional.empty())) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -127,8 +126,7 @@ public void verifyCreatePrivacyGroupWithoutName() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup( - ADDRESSES, null, DESCRIPTION, enclavePublicKey(requestContext.getUser()))) + when(privacyController.createPrivacyGroup(ADDRESSES, null, DESCRIPTION, Optional.empty())) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -164,8 +162,7 @@ public void verifyCreatePrivacyGroupWithoutOptionalParams() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup( - ADDRESSES, null, null, enclavePublicKey(requestContext.getUser()))) + when(privacyController.createPrivacyGroup(ADDRESSES, null, null, Optional.empty())) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -247,8 +244,7 @@ public void returnsCorrectExceptionMissingParam() { @Test public void returnsCorrectErrorEnclaveError() { - when(privacyController.createPrivacyGroup( - ADDRESSES, NAME, DESCRIPTION, enclavePublicKey(requestContext.getUser()))) + when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, Optional.empty())) .thenThrow(new EnclaveException("")); final PrivCreatePrivacyGroup privCreatePrivacyGroup = new PrivCreatePrivacyGroup(privacyController); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java index d231cc8c2c8..4db3a63b900 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java @@ -58,11 +58,10 @@ public void before() { @Test public void validTransactionHashReturnedAfterDistribute() { final String enclavePublicKey = "93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng="; - when(privacyController.sendTransaction( - any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser()))) + when(privacyController.sendTransaction(any(PrivateTransaction.class), any())) .thenReturn(new SendTransactionResponse(enclavePublicKey, "")); when(privacyController.validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser()))) + any(PrivateTransaction.class), any(String.class), any())) .thenReturn(ValidationResult.valid()); final JsonRpcRequestContext request = @@ -79,10 +78,8 @@ public void validTransactionHashReturnedAfterDistribute() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); + verify(privacyController).sendTransaction(any(PrivateTransaction.class), any()); verify(privacyController) - .sendTransaction(any(PrivateTransaction.class), enclavePublicKey(requestContext.getUser())); - verify(privacyController) - .validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), enclavePublicKey(request.getUser())); + .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class), any()); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index ec3502e5a92..35321220a5d 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -130,8 +130,7 @@ public void returnsPrivateTransactionLegacy() { final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); - when(privacyController.retrieveTransaction( - anyString(), enclavePublicKey(requestContext.getUser()))) + when(privacyController.retrieveTransaction(anyString(), any())) .thenReturn( new ReceiveResponse( Base64.getEncoder().encodeToString(bvrlp.encoded().extractArray()).getBytes(UTF_8), @@ -166,8 +165,7 @@ public void returnsPrivateTransactionGroup() { final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); - when(privacyController.retrieveTransaction( - anyString(), enclavePublicKey(requestContext.getUser()))) + when(privacyController.retrieveTransaction(anyString(), any())) .thenReturn( new ReceiveResponse( Base64.getEncoder().encodeToString(bvrlp.encoded().extractArray()).getBytes(UTF_8), diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java index aebbd1bdd23..b1cf1369d7a 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java @@ -28,6 +28,8 @@ import org.hyperledger.besu.util.bytes.BytesValue; import org.hyperledger.besu.util.bytes.BytesValues; +import java.util.Optional; + import org.junit.Before; import org.junit.Test; @@ -46,8 +48,7 @@ public class PrivGetTransactionCountTest { @Before public void before() { when(privacyParameters.isEnabled()).thenReturn(true); - when(privacyController.determineNonce( - senderAddress, privacyGroupId, enclavePublicKey(requestContext.getUser()))) + when(privacyController.determineNonce(senderAddress, privacyGroupId, Optional.empty())) .thenReturn(NONCE); } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java index af396c40851..6ef2aa4793a 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java @@ -147,8 +147,7 @@ public class PrivGetTransactionReceiptTest { @Before public void setUp() { - when(privacyController.retrieveTransaction( - anyString(), enclavePublicKey(requestContext.getUser()))) + when(privacyController.retrieveTransaction(anyString(), any())) .thenReturn( new ReceiveResponse( Base64.getEncoder().encode(RLP.encode(privateTransaction::writeTo).extractArray()), @@ -193,8 +192,7 @@ public void returnReceiptIfTransactionExists() { @Test public void enclavePayloadNotFoundResultsInSuccessButNullResponse() { - when(failingPrivacyController.retrieveTransaction( - anyString(), enclavePublicKey(requestContext.getUser()))) + when(failingPrivacyController.retrieveTransaction(anyString(), any())) .thenThrow(new EnclaveException("EnclavePayloadNotFound")); final PrivGetTransactionReceipt privGetTransactionReceipt = @@ -233,8 +231,7 @@ public void markerTransactionNotAvailableResultsInNullResponse() { @Test public void enclaveConnectionIssueThrowsRuntimeException() { - when(failingPrivacyController.retrieveTransaction( - anyString(), enclavePublicKey(requestContext.getUser()))) + when(failingPrivacyController.retrieveTransaction(anyString(), any())) .thenThrow(EnclaveException.class); final PrivGetTransactionReceipt privGetTransactionReceipt = new PrivGetTransactionReceipt( @@ -270,7 +267,7 @@ public void transactionReceiptContainsRevertReasonWhenInvalidTransactionOccurs() public void enclaveKeysCannotDecryptPayloadThrowsRuntimeException() { final String keysCannotDecryptPayloadMsg = "EnclaveKeysCannotDecryptPayload"; when(privacyParameters.getEnclave()).thenReturn(enclave); - when(privacyController.retrieveTransaction(any(), enclavePublicKey(requestContext.getUser()))) + when(privacyController.retrieveTransaction(any(), any())) .thenThrow(new EnclaveException(keysCannotDecryptPayloadMsg)); final PrivGetTransactionReceipt privGetTransactionReceipt = diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java index b42a287f42f..5da9e038028 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java @@ -163,13 +163,11 @@ public void sendsValidLegacyTransaction() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(1); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); + privacyController.sendTransaction(transaction, Optional.empty()); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, - sendTransactionResponse.getPrivacyGroupId(), - enclavePublicKey(request.getUser())); + transaction, sendTransactionResponse.getPrivacyGroupId(), Optional.empty()); final Transaction markerTransaction = privacyController.createPrivacyMarkerTransaction( @@ -189,13 +187,11 @@ public void sendValidBesuTransaction() { final PrivateTransaction transaction = buildBesuPrivateTransaction(1); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); + privacyController.sendTransaction(transaction, Optional.empty()); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, - transaction.getPrivacyGroupId().get().toString(), - enclavePublicKey(request.getUser())); + transaction, transaction.getPrivacyGroupId().get().toString(), Optional.empty()); final Transaction markerTransaction = privacyController.createPrivacyMarkerTransaction( @@ -215,7 +211,7 @@ public void sendTransactionWhenEnclaveFailsThrowsEnclaveError() { .isThrownBy( () -> brokenPrivacyController.sendTransaction( - buildLegacyPrivateTransaction(), enclavePublicKey(requestContext.getUser()))); + buildLegacyPrivateTransaction(), Optional.empty())); } @Test @@ -225,12 +221,10 @@ public void validateTransactionWithTooLowNonceReturnsError() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(0); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); + privacyController.sendTransaction(transaction, Optional.empty()); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, - sendTransactionResponse.getPrivacyGroupId(), - enclavePublicKey(request.getUser())); + transaction, sendTransactionResponse.getPrivacyGroupId(), Optional.empty()); assertThat(validationResult).isEqualTo(ValidationResult.invalid(PRIVATE_NONCE_TOO_LOW)); } @@ -242,12 +236,10 @@ public void validateTransactionWithIncorrectNonceReturnsError() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(2); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, enclavePublicKey(requestContext.getUser())); + privacyController.sendTransaction(transaction, Optional.empty()); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, - sendTransactionResponse.getPrivacyGroupId(), - enclavePublicKey(request.getUser())); + transaction, sendTransactionResponse.getPrivacyGroupId(), Optional.empty()); assertThat(validationResult).isEqualTo(ValidationResult.invalid(INCORRECT_PRIVATE_NONCE)); } @@ -257,8 +249,7 @@ public void retrievesTransaction() { .thenReturn(new ReceiveResponse(PAYLOAD, PRIVACY_GROUP_ID)); final ReceiveResponse receiveResponse = - privacyController.retrieveTransaction( - TRANSACTION_KEY, enclavePublicKey(requestContext.getUser())); + privacyController.retrieveTransaction(TRANSACTION_KEY, Optional.empty()); assertThat(receiveResponse.getPayload()).isEqualTo(PAYLOAD); assertThat(receiveResponse.getPrivacyGroupId()).isEqualTo(PRIVACY_GROUP_ID); @@ -282,7 +273,7 @@ public void createsPrivacyGroup() { PRIVACY_GROUP_ADDRESSES, PRIVACY_GROUP_NAME, PRIVACY_GROUP_DESCRIPTION, - enclavePublicKey(requestContext.getUser())); + Optional.empty()); assertThat(privacyGroup).isEqualToComparingFieldByField(enclavePrivacyGroupResponse); verify(enclave) @@ -298,8 +289,7 @@ public void deletesPrivacyGroup() { when(enclave.deletePrivacyGroup(anyString(), anyString())).thenReturn(PRIVACY_GROUP_ID); final String deletedPrivacyGroupId = - privacyController.deletePrivacyGroup( - PRIVACY_GROUP_ID, enclavePublicKey(requestContext.getUser())); + privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.empty()); assertThat(deletedPrivacyGroupId).isEqualTo(PRIVACY_GROUP_ID); verify(enclave).deletePrivacyGroup(PRIVACY_GROUP_ID, enclavePublicKey); @@ -317,8 +307,7 @@ public void findsPrivacyGroup() { when(enclave.findPrivacyGroup(any())).thenReturn(new PrivacyGroup[] {privacyGroup}); final PrivacyGroup[] privacyGroups = - privacyController.findPrivacyGroup( - PRIVACY_GROUP_ADDRESSES, enclavePublicKey(requestContext.getUser())); + privacyController.findPrivacyGroup(PRIVACY_GROUP_ADDRESSES, Optional.empty()); assertThat(privacyGroups).hasSize(1); assertThat(privacyGroups[0]).isEqualToComparingFieldByField(privacyGroup); verify(enclave).findPrivacyGroup(PRIVACY_GROUP_ADDRESSES); @@ -338,10 +327,7 @@ public void determinesNonceForEeaRequest() { final long nonce = privacyController.determineNonce( - "privateFrom", - new String[] {"first", "second"}, - address, - enclavePublicKey(requestContext.getUser())); + "privateFrom", new String[] {"first", "second"}, address, Optional.empty()); assertThat(nonce).isEqualTo(reportedNonce); verify(enclave) @@ -359,10 +345,7 @@ public void determineNonceForEeaRequestWithNoMatchingGroupReturnsZero() { final long nonce = privacyController.determineNonce( - "privateFrom", - new String[] {"first", "second"}, - address, - enclavePublicKey(requestContext.getUser())); + "privateFrom", new String[] {"first", "second"}, address, Optional.empty()); assertThat(nonce).isEqualTo(reportedNonce); verify(enclave) @@ -385,10 +368,7 @@ public void determineNonceForEeaRequestWithMoreThanOneMatchingGroupThrowsExcepti .isThrownBy( () -> privacyController.determineNonce( - "privateFrom", - new String[] {"first", "second"}, - address, - enclavePublicKey(requestContext.getUser()))); + "privateFrom", new String[] {"first", "second"}, address, Optional.empty())); } @Test @@ -397,9 +377,7 @@ public void determineNonceForPrivacyGroupRequestWhenAccountExists() { when(account.getNonce()).thenReturn(4L); - final long nonce = - privacyController.determineNonce( - address, "Group1", enclavePublicKey(requestContext.getUser())); + final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); assertThat(nonce).isEqualTo(4L); verify(privateStateStorage).getLatestStateRoot(BytesValues.fromBase64("Group1")); @@ -414,9 +392,7 @@ public void determineNonceForPrivacyGroupRequestWhenPrivateStateDoesNotExist() { when(privateStateStorage.getLatestStateRoot(BytesValues.fromBase64("Group1"))) .thenReturn(Optional.empty()); - final long nonce = - privacyController.determineNonce( - address, "Group1", enclavePublicKey(requestContext.getUser())); + final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(worldStateArchive, mutableWorldState, account); @@ -430,9 +406,7 @@ public void determineNonceForPrivacyGroupRequestWhenWorldStateDoesNotExist() { .thenReturn(Optional.of(hash)); when(worldStateArchive.getMutable(hash)).thenReturn(Optional.empty()); - final long nonce = - privacyController.determineNonce( - address, "Group1", enclavePublicKey(requestContext.getUser())); + final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(mutableWorldState, account); @@ -447,9 +421,7 @@ public void determineNonceForPrivacyGroupRequestWhenAccountDoesNotExist() { when(worldStateArchive.getMutable(hash)).thenReturn(Optional.of(mutableWorldState)); when(mutableWorldState.get(address)).thenReturn(null); - final long nonce = - privacyController.determineNonce( - address, "Group1", enclavePublicKey(requestContext.getUser())); + final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(account); From 3fca1ec28cb89391b8617818c2e223ffdfb89f5a Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 10:52:53 +1000 Subject: [PATCH 03/13] unit tests - verify enclave public key is passed to the privacy controller Signed-off-by: Jason Frame --- .../eea/EeaSendRawTransactionTest.java | 20 ++++++++++---- .../priv/PrivCreatePrivacyGroupTest.java | 14 ++++++++-- .../PrivDistributeRawTransactionTest.java | 19 +++++++++++--- .../priv/PrivGetPrivateTransactionTest.java | 26 ++++++++++++++----- .../priv/PrivGetTransactionCountTest.java | 14 ++++++++-- .../priv/PrivGetTransactionReceiptTest.java | 16 ++++++++++-- 6 files changed, 88 insertions(+), 21 deletions(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java index e258a07c417..9a58f67df27 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java @@ -16,6 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyZeroInteractions; @@ -43,6 +44,9 @@ import java.math.BigInteger; import java.util.Optional; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -106,9 +110,12 @@ public class EeaSendRawTransactionTest { BytesValue.fromHexString("0x"), Address.wrap(BytesValue.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f")), Optional.empty()); + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; - final String MOCK_ORION_KEY = ""; - final String MOCK_PRIVACY_GROUP = ""; + private final String MOCK_ORION_KEY = ""; + private final String MOCK_PRIVACY_GROUP = ""; + private final User user = + new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); @Mock private TransactionPool transactionPool; @@ -208,7 +215,8 @@ public void validTransactionIsSentToTransactionPool() { final JsonRpcRequestContext request = new JsonRpcRequestContext( new JsonRpcRequest( - "2.0", "eea_sendRawTransaction", new String[] {VALID_PRIVATE_TRANSACTION_RLP})); + "2.0", "eea_sendRawTransaction", new String[] {VALID_PRIVATE_TRANSACTION_RLP}), + user); final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse( @@ -218,9 +226,11 @@ public void validTransactionIsSentToTransactionPool() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); - verify(privacyController).sendTransaction(any(PrivateTransaction.class), any()); verify(privacyController) - .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class), any()); + .sendTransaction(any(PrivateTransaction.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); + verify(privacyController) + .validatePrivateTransaction( + any(PrivateTransaction.class), any(String.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java index 1fdadf15183..5e110999607 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.catchThrowableOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.enclave.Enclave; @@ -36,6 +37,9 @@ import java.util.List; import java.util.Optional; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.assertj.core.util.Lists; import org.junit.Before; import org.junit.Test; @@ -46,10 +50,13 @@ public class PrivCreatePrivacyGroupTest { private static final String NAME = "testName"; private static final String DESCRIPTION = "testDesc"; private static final List ADDRESSES = Lists.newArrayList(FROM, "second participant"); + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; private final Enclave enclave = mock(Enclave.class); private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); private final PrivacyController privacyController = mock(PrivacyController.class); + private final User user = + new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); @Before public void setUp() { @@ -62,7 +69,8 @@ public void verifyCreatePrivacyGroup() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, Optional.empty())) + when(privacyController.createPrivacyGroup( + ADDRESSES, NAME, DESCRIPTION, Optional.of(ENCLAVE_PUBLIC_KEY))) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); @@ -75,7 +83,7 @@ public void verifyCreatePrivacyGroup() { final Object[] params = new Object[] {param}; final JsonRpcRequestContext request = - new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_createPrivacyGroup", params)); + new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_createPrivacyGroup", params), user); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privCreatePrivacyGroup.response(request); @@ -83,6 +91,8 @@ public void verifyCreatePrivacyGroup() { final String result = (String) response.getResult(); assertThat(result).isEqualTo(expected); + verify(privacyController) + .createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, Optional.of(ENCLAVE_PUBLIC_KEY)); } @Test diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java index 4db3a63b900..295af3326ac 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java @@ -16,6 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -29,6 +30,11 @@ import org.hyperledger.besu.ethereum.privacy.SendTransactionResponse; import org.hyperledger.besu.util.bytes.BytesValues; +import java.util.Optional; + +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -46,6 +52,10 @@ public class PrivDistributeRawTransactionTest { + "e60551d7a19cf30603db5bfc23e5ac43a56f57f25f75486aa00f" + "200e885ff29e973e2576b6600181d1b0a2b5294e30d9be4a1981" + "ffb33a0b8c8a72657374726963746564"; + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + + private final User user = + new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); @Mock private PrivDistributeRawTransaction method; @Mock private PrivacyController privacyController; @@ -69,7 +79,8 @@ public void validTransactionHashReturnedAfterDistribute() { new JsonRpcRequest( "2.0", "priv_distributeRawTransaction", - new String[] {VALID_PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP})); + new String[] {VALID_PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP}), + user); final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse( @@ -78,8 +89,10 @@ public void validTransactionHashReturnedAfterDistribute() { final JsonRpcResponse actualResponse = method.response(request); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); - verify(privacyController).sendTransaction(any(PrivateTransaction.class), any()); verify(privacyController) - .validatePrivateTransaction(any(PrivateTransaction.class), any(String.class), any()); + .sendTransaction(any(PrivateTransaction.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); + verify(privacyController) + .validatePrivateTransaction( + any(PrivateTransaction.class), any(String.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index 35321220a5d..29af32a5af9 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -19,6 +19,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.crypto.SECP256K1; @@ -49,6 +50,9 @@ import java.util.Optional; import com.google.common.collect.Lists; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -65,6 +69,11 @@ public class PrivGetPrivateTransactionTest { SECP256K1.PrivateKey.create( new BigInteger( "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16))); + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + private static final String TRANSACTION_HASH = + BytesValues.fromBase64("5bpr9tz4zhmWmk9RlNng93Ky7lXwFkMc7+ckoFgUMku=").toString(); + private static final BytesValue ENCLAVE_KEY = + BytesValues.fromBase64("93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng="); private final PrivateTransaction.Builder privateTransactionBuilder = PrivateTransaction.builder() @@ -90,15 +99,14 @@ public class PrivGetPrivateTransactionTest { .privateFrom(BytesValues.fromBase64("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")) .restriction(Restriction.RESTRICTED); - private final String enclaveKey = - BytesValues.fromBase64("93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng=").toString(); - private final Enclave enclave = mock(Enclave.class); private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); private final BlockchainQueries blockchain = mock(BlockchainQueries.class); private final TransactionWithMetadata returnedTransaction = mock(TransactionWithMetadata.class); private final Transaction justTransaction = mock(Transaction.class); private final PrivacyController privacyController = mock(PrivacyController.class); + private final User user = + new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); @Before public void before() { @@ -111,7 +119,7 @@ public void returnsPrivateTransactionLegacy() { when(blockchain.transactionByHash(any(Hash.class))) .thenReturn(Optional.of(returnedTransaction)); when(returnedTransaction.getTransaction()).thenReturn(justTransaction); - when(justTransaction.getPayload()).thenReturn(BytesValues.fromBase64("")); + when(justTransaction.getPayload()).thenReturn(ENCLAVE_KEY); final PrivateTransaction privateTransaction = privateTransactionBuilder @@ -124,9 +132,10 @@ public void returnsPrivateTransactionLegacy() { final PrivGetPrivateTransaction privGetPrivateTransaction = new PrivGetPrivateTransaction(blockchain, privacyController); - final Object[] params = new Object[] {enclaveKey}; + final Object[] params = new Object[] {TRANSACTION_HASH}; final JsonRpcRequestContext request = - new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getPrivateTransaction", params)); + new JsonRpcRequestContext( + new JsonRpcRequest("1", "priv_getPrivateTransaction", params), user); final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); @@ -140,6 +149,9 @@ public void returnsPrivateTransactionLegacy() { final PrivateTransactionResult result = (PrivateTransactionResult) response.getResult(); assertThat(result).isEqualToComparingFieldByField(privateTransactionLegacyResult); + verify(privacyController) + .retrieveTransaction( + BytesValues.asBase64String(ENCLAVE_KEY), Optional.of(ENCLAVE_PUBLIC_KEY)); } @Test @@ -159,7 +171,7 @@ public void returnsPrivateTransactionGroup() { final PrivGetPrivateTransaction privGetPrivateTransaction = new PrivGetPrivateTransaction(blockchain, privacyController); - final Object[] params = new Object[] {enclaveKey}; + final Object[] params = new Object[] {TRANSACTION_HASH}; final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getPrivateTransaction", params)); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java index b1cf1369d7a..6ae61fd68a9 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java @@ -17,6 +17,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; @@ -30,11 +31,15 @@ import java.util.Optional; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.junit.Before; import org.junit.Test; public class PrivGetTransactionCountTest { + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); private final PrivacyController privacyController = mock(PrivacyController.class); @@ -44,11 +49,13 @@ public class PrivGetTransactionCountTest { private final Address senderAddress = Address.fromHexString("0x627306090abab3a6e1400e9345bc60c78a8bef57"); private final long NONCE = 5; + private User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); @Before public void before() { when(privacyParameters.isEnabled()).thenReturn(true); - when(privacyController.determineNonce(senderAddress, privacyGroupId, Optional.empty())) + when(privacyController.determineNonce( + senderAddress, privacyGroupId, Optional.of(ENCLAVE_PUBLIC_KEY))) .thenReturn(NONCE); } @@ -59,11 +66,14 @@ public void verifyTransactionCount() { final Object[] params = new Object[] {senderAddress, privacyGroupId}; final JsonRpcRequestContext request = - new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionCount", params)); + new JsonRpcRequestContext( + new JsonRpcRequest("1", "priv_getTransactionCount", params), user); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privGetTransactionCount.response(request); assertThat(response.getResult()).isEqualTo(String.format("0x%X", NONCE)); + verify(privacyController) + .determineNonce(senderAddress, privacyGroupId, Optional.of(ENCLAVE_PUBLIC_KEY)); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java index 6ef2aa4793a..916a547cfb6 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java @@ -17,10 +17,12 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.hyperledger.besu.util.bytes.BytesValues.asBase64String; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.crypto.SECP256K1; @@ -57,6 +59,9 @@ import java.util.Optional; import com.google.common.collect.Lists; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -66,6 +71,8 @@ public class PrivGetTransactionReceiptTest { @Rule public final TemporaryFolder temp = new TemporaryFolder(); + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + private static final BytesValue ENCLAVE_KEY = BytesValue.wrap("EnclaveKey".getBytes(UTF_8)); private static final Address SENDER = Address.fromHexString("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); @@ -111,7 +118,7 @@ public class PrivGetTransactionReceiptTest { .gasLimit(3000000) .to(Address.fromHexString("0x627306090abab3a6e1400e9345bc60c78a8bef57")) .value(Wei.ZERO) - .payload(BytesValue.wrap("EnclaveKey".getBytes(UTF_8))) + .payload(ENCLAVE_KEY) .sender(SENDER) .chainId(BigInteger.valueOf(2018)) .signAndBuild(KEY_PAIR); @@ -137,6 +144,8 @@ public class PrivGetTransactionReceiptTest { null, Quantity.create(BytesValues.asUnsignedBigInteger(BytesValue.of(1)))); + private User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final BlockchainQueries blockchainQueries = mock(BlockchainQueries.class); private final Blockchain blockchain = mock(Blockchain.class); private final Enclave enclave = mock(Enclave.class); @@ -180,7 +189,8 @@ public void returnReceiptIfTransactionExists() { new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequestContext request = - new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params)); + new JsonRpcRequestContext( + new JsonRpcRequest("1", "priv_getTransactionReceipt", params), user); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privGetTransactionReceipt.response(request); @@ -188,6 +198,8 @@ public void returnReceiptIfTransactionExists() { (PrivateTransactionReceiptResult) response.getResult(); assertThat(result).isEqualToComparingFieldByField(expectedResult); + verify(privacyController) + .retrieveTransaction(asBase64String(ENCLAVE_KEY), Optional.of(ENCLAVE_PUBLIC_KEY)); } @Test From b2b48e5a1cf000bb4c85dd228615a064ac05846f Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 11:31:43 +1000 Subject: [PATCH 04/13] unit tests - test for delete and find Signed-off-by: Jason Frame --- .../methods/priv/PrivDeletePrivacyGroup.java | 3 +- .../priv/PrivDeletePrivacyGroupTest.java | 89 +++++++++++++++++ .../priv/PrivFindPrivacyGroupTest.java | 99 +++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java create mode 100644 ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java index 75b4854faf5..c38bbadecd8 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java @@ -21,6 +21,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.privacy.PrivacyController; @@ -54,7 +55,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { privacyGroupId, enclavePublicKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to fetch transaction", e); - return new JsonRpcSuccessResponse( + return new JsonRpcErrorResponse( requestContext.getRequest().getId(), JsonRpcError.DELETE_PRIVACY_GROUP_ERROR); } return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), response); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java new file mode 100644 index 00000000000..c65fe5eb6e5 --- /dev/null +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java @@ -0,0 +1,89 @@ +/* + * Copyright ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.hyperledger.besu.enclave.Enclave; +import org.hyperledger.besu.enclave.EnclaveException; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.privacy.PrivacyController; + +import java.util.Optional; + +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; +import org.junit.Before; +import org.junit.Test; + +public class PrivDeletePrivacyGroupTest { + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + private static final String PRIVACY_GROUP_ID = "privacyGroupId"; + + private final Enclave enclave = mock(Enclave.class); + private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); + private final PrivacyController privacyController = mock(PrivacyController.class); + private final User user = + new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private JsonRpcRequestContext request; + + @Before + public void setUp() { + when(privacyParameters.getEnclave()).thenReturn(enclave); + when(privacyParameters.isEnabled()).thenReturn(true); + request = + new JsonRpcRequestContext( + new JsonRpcRequest("1", "priv_deletePrivacyGroup", new Object[] {PRIVACY_GROUP_ID}), + user); + } + + @Test + public void deletesPrivacyGroupWithValidGroupId() { + when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY))) + .thenReturn(PRIVACY_GROUP_ID); + + final PrivDeletePrivacyGroup privDeletePrivacyGroup = + new PrivDeletePrivacyGroup(privacyController); + + final JsonRpcSuccessResponse response = + (JsonRpcSuccessResponse) privDeletePrivacyGroup.response(request); + final String result = (String) response.getResult(); + assertThat(result).isEqualTo(PRIVACY_GROUP_ID); + verify(privacyController).deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY)); + } + + @Test + public void failsWithDeletePrivacyGroupErrorIfEnclaveFails() { + when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY))) + .thenThrow(new EnclaveException("")); + + final PrivDeletePrivacyGroup privDeletePrivacyGroup = + new PrivDeletePrivacyGroup(privacyController); + + final JsonRpcErrorResponse response = + (JsonRpcErrorResponse) privDeletePrivacyGroup.response(request); + assertThat(response.getError()).isEqualTo(JsonRpcError.DELETE_PRIVACY_GROUP_ERROR); + verify(privacyController).deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY)); + } +} diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java new file mode 100644 index 00000000000..62e4471bfac --- /dev/null +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java @@ -0,0 +1,99 @@ +/* + * Copyright ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.hyperledger.besu.enclave.Enclave; +import org.hyperledger.besu.enclave.EnclaveException; +import org.hyperledger.besu.enclave.types.PrivacyGroup; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.privacy.PrivacyController; + +import java.util.List; +import java.util.Optional; + +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; +import org.assertj.core.util.Lists; +import org.junit.Before; +import org.junit.Test; + +public class PrivFindPrivacyGroupTest { + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + private static final List ADDRESSES = + Lists.newArrayList( + "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", + "0x627306090abab3a6e1400e9345bc60c78a8bef57"); + + private final Enclave enclave = mock(Enclave.class); + private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); + private final PrivacyController privacyController = mock(PrivacyController.class); + private final User user = + new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + + private JsonRpcRequestContext request; + private PrivacyGroup privacyGroup; + + @Before + public void setUp() { + when(privacyParameters.getEnclave()).thenReturn(enclave); + when(privacyParameters.isEnabled()).thenReturn(true); + request = + new JsonRpcRequestContext( + new JsonRpcRequest("1", "priv_deletePrivacyGroup", new Object[] {ADDRESSES}), user); + privacyGroup = new PrivacyGroup(); + privacyGroup.setName("privacyGroup"); + privacyGroup.setDescription("privacyGroup desc"); + privacyGroup.setPrivacyGroupId("privacy group id"); + privacyGroup.setMembers(Lists.list("member1")); + } + + @Test + public void findsPrivacyGroupWithValidAddresses() { + when(privacyController.findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY))) + .thenReturn(new PrivacyGroup[] {privacyGroup}); + + final PrivFindPrivacyGroup privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyController); + + final JsonRpcSuccessResponse response = + (JsonRpcSuccessResponse) privFindPrivacyGroup.response(request); + final PrivacyGroup[] result = (PrivacyGroup[]) response.getResult(); + assertThat(result).hasSize(1); + assertThat(result[0]).isEqualToComparingFieldByField(privacyGroup); + verify(privacyController).findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY)); + } + + @Test + public void failsWithFindPrivacyGroupErrorIfEnclaveFails() { + when(privacyController.findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY))) + .thenThrow(new EnclaveException("")); + final PrivFindPrivacyGroup privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyController); + + final JsonRpcErrorResponse response = + (JsonRpcErrorResponse) privFindPrivacyGroup.response(request); + assertThat(response.getError()).isEqualTo(JsonRpcError.FIND_PRIVACY_GROUP_ERROR); + verify(privacyController).findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY)); + } +} From b35f610a4c1c0b212bf52628f745530fb55ea125 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 11:40:22 +1000 Subject: [PATCH 05/13] spotless Signed-off-by: Jason Frame --- .../besu/ethereum/privacy/PrivacyControllerTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java index 8e3768dcaf7..273b0cc4edd 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java @@ -209,7 +209,10 @@ public void sendValidBesuTransaction() { @Test public void sendTransactionWhenEnclaveFailsThrowsEnclaveError() { assertThatExceptionOfType(EnclaveServerException.class) - .isThrownBy(() -> brokenPrivacyController.sendTransaction(buildLegacyPrivateTransaction(), Optional.empty())); + .isThrownBy( + () -> + brokenPrivacyController.sendTransaction( + buildLegacyPrivateTransaction(), Optional.empty())); } @Test From 6ebf1730b77d35e9661cca69714575514f6266b3 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 11:51:58 +1000 Subject: [PATCH 06/13] fix unit tests after merge from master Signed-off-by: Jason Frame --- .../privacy/methods/priv/PrivDeletePrivacyGroupTest.java | 3 +-- .../privacy/methods/priv/PrivFindPrivacyGroupTest.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java index c65fe5eb6e5..d5d7607d39d 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java @@ -20,7 +20,6 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.enclave.Enclave; -import org.hyperledger.besu.enclave.EnclaveException; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; @@ -76,7 +75,7 @@ public void deletesPrivacyGroupWithValidGroupId() { @Test public void failsWithDeletePrivacyGroupErrorIfEnclaveFails() { when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY))) - .thenThrow(new EnclaveException("")); + .thenThrow(new IllegalStateException("some failure")); final PrivDeletePrivacyGroup privDeletePrivacyGroup = new PrivDeletePrivacyGroup(privacyController); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java index 62e4471bfac..ad534d88e2b 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java @@ -20,7 +20,6 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.enclave.Enclave; -import org.hyperledger.besu.enclave.EnclaveException; import org.hyperledger.besu.enclave.types.PrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; @@ -88,7 +87,7 @@ public void findsPrivacyGroupWithValidAddresses() { @Test public void failsWithFindPrivacyGroupErrorIfEnclaveFails() { when(privacyController.findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY))) - .thenThrow(new EnclaveException("")); + .thenThrow(new IllegalStateException("some failure")); final PrivFindPrivacyGroup privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyController); final JsonRpcErrorResponse response = From d4d0923c9500aac9e1112dbbc51ee210168dc678 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 13:03:13 +1000 Subject: [PATCH 07/13] fix unit tests after merge from master Signed-off-by: Jason Frame --- .../methods/priv/PrivGetPrivateTransaction.java | 3 ++- .../methods/priv/PrivGetTransactionReceipt.java | 4 +++- .../methods/eea/EeaSendRawTransactionTest.java | 5 ++++- .../priv/PrivDistributeRawTransactionTest.java | 3 +-- .../priv/PrivGetPrivateTransactionTest.java | 15 ++++++++------- .../priv/PrivGetTransactionReceiptTest.java | 5 ++--- .../besu/ethereum/privacy/PrivacyController.java | 4 +++- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java index b280c8ed4d7..642ca89c88c 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java @@ -69,7 +69,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { LOG.trace("Fetching transaction information"); final ReceiveResponse receiveResponse = privacyController.retrieveTransaction( - resultTransaction.getTransaction().getPayloadBytes().toBase64String()); + resultTransaction.getTransaction().getPayloadBytes().toBase64String(), + enclavePublicKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput input = diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java index 44938517754..af08d6c025e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java @@ -93,7 +93,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final String privacyGroupId; try { final ReceiveResponse receiveResponse = - privacyController.retrieveTransaction(transaction.getPayloadBytes().toBase64String(), enclavePublicKey(requestContext.getUser())); + privacyController.retrieveTransaction( + transaction.getPayloadBytes().toBase64String(), + enclavePublicKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput input = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java index e34b83d4d1f..c4543629f9b 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java @@ -43,6 +43,9 @@ import java.math.BigInteger; import java.util.Optional; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.apache.tuweni.bytes.Bytes; import org.junit.Before; import org.junit.Test; @@ -212,7 +215,7 @@ public void validTransactionIsSentToTransactionPool() { final JsonRpcRequestContext request = new JsonRpcRequestContext( new JsonRpcRequest( - "2.0", "eea_sendRawTransaction", new String[] {VALID_PRIVATE_TRANSACTION_RLP})), + "2.0", "eea_sendRawTransaction", new String[] {VALID_PRIVATE_TRANSACTION_RLP}), user); final JsonRpcResponse expectedResponse = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java index cc7a2bad30b..b33dafab083 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java @@ -30,13 +30,12 @@ import org.hyperledger.besu.ethereum.privacy.SendTransactionResponse; import java.util.Base64; - -import org.apache.tuweni.bytes.Bytes; import java.util.Optional; import io.vertx.core.json.JsonObject; import io.vertx.ext.auth.User; import io.vertx.ext.auth.jwt.impl.JWTUser; +import org.apache.tuweni.bytes.Bytes; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index 8bea6a9fd71..901bbef99ea 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -48,6 +48,9 @@ import java.util.Optional; import com.google.common.collect.Lists; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.apache.tuweni.bytes.Bytes; import org.junit.Before; import org.junit.Rule; @@ -67,9 +70,9 @@ public class PrivGetPrivateTransactionTest { "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16))); private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; private static final String TRANSACTION_HASH = - BytesValues.fromBase64("5bpr9tz4zhmWmk9RlNng93Ky7lXwFkMc7+ckoFgUMku=").toString(); - private static final BytesValue ENCLAVE_KEY = - BytesValues.fromBase64("93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng="); + Bytes.fromBase64String("5bpr9tz4zhmWmk9RlNng93Ky7lXwFkMc7+ckoFgUMku=").toString(); + private static final Bytes ENCLAVE_KEY = + Bytes.fromBase64String("93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng="); private final PrivateTransaction.Builder privateTransactionBuilder = PrivateTransaction.builder() @@ -118,8 +121,7 @@ public void returnsPrivateTransactionLegacy() { when(blockchain.transactionByHash(any(Hash.class))) .thenReturn(Optional.of(returnedTransaction)); when(returnedTransaction.getTransaction()).thenReturn(justTransaction); - when(justTransaction.getPayload()).thenReturn(ENCLAVE_KEY); - when(justTransaction.getPayloadBytes()).thenReturn(Bytes.fromBase64String("")); + when(justTransaction.getPayloadBytes()).thenReturn(ENCLAVE_KEY); final PrivateTransaction privateTransaction = privateTransactionBuilder @@ -149,8 +151,7 @@ public void returnsPrivateTransactionLegacy() { assertThat(result).isEqualToComparingFieldByField(privateTransactionLegacyResult); verify(privacyController) - .retrieveTransaction( - BytesValues.asBase64String(ENCLAVE_KEY), Optional.of(ENCLAVE_PUBLIC_KEY)); + .retrieveTransaction(ENCLAVE_KEY.toBase64String(), Optional.of(ENCLAVE_PUBLIC_KEY)); } @Test diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java index c3a89b83030..13d3fc74ce4 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java @@ -17,7 +17,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import static org.hyperledger.besu.util.bytes.BytesValues.asBase64String; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; @@ -71,7 +70,7 @@ public class PrivGetTransactionReceiptTest { @Rule public final TemporaryFolder temp = new TemporaryFolder(); private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; - private static final BytesValue ENCLAVE_KEY = BytesValue.wrap("EnclaveKey".getBytes(UTF_8)); + private static final Bytes ENCLAVE_KEY = Bytes.wrap("EnclaveKey".getBytes(UTF_8)); private static final Address SENDER = Address.fromHexString("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); @@ -192,7 +191,7 @@ public void returnReceiptIfTransactionExists() { assertThat(result).isEqualToComparingFieldByField(expectedResult); verify(privacyController) - .retrieveTransaction(asBase64String(ENCLAVE_KEY), Optional.of(ENCLAVE_PUBLIC_KEY)); + .retrieveTransaction(ENCLAVE_KEY.toBase64String(), Optional.of(ENCLAVE_PUBLIC_KEY)); } @Test diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java index 7be8c7c01da..a6bdb6e7875 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java @@ -198,7 +198,9 @@ private SendResponse sendRequest(final PrivateTransaction privateTransaction) { if (privateTransaction.getPrivacyGroupId().isPresent()) { return enclave.send( - payload, defaultEnclavePublicKey, privateTransaction.getPrivacyGroupId().get().toBase64String()); + payload, + defaultEnclavePublicKey, + privateTransaction.getPrivacyGroupId().get().toBase64String()); } else { final List privateFor = privateTransaction.getPrivateFor().get().stream() From ff42bea72e6940125c6dbffcdafb8c7e20113149 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 13:43:28 +1000 Subject: [PATCH 08/13] remove unused var Signed-off-by: Jason Frame --- .../privacy/methods/priv/PrivGetPrivateTransactionTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index 901bbef99ea..5aa4158d97b 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -98,9 +98,6 @@ public class PrivGetPrivateTransactionTest { .privateFrom(Bytes.fromBase64String("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")) .restriction(Restriction.RESTRICTED); - private final String enclaveKey = - Bytes.fromBase64String("93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng=").toString(); - private final Enclave enclave = mock(Enclave.class); private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); private final BlockchainQueries blockchain = mock(BlockchainQueries.class); From b18c7449204a8d9197386721944b8410d605d4e4 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 14:28:25 +1000 Subject: [PATCH 09/13] pr changes Signed-off-by: Jason Frame --- .../internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java index ad534d88e2b..3f0032a7edb 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java @@ -42,7 +42,7 @@ public class PrivFindPrivacyGroupTest { private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; private static final List ADDRESSES = - Lists.newArrayList( + List.of( "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", "0x627306090abab3a6e1400e9345bc60c78a8bef57"); From 36b0827d59fe8850d4fe9746c90b9ad26e48d469 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 16:44:26 +1000 Subject: [PATCH 10/13] implemented multi-tenancy enclave public key resolution. and pulled the enclave public key resolution out of the privacy controller Signed-off-by: Jason Frame --- ...vGetPrivateTransactionIntegrationTest.java | 7 +- .../methods/EnclavePublicKeyProvider.java | 24 ++++ .../methods/PrivacySendTransaction.java | 12 +- .../methods/eea/EeaSendRawTransaction.java | 14 ++- .../methods/priv/PrivCreatePrivacyGroup.java | 10 +- .../methods/priv/PrivDeletePrivacyGroup.java | 10 +- .../priv/PrivDistributeRawTransaction.java | 14 ++- .../methods/priv/PrivFindPrivacyGroup.java | 11 +- .../priv/PrivGetEeaTransactionCount.java | 13 ++- .../priv/PrivGetPrivateTransaction.java | 10 +- .../methods/priv/PrivGetTransactionCount.java | 13 ++- .../priv/PrivGetTransactionReceipt.java | 9 +- .../jsonrpc/methods/EeaJsonRpcMethods.java | 16 ++- .../jsonrpc/methods/PrivJsonRpcMethods.java | 23 ++-- .../PrivacyApiGroupJsonRpcMethods.java | 19 +++- .../eea/PrivGetEeaTransactionCountTest.java | 20 ++-- .../eea/EeaSendRawTransactionTest.java | 9 +- .../priv/PrivCreatePrivacyGroupTest.java | 31 +++--- .../priv/PrivDeletePrivacyGroupTest.java | 16 +-- .../PrivDistributeRawTransactionTest.java | 9 +- .../priv/PrivFindPrivacyGroupTest.java | 17 +-- .../priv/PrivGetPrivateTransactionTest.java | 9 +- .../priv/PrivGetTransactionCountTest.java | 12 +- .../priv/PrivGetTransactionReceiptTest.java | 23 ++-- .../PrivacyApiGroupJsonRpcMethodsTest.java | 104 +++++++++++++++--- .../ethereum/privacy/PrivacyController.java | 37 +++---- .../privacy/PrivacyControllerTest.java | 61 +++++----- 27 files changed, 367 insertions(+), 186 deletions(-) create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/EnclavePublicKeyProvider.java diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java index 035fbf1383d..d388f79c567 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java @@ -26,6 +26,7 @@ import org.hyperledger.besu.enclave.types.SendResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivateTransaction; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult; @@ -75,6 +76,8 @@ public class PrivGetPrivateTransactionIntegrationTest { private static Vertx vertx = Vertx.vertx(); + private EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; + @BeforeClass public static void setUpOnce() throws Exception { folder.create(); @@ -88,7 +91,7 @@ public static void setUpOnce() throws Exception { final EnclaveFactory factory = new EnclaveFactory(vertx); enclave = factory.createVertxEnclave(testHarness.clientUrl()); - privacyController = new PrivacyController(enclave, ENCLAVE_PUBLIC_KEY, null, null, null, null); + privacyController = new PrivacyController(enclave, null, null, null, null); } @AfterClass @@ -147,7 +150,7 @@ public void before() { public void returnsStoredPrivateTransaction() { final PrivGetPrivateTransaction privGetPrivateTransaction = - new PrivGetPrivateTransaction(blockchain, privacyController); + new PrivGetPrivateTransaction(blockchain, privacyController, enclavePublicKeyProvider); when(blockchain.transactionByHash(any(Hash.class))) .thenReturn(Optional.of(returnedTransaction)); diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/EnclavePublicKeyProvider.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/EnclavePublicKeyProvider.java new file mode 100644 index 00000000000..44a3350530b --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/EnclavePublicKeyProvider.java @@ -0,0 +1,24 @@ +/* + * Copyright ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods; + +import java.util.Optional; + +import io.vertx.ext.auth.User; + +@FunctionalInterface +public interface EnclavePublicKeyProvider { + String getEnclaveKey(Optional user); +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java index a997be12bef..8750a1f3497 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java @@ -14,8 +14,6 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; - import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException; @@ -39,9 +37,13 @@ public class PrivacySendTransaction { private static final Logger LOG = LogManager.getLogger(); protected final PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivacySendTransaction(final PrivacyController privacyController) { + public PrivacySendTransaction( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } public PrivateTransaction validateAndDecodeRequest(final JsonRpcRequestContext request) @@ -77,7 +79,9 @@ public JsonRpcResponse validateAndExecute( final Supplier successfulJsonRpcResponse) { return privacyController .validatePrivateTransaction( - privateTransaction, privacyGroupId, enclavePublicKey(request.getUser())) + privateTransaction, + privacyGroupId, + enclavePublicKeyProvider.getEnclaveKey(request.getUser())) .either( successfulJsonRpcResponse, (errorReason) -> diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java index d8e138c583b..918b1d2c529 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java @@ -14,13 +14,12 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.eea; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; - import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction.ErrorResponseException; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; @@ -35,14 +34,19 @@ public class EeaSendRawTransaction implements JsonRpcMethod { private final PrivacySendTransaction privacySendTransaction; + private EnclavePublicKeyProvider enclavePublicKeyProvider; private TransactionPool transactionPool; private PrivacyController privacyController; public EeaSendRawTransaction( - final TransactionPool transactionPool, final PrivacyController privacyController) { + final TransactionPool transactionPool, + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.transactionPool = transactionPool; this.privacyController = privacyController; - this.privacySendTransaction = new PrivacySendTransaction(privacyController); + this.privacySendTransaction = + new PrivacySendTransaction(privacyController, enclavePublicKeyProvider); + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -63,7 +67,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { sendTransactionResponse = privacyController.sendTransaction( - privateTransaction, enclavePublicKey(requestContext.getUser())); + privateTransaction, enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); } catch (final Exception e) { return new JsonRpcErrorResponse( requestContext.getRequest().getId(), diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java index 8b1095fb1d6..23e92412160 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java @@ -15,13 +15,13 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.types.PrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.parameters.CreatePrivacyGroupParameter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -34,9 +34,13 @@ public class PrivCreatePrivacyGroup implements JsonRpcMethod { private static final Logger LOG = getLogger(); private PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivCreatePrivacyGroup(final PrivacyController privacyController) { + public PrivCreatePrivacyGroup( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -63,7 +67,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { parameter.getAddresses(), parameter.getName(), parameter.getDescription(), - enclavePublicKey(requestContext.getUser())); + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to create privacy group", e); return new JsonRpcErrorResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java index c38bbadecd8..c5cc9eb311d 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java @@ -15,11 +15,11 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -32,9 +32,13 @@ public class PrivDeletePrivacyGroup implements JsonRpcMethod { private static final Logger LOG = getLogger(); private PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivDeletePrivacyGroup(final PrivacyController privacyController) { + public PrivDeletePrivacyGroup( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -52,7 +56,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { response = privacyController.deletePrivacyGroup( - privacyGroupId, enclavePublicKey(requestContext.getUser())); + privacyGroupId, enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to fetch transaction", e); return new JsonRpcErrorResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java index 37b51d4f1d7..68fd2889472 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java @@ -14,12 +14,11 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; - import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction.ErrorResponseException; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; @@ -37,10 +36,15 @@ public class PrivDistributeRawTransaction implements JsonRpcMethod { private final PrivacyController privacyController; private final PrivacySendTransaction privacySendTransaction; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivDistributeRawTransaction(final PrivacyController privacyController) { + public PrivDistributeRawTransaction( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; - this.privacySendTransaction = new PrivacySendTransaction(privacyController); + this.privacySendTransaction = + new PrivacySendTransaction(privacyController, enclavePublicKeyProvider); + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -61,7 +65,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { sendTransactionResponse = privacyController.sendTransaction( - privateTransaction, enclavePublicKey(requestContext.getUser())); + privateTransaction, enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); } catch (final Exception e) { return new JsonRpcErrorResponse( requestContext.getRequest().getId(), diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java index 6e55023dc87..16df647db85 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java @@ -15,12 +15,12 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.types.PrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -35,9 +35,13 @@ public class PrivFindPrivacyGroup implements JsonRpcMethod { private static final Logger LOG = getLogger(); private PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivFindPrivacyGroup(final PrivacyController privacyController) { + public PrivFindPrivacyGroup( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -57,7 +61,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { response = privacyController.findPrivacyGroup( - Arrays.asList(addresses), enclavePublicKey(requestContext.getUser())); + Arrays.asList(addresses), + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); } catch (Exception e) { LOG.error("Failed to fetch privacy group", e); return new JsonRpcErrorResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java index c48ed8ac533..dbdc8d55a34 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java @@ -15,11 +15,11 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -35,9 +35,13 @@ public class PrivGetEeaTransactionCount implements JsonRpcMethod { private static final Logger LOG = getLogger(); private PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivGetEeaTransactionCount(final PrivacyController privacyController) { + public PrivGetEeaTransactionCount( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -59,7 +63,10 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { final long nonce = privacyController.determineNonce( - privateFrom, privateFor, address, enclavePublicKey(requestContext.getUser())); + privateFrom, + privateFor, + address, + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); return new JsonRpcSuccessResponse( requestContext.getRequest().getId(), Quantity.create(nonce)); } catch (final Exception e) { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java index 642ca89c88c..aa7000eb384 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java @@ -16,12 +16,12 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.types.ReceiveResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult; @@ -42,11 +42,15 @@ public class PrivGetPrivateTransaction implements JsonRpcMethod { private final BlockchainQueries blockchain; private final PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; public PrivGetPrivateTransaction( - final BlockchainQueries blockchain, final PrivacyController privacyController) { + final BlockchainQueries blockchain, + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.blockchain = blockchain; this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -70,7 +74,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final ReceiveResponse receiveResponse = privacyController.retrieveTransaction( resultTransaction.getTransaction().getPayloadBytes().toBase64String(), - enclavePublicKey(requestContext.getUser())); + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput input = diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java index ee4cb856091..dd7d750dc45 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java @@ -14,11 +14,10 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; - import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -30,9 +29,13 @@ public class PrivGetTransactionCount implements JsonRpcMethod { private final PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; - public PrivGetTransactionCount(final PrivacyController privacyController) { + public PrivGetTransactionCount( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -52,7 +55,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final long nonce = privacyController.determineNonce( - address, privacyGroupId, enclavePublicKey(requestContext.getUser())); + address, + privacyGroupId, + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), Quantity.create(nonce)); } } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java index af08d6c025e..b4d72ff1cd1 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java @@ -16,7 +16,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; import org.hyperledger.besu.enclave.EnclaveClientException; import org.hyperledger.besu.enclave.types.ReceiveResponse; @@ -24,6 +23,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; @@ -57,14 +57,17 @@ public class PrivGetTransactionReceipt implements JsonRpcMethod { private final BlockchainQueries blockchain; private PrivacyParameters privacyParameters; private PrivacyController privacyController; + private EnclavePublicKeyProvider enclavePublicKeyProvider; public PrivGetTransactionReceipt( final BlockchainQueries blockchain, final PrivacyParameters privacyParameters, - final PrivacyController privacyController) { + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { this.blockchain = blockchain; this.privacyParameters = privacyParameters; this.privacyController = privacyController; + this.enclavePublicKeyProvider = enclavePublicKeyProvider; } @Override @@ -95,7 +98,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final ReceiveResponse receiveResponse = privacyController.retrieveTransaction( transaction.getPayloadBytes().toBase64String(), - enclavePublicKey(requestContext.getUser())); + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput input = diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java index 842133ba203..71edf1d4698 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java @@ -17,6 +17,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.eea.EeaSendRawTransaction; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetEeaTransactionCount; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; @@ -38,14 +39,17 @@ public EeaJsonRpcMethods( } @Override - protected RpcApi getApiGroup() { - return RpcApis.EEA; + protected Map create( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { + return mapOf( + new EeaSendRawTransaction( + getTransactionPool(), privacyController, enclavePublicKeyProvider), + new PrivGetEeaTransactionCount(privacyController, enclavePublicKeyProvider)); } @Override - protected Map create(final PrivacyController privacyController) { - return mapOf( - new EeaSendRawTransaction(getTransactionPool(), privacyController), - new PrivGetEeaTransactionCount(privacyController)); + protected RpcApi getApiGroup() { + return RpcApis.EEA; } } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java index 82744f594c9..4400dbdc98c 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java @@ -17,6 +17,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivCreatePrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDeletePrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDistributeRawTransaction; @@ -49,16 +50,22 @@ protected RpcApi getApiGroup() { } @Override - protected Map create(final PrivacyController privacyController) { + protected Map create( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { return mapOf( new PrivGetTransactionReceipt( - getBlockchainQueries(), getPrivacyParameters(), privacyController), - new PrivCreatePrivacyGroup(privacyController), - new PrivDeletePrivacyGroup(privacyController), - new PrivFindPrivacyGroup(privacyController), + getBlockchainQueries(), + getPrivacyParameters(), + privacyController, + enclavePublicKeyProvider), + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider), + new PrivDeletePrivacyGroup(privacyController, enclavePublicKeyProvider), + new PrivFindPrivacyGroup(privacyController, enclavePublicKeyProvider), new PrivGetPrivacyPrecompileAddress(getPrivacyParameters()), - new PrivGetTransactionCount(privacyController), - new PrivGetPrivateTransaction(getBlockchainQueries(), privacyController), - new PrivDistributeRawTransaction(privacyController)); + new PrivGetTransactionCount(privacyController, enclavePublicKeyProvider), + new PrivGetPrivateTransaction( + getBlockchainQueries(), privacyController, enclavePublicKeyProvider), + new PrivDistributeRawTransaction(privacyController, enclavePublicKeyProvider)); } } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java index 33c43b17534..10d83b91e62 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java @@ -14,9 +14,12 @@ */ package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyUserUtil.enclavePublicKey; + import org.hyperledger.besu.ethereum.api.jsonrpc.LatestNonceProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.DisabledPrivacyRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyRpcMethodDecorator; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.core.Address; @@ -77,13 +80,25 @@ protected Map create() { new PrivacyController( privacyParameters, protocolSchedule.getChainId(), markerTransactionFactory); - return create(privacyController).entrySet().stream() + final EnclavePublicKeyProvider enclavePublicProvider = + privacyParameters.isMultiTenancyEnabled() + ? user -> + enclavePublicKey(user) + .orElseThrow( + () -> + new IllegalStateException( + "Request does not contain an authorization token")) + : user -> privacyParameters.getEnclavePublicKey(); + + return create(privacyController, enclavePublicProvider).entrySet().stream() .collect( Collectors.toMap( Entry::getKey, entry -> createPrivacyMethod(privacyParameters, entry.getValue()))); } - protected abstract Map create(final PrivacyController privacyController); + protected abstract Map create( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider); private PrivateMarkerTransactionFactory createPrivateMarkerTransactionFactory( final PrivacyParameters privacyParameters, diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java index 4a342303297..2c748a3290a 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java @@ -20,6 +20,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetEeaTransactionCount; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; @@ -29,12 +30,11 @@ import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.privacy.PrivacyController; -import java.util.Optional; - import org.junit.Before; import org.junit.Test; public class PrivGetEeaTransactionCountTest { + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); private final PrivacyController privacyController = mock(PrivacyController.class); @@ -43,6 +43,7 @@ public class PrivGetEeaTransactionCountTest { private final String privateFrom = "thePrivateFromKey"; private final String[] privateFor = new String[] {"first", "second", "third"}; private final Address address = Address.fromHexString("55"); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; @Before public void setup() { @@ -56,9 +57,10 @@ public void setup() { @Test public void validRequestProducesExpectedNonce() { final long reportedNonce = 8L; - final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); + final PrivGetEeaTransactionCount method = + new PrivGetEeaTransactionCount(privacyController, enclavePublicKeyProvider); - when(privacyController.determineNonce(privateFrom, privateFor, address, Optional.empty())) + when(privacyController.determineNonce(privateFrom, privateFor, address, ENCLAVE_PUBLIC_KEY)) .thenReturn(reportedNonce); final JsonRpcResponse response = method.response(request); @@ -71,9 +73,10 @@ public void validRequestProducesExpectedNonce() { @Test public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() { - final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); + final PrivGetEeaTransactionCount method = + new PrivGetEeaTransactionCount(privacyController, enclavePublicKeyProvider); - when(privacyController.determineNonce(privateFrom, privateFor, address, Optional.empty())) + when(privacyController.determineNonce(privateFrom, privateFor, address, ENCLAVE_PUBLIC_KEY)) .thenThrow(RuntimeException.class); final JsonRpcResponse response = method.response(request); @@ -86,9 +89,10 @@ public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() { @Test public void nonceProviderThrowsAnExceptionProducesErrorResponse() { - final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController); + final PrivGetEeaTransactionCount method = + new PrivGetEeaTransactionCount(privacyController, enclavePublicKeyProvider); - when(privacyController.determineNonce(privateFrom, privateFor, address, Optional.empty())) + when(privacyController.determineNonce(privateFrom, privateFor, address, ENCLAVE_PUBLIC_KEY)) .thenThrow(RuntimeException.class); final JsonRpcResponse response = method.response(request); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java index c4543629f9b..a1a7bf8e7cf 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java @@ -26,6 +26,7 @@ import org.hyperledger.besu.enclave.EnclaveServerException; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -116,6 +117,7 @@ public class EeaSendRawTransactionTest { private final String MOCK_PRIVACY_GROUP = ""; private final User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; @Mock private TransactionPool transactionPool; @@ -125,7 +127,8 @@ public class EeaSendRawTransactionTest { @Before public void before() { - method = new EeaSendRawTransaction(transactionPool, privacyController); + method = + new EeaSendRawTransaction(transactionPool, privacyController, enclavePublicKeyProvider); } @Test @@ -227,10 +230,10 @@ public void validTransactionIsSentToTransactionPool() { assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); verify(privacyController) - .sendTransaction(any(PrivateTransaction.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); + .sendTransaction(any(PrivateTransaction.class), eq(ENCLAVE_PUBLIC_KEY)); verify(privacyController) .validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); + any(PrivateTransaction.class), any(String.class), eq(ENCLAVE_PUBLIC_KEY)); verify(privacyController) .createPrivacyMarkerTransaction(any(String.class), any(PrivateTransaction.class)); verify(transactionPool).addLocalTransaction(any(Transaction.class)); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java index 0d49587cc29..2f4030dfa88 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java @@ -27,6 +27,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.parameters.CreatePrivacyGroupParameter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; @@ -35,7 +36,6 @@ import org.hyperledger.besu.ethereum.privacy.PrivacyController; import java.util.List; -import java.util.Optional; import io.vertx.core.json.JsonObject; import io.vertx.ext.auth.User; @@ -57,6 +57,7 @@ public class PrivCreatePrivacyGroupTest { private final PrivacyController privacyController = mock(PrivacyController.class); private final User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; @Before public void setUp() { @@ -69,13 +70,12 @@ public void verifyCreatePrivacyGroup() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup( - ADDRESSES, NAME, DESCRIPTION, Optional.of(ENCLAVE_PUBLIC_KEY))) + when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, ENCLAVE_PUBLIC_KEY)) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final CreatePrivacyGroupParameter param = new CreatePrivacyGroupParameter(ADDRESSES, NAME, DESCRIPTION); @@ -91,8 +91,7 @@ public void verifyCreatePrivacyGroup() { final String result = (String) response.getResult(); assertThat(result).isEqualTo(expected); - verify(privacyController) - .createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, ENCLAVE_PUBLIC_KEY); } @Test @@ -100,12 +99,12 @@ public void verifyCreatePrivacyGroupWithoutDescription() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, NAME, null, Optional.empty())) + when(privacyController.createPrivacyGroup(ADDRESSES, NAME, null, ENCLAVE_PUBLIC_KEY)) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] { @@ -136,12 +135,12 @@ public void verifyCreatePrivacyGroupWithoutName() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, null, DESCRIPTION, Optional.empty())) + when(privacyController.createPrivacyGroup(ADDRESSES, null, DESCRIPTION, ENCLAVE_PUBLIC_KEY)) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] { @@ -172,12 +171,12 @@ public void verifyCreatePrivacyGroupWithoutOptionalParams() { final String expected = "a wonderful group"; final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES); - when(privacyController.createPrivacyGroup(ADDRESSES, null, null, Optional.empty())) + when(privacyController.createPrivacyGroup(ADDRESSES, null, null, ENCLAVE_PUBLIC_KEY)) .thenReturn(privacyGroup); when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] { @@ -209,7 +208,7 @@ public void returnsCorrectExceptionInvalidParam() { when(privacyParameters.getEnclavePublicKey()).thenReturn(FROM); final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] { @@ -238,7 +237,7 @@ public String getDescription() { public void returnsCorrectExceptionMissingParam() { final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {}; @@ -254,10 +253,10 @@ public void returnsCorrectExceptionMissingParam() { @Test public void returnsCorrectErrorEnclaveError() { - when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, Optional.empty())) + when(privacyController.createPrivacyGroup(ADDRESSES, NAME, DESCRIPTION, ENCLAVE_PUBLIC_KEY)) .thenThrow(new EnclaveServerException(500, "")); final PrivCreatePrivacyGroup privCreatePrivacyGroup = - new PrivCreatePrivacyGroup(privacyController); + new PrivCreatePrivacyGroup(privacyController, enclavePublicKeyProvider); final CreatePrivacyGroupParameter param = new CreatePrivacyGroupParameter(ADDRESSES, NAME, DESCRIPTION); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java index d5d7607d39d..a7c92679886 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java @@ -22,14 +22,13 @@ import org.hyperledger.besu.enclave.Enclave; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.privacy.PrivacyController; -import java.util.Optional; - import io.vertx.core.json.JsonObject; import io.vertx.ext.auth.User; import io.vertx.ext.auth.jwt.impl.JWTUser; @@ -45,6 +44,7 @@ public class PrivDeletePrivacyGroupTest { private final PrivacyController privacyController = mock(PrivacyController.class); private final User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; private JsonRpcRequestContext request; @Before @@ -59,30 +59,30 @@ public void setUp() { @Test public void deletesPrivacyGroupWithValidGroupId() { - when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY))) + when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, ENCLAVE_PUBLIC_KEY)) .thenReturn(PRIVACY_GROUP_ID); final PrivDeletePrivacyGroup privDeletePrivacyGroup = - new PrivDeletePrivacyGroup(privacyController); + new PrivDeletePrivacyGroup(privacyController, enclavePublicKeyProvider); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privDeletePrivacyGroup.response(request); final String result = (String) response.getResult(); assertThat(result).isEqualTo(PRIVACY_GROUP_ID); - verify(privacyController).deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).deletePrivacyGroup(PRIVACY_GROUP_ID, ENCLAVE_PUBLIC_KEY); } @Test public void failsWithDeletePrivacyGroupErrorIfEnclaveFails() { - when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY))) + when(privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, ENCLAVE_PUBLIC_KEY)) .thenThrow(new IllegalStateException("some failure")); final PrivDeletePrivacyGroup privDeletePrivacyGroup = - new PrivDeletePrivacyGroup(privacyController); + new PrivDeletePrivacyGroup(privacyController, enclavePublicKeyProvider); final JsonRpcErrorResponse response = (JsonRpcErrorResponse) privDeletePrivacyGroup.response(request); assertThat(response.getError()).isEqualTo(JsonRpcError.DELETE_PRIVACY_GROUP_ERROR); - verify(privacyController).deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).deletePrivacyGroup(PRIVACY_GROUP_ID, ENCLAVE_PUBLIC_KEY); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java index b33dafab083..839ad3c6fcf 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java @@ -22,6 +22,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.mainnet.ValidationResult; @@ -30,7 +31,6 @@ import org.hyperledger.besu.ethereum.privacy.SendTransactionResponse; import java.util.Base64; -import java.util.Optional; import io.vertx.core.json.JsonObject; import io.vertx.ext.auth.User; @@ -57,13 +57,14 @@ public class PrivDistributeRawTransactionTest { private final User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; @Mock private PrivDistributeRawTransaction method; @Mock private PrivacyController privacyController; @Before public void before() { - method = new PrivDistributeRawTransaction(privacyController); + method = new PrivDistributeRawTransaction(privacyController, enclavePublicKeyProvider); } @Test @@ -92,9 +93,9 @@ public void validTransactionHashReturnedAfterDistribute() { assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); verify(privacyController) - .sendTransaction(any(PrivateTransaction.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); + .sendTransaction(any(PrivateTransaction.class), eq(ENCLAVE_PUBLIC_KEY)); verify(privacyController) .validatePrivateTransaction( - any(PrivateTransaction.class), any(String.class), eq(Optional.of(ENCLAVE_PUBLIC_KEY))); + any(PrivateTransaction.class), any(String.class), eq(ENCLAVE_PUBLIC_KEY)); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java index 3f0032a7edb..1ae33a30d21 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java @@ -23,6 +23,7 @@ import org.hyperledger.besu.enclave.types.PrivacyGroup; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; @@ -30,7 +31,6 @@ import org.hyperledger.besu.ethereum.privacy.PrivacyController; import java.util.List; -import java.util.Optional; import io.vertx.core.json.JsonObject; import io.vertx.ext.auth.User; @@ -51,6 +51,7 @@ public class PrivFindPrivacyGroupTest { private final PrivacyController privacyController = mock(PrivacyController.class); private final User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; private JsonRpcRequestContext request; private PrivacyGroup privacyGroup; @@ -71,28 +72,30 @@ public void setUp() { @Test public void findsPrivacyGroupWithValidAddresses() { - when(privacyController.findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY))) + when(privacyController.findPrivacyGroup(ADDRESSES, ENCLAVE_PUBLIC_KEY)) .thenReturn(new PrivacyGroup[] {privacyGroup}); - final PrivFindPrivacyGroup privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyController); + final PrivFindPrivacyGroup privFindPrivacyGroup = + new PrivFindPrivacyGroup(privacyController, enclavePublicKeyProvider); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privFindPrivacyGroup.response(request); final PrivacyGroup[] result = (PrivacyGroup[]) response.getResult(); assertThat(result).hasSize(1); assertThat(result[0]).isEqualToComparingFieldByField(privacyGroup); - verify(privacyController).findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).findPrivacyGroup(ADDRESSES, ENCLAVE_PUBLIC_KEY); } @Test public void failsWithFindPrivacyGroupErrorIfEnclaveFails() { - when(privacyController.findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY))) + when(privacyController.findPrivacyGroup(ADDRESSES, ENCLAVE_PUBLIC_KEY)) .thenThrow(new IllegalStateException("some failure")); - final PrivFindPrivacyGroup privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyController); + final PrivFindPrivacyGroup privFindPrivacyGroup = + new PrivFindPrivacyGroup(privacyController, enclavePublicKeyProvider); final JsonRpcErrorResponse response = (JsonRpcErrorResponse) privFindPrivacyGroup.response(request); assertThat(response.getError()).isEqualTo(JsonRpcError.FIND_PRIVACY_GROUP_ERROR); - verify(privacyController).findPrivacyGroup(ADDRESSES, Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).findPrivacyGroup(ADDRESSES, ENCLAVE_PUBLIC_KEY); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index 5aa4158d97b..8c005212c24 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -27,6 +27,7 @@ import org.hyperledger.besu.enclave.types.ReceiveResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult; @@ -106,6 +107,7 @@ public class PrivGetPrivateTransactionTest { private final PrivacyController privacyController = mock(PrivacyController.class); private final User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; @Before public void before() { @@ -130,7 +132,7 @@ public void returnsPrivateTransactionLegacy() { new PrivateTransactionLegacyResult(privateTransaction); final PrivGetPrivateTransaction privGetPrivateTransaction = - new PrivGetPrivateTransaction(blockchain, privacyController); + new PrivGetPrivateTransaction(blockchain, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {TRANSACTION_HASH}; final JsonRpcRequestContext request = new JsonRpcRequestContext( @@ -147,8 +149,7 @@ public void returnsPrivateTransactionLegacy() { final PrivateTransactionResult result = (PrivateTransactionResult) response.getResult(); assertThat(result).isEqualToComparingFieldByField(privateTransactionLegacyResult); - verify(privacyController) - .retrieveTransaction(ENCLAVE_KEY.toBase64String(), Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).retrieveTransaction(ENCLAVE_KEY.toBase64String(), ENCLAVE_PUBLIC_KEY); } @Test @@ -166,7 +167,7 @@ public void returnsPrivateTransactionGroup() { new PrivateTransactionGroupResult(privateTransaction); final PrivGetPrivateTransaction privGetPrivateTransaction = - new PrivGetPrivateTransaction(blockchain, privacyController); + new PrivGetPrivateTransaction(blockchain, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {TRANSACTION_HASH}; final JsonRpcRequestContext request = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java index 8e550e23c3b..4060aab333e 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java @@ -22,13 +22,12 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.privacy.PrivacyController; -import java.util.Optional; - import io.vertx.core.json.JsonObject; import io.vertx.ext.auth.User; import io.vertx.ext.auth.jwt.impl.JWTUser; @@ -48,19 +47,19 @@ public class PrivGetTransactionCountTest { Address.fromHexString("0x627306090abab3a6e1400e9345bc60c78a8bef57"); private final long NONCE = 5; private User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; @Before public void before() { when(privacyParameters.isEnabled()).thenReturn(true); - when(privacyController.determineNonce( - senderAddress, privacyGroupId, Optional.of(ENCLAVE_PUBLIC_KEY))) + when(privacyController.determineNonce(senderAddress, privacyGroupId, ENCLAVE_PUBLIC_KEY)) .thenReturn(NONCE); } @Test public void verifyTransactionCount() { final PrivGetTransactionCount privGetTransactionCount = - new PrivGetTransactionCount(privacyController); + new PrivGetTransactionCount(privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {senderAddress, privacyGroupId}; final JsonRpcRequestContext request = @@ -71,7 +70,6 @@ public void verifyTransactionCount() { (JsonRpcSuccessResponse) privGetTransactionCount.response(request); assertThat(response.getResult()).isEqualTo(String.format("0x%X", NONCE)); - verify(privacyController) - .determineNonce(senderAddress, privacyGroupId, Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).determineNonce(senderAddress, privacyGroupId, ENCLAVE_PUBLIC_KEY); } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java index 13d3fc74ce4..a8552f67557 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java @@ -30,6 +30,7 @@ import org.hyperledger.besu.enclave.types.ReceiveResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionReceiptResult; @@ -142,6 +143,7 @@ public class PrivGetTransactionReceiptTest { Quantity.create(Bytes.of(1).toUnsignedBigInteger())); private User user = new JWTUser(new JsonObject().put("privacyPublicKey", ENCLAVE_PUBLIC_KEY), ""); + private final EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY; private final BlockchainQueries blockchainQueries = mock(BlockchainQueries.class); private final Blockchain blockchain = mock(Blockchain.class); @@ -178,7 +180,8 @@ public void setUp() { public void returnReceiptIfTransactionExists() { final PrivGetTransactionReceipt privGetTransactionReceipt = - new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); + new PrivGetTransactionReceipt( + blockchainQueries, privacyParameters, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequestContext request = new JsonRpcRequestContext( @@ -190,8 +193,7 @@ public void returnReceiptIfTransactionExists() { (PrivateTransactionReceiptResult) response.getResult(); assertThat(result).isEqualToComparingFieldByField(expectedResult); - verify(privacyController) - .retrieveTransaction(ENCLAVE_KEY.toBase64String(), Optional.of(ENCLAVE_PUBLIC_KEY)); + verify(privacyController).retrieveTransaction(ENCLAVE_KEY.toBase64String(), ENCLAVE_PUBLIC_KEY); } @Test @@ -200,7 +202,8 @@ public void enclavePayloadNotFoundResultsInSuccessButNullResponse() { .thenThrow(new EnclaveClientException(404, "EnclavePayloadNotFound")); final PrivGetTransactionReceipt privGetTransactionReceipt = - new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); + new PrivGetTransactionReceipt( + blockchainQueries, privacyParameters, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params)); @@ -218,7 +221,8 @@ public void markerTransactionNotAvailableResultsInNullResponse() { when(blockchain.getTransactionLocation(nullable(Hash.class))).thenReturn(Optional.empty()); final PrivGetTransactionReceipt privGetTransactionReceipt = - new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); + new PrivGetTransactionReceipt( + blockchainQueries, privacyParameters, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params)); @@ -236,7 +240,8 @@ public void enclaveConnectionIssueThrowsRuntimeException() { when(privacyController.retrieveTransaction(anyString(), any())) .thenThrow(EnclaveServerException.class); final PrivGetTransactionReceipt privGetTransactionReceipt = - new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); + new PrivGetTransactionReceipt( + blockchainQueries, privacyParameters, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params)); @@ -251,7 +256,8 @@ public void transactionReceiptContainsRevertReasonWhenInvalidTransactionOccurs() .thenReturn(Optional.of(Bytes.fromHexString("0x01"))); final PrivGetTransactionReceipt privGetTransactionReceipt = - new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); + new PrivGetTransactionReceipt( + blockchainQueries, privacyParameters, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequest request = new JsonRpcRequest("1", "priv_getTransactionReceipt", params); @@ -271,7 +277,8 @@ public void enclaveKeysCannotDecryptPayloadThrowsRuntimeException() { .thenThrow(new EnclaveClientException(400, keysCannotDecryptPayloadMsg)); final PrivGetTransactionReceipt privGetTransactionReceipt = - new PrivGetTransactionReceipt(blockchainQueries, privacyParameters, privacyController); + new PrivGetTransactionReceipt( + blockchainQueries, privacyParameters, privacyController, enclavePublicKeyProvider); final Object[] params = new Object[] {transaction.getHash()}; final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params)); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethodsTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethodsTest.java index 61534db1509..d1d47d15757 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethodsTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethodsTest.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.PRIVACY_NOT_ENABLED; import static org.mockito.Mockito.when; @@ -23,6 +24,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.MultiTenancyRpcMethodDecorator; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; @@ -34,7 +36,11 @@ import org.hyperledger.besu.ethereum.privacy.PrivacyController; import java.util.Map; +import java.util.Optional; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.jwt.impl.JWTUser; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,18 +49,23 @@ @RunWith(MockitoJUnitRunner.class) public class PrivacyApiGroupJsonRpcMethodsTest { + private static final String DEFAULT_ENCLAVE_PUBLIC_KEY = + "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + @Mock private JsonRpcMethod rpcMethod; @Mock private BlockchainQueries blockchainQueries; @Mock private ProtocolSchedule protocolSchedule; @Mock private TransactionPool transactionPool; @Mock private PrivacyParameters privacyParameters; - private PrivacyApiGroupJsonRpcMethods privacyApiGroupJsonRpcMethods; + private TestPrivacyApiGroupJsonRpcMethods privacyApiGroupJsonRpcMethods; @Before public void setup() { when(rpcMethod.getName()).thenReturn("priv_method"); - privacyApiGroupJsonRpcMethods = createPrivacyApiGroupJsonRpcMethods(); + privacyApiGroupJsonRpcMethods = + new TestPrivacyApiGroupJsonRpcMethods( + blockchainQueries, protocolSchedule, transactionPool, privacyParameters, rpcMethod); } @Test @@ -75,6 +86,52 @@ public void rpcsCreatedWithoutMultiTenancyUseOriginalRpcMethod() { assertThat(privMethod).isSameAs(rpcMethod); } + @Test + public void rpcsCreatedWithoutMultiTenancyUseFixedEnclavePublicKey() { + when(privacyParameters.isEnabled()).thenReturn(true); + when(privacyParameters.getEnclavePublicKey()).thenReturn(DEFAULT_ENCLAVE_PUBLIC_KEY); + + final User user = createUser(DEFAULT_ENCLAVE_PUBLIC_KEY); + privacyApiGroupJsonRpcMethods.create(); + final EnclavePublicKeyProvider enclavePublicKeyProvider = + privacyApiGroupJsonRpcMethods.enclavePublicKeyProvider; + + assertThat(enclavePublicKeyProvider.getEnclaveKey(Optional.of(user))) + .isEqualTo(DEFAULT_ENCLAVE_PUBLIC_KEY); + assertThat(enclavePublicKeyProvider.getEnclaveKey(Optional.empty())) + .isEqualTo(DEFAULT_ENCLAVE_PUBLIC_KEY); + } + + @Test + public void rpcsCreatedWithMultiTenancyUseEnclavePublicKeyFromRequest() { + when(privacyParameters.isEnabled()).thenReturn(true); + when(privacyParameters.isMultiTenancyEnabled()).thenReturn(true); + + final User user1 = createUser("key1"); + final User user2 = createUser("key2"); + + privacyApiGroupJsonRpcMethods.create(); + final EnclavePublicKeyProvider enclavePublicKeyProvider = + privacyApiGroupJsonRpcMethods.enclavePublicKeyProvider; + + assertThat(enclavePublicKeyProvider.getEnclaveKey(Optional.of(user1))).isEqualTo("key1"); + assertThat(enclavePublicKeyProvider.getEnclaveKey(Optional.of(user2))).isEqualTo("key2"); + } + + @Test + public void rpcsCreatedWithMultiTenancyAndWithoutUserFail() { + when(privacyParameters.isEnabled()).thenReturn(true); + when(privacyParameters.isMultiTenancyEnabled()).thenReturn(true); + + privacyApiGroupJsonRpcMethods.create(); + final EnclavePublicKeyProvider enclavePublicKeyProvider = + privacyApiGroupJsonRpcMethods.enclavePublicKeyProvider; + + assertThatThrownBy(() -> enclavePublicKeyProvider.getEnclaveKey(Optional.empty())) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Request does not contain an authorization token"); + } + @Test public void rpcMethodsCreatedWhenPrivacyIsNotEnabledAreDisabled() { final Map rpcMethods = privacyApiGroupJsonRpcMethods.create(); @@ -90,19 +147,36 @@ public void rpcMethodsCreatedWhenPrivacyIsNotEnabledAreDisabled() { assertThat(errorResponse.getError()).isEqualTo(PRIVACY_NOT_ENABLED); } - private PrivacyApiGroupJsonRpcMethods createPrivacyApiGroupJsonRpcMethods() { - return new PrivacyApiGroupJsonRpcMethods( - blockchainQueries, protocolSchedule, transactionPool, privacyParameters) { - - @Override - protected RpcApi getApiGroup() { - return RpcApis.PRIV; - } + private User createUser(final String enclavePublicKey) { + return new JWTUser(new JsonObject().put("privacyPublicKey", enclavePublicKey), ""); + } - @Override - protected Map create(final PrivacyController privacyController) { - return mapOf(rpcMethod); - } - }; + private static class TestPrivacyApiGroupJsonRpcMethods extends PrivacyApiGroupJsonRpcMethods { + + private final JsonRpcMethod rpcMethod; + private EnclavePublicKeyProvider enclavePublicKeyProvider; + + public TestPrivacyApiGroupJsonRpcMethods( + final BlockchainQueries blockchainQueries, + final ProtocolSchedule protocolSchedule, + final TransactionPool transactionPool, + final PrivacyParameters privacyParameters, + final JsonRpcMethod rpcMethod) { + super(blockchainQueries, protocolSchedule, transactionPool, privacyParameters); + this.rpcMethod = rpcMethod; + } + + @Override + protected Map create( + final PrivacyController privacyController, + final EnclavePublicKeyProvider enclavePublicKeyProvider) { + this.enclavePublicKeyProvider = enclavePublicKeyProvider; + return mapOf(rpcMethod); + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.PRIV; + } } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java index a6bdb6e7875..512d7f6a48d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java @@ -46,7 +46,6 @@ public class PrivacyController { private static final Logger LOG = LogManager.getLogger(); private final Enclave enclave; - private final String defaultEnclavePublicKey; private final PrivateStateStorage privateStateStorage; private final WorldStateArchive privateWorldStateArchive; private final PrivateTransactionValidator privateTransactionValidator; @@ -58,7 +57,6 @@ public PrivacyController( final PrivateMarkerTransactionFactory privateMarkerTransactionFactory) { this( privacyParameters.getEnclave(), - privacyParameters.getEnclavePublicKey(), privacyParameters.getPrivateStateStorage(), privacyParameters.getPrivateWorldStateArchive(), new PrivateTransactionValidator(chainId), @@ -67,13 +65,11 @@ public PrivacyController( public PrivacyController( final Enclave enclave, - final String defaultEnclavePublicKey, final PrivateStateStorage privateStateStorage, final WorldStateArchive privateWorldStateArchive, final PrivateTransactionValidator privateTransactionValidator, final PrivateMarkerTransactionFactory privateMarkerTransactionFactory) { this.enclave = enclave; - this.defaultEnclavePublicKey = defaultEnclavePublicKey; this.privateStateStorage = privateStateStorage; this.privateWorldStateArchive = privateWorldStateArchive; this.privateTransactionValidator = privateTransactionValidator; @@ -81,10 +77,10 @@ public PrivacyController( } public SendTransactionResponse sendTransaction( - final PrivateTransaction privateTransaction, final Optional enclavePublicKey) { + final PrivateTransaction privateTransaction, final String enclavePublicKey) { try { LOG.trace("Storing private transaction in enclave"); - final SendResponse sendResponse = sendRequest(privateTransaction); + final SendResponse sendResponse = sendRequest(privateTransaction, enclavePublicKey); final String enclaveKey = sendResponse.getKey(); if (privateTransaction.getPrivacyGroupId().isPresent()) { final String privacyGroupId = privateTransaction.getPrivacyGroupId().get().toBase64String(); @@ -101,25 +97,23 @@ public SendTransactionResponse sendTransaction( } public ReceiveResponse retrieveTransaction( - final String enclaveKey, final Optional enclavePublicKey) { - return enclave.receive(enclaveKey, this.defaultEnclavePublicKey); + final String enclaveKey, final String enclavePublicKey) { + return enclave.receive(enclaveKey, enclavePublicKey); } public PrivacyGroup createPrivacyGroup( final List addresses, final String name, final String description, - final Optional enclavePublicKey) { - return enclave.createPrivacyGroup(addresses, defaultEnclavePublicKey, name, description); + final String enclavePublicKey) { + return enclave.createPrivacyGroup(addresses, enclavePublicKey, name, description); } - public String deletePrivacyGroup( - final String privacyGroupId, final Optional enclavePublicKey) { - return enclave.deletePrivacyGroup(privacyGroupId, defaultEnclavePublicKey); + public String deletePrivacyGroup(final String privacyGroupId, final String enclavePublicKey) { + return enclave.deletePrivacyGroup(privacyGroupId, enclavePublicKey); } - public PrivacyGroup[] findPrivacyGroup( - final List addresses, final Optional enclavePublicKey) { + public PrivacyGroup[] findPrivacyGroup(final List addresses, String enclavePublicKey) { return enclave.findPrivacyGroup(addresses); } @@ -131,7 +125,7 @@ public Transaction createPrivacyMarkerTransaction( public ValidationResult validatePrivateTransaction( final PrivateTransaction privateTransaction, final String privacyGroupId, - final Optional enclavePublicKey) { + final String enclavePublicKey) { return privateTransactionValidator.validate( privateTransaction, determineNonce(privateTransaction.getSender(), privacyGroupId, enclavePublicKey)); @@ -141,7 +135,7 @@ public long determineNonce( final String privateFrom, final String[] privateFor, final Address address, - final Optional enclavePublicKey) { + final String enclavePublicKey) { final List groupMembers = Lists.asList(privateFrom, privateFor); final List matchingGroups = @@ -167,7 +161,7 @@ public long determineNonce( } public long determineNonce( - final Address sender, final String privacyGroupId, final Optional enclavePublicKey) { + final Address sender, final String privacyGroupId, final String enclavePublicKey) { return privateStateStorage .getLatestStateRoot(Bytes.fromBase64String(privacyGroupId)) .map( @@ -191,16 +185,15 @@ public long determineNonce( Account.DEFAULT_NONCE); } - private SendResponse sendRequest(final PrivateTransaction privateTransaction) { + private SendResponse sendRequest( + final PrivateTransaction privateTransaction, final String enclavePublicKey) { final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); privateTransaction.writeTo(rlpOutput); final String payload = rlpOutput.encoded().toBase64String(); if (privateTransaction.getPrivacyGroupId().isPresent()) { return enclave.send( - payload, - defaultEnclavePublicKey, - privateTransaction.getPrivacyGroupId().get().toBase64String()); + payload, enclavePublicKey, privateTransaction.getPrivacyGroupId().get().toBase64String()); } else { final List privateFor = privateTransaction.getPrivateFor().get().stream() diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java index cfa8cb93d17..712c678d00e 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivacyControllerTest.java @@ -24,6 +24,7 @@ import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -71,10 +72,12 @@ public class PrivacyControllerTest { new BigInteger( "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16))); private static final byte[] PAYLOAD = new byte[0]; - private static final String PRIVACY_GROUP_ID = "pg_id"; private static final List PRIVACY_GROUP_ADDRESSES = newArrayList("8f2a", "fb23"); private static final String PRIVACY_GROUP_NAME = "pg_name"; private static final String PRIVACY_GROUP_DESCRIPTION = "pg_desc"; + private static final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; + private static final String ENCLAVE_KEY2 = "Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs="; + private static final String PRIVACY_GROUP_ID = "DyAOiF/ynpc+JXa2YAGB0bCitSlOMNm+ShmB/7M6C4w="; private PrivacyController privacyController; private PrivacyController brokenPrivacyController; @@ -140,7 +143,6 @@ public void setUp() throws Exception { privacyController = new PrivacyController( enclave, - enclavePublicKey, privateStateStorage, worldStateArchive, privateTransactionValidator, @@ -149,7 +151,6 @@ public void setUp() throws Exception { brokenPrivacyController = new PrivacyController( brokenMockEnclave(), - enclavePublicKey, privateStateStorage, worldStateArchive, privateTransactionValidator, @@ -163,11 +164,11 @@ public void sendsValidLegacyTransaction() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(1); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, Optional.empty()); + privacyController.sendTransaction(transaction, ENCLAVE_PUBLIC_KEY); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, sendTransactionResponse.getPrivacyGroupId(), Optional.empty()); + transaction, sendTransactionResponse.getPrivacyGroupId(), ENCLAVE_PUBLIC_KEY); final Transaction markerTransaction = privacyController.createPrivacyMarkerTransaction( @@ -179,6 +180,8 @@ public void sendsValidLegacyTransaction() { assertThat(markerTransaction.getNonce()).isEqualTo(PUBLIC_TRANSACTION.getNonce()); assertThat(markerTransaction.getSender()).isEqualTo(PUBLIC_TRANSACTION.getSender()); assertThat(markerTransaction.getValue()).isEqualTo(PUBLIC_TRANSACTION.getValue()); + verify(enclave) + .send(anyString(), eq(ENCLAVE_PUBLIC_KEY), eq(List.of(ENCLAVE_PUBLIC_KEY, ENCLAVE_KEY2))); } @Test @@ -187,11 +190,11 @@ public void sendValidBesuTransaction() { final PrivateTransaction transaction = buildBesuPrivateTransaction(1); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, Optional.empty()); + privacyController.sendTransaction(transaction, ENCLAVE_PUBLIC_KEY); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, transaction.getPrivacyGroupId().get().toString(), Optional.empty()); + transaction, transaction.getPrivacyGroupId().get().toString(), ENCLAVE_PUBLIC_KEY); final Transaction markerTransaction = privacyController.createPrivacyMarkerTransaction( @@ -203,6 +206,7 @@ public void sendValidBesuTransaction() { assertThat(markerTransaction.getNonce()).isEqualTo(PUBLIC_TRANSACTION.getNonce()); assertThat(markerTransaction.getSender()).isEqualTo(PUBLIC_TRANSACTION.getSender()); assertThat(markerTransaction.getValue()).isEqualTo(PUBLIC_TRANSACTION.getValue()); + verify(enclave).send(anyString(), eq(ENCLAVE_PUBLIC_KEY), eq(PRIVACY_GROUP_ID)); } @Test @@ -211,7 +215,7 @@ public void sendTransactionWhenEnclaveFailsThrowsEnclaveError() { .isThrownBy( () -> brokenPrivacyController.sendTransaction( - buildLegacyPrivateTransaction(), Optional.empty())); + buildLegacyPrivateTransaction(), ENCLAVE_PUBLIC_KEY)); } @Test @@ -221,10 +225,10 @@ public void validateTransactionWithTooLowNonceReturnsError() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(0); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, Optional.empty()); + privacyController.sendTransaction(transaction, ENCLAVE_PUBLIC_KEY); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, sendTransactionResponse.getPrivacyGroupId(), Optional.empty()); + transaction, sendTransactionResponse.getPrivacyGroupId(), ENCLAVE_PUBLIC_KEY); assertThat(validationResult).isEqualTo(ValidationResult.invalid(PRIVATE_NONCE_TOO_LOW)); } @@ -236,10 +240,10 @@ public void validateTransactionWithIncorrectNonceReturnsError() { final PrivateTransaction transaction = buildLegacyPrivateTransaction(2); final SendTransactionResponse sendTransactionResponse = - privacyController.sendTransaction(transaction, Optional.empty()); + privacyController.sendTransaction(transaction, ENCLAVE_PUBLIC_KEY); final ValidationResult validationResult = privacyController.validatePrivateTransaction( - transaction, sendTransactionResponse.getPrivacyGroupId(), Optional.empty()); + transaction, sendTransactionResponse.getPrivacyGroupId(), ENCLAVE_PUBLIC_KEY); assertThat(validationResult).isEqualTo(ValidationResult.invalid(INCORRECT_PRIVATE_NONCE)); } @@ -249,7 +253,7 @@ public void retrievesTransaction() { .thenReturn(new ReceiveResponse(PAYLOAD, PRIVACY_GROUP_ID)); final ReceiveResponse receiveResponse = - privacyController.retrieveTransaction(TRANSACTION_KEY, Optional.empty()); + privacyController.retrieveTransaction(TRANSACTION_KEY, ENCLAVE_PUBLIC_KEY); assertThat(receiveResponse.getPayload()).isEqualTo(PAYLOAD); assertThat(receiveResponse.getPrivacyGroupId()).isEqualTo(PRIVACY_GROUP_ID); @@ -273,7 +277,7 @@ public void createsPrivacyGroup() { PRIVACY_GROUP_ADDRESSES, PRIVACY_GROUP_NAME, PRIVACY_GROUP_DESCRIPTION, - Optional.empty()); + ENCLAVE_PUBLIC_KEY); assertThat(privacyGroup).isEqualToComparingFieldByField(enclavePrivacyGroupResponse); verify(enclave) @@ -289,7 +293,7 @@ public void deletesPrivacyGroup() { when(enclave.deletePrivacyGroup(anyString(), anyString())).thenReturn(PRIVACY_GROUP_ID); final String deletedPrivacyGroupId = - privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, Optional.empty()); + privacyController.deletePrivacyGroup(PRIVACY_GROUP_ID, ENCLAVE_PUBLIC_KEY); assertThat(deletedPrivacyGroupId).isEqualTo(PRIVACY_GROUP_ID); verify(enclave).deletePrivacyGroup(PRIVACY_GROUP_ID, enclavePublicKey); @@ -307,7 +311,7 @@ public void findsPrivacyGroup() { when(enclave.findPrivacyGroup(any())).thenReturn(new PrivacyGroup[] {privacyGroup}); final PrivacyGroup[] privacyGroups = - privacyController.findPrivacyGroup(PRIVACY_GROUP_ADDRESSES, Optional.empty()); + privacyController.findPrivacyGroup(PRIVACY_GROUP_ADDRESSES, ENCLAVE_PUBLIC_KEY); assertThat(privacyGroups).hasSize(1); assertThat(privacyGroups[0]).isEqualToComparingFieldByField(privacyGroup); verify(enclave).findPrivacyGroup(PRIVACY_GROUP_ADDRESSES); @@ -327,7 +331,7 @@ public void determinesNonceForEeaRequest() { final long nonce = privacyController.determineNonce( - "privateFrom", new String[] {"first", "second"}, address, Optional.empty()); + "privateFrom", new String[] {"first", "second"}, address, ENCLAVE_PUBLIC_KEY); assertThat(nonce).isEqualTo(reportedNonce); verify(enclave) @@ -345,7 +349,7 @@ public void determineNonceForEeaRequestWithNoMatchingGroupReturnsZero() { final long nonce = privacyController.determineNonce( - "privateFrom", new String[] {"first", "second"}, address, Optional.empty()); + "privateFrom", new String[] {"first", "second"}, address, ENCLAVE_PUBLIC_KEY); assertThat(nonce).isEqualTo(reportedNonce); verify(enclave) @@ -368,7 +372,7 @@ public void determineNonceForEeaRequestWithMoreThanOneMatchingGroupThrowsExcepti .isThrownBy( () -> privacyController.determineNonce( - "privateFrom", new String[] {"first", "second"}, address, Optional.empty())); + "privateFrom", new String[] {"first", "second"}, address, ENCLAVE_PUBLIC_KEY)); } @Test @@ -377,7 +381,7 @@ public void determineNonceForPrivacyGroupRequestWhenAccountExists() { when(account.getNonce()).thenReturn(4L); - final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); + final long nonce = privacyController.determineNonce(address, "Group1", ENCLAVE_PUBLIC_KEY); assertThat(nonce).isEqualTo(4L); verify(privateStateStorage).getLatestStateRoot(Base64.decode("Group1")); @@ -392,7 +396,7 @@ public void determineNonceForPrivacyGroupRequestWhenPrivateStateDoesNotExist() { when(privateStateStorage.getLatestStateRoot(Base64.decode("Group1"))) .thenReturn(Optional.empty()); - final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); + final long nonce = privacyController.determineNonce(address, "Group1", ENCLAVE_PUBLIC_KEY); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(worldStateArchive, mutableWorldState, account); @@ -406,7 +410,7 @@ public void determineNonceForPrivacyGroupRequestWhenWorldStateDoesNotExist() { .thenReturn(Optional.of(hash)); when(worldStateArchive.getMutable(hash)).thenReturn(Optional.empty()); - final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); + final long nonce = privacyController.determineNonce(address, "Group1", ENCLAVE_PUBLIC_KEY); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(mutableWorldState, account); @@ -421,7 +425,7 @@ public void determineNonceForPrivacyGroupRequestWhenAccountDoesNotExist() { when(worldStateArchive.getMutable(hash)).thenReturn(Optional.of(mutableWorldState)); when(mutableWorldState.get(address)).thenReturn(null); - final long nonce = privacyController.determineNonce(address, "Group1", Optional.empty()); + final long nonce = privacyController.determineNonce(address, "Group1", ENCLAVE_PUBLIC_KEY); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(account); @@ -433,19 +437,16 @@ private static PrivateTransaction buildLegacyPrivateTransaction() { private static PrivateTransaction buildLegacyPrivateTransaction(final long nonce) { return buildPrivateTransaction(nonce) - .privateFrom(Base64.decode("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")) - .privateFor( - newArrayList( - Base64.decode("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="), - Base64.decode("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs="))) + .privateFrom(Base64.decode(ENCLAVE_PUBLIC_KEY)) + .privateFor(newArrayList(Base64.decode(ENCLAVE_PUBLIC_KEY), Base64.decode(ENCLAVE_KEY2))) .signAndBuild(KEY_PAIR); } private static PrivateTransaction buildBesuPrivateTransaction(final long nonce) { return buildPrivateTransaction(nonce) - .privateFrom(Base64.decode("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")) - .privacyGroupId(Base64.decode("DyAOiF/ynpc+JXa2YAGB0bCitSlOMNm+ShmB/7M6C4w=")) + .privateFrom(Base64.decode(ENCLAVE_PUBLIC_KEY)) + .privacyGroupId(Base64.decode(PRIVACY_GROUP_ID)) .signAndBuild(KEY_PAIR); } From 949c0038ad117b3649228248cf5d23452b7d4992 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 16:47:27 +1000 Subject: [PATCH 11/13] final Signed-off-by: Jason Frame --- .../hyperledger/besu/ethereum/privacy/PrivacyController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java index 512d7f6a48d..ca5e914557a 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java @@ -113,7 +113,7 @@ public String deletePrivacyGroup(final String privacyGroupId, final String encla return enclave.deletePrivacyGroup(privacyGroupId, enclavePublicKey); } - public PrivacyGroup[] findPrivacyGroup(final List addresses, String enclavePublicKey) { + public PrivacyGroup[] findPrivacyGroup(final List addresses, final String enclavePublicKey) { return enclave.findPrivacyGroup(addresses); } From 9b8431485554469a0bcfd6239356b61cb8f41203 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Fri, 20 Dec 2019 16:49:35 +1000 Subject: [PATCH 12/13] spotless Signed-off-by: Jason Frame --- .../hyperledger/besu/ethereum/privacy/PrivacyController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java index ca5e914557a..4efdf5334c2 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivacyController.java @@ -113,7 +113,8 @@ public String deletePrivacyGroup(final String privacyGroupId, final String encla return enclave.deletePrivacyGroup(privacyGroupId, enclavePublicKey); } - public PrivacyGroup[] findPrivacyGroup(final List addresses, final String enclavePublicKey) { + public PrivacyGroup[] findPrivacyGroup( + final List addresses, final String enclavePublicKey) { return enclave.findPrivacyGroup(addresses); } From 09b6e1e15b45b5d9d4118183e0eeb0406c7e0502 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Mon, 23 Dec 2019 10:53:46 +1000 Subject: [PATCH 13/13] spotless Signed-off-by: Jason Frame --- .../privacy/methods/priv/PrivGetPrivateTransaction.java | 3 ++- .../privacy/methods/priv/PrivGetTransactionReceipt.java | 4 +++- .../privacy/methods/priv/PrivGetPrivateTransactionTest.java | 3 +-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java index ab4e179ca72..f1c9e1f5df8 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java @@ -73,7 +73,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { LOG.trace("Fetching transaction information"); final ReceiveResponse receiveResponse = privacyController.retrieveTransaction( - resultTransaction.getTransaction().getPayload().toBase64String(), enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); + resultTransaction.getTransaction().getPayload().toBase64String(), + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput input = diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java index 6bb1369ad08..d3dd65a100e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java @@ -96,7 +96,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final String privacyGroupId; try { final ReceiveResponse receiveResponse = - privacyController.retrieveTransaction(transaction.getPayload().toBase64String(), enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); + privacyController.retrieveTransaction( + transaction.getPayload().toBase64String(), + enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())); LOG.trace("Received transaction information"); final BytesValueRLPInput input = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java index e5296d6c62c..9f610f724dd 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java @@ -121,8 +121,7 @@ public void returnsPrivateTransactionLegacy() { when(blockchain.transactionByHash(any(Hash.class))) .thenReturn(Optional.of(returnedTransaction)); when(returnedTransaction.getTransaction()).thenReturn(justTransaction); - when(justTransaction.getPayload()) - .thenReturn(new UnformattedDataImpl(ENCLAVE_KEY)); + when(justTransaction.getPayload()).thenReturn(new UnformattedDataImpl(ENCLAVE_KEY)); final PrivateTransaction privateTransaction = privateTransactionBuilder