From 5d9d7a9a3d5ea479e816781807f4c69f92e2b9e5 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sat, 17 May 2014 11:08:22 +0200 Subject: [PATCH] payment process cleanup --- src/main/java/io/bitsquare/btc/Fees.java | 4 +- .../java/io/bitsquare/btc/WalletFacade.java | 687 +++++++----------- .../bitsquare/gui/funds/FundsController.java | 2 +- .../bitsquare/gui/setup/SetupController.java | 14 +- .../trade/offerer/OffererPaymentProtocol.java | 13 +- .../trade/taker/TakerPaymentProtocol.java | 12 +- 6 files changed, 261 insertions(+), 471 deletions(-) diff --git a/src/main/java/io/bitsquare/btc/Fees.java b/src/main/java/io/bitsquare/btc/Fees.java index fc95a97bf2..f4a1f8ca4d 100644 --- a/src/main/java/io/bitsquare/btc/Fees.java +++ b/src/main/java/io/bitsquare/btc/Fees.java @@ -9,7 +9,7 @@ public class Fees { // min dust value lead to exception at for non standard to address pay scripts, so we use a value >= 7860 instead public static BigInteger MS_TX_FEE = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; - public static BigInteger ACCOUNT_REGISTRATION_FEE = Utils.toNanoCoins("0.01").subtract(Transaction.MIN_NONDUST_OUTPUT).subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); - public static BigInteger OFFER_CREATION_FEE = Utils.toNanoCoins("0.001").subtract(Transaction.MIN_NONDUST_OUTPUT).subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); + public static BigInteger ACCOUNT_REGISTRATION_FEE = Utils.toNanoCoins("0.01"); + public static BigInteger OFFER_CREATION_FEE = Utils.toNanoCoins("0.001"); public static BigInteger OFFER_TAKER_FEE = OFFER_CREATION_FEE; } diff --git a/src/main/java/io/bitsquare/btc/WalletFacade.java b/src/main/java/io/bitsquare/btc/WalletFacade.java index 8b717b9322..c2c521c8de 100644 --- a/src/main/java/io/bitsquare/btc/WalletFacade.java +++ b/src/main/java/io/bitsquare/btc/WalletFacade.java @@ -28,12 +28,7 @@ import java.util.concurrent.ExecutionException; import static com.google.bitcoin.script.ScriptOpCodes.OP_RETURN; -/** - * That facade delivers wallet functionality from the bitcoinJ library - * 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 implements WalletEventListener +public class WalletFacade { public static final String MAIN_NET = "MAIN_NET"; public static final String TEST_NET = "TEST_NET"; @@ -41,22 +36,13 @@ public class WalletFacade implements WalletEventListener public static String WALLET_PREFIX = BitSquare.ID; - // for testing trade process between offerer and taker - //public static String WALLET_PREFIX = "offerer"; // offerer - //public static String WALLET_PREFIX = "taker"; // offerer - private static final Logger log = LoggerFactory.getLogger(WalletFacade.class); private NetworkParameters params; private WalletAppKit walletAppKit; private CryptoFacade cryptoFacade; - // that wallet is used only for the registration process - //private AccountRegistrationWallet accountRegistrationWallet = null; - private List downloadListeners = new ArrayList<>(); - private List walletListeners = new ArrayList<>(); - private Wallet wallet; private ECKey registrationKey; @@ -115,30 +101,20 @@ public class WalletFacade implements WalletEventListener tradingKey = new ECKey(); wallet.addKey(tradingKey); - log.info("getConnectedPeers " + walletAppKit.peerGroup().getConnectedPeers().size()); wallet.allowSpendingUnconfirmedTransactions(); // walletAppKit.peerGroup().setMaxConnections(10); + //TODO for regtest set 1 walletAppKit.peerGroup().setMinBroadcastConnections(1); - wallet.addEventListener(this); - wallet.addWatchedAddress(tradingKey.toAddress(params)); - - // testTradeProcessDepositTx(); - // testTradeProcessPayOutTx(); + //wallet.addWatchedAddress(tradingKey.toAddress(params)); } public void shutDown() { - /*if (accountRegistrationWallet != null) - accountRegistrationWallet.shutDown(); */ walletAppKit.stopAsync(); walletAppKit.awaitTerminated(); } - public void updateWallet() - { - - } /////////////////////////////////////////////////////////////////////////////////////////// // Listener @@ -154,26 +130,6 @@ public class WalletFacade implements WalletEventListener 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); - } */ - /////////////////////////////////////////////////////////////////////////////////////////// // Trading wallet @@ -184,25 +140,70 @@ public class WalletFacade implements WalletEventListener return wallet.getBalance(Wallet.BalanceType.ESTIMATED); } - public String getAddressAsString() + public String getTradingAddress() { return tradingKey.toAddress(params).toString(); } - public String payOfferFee(BigInteger fee, FutureCallback callback) throws InsufficientMoneyException + public Wallet getWallet() { - Transaction tx = new Transaction(params); - //TransactionOutput output = new TransactionOutput(params, tx, Transaction.MIN_NONDUST_OUTPUT, WalletUtil.getEmptyOP_RETURNScript()); - tx.addOutput(Transaction.MIN_NONDUST_OUTPUT, WalletUtil.getEmptyOP_RETURNScript()); - Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tx); + return wallet; + } + + public String getPubKeyAsHex() + { + return Utils.bytesToHexString(tradingKey.getPubKey()); + } + + + /////////////////////////////////////////////////////////////////////////////////////////// + // Account registration + /////////////////////////////////////////////////////////////////////////////////////////// + + public Address getRegistrationAddress() + { + return registrationKey.toAddress(params); + } + + public ECKey getRegistrationKey() + { + return registrationKey; + } + + //TODO assume for now that there is only reg address funding, so total wallet balance = reg. balance + public BigInteger getRegistrationBalance() + { + return wallet.getBalance(Wallet.BalanceType.ESTIMATED); + } + + public void publishRegistrationTxWithExtraData(String stringifiedBankAccounts) throws InsufficientMoneyException + { + byte[] dataToEmbed = cryptoFacade.getEmbeddedAccountRegistrationData(registrationKey, stringifiedBankAccounts); + Script script = new ScriptBuilder().op(OP_RETURN).data(dataToEmbed).build(); + Transaction transaction = new Transaction(params); + TransactionOutput dataOutput = new TransactionOutput(params, transaction, Transaction.MIN_NONDUST_OUTPUT, script.getProgram()); + transaction.addOutput(dataOutput); + Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(transaction); // give fee to miners yet. Later it could be spent to other traders via lottery... - sendRequest.fee = fee; - + sendRequest.fee = Fees.ACCOUNT_REGISTRATION_FEE; Wallet.SendResult sendResult = wallet.sendCoins(sendRequest); - Futures.addCallback(sendResult.broadcastComplete, callback); + log.debug("Registration transaction: " + transaction.toString()); + //TODO + Futures.addCallback(sendResult.broadcastComplete, new FutureCallback() + { + @Override + public void onSuccess(Transaction result) + { + log.debug("sendResult onSuccess:" + result.toString()); + } - return tx.getHashAsString(); + @Override + public void onFailure(Throwable t) + { + log.error("sendResult onFailure:" + t.toString()); + } + }); } @@ -210,6 +211,22 @@ public class WalletFacade implements WalletEventListener // Trade process /////////////////////////////////////////////////////////////////////////////////////////// + public String payOfferFee(BigInteger fee, FutureCallback callback) throws InsufficientMoneyException + { + log.debug("payOfferFee fee=" + Utils.bitcoinValueToFriendlyString(fee)); + Transaction tx = new Transaction(params); + tx.addOutput(Transaction.MIN_NONDUST_OUTPUT, WalletUtil.getEmptyOP_RETURNScript()); + Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tx); + + sendRequest.fee = fee.subtract(Transaction.MIN_NONDUST_OUTPUT).subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); + + Wallet.SendResult sendResult = wallet.sendCoins(sendRequest); + Futures.addCallback(sendResult.broadcastComplete, callback); + + log.debug("tx=" + tx.toString()); + return tx.getHashAsString(); + } + public int getNumOfPeersSeenTx(String txID) { // TODO check from blockchain @@ -217,344 +234,78 @@ public class WalletFacade implements WalletEventListener return 3; } - /////////////////////////////////////////////////////////////////////////////////////////// - // Account registration - /////////////////////////////////////////////////////////////////////////////////////////// - - public Address getAccountRegistrationAddress() - { - return registrationKey.toAddress(params); - } - - /* public String getAccountRegistrationPubKey() - { - return Utils.bytesToHexString(getAccountRegistrationKey().getPubKey()); - } */ - - public BigInteger getAccountRegistrationBalance() - { - return wallet.getBalance(Wallet.BalanceType.ESTIMATED); - // return getAccountRegistrationWallet().getBalance(Wallet.BalanceType.ESTIMATED); - } - - public void sendRegistrationTx(String stringifiedBankAccounts) throws InsufficientMoneyException - { - saveToBlockchain(cryptoFacade.getEmbeddedAccountRegistrationData(registrationKey, stringifiedBankAccounts)); - } - - public ECKey getRegistrationKey() - { - return registrationKey; - } - - void saveToBlockchain(byte[] dataToEmbed) throws InsufficientMoneyException - { - Script script = new ScriptBuilder() - .op(OP_RETURN) - .data(dataToEmbed) - .build(); - Transaction transaction = new Transaction(params); - TransactionOutput dataOutput = new TransactionOutput(params, - transaction, - Transaction.MIN_NONDUST_OUTPUT, - script.getProgram()); - transaction.addOutput(dataOutput); - Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(transaction); - - // give fee to miners yet. Later it could be spent to other traders via lottery... - sendRequest.fee = Fees.ACCOUNT_REGISTRATION_FEE; - Wallet.SendResult sendResult = null; - sendResult = wallet.sendCoins(sendRequest); - log.info("Registration transaction: " + transaction.toString()); - //TODO - Futures.addCallback(sendResult.broadcastComplete, new FutureCallback() - { - @Override - public void onSuccess(Transaction result) - { - log.info("sendResult onSuccess:" + result.toString()); - // Platform.runLater(overlayUi::done); - } - - @Override - public void onFailure(Throwable t) - { - log.warn("sendResult onFailure:" + t.toString()); - // We died trying to empty the wallet. - // crashAlert(t); - } - }); - - //TODO - sendResult.tx.getConfidence().addEventListener((tx, reason) -> { - //if (reason == TransactionConfidence.Listener.ChangeReason.SEEN_PEERS) - //updateTitleForBroadcast(); - }); - } - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Getter - /////////////////////////////////////////////////////////////////////////////////////////// - - /*public int getRegConfNumBroadcastPeers() - { - return getAccountRegistrationWallet().getConfNumBroadcastPeers(); - } - - public int getRegConfDepthInBlocks() - { - return WalletUtil.getConfDepthInBlocks(getAccountRegistrationWallet()); - } */ - - public Wallet getWallet() - { - return wallet; - } - - /////////////////////////////////////////////////////////////////////////////////////////// - // Interface implementation: 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(), WalletUtil.getConfDepthInBlocks(wallet)); - - // 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