use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class SendPublishDepositTxRequest method run.
@Override
protected void run() {
try {
runInterceptHook();
WalletService walletService = processModel.getWalletService();
AddressEntry offererMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
AddressEntry offererPayoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
PublishDepositTxRequest tradeMessage = new PublishDepositTxRequest(processModel.getId(), processModel.getPaymentAccountContractData(trade), processModel.getAccountId(), offererMultiSigAddressEntry.getPubKey(), trade.getContractAsJson(), trade.getOffererContractSignature(), offererPayoutAddressEntry.getAddressString(), processModel.getPreparedDepositTx(), processModel.getRawTransactionInputs());
processModel.getP2PService().sendEncryptedDirectMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), tradeMessage, new SendDirectMessageListener() {
@Override
public void onArrived() {
log.trace("Message arrived at peer.");
trade.setState(Trade.State.OFFERER_SENT_PUBLISH_DEPOSIT_TX_REQUEST);
complete();
}
@Override
public void onFault() {
appendToErrorMessage("PublishDepositTxRequest sending failed");
failed();
}
});
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class OffererCreatesAndSignsDepositTxAsBuyer method run.
@Override
protected void run() {
try {
runInterceptHook();
checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
Offer offer = trade.getOffer();
Coin securityDeposit = FeePolicy.getSecurityDeposit(offer);
Coin buyerInputAmount = securityDeposit.add(FeePolicy.getFixedTxFeeForTrades(offer));
Coin msOutputAmount = buyerInputAmount.add(securityDeposit).add(trade.getTradeAmount());
log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
byte[] contractHash = Hash.getHash(trade.getContractAsJson());
trade.setContractHash(contractHash);
WalletService walletService = processModel.getWalletService();
String id = processModel.getOffer().getId();
AddressEntry offererAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
AddressEntry buyerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
buyerMultiSigAddressEntry.setCoinLockedInMultiSig(buyerInputAmount.subtract(FeePolicy.getFixedTxFeeForTrades(offer)));
Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
PreparedDepositTxAndOffererInputs result = processModel.getTradeWalletService().offererCreatesAndSignsDepositTx(true, contractHash, buyerInputAmount, msOutputAmount, processModel.tradingPeer.getRawTransactionInputs(), processModel.tradingPeer.getChangeOutputValue(), processModel.tradingPeer.getChangeOutputAddress(), offererAddressEntry.getAddress(), changeAddress, buyerMultiSigAddressEntry.getPubKey(), processModel.tradingPeer.getMultiSigPubKey(), trade.getArbitratorPubKey());
processModel.setPreparedDepositTx(result.depositTransaction);
processModel.setRawTransactionInputs(result.rawOffererInputs);
complete();
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class SendFiatTransferStartedMessage method run.
@Override
protected void run() {
try {
runInterceptHook();
WalletService walletService = processModel.getWalletService();
AddressEntry payoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
processModel.getP2PService().sendEncryptedMailboxMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), new FiatTransferStartedMessage(processModel.getId(), payoutAddressEntry.getAddressString(), processModel.getMyNodeAddress()), new SendMailboxMessageListener() {
@Override
public void onArrived() {
log.debug("Message arrived at peer.");
trade.setState(Trade.State.BUYER_SENT_FIAT_PAYMENT_INITIATED_MSG);
complete();
}
@Override
public void onStoredInMailbox() {
log.debug("Message stored in mailbox.");
trade.setState(Trade.State.BUYER_SENT_FIAT_PAYMENT_INITIATED_MSG);
complete();
}
@Override
public void onFault(String errorMessage) {
appendToErrorMessage("FiatTransferStartedMessage sending failed");
failed(errorMessage);
}
});
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class SignAndPublishDepositTxAsBuyer method run.
@Override
protected void run() {
try {
runInterceptHook();
log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
byte[] contractHash = Hash.getHash(trade.getContractAsJson());
trade.setContractHash(contractHash);
ArrayList<RawTransactionInput> buyerInputs = processModel.getRawTransactionInputs();
WalletService walletService = processModel.getWalletService();
AddressEntry buyerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
Coin buyerInput = Coin.valueOf(buyerInputs.stream().mapToLong(input -> input.value).sum());
buyerMultiSigAddressEntry.setCoinLockedInMultiSig(buyerInput.subtract(FeePolicy.getFixedTxFeeForTrades(trade.getOffer())));
TradingPeer tradingPeer = processModel.tradingPeer;
Transaction depositTx = processModel.getTradeWalletService().takerSignsAndPublishesDepositTx(false, contractHash, processModel.getPreparedDepositTx(), buyerInputs, tradingPeer.getRawTransactionInputs(), buyerMultiSigAddressEntry.getPubKey(), tradingPeer.getMultiSigPubKey(), trade.getArbitratorPubKey(), new FutureCallback<Transaction>() {
@Override
public void onSuccess(Transaction transaction) {
log.trace("takerSignAndPublishTx succeeded " + transaction);
trade.setDepositTx(transaction);
trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
complete();
}
@Override
public void onFailure(@NotNull Throwable t) {
failed(t);
}
});
trade.setDepositTx(depositTx);
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class SetupDepositBalanceListener method run.
@Override
protected void run() {
try {
runInterceptHook();
WalletService walletService = processModel.getWalletService();
Address address = walletService.getOrCreateAddressEntry(trade.getId(), AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
balanceListener = new BalanceListener(address) {
@Override
public void onBalanceChanged(Coin balance, Transaction tx) {
updateBalance(balance);
}
};
walletService.addBalanceListener(balanceListener);
tradeStateSubscription = EasyBind.subscribe(trade.stateProperty(), newValue -> {
log.debug("tradeStateSubscription newValue " + newValue);
if (newValue == Trade.State.OFFERER_RECEIVED_DEPOSIT_TX_PUBLISHED_MSG || newValue == Trade.State.DEPOSIT_SEEN_IN_NETWORK) {
walletService.removeBalanceListener(balanceListener);
UserThread.execute(this::unSubscribe);
}
});
updateBalance(walletService.getBalanceForAddress(address));
// we complete immediately, our object stays alive because the balanceListener is stored in the WalletService
complete();
} catch (Throwable t) {
failed(t);
}
}
Aggregations