org.web3j.protocol.eea.crypto.PrivateTransactionEncoder Java Examples

The following examples show how to use org.web3j.protocol.eea.crypto.PrivateTransactionEncoder. 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: PrivacyRequestFactory.java    From besu with Apache License 2.0 6 votes vote down vote up
public String privxAddToPrivacyGroup(
    final Base64String privacyGroupId, final PrivacyNode adder, final List<String> addresses)
    throws IOException, TransactionException {

  final BigInteger nonce =
      besuClient
          .privGetTransactionCount(adder.getAddress().toHexString(), privacyGroupId)
          .send()
          .getTransactionCount();

  final Bytes payload =
      encodeAddToGroupFunctionCall(
          Bytes.fromBase64String(adder.getEnclaveKey()),
          addresses.stream().map(Bytes::fromBase64String).collect(Collectors.toList()));

  final RawPrivateTransaction privateTransaction =
      RawPrivateTransaction.createTransaction(
          nonce,
          BigInteger.valueOf(1000),
          BigInteger.valueOf(3000000),
          Address.ONCHAIN_PRIVACY_PROXY.toHexString(),
          payload.toHexString(),
          Base64String.wrap(adder.getEnclaveKey()),
          privacyGroupId,
          org.web3j.utils.Restriction.RESTRICTED);

  return besuClient
      .eeaSendRawTransaction(
          Numeric.toHexString(
              PrivateTransactionEncoder.signMessage(
                  privateTransaction, Credentials.create(adder.getTransactionSigningKey()))))
      .send()
      .getTransactionHash();
}
 
Example #2
Source File: OnChainPrivacyTransactionBuilder.java    From web3j with Apache License 2.0 6 votes vote down vote up
public static String buildOnChainPrivateTransaction(
        Base64String privacyGroupId,
        Credentials credentials,
        Base64String enclaveKey,
        final BigInteger nonce,
        String call) {

    RawPrivateTransaction rawTransaction =
            RawPrivateTransaction.createTransaction(
                    nonce,
                    ZERO_GAS_PROVIDER.getGasPrice(),
                    ZERO_GAS_PROVIDER.getGasLimit(),
                    "0x000000000000000000000000000000000000007c",
                    call,
                    enclaveKey,
                    privacyGroupId,
                    Restriction.RESTRICTED);

    return Numeric.toHexString(
            PrivateTransactionEncoder.signMessage(rawTransaction, 2018, credentials));
}
 
Example #3
Source File: PrivateTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] rlpEncode(final SignatureData signatureData) {
  final RawPrivateTransaction rawTransaction = createTransaction();
  final List<RlpType> values =
      PrivateTransactionEncoder.asRlpValues(rawTransaction, signatureData);
  final RlpList rlpList = new RlpList(values);
  return RlpEncoder.encode(rlpList);
}
 
Example #4
Source File: PrivacyRequestFactory.java    From besu with Apache License 2.0 5 votes vote down vote up
public String privxRemoveFromPrivacyGroup(
    final Base64String privacyGroupId, final PrivacyNode remover, final String toRemove)
    throws IOException, TransactionException {

  final BigInteger nonce =
      besuClient
          .privGetTransactionCount(remover.getAddress().toHexString(), privacyGroupId)
          .send()
          .getTransactionCount();

  final Bytes payload =
      encodeRemoveFromGroupFunctionCall(
          Bytes.fromBase64String(remover.getEnclaveKey()), Bytes.fromBase64String(toRemove));

  final RawPrivateTransaction privateTransaction =
      RawPrivateTransaction.createTransaction(
          nonce,
          BigInteger.valueOf(1000),
          BigInteger.valueOf(3000000),
          Address.ONCHAIN_PRIVACY_PROXY.toHexString(),
          payload.toHexString(),
          Base64String.wrap(remover.getEnclaveKey()),
          privacyGroupId,
          org.web3j.utils.Restriction.RESTRICTED);

  return besuClient
      .eeaSendRawTransaction(
          Numeric.toHexString(
              PrivateTransactionEncoder.signMessage(
                  privateTransaction, Credentials.create(remover.getTransactionSigningKey()))))
      .send()
      .getTransactionHash();
}
 
Example #5
Source File: PrivacyRequestFactory.java    From besu with Apache License 2.0 5 votes vote down vote up
public String privxLockPrivacyGroup(final PrivacyNode locker, final Base64String privacyGroupId)
    throws IOException, TransactionException {
  final BigInteger nonce =
      besuClient
          .privGetTransactionCount(locker.getAddress().toHexString(), privacyGroupId)
          .send()
          .getTransactionCount();

  final RawPrivateTransaction privateTransaction =
      RawPrivateTransaction.createTransaction(
          nonce,
          BigInteger.valueOf(1000),
          BigInteger.valueOf(3000000),
          Address.ONCHAIN_PRIVACY_PROXY.toHexString(),
          OnChainGroupManagement.LOCK_GROUP_METHOD_SIGNATURE.toHexString(),
          Base64String.wrap(locker.getEnclaveKey()),
          privacyGroupId,
          org.web3j.utils.Restriction.RESTRICTED);

  final String transactionHash =
      besuClient
          .eeaSendRawTransaction(
              Numeric.toHexString(
                  PrivateTransactionEncoder.signMessage(
                      privateTransaction, Credentials.create(locker.getTransactionSigningKey()))))
          .send()
          .getTransactionHash();

  final PrivateTransactionReceipt privateTransactionReceipt =
      new PollingPrivateTransactionReceiptProcessor(besuClient, 3000, 10)
          .waitForTransactionReceipt(transactionHash);

  assertThat(privateTransactionReceipt.getStatus()).isEqualTo("0x1");

  return privateTransactionReceipt.getcommitmentHash();
}
 
Example #6
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
public String sign(RawPrivateTransaction rawTransaction) {

        byte[] signedMessage;

        if (chainId > ChainIdLong.NONE) {
            signedMessage =
                    PrivateTransactionEncoder.signMessage(rawTransaction, chainId, credentials);
        } else {
            signedMessage = PrivateTransactionEncoder.signMessage(rawTransaction, credentials);
        }

        return Numeric.toHexString(signedMessage);
    }
 
Example #7
Source File: PrivacyRequestFactory.java    From besu with Apache License 2.0 4 votes vote down vote up
public PrivxCreatePrivacyGroupResponse privxCreatePrivacyGroup(
    final PrivacyNode creator, final List<String> addresses) throws IOException {

  final byte[] bytes = new byte[32];
  secureRandom.nextBytes(bytes);
  final Bytes privacyGroupId = Bytes.wrap(bytes);

  final BigInteger nonce =
      besuClient
          .privGetTransactionCount(
              creator.getAddress().toHexString(),
              Base64String.wrap(privacyGroupId.toArrayUnsafe()))
          .send()
          .getTransactionCount();

  final Bytes payload =
      encodeAddToGroupFunctionCall(
          Bytes.fromBase64String(creator.getEnclaveKey()),
          addresses.stream().map(Bytes::fromBase64String).collect(Collectors.toList()));

  final RawPrivateTransaction privateTransaction =
      RawPrivateTransaction.createTransaction(
          nonce,
          BigInteger.valueOf(1000),
          BigInteger.valueOf(3000000),
          Address.ONCHAIN_PRIVACY_PROXY.toHexString(),
          payload.toHexString(),
          Base64String.wrap(creator.getEnclaveKey()),
          Base64String.wrap(privacyGroupId.toArrayUnsafe()),
          org.web3j.utils.Restriction.RESTRICTED);

  final String transactionHash =
      besuClient
          .eeaSendRawTransaction(
              Numeric.toHexString(
                  PrivateTransactionEncoder.signMessage(
                      privateTransaction,
                      Credentials.create(creator.getTransactionSigningKey()))))
          .send()
          .getTransactionHash();
  return new PrivxCreatePrivacyGroupResponse(privacyGroupId.toBase64String(), transactionHash);
}
 
Example #8
Source File: PrivacyClusterAcceptanceTest.java    From besu with Apache License 2.0 4 votes vote down vote up
@Test
public void aliceCanUsePrivDistributeTransaction() {
  // Contract address is generated from sender address and transaction nonce
  final String contractAddress = "0xebf56429e6500e84442467292183d4d621359838";

  final RawPrivateTransaction rawPrivateTransaction =
      RawPrivateTransaction.createContractTransaction(
          BigInteger.ZERO,
          BigInteger.ZERO,
          BigInteger.ZERO,
          Numeric.prependHexPrefix(EventEmitter.BINARY),
          Base64String.wrap(alice.getEnclaveKey()),
          Collections.singletonList(Base64String.wrap(bob.getEnclaveKey())),
          Restriction.RESTRICTED);

  final String signedPrivateTransaction =
      Numeric.toHexString(
          PrivateTransactionEncoder.signMessage(
              rawPrivateTransaction,
              POW_CHAIN_ID,
              Credentials.create(alice.getTransactionSigningKey())));
  final String transactionKey =
      alice.execute(privacyTransactions.privDistributeTransaction(signedPrivateTransaction));

  final Enclave aliceEnclave = enclaveFactory.createVertxEnclave(alice.getOrion().clientUrl());
  final ReceiveResponse aliceRR =
      aliceEnclave.receive(
          Bytes.fromHexString(transactionKey).toBase64String(), alice.getEnclaveKey());

  final Enclave bobEnclave = enclaveFactory.createVertxEnclave(bob.getOrion().clientUrl());
  final ReceiveResponse bobRR =
      bobEnclave.receive(
          Bytes.fromHexString(transactionKey).toBase64String(), bob.getEnclaveKey());

  assertThat(bobRR).isEqualToComparingFieldByField(aliceRR);

  final RawTransaction pmt =
      RawTransaction.createTransaction(
          BigInteger.ZERO,
          BigInteger.valueOf(1000),
          BigInteger.valueOf(65000),
          Address.DEFAULT_PRIVACY.toString(),
          transactionKey);

  final String signedPmt =
      Numeric.toHexString(
          TransactionEncoder.signMessage(
              pmt, POW_CHAIN_ID, Credentials.create(alice.getTransactionSigningKey())));

  final String transactionHash = alice.execute(ethTransactions.sendRawTransaction(signedPmt));

  final PrivateTransactionReceipt expectedReceipt =
      new PrivateTransactionReceipt(
          contractAddress,
          "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
          null,
          eventEmmitterDeployed,
          Collections.emptyList(),
          "0x023955c49d6265c579561940287449242704d5fd239ff07ea36a3fc7aface61c",
          "0x82e521ee16ff13104c5f81e8354ecaaafd5450b710b07f620204032bfe76041a",
          "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=",
          new ArrayList<>(
              Collections.singletonList("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=")),
          null,
          "0x1",
          null);

  alice.verify(
      privateTransactionVerifier.validPrivateTransactionReceipt(
          transactionHash, expectedReceipt));

  bob.verify(
      privateTransactionVerifier.validPrivateTransactionReceipt(
          transactionHash, expectedReceipt));
}
 
Example #9
Source File: BesuPrivacyQuickstartIntegrationTest.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void simplePrivateTransactions() throws Exception {

    // Build new privacy group using the create API
    final Base64String privacyGroupId =
            nodeBob.privCreatePrivacyGroup(
                            Arrays.asList(
                                    ENCLAVE_KEY_ALICE, ENCLAVE_KEY_BOB, ENCLAVE_KEY_CHARLIE),
                            "AliceBobCharlie",
                            "AliceBobCharlie group")
                    .send()
                    .getPrivacyGroupId();

    final BigInteger nonce =
            nodeCharlie
                    .privGetTransactionCount(ALICE.getAddress(), privacyGroupId)
                    .send()
                    .getTransactionCount();
    final RawPrivateTransaction rawPrivateTransaction =
            RawPrivateTransaction.createContractTransaction(
                    nonce,
                    ZERO_GAS_PROVIDER.getGasPrice(),
                    ZERO_GAS_PROVIDER.getGasLimit(),
                    HUMAN_STANDARD_TOKEN_BINARY,
                    ENCLAVE_KEY_ALICE,
                    privacyGroupId,
                    RESTRICTED);

    final String signedTransactionData =
            Numeric.toHexString(
                    PrivateTransactionEncoder.signMessage(rawPrivateTransaction, 2018, ALICE));

    final String transactionHash =
            nodeAlice.eeaSendRawTransaction(signedTransactionData).send().getTransactionHash();

    final PollingPrivateTransactionReceiptProcessor receiptProcessor =
            new PollingPrivateTransactionReceiptProcessor(nodeAlice, 1 * 1000, 120);
    final PrivateTransactionReceipt receipt =
            receiptProcessor.waitForTransactionReceipt(transactionHash);

    assertEquals(receipt.getFrom(), (ALICE.getAddress()));
    assertEquals(receipt.getLogs().size(), (0));
    assertNull(receipt.getTo());
    assertNotNull(receipt.getContractAddress());

    assertNotNull(receipt.getStatus());

    assertNull(receipt.getRevertReason());

    final PrivateTransactionWithPrivacyGroup privateTransaction =
            (PrivateTransactionWithPrivacyGroup)
                    nodeAlice
                            .privGetPrivateTransaction(transactionHash)
                            .send()
                            .getPrivateTransaction()
                            .get();

    assertEquals(privateTransaction.getFrom(), (ALICE.getAddress()));
    assertEquals(privateTransaction.getGas(), (ZERO_GAS_PROVIDER.getGasLimit()));
    assertEquals(privateTransaction.getGasPrice(), (ZERO_GAS_PROVIDER.getGasPrice()));
    assertEquals(privateTransaction.getNonce(), (nonce));

    final byte[] encodedTransaction =
            PrivateTransactionEncoder.encode(rawPrivateTransaction, 2018);
    final Sign.SignatureData signatureData =
            Sign.signMessage(encodedTransaction, ALICE.getEcKeyPair());
    final Sign.SignatureData eip155SignatureData =
            TransactionEncoder.createEip155SignatureData(signatureData, 2018);
    assertEquals(
            Numeric.toBytesPadded(BigInteger.valueOf(privateTransaction.getV()), 2),
            (eip155SignatureData.getV()));
    assertEquals(
            Numeric.hexStringToByteArray(privateTransaction.getR()),
            (eip155SignatureData.getR()));
    assertEquals(
            Numeric.hexStringToByteArray(privateTransaction.getS()),
            (eip155SignatureData.getS()));

    assertEquals(privateTransaction.getPrivateFrom(), (ENCLAVE_KEY_ALICE));
    assertEquals(privateTransaction.getPrivacyGroupId(), (privacyGroupId));
    assertEquals(privateTransaction.getRestriction(), (RESTRICTED));
    assertNull(privateTransaction.getTo());
}