org.web3j.crypto.RawTransaction Java Examples

The following examples show how to use org.web3j.crypto.RawTransaction. 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: CreateTransactionInteract.java    From Upchain-wallet with GNU Affero General Public License v3.0 7 votes vote down vote up
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 #2
Source File: RawTransactionManager.java    From client-sdk-java with 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 #3
Source File: HumanStandardTokenIT.java    From web3j with 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 #4
Source File: SignTransactionIT.java    From etherscan-explorer with GNU General Public License v3.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 #5
Source File: CreateRawTransactionIT.java    From web3j with 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 #6
Source File: CreateRawTransactionIT.java    From web3j with 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 #7
Source File: CreateRawTransactionIT.java    From etherscan-explorer with GNU General Public License v3.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);

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

    assertFalse("Contract execution ran out of gas",
            rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()));
}
 
Example #8
Source File: CreateRawTransactionIT.java    From etherscan-explorer with GNU General Public License v3.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);

    assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
}
 
Example #9
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.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 File: SignTransactionIT.java    From web3j with 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 #11
Source File: RawTransactionManager.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public EthSendTransaction sendTransaction(
        BigInteger gasPrice, BigInteger gasLimit, String to,
        String data, BigInteger value) throws IOException {

    BigInteger nonce = getNonce();

    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            gasPrice,
            gasLimit,
            to,
            value,
            data);

    return signAndSend(rawTransaction);
}
 
Example #12
Source File: RawTransactionManager.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public PlatonSendTransaction sendTransaction(
        BigInteger gasPrice, BigInteger gasLimit, String to,
        String data, BigInteger value) throws IOException {

    BigInteger nonce = getNonce();

    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            gasPrice,
            gasLimit,
            to,
            value,
            data);

    return signAndSend(rawTransaction);
}
 
Example #13
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 #14
Source File: RawTransactionManager.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Override
public EthSendTransaction sendTransaction(
        BigInteger gasPrice,
        BigInteger gasLimit,
        String to,
        String data,
        BigInteger value,
        boolean constructor)
        throws IOException {

    BigInteger nonce = getNonce();

    RawTransaction rawTransaction =
            RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data);

    return signAndSend(rawTransaction);
}
 
Example #15
Source File: RawTransactionManager.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Override
public EthSendTransaction sendTransactionEIP1559(
        BigInteger gasPremium,
        BigInteger feeCap,
        BigInteger gasLimit,
        String to,
        String data,
        BigInteger value,
        boolean constructor)
        throws IOException {

    BigInteger nonce = getNonce();

    RawTransaction rawTransaction =
            RawTransaction.createTransaction(
                    nonce, null, gasLimit, to, value, data, gasPremium, feeCap);

    return signAndSend(rawTransaction);
}
 
Example #16
Source File: SendingEther.java    From Android-Wallet-Token-ERC20 with 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 #17
Source File: TransactionRepository.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private Single<byte[]> encodeTransaction(byte[] signatureBytes, RawTransaction rtx)
{
	return Single.fromCallable(() -> {
		Sign.SignatureData sigData = sigFromByteArray(signatureBytes);
		if (sigData == null) return FAILED_SIGNATURE.getBytes();
		return encode(rtx, sigData);
	});
}
 
Example #18
Source File: HumanStandardTokenIT.java    From web3j with 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 #19
Source File: EthereumUtils.java    From jbpm-work-items with 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 #20
Source File: EthTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
private RawTransaction createTransaction() {
  return RawTransaction.createTransaction(
      nonce,
      transactionJsonParameters.gasPrice().orElse(DEFAULT_GAS_PRICE),
      transactionJsonParameters.gas().orElse(DEFAULT_GAS),
      transactionJsonParameters.receiver().orElse(DEFAULT_TO),
      transactionJsonParameters.value().orElse(DEFAULT_VALUE),
      transactionJsonParameters.data().orElse(DEFAULT_DATA));
}
 
Example #21
Source File: SignTransactionIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static RawTransaction createTransaction() {
    BigInteger value = Convert.toWei("1", Convert.Unit.ETHER).toBigInteger();

    return RawTransaction.createEtherTransaction(
            BigInteger.valueOf(1048587),
            BigInteger.valueOf(500000),
            BigInteger.valueOf(500000),
            "0x9C98E381Edc5Fe1Ac514935F3Cc3eDAA764cf004",
            value);
}
 
Example #22
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 Base64String privacyGroupId,
        final Restriction restriction) {
    this(rawTransaction, privateFrom, null, privacyGroupId, restriction);
}
 
Example #23
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 #24
Source File: RawTransactionManager.java    From web3j with 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 #25
Source File: RawTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
public EthSendTransaction signAndSend(RawTransaction rawTransaction) throws IOException {
    String hexValue = sign(rawTransaction);
    EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(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 #26
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.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 #27
Source File: EthTransaction.java    From ethsigner with 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 #28
Source File: TransferTransaction.java    From besu with 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 #29
Source File: CreateTransactionInteract.java    From Upchain-wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
private Single<RawTransaction> getRawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, BigInteger value, String data)
{
    return Single.fromCallable(() ->
            RawTransaction.createContractTransaction(
                    nonce,
                    gasPrice,
                    gasLimit,
                    value,
                    data));
}
 
Example #30
Source File: CreateTransactionInteract.java    From Upchain-wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
private Single<RawTransaction> getRawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to , BigInteger value, String data)
{
    return Single.fromCallable(() ->
            RawTransaction.createTransaction(
                    nonce,
                    gasPrice,
                    gasLimit,
                    to,
                    value,
                    data));
}