0

EDIT: I am using android platform

I think I'm a bit confused conceptually. I would appreciate it if someone could answer these few questions for me as I'm trying to make an app so that I can be clear on exactly what's going on here. I'm trying to read and decipher the documentation at the same time.

-I have generated my address and private key using web3j ECKeyPair class

Now I need to be able to send that ether to an address using that data and I'm not sure how I'm supposed to go about doing it

  • I think I'm supposed to use something called a rawtransaction but again my understanding is hazy

  • I would also like to be able to encrypt my private key however I saw that it encrypts the data into a keystore file. Is there any way to clear out the unneccesary data here as I'm only trying to encrypt the private key using a password.

If a more knowledgeable member than myself could clear up the steps involved in "signing a transaction" so that ether can be sent from one address to another, you'd be a huge help to my design project.

I also don't understand how transactions can be signed offline as I thought the point in signing a transaction was so it could be verified by the network

Thank you.

2 Answers 2

1

You basically need to to three steps:

  1. Construct transaction object
  2. Sign it with private key
  3. Publish signed transaction

For step 1 you do something like this:

var from = "0x..."; // The address you are sending ether from
var tx = {
  nonce: web3.eth.getTransactionCount (from),
  chainId: 1, // 1 means Mainnet, more chain IDs here: https://chainid.network/
  to: "0x...", // The address you are sending ether to
  data: "",
  value: value, // The amount of ether to send (in Wei)
  gasPrice: web3.eth.getGasPrice(),
  gas: 21000 // Enough for simple transfer
};

For step 2 you do:

var signedTx = web3.eth.accounts.signTransaction (tx, privateKey);

For step 3 you do:

web3.eth.sendSignedTransaction(signedTx);

See Web3js documentation for more info.

Sorry, my answer is about Web3js, not Web3j. But, for Web3j steps are basically the same:

  1. Prepare transaction object (RawTransaction)
  2. Sign it (TransactionEncoder)
  3. Publish it (ethSendRawTransaction)

See official documentation for details.

2

If i understand your question correctly, you should be able to just use web3.eth.sendTransaction by passing in a transaction object.

var transactionObject = {
    from: web3.defaultAccount,
    to: addressToSendTo,
    value: web3.toWei(1.0, "ether"),
    gas: gasAmountInWei
};

web3.eth.sendTransaction(transactionObject, function(err, data) {
    // if no err, transaction was success
});

For the gas value, you can estimate the gas needed like this

// note that `transactionObject` is the same as above but without the gas value
web3.eth.estimateGas(transactionObject, function(err, estimate) {
    transactionObject.gas = estimate;
});

If you have issues with gas not being enough, experiment with adding more. I generally multiply by 2, since it only uses what is needed and not the entire amount (since it is supposed to just be an estimate).

1
  • I am working on the android platform and java. My apologies for not stating that above
    – Stephen
    Mar 17, 2018 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.