org.web3j.protocol.core.methods.request.RawTransaction Java Examples

The following examples show how to use org.web3j.protocol.core.methods.request.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: EthereumNetworkManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@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 #2
Source File: EthereumNetworkManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@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 #3
Source File: EthereumNetworkManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@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 #4
Source File: PaperWallet.java    From ethereum-paper-wallet with 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 #5
Source File: TransferEtherTest.java    From web3j_demo with Apache License 2.0 4 votes vote down vote up
/**
 * Ether transfer tests using methods {@link RawTransaction#createEtherTransaction()}, {@link TransactionEncoder#signMessage()} and {@link Web3j#ethSendRawTransaction()}.
 * Most complex transfer mechanism, but offers the highest flexibility.   
 */
@Test
public void testCreateSignAndSendTransaction() throws Exception {

	String from = Alice.ADDRESS;
	Credentials credentials = Alice.CREDENTIALS;
	BigInteger nonce = getNonce(from);
	String to = Bob.ADDRESS; 
	BigInteger amountWei = Convert.toWei("0.789", Convert.Unit.ETHER).toBigInteger();

	// create raw transaction
	RawTransaction txRaw = RawTransaction
			.createEtherTransaction(
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					to, 
					amountWei);

	// sign raw transaction using the sender's credentials
	byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
	String txSigned = Numeric.toHexString(txSignedBytes);

	BigInteger txFeeEstimate = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);

	// make sure sender has sufficient funds
	ensureFunds(Alice.ADDRESS, amountWei.add(txFeeEstimate));

	// record balanances before the ether transfer
	BigInteger fromBalanceBefore = getBalanceWei(Alice.ADDRESS);
	BigInteger toBalanceBefore = getBalanceWei(Bob.ADDRESS);

	// send the signed transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendRawTransaction(txSigned)
			.sendAsync()
			.get();

	Error error = ethSendTx.getError();
	assertTrue(error == null);
	
	String txHash = ethSendTx.getTransactionHash();
	assertFalse(txHash.isEmpty());
	
	TransactionReceipt txReceipt = waitForReceipt(txHash);
	BigInteger txFee = txReceipt.getCumulativeGasUsed().multiply(Web3jConstants.GAS_PRICE);

	assertEquals("Unexected balance for 'from' address", fromBalanceBefore.subtract(amountWei.add(txFee)), getBalanceWei(from));
	assertEquals("Unexected balance for 'to' address", toBalanceBefore.add(amountWei), getBalanceWei(to));
}
 
Example #6
Source File: CreateAccountTest.java    From web3j_demo with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateAccountFromScratch() throws Exception {
	
	// create new private/public key pair
	ECKeyPair keyPair = Keys.createEcKeyPair();
	
	BigInteger publicKey = keyPair.getPublicKey();
	String publicKeyHex = Numeric.toHexStringWithPrefix(publicKey);
	
	BigInteger privateKey = keyPair.getPrivateKey();
	String privateKeyHex = Numeric.toHexStringWithPrefix(privateKey);
	
	// create credentials + address from private/public key pair
	Credentials credentials = Credentials.create(new ECKeyPair(privateKey, publicKey));
	String address = credentials.getAddress();
	
	// print resulting data of new account
	System.out.println("private key: '" + privateKeyHex + "'");
	System.out.println("public key: '" + publicKeyHex + "'");
	System.out.println("address: '" + address + "'\n");
	
	// test (1) check if it's possible to transfer funds to new address
	BigInteger amountWei = Convert.toWei("0.131313", Convert.Unit.ETHER).toBigInteger();
	transferWei(getCoinbase(), address, amountWei);

	BigInteger balanceWei = getBalanceWei(address);
	BigInteger nonce = getNonce(address);
	
	assertEquals("Unexpected nonce for 'to' address", BigInteger.ZERO, nonce);
	assertEquals("Unexpected balance for 'to' address", amountWei, balanceWei);

	// test (2) funds can be transferred out of the newly created account
	BigInteger txFees = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);
	RawTransaction txRaw = RawTransaction
			.createEtherTransaction(
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					getCoinbase(), 
					amountWei.subtract(txFees));

	// sign raw transaction using the sender's credentials
	byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
	String txSigned = Numeric.toHexString(txSignedBytes);

	// send the signed transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendRawTransaction(txSigned)
			.sendAsync()
			.get();

	Error error = ethSendTx.getError();
	String txHash = ethSendTx.getTransactionHash();
	assertNull(error);		
	assertFalse(txHash.isEmpty());
	
	waitForReceipt(txHash);

	assertEquals("Unexpected nonce for 'to' address", BigInteger.ONE, getNonce(address));
	assertTrue("Balance for 'from' address too large: " + getBalanceWei(address), getBalanceWei(address).compareTo(txFees) < 0);
}