From fc85a8ee17e63cb88e0f067a7981115abd5c087a Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sun, 24 Aug 2014 14:38:16 +0200 Subject: [PATCH] improve addressentry handling --- .../java/io/bitsquare/btc/AddressEntry.java | 18 +-- .../java/io/bitsquare/btc/WalletFacade.java | 111 ++++-------------- .../gui/funds/deposit/DepositController.java | 13 +- .../gui/funds/deposit/DepositView.fxml | 2 - .../withdrawal/WithdrawalController.java | 6 +- .../funds/withdrawal/WithdrawalListItem.java | 4 +- .../createoffer/CreateOfferController.java | 11 +- src/main/java/io/bitsquare/trade/Offer.java | 5 +- .../java/io/bitsquare/trade/TradeManager.java | 6 +- src/test/java/io/bitsquare/msg/UtilsDHT2.java | 4 +- 10 files changed, 58 insertions(+), 122 deletions(-) diff --git a/src/main/java/io/bitsquare/btc/AddressEntry.java b/src/main/java/io/bitsquare/btc/AddressEntry.java index 0e4770f2a4..d8ba87048a 100644 --- a/src/main/java/io/bitsquare/btc/AddressEntry.java +++ b/src/main/java/io/bitsquare/btc/AddressEntry.java @@ -12,28 +12,28 @@ public class AddressEntry implements Serializable private transient DeterministicKey key; private final NetworkParameters params; private final AddressContext addressContext; + private final String offerId; private final byte[] pubKeyHash; - private String tradeId = null; public AddressEntry(DeterministicKey key, NetworkParameters params, AddressContext addressContext) + { + this(key, params, addressContext, null); + } + + public AddressEntry(DeterministicKey key, NetworkParameters params, AddressContext addressContext, String offerId) { this.key = key; this.params = params; this.addressContext = addressContext; + this.offerId = offerId; pubKeyHash = key.getPubOnly().getPubKeyHash(); } - - public String getTradeId() + public String getOfferId() { - return tradeId; - } - - public void setTradeId(String tradeId) - { - this.tradeId = tradeId; + return offerId; } public AddressContext getAddressContext() diff --git a/src/main/java/io/bitsquare/btc/WalletFacade.java b/src/main/java/io/bitsquare/btc/WalletFacade.java index 35a34d09ce..928ec5cb78 100644 --- a/src/main/java/io/bitsquare/btc/WalletFacade.java +++ b/src/main/java/io/bitsquare/btc/WalletFacade.java @@ -9,9 +9,7 @@ import com.google.bitcoin.params.RegTestParams; import com.google.bitcoin.script.Script; import com.google.bitcoin.script.ScriptBuilder; import com.google.bitcoin.utils.Threading; -import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -62,6 +60,8 @@ public class WalletFacade private final List balanceListeners = new ArrayList<>(); private Wallet wallet; private WalletEventListener walletEventListener; + private AddressEntry registrationAddressEntry; + private AddressEntry arbitratorDepositAddressInfo; @GuardedBy("lock") private List addressEntryList = new ArrayList<>(); @@ -206,10 +206,10 @@ public class WalletFacade { lock.lock(); DeterministicKey registrationKey = wallet.currentReceiveKey(); - addressEntryList.add(new AddressEntry(registrationKey, params, AddressEntry.AddressContext.REGISTRATION_FEE)); + registrationAddressEntry = new AddressEntry(registrationKey, params, AddressEntry.AddressContext.REGISTRATION_FEE); + addressEntryList.add(registrationAddressEntry); lock.unlock(); saveAddressInfoList(); - getNewTradeAddressEntry(); } } @@ -273,71 +273,27 @@ public class WalletFacade return ImmutableList.copyOf(addressEntryList); } - public AddressEntry getRegistrationAddressInfo() { - return getAddressInfoByAddressContext(AddressEntry.AddressContext.REGISTRATION_FEE); + return registrationAddressEntry; } - public AddressEntry getArbitratorDepositAddressInfo() { - AddressEntry arbitratorDepositAddressEntry = getAddressInfoByAddressContext(AddressEntry.AddressContext.ARBITRATOR_DEPOSIT); - if (arbitratorDepositAddressEntry == null) - { - arbitratorDepositAddressEntry = getNewArbitratorDepositAddressEntry(); - } + if (arbitratorDepositAddressInfo == null) + arbitratorDepositAddressInfo = getNewAddressEntry(AddressEntry.AddressContext.ARBITRATOR_DEPOSIT, null); - return arbitratorDepositAddressEntry; + return arbitratorDepositAddressInfo; } - - public AddressEntry getUnusedTradeAddressInfo() + public AddressEntry getAddressInfoByTradeID(String offerId) { - List filteredList = Lists.newArrayList(Collections2.filter(ImmutableList.copyOf(addressEntryList), - e -> (e != null && e.getAddressContext().equals(AddressEntry.AddressContext.TRADE) && e.getTradeId() == null))); + Optional addressEntry = getAddressEntryList().stream().filter(e -> e.getOfferId().equals(offerId)).findFirst(); - if (filteredList != null && !filteredList.isEmpty()) - { - return filteredList.get(0); - } + if (addressEntry.isPresent()) + return addressEntry.get(); else - { - return getNewTradeAddressEntry(); - } - } - - - private AddressEntry getAddressInfoByAddressContext(AddressEntry.AddressContext addressContext) - { - List filteredList = Lists.newArrayList(Collections2.filter(ImmutableList.copyOf(addressEntryList), - e -> (e != null && e.getAddressContext() != null && e.getAddressContext().equals(addressContext)))); - - if (filteredList != null && !filteredList.isEmpty()) - { - return filteredList.get(0); - } - else - { - return null; - } - } - - - public AddressEntry getAddressInfoByTradeID(String tradeId) - { - for (AddressEntry addressEntry : ImmutableList.copyOf(addressEntryList)) - { - if (addressEntry.getTradeId() != null && addressEntry.getTradeId().equals(tradeId)) - { - return addressEntry; - } - } - - AddressEntry addressEntry = getUnusedTradeAddressInfo(); - assert addressEntry != null; - addressEntry.setTradeId(tradeId); - return addressEntry; + return getNewAddressEntry(AddressEntry.AddressContext.TRADE, offerId); } @@ -345,20 +301,12 @@ public class WalletFacade // Create new AddressInfo objects /////////////////////////////////////////////////////////////////////////////////////////// - - public AddressEntry getNewTradeAddressEntry() - { - return getNewAddressEntry(AddressEntry.AddressContext.TRADE); - } - - - private AddressEntry getNewAddressEntry(AddressEntry.AddressContext addressContext) + private AddressEntry getNewAddressEntry(AddressEntry.AddressContext addressContext, String offerId) { lock.lock(); wallet.getLock().lock(); - DeterministicKey key = wallet.freshReceiveKey(); - AddressEntry addressEntry = new AddressEntry(key, params, addressContext); + AddressEntry addressEntry = new AddressEntry(key, params, addressContext, offerId); addressEntryList.add(addressEntry); saveAddressInfoList(); lock.unlock(); @@ -366,21 +314,9 @@ public class WalletFacade return addressEntry; } - - private AddressEntry getNewArbitratorDepositAddressEntry() + private Optional getAddressEntryByAddressString(String address) { - return getNewAddressEntry(AddressEntry.AddressContext.ARBITRATOR_DEPOSIT); - } - - - private AddressEntry getAddressEntryByAddressString(String address) - { - for (AddressEntry addressEntry : addressEntryList) - { - if (addressEntry.getAddressString().equals(address)) - return addressEntry; - } - return null; + return getAddressEntryList().stream().filter(e -> e.getAddressString().equals(address)).findFirst(); } @@ -573,7 +509,7 @@ public class WalletFacade return getRegistrationBalance().compareTo(FeePolicy.ACCOUNT_REGISTRATION_FEE) >= 0; } - public boolean isUnusedTradeAddressBalanceAboveCreationFee() + /* public boolean isUnusedTradeAddressBalanceAboveCreationFee() { AddressEntry unUsedAddressEntry = getUnusedTradeAddressInfo(); Coin unUsedAddressInfoBalance = getBalanceForAddress(unUsedAddressEntry.getAddress()); @@ -585,7 +521,7 @@ public class WalletFacade AddressEntry unUsedAddressEntry = getUnusedTradeAddressInfo(); Coin unUsedAddressInfoBalance = getBalanceForAddress(unUsedAddressEntry.getAddress()); return unUsedAddressInfoBalance.compareTo(FeePolicy.TAKE_OFFER_FEE) > 0; - } + }*/ //TODO public int getNumOfPeersSeenTx(String txID) @@ -706,9 +642,12 @@ public class WalletFacade sendRequest.shuffleOutputs = false; // we allow spending of unconfirmed tx (double spend risk is low and usability would suffer if we need to wait for 1 confirmation) + Optional addressEntry = getAddressEntryByAddressString(withdrawFromAddress); + if (!addressEntry.isPresent()) + throw new IllegalArgumentException("WithdrawFromAddress is not found in our wallets."); - sendRequest.coinSelector = new AddressBasedCoinSelector(params, getAddressEntryByAddressString(withdrawFromAddress), true); - sendRequest.changeAddress = getAddressEntryByAddressString(changeAddress).getAddress(); + sendRequest.coinSelector = new AddressBasedCoinSelector(params, addressEntry.get(), true); + sendRequest.changeAddress = addressEntry.get().getAddress(); Wallet.SendResult sendResult = wallet.sendCoins(sendRequest); Futures.addCallback(sendResult.broadcastComplete, callback); @@ -759,7 +698,6 @@ public class WalletFacade Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tx); sendRequest.shuffleOutputs = false; AddressEntry addressEntry = getAddressInfoByTradeID(tradeId); - addressEntry.setTradeId(tradeId); // we allow spending of unconfirmed tx (double spend risk is low and usability would suffer if we need to wait for 1 confirmation) sendRequest.coinSelector = new AddressBasedCoinSelector(params, addressEntry, true); sendRequest.changeAddress = addressEntry.getAddress(); @@ -819,7 +757,6 @@ public class WalletFacade Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tempTx); sendRequest.shuffleOutputs = false; AddressEntry addressEntry = getAddressInfoByTradeID(tradeId); - addressEntry.setTradeId(tradeId); // we allow spending of unconfirmed tx (double spend risk is low and usability would suffer if we need to wait for 1 confirmation) sendRequest.coinSelector = new AddressBasedCoinSelector(params, addressEntry, true); sendRequest.changeAddress = addressEntry.getAddress(); diff --git a/src/main/java/io/bitsquare/gui/funds/deposit/DepositController.java b/src/main/java/io/bitsquare/gui/funds/deposit/DepositController.java index 0a018ea18a..69d4d689f3 100644 --- a/src/main/java/io/bitsquare/gui/funds/deposit/DepositController.java +++ b/src/main/java/io/bitsquare/gui/funds/deposit/DepositController.java @@ -30,7 +30,6 @@ public class DepositController extends CachedViewController @FXML private TableView tableView; @FXML private TableColumn labelColumn, addressColumn, balanceColumn, copyColumn, confidenceColumn; - @FXML private Button addNewAddressButton; /////////////////////////////////////////////////////////////////////////////////////////// @@ -87,12 +86,6 @@ public class DepositController extends CachedViewController // UI handlers /////////////////////////////////////////////////////////////////////////////////////////// - @FXML - public void onAddNewTradeAddress() - { - addressList.add(new DepositListItem(walletFacade.getNewTradeAddressEntry(), walletFacade)); - } - /////////////////////////////////////////////////////////////////////////////////////////// // Private methods @@ -126,12 +119,12 @@ public class DepositController extends CachedViewController { hyperlink = new Hyperlink(item.getLabel()); hyperlink.setId("id-link"); - if (item.getAddressEntry().getTradeId() != null) + if (item.getAddressEntry().getOfferId() != null) { - Tooltip tooltip = new Tooltip(item.getAddressEntry().getTradeId()); + Tooltip tooltip = new Tooltip(item.getAddressEntry().getOfferId()); Tooltip.install(hyperlink, tooltip); - hyperlink.setOnAction(event -> log.info("Show trade details " + item.getAddressEntry().getTradeId())); + hyperlink.setOnAction(event -> log.info("Show trade details " + item.getAddressEntry().getOfferId())); } setGraphic(hyperlink); } diff --git a/src/main/java/io/bitsquare/gui/funds/deposit/DepositView.fxml b/src/main/java/io/bitsquare/gui/funds/deposit/DepositView.fxml index 23166767cc..4b8837c924 100644 --- a/src/main/java/io/bitsquare/gui/funds/deposit/DepositView.fxml +++ b/src/main/java/io/bitsquare/gui/funds/deposit/DepositView.fxml @@ -22,6 +22,4 @@ - -