I launch the ethereum blockchain by running testrpc
. That is creating 10 test accounts with sufficient funds.
When I use web3 with node.js to to deploy a new smart contract, I can use:
MyContract.new(['MyProps'],{data: byteCode, from: web3.eth.accounts[0], gas: 4800000})
When I use web3 with Java, I need a Credentials object. The following line seems not to work, because the deployment returns that the account has not sufficient funds.
String account = web3.ethAccounts().send().getAccounts().get(0);
Credentials c = Credentials.create(account);
Contract_sol_Contract contract = Contract_sol_Contract.deploy(web3, c, new BigInteger("240000"),new BigInteger("2400000"), props).send();
-->sender doesn't have enough funds to send tx. The upfront cost is: 576 and the sender's account only has: 0
Obviously, the Credentials object is not correct. As far as I understand, the private key must be passed and I used the public address.
- How can I access the private address in Java web3j?
- Why is it sufficient in web3 (node.js) to use the public address for the deployment of the contract?
Thanks in advance,
Marco
Best Answer
Here is a more complete program than the one you provided. The only undefined part is
Contract_sol_Contract
, which you did not define. Notice howprivateKey
andpublicKey
are computed.I think that the reason that
web3j
deviated fromweb3
in requiring just the public or private key is because providing less information is a better security practice. The web3 accounts docs has this warning:The accounts private key. This should never be shared or stored unencrypted in localstorage! Also make sure to null the memory after usage.
This means passing around the entire accounts object is discouraged.Seems the
web3
authors knew they created a security issue because earlier on that same page they give this big fat warning:This package has NOT been audited and might potentially be unsafe. Take precautions to clear memory properly, store the private keys safely, and test transaction receiving and sending functionality properly before using in production!
.