use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.
the class SellerAsTakerSignAndPublishDepositTx method run.
@Override
protected void run() {
try {
runInterceptHook();
log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
byte[] contractHash = Hash.getSha256Hash(trade.getContractAsJson());
trade.setContractHash(contractHash);
List<RawTransactionInput> sellerInputs = checkNotNull(processModel.getRawTransactionInputs(), "sellerInputs must not be null");
BtcWalletService walletService = processModel.getBtcWalletService();
String id = processModel.getOffer().getId();
Optional<AddressEntry> addressEntryOptional = walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG);
checkArgument(addressEntryOptional.isPresent(), "addressEntryOptional must be present");
AddressEntry sellerMultiSigAddressEntry = addressEntryOptional.get();
byte[] sellerMultiSigPubKey = processModel.getMyMultiSigPubKey();
checkArgument(Arrays.equals(sellerMultiSigPubKey, sellerMultiSigAddressEntry.getPubKey()), "sellerMultiSigPubKey from AddressEntry must match the one from the trade data. trade id =" + id);
Coin sellerInput = Coin.valueOf(sellerInputs.stream().mapToLong(input -> input.value).sum());
sellerMultiSigAddressEntry.setCoinLockedInMultiSig(sellerInput.subtract(trade.getTxFee().multiply(2)));
walletService.saveAddressEntryList();
TradingPeer tradingPeer = processModel.getTradingPeer();
Transaction depositTx = processModel.getTradeWalletService().takerSignsAndPublishesDepositTx(true, contractHash, processModel.getPreparedDepositTx(), tradingPeer.getRawTransactionInputs(), sellerInputs, tradingPeer.getMultiSigPubKey(), sellerMultiSigPubKey, trade.getArbitratorBtcPubKey(), new FutureCallback<Transaction>() {
@Override
public void onSuccess(Transaction transaction) {
if (!completed) {
log.trace("takerSignAndPublishTx succeeded " + transaction);
trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
complete();
} else {
log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
}
}
@Override
public void onFailure(@NotNull Throwable t) {
if (!completed) {
failed(t);
} else {
log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
}
}
});
// We set the deposit tx in case we get the onFailure called.
trade.setDepositTx(depositTx);
} catch (Throwable t) {
final Contract contract = trade.getContract();
if (contract != null)
contract.printDiff(processModel.getTradingPeer().getContractAsJson());
failed(t);
}
}
use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.
the class TakerSendPayDepositRequest method run.
@Override
protected void run() {
try {
runInterceptHook();
checkNotNull(trade.getTradeAmount(), "TradeAmount must not be null");
checkNotNull(trade.getTakerFeeTxId(), "TakeOfferFeeTxId must not be null");
final User user = processModel.getUser();
checkNotNull(user, "User must not be null");
final List<NodeAddress> acceptedArbitratorAddresses = user.getAcceptedArbitratorAddresses();
final List<NodeAddress> acceptedMediatorAddresses = user.getAcceptedMediatorAddresses();
checkNotNull(acceptedArbitratorAddresses, "acceptedArbitratorAddresses must not be null");
checkNotNull(acceptedMediatorAddresses, "acceptedMediatorAddresses must not be null");
BtcWalletService walletService = processModel.getBtcWalletService();
String id = processModel.getOffer().getId();
checkArgument(!walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG).isPresent(), "addressEntry must not be set here.");
AddressEntry addressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
byte[] takerMultiSigPubKey = addressEntry.getPubKey();
processModel.setMyMultiSigPubKey(takerMultiSigPubKey);
AddressEntry takerPayoutAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT);
String takerPayoutAddressString = takerPayoutAddressEntry.getAddressString();
final String offerId = processModel.getOfferId();
// Taker has to use offerId as nonce (he cannot manipulate that - so we avoid to have a challenge protocol for passing the nonce we want to get signed)
// He cannot manipulate the offerId - so we avoid to have a challenge protocol for passing the nonce we want to get signed.
final PaymentAccountPayload paymentAccountPayload = checkNotNull(processModel.getPaymentAccountPayload(trade), "processModel.getPaymentAccountPayload(trade) must not be null");
byte[] sig = Sig.sign(processModel.getKeyRing().getSignatureKeyPair().getPrivate(), offerId.getBytes());
PayDepositRequest message = new PayDepositRequest(offerId, processModel.getMyNodeAddress(), trade.getTradeAmount().value, trade.getTradePrice().getValue(), trade.getTxFee().getValue(), trade.getTakerFee().getValue(), trade.isCurrencyForTakerFeeBtc(), processModel.getRawTransactionInputs(), processModel.getChangeOutputValue(), processModel.getChangeOutputAddress(), takerMultiSigPubKey, takerPayoutAddressString, processModel.getPubKeyRing(), paymentAccountPayload, processModel.getAccountId(), trade.getTakerFeeTxId(), new ArrayList<>(acceptedArbitratorAddresses), new ArrayList<>(acceptedMediatorAddresses), trade.getArbitratorNodeAddress(), trade.getMediatorNodeAddress(), UUID.randomUUID().toString(), Version.getP2PMessageVersion(), sig, new Date().getTime());
processModel.getP2PService().sendEncryptedDirectMessage(trade.getTradingPeerNodeAddress(), processModel.getTradingPeer().getPubKeyRing(), message, new SendDirectMessageListener() {
@Override
public void onArrived() {
log.debug("Message arrived at peer. tradeId={}, message{}", id, message);
complete();
}
@Override
public void onFault() {
appendToErrorMessage("Sending message failed: message=" + message + "\nerrorMessage=" + errorMessage);
failed();
}
});
} catch (Throwable t) {
failed(t);
}
}
use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.
the class TradableList method fromProto.
@Nullable
public static TradableList fromProto(PB.TradableList proto, CoreProtoResolver coreProtoResolver, Storage<TradableList<Tradable>> storage, BtcWalletService btcWalletService) {
log.debug("TradableList fromProto of {} ", proto);
List<Tradable> list = proto.getTradableList().stream().map(tradable -> {
switch(tradable.getMessageCase()) {
case OPEN_OFFER:
return OpenOffer.fromProto(tradable.getOpenOffer());
case BUYER_AS_MAKER_TRADE:
return BuyerAsMakerTrade.fromProto(tradable.getBuyerAsMakerTrade(), storage, btcWalletService, coreProtoResolver);
case BUYER_AS_TAKER_TRADE:
return BuyerAsTakerTrade.fromProto(tradable.getBuyerAsTakerTrade(), storage, btcWalletService, coreProtoResolver);
case SELLER_AS_MAKER_TRADE:
return SellerAsMakerTrade.fromProto(tradable.getSellerAsMakerTrade(), storage, btcWalletService, coreProtoResolver);
case SELLER_AS_TAKER_TRADE:
return SellerAsTakerTrade.fromProto(tradable.getSellerAsTakerTrade(), storage, btcWalletService, coreProtoResolver);
default:
log.error("Unknown messageCase. tradable.getMessageCase() = " + tradable.getMessageCase());
throw new ProtobufferException("Unknown messageCase. tradable.getMessageCase() = " + tradable.getMessageCase());
}
}).collect(Collectors.toList());
return new TradableList<>(storage, list);
}
use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.
the class BuyerAsMakerCreatesAndSignsDepositTx method run.
@Override
protected void run() {
try {
runInterceptHook();
checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
BtcWalletService walletService = processModel.getBtcWalletService();
String id = processModel.getOffer().getId();
TradingPeer tradingPeer = processModel.getTradingPeer();
final Offer offer = trade.getOffer();
// params
final boolean makerIsBuyer = true;
final byte[] contractHash = Hash.getSha256Hash(trade.getContractAsJson());
trade.setContractHash(contractHash);
log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
final Coin makerInputAmount = offer.getBuyerSecurityDeposit();
Optional<AddressEntry> addressEntryOptional = walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG);
checkArgument(addressEntryOptional.isPresent(), "addressEntryOptional must be present");
AddressEntry makerMultiSigAddressEntry = addressEntryOptional.get();
makerMultiSigAddressEntry.setCoinLockedInMultiSig(makerInputAmount);
walletService.saveAddressEntryList();
final Coin msOutputAmount = makerInputAmount.add(trade.getTxFee()).add(offer.getSellerSecurityDeposit()).add(trade.getTradeAmount());
final List<RawTransactionInput> takerRawTransactionInputs = tradingPeer.getRawTransactionInputs();
final long takerChangeOutputValue = tradingPeer.getChangeOutputValue();
final String takerChangeAddressString = tradingPeer.getChangeOutputAddress();
final Address makerAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
final Address makerChangeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
final byte[] buyerPubKey = processModel.getMyMultiSigPubKey();
checkArgument(Arrays.equals(buyerPubKey, makerMultiSigAddressEntry.getPubKey()), "buyerPubKey from AddressEntry must match the one from the trade data. trade id =" + id);
final byte[] sellerPubKey = tradingPeer.getMultiSigPubKey();
final byte[] arbitratorBtcPubKey = trade.getArbitratorBtcPubKey();
PreparedDepositTxAndMakerInputs result = processModel.getTradeWalletService().makerCreatesAndSignsDepositTx(makerIsBuyer, contractHash, makerInputAmount, msOutputAmount, takerRawTransactionInputs, takerChangeOutputValue, takerChangeAddressString, makerAddress, makerChangeAddress, buyerPubKey, sellerPubKey, arbitratorBtcPubKey);
processModel.setPreparedDepositTx(result.depositTransaction);
processModel.setRawTransactionInputs(result.rawMakerInputs);
complete();
} catch (Throwable t) {
failed(t);
}
}
use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.
the class BuyerAsTakerCreatesDepositTxInputs method run.
@Override
protected void run() {
try {
runInterceptHook();
// In case we pay the taker fee in bsq we reduce tx fee by that as the burned bsq satoshis goes to miners.
Coin bsqTakerFee = trade.isCurrencyForTakerFeeBtc() ? Coin.ZERO : trade.getTakerFee();
Coin txFee = trade.getTxFee();
Coin takerInputAmount = trade.getOffer().getBuyerSecurityDeposit().add(txFee).add(txFee).subtract(bsqTakerFee);
BtcWalletService walletService = processModel.getBtcWalletService();
Address takersAddress = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
Address takersChangeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
InputsAndChangeOutput result = processModel.getTradeWalletService().takerCreatesDepositsTxInputs(takerInputAmount, txFee.subtract(bsqTakerFee), takersAddress, takersChangeAddress);
processModel.setRawTransactionInputs(result.rawTransactionInputs);
processModel.setChangeOutputValue(result.changeOutputValue);
processModel.setChangeOutputAddress(result.changeOutputAddress);
complete();
} catch (Throwable t) {
failed(t);
}
}
Aggregations