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

The following examples show how to use org.web3j.protocol.eea.crypto.RawPrivateTransaction. 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: 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 #2
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 #3
Source File: EeaPrivateTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
protected RawPrivateTransaction createTransaction() {
  return RawPrivateTransaction.createTransaction(
      nonce,
      transactionJsonParameters.gasPrice().orElse(DEFAULT_GAS_PRICE),
      transactionJsonParameters.gas().orElse(DEFAULT_GAS),
      transactionJsonParameters.receiver().orElse(DEFAULT_TO),
      transactionJsonParameters.data().orElse(DEFAULT_DATA),
      transactionJsonParameters.privateFrom(),
      privateFor,
      Restriction.fromString(transactionJsonParameters.restriction()));
}
 
Example #4
Source File: BesuPrivateTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
protected RawPrivateTransaction createTransaction() {
  return RawPrivateTransaction.createTransaction(
      nonce,
      transactionJsonParameters.gasPrice().orElse(DEFAULT_GAS_PRICE),
      transactionJsonParameters.gas().orElse(DEFAULT_GAS),
      transactionJsonParameters.receiver().orElse(DEFAULT_TO),
      transactionJsonParameters.data().orElse(DEFAULT_DATA),
      transactionJsonParameters.privateFrom(),
      privacyGroupId,
      Restriction.fromString(transactionJsonParameters.restriction()));
}
 
Example #5
Source File: EeaSendRawTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
private RawPrivateTransaction createBesuRawPrivateTransaction(
    final JsonObject transaction, final String privacyGroupId) {
  return RawPrivateTransaction.createTransaction(
      valueToBigDecimal(transaction.getString("nonce")),
      valueToBigDecimal(transaction.getString("gasPrice")),
      valueToBigDecimal(transaction.getString("gas")),
      transaction.getString("to"),
      transaction.getString("data"),
      valueToBase64String(transaction.getString("privateFrom")),
      valueToBase64String(privacyGroupId),
      Restriction.fromString(transaction.getString("restriction")));
}
 
Example #6
Source File: EeaSendRawTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
private RawPrivateTransaction createEeaRawPrivateTransaction(final JsonObject transaction) {
  return RawPrivateTransaction.createTransaction(
      valueToBigDecimal(transaction.getString("nonce")),
      valueToBigDecimal(transaction.getString("gasPrice")),
      valueToBigDecimal(transaction.getString("gas")),
      transaction.getString("to"),
      transaction.getString("data"),
      valueToBase64String(transaction.getString("privateFrom")),
      privateFor(transaction.getJsonArray("privateFor")),
      Restriction.fromString(transaction.getString("restriction")));
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 4 votes vote down vote up
public PrivateEnclaveKey signAndDistribute(RawPrivateTransaction rawTransaction)
        throws IOException {
    String hexValue = sign(rawTransaction);
    return this.besu.privDistributeRawTransaction(hexValue).send();
}
 
Example #13
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 4 votes vote down vote up
public EthSendTransaction signAndSend(RawPrivateTransaction rawTransaction) throws IOException {
    String hexValue = sign(rawTransaction);
    return this.besu.eeaSendRawTransaction(hexValue).send();
}
 
Example #14
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 4 votes vote down vote up
public EthSendTransaction sendTransactionEIP1559(
        BigInteger gasPremium,
        BigInteger feeCap,
        BigInteger gasLimit,
        String to,
        String data,
        BigInteger value,
        boolean constructor)
        throws IOException {
    final BigInteger nonce =
            besu.privGetTransactionCount(credentials.getAddress(), getPrivacyGroupId())
                    .send()
                    .getTransactionCount();

    final Object privacyGroupIdOrPrivateFor = privacyGroupIdOrPrivateFor();

    final RawPrivateTransaction transaction;
    if (privacyGroupIdOrPrivateFor instanceof Base64String) {
        transaction =
                RawPrivateTransaction.createTransactionEIP1559(
                        nonce,
                        gasPremium,
                        feeCap,
                        gasLimit,
                        to,
                        data,
                        privateFrom,
                        (Base64String) privacyGroupIdOrPrivateFor,
                        RESTRICTED);
    } else {
        transaction =
                RawPrivateTransaction.createTransactionEIP1559(
                        nonce,
                        gasPremium,
                        feeCap,
                        gasLimit,
                        to,
                        data,
                        privateFrom,
                        (List<Base64String>) privacyGroupIdOrPrivateFor,
                        RESTRICTED);
    }

    return signAndSend(transaction);
}
 
Example #15
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public EthSendTransaction sendTransaction(
        final BigInteger gasPrice,
        final BigInteger gasLimit,
        final String to,
        final String data,
        final BigInteger value,
        boolean constructor)
        throws IOException {

    final BigInteger nonce =
            besu.privGetTransactionCount(credentials.getAddress(), getPrivacyGroupId())
                    .send()
                    .getTransactionCount();

    final Object privacyGroupIdOrPrivateFor = privacyGroupIdOrPrivateFor();

    final RawPrivateTransaction transaction;
    if (privacyGroupIdOrPrivateFor instanceof Base64String) {
        transaction =
                RawPrivateTransaction.createTransaction(
                        nonce,
                        gasPrice,
                        gasLimit,
                        to,
                        data,
                        privateFrom,
                        (Base64String) privacyGroupIdOrPrivateFor,
                        RESTRICTED);
    } else {
        transaction =
                RawPrivateTransaction.createTransaction(
                        nonce,
                        gasPrice,
                        gasLimit,
                        to,
                        data,
                        privateFrom,
                        (List<Base64String>) privacyGroupIdOrPrivateFor,
                        RESTRICTED);
    }

    return signAndSend(transaction);
}
 
Example #16
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());
}
 
Example #17
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 #18
Source File: PrivateTransaction.java    From ethsigner with Apache License 2.0 votes vote down vote up
protected abstract RawPrivateTransaction createTransaction();