mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-03-15 10:26:37 -04:00
improve addressentry handling
This commit is contained in:
parent
00b41cf537
commit
fc85a8ee17
@ -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()
|
||||
|
@ -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<BalanceListener> balanceListeners = new ArrayList<>();
|
||||
private Wallet wallet;
|
||||
private WalletEventListener walletEventListener;
|
||||
private AddressEntry registrationAddressEntry;
|
||||
private AddressEntry arbitratorDepositAddressInfo;
|
||||
|
||||
@GuardedBy("lock")
|
||||
private List<AddressEntry> 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<AddressEntry> filteredList = Lists.newArrayList(Collections2.filter(ImmutableList.copyOf(addressEntryList),
|
||||
e -> (e != null && e.getAddressContext().equals(AddressEntry.AddressContext.TRADE) && e.getTradeId() == null)));
|
||||
Optional<AddressEntry> 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<AddressEntry> 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<AddressEntry> 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> 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();
|
||||
|
@ -30,7 +30,6 @@ public class DepositController extends CachedViewController
|
||||
|
||||
@FXML private TableView<DepositListItem> tableView;
|
||||
@FXML private TableColumn<String, DepositListItem> 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);
|
||||
}
|
||||
|
@ -22,6 +22,4 @@
|
||||
<TableColumn text="Status" fx:id="confidenceColumn" minWidth="30" sortable="false"/>
|
||||
</columns>
|
||||
</TableView>
|
||||
|
||||
<Button fx:id="addNewAddressButton" text="Add new address" onAction="#onAddNewTradeAddress"/>
|
||||
</VBox>
|
@ -221,12 +221,12 @@ public class WithdrawalController 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);
|
||||
}
|
||||
|
@ -122,9 +122,9 @@ public class WithdrawalListItem
|
||||
case REGISTRATION_FEE:
|
||||
return "Registration fee";
|
||||
case TRADE:
|
||||
if (addressEntry.getTradeId() != null)
|
||||
if (addressEntry.getOfferId() != null)
|
||||
{
|
||||
return "Trade ID: " + addressEntry.getTradeId();
|
||||
return "Trade ID: " + addressEntry.getOfferId();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -25,6 +25,7 @@ import io.bitsquare.user.User;
|
||||
import java.net.URL;
|
||||
import java.util.Random;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.UUID;
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
@ -49,6 +50,7 @@ public class CreateOfferController extends CachedViewController
|
||||
private final WalletFacade walletFacade;
|
||||
final ViewModel viewModel = new ViewModel();
|
||||
private final double collateral;
|
||||
private final String offerId;
|
||||
private Direction direction;
|
||||
private AddressEntry addressEntry;
|
||||
|
||||
@ -87,6 +89,8 @@ public class CreateOfferController extends CachedViewController
|
||||
viewModel.acceptedCountries.set(BitSquareFormatter.countryLocalesToString(settings.getAcceptedCountries()));
|
||||
viewModel.acceptedLanguages.set(BitSquareFormatter.languageLocalesToString(settings.getAcceptedLanguageLocales()));
|
||||
viewModel.feeLabel.set(BitSquareFormatter.formatCoinWithCode(FeePolicy.CREATE_OFFER_FEE.add(FeePolicy.TX_FEE)));
|
||||
|
||||
offerId = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
|
||||
@ -113,7 +117,7 @@ public class CreateOfferController extends CachedViewController
|
||||
//TODO
|
||||
if (walletFacade.getWallet() != null)
|
||||
{
|
||||
addressEntry = walletFacade.getUnusedTradeAddressInfo();
|
||||
addressEntry = walletFacade.getAddressInfoByTradeID(offerId);
|
||||
addressTextField.setAddress(addressEntry.getAddress().toString());
|
||||
|
||||
balanceTextField.setAddress(addressEntry.getAddress());
|
||||
@ -166,8 +170,9 @@ public class CreateOfferController extends CachedViewController
|
||||
if (amountTextField.getIsValid() && minAmountTextField.getIsValid() && volumeTextField.getIsValid() && amountTextField.getIsValid())
|
||||
{
|
||||
viewModel.isPlaceOfferButtonDisabled.set(true);
|
||||
|
||||
tradeManager.requestPlaceOffer(direction,
|
||||
|
||||
tradeManager.requestPlaceOffer(offerId,
|
||||
direction,
|
||||
BitSquareFormatter.parseToDouble(viewModel.price.get()),
|
||||
BitSquareFormatter.parseToCoin(viewModel.amount.get()),
|
||||
BitSquareFormatter.parseToCoin(viewModel.minAmount.get()),
|
||||
|
@ -41,7 +41,8 @@ public class Offer implements Serializable
|
||||
// Constructor
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public Offer(PublicKey messagePublicKey,
|
||||
public Offer(String id,
|
||||
PublicKey messagePublicKey,
|
||||
Direction direction,
|
||||
double price,
|
||||
Coin amount,
|
||||
@ -55,6 +56,7 @@ public class Offer implements Serializable
|
||||
List<Country> acceptedCountries,
|
||||
List<Locale> acceptedLanguageLocales)
|
||||
{
|
||||
this.id = id;
|
||||
this.messagePublicKey = messagePublicKey;
|
||||
this.direction = direction;
|
||||
this.price = price;
|
||||
@ -71,7 +73,6 @@ public class Offer implements Serializable
|
||||
this.acceptedLanguageLocales = acceptedLanguageLocales;
|
||||
|
||||
creationDate = new Date();
|
||||
id = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
|
||||
|
@ -127,7 +127,8 @@ public class TradeManager
|
||||
// Manage offers
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void requestPlaceOffer(Direction direction,
|
||||
public void requestPlaceOffer(String id,
|
||||
Direction direction,
|
||||
double price,
|
||||
Coin amount,
|
||||
Coin minAmount,
|
||||
@ -135,7 +136,8 @@ public class TradeManager
|
||||
ErrorMessageHandler errorMessageHandler)
|
||||
{
|
||||
|
||||
Offer offer = new Offer(user.getMessagePublicKey(),
|
||||
Offer offer = new Offer(id,
|
||||
user.getMessagePublicKey(),
|
||||
direction,
|
||||
price,
|
||||
amount,
|
||||
|
@ -227,7 +227,7 @@ public class UtilsDHT2
|
||||
for (int i = 0; i < peers.length; i++)
|
||||
{
|
||||
for (int j = 0; j < peers.length; j++)
|
||||
peers[i].peer().peerBean().peerMap().peerFound(peers[j].peer().peerAddress(), null, null);
|
||||
peers[i].peer().peerBean().peerMap().peerFound(peers[j].peer().peerAddress(), null);
|
||||
}
|
||||
System.err.println("perfect routing done.");
|
||||
}
|
||||
@ -237,7 +237,7 @@ public class UtilsDHT2
|
||||
for (int i = 0; i < peers.length; i++)
|
||||
{
|
||||
for (int j = 0; j < peers.length; j++)
|
||||
peers[i].peerBean().peerMap().peerFound(peers[j].peerAddress(), peers[j].peerAddress(), null);
|
||||
peers[i].peerBean().peerMap().peerFound(peers[j].peerAddress(), peers[j].peerAddress());
|
||||
}
|
||||
System.err.println("perfect routing done.");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user