nav storage

This commit is contained in:
Manfred Karrer 2014-06-26 23:27:51 +02:00
parent dc9141ca19
commit 3463f07022
6 changed files with 116 additions and 32 deletions

12
pom.xml
View file

@ -176,6 +176,18 @@
<artifactId>core</artifactId> <artifactId>core</artifactId>
<version>1.50.0.0</version> <version>1.50.0.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
</dependency>
<!-- <!--
<dependency> <dependency>
<groupId>net.sf.proguard</groupId> <groupId>net.sf.proguard</groupId>

View file

@ -1,15 +1,38 @@
package io.bitsquare.btc; package io.bitsquare.btc;
import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.AddressFormatException;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.core.Transaction; import com.google.bitcoin.core.Transaction;
import com.google.inject.Inject;
import java.math.BigInteger; import java.math.BigInteger;
public class BtcValidator public class BtcValidator
{ {
private static NetworkParameters params;
@Inject
public BtcValidator(NetworkParameters params)
{
BtcValidator.params = params;
}
public static boolean isMinSpendableAmount(BigInteger amount) public static boolean isMinSpendableAmount(BigInteger amount)
{ {
return amount != null && amount.compareTo(FeePolicy.TX_FEE.add(Transaction.MIN_NONDUST_OUTPUT)) > 0; return amount != null && amount.compareTo(FeePolicy.TX_FEE.add(Transaction.MIN_NONDUST_OUTPUT)) > 0;
} }
public boolean isAddressValid(String addressString)
{
try
{
new Address(BtcValidator.params, addressString);
return true;
} catch (AddressFormatException e)
{
return false;
}
}
} }

View file

@ -26,12 +26,17 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.ReentrantLock;
import static com.google.bitcoin.script.ScriptOpCodes.OP_RETURN; import static com.google.bitcoin.script.ScriptOpCodes.OP_RETURN;
/**
* TODO: use walletextension (with protobuffer) instead of saving addressEntryList via storage
*/
public class WalletFacade public class WalletFacade
{ {
public static final String MAIN_NET = "MAIN_NET"; public static final String MAIN_NET = "MAIN_NET";
@ -42,6 +47,8 @@ public class WalletFacade
private static final Logger log = LoggerFactory.getLogger(WalletFacade.class); private static final Logger log = LoggerFactory.getLogger(WalletFacade.class);
private final ReentrantLock lock = Threading.lock("lock");
private String saveAddressEntryListId; private String saveAddressEntryListId;
private NetworkParameters params; private NetworkParameters params;
private BitSquareWalletAppKit walletAppKit; private BitSquareWalletAppKit walletAppKit;
@ -53,9 +60,9 @@ public class WalletFacade
private List<DownloadListener> downloadListeners = new ArrayList<>(); private List<DownloadListener> downloadListeners = new ArrayList<>();
private List<ConfidenceListener> confidenceListeners = new ArrayList<>(); private List<ConfidenceListener> confidenceListeners = new ArrayList<>();
private List<BalanceListener> balanceListeners = new ArrayList<>(); private List<BalanceListener> balanceListeners = new ArrayList<>();
@GuardedBy("lock")
private List<AddressEntry> addressEntryList = new ArrayList<>(); private List<AddressEntry> addressEntryList = new ArrayList<>();
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Constructor // Constructor
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -68,6 +75,8 @@ public class WalletFacade
this.feePolicy = feePolicy; this.feePolicy = feePolicy;
this.cryptoFacade = cryptoFacade; this.cryptoFacade = cryptoFacade;
this.storage = storage; this.storage = storage;
saveAddressEntryListId = this.getClass().getName() + ".addressEntryList";
} }
@ -162,7 +171,6 @@ public class WalletFacade
}; };
wallet.addEventListener(walletEventListener); wallet.addEventListener(walletEventListener);
saveAddressEntryListId = this.getClass().getName() + ".addressEntryList";
List<AddressEntry> savedAddressEntryList = (List<AddressEntry>) storage.read(saveAddressEntryListId); List<AddressEntry> savedAddressEntryList = (List<AddressEntry>) storage.read(saveAddressEntryListId);
if (savedAddressEntryList != null) if (savedAddressEntryList != null)
{ {
@ -170,15 +178,23 @@ public class WalletFacade
} }
else else
{ {
ECKey registrationKey = wallet.getKeys().get(0); lock.lock();
AddressEntry registrationAddressEntry = new AddressEntry(registrationKey, params, AddressEntry.AddressContext.REGISTRATION_FEE); try
addressEntryList.add(registrationAddressEntry); {
saveAddressInfoList(); ECKey registrationKey = wallet.getKeys().get(0);
AddressEntry registrationAddressEntry = new AddressEntry(registrationKey, params, AddressEntry.AddressContext.REGISTRATION_FEE);
addressEntryList.add(registrationAddressEntry);
} finally
{
lock.unlock();
}
saveAddressInfoList();
getNewTradeAddressEntry(); getNewTradeAddressEntry();
} }
} }
public void shutDown() public void shutDown()
{ {
wallet.removeEventListener(walletEventListener); wallet.removeEventListener(walletEventListener);
@ -192,12 +208,6 @@ public class WalletFacade
return wallet; return wallet;
} }
private void saveAddressInfoList()
{
// use wallet extension?
storage.write(saveAddressEntryListId, addressEntryList);
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Listener // Listener
@ -243,7 +253,7 @@ public class WalletFacade
public List<AddressEntry> getAddressEntryList() public List<AddressEntry> getAddressEntryList()
{ {
return addressEntryList; return ImmutableList.copyOf(addressEntryList);
} }
public AddressEntry getRegistrationAddressInfo() public AddressEntry getRegistrationAddressInfo()
@ -262,7 +272,7 @@ public class WalletFacade
public AddressEntry getUnusedTradeAddressInfo() public AddressEntry getUnusedTradeAddressInfo()
{ {
List<AddressEntry> filteredList = Lists.newArrayList(Collections2.filter(addressEntryList, new Predicate<AddressEntry>() List<AddressEntry> filteredList = Lists.newArrayList(Collections2.filter(ImmutableList.copyOf(addressEntryList), new Predicate<AddressEntry>()
{ {
@Override @Override
public boolean apply(@Nullable AddressEntry addressInfo) public boolean apply(@Nullable AddressEntry addressInfo)
@ -279,7 +289,7 @@ public class WalletFacade
private AddressEntry getAddressInfoByAddressContext(AddressEntry.AddressContext addressContext) private AddressEntry getAddressInfoByAddressContext(AddressEntry.AddressContext addressContext)
{ {
List<AddressEntry> filteredList = Lists.newArrayList(Collections2.filter(addressEntryList, new Predicate<AddressEntry>() List<AddressEntry> filteredList = Lists.newArrayList(Collections2.filter(ImmutableList.copyOf(addressEntryList), new Predicate<AddressEntry>()
{ {
@Override @Override
public boolean apply(@Nullable AddressEntry addressInfo) public boolean apply(@Nullable AddressEntry addressInfo)
@ -296,7 +306,7 @@ public class WalletFacade
public AddressEntry getAddressInfoByTradeID(String tradeId) public AddressEntry getAddressInfoByTradeID(String tradeId)
{ {
for (AddressEntry addressEntry : addressEntryList) for (AddressEntry addressEntry : ImmutableList.copyOf(addressEntryList))
{ {
if (addressEntry.getTradeId() != null && addressEntry.getTradeId().equals(tradeId)) if (addressEntry.getTradeId() != null && addressEntry.getTradeId().equals(tradeId))
return addressEntry; return addressEntry;
@ -319,12 +329,23 @@ public class WalletFacade
private AddressEntry getNewAddressEntry(AddressEntry.AddressContext addressContext) private AddressEntry getNewAddressEntry(AddressEntry.AddressContext addressContext)
{ {
ECKey key = new ECKey(); AddressEntry addressEntry = null;
wallet.addKey(key); lock.lock();
wallet.addWatchedAddress(key.toAddress(params)); wallet.getLock().lock();
AddressEntry addressEntry = new AddressEntry(key, params, addressContext); try
addressEntryList.add(addressEntry); {
saveAddressInfoList(); ECKey key = new ECKey();
wallet.addKey(key);
wallet.addWatchedAddress(key.toAddress(params));
addressEntry = new AddressEntry(key, params, addressContext);
addressEntryList.add(addressEntry);
saveAddressInfoList();
} finally
{
lock.unlock();
wallet.getLock().unlock();
}
return addressEntry; return addressEntry;
} }
@ -1037,6 +1058,19 @@ public class WalletFacade
// Private methods // Private methods
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
private void saveAddressInfoList()
{
// use wallet extension?
lock.lock();
try
{
storage.write(saveAddressEntryListId, addressEntryList);
} finally
{
lock.unlock();
}
}
private Script getMultiSigScript(String offererPubKey, String takerPubKey, String arbitratorPubKey) private Script getMultiSigScript(String offererPubKey, String takerPubKey, String arbitratorPubKey)
{ {
ECKey offererKey = new ECKey(null, Utils.parseAsHexOrBase58(offererPubKey)); ECKey offererKey = new ECKey(null, Utils.parseAsHexOrBase58(offererPubKey));
@ -1047,11 +1081,12 @@ public class WalletFacade
return ScriptBuilder.createMultiSigOutputScript(2, keys); return ScriptBuilder.createMultiSigOutputScript(2, keys);
} }
public Transaction createPayoutTx(String depositTxAsHex,
BigInteger offererPaybackAmount, private Transaction createPayoutTx(String depositTxAsHex,
BigInteger takerPaybackAmount, BigInteger offererPaybackAmount,
String offererAddress, BigInteger takerPaybackAmount,
String takerAddress) throws AddressFormatException String offererAddress,
String takerAddress) throws AddressFormatException
{ {
log.trace("createPayoutTx"); log.trace("createPayoutTx");
log.trace("inputs: "); log.trace("inputs: ");

View file

@ -16,6 +16,7 @@ import io.bitsquare.gui.util.Icons;
import io.bitsquare.locale.Localisation; import io.bitsquare.locale.Localisation;
import io.bitsquare.msg.MessageFacade; import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.TradeMessage; import io.bitsquare.msg.TradeMessage;
import io.bitsquare.storage.Storage;
import io.bitsquare.trade.Direction; import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Trading; import io.bitsquare.trade.Trading;
import io.bitsquare.user.User; import io.bitsquare.user.User;
@ -59,6 +60,9 @@ public class MainController implements Initializable, NavigationController
private ToggleButton buyButton, sellButton, homeButton, msgButton, ordersButton, historyButton, fundsButton, settingsButton; private ToggleButton buyButton, sellButton, homeButton, msgButton, ordersButton, historyButton, fundsButton, settingsButton;
private Pane msgButtonHolder, buyButtonHolder, sellButtonHolder, ordersButtonButtonHolder; private Pane msgButtonHolder, buyButtonHolder, sellButtonHolder, ordersButtonButtonHolder;
private TextField balanceTextField; private TextField balanceTextField;
private Storage storage;
private String storageId;
private ToggleButton selectedNavigationItem;
@FXML @FXML
public Pane contentPane; public Pane contentPane;
@ -75,14 +79,17 @@ public class MainController implements Initializable, NavigationController
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
public MainController(User user, WalletFacade walletFacade, MessageFacade messageFacade, Trading trading) public MainController(User user, WalletFacade walletFacade, MessageFacade messageFacade, Trading trading, Storage storage)
{ {
this.user = user; this.user = user;
this.walletFacade = walletFacade; this.walletFacade = walletFacade;
this.messageFacade = messageFacade; this.messageFacade = messageFacade;
this.trading = trading; this.trading = trading;
this.storage = storage;
MainController.mainController = this; MainController.mainController = this;
storageId = this.getClass().getName() + ".selectedNavigationItem";
} }
public static MainController getInstance() public static MainController getInstance()
@ -123,9 +130,14 @@ public class MainController implements Initializable, NavigationController
buildNavigation(); buildNavigation();
selectedNavigationItem = (ToggleButton) storage.read(storageId);
if (selectedNavigationItem == null)
selectedNavigationItem = homeButton;
selectedNavigationItem.fire();
//homeButton.fire(); //homeButton.fire();
//settingsButton.fire(); //settingsButton.fire();
fundsButton.fire(); //fundsButton.fire();
// sellButton.fire(); // sellButton.fire();
// ordersButton.fire(); // ordersButton.fire();
// homeButton.fire(); // homeButton.fire();

View file

@ -62,6 +62,8 @@ public class WithdrawalController implements Initializable, ChildController, Hib
public WithdrawalController(WalletFacade walletFacade) public WithdrawalController(WalletFacade walletFacade)
{ {
this.walletFacade = walletFacade; this.walletFacade = walletFacade;
if (walletFacade == null)
walletFacade = null;
} }

View file

@ -4,11 +4,11 @@
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?> <?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<VBox spacing="10" fx:controller="io.bitsquare.gui.funds.withdrawal.WithdrawalController" xmlns="http://javafx.com/javafx/8" <VBox spacing="10" fx:controller="io.bitsquare.gui.funds.withdrawal.WithdrawalController" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
xmlns:fx="http://javafx.com/fxml/1">
<padding> <padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding> </padding>
<TableView fx:id="tableView" VBox.vgrow="ALWAYS"> <TableView fx:id="tableView" VBox.vgrow="ALWAYS">
<columns> <columns>
<TableColumn text="Label" fx:id="labelColumn" minWidth="100" sortable="false"/> <TableColumn text="Label" fx:id="labelColumn" minWidth="100" sortable="false"/>
@ -53,9 +53,9 @@
<RowConstraints vgrow="NEVER"/> <RowConstraints vgrow="NEVER"/>
<RowConstraints vgrow="NEVER"/> <RowConstraints vgrow="NEVER"/>
<RowConstraints vgrow="NEVER"/> <RowConstraints vgrow="NEVER"/>
<RowConstraints vgrow="NEVER"/>
</rowConstraints> </rowConstraints>
</GridPane> </GridPane>
</VBox> </VBox>