use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class CreateAndSignContract method run.
@Override
protected void run() {
try {
runInterceptHook();
checkNotNull(trade.getTakeOfferFeeTxId(), "trade.getTakeOfferFeeTxId() must not be null");
TradingPeer taker = processModel.tradingPeer;
PaymentAccountContractData offererPaymentAccountContractData = processModel.getPaymentAccountContractData(trade);
checkNotNull(offererPaymentAccountContractData, "offererPaymentAccountContractData must not be null");
PaymentAccountContractData takerPaymentAccountContractData = taker.getPaymentAccountContractData();
boolean isBuyerOffererAndSellerTaker = trade instanceof BuyerAsOffererTrade;
NodeAddress buyerNodeAddress = isBuyerOffererAndSellerTaker ? processModel.getMyNodeAddress() : processModel.getTempTradingPeerNodeAddress();
NodeAddress sellerNodeAddress = isBuyerOffererAndSellerTaker ? processModel.getTempTradingPeerNodeAddress() : processModel.getMyNodeAddress();
log.debug("isBuyerOffererAndSellerTaker " + isBuyerOffererAndSellerTaker);
log.debug("buyerAddress " + buyerNodeAddress);
log.debug("sellerAddress " + sellerNodeAddress);
WalletService walletService = processModel.getWalletService();
AddressEntry takerAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
AddressEntry offererAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
Contract contract = new Contract(processModel.getOffer(), trade.getTradeAmount(), trade.getTradePrice(), trade.getTakeOfferFeeTxId(), buyerNodeAddress, sellerNodeAddress, trade.getArbitratorNodeAddress(), isBuyerOffererAndSellerTaker, processModel.getAccountId(), taker.getAccountId(), offererPaymentAccountContractData, takerPaymentAccountContractData, processModel.getPubKeyRing(), taker.getPubKeyRing(), takerAddressEntry.getAddressString(), taker.getPayoutAddressString(), offererAddressEntry.getPubKey(), taker.getMultiSigPubKey());
String contractAsJson = Utilities.objectToJson(contract);
String signature = Sig.sign(processModel.getKeyRing().getSignatureKeyPair().getPrivate(), contractAsJson);
trade.setContract(contract);
trade.setContractAsJson(contractAsJson);
trade.setOffererContractSignature(signature);
complete();
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class CreateOfferFeeTx method run.
@Override
protected void run() {
try {
runInterceptHook();
NodeAddress selectedArbitratorNodeAddress = ArbitrationSelectionRule.select(model.user.getAcceptedArbitratorAddresses(), model.offer);
log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
Arbitrator selectedArbitrator = model.user.getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateOfferFeeTx");
WalletService walletService = model.walletService;
String id = model.offer.getId();
Address fundingAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING).getAddress();
Address reservedForTradeAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
Transaction transaction = model.tradeWalletService.createTradingFeeTx(fundingAddress, reservedForTradeAddress, changeAddress, model.reservedFundsForOffer, model.useSavingsWallet, FeePolicy.getCreateOfferFee(), selectedArbitrator.getBtcAddress());
// We assume there will be no tx malleability. We add a check later in case the published offer has a different hash.
// As the txId is part of the offer and therefore change the hash data we need to be sure to have no
// tx malleability
model.offer.setOfferFeePaymentTxID(transaction.getHashAsString());
model.setTransaction(transaction);
complete();
} catch (Throwable t) {
model.offer.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class SendFinalizePayoutTxRequest method run.
@Override
protected void run() {
try {
runInterceptHook();
if (trade.getTradingPeerNodeAddress() != null) {
WalletService walletService = processModel.getWalletService();
AddressEntry sellerPayoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
FinalizePayoutTxRequest message = new FinalizePayoutTxRequest(processModel.getId(), processModel.getPayoutTxSignature(), sellerPayoutAddressEntry.getAddressString(), trade.getLockTimeAsBlockHeight(), processModel.getMyNodeAddress());
processModel.getP2PService().sendEncryptedMailboxMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), message, new SendMailboxMessageListener() {
@Override
public void onArrived() {
log.trace("Message arrived at peer.");
trade.setState(Trade.State.SELLER_SENT_FIAT_PAYMENT_RECEIPT_MSG);
complete();
}
@Override
public void onStoredInMailbox() {
log.trace("Message stored in mailbox.");
trade.setState(Trade.State.SELLER_SENT_FIAT_PAYMENT_RECEIPT_MSG);
complete();
}
@Override
public void onFault(String errorMessage) {
appendToErrorMessage("FinalizePayoutTxRequest sending failed. errorMessage=" + errorMessage);
failed(errorMessage);
}
});
} else {
log.error("trade.getTradingPeerAddress() = " + trade.getTradingPeerNodeAddress());
failed("A needed dependency is null");
}
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.btc.WalletService in project bitsquare by bitsquare.
the class SignAndPublishDepositTxAsSeller 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> sellerInputs = processModel.getRawTransactionInputs();
WalletService walletService = processModel.getWalletService();
AddressEntry sellerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
sellerMultiSigAddressEntry.setCoinLockedInMultiSig(Coin.valueOf(sellerInputs.stream().mapToLong(input -> input.value).sum()).subtract(FeePolicy.getFixedTxFeeForTrades(trade.getOffer())));
TradingPeer tradingPeer = processModel.tradingPeer;
Transaction depositTx = processModel.getTradeWalletService().takerSignsAndPublishesDepositTx(true, contractHash, processModel.getPreparedDepositTx(), tradingPeer.getRawTransactionInputs(), sellerInputs, tradingPeer.getMultiSigPubKey(), sellerMultiSigAddressEntry.getPubKey(), 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 SignPayoutTx method run.
@Override
protected void run() {
try {
runInterceptHook();
checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
checkNotNull(trade.getDepositTx(), "trade.getDepositTx() must not be null");
Coin sellerPayoutAmount = FeePolicy.getSecurityDeposit(trade.getOffer());
Coin buyerPayoutAmount = sellerPayoutAmount.add(trade.getTradeAmount());
// We use the sellers LastBlockSeenHeight, which might be different to the buyers one.
// If lock time is 0 we set lockTimeAsBlockHeight to 0 to mark it as "not set".
// In the tradeWallet we apply the lockTime only if it is set, otherwise we use the default values for
// transaction lockTime and sequence number
long lockTime = trade.getOffer().getPaymentMethod().getLockTime();
long lockTimeAsBlockHeight = 0;
if (lockTime > 0)
lockTimeAsBlockHeight = processModel.getTradeWalletService().getLastBlockSeenHeight() + lockTime;
trade.setLockTimeAsBlockHeight(lockTimeAsBlockHeight);
String id = processModel.getOffer().getId();
WalletService walletService = processModel.getWalletService();
AddressEntry sellerPayoutAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT);
AddressEntry multiSigAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
byte[] payoutTxSignature = processModel.getTradeWalletService().sellerSignsPayoutTx(trade.getDepositTx(), buyerPayoutAmount, sellerPayoutAmount, processModel.tradingPeer.getPayoutAddressString(), sellerPayoutAddressEntry.getAddressString(), multiSigAddressEntry.getKeyPair(), lockTimeAsBlockHeight, processModel.tradingPeer.getMultiSigPubKey(), multiSigAddressEntry.getPubKey(), trade.getArbitratorPubKey());
processModel.setPayoutTxSignature(payoutTxSignature);
complete();
} catch (Throwable t) {
failed(t);
}
}
Aggregations