以太坊Web3J插件功能探究

1  Java Web3J 概述java

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

1.1 Web3J 提供的功能编程

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

  • Ethereum钱包支持安全

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

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

  • 支持Parity的 我的和Geth的 我的客户端APIsocket

  • 支持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

相关文章
相关标签/搜索