org.web3j.utils.Base64String Java Examples

The following examples show how to use org.web3j.utils.Base64String. 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: PrivacyGroupAcceptanceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void nodeCanCreatePrivacyGroupWithoutName() {
  final String privacyGroupId =
      alice.execute(
          privacyTransactions.createPrivacyGroup(null, "my group description", alice, bob));

  assertThat(privacyGroupId).isNotNull();

  final PrivacyGroup expected =
      new PrivacyGroup(
          privacyGroupId,
          PrivacyGroup.Type.PANTHEON,
          "",
          "my group description",
          Base64String.wrapList(alice.getEnclaveKey(), bob.getEnclaveKey()));

  alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));

  bob.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));
}
 
Example #2
Source File: JsonRpc2_0Besu.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Override
public Request<?, EthSendTransaction> privOnChainRemoveFromPrivacyGroup(
        final Base64String privacyGroupId,
        final Credentials credentials,
        final Base64String enclaveKey,
        final Base64String participant)
        throws IOException {
    BigInteger transactionCount =
            privGetTransactionCount(credentials.getAddress(), privacyGroupId)
                    .send()
                    .getTransactionCount();
    String removeFromContractCall =
            OnChainPrivacyTransactionBuilder.getEncodedRemoveFromGroupFunction(
                    enclaveKey, participant.raw());

    String removeFromProupTransactionPayload =
            OnChainPrivacyTransactionBuilder.buildOnChainPrivateTransaction(
                    privacyGroupId,
                    credentials,
                    enclaveKey,
                    transactionCount,
                    removeFromContractCall);

    return eeaSendRawTransaction(removeFromProupTransactionPayload);
}
 
Example #3
Source File: RawPrivateTransaction.java    From web3j with Apache License 2.0 6 votes vote down vote up
protected RawPrivateTransaction(
        final BigInteger nonce,
        final BigInteger gasPremium,
        final BigInteger feeCap,
        final BigInteger gasLimit,
        final String to,
        final String data,
        final Base64String privateFrom,
        final List<Base64String> privateFor,
        final Base64String privacyGroupId,
        final Restriction restriction) {
    super(nonce, null, gasLimit, to, BigInteger.ZERO, data, gasPremium, feeCap);
    this.privateFrom = privateFrom;
    this.privateFor = privateFor;
    this.privacyGroupId = privacyGroupId;
    this.restriction = restriction;
}
 
Example #4
Source File: RawPrivateTransaction.java    From web3j with Apache License 2.0 6 votes vote down vote up
protected RawPrivateTransaction(
        final BigInteger nonce,
        final BigInteger gasPrice,
        final BigInteger gasLimit,
        final String to,
        final String data,
        final Base64String privateFrom,
        final List<Base64String> privateFor,
        final Base64String privacyGroupId,
        final Restriction restriction) {
    super(nonce, gasPrice, gasLimit, to, BigInteger.ZERO, data);
    this.privateFrom = privateFrom;
    this.privateFor = privateFor;
    this.privacyGroupId = privacyGroupId;
    this.restriction = restriction;
}
 
Example #5
Source File: RawPrivateTransaction.java    From web3j with Apache License 2.0 6 votes vote down vote up
private RawPrivateTransaction(
        final RawTransaction rawTransaction,
        final Base64String privateFrom,
        final List<Base64String> privateFor,
        final Base64String privacyGroupId,
        final Restriction restriction) {
    this(
            rawTransaction.getNonce(),
            rawTransaction.getGasPrice(),
            rawTransaction.getGasLimit(),
            rawTransaction.getTo(),
            rawTransaction.getData(),
            privateFrom,
            privateFor,
            privacyGroupId,
            restriction);
}
 
Example #6
Source File: PrivacyGroupAcceptanceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void nodeCanCreatePrivacyGroupWithoutOptionalParams() {
  final String privacyGroupId =
      alice.execute(privacyTransactions.createPrivacyGroup(null, null, alice, bob));

  assertThat(privacyGroupId).isNotNull();

  final PrivacyGroup expected =
      new PrivacyGroup(
          privacyGroupId,
          PrivacyGroup.Type.PANTHEON,
          "",
          "",
          Base64String.wrapList(alice.getEnclaveKey(), bob.getEnclaveKey()));

  alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));

  bob.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));
}
 
Example #7
Source File: PrivGetCodeAcceptanceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
private String createPrivacyGroup() {
  final String privacyGroupId =
      alice.execute(
          privacyTransactions.createPrivacyGroup("myGroupName", "my group description", alice));

  assertThat(privacyGroupId).isNotNull();

  final PrivacyGroup expected =
      new PrivacyGroup(
          privacyGroupId,
          PrivacyGroup.Type.PANTHEON,
          "myGroupName",
          "my group description",
          Base64String.wrapList(alice.getEnclaveKey()));

  alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));

  return privacyGroupId;
}
 
Example #8
Source File: BesuOnChainPrivacyIntegrationTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateAndFindOnChainPrivacyGroup() throws Exception {
    Base64String privacyGroupId = Base64String.wrap(generateRandomBytes(32));
    final String txHash =
            nodeAlice
                    .privOnChainCreatePrivacyGroup(
                            privacyGroupId,
                            ALICE,
                            ENCLAVE_KEY_ALICE,
                            Collections.singletonList(ENCLAVE_KEY_BOB))
                    .send()
                    .getTransactionHash();

    TransactionReceipt receipt = processor.waitForTransactionReceipt(txHash);
    assertTrue(receipt.isStatusOK());

    List<PrivacyGroup> groups =
            nodeAlice
                    .privOnChainFindPrivacyGroup(
                            Arrays.asList(ENCLAVE_KEY_ALICE, ENCLAVE_KEY_BOB))
                    .send()
                    .getGroups();

    assertTrue(groups.stream().anyMatch(x -> x.getPrivacyGroupId().equals(privacyGroupId)));
}
 
Example #9
Source File: PrivacyGroupAcceptanceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void nodeCanCreatePrivacyGroup() {
  final String privacyGroupId =
      alice.execute(
          privacyTransactions.createPrivacyGroup(
              "myGroupName", "my group description", alice, bob));

  assertThat(privacyGroupId).isNotNull();

  final PrivacyGroup expected =
      new PrivacyGroup(
          privacyGroupId,
          PrivacyGroup.Type.PANTHEON,
          "myGroupName",
          "my group description",
          Base64String.wrapList(alice.getEnclaveKey(), bob.getEnclaveKey()));

  alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));

  bob.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected));
}
 
Example #10
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 6 votes vote down vote up
protected PrivateTransactionManager(
        final Besu besu,
        final BesuPrivacyGasProvider gasProvider,
        final Credentials credentials,
        final long chainId,
        final Base64String privateFrom,
        final int attempts,
        final int sleepDuration) {
    this(
            besu,
            gasProvider,
            credentials,
            chainId,
            privateFrom,
            new PollingPrivateTransactionReceiptProcessor(besu, sleepDuration, attempts));
}
 
Example #11
Source File: PrivateTransactionEncoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
public static List<RlpType> asRlpValues(
        final RawPrivateTransaction privateTransaction,
        final Sign.SignatureData signatureData) {

    final List<RlpType> result =
            new ArrayList<>(
                    TransactionEncoder.asRlpValues(
                            privateTransaction.asRawTransaction(), signatureData));

    result.add(privateTransaction.getPrivateFrom().asRlp());

    privateTransaction
            .getPrivateFor()
            .ifPresent(privateFor -> result.add(Base64String.unwrapListToRlp(privateFor)));

    privateTransaction.getPrivacyGroupId().map(Base64String::asRlp).ifPresent(result::add);

    result.add(RlpString.create(privateTransaction.getRestriction().getRestriction()));

    return result;
}
 
Example #12
Source File: EeaSendTransactionJsonParametersTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void transactionStoredInJsonArrayCanBeDecoded() {
  final JsonObject parameters = validEeaTransactionParameters();

  final JsonRpcRequest request = wrapParametersInRequest(parameters);
  final EeaSendTransactionJsonParameters txnParams =
      factory.fromRpcRequestToJsonParam(EeaSendTransactionJsonParameters.class, request);

  assertThat(txnParams.gas()).isEqualTo(getStringAsOptionalBigInteger(parameters, "gas"));
  assertThat(txnParams.gasPrice())
      .isEqualTo(getStringAsOptionalBigInteger(parameters, "gasPrice"));
  assertThat(txnParams.nonce()).isEqualTo(getStringAsOptionalBigInteger(parameters, "nonce"));
  assertThat(txnParams.receiver()).isEqualTo(Optional.of(parameters.getString("to")));
  assertThat(txnParams.value()).isEqualTo(getStringAsOptionalBigInteger(parameters, "value"));
  assertThat(txnParams.privateFrom())
      .isEqualTo(Base64String.wrap(parameters.getString("privateFrom")));
  assertThat(txnParams.privateFor().get())
      .containsExactly(Base64String.wrap(parameters.getJsonArray("privateFor").getString(0)));
  assertThat(txnParams.restriction()).isEqualTo(parameters.getString("restriction"));
}
 
Example #13
Source File: EeaSendTransactionJsonParametersTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void transactionNotStoredInJsonArrayCanBeDecoded() {
  final JsonObject parameters = validEeaTransactionParameters();

  final JsonRpcRequest request = wrapParametersInRequest(parameters);
  final EeaSendTransactionJsonParameters txnParams =
      factory.fromRpcRequestToJsonParam(EeaSendTransactionJsonParameters.class, request);

  assertThat(txnParams.gas()).isEqualTo(getStringAsOptionalBigInteger(parameters, "gas"));
  assertThat(txnParams.gasPrice())
      .isEqualTo(getStringAsOptionalBigInteger(parameters, "gasPrice"));
  assertThat(txnParams.nonce()).isEqualTo(getStringAsOptionalBigInteger(parameters, "nonce"));
  assertThat(txnParams.receiver()).isEqualTo(Optional.of(parameters.getString("to")));
  assertThat(txnParams.value()).isEqualTo(getStringAsOptionalBigInteger(parameters, "value"));
  assertThat(txnParams.privateFrom())
      .isEqualTo(Base64String.wrap(parameters.getString("privateFrom")));
  assertThat(txnParams.privateFor().get())
      .containsExactly(Base64String.wrap(parameters.getJsonArray("privateFor").getString(0)));
  assertThat(txnParams.restriction()).isEqualTo(parameters.getString("restriction"));
}
 
Example #14
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 #15
Source File: RawPrivateTransaction.java    From web3j with Apache License 2.0 5 votes vote down vote up
protected RawPrivateTransaction(
        final RawTransaction rawTransaction,
        final Base64String privateFrom,
        final List<Base64String> privateFor,
        final Restriction restriction) {
    this(rawTransaction, privateFrom, privateFor, null, restriction);
}
 
Example #16
Source File: BesuPrivateTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
public BesuPrivateTransactionManager(
        final Besu besu,
        final BesuPrivacyGasProvider gasProvider,
        final Credentials credentials,
        final long chainId,
        final Base64String privateFrom,
        final Base64String privacyGroupId) {
    super(besu, gasProvider, credentials, chainId, privateFrom);
    this.privacyGroupId = privacyGroupId;
}
 
Example #17
Source File: JsonRpc2_0Besu.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Request<?, EthGetTransactionCount> privGetTransactionCount(
        final String address, final Base64String privacyGroupId) {
    return new Request<>(
            "priv_getTransactionCount",
            Arrays.asList(address, privacyGroupId.toString()),
            web3jService,
            EthGetTransactionCount.class);
}
 
Example #18
Source File: RawPrivateTransaction.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static RawPrivateTransaction createContractTransaction(
        final BigInteger nonce,
        final BigInteger gasPrice,
        final BigInteger gasLimit,
        final String init,
        final Base64String privateFrom,
        final List<Base64String> privateFor,
        final Restriction restriction) {

    return new RawPrivateTransaction(
            nonce, gasPrice, gasLimit, "", init, privateFrom, privateFor, null, restriction);
}
 
Example #19
Source File: JsonRpc2_0Besu.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Request<?, EthSendTransaction> privOnChainCreatePrivacyGroup(
        final Base64String privacyGroupId,
        final Credentials credentials,
        final Base64String enclaveKey,
        final List<Base64String> participants)
        throws IOException {
    List<byte[]> participantsAsBytes =
            participants.stream().map(Base64String::raw).collect(Collectors.toList());
    BigInteger transactionCount =
            privGetTransactionCount(credentials.getAddress(), privacyGroupId)
                    .send()
                    .getTransactionCount();
    String addToContractCall =
            OnChainPrivacyTransactionBuilder.getEncodedAddToGroupFunction(
                    enclaveKey, participantsAsBytes);

    String addToPrivacyGroupTransactionPayload =
            OnChainPrivacyTransactionBuilder.buildOnChainPrivateTransaction(
                    privacyGroupId,
                    credentials,
                    enclaveKey,
                    transactionCount,
                    addToContractCall);

    return eeaSendRawTransaction(addToPrivacyGroupTransactionPayload);
}
 
Example #20
Source File: CreatePrivacyGroupRequest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@JsonCreator
public CreatePrivacyGroupRequest(
        @JsonProperty("addresses") final List<Base64String> addresses,
        @JsonProperty("name") final String name,
        @JsonProperty("description") final String description) {
    this.addresses = addresses;
    this.name = name;
    this.description = description;
}
 
Example #21
Source File: CallOnChainPermissioningPrivateSmartContractFunction.java    From besu with Apache License 2.0 5 votes vote down vote up
public CallOnChainPermissioningPrivateSmartContractFunction(
    final String contractAddress,
    final String encodedFunction,
    final String transactionSigningKey,
    final long chainId,
    final String privateFrom,
    final String privacyGroupId) {

  this.contractAddress = contractAddress;
  this.encodedFunction = encodedFunction;
  this.senderCredentials = Credentials.create(transactionSigningKey);
  this.chainId = chainId;
  this.privateFrom = Base64String.wrap(privateFrom);
  this.privacyGroupId = Base64String.wrap(privacyGroupId);
}
 
Example #22
Source File: BesuOnChainPrivacyIntegrationTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCannotAddToAlreadyLockedGroup() throws Exception {
    Base64String privacyGroupId = Base64String.wrap(generateRandomBytes(32));
    final String createTxHash =
            nodeAlice
                    .privOnChainCreatePrivacyGroup(
                            privacyGroupId,
                            ALICE,
                            ENCLAVE_KEY_ALICE,
                            Collections.singletonList(ENCLAVE_KEY_BOB))
                    .send()
                    .getTransactionHash();

    TransactionReceipt createReceipt = processor.waitForTransactionReceipt(createTxHash);
    assertTrue(createReceipt.isStatusOK());

    final String lockTxHash =
            nodeAlice
                    .privOnChainSetGroupLockState(
                            privacyGroupId, ALICE, ENCLAVE_KEY_ALICE, true)
                    .send()
                    .getTransactionHash();

    TransactionReceipt lockReceipt = processor.waitForTransactionReceipt(lockTxHash);
    assertTrue(lockReceipt.isStatusOK());

    assertThrows(
            TransactionException.class,
            () ->
                    nodeBob.privOnChainAddToPrivacyGroup(
                                    privacyGroupId,
                                    BOB,
                                    ENCLAVE_KEY_BOB,
                                    Collections.singletonList(ENCLAVE_KEY_CHARLIE))
                            .send());
}
 
Example #23
Source File: OnChainPrivacyTransactionBuilder.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static String getEncodedRemoveFromGroupFunction(
        Base64String enclaveKey, byte[] participant) {
    final org.web3j.abi.datatypes.Function function =
            new org.web3j.abi.datatypes.Function(
                    "removeParticipant",
                    Arrays.asList(new Bytes32(enclaveKey.raw()), new Bytes32(participant)),
                    Collections.emptyList());
    return FunctionEncoder.encode(function);
}
 
Example #24
Source File: EthSignerAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void privateSmartContractMustDeployWithPrivacyGroupNoNonce() throws IOException {
  final String privacyGroupId =
      minerNode.execute(privacyTransactions.createPrivacyGroup(null, null, minerNode));

  minerNode.verify(
      privateTransactionVerifier.validPrivacyGroupCreated(
          new PrivacyGroup(
              privacyGroupId,
              PrivacyGroup.Type.PANTHEON,
              "",
              "",
              Base64String.wrapList(minerNode.getEnclaveKey()))));

  final String transactionHash =
      ethSignerClient.eeaSendTransaction(
          null,
          BigInteger.valueOf(23176),
          BigInteger.valueOf(1000),
          EventEmitter.BINARY,
          minerNode.getEnclaveKey(),
          privacyGroupId,
          "restricted");

  final PrivateTransactionReceipt receipt =
      minerNode.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash));

  minerNode.verify(
      privateTransactionVerifier.validPrivateTransactionReceipt(transactionHash, receipt));
}
 
Example #25
Source File: EthSignerAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void privateSmartContractMustDeployWithPrivacyGroup() throws IOException {
  final String privacyGroupId =
      minerNode.execute(privacyTransactions.createPrivacyGroup(null, null, minerNode));

  minerNode.verify(
      privateTransactionVerifier.validPrivacyGroupCreated(
          new PrivacyGroup(
              privacyGroupId,
              PrivacyGroup.Type.PANTHEON,
              "",
              "",
              Base64String.wrapList(minerNode.getEnclaveKey()))));

  final String transactionHash =
      ethSignerClient.eeaSendTransaction(
          null,
          BigInteger.valueOf(23176),
          BigInteger.valueOf(1000),
          EventEmitter.BINARY,
          BigInteger.valueOf(0),
          minerNode.getEnclaveKey(),
          privacyGroupId,
          "restricted");

  final PrivateTransactionReceipt receipt =
      minerNode.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash));

  minerNode.verify(
      privateTransactionVerifier.validPrivateTransactionReceipt(transactionHash, receipt));
}
 
Example #26
Source File: ExpectValidPrivacyGroupCreated.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(final PrivacyNode node) {
  Awaitility.await()
      .untilAsserted(
          () -> {
            final List<PrivacyGroup> groups =
                node.execute(
                    transactions.findPrivacyGroup(
                        Base64String.unwrapList(expected.getMembers())));
            assertThat(groups).contains(expected);
          });
}
 
Example #27
Source File: PrivateTransactionVerifier.java    From besu with Apache License 2.0 5 votes vote down vote up
public ExpectValidOnChainPrivacyGroupCreated onChainPrivacyGroupExists(
    final String privacyGroupId, final PrivacyNode... members) {

  final List<Base64String> membersEnclaveKeys =
      Arrays.stream(members)
          .map(PrivacyNode::getEnclaveKey)
          .map(Base64String::wrap)
          .collect(Collectors.toList());

  final OnChainPrivacyGroup expectedGroup =
      new OnChainPrivacyGroup(privacyGroupId, membersEnclaveKeys);

  return new ExpectValidOnChainPrivacyGroupCreated(transactions, expectedGroup);
}
 
Example #28
Source File: DeployPrivateSmartContractTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
public DeployPrivateSmartContractTransaction(
    final Class<T> clazz,
    final String transactionSigningKey,
    final long chainId,
    final String privateFrom,
    final List<String> privateFor) {
  this.clazz = clazz;
  this.senderCredentials = Credentials.create(transactionSigningKey);
  this.chainId = chainId;
  this.privateFrom = Base64String.wrap(privateFrom);
  this.privateFor = Base64String.wrapList(privateFor);
}
 
Example #29
Source File: LegacyPrivateTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
public LegacyPrivateTransactionManager(
        final Besu besu,
        final BesuPrivacyGasProvider gasProvider,
        final Credentials credentials,
        final long chainId,
        final Base64String privateFrom,
        final List<Base64String> privateFor,
        final int attempts,
        final int sleepDuration) {
    super(besu, gasProvider, credentials, chainId, privateFrom, attempts, sleepDuration);
    this.privateFor = privateFor;
    this.privacyGroupId = PrivacyGroupUtils.generateLegacyGroup(privateFrom, privateFor);
}
 
Example #30
Source File: LoadPrivateSmartContractTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
public LoadPrivateSmartContractTransaction(
    final String contractAddress,
    final Class<T> clazz,
    final String transactionSigningKey,
    final long chainId,
    final String privateFrom,
    final List<String> privateFor) {

  this.contractAddress = contractAddress;
  this.clazz = clazz;
  this.senderCredentials = Credentials.create(transactionSigningKey);
  this.chainId = chainId;
  this.privateFrom = Base64String.wrap(privateFrom);
  this.privateFor = Base64String.wrapList(privateFor);
}