Ethereumj

乙太坊帳戶設置,與區塊鏈連接並跟踪更新

  • July 10, 2019

我有一個Bitcoin開發的錢包Java/Spring MVC,支持賬戶設置,連接區塊鏈並跟踪更新,如餘額、交易等。提供的範常式式碼,

public class WalletModel {

   // keep tracks of the updates in the wallet
   private void update(Wallet wallet) {

         this.balance = wallet.getBalance();

   this.address = wallet.currentReceiveAddress();

   transactions.addAll(wallet.getRecentTransactions(100, false));

   // find to most recent transaction
   this.transaction = Objects.isNull(transactions) ||
           transactions.isEmpty() ? "No transaction" : String.valueOf(transactions.get(0));
 }

public boolean setWallet(Wallet wallet) {

   try {

       wallet.addChangeEventListener(new WalletChangeEventListener() {
           @Override
           public void onWalletChanged(Wallet wallet) {
               update(wallet);
           }
       });
       update(wallet);
       return true;
   } catch (Exception e) {
       e.printStackTrace();
   }
   return false;
    }
}

WalletManager程式碼例如,

public class WalletManager {

     public static WalletAppKit bitcoin;
     public static NetworkParameters networkParameters = 
TestNet3Params.get();

public static WalletManager setupWallet(final String walletName) {

       logger.info("Setup Wallet");

       WalletManager walletManager = new WalletManager();
       walletManager.setupWalletKit(walletName);

       try {
           if (walletManager.bitcoin.isChainFileLocked()) {
               return walletManager;
           }
       } catch (IOException e) {
           e.printStackTrace();
           return walletManager;
       }

       walletManager.bitcoin.startAsync();
       return walletManager;
   }

private void setupWalletKit(final String walletId) {

       File directory = getWalletDirectory(walletId);

       // if the seed is not null, that means we are restoring from the backup
       bitcoin = new WalletAppKit(networkParameters, directory, WALLET_FILE_NAME) {

           @Override
           protected void onSetupCompleted() {

               // Don't make the user wait for confirmations
               // they're sending their own money anyway!!
               bitcoin.wallet().allowSpendingUnconfirmedTransactions();
               Wallet wallet = bitcoin.wallet();

               model.setWallet(wallet);
               setupCompletedListeners.forEach(listener -> listener.onSetupCompleted(wallet));
           }
       };

       // Now configure and start the appkit. This will take a second or two - we could show a temporary splash screen
       // or progress widget to keep the user engaged whilst we initialise, but we don't.
       if (networkParameters == RegTestParams.get()) {
           bitcoin.connectToLocalHost();   // You should run a regtest mode bitcoind locally.
       } else if (networkParameters == TestNet3Params.get()) {
           bitcoin.useTor();
       }

       bitcoin.setDownloadListener(model.getSyncProgressUpdater())
               .setBlockingStartup(false)
               .setUserAgent(APP_NAME, "1.0");
   }
}

如何在乙太坊中用 Java 編寫類似的操作?

更新

maven用於開發和使用EthereumJ依賴,例如;

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

這似乎很好,因為文件中沒有錯誤POM。當我嘗試導入源文件時,RED出現錯誤,

在此處輸入圖像描述

我也嘗試過使用其他版本的依賴項。這裡有什麼問題?

我認為使用 api 來監控帳戶詳細資訊會更容易。例如, Amberdata.io有一種方法可以獲取有關您正在查找的帳戶的詳細資訊。你可以在這裡試試!

為了獲得 Maven 的依賴,你應該把它添加到你的 pom 中:

<repositories>
   <repository>
       <id>ethereum</id>
       <url>http://dl.bintray.com/ethereum/maven</url>
   </repository>
</repositories>

因為工件沒有在中央http://maven.org/上發布

引用自:https://ethereum.stackexchange.com/questions/24609