use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.
the class PrivCreatePrivacyGroupTest method verifyCreatePrivacyGroupWithoutOptionalParams.
@Test
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, ENCLAVE_PUBLIC_KEY)).thenReturn(privacyGroup);
when(privacyParameters.getPrivacyUserId()).thenReturn(FROM);
final PrivCreatePrivacyGroup privCreatePrivacyGroup = new PrivCreatePrivacyGroup(privacyController, privacyIdProvider);
final Object[] params = new Object[] { new Object() {
public List<String> getAddresses() {
return ADDRESSES;
}
} };
final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_createPrivacyGroup", params));
final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privCreatePrivacyGroup.response(request);
final String result = (String) response.getResult();
assertThat(result).isEqualTo(expected);
}
use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.
the class PrivCreatePrivacyGroupTest method returnsCorrectExceptionInvalidParam.
@Test
public void returnsCorrectExceptionInvalidParam() {
final String expected = "a wonderful group";
final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES);
when(enclave.createPrivacyGroup(any(), any(), any(), any())).thenReturn(privacyGroup);
when(privacyParameters.getPrivacyUserId()).thenReturn(FROM);
final PrivCreatePrivacyGroup privCreatePrivacyGroup = new PrivCreatePrivacyGroup(privacyController, privacyIdProvider);
final Object[] params = new Object[] { new Object() {
public String getName() {
return NAME;
}
public String getDescription() {
return DESCRIPTION;
}
} };
final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_createPrivacyGroup", params));
final Throwable response = catchThrowableOfType(() -> privCreatePrivacyGroup.response(request), InvalidJsonRpcParameters.class);
assertThat(response.getMessage()).isEqualTo("Invalid json rpc parameter at index 0");
}
use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.
the class PrivDebugGetStateRoot method resultByBlockNumber.
@Override
protected Object resultByBlockNumber(final JsonRpcRequestContext requestContext, final long blockNumber) {
final String privacyGroupId = requestContext.getRequiredParameter(0, String.class);
final String privacyUserId = privacyIdProvider.getPrivacyUserId(requestContext.getUser());
if (LOG.isTraceEnabled()) {
LOG.trace("Executing {}", getName());
}
final Optional<PrivacyGroup> privacyGroup;
try {
privacyGroup = privacyController.findPrivacyGroupByGroupId(privacyGroupId, privacyUserId);
} catch (final MultiTenancyValidationException e) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
} catch (final EnclaveClientException e) {
final Pattern pattern = Pattern.compile("^Privacy group.*not found$");
if (e.getMessage().equals(JsonRpcError.ENCLAVE_PRIVACY_GROUP_MISSING.getMessage()) || pattern.matcher(e.getMessage()).find()) {
LOG.error("Failed to retrieve privacy group");
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
} else {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.ENCLAVE_ERROR);
}
} catch (final Exception e) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}
if (privacyGroup.isEmpty()) {
LOG.error("Failed to retrieve privacy group");
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
}
return privacyController.getStateRootByBlockNumber(privacyGroupId, privacyUserId, blockNumber).<JsonRpcResponse>map(stateRootHash -> new JsonRpcSuccessResponse(requestContext.getRequest().getId(), stateRootHash.toString())).orElse(new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INTERNAL_ERROR));
}
use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.
the class PrivFindPrivacyGroup method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_FIND_PRIVACY_GROUP.getMethodName());
final String[] addresses = requestContext.getRequiredParameter(0, String[].class);
LOG.trace("Finding a privacy group with members {}", Arrays.toString(addresses));
final List<PrivacyGroup> response;
try {
response = Arrays.asList(privacyController.findPrivacyGroupByMembers(Arrays.asList(addresses), privacyIdProvider.getPrivacyUserId(requestContext.getUser())));
} catch (final MultiTenancyValidationException e) {
LOG.error("Unauthorized privacy multi-tenancy rpc request. {}", e.getMessage());
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
} catch (final Exception e) {
LOG.error("Failed to fetch privacy group", e);
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
}
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), response);
}
use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.
the class RestrictedFlexibleEeaSendRawTransaction method createPrivateMarkerTransaction.
@Override
protected Transaction createPrivateMarkerTransaction(final Address sender, final PrivateTransaction privateTransaction, final Optional<User> user) {
final Optional<Bytes> maybePrivacyGroupId = privateTransaction.getPrivacyGroupId();
if (maybePrivacyGroupId.isEmpty()) {
throw new JsonRpcErrorResponseException(JsonRpcError.FLEXIBLE_PRIVACY_GROUP_ID_NOT_AVAILABLE);
}
final Bytes privacyGroupId = maybePrivacyGroupId.get();
final String privacyUserId = privacyIdProvider.getPrivacyUserId(user);
Optional<PrivacyGroup> maybePrivacyGroup = privacyController.findPrivacyGroupByGroupId(privacyGroupId.toBase64String(), privacyUserId);
final boolean isGroupAdditionTransaction = FlexibleUtil.isGroupAdditionTransaction(privateTransaction);
if (maybePrivacyGroup.isEmpty() && !isGroupAdditionTransaction) {
throw new JsonRpcErrorResponseException(JsonRpcError.FLEXIBLE_PRIVACY_GROUP_DOES_NOT_EXIST);
}
if (isGroupAdditionTransaction) {
final List<String> participantsFromParameter = FlexibleUtil.getParticipantsFromParameter(privateTransaction.getPayload());
if (maybePrivacyGroup.isEmpty()) {
maybePrivacyGroup = Optional.of(new PrivacyGroup(privacyGroupId.toBase64String(), PrivacyGroup.Type.FLEXIBLE, null, null, participantsFromParameter));
} else {
maybePrivacyGroup.get().addMembers(participantsFromParameter);
}
}
if (!maybePrivacyGroup.get().getMembers().contains(privacyUserId)) {
throw new JsonRpcErrorResponseException(JsonRpcError.FLEXIBLE_PRIVACY_GROUP_DOES_NOT_EXIST);
}
final String pmtPayload = privacyController.createPrivateMarkerTransactionPayload(privateTransaction, privacyUserId, maybePrivacyGroup);
return createPrivateMarkerTransaction(sender, FLEXIBLE_PRIVACY, pmtPayload, privateTransaction, privacyUserId);
}
Aggregations