如何从智能合约中生成Java Wrapper

在本文中,我们将了解如何直接从智能合约生成java wrapper类以与java中的智能合约进行交互。
从智能合约生成java wrapper类有不同的方法:
1. web3j命令行工具和solc
2. web3j命令行工具和truffle构建生成的工件
3. web3j-maven-plugin
4. web3j-gradle-plugin
为了演示如何使用上述方法,本教程使用以下智能合约将文档公证到以太坊区块链上的注册表中。
documentregistry.sol
pragma solidity ^0.5.6;
/**
* @dev smart contract responsible to notarize documents on the ethereum blockchain
*/
contract documentregistry {
struct document {
address signer; // notary
uint date; // date of notarization
bytes32 hash; // document hash
}
/**
* @dev storage space used to record all documents notarized with metadata
*/
mapping(bytes32 =》 document) registry;
/**
* @dev notarize a document identified by its 32 bytes hash by recording the hash, the sender and date in the registry
* @dev emit an event notarized in case of success
* @param _documenthash document hash
*/
function notarizedocument(bytes32 _documenthash) external returns (bool) {
registry[_documenthash].signer = msg.sender;
registry[_documenthash].date = now;
registry[_documenthash].hash = _documenthash;
emit notarized(msg.sender, _documenthash);
return true;
}
/**
* @dev verify a document identified by its hash was noterized in the registry.
* @param _documenthash document hash
* @return bool if document was noterized previsouly in the registry
*/
function isnotarized(bytes32 _documenthash) external view returns (bool) {
return registry[_documenthash].hash == _documenthash;
}
/**
* @dev definition of the event triggered when a document is successfully notarized in the registry
*/
event notarized(address indexed _signer, bytes32 _documenthash);
}
web3j命令行工具和solc
第一种方法使用solc生成smart合约abi和bytecode,并将这两个文件作为输入提供给web3j-cli以生成wrapper。
1、安装solc并验证版本
安装solc并运行以下命令以确保solc版本大于或等于0.5.6(智能合约中指定的版本)。
$ solc --version
solc, the solidity compiler commandline interface
version: 0.5.9+commit.c68bc34e.linux.g++
2、安装web3j cli
要安装web3j cli,请从项目存储库的“发布”页面的“下载”部分下下载zipfile/tarball,或通过homebrew为macos用户或通过aur为arch linux用户下载zipfile/tarball。
brew tap web3j/web3j
brew install web3j
web3j
要通过zipfile运行,解压缩并运行二进制文件,您可能还需要将二进制文件添加到path中:
$ unzip web3j-4.3.0.zip
creating: web3j-4.3.0/lib/
inflating: web3j-4.3.0/lib/core-1.0.2-all.jar
creating: web3j-4.3.0/bin/
inflating: web3j-4.3.0/bin/web3j
inflating: web3j-4.3.0/bin/web3j.bat
$ 。/web3j-《version》/bin/web3j
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
/ / / _ ‘_ | | | / _
v v / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
usage: web3j version|wallet|solidity 。..
3、使用solc编译智能合约
给定我们的solidity文件documentregistry.sol,solc 《sol》 --bin --abi --optimize -o 《output》命令编译智能合约并在同一目录中生成两个新文件:
$ solc documentregistry.sol --bin --abi --optimize -o 。/
compiler run successful. artifact(s) can be found in directory 。/.
$ ls -l
total 12
-rw-rw-r-- 1 gjeanmart gjeanmart 565 jun 24 13:42 documentregistry.abi
-rw-rw-r-- 1 gjeanmart gjeanmart 676 jun 24 13:42 documentregistry.bin
-rw-rw-r-- 1 gjeanmart gjeanmart 1488 jun 24 13:40 documentregistry.sol
documentregistry.bin:二进制文件,智能合约的字节码
documentregistry.abi:智能合约的abi(应用程序二进制接口),它以json格式定义接口。
4、使用web3j-cli生成包装器
使用abi和bytecode(在步骤3中生成)和web3j-cli(在步骤2中安装),我们现在可以使用以下命令生成我们的智能合约的java wrapper:
web3j solidity generate -a=《abifile》 -b=《binfile》 -o=《destinationfiledir》 -p=《packagename》
示例:
$ web3j solidity generate -a documentregistry.abi
-b documentregistry.bin -o 。
-p me.gjeanmart.tutorials.javaethereum.wrapper
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
/ / / _ ’_ | | | / _
v v / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
generating me.gjeanmart.tutorials.javaethereum.wrapper.documentregistry 。.. file written to 。
因此,您应该看到生成到文件夹/.java中的java wrapper文件,您可以将其复制到项目的src / main / java /文件夹中。
。/me/gjeanmart/tutorials/javaethereum/wrapper/documentregistry.java
web3j命令行工具和truffle artefacts
truffle是最著名的框架之一,可帮助您使用以太坊开发、测试和部署。 我们可以使用truffle使用web3j命令行工具生成的artefacts来创建wrapper类。
1、安装truffle
truffle可作为npm wrapper提供。
$ npm install truffle -g
- fetching solc version list from solc-bin. attempt #1
+ truffle@5.0.24
added 27 packages from 439 contributors in 11.636s
$ truffle version
truffle v5.0.24 (core: 5.0.24)
solidity v0.5.0 (solc-js)
node v10.15.3
web3.js v1.0.0-beta.37
2、初始化新的truffle项目
要初始化truffle项目,请在新文件夹中使用truffle init命令。该命令创建文件夹contract /,migration /和test /,以及文件truffle-config.js。
$ mkdir truffle
$ cd truffle
$ truffle init
? preparing to download
? downloading
? cleaning up temporary files
? setting up box
unbox successful. sweet!
commands:
compile: truffle compile
migrate: truffle migrate
test contracts: truffle test
$ ls -l
total 20
drwxrwxr-x 2 gjeanmart gjeanmart 4096 jun 24 14:25 contracts
drwxrwxr-x 2 gjeanmart gjeanmart 4096 jun 24 14:25 migrations
drwxrwxr-x 2 gjeanmart gjeanmart 4096 jun 24 14:25 test
-rw-rw-r-- 1 gjeanmart gjeanmart 4233 jun 24 14:25 truffle-config.js
3、将合同添加到文件夹合约中
将智能合约源documentregistry.sol复制到文件夹contracts中。
4、编译合约
使用命令truffle compile编译智能合约,此命令生成一个新的文件夹build/contracts/,其中包含每个编译的智能合约的truffle artefact。
$ truffle compile
compiling your contracts.。.
===========================
》 compiling 。/contracts/documentregistry.sol
》 compiling 。/contracts/migrations.sol
》 artifacts written to /home/gjeanmart/workspace/tutorials/java-ethereum-wrapper/truffle/build/contracts
》 compiled successfully using:
- solc: 0.5.8+commit.23d335f2.emscripten.clang
$ ls -l build/contracts/
total 136
-rw-rw-r-- 1 gjeanmart gjeanmart 79721 jun 24 14:33 documentregistry.json
-rw-rw-r-- 1 gjeanmart gjeanmart 54043 jun 24 14:33 migrations.json
5、从truffle artefact生成智能合约java wrapper
最后,web3j-cli提供了一种方法,可以使用以下命令直接从truffle编译的truffle artefact结果生成wrapper:
$ web3j truffle generate 。/build/contracts/documentregistry.json -o 。 -p me.gjeanmart.tutorials.javaethereum.wrapper
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
/ / / _ ‘_ | | | / _
v v / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
generating me.gjeanmart.tutorials.javaethereum.wrapper.documentregistry 。.. file written to 。
因此,您应该看到生成到《packagefolders》 /。java_文件夹中的java wrapper文件,您可以将其复制到项目的src / main / java /文件夹中。
。/me/gjeanmart/tutorials/javaethereum/wrapper/documentregistry.java
注意:使用truffle,您可以做的比本文中显示的更多,例如部署脚本(迁移)、多网络配置、测试、调试。
web3j-maven-plugin
下一个解决方案比前两个解决方案更优雅,因为我们不必安装webj-cli并将文件复制到源文件夹。我们可以使用maven和web3j-maven-plugin直接在java项目中使用此方法。以下步骤假定您已创建maven项目。
1、先决条件
安装solc并运行以下命令以确保solc版本大于或等于0.5.6(智能合约中指定的版本)。
$ solc --version
solc, the solidity compiler commandline interface
version: 0.5.9+commit.c68bc34e.linux.g++
2、将智能合约复制到文件夹src / main / resources中
将smart contract源documentregistry.sol复制到maven项目的src / main / resources文件夹中。
3、配置maven以在generate-sources阶段生成wrapper
在此步骤中,我们配置两个maven插件:
web3j - maven的插件
第一个插件与前两个方法相同,但与maven集成。首先,我们将插件配置为在进入项目的generate-sources阶段时自动执行。
其次我们配置插件参数:
· packagename:要应用于生成的wrapper类的包名称
· sourcedestination:目标文件夹,用于移动生成的wrapper类
· soliditysourcefiles:在何处查找smart contract源文件
建立辅助性maven的插件
第二个插件将sourcedestination文件夹添加到类路径中,以便我们可以导入生成的wrapper类
pom.xml
《build》
《plugins》
《plugin》
《groupid》org.web3j《/groupid》
《artifactid》web3j-maven-plugin《/artifactid》
《version》4.2.0《/version》
《executions》
《execution》
《id》generate-sources-web3j《/id》
《phase》generate-sources《/phase》
《goals》
《goal》generate-sources《/goal》
《/goals》
《configuration》
《packagename》me.gjeanmart.tutorials.javaethereum.contracts.generated《/packagename》
《sourcedestination》${basedir}/target/generated-sources/contracts《/sourcedestination》
《soliditysourcefiles》
《directory》${basedir}/src/main/resources《/directory》
《includes》
《include》**/*.sol《/include》
《/includes》
《/soliditysourcefiles》
《/configuration》
《/execution》
《/executions》
《/plugin》
《plugin》
《groupid》org.codehaus.mojo《/groupid》
《artifactid》build-helper-maven-plugin《/artifactid》
《executions》
《execution》
《id》add-source《/id》
《phase》generate-sources《/phase》
《goals》
《goal》add-source《/goal》
《/goals》
《configuration》
《sources》
《source》${basedir}/target/generated-sources/contracts《/source》
《/sources》
《/configuration》
《/execution》
《/executions》
《/plugin》
《/plugins》
《/build》
4、运行maven生成源
最后,使用mvn clean package(包括generate-sources阶段)构建maven项目。因此,我们可以看到java wrapper已生成到/target/generated-sources/contracts/me/gjeanmart/tutorials/javaethereum/contracts/generated/documentregistry.java并自动添加到类路径中。
web3j gradle插件
最后一个方法与之前使用maven的方法类似,但使用的是gradle。
1、先决条件
安装solc并运行以下命令以确保solc版本大于或等于0.5.6(智能合约中指定的版本)。
$ solc --version
solc, the solidity compiler commandline interface
version: 0.5.9+commit.c68bc34e.linux.g++
2、将智能合约放入文件夹src / main / solidity
将smart contract源documentregistry.sol复制到gradle项目的src / main / solidity文件夹中。
3、配置gradle以在构建期间生成wrapper首先将web3j-gradle插件导入build.gradle文件。
plugins {
id ’org.web3j‘ version ’4.3.0‘
}
然后我们可以配置插件来为生成的wrapper类指定包名称和目标文件夹:
web3j {
generatedpackagename = ’me.gjeanmart.tutorials.javaethereum.contracts.generated‘
generatedfilesbasedir = “$builddir/contracts”
}
要使用系统安装的solc版本而不是与插件捆绑的版本,请将以下行添加到build.gradle:
solidity {
executable = “solc”
}
build.gradle
/*
* this file was generated by the gradle ’init‘ task.
*
* this generated file contains a sample java library project to get you started.
* for more details take a look at the java libraries chapter in the gradle
* user guide available at https://docs.gradle.org/5.0/userguide/java_library_plugin.html
*/
plugins {
// apply the java-library plugin to add support for java library
id ’java-library‘
id ’org.web3j‘ version ’4.3.0‘
}
repositories {
// use jcenter for resolving your dependencies.
// you can declare any maven/ivy/file repository here.
jcenter()
}
dependencies {
// this dependency is exported to consumers, that is to say found on their compile classpath.
api ’org.apache.commons:commons-math3:3.6.1‘
// this dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation ’com.google.guava:guava:26.0-jre‘
implementation ’org.web3j:core:4.3.0‘
// use junit test framework
testimplementation ’junit:junit:4.12‘
}
web3j {
generatedpackagename = ’me.gjeanmart.tutorials.javaethereum.contracts.generated‘
generatedfilesbasedir = “$builddir/contracts”
}
solidity {
executable = “solc”
}
4、执行gradle构建
在最后一步中,我们使用。/gradlew tasks执行构建--all并验证我们生成的wrapper类是否已生成。

显示器处理器芯片是什么?显示器处理器芯片市场的发展趋势
斯坦福学者提出GIoU,目标检测任务的新Loss
TQC粘度杯VF2029/VF2030/VF2031/VF2032的介绍
5G时代会让微软成功进入电信消费市场吗?
[组图]我做高频用的电子元件
如何从智能合约中生成Java Wrapper
双轨奖金制度自动计算直销软件系统
WM8310/WM8311/WM8312 欧胜微电子推出的电
水分损失测量仪的最小潮气量是多少
浅谈SEW存储卡的数据保存及备份注意事项
单片机电源供电电路设计方案
SpaceX将让月球旅行成真?已签下首位私人乘客
惠东江波汽车音响改装为您找到中国好声音
VR是如何让人产生身临其境的感觉
华尔思与思必驰签署战略合作协议,开展AI技术战略合作
壁挂式流量计
农药残留检测仪器设备的产品介绍说明
华为MateBook 14笔记本,你的贴心旅途伴侣
嵌入式开发板的功能及选择
光学封装公司Fabrine最近一个财季业绩超出预期