Java如何调用以太坊账户

启动私链

相关内容见之前笔记记录

导入Jar包

  1. Java调用以太坊账户需要依赖web3j

    导入方式一:Maven

    	<dependency>
          <groupId>org.web3jgroupId>
          <artifactId>coreartifactId>
          <version>4.2.0version>
        dependency>
    

    导入方式二:下载jar包添加到项目library中

  2. 导入日志包(同上)

        <dependency>
          <groupId>log4jgroupId>
          <artifactId>log4jartifactId>
          <version>1.2.17version>
          <scope>compilescope>
        dependency>
        <dependency>
          <groupId>org.slf4jgroupId>
          <artifactId>slf4j-apiartifactId>
          <version>1.7.18version>
          <scope>compilescope>
        dependency>
        <dependency>
          <groupId>org.slf4jgroupId>
          <artifactId>slf4j-log4j12artifactId>
          <version>1.7.18version>
          <scope>compilescope>
        dependency>
    

    否则运行可能会出现如下情况:

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

编写代码

import java.math.BigDecimal;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

import com.mao.utils.Consts;

public class MainTest {

	public static void main(String[] argv) throws Exception {
		// 1.连接到以太坊网络上节点
		// 默认参数为http://localhost:8545
		// 下面两个语句等同
		// Web3j web3j = Web3j.build(new HttpService());
		Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
		// 2.加载以太坊钱包 参数说明: 密码 账户文件路径
		// loadCredentials(String password, String source)
		Credentials credentials = WalletUtils.loadCredentials(Consts.PASSWORD, Consts.PATH);
		// 3.创建交易 参数列表: 网络 钱包 接收方地址 数值 单位
		// sendFunds(Web3j web3j, Credentials credentials, String toAddress, BigDecimal
		// value, Unit unit)
		TransactionReceipt transferReceipt = Transfer.sendFunds(web3j, credentials,
				"0x118C56BAADdC4DA63aDD3B4bcf241681608ead71", BigDecimal.ONE, Convert.Unit.WEI).send();
		// 4.输出交易的Hash值
		System.out.println(transferReceipt.getTransactionHash());
	}
}

钱包加载说明:

password: 创建以太坊账户时设置的密码

source: 账户存储路径

例如:E:/BlockChain/node/data/keystore/UTC--2020-03-07T04-19-25.882222900Z--05a264bc64d6dd81492b1f5a4c93240d6ab15164

Java如何调用以太坊账户_第1张图片
Java如何调用以太坊账户_第2张图片

运行程序

  1. 运行程序

    可以发现控制台处于阻塞状态

    Java如何调用以太坊账户_第3张图片

  2. 命令行的变化

    可以发现提交了一个交易( transaction)

    Java如何调用以太坊账户_第4张图片

  3. txpool.status命令可以看到本地交易池中有一个待确认的交易

    > txpool.status
    {
      pending: 1,
      queued: 0
    }
    >
    
  4. 可以使用eth.getBlock("pending", true).transactions查看当前待确认交易

    > eth.getBlock("pending", true).transactions
    [{
        blockHash: "0x05a1680dd4304acb5d5df4de5c6963fe8449764afee5bfe452a309180ae66234",
        blockNumber: 172,
        from: "0x05a264bc64d6dd81492b1f5a4c93240d6ab15164",
        gas: 21000,
        gasPrice: 1000000000,
        hash: "0xf841f29ff171685e36b527f039e09ca65282b643e5ce92e678449ef1d24ab815",
        input: "0x",
        nonce: 11,
        r: "0x629941aa58684b4dab5ef5f233bbc5c9360facf6cad615fbdaedf0d22493e0c7",
        s: "0x2892bd6e78f4f23db4f6a7e52eb1b8f186224fa7944c45543d41bf391b7d3bce",
        to: "0x118c56baaddc4da63add3b4bcf241681608ead71",
        transactionIndex: 0,
        v: "0x1c",
        value: 1
    }]
    >
    
  5. 启动挖矿

    miner.start(1)

  6. 控制台输出

    Java如何调用以太坊账户_第5张图片

    注意:当600秒内没有挖矿,将记录写入区块链,则会抛出异常

    Exception in thread "main" org.web3j.protocol.exceptions.TransactionException: Transaction receipt was not generated after 600 seconds for transaction: 0xf841f29ff171685e36b527f039e09ca65282b643e5ce92e678449ef1d24ab815
    at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:51)
    at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:29)
    at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:72)
    at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51)
    at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:87)
    at org.web3j.tx.Transfer.send(Transfer.java:65)
    at org.web3j.tx.Transfer.send(Transfer.java:49)
    at org.web3j.tx.Transfer.lambda$sendFunds$0(Transfer.java:76)
    at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
    at com.mao.main.MainTest.main(MainTest.java:28)
    

你可能感兴趣的:(以太坊)