use of org.bitcoinj.core.Address in project bisq-desktop by bisq-network.
the class MainViewModel method updateReservedBalance.
private void updateReservedBalance() {
Coin sum = Coin.valueOf(openOfferManager.getObservableList().stream().map(openOffer -> {
final Optional<AddressEntry> addressEntryOptional = btcWalletService.getAddressEntry(openOffer.getId(), AddressEntry.Context.RESERVED_FOR_TRADE);
if (addressEntryOptional.isPresent()) {
Address address = addressEntryOptional.get().getAddress();
return btcWalletService.getBalanceForAddress(address);
} else {
return null;
}
}).filter(e -> e != null).mapToLong(Coin::getValue).sum());
reservedBalance.set(formatter.formatCoinWithCode(sum));
}
use of org.bitcoinj.core.Address in project bisq-core by bisq-network.
the class CreateMakerFeeTx method run.
@Override
protected void run() {
Offer offer = model.getOffer();
try {
runInterceptHook();
String id = offer.getId();
BtcWalletService walletService = model.getWalletService();
NodeAddress selectedArbitratorNodeAddress = ArbitratorSelectionRule.select(model.getUser().getAcceptedArbitratorAddresses(), model.getOffer());
log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
Arbitrator selectedArbitrator = model.getUser().getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateOfferFeeTx");
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();
final TradeWalletService tradeWalletService = model.getTradeWalletService();
if (offer.isCurrencyForMakerFeeBtc()) {
tradeFeeTx = tradeWalletService.createBtcTradingFeeTx(fundingAddress, reservedForTradeAddress, changeAddress, model.getReservedFundsForOffer(), model.isUseSavingsWallet(), offer.getMakerFee(), offer.getTxFee(), selectedArbitrator.getBtcAddress(), new FutureCallback<Transaction>() {
@Override
public void onSuccess(Transaction transaction) {
// we delay one render frame to be sure we don't get called before the method call has
// returned (tradeFeeTx would be null in that case)
UserThread.execute(() -> {
if (!completed) {
offer.setOfferFeePaymentTxId(transaction.getHashAsString());
model.setTransaction(transaction);
walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
model.getOffer().setState(Offer.State.OFFER_FEE_PAID);
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().");
}
}
});
} else {
final BsqWalletService bsqWalletService = model.getBsqWalletService();
Transaction preparedBurnFeeTx = model.getBsqWalletService().getPreparedBurnFeeTx(offer.getMakerFee());
Transaction txWithBsqFee = tradeWalletService.completeBsqTradingFeeTx(preparedBurnFeeTx, fundingAddress, reservedForTradeAddress, changeAddress, model.getReservedFundsForOffer(), model.isUseSavingsWallet(), offer.getTxFee());
Transaction signedTx = model.getBsqWalletService().signTx(txWithBsqFee);
WalletService.checkAllScriptSignaturesForTx(signedTx);
bsqWalletService.commitTx(signedTx);
// We need to create another instance, otherwise the tx would trigger an invalid state exception
// if it gets committed 2 times
tradeWalletService.commitTx(tradeWalletService.getClonedTransaction(signedTx));
bsqWalletService.broadcastTx(signedTx, new FutureCallback<Transaction>() {
@Override
public void onSuccess(@Nullable Transaction transaction) {
if (transaction != null) {
offer.setOfferFeePaymentTxId(transaction.getHashAsString());
model.setTransaction(transaction);
log.debug("onSuccess, offerId={}, OFFER_FUNDING", id);
walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
log.debug("Successfully sent tx with id " + transaction.getHashAsString());
model.getOffer().setState(Offer.State.OFFER_FEE_PAID);
complete();
}
}
@Override
public void onFailure(@NotNull Throwable t) {
log.error(t.toString());
t.printStackTrace();
offer.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
failed(t);
}
});
}
} catch (Throwable t) {
offer.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
failed(t);
}
}
use of org.bitcoinj.core.Address in project sentinel-android by Samourai-Wallet.
the class FormatsUtil method isValidBitcoinAddress.
public boolean isValidBitcoinAddress(final String address) {
boolean ret = false;
Address addr = null;
if (address.toLowerCase().startsWith("bc")) {
try {
Pair<Byte, byte[]> pair = Bech32Segwit.decode(address.substring(0, 2), address);
if (pair.getLeft() == null || pair.getRight() == null) {
;
} else {
ret = true;
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
addr = new Address(MainNetParams.get(), address);
if (addr != null) {
ret = true;
}
} catch (WrongNetworkException wne) {
ret = false;
} catch (AddressFormatException afe) {
ret = false;
}
}
return ret;
}
use of org.bitcoinj.core.Address in project catena-java by alinush.
the class SimpleWallet method getLastUtxo.
/**
* Returns the last UTXO in the Catena chain used to fund and link the next TX
* that commits the next statement.
* If there is more than one UTXO, a warning is issued, since that case should never arise.
*/
public TransactionOutput getLastUtxo() {
List<TransactionOutput> utxos = getUnspents();
ArrayList<TransactionOutput> chainKeyUtxos = new ArrayList<TransactionOutput>();
Address addr = getChainAddress();
for (TransactionOutput o : utxos) {
if (o.isMineOrWatched(this) && o.getAddressFromP2PKHScript(params).equals(addr)) {
chainKeyUtxos.add(o);
} else {
log.trace("Ineligible UTXO (mine=" + o.isMineOrWatched(this) + ", to=" + o.getAddressFromP2PKHScript(params) + ", chainAddr=" + addr + "): " + o);
}
}
// There should be exactly one Catena UTXO
if (chainKeyUtxos.size() > 1) {
log.warn("More than one UTXO spendable by '" + addr + "': " + chainKeyUtxos.size());
}
if (chainKeyUtxos.isEmpty()) {
throw new RuntimeException("No UTXOs spendable by '" + addr + "'");
}
// Make sure the UTXO we found is from a Catena TX: does not check sigs,
// checks the address in the TX's output is the right one, does not check the previous link
TransactionOutput lastCatenaUtxo = chainKeyUtxos.get(0);
if (CatenaUtils.maybeCatenaTx(lastCatenaUtxo.getParentTransaction(), addr) == false) {
throw new RuntimeException("Expected to get an UTXO of a Catena TX.");
}
return lastCatenaUtxo;
}
use of org.bitcoinj.core.Address in project catena-java by alinush.
the class SimpleWallet method sendCatenaTxOffline.
/**
* A modified sendCoinsOffline, for Catena TXs purposes.
*
* @param request
* @param commit
* @return
* @throws InsufficientMoneyException
*/
public Transaction sendCatenaTxOffline(SendRequest request, boolean isRootOfTrustTxn, boolean commit) throws InsufficientMoneyException {
// First we check a properly-structured Catena TX was given to us
Address addr = request.tx.getOutput(0).getAddressFromP2PKHScript(request.tx.getParams());
TransactionOutput utxo = request.tx.getInput(0).getConnectedOutput();
// We don't have a signature yet (completeCatenaTx() call below will sign)
if (!CatenaUtils.isCatenaTxNoSig(request.tx, addr, utxo, isRootOfTrustTxn == false)) {
throw new RuntimeException("You must provide a Catena TX as input to this method, not: " + request.tx);
}
lock.lock();
try {
// Modified Wallet::completeTx and got rid of unnecessary complexities
completeCatenaTx(request);
// much when funding it and computing its tx fee.
if (!CatenaUtils.isSignedCatenaTx(request.tx, addr, utxo, isRootOfTrustTxn == false)) {
throw new RuntimeException("bitcoinj meddled with the Catena tx too much: " + request.tx);
}
// When generating lies, for testing code, we don't commit the lies because otherwise we can't double-spend.
if (commit)
commitTx(request.tx);
return request.tx;
} finally {
lock.unlock();
}
}
Aggregations