區塊鏈研究實驗室|從您的智慧合同生成Java封裝器

買賣虛擬貨幣

在本文中,我們將瞭解如何直接從智慧合約生成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
   */

functionnotarizeDocument(bytes32 _documentHash)externalreturns(bool){
    registry[_documentHash].signer = msg.sender;
    registry[_documentHash].date = now;
    registry[_documentHash].hash = _documentHash;

    emit Notarized(msg.sender, _documentHash);

returntrue;
  }

/**
   *  @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
   */

functionisNotarized(bytes32 _documentHash)externalviewreturns(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);
}

1

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

1

Web3j命令列工具和Truffle artefacts

Truffle是最著名的框架之一,可幫助您使用以太坊開發、測試和部署。 我們可以使用Truffle使用Web3j命令列工具生成的artefacts來建立wrapper類。

1、安裝Truffle

Truffle可作為npm wrapper提供。

$ npm install truffle -g
- Fetching solc versionlistfrom solc-bin. Attempt #1
+ truffle@5.0.24
added 27 packages from439contributorsin11.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,您可以做的比本文中顯示的更多,例如部署指令碼(遷移)、多網路配置、測試、除錯。

1

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並自動新增到類路徑中。

1

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類是否已生成。

相關閱讀:

區塊鏈研究實驗室|在Java中監聽以太坊智慧合約事件

免責聲明:

  1. 本文版權歸原作者所有,僅代表作者本人觀點,不代表鏈報觀點或立場。
  2. 如發現文章、圖片等侵權行爲,侵權責任將由作者本人承擔。
  3. 鏈報僅提供相關項目信息,不構成任何投資建議

推荐阅读

;