Java Code Examples for org.apache.tuweni.bytes.Bytes#fromBase64String()

The following examples show how to use org.apache.tuweni.bytes.Bytes#fromBase64String() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultPrivacyControllerTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void getContractCodeCallsPrivateWorldStateReader() {
  final Hash blockHash = Hash.ZERO;
  final Address contractAddress = Address.ZERO;
  final Bytes contractCode = Bytes.fromBase64String("ZXhhbXBsZQ==");

  when(privateWorldStateReader.getContractCode(
          eq(PRIVACY_GROUP_ID), eq(blockHash), eq(contractAddress)))
      .thenReturn(Optional.of(contractCode));

  assertThat(
          privacyController.getContractCode(
              PRIVACY_GROUP_ID, contractAddress, blockHash, ENCLAVE_PUBLIC_KEY))
      .isPresent()
      .hasValue(contractCode);
}
 
Example 2
Source File: PrivacyReorgTest.java    From besu with Apache License 2.0 6 votes vote down vote up
private Bytes getEnclaveKey(final URI enclaveURI) {
  final Enclave enclave = new EnclaveFactory(Vertx.vertx()).createVertxEnclave(enclaveURI);
  final SendResponse sendResponse =
      sendRequest(enclave, PRIVATE_TRANSACTION, ENCLAVE_PUBLIC_KEY.toBase64String());
  final Bytes payload = Bytes.fromBase64String(sendResponse.getKey());

  // If the key has 0 bytes generate a new key.
  // This is to keep the gasUsed constant allowing
  // hard-coded receipt roots in the block headers
  for (int i = 0; i < payload.size(); i++) {
    if (payload.get(i) == 0) {
      return getEnclaveKey(enclaveURI);
    }
  }

  return payload;
}
 
Example 3
Source File: Invite.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
private static Ed25519PublicKeyIdentity toPublicKey(String keyPart) {
  // Remove the @ from the front of the key
  String keyPartSuffix = keyPart.substring(1);
  Bytes publicKeyBytes = Bytes.fromBase64String(keyPartSuffix);
  Signature.PublicKey publicKey = Signature.PublicKey.fromBytes(publicKeyBytes);
  return new Ed25519PublicKeyIdentity(publicKey);
}
 
Example 4
Source File: PrivGetPrivateTransactionIntegrationTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void returnsStoredPrivateTransaction() {
  final PrivGetPrivateTransaction privGetPrivateTransaction =
      new PrivGetPrivateTransaction(privacyController, enclavePublicKeyProvider);

  final Hash blockHash = Hash.ZERO;
  final Transaction pmt = spy(privacyMarkerTransaction());
  when(blockchain.getTransactionByHash(eq(pmt.getHash()))).thenReturn(Optional.of(pmt));
  when(blockchain.getTransactionLocation(eq(pmt.getHash())))
      .thenReturn(Optional.of(new TransactionLocation(blockHash, 0)));

  final BlockHeader blockHeader = mock(BlockHeader.class);
  when(blockHeader.getHash()).thenReturn(blockHash);
  when(blockchain.getBlockHeader(eq(blockHash))).thenReturn(Optional.of(blockHeader));

  final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput();
  privateTransaction.writeTo(bvrlp);

  final String payload = Base64.getEncoder().encodeToString(bvrlp.encoded().toArrayUnsafe());
  final ArrayList<String> to = Lists.newArrayList("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=");
  final SendResponse sendResponse = enclave.send(payload, ENCLAVE_PUBLIC_KEY, to);

  final Bytes hexKey = Bytes.fromBase64String(sendResponse.getKey());
  when(pmt.getPayload()).thenReturn(hexKey);

  final Object[] params = new Object[] {pmt.getHash()};

  final JsonRpcRequestContext request =
      new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getPrivateTransaction", params));

  final JsonRpcSuccessResponse response =
      (JsonRpcSuccessResponse) privGetPrivateTransaction.response(request);
  final PrivateTransactionLegacyResult result =
      (PrivateTransactionLegacyResult) response.getResult();

  assertThat(new PrivateTransactionLegacyResult(this.privateTransaction))
      .isEqualToComparingFieldByField(result);
}
 
Example 5
Source File: PrivateTransactionLocator.java    From besu with Apache License 2.0 5 votes vote down vote up
private TransactionFromEnclave readPrivateTransactionFromPayload(
    final ReceiveResponse receiveResponse) {
  final PrivateTransaction privateTransaction;
  final BytesValueRLPInput input =
      new BytesValueRLPInput(
          Bytes.fromBase64String(new String(receiveResponse.getPayload(), UTF_8)), false);

  /*
   When using onchain privacy groups, the payload is a list with the first element being the
   private transaction RLP and the second element being the version. This is why we have the
   nextIsList() check.
  */
  try {
    input.enterList();
    if (input.nextIsList()) {
      // private transaction and version (we only read the first element in the list)
      privateTransaction = PrivateTransaction.readFrom(input);
      input.leaveListLenient();
    } else {
      // private transaction only (read the whole RLP)
      input.reset();
      privateTransaction = PrivateTransaction.readFrom(input);
    }
  } catch (final RLPException e) {
    LOG.debug("Error de-serializing private transaction from enclave", e);
    throw e;
  }

  return new TransactionFromEnclave(privateTransaction, receiveResponse.getPrivacyGroupId());
}
 
Example 6
Source File: DefaultPrivacyController.java    From besu with Apache License 2.0 5 votes vote down vote up
private List<PrivateTransactionWithMetadata> retrievePrivateTransactions(
    final Bytes32 privacyGroupId,
    final List<PrivateTransactionMetadata> privateTransactionMetadataList,
    final String enclavePublicKey) {
  final ArrayList<PrivateTransactionWithMetadata> privateTransactions = new ArrayList<>();
  privateStateStorage
      .getAddDataKey(privacyGroupId)
      .ifPresent(key -> privateTransactions.addAll(retrieveAddBlob(key.toBase64String())));
  for (int i = privateTransactions.size(); i < privateTransactionMetadataList.size(); i++) {
    final PrivateTransactionMetadata privateTransactionMetadata =
        privateTransactionMetadataList.get(i);
    final Transaction privateMarkerTransaction =
        blockchain
            .getTransactionByHash(privateTransactionMetadata.getPrivacyMarkerTransactionHash())
            .orElseThrow();
    final ReceiveResponse receiveResponse =
        retrieveTransaction(
            privateMarkerTransaction.getPayload().slice(0, 32).toBase64String(),
            enclavePublicKey);
    final BytesValueRLPInput input =
        new BytesValueRLPInput(
            Bytes.fromBase64String(new String(receiveResponse.getPayload(), UTF_8)), false);
    input.enterList();
    privateTransactions.add(
        new PrivateTransactionWithMetadata(
            PrivateTransaction.readFrom(input), privateTransactionMetadata));
    input.leaveListLenient();
  }

  return privateTransactions;
}
 
Example 7
Source File: MultiTenancyPrivacyControllerTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void getContractCodeWorksForValidEnclaveKey() {
  final Bytes contractCode = Bytes.fromBase64String("ZXhhbXBsZQ==");

  when(enclave.retrievePrivacyGroup(PRIVACY_GROUP_ID))
      .thenReturn(PANTHEON_GROUP_WITH_ENCLAVE_KEY_1);
  when(privacyController.getContractCode(any(), any(), any(), any()))
      .thenReturn(Optional.of(contractCode));

  final Optional<Bytes> result =
      multiTenancyPrivacyController.getContractCode(
          PRIVACY_GROUP_ID, Address.ZERO, Hash.ZERO, ENCLAVE_PUBLIC_KEY1);

  assertThat(result).isPresent().hasValue(contractCode);
}
 
Example 8
Source File: Invite.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
private static Signature.Seed toSecretKey(String secretKeyPart) {
  Bytes secret = Bytes.fromBase64String(secretKeyPart);
  return Signature.Seed.fromBytes(secret);
}