美丽说网站代码与蘑菇街网站代码是用什么网站语言做的,免费网站域名申请,wordpress文章保存图片,成都科技网站建设哪里有在本文中#xff0c;我们将了解如何直接从智能合约生成Java Wrapper类以与Java中的智能合约进行交互。从智能合约生成Java Wrapper类有不同的方法#xff1a;1. Web3j命令行工具和solc2. Web3j命令行工具和Truffle构建生成的工件3. web3j-maven-plugin4. web3j-gradle-plugin…在本文中我们将了解如何直接从智能合约生成Java Wrapper类以与Java中的智能合约进行交互。从智能合约生成Java Wrapper类有不同的方法1. Web3j命令行工具和solc2. Web3j命令行工具和Truffle构建生成的工件3. web3j-maven-plugin4. web3j-gradle-plugin为了演示如何使用上述方法本教程使用以下智能合约将文档公证到以太坊区块链上的注册表中。DocumentRegistry.solpragma solidity ^0.5.6;/*** dev Smart Contract responsible to notarize documents on the Ethereum Blockchain*/contract DocumentRegistry {struct Document {address signer; // Notaryuint date; // Date of notarizationbytes32 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 --versionsolc the solidity compiler commandline interfaceVersion 0.5.9commit.c68bc34e.Linux.g2、安装Web3J CLI要安装web3j cli请从项目存储库的“发布”页面的“下载”部分下下载zipfile/tarball或通过homebrew为MacOS用户或通过aur为Arch Linux用户下载zipfile/tarball。brew tap web3j/web3jbrew install web3jweb3j要通过zipfile运行解压缩并运行二进制文件您可能还需要将二进制文件添加到PATH中$ unzip web3j-4.3.0.zipcreating web3j-4.3.0/lib/inflating web3j-4.3.0/lib/core-1.0.2-all.jarcreating web3j-4.3.0/bin/inflating web3j-4.3.0/bin/web3jinflating web3j-4.3.0/bin/web3j.bat$ 。/web3j-《version》/bin/web3j_ _____ _ _| | |____ (_) (_)__ _____| |__ / /_ _ ___\ \ /\ / / _ \ ‘_ \ \ \ | | | / _ \\ V V / __/ |_) |.___/ / | _ | || (_) |\_/\_/ \___|_.__/ \____/| |(_)|_| \___/_/ ||__/Usage web3j version|wallet|solidity 。..3、使用solc编译智能合约给定我们的Solidity文件DocumentRegistry.solsolc 《sol》 --bin --abi --optimize -o 《output》命令编译智能合约并在同一目录中生成两个新文件$ solc DocumentRegistry.sol --bin --abi --optimize -o 。/Compiler run successful. Artifact(s) can be found in directory 。/.$ ls -ltotal 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.solDocumentRegistry.bin二进制文件智能合约的字节码DocumentRegistry.abi智能合约的ABI(应用程序二进制接口)它以JSON格式定义接口。4、使用web3j-cli生成包装器使用ABI和bytecode(在步骤3中生成)和web3j-cli(在步骤2中安装)我们现在可以使用以下命令生成我们的智能合约的Java Wrapperweb3j 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.javaWeb3j命令行工具和Truffle artefactsTruffle是最著名的框架之一可帮助您使用以太坊开发、测试和部署。 我们可以使用Truffle使用Web3j命令行工具生成的artefacts来创建wrapper类。1、安装TruffleTruffle可作为npm wrapper提供。$ npm install truffle -g- Fetching solc version list from solc-bin. Attempt #1 truffle5.0.24added 27 packages from 439 contributors in 11.636s$ truffle versionTruffle v5.0.24 (core 5.0.24)Solidity v0.5.0 (solc-js)Node v10.15.3Web3.js v1.0.0-beta.372、初始化新的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 boxUnbox successful. SweetCommandsCompile truffle compileMigrate truffle migrateTest contracts truffle test$ ls -ltotal 20drwxrwxr-x 2 gjeanmart gjeanmart 4096 Jun 24 14:25 contractsdrwxrwxr-x 2 gjeanmart gjeanmart 4096 Jun 24 14:25 migrationsdrwxrwxr-x 2 gjeanmart gjeanmart 4096 Jun 24 14:25 test-rw-rw-r-- 1 gjeanmart gjeanmart 4233 Jun 24 14:25 truffle-config.js3、将合同添加到文件夹合约中将智能合约源documentregistry.sol复制到文件夹contracts中。4、编译合约使用命令truffle compile编译智能合约此命令生成一个新的文件夹build/contracts/其中包含每个编译的智能合约的truffle artefact。$ truffle compileCompiling 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.8commit.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.json5、从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 --versionsolc the solidity compiler commandline interfaceVersion 0.5.9commit.c68bc34e.Linux.g2、将智能合约复制到文件夹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 --versionsolc the solidity compiler commandline interfaceVersion 0.5.9commit.c68bc34e.Linux.g2、将智能合约放入文件夹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.gradlesolidity {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 Libraryid ’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 frameworktestImplementation ’junit:junit:4.12‘}web3j {generatedPackageName ’me.gjeanmart.tutorials.javaethereum.contracts.generated‘generatedFilesBaseDir “$buildDir/contracts”}solidity {executable “solc”}4、执行gradle构建在最后一步中我们使用。/gradlew tasks执行构建--all并验证我们生成的wrapper类是否已生成。