1. 程式人生 > >java開發以太坊應用的庫ethereumj

java開發以太坊應用的庫ethereumj

EthereumJ是以太坊協議的純Java實現。有關以太坊及其目標的高階資訊,請訪問ethereum.org,其白皮書提供了一個完整的概念的概述,和黃皮書一起提供了協議的正式定義。

我們儘可能保持EthereumJ簡單。對於JSON-RPC支援和其他客戶端功能,請看Ethereum Harmony

執行EthereumJ

作為依賴項新增到Maven專案:

   <dependency>
     <groupId>org.ethereum</groupId>
     <artifactId>ethereumj-core</artifactId
>
<version>1.8.1-RELEASE</version> </dependency>

或者你的Gradle專案:

repositories {
       mavenCentral()
       jcenter()
       maven { url "https://dl.bintray.com/ethereum/maven/" }
   }
   compile "org.ethereum:ethereumj-core:1.8.+"

作為你專案的起點,任何一個專案都可以以下列方式啟動:

git clone https://github.com/ether-camp/ethereumj.starter
./gradlew run

檢查你的本地區塊鏈如何同步使用:

 curl -w "\n" -X GET http://localhost:8080/bestBlock

構建可執行的JAR

git clone https://github.com/ethereum/ethereumj
cd ethereumj
cp ethereumj-core/src/main/resources/ethereumj.conf ethereumj-core/src/main/resources/user.conf
vim ethereumj-core/src/main/resources/user.conf # adjust user.conf to your needs
./gradlew clean fatJar java -jar ethereumj-core/build/libs/ethereumj-core-*-all.jar

從命令列執行:

> git clone https://github.com/ethereum/ethereumj
> cd ethereumj
> ./gradlew run [-PmainClass=<sample class>]

可以執行任意一個測試示例:

./gradlew run -PmainClass=org.ethereum.samples.BasicSample
./gradlew run -PmainClass=org.ethereum.samples.FollowAccount
./gradlew run -PmainClass=org.ethereum.samples.PendingStateSample
./gradlew run -PmainClass=org.ethereum.samples.PriceFeedSample
./gradlew run -PmainClass=org.ethereum.samples.PrivateMinerSample
./gradlew run -PmainClass=org.ethereum.samples.TestNetSample
./gradlew run -PmainClass=org.ethereum.samples.TransactionBomb

快照構建

請注意,快照不穩定,目前正在開發中!如果你還想嘗試一下:

  • 新增依賴項org.ethereum:ethereumj-core:${VERSION}${VERSION}位置的值是1.9.0-SNAPSHOT

示例如下:

<repository>
    <id>jfrog-snapshots</id>
    <name>oss.jfrog.org</name>
    <url>https://oss.jfrog.org/libs-snapshot/</url>
    <snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- ... -->
<dependency>
   <groupId>org.ethereum</groupId>
   <artifactId>ethereumj-core</artifactId>
   <version>1.9.0-SNAPSHOT</version>
</dependency>

將專案匯入IntelliJ IDEA:

> git clone https://github.com/ethereum/ethereumj
> cd ethereumj
> gradlew build

IDEA:

  • File -> New -> Project from existing sources……
  • 選擇 ethereumj/build.gradle
  • 對話方塊“Import Project from gradle”:點選“ok”
  • 在構建之後執行org.ethereum.Startorg.ethereum.samples.*之一或建立自己的主程式。

配置EthereumJ

有關所有現有選項,其描述和預設值的參考,你可以參考預設配置ethereumj.conf(你可以在jar庫或原始碼ethereum-core/src/main/resources目錄中找到它),要重新設定所需的選項,你可以使用以下方法之一:

  • 把你的選項放到<working dir>/config/ethereumj.conf檔案中
  • user.conf放到classpath的啟動目錄中(作為一個資源)
  • 將你的選項放在任何檔案中並通過它提供-Dethereumj.conf.file=<your config>
  • 程式設計通過SystemProperties.CONFIG.override*()使用
  • 程式設計重寫Spring SystemPropertiesbean來使用

請注意,不需要將所有選項都放在自定義配置中,只需要放置你需要修改的那些選項。

如果你希望儘快的使用java開始以太坊的開發,可以選擇web3j類庫,它封裝實現了絕大部分web3的功能,十分好用:

java以太坊教程,主要是針對java和android程式設計師進行區塊鏈以太坊開發的web3j詳解。

另外分享下之前其他語言的以太坊課程連結:

  • 以太坊教程,主要介紹智慧合約與dapp應用開發,適合入門。
  • 以太坊開發,主要是介紹使用node.js、mongodb、區塊鏈、ipfs實現去中心化電商DApp實戰,適合進階。
  • python以太坊,主要是針對python工程師使用web3.py進行區塊鏈以太坊開發的詳解。
  • php以太坊,主要是介紹使用php進行智慧合約開發互動,進行賬號建立、交易、轉賬、代幣開發以及過濾器和事件等內容。
  • C#以太坊,主要講解如何使用C#開發基於.Net的以太坊應用,包括賬戶管理、狀態與交易、智慧合約開發與互動、過濾器和事件等。

匯智網原創翻譯,轉載請標明出處。這裡是原文