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>
<version>1.50.0.0</version>
</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>
<groupId>net.sf.proguard</groupId>

View file

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

View file

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

View file

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

View file

@ -4,11 +4,11 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.*?>
<VBox spacing="10" fx:controller="io.bitsquare.gui.funds.withdrawal.WithdrawalController" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1">
<VBox spacing="10" fx:controller="io.bitsquare.gui.funds.withdrawal.WithdrawalController" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<TableView fx:id="tableView" VBox.vgrow="ALWAYS">
<columns>
<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>
</GridPane>
</VBox>