我通过运行testrpc启动ethereum区块链。这意味着用足够的资金创建10个测试帐户。当我使用web3和node.js部署一个新的智能契约时,我可以使用:
MyContract.new(['MyProps'],{data: byteCode, from: web3.eth.accounts[0], gas: 4800000})当我在Java中使用web3时,我需要一个凭据对象。下面的行似乎不起作用,因为部署返回帐户没有足够的资金。
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显然,凭据对象是不正确的。据我所知,私钥必须通过,我用的是公共广播。
提前谢谢你,马可
发布于 2017-12-16 09:17:25
这是一个比你提供的更完整的程序。唯一未定义的部分是Contract_sol_Contract,您没有定义它。注意如何计算privateKey和publicKey。
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
public class SO3 {
public SO3() throws IOException {
Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
String account = web3.ethAccounts().send().getAccounts().get(0);
Credentials credentials = Credentials.create(account);
ECKeyPair keyPair = credentials.getEcKeyPair();
BigInteger privateKey = keyPair.getPrivateKey();
BigInteger publicKey = keyPair.getPublicKey();
Contract_sol_Contract contract = Contract_sol_Contract.deploy(web3, credentials, new BigInteger("240000"), new BigInteger("2400000"), props).send();
}
}我认为web3j在只要求公钥或私钥方面偏离了web3的原因是因为提供更少的信息是一种更好的安全实践。web3 3帐户文档有这样的警告:The accounts private key. This should never be shared or stored unencrypted in localstorage! Also make sure to null the memory after usage. --这意味着不鼓励传递整个accounts对象。
似乎web3的作者知道他们制造了一个安全问题,因为在同一页的早些时候,他们给出了一个很大的警告: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!。
发布于 2017-12-18 15:51:01
非常感谢。我通过从testrpc命令的终端粘贴私钥来管理它。
当我在契约上调用一个方法时,结果是一个TransactionReceipt
TransactionReceipt tx = contract.currentAnswerCount(items.get(0)).send();实际上,返回值应该是uint。我怎么才能得到那个值。TransactionReceipt没有类似于getResult()或类似的东西。
以下是适当的稳固代码:
function currentAnswersCount(bytes32 item) public returns (uint8) {
return answers[item];
}https://ethereum.stackexchange.com/questions/33272
复制相似问题