以太坊Web3J插件功能探究

1  Java Web3J 概述

Web3j是一个轻量级,Reactive(响应式),类型安全的Java库,用于与Ethereum网络上的客户端(节点)集成,这允许您使用Ethereum块链,而不需要为平台编写自己的集成代码的额外开销。

1.1 Web3J 提供的功能

  • 通过HTTP和IPC 完成Ethereum的JSON-RPC客户端API的实现

  • Ethereum钱包支持

  • 使用过滤器的函数式编程功能的API

  • 自动生成Java智能合约包装器,以创建、部署、处理和调用来自本地Java代码的智能合约

  • 支持Parity的 个人和Geth的 个人客户端API

  • 支持Infura,所以您不必自己运行一个Ethereum客户端

  • 综合整合测试展示了上述一些场景

  • 命令行工具

1.2 Web3J的依赖的库(中间件)

  • RxJava函数式编程的API中间件

  • Apache HTTP Client中间件

  • Jackson Core 用于快速JSON序列化/反序列化中间件

  • Bouncy Castle加密解密和 Java Scrypt加密中间件

  • 生成智能合约java包装器类的java源代码(.java)的JavaPoet中间件

  • Java的UNIX域套接字的*nix系统进程间通信API中间件

1.3 启动Ethereum客户端

$ geth --fast --cache = 512 –networkid 2 - -rpcapi “personal,db,eth,net,web3” --rpc --dev

1.4 Web3J的进程间通信IPC

Web3j还支持通过文件套接字快速进行进程间通信(IPC)到在与web3j相同的主机上运行的客户端。在创建服务时,连接只需使用相关的IpcService实现而不是HttpService: 

//OS X/Linux/Unix;
Web3j web3 = Web3j.build(new UnixIpcService("/path/to/socketfile"));

1.5 Web3J的过滤器

Web3j的函数式编程的特性让我们设置观察者很容易,这样通知订阅者在区块链以便知道区块链上设置的事件。

  • 1.5.1 区块过滤器

当所有新的块被添加到块链中的时候,接收到这些区块:

Subscription subscription = web3j.blockObservable(false).subscribe(block ->{});

如果您希望查看最近的当前的区块,以便于新的后续块的创建: 

Subscription subscription = catchUpToLatestAndSubscribeToNewBlocksObservable(
<startBlockNumber>,<fullTxObjects>)
.subscribe(block ->{
});

  • 1.5.2 交易过滤器

当所有新的交易被添加到块链中的时候,接收到这些交易:

Subscription subscription  = web3j.transactionObservable().subscribe(tx ->{
});

  • 1.5.3 待处理的交易过滤器

当所有待处理的交易被提交到网络时(也就是说,在它们被分组到一个块之前),接收这些交易:

Subscription subscription  = web3j.pendingTransactionObservable().subscribe(tx ->{
});

  • 1.5.4 使用Web3J交易

Web3j支持使用Ethereum钱包文件(推荐)和用于发送交易的Ethereum客户端管理命令。 使用您的Ethereum钱包文件将Ether发送到另一方:

Web3j web3 = Web3j,build(new HttpService());//defaults to http://localhost:8545/Credentials credentials = WalletUtils.loadCredentials("password","/path/to/walletfile");
TransactionReceipt transactionReceipt = Transfer.sendFunds(web3,credentials,"0×04",BigDecimal.valueOf(1.0),Convert.Unit.ETHER);
Web3j web3 = Web3j,build(new HttpService());//defaults to http://localhost:8545/Credentials credentials = WalletUtils.loadCredentials("password","/path/to/walletfile");

如果想自定义交易

 1. 获取可用的nonce

EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(address,DefaultBlockParameterName.LATEST).sendAsync().get();BigInteger nonce = ethGet TransactionCount.getTransactionCount();

 2. 创建交易

RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce,<gas price>,<gas limit>,<toAddress>,<value>);

 3. 签名并发送交易

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction,credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();

 4. 使用web3j的智能合约包装器要简单得多 

Parity parity = Parity.build(new HttpService());//dedaults to http://localhost:8545/PersonalUnlockAccount personalUnlockAccount = parity.personalUnlockAccount["0×000...","a password"].sendAsync().get();
if (personalUnlockAccount.accountUnlocked()){
parity.personalSignAndSendTransaction()
}

1.6 使用Web3J智能合约

使用Java智能合约包装器处理智能合约 web3j可以自动生成智能合约包装器代码,以便在不离开Java的情况下部署和与智能合约进行交互,生成包装代码

  • 1.6.1 编译智能合约

 $ solc .sol --bin --abi --optimize -o 

  • 1.6.2 然后使用Web3J的命令行工具生成包装器代码

  • 1.6.3 创建并部署智能合约

Web3j  web3 = web3j .build(new HttpService());
YourSmartContract contract = YourSmartContract.deploy(<web3j>,<credential>,GAS_PRICE,GAS_LIMIT,<initialEtherValue>,<paramL>,...,<paramN>).get();//构造函数参数

  • 1.6.4 使用已存在的智能合约

YourSmartContract contract = YourSmartContract.load("0×<adress>",<web3j>,<credentials>,GAS_PRICE,GAS_LIMIT)

原文链接:https://mp.weixin.qq.com/s/NOuI7ZtsD6VOupp67AEZIA

原文地址:https://www.cnblogs.com/blockchain/p/9465239.html