Java Code Examples for org.web3j.crypto.TransactionEncoder

The following examples show how to use org.web3j.crypto.TransactionEncoder. These examples are extracted from open source projects. 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 Project: ethsigner   Source File: TransactionSerializer.java    License: Apache License 2.0 6 votes vote down vote up
public String serialize(final Transaction transaction) {
  final byte[] bytesToSign = transaction.rlpEncode(chainId);

  final Signature signature = signer.sign(bytesToSign);

  final SignatureData web3jSignature =
      new SignatureData(
          signature.getV().toByteArray(),
          signature.getR().toByteArray(),
          signature.getS().toByteArray());

  final SignatureData eip155Signature =
      TransactionEncoder.createEip155SignatureData(web3jSignature, chainId);

  final byte[] serializedBytes = transaction.rlpEncode(eip155Signature);
  return Numeric.toHexString(serializedBytes);
}
 
Example 2
public Single<String>  createEthTransaction(ETHWallet from,  String to, BigInteger amount, BigInteger gasPrice, BigInteger gasLimit, String password) {
    final Web3j web3j = Web3j.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl));

    return networkRepository.getLastTransactionNonce(web3j, from.address)
            .flatMap(nonce -> Single.fromCallable( () -> {

        Credentials credentials = WalletUtils.loadCredentials(password,  from.getKeystorePath());
        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, amount);
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);

        String hexValue = Numeric.toHexString(signedMessage);
        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();

        return ethSendTransaction.getTransactionHash();

    } ).subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread()));
}
 
Example 3
Source Project: client-sdk-java   Source File: RawTransactionManager.java    License: Apache License 2.0 6 votes vote down vote up
public PlatonSendTransaction signAndSend(RawTransaction rawTransaction)
        throws IOException {

    byte[] signedMessage;

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

    String hexValue = Numeric.toHexString(signedMessage);
    PlatonSendTransaction ethSendTransaction = web3j.platonSendRawTransaction(hexValue).send();

    if (ethSendTransaction != null && !ethSendTransaction.hasError()) {
        String txHashLocal = Hash.sha3(hexValue);
        String txHashRemote = ethSendTransaction.getTransactionHash();
        if (!txHashVerifier.verify(txHashLocal, txHashRemote)) {
            throw new TxHashMismatchException(txHashLocal, txHashRemote);
        }
    }

    return ethSendTransaction;
}
 
Example 4
private String execute(
        Credentials credentials, Function function, String contractAddress) throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedFunction = FunctionEncoder.encode(function);

    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            contractAddress,
            encodedFunction);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example 5
@Test
public void testTransferEther() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());
    RawTransaction rawTransaction = createEtherTransaction(
            nonce, BOB.getAddress());

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    String transactionHash = ethSendTransaction.getTransactionHash();

    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt =
            waitForTransactionReceipt(transactionHash);

    assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
}
 
Example 6
@Test
public void testDeploySmartContract() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());
    RawTransaction rawTransaction = createSmartContractTransaction(nonce);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    String transactionHash = ethSendTransaction.getTransactionHash();

    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt =
            waitForTransactionReceipt(transactionHash);

    assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));

    assertFalse("Contract execution ran out of gas",
            rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()));
}
 
Example 7
@Test
public void testSignTransaction() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    RawTransaction rawTransaction = createTransaction();

    byte[] encoded = TransactionEncoder.encode(rawTransaction);
    byte[] hashed = Hash.sha3(encoded);

    EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed))
            .sendAsync().get();

    String signature = ethSign.getSignature();
    assertNotNull(signature);
    assertFalse(signature.isEmpty());
}
 
Example 8
Source Project: Android-Wallet-Token-ERC20   Source File: SendingEther.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected EthSendTransaction doInBackground(String... values) {
    BigInteger ammount = Convert.toWei(values[1], Convert.Unit.ETHER).toBigInteger();
    try {

        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(getNonce(), getGasPrice(), getGasLimit(), values[0], ammount);

        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, mCredentials);
        String hexValue = "0x"+ Hex.toHexString(signedMessage);

        return mWeb3j.ethSendRawTransaction(hexValue.toString()).sendAsync().get();

    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 9
Source Project: web3j   Source File: HumanStandardTokenIT.java    License: Apache License 2.0 6 votes vote down vote up
private String execute(Credentials credentials, Function function, String contractAddress)
        throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedFunction = FunctionEncoder.encode(function);

    RawTransaction rawTransaction =
            RawTransaction.createTransaction(
                    nonce, GAS_PRICE, GAS_LIMIT, contractAddress, encodedFunction);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example 10
Source Project: web3j   Source File: CreateRawTransactionIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTransferEther() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());
    RawTransaction rawTransaction = createEtherTransaction(nonce, BOB.getAddress());

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    String transactionHash = ethSendTransaction.getTransactionHash();

    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash);

    assertEquals(transactionHash, transactionReceipt.getTransactionHash());
}
 
Example 11
Source Project: web3j   Source File: CreateRawTransactionIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDeploySmartContract() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());
    RawTransaction rawTransaction = createSmartContractTransaction(nonce);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    String transactionHash = ethSendTransaction.getTransactionHash();

    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash);

    assertEquals(transactionHash, transactionReceipt.getTransactionHash());

    assertFalse(
            rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()),
            "Contract execution ran out of gas");
}
 
Example 12
Source Project: web3j   Source File: SignTransactionIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSignTransaction() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    RawTransaction rawTransaction = createTransaction();

    byte[] encoded = TransactionEncoder.encode(rawTransaction);
    byte[] hashed = Hash.sha3(encoded);

    EthSign ethSign =
            web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed)).sendAsync().get();

    String signature = ethSign.getSignature();
    assertNotNull(signature);
    assertFalse(signature.isEmpty());
}
 
Example 13
Source Project: web3j   Source File: PrivateTransactionEncoder.java    License: 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 14
Source Project: ethsigner   Source File: EthTransaction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] rlpEncode(final SignatureData signatureData) {
  final RawTransaction rawTransaction = createTransaction();
  final List<RlpType> values = TransactionEncoder.asRlpValues(rawTransaction, signatureData);
  final RlpList rlpList = new RlpList(values);
  return RlpEncoder.encode(rlpList);
}
 
Example 15
Source Project: besu   Source File: TransferTransaction.java    License: Apache License 2.0 5 votes vote down vote up
public String signedTransactionData() {
  final Optional<BigInteger> nonce = getNonce();

  final RawTransaction transaction =
      RawTransaction.createEtherTransaction(
          nonce.orElse(nonce.orElseGet(sender::getNextNonce)),
          gasPrice,
          INTRINSIC_GAS,
          recipient.getAddress(),
          Convert.toWei(transferAmount, transferUnit).toBigIntegerExact());

  return toHexString(
      TransactionEncoder.signMessage(transaction, sender.web3jCredentialsOrThrow()));
}
 
Example 16
private  Single<byte[]> signEncodeRawTransaction(RawTransaction rtx, String password, ETHWallet wallet, int chainId) {

        return Single.fromCallable(() -> {
            Credentials credentials = WalletUtils.loadCredentials(password, wallet.getKeystorePath());
            byte[] signedMessage = TransactionEncoder.signMessage(rtx, credentials);
            return signedMessage;
        });
    }
 
Example 17
Source Project: client-sdk-java   Source File: TransferTest.java    License: Apache License 2.0 5 votes vote down vote up
public String sendTransaction(String privateKey, String toAddress, BigDecimal amount, long gasPrice, long gasLimit) {

        BigInteger GAS_PRICE = BigInteger.valueOf(gasPrice);
        BigInteger GAS_LIMIT = BigInteger.valueOf(gasLimit);

        Credentials credentials = Credentials.create(privateKey);

        try {

            List<RlpType> result = new ArrayList<>();
            result.add(RlpString.create(Numeric.hexStringToByteArray(PlatOnTypeEncoder.encode(new Int64(0)))));
            String txType = Hex.toHexString(RlpEncoder.encode(new RlpList(result)));

            RawTransaction rawTransaction = RawTransaction.createTransaction(getNonce(), GAS_PRICE, GAS_LIMIT, toAddress, amount.toBigInteger(),
                    txType);

            byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, new Byte("100"), credentials);
            String hexValue = Numeric.toHexString(signedMessage);

            PlatonSendTransaction transaction = web3j.platonSendRawTransaction(hexValue).send();

            return transaction.getTransactionHash();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
Example 18
private String sendCreateContractTransaction(
        Credentials credentials, BigInteger initialSupply) throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedConstructor =
            FunctionEncoder.encodeConstructor(
                    Arrays.asList(
                            new Uint256(initialSupply),
                            new Utf8String("web3j tokens"),
                            new Uint8(BigInteger.TEN),
                            new Utf8String("w3j$")));

    RawTransaction rawTransaction = RawTransaction.createContractTransaction(
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            BigInteger.ZERO,
            getHumanStandardTokenBinary() + encodedConstructor);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example 19
public EthSendTransaction signAndSend(RawTransaction rawTransaction)
        throws IOException {

    byte[] signedMessage;

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

    String hexValue = Numeric.toHexString(signedMessage);

    return web3j.ethSendRawTransaction(hexValue).send();
}
 
Example 20
@Override
protected RawTransactionResponse doInBackground(Void... params) {
    BigInteger nonce = getNonce();
    RawTransaction rawTransaction;
    String blockNumber = getBlockNumber();
    byte[] signedMessage;
    if (TextUtils.isEmpty(contractData)) {
        rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, value);
    } else {
        rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, new String(Hex.encode(contractData.getBytes())));
    }



    if (walletManager.getCredentials() != null && rawTransaction != null) {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, walletManager.getCredentials());
    } else {
        return null;
    }

    String hexValue = Numeric.toHexString(signedMessage);
    EthSendTransaction ethSendTransaction = null;


    try {
        ethSendTransaction = web3jConnection.ethSendRawTransaction(hexValue).send();
    } catch (IOException e) {
        e.printStackTrace();
        Log.d("psd", e.toString());
    }

    if (ethSendTransaction != null && ethSendTransaction.getTransactionHash() != null) {
        return new RawTransactionResponse(ethSendTransaction.getTransactionHash(),
                hexValue, blockNumber);
    } else {
        return null;
    }
}
 
Example 21
@Override
protected RawTransactionResponse doInBackground(Void... params) {
    BigInteger nonce = getNonce();
    if (nonce == null) return null;
    RawTransaction rawTransaction;
    String blockNumber = getBlocNumber();
    if (TextUtils.isEmpty(contractData)) {
        rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, value);
    } else {
        rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, new String(Hex.encode(contractData.getBytes())));
    }

    byte[] signedMessage;
    if (walletManager.getCredentials() != null && rawTransaction != null) {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, walletManager.getCredentials());
    } else {
        return null;
    }

    String hexValue = Numeric.toHexString(signedMessage);
    EthSendTransaction ethSendTransaction = null;


        try {
            ethSendTransaction = web3jConnection.ethSendRawTransaction(hexValue).send();
        } catch (IOException e) {
            e.printStackTrace();
        }

    if (ethSendTransaction != null && ethSendTransaction.getTransactionHash() != null) {
        return new RawTransactionResponse(ethSendTransaction.getTransactionHash(),
                hexValue, blockNumber);
    } else {
        return null;
    }
}
 
Example 22
@Override
protected RawTransactionResponse doInBackground(Void... params) {
    BigInteger nonce = getNonce();
    RawTransaction rawTransaction;
    String blockNumber = getBlocNumber();
    byte[] signedMessage;
    if (TextUtils.isEmpty(contractData)) {
        rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, value);
    } else {
        rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, new String(Hex.encode(contractData.getBytes())));
    }



    if (walletManager.getCredentials() != null && rawTransaction != null) {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, walletManager.getCredentials());
    } else {
        return null;
    }

    String hexValue = Numeric.toHexString(signedMessage);
    EthSendTransaction ethSendTransaction = null;


    try {
        ethSendTransaction = web3jConnection.ethSendRawTransaction(hexValue).send();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (ethSendTransaction != null && ethSendTransaction.getTransactionHash() != null) {
        return new RawTransactionResponse(ethSendTransaction.getTransactionHash(),
                hexValue, blockNumber);
    } else {
        return null;
    }
}
 
Example 23
Source Project: alpha-wallet-android   Source File: TransactionRepository.java    License: MIT License 5 votes vote down vote up
private Single<byte[]> signEncodeRawTransaction(RawTransaction rtx, Wallet wallet, int chainId)
{
	return Single.fromCallable(() -> TransactionEncoder.encode(rtx))
			.flatMap(encoded -> accountKeystoreService.signTransaction(wallet, encoded, chainId))
			.flatMap(signatureReturn -> {
					 	if (signatureReturn.sigType != SignatureReturnType.SIGNATURE_GENERATED)
						{
							throw new Exception(signatureReturn.failMessage);
						}
					 	return encodeTransaction(signatureReturn.signature, rtx);
					 });
}
 
Example 24
Source Project: jbpm-work-items   Source File: EthereumUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String deployContract(Credentials credentials,
                                    Web3j web3j,
                                    String contractBinary,
                                    int toSendEther,
                                    boolean waitForReceipt,
                                    int sleepDuration,
                                    int attempts) throws Exception {

    BigInteger depositEtherAmountToSend = BigInteger.valueOf(toSendEther);

    RawTransaction rawTransaction = RawTransaction.createContractTransaction(
            getNextNonce(credentials.getAddress(),
                         web3j),
            DEFAULT_GAS_PRICE,
            DEFAULT_GAS_LIMIT,
            depositEtherAmountToSend,
            contractBinary);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction,
                                                          credentials);

    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();

    if (waitForReceipt) {
        TransactionReceipt transReceipt = waitForTransactionReceipt(
                ethSendTransaction.getTransactionHash(),
                sleepDuration,
                attempts,
                web3j
        );
        if (transReceipt != null) {
            return transReceipt.getContractAddress();
        }
    }
    // we dont have a contract address
    logger.warn("Unable to retrieve contract address.");
    return null;
}
 
Example 25
Source Project: web3j   Source File: HumanStandardTokenIT.java    License: Apache License 2.0 5 votes vote down vote up
private String sendCreateContractTransaction(Credentials credentials, BigInteger initialSupply)
        throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedConstructor =
            FunctionEncoder.encodeConstructor(
                    Arrays.asList(
                            new Uint256(initialSupply),
                            new Utf8String("web3j tokens"),
                            new Uint8(BigInteger.TEN),
                            new Utf8String("w3j$")));

    RawTransaction rawTransaction =
            RawTransaction.createContractTransaction(
                    nonce,
                    GAS_PRICE,
                    GAS_LIMIT,
                    BigInteger.ZERO,
                    getHumanStandardTokenBinary() + encodedConstructor);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example 26
Source Project: web3j   Source File: PrivateTransactionEncoder.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] signMessage(
        final RawPrivateTransaction rawTransaction,
        final long chainId,
        final Credentials credentials) {
    final byte[] encodedTransaction = encode(rawTransaction, chainId);
    final Sign.SignatureData signatureData =
            Sign.signMessage(encodedTransaction, credentials.getEcKeyPair());

    final Sign.SignatureData eip155SignatureData =
            TransactionEncoder.createEip155SignatureData(signatureData, chainId);
    return encode(rawTransaction, eip155SignatureData);
}
 
Example 27
Source Project: web3j   Source File: RawTransactionManager.java    License: Apache License 2.0 5 votes vote down vote up
public String sign(RawTransaction rawTransaction) {

        byte[] signedMessage;

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

        return Numeric.toHexString(signedMessage);
    }
 
Example 28
Source Project: ethereum-paper-wallet   Source File: PaperWallet.java    License: Apache License 2.0 5 votes vote down vote up
public String createOfflineTx(String toAddress, BigInteger gasPrice, BigInteger gasLimit, BigInteger amount, BigInteger nonce) {
	RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, amount);
	byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
	String hexValue = Numeric.toHexString(signedMessage);
	
	return hexValue;
}
 
Example 29
Source Project: besu   Source File: PrivacyClusterAcceptanceTest.java    License: 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 30
public Single<String>  createERC20Transfer(ETHWallet from,  String to, String contractAddress, BigInteger amount, BigInteger gasPrice, BigInteger gasLimit, String password) {
    final Web3j web3j = Web3j.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl));

    String callFuncData = TokenRepository.createTokenTransferData(to, amount);


    return networkRepository.getLastTransactionNonce(web3j, from.address)
            .flatMap(nonce -> Single.fromCallable( () -> {

        Credentials credentials = WalletUtils.loadCredentials(password,  from.getKeystorePath());
        RawTransaction rawTransaction = RawTransaction.createTransaction(
                nonce, gasPrice, gasLimit, contractAddress, callFuncData);

        LogUtils.d("rawTransaction:" + rawTransaction);

        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);

        String hexValue = Numeric.toHexString(signedMessage);
        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();

        return ethSendTransaction.getTransactionHash();

    } ).subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread()));
}