通过以太坊ethereum客户端进行认证签名交易

优质
小牛编辑
112浏览
2023-12-01

通过以太坊ethereum客户端进行认证签名交易

为了通过以太坊客户端进行交易,首先需要确保你正在使用的客户端知道你的钱包地址。最好是运行自己的以太坊客户端,比如geth/Parity,以便可以更方便的做到这一点。一旦你有一个客户端运行,你可以创建一个以太坊钱包,通过:

  • geth Wiki包含了geth支持的良好运行的不同机制,例如导入私有密钥文件,并通过控制台创建新的以太坊帐户。
  • 或者,你可以通过客户端使用JSON-RPC管理命令,例如用personal_newAccountgeth/Parity创建新以太坊账户。

通过创建你的钱包文件,你可以通过web3j打开帐户,首先创建支持geth/Parity管理命令的web3j实例:

Admin web3j = Admin.build(new HttpService());

然后,你可以解锁帐户,并如果是成功的,就可以发送一个交易:

PersonalUnlockAccount personalUnlockAccount = web3j.personalUnlockAccount("0x000...", "a password").send();
if (personalUnlockAccount.accountUnlocked()) {
    // send a transaction
}

以这种方式发送的交易应该通过EthSendTransaction创建,使用Transaction类型:

Transaction transaction = Transaction.createContractTransaction(
              <from address>,
              <nonce>,
              BigInteger.valueOf(<gas price>),  // we use default gas limit
              "0x...<smart contract code to execute>"
      );

      org.web3j.protocol.core.methods.response.EthSendTransaction
              transactionResponse = parity.ethSendTransaction(ethSendTransaction)
              .send();

      String transactionHash = transactionResponse.getTransactionHash();

      // poll for transaction response via org.web3j.protocol.Web3j.ethGetTransactionReceipt(<txHash>)

其中nonce值获得方式,下文会提到。 有关此交易工作流的详细信息,请参阅 DeployContractITScenario

web3j支持的各种管理命令的进一步细节在Management APIs中。