首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web3j测试帐户和凭据对象

Web3j测试帐户和凭据对象
EN

Ethereum用户
提问于 2017-12-13 11:01:51
回答 2查看 2.5K关注 0票数 1

我通过运行testrpc启动ethereum区块链。这意味着用足够的资金创建10个测试帐户。当我使用web3和node.js部署一个新的智能契约时,我可以使用:

代码语言:javascript
复制
MyContract.new(['MyProps'],{data: byteCode, from: web3.eth.accounts[0], gas: 4800000})

当我在Java中使用web3时,我需要一个凭据对象。下面的行似乎不起作用,因为部署返回帐户没有足够的资金。

代码语言:javascript
复制
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

显然,凭据对象是不正确的。据我所知,私钥必须通过,我用的是公共广播。

  1. 如何访问Java web3j中的私有地址?
  2. 为什么在web3 (node.js)中使用公共地址来部署合同就足够了?

提前谢谢你,马可

EN

回答 2

Ethereum用户

发布于 2017-12-16 09:17:25

这是一个比你提供的更完整的程序。唯一未定义的部分是Contract_sol_Contract,您没有定义它。注意如何计算privateKeypublicKey

代码语言:javascript
复制
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!

票数 1
EN

Ethereum用户

发布于 2017-12-18 15:51:01

非常感谢。我通过从testrpc命令的终端粘贴私钥来管理它。

当我在契约上调用一个方法时,结果是一个TransactionReceipt

代码语言:javascript
复制
TransactionReceipt tx = contract.currentAnswerCount(items.get(0)).send();

实际上,返回值应该是uint。我怎么才能得到那个值。TransactionReceipt没有类似于getResult()或类似的东西。

以下是适当的稳固代码:

代码语言:javascript
复制
  function currentAnswersCount(bytes32 item) public returns (uint8) {
    return answers[item];
  }
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/33272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档