From 6509441e8ce46268ff8d1ddba5f406f15e4b883a Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Mon, 28 Apr 2014 02:36:43 +0200 Subject: [PATCH] add listeners to setupController --- .../btc/AccountRegistrationWallet.java | 99 ++++++++------- .../io/bitsquare/btc/DownloadListener.java | 10 -- .../java/io/bitsquare/btc/WalletFacade.java | 119 +++++++++++++++--- .../java/io/bitsquare/gui/MainController.java | 3 +- .../bitsquare/gui/setup/SetupController.java | 107 ++++++++++++---- .../resources/images/tx/circleProgress0.png | Bin 609 -> 2808 bytes .../images/unused/circleProgress0.png | Bin 0 -> 609 bytes 7 files changed, 240 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/io/bitsquare/btc/DownloadListener.java create mode 100644 src/main/resources/images/unused/circleProgress0.png diff --git a/src/main/java/io/bitsquare/btc/AccountRegistrationWallet.java b/src/main/java/io/bitsquare/btc/AccountRegistrationWallet.java index 65b6322326..2a2f143f98 100644 --- a/src/main/java/io/bitsquare/btc/AccountRegistrationWallet.java +++ b/src/main/java/io/bitsquare/btc/AccountRegistrationWallet.java @@ -15,6 +15,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigInteger; +import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -26,8 +27,9 @@ public class AccountRegistrationWallet extends Wallet implements WalletEventList private static final Logger log = LoggerFactory.getLogger(AccountRegistrationWallet.class); private NetworkParameters networkParameters; + private List walletListeners = new ArrayList<>(); - public AccountRegistrationWallet(NetworkParameters networkParameters, BlockChain chain, PeerGroup peerGroup) + AccountRegistrationWallet(NetworkParameters networkParameters, BlockChain chain, PeerGroup peerGroup) { super(networkParameters); @@ -61,17 +63,33 @@ public class AccountRegistrationWallet extends Wallet implements WalletEventList autosaveToFile(walletFile, 1, TimeUnit.SECONDS, null); } - public Address getAddress() + Address getAddress() { return getKey().toAddress(networkParameters); } - public ECKey getKey() + ECKey getKey() { return getKeys().get(0); } - public void saveToBlockchain(byte[] dataToEmbed) throws InsufficientMoneyException + void addWalletListener(WalletFacade.WalletListener listener) + { + if (walletListeners.size() == 0) + addEventListener(this); + + walletListeners.add(listener); + } + + void removeWalletListener(WalletFacade.WalletListener listener) + { + walletListeners.remove(listener); + + if (walletListeners.size() == 0) + removeEventListener(this); + } + + void saveToBlockchain(byte[] dataToEmbed) throws InsufficientMoneyException { Script script = new ScriptBuilder() .op(OP_RETURN) @@ -116,47 +134,25 @@ public class AccountRegistrationWallet extends Wallet implements WalletEventList }); } - public int getConfirmations() - { - // TODO just a quick impl. need to be checked if it works for all cases... - Set transactions = getTransactions(true); - if (transactions != null && transactions.size() == 1) - { - Transaction transaction = transactions.iterator().next(); - final int lastBlockSeenHeight = getLastBlockSeenHeight(); - int appearedAtChainHeight = 0; - if (transaction.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING) - appearedAtChainHeight = transaction.getConfidence().getAppearedAtChainHeight(); - final int numberOfBlocksEmbedded = lastBlockSeenHeight - appearedAtChainHeight + 1; - - if (numberOfBlocksEmbedded > 0) - return numberOfBlocksEmbedded; - else - return 0; - } - return 0; - } - - public int getNumberOfPeersSeenTx() - { - // TODO just a quick impl. need to be checked if it works for all cases... - Set transactions = getTransactions(true); - if (transactions != null && transactions.size() == 1) - { - Transaction transaction = transactions.iterator().next(); - return (transaction == null || transaction.getConfidence() == null) ? 0 : transaction.getConfidence().numBroadcastPeers(); - } - return 0; - } - - //TODO those handlers are not called yet... @Override public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { + for (WalletFacade.WalletListener walletListener : walletListeners) + walletListener.onCoinsReceived(newBalance); + log.info("onCoinsReceived"); } + @Override + public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) + { + for (WalletFacade.WalletListener walletListener : walletListeners) + walletListener.onConfidenceChanged(tx.getConfidence().numBroadcastPeers(), tx.getConfidence().getDepthInBlocks()); + + log.info("onTransactionConfidenceChanged " + tx.getConfidence().toString()); + } + @Override public void onCoinsSent(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { @@ -169,12 +165,6 @@ public class AccountRegistrationWallet extends Wallet implements WalletEventList log.info("onReorganize"); } - @Override - public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) - { - log.info("onTransactionConfidenceChanged"); - } - @Override public void onWalletChanged(Wallet wallet) { @@ -193,5 +183,26 @@ public class AccountRegistrationWallet extends Wallet implements WalletEventList log.info("onScriptsAdded"); } + int getConfirmationNumBroadcastPeers() + { + Transaction transaction = getTransaction(); + return (transaction == null || transaction.getConfidence() == null) ? 0 : transaction.getConfidence().numBroadcastPeers(); + } + int getConfirmationDepthInBlocks() + { + Transaction transaction = getTransaction(); + return (transaction == null || transaction.getConfidence() == null) ? 0 : transaction.getConfidence().getDepthInBlocks(); + } + + //TODO only 1 tx supported yet... + private Transaction getTransaction() + { + Set transactions = getTransactions(true); + if (transactions != null && transactions.size() == 1) + { + return transactions.iterator().next(); + } + return null; + } } diff --git a/src/main/java/io/bitsquare/btc/DownloadListener.java b/src/main/java/io/bitsquare/btc/DownloadListener.java deleted file mode 100644 index b90886ba8d..0000000000 --- a/src/main/java/io/bitsquare/btc/DownloadListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.bitsquare.btc; - -import java.util.Date; - -public interface DownloadListener -{ - void progress(double percent, int blocksSoFar, Date date); - - void doneDownload(); -} diff --git a/src/main/java/io/bitsquare/btc/WalletFacade.java b/src/main/java/io/bitsquare/btc/WalletFacade.java index 665d68f79d..b89054c32c 100644 --- a/src/main/java/io/bitsquare/btc/WalletFacade.java +++ b/src/main/java/io/bitsquare/btc/WalletFacade.java @@ -4,6 +4,7 @@ import com.google.bitcoin.core.*; import com.google.bitcoin.kits.WalletAppKit; import com.google.bitcoin.params.MainNetParams; import com.google.bitcoin.params.RegTestParams; +import com.google.bitcoin.script.Script; import com.google.bitcoin.utils.Threading; import com.google.inject.Inject; import io.bitsquare.crypto.CryptoFacade; @@ -22,7 +23,7 @@ import java.util.UUID; * Code from BitcoinJ must not be used outside that facade. * That way a change of the library will only affect that class. */ -public class WalletFacade +public class WalletFacade implements WalletEventListener { public static final String MAIN_NET = "MAIN_NET"; public static final String TEST_NET = "TEST_NET"; @@ -38,6 +39,7 @@ public class WalletFacade private AccountRegistrationWallet accountRegistrationWallet = null; private List downloadListeners = new ArrayList<>(); + private List walletListeners = new ArrayList<>(); @Inject public WalletFacade(NetworkParameters networkParameters, WalletAppKit walletAppKit, CryptoFacade cryptoFacade, BlockChainFacade blockChainFacade) @@ -80,6 +82,8 @@ public class WalletFacade walletAppKit.wallet().allowSpendingUnconfirmedTransactions(); walletAppKit.peerGroup().setMaxConnections(11); + walletAppKit.wallet().addEventListener(this); + log.info(walletAppKit.wallet().toString()); } @@ -89,16 +93,37 @@ public class WalletFacade walletAppKit.awaitTerminated(); } - public void addDownloadListener(DownloadListener downloadListener) + public void addDownloadListener(DownloadListener listener) { - downloadListeners.add(downloadListener); + downloadListeners.add(listener); } - public void removeDownloadListener(DownloadListener downloadListener) + public void removeDownloadListener(DownloadListener listener) { - downloadListeners.remove(downloadListener); + downloadListeners.remove(listener); } + public void addWalletListener(WalletListener listener) + { + walletListeners.add(listener); + } + + public void removeWalletListener(WalletListener listener) + { + walletListeners.remove(listener); + } + + public void addRegistrationWalletListener(WalletListener listener) + { + getAccountRegistrationWallet().addWalletListener(listener); + } + + public void removeRegistrationWalletListener(WalletListener listener) + { + getAccountRegistrationWallet().removeWalletListener(listener); + } + + //MOCK public KeyPair createNewAddress() { @@ -132,16 +157,6 @@ public class WalletFacade return getAccountRegistrationWallet().getBalance(Wallet.BalanceType.ESTIMATED); } - public int getAccountRegistrationConfirmations() - { - return getAccountRegistrationWallet().getConfirmations(); - } - - public int getAccountRegistrationNumberOfPeersSeenTx() - { - return getAccountRegistrationWallet().getNumberOfPeersSeenTx(); - } - public void sendRegistrationTx(String stringifiedBankAccounts) throws InsufficientMoneyException { getAccountRegistrationWallet().saveToBlockchain(cryptoFacade.getEmbeddedAccountRegistrationData(getAccountRegistrationWallet().getKey(), stringifiedBankAccounts)); @@ -154,6 +169,66 @@ public class WalletFacade && blockChainFacade.verifyAddressInBlockChain(hashAsHexStringToVerify, address); } + public int getRegistrationConfirmationNumBroadcastPeers() + { + return getAccountRegistrationWallet().getConfirmationNumBroadcastPeers(); + } + + public int getRegistrationConfirmationDepthInBlocks() + { + return getAccountRegistrationWallet().getConfirmationDepthInBlocks(); + } + + // WalletEventListener + @Override + public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) + { + for (WalletListener walletListener : walletListeners) + walletListener.onCoinsReceived(newBalance); + + log.info("onCoinsReceived"); + } + + @Override + public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) + { + for (WalletListener walletListener : walletListeners) + walletListener.onConfidenceChanged(tx.getConfidence().numBroadcastPeers(), tx.getConfidence().getDepthInBlocks()); + + log.info("onTransactionConfidenceChanged " + tx.getConfidence().toString()); + } + + @Override + public void onCoinsSent(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) + { + log.info("onCoinsSent"); + } + + @Override + public void onReorganize(Wallet wallet) + { + log.info("onReorganize"); + } + + @Override + public void onWalletChanged(Wallet wallet) + { + log.info("onWalletChanged"); + } + + @Override + public void onKeysAdded(Wallet wallet, List keys) + { + log.info("onKeysAdded"); + } + + @Override + public void onScriptsAdded(Wallet wallet, List