Bitcoinj

如何獲取收到付款的地址?

  • May 8, 2015

我正在使用 BitcoinJ 並且想知道我應該歸功於誰,所有使用者都有自己的接收地址。如果你有時間,也請發布一個例子。:) 目前程式碼:

   wallet.addEventListener(new AbstractWalletEventListener(){
       @Override
       public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
           String txid = tx.getHashAsString();
           long received = newBalance.value-prevBalance.value;
           String address; // The receive address that the payment was sent to.
           int UserID = MySQL.getUserIDbyAddress(address);
           // Here will I credit the user.
       }
   });

我最終發現瞭如何做到這一點,在這裡你可以看到:

   wallet.addEventListener(new AbstractWalletEventListener(){
       @Override
       public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
           String hash = tx.getHashAsString();
           long received = newBalance.value-prevBalance.value;
           String address = null;
           for (TransactionOutput txop : tx.getOutputs()){
               if (txop.isMine(wallet)){
                   address = txop.getScriptPubKey().getToAddress(params).toString();
               }
           }
           int UserID = MySQL.getUserIDbyAddress(address);
           // Code...
       }
   });

引用自:https://bitcoin.stackexchange.com/questions/37264