use of com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex in project sparrow by sparrowwallet.
the class WalletNodeDao method addOrUpdate.
default void addOrUpdate(WalletNode addressNode, BlockTransactionHashIndex txo) {
Long spentById = null;
if (txo.isSpent()) {
BlockTransactionHashIndex spentBy = txo.getSpentBy();
if (spentBy.getId() == null) {
spentById = insertBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), truncate(spentBy.getLabel()), spentBy.getIndex(), spentBy.getValue(), spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId());
spentBy.setId(spentById);
} else {
updateBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), truncate(spentBy.getLabel()), spentBy.getIndex(), spentBy.getValue(), spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId(), spentBy.getId());
spentById = spentBy.getId();
}
}
if (txo.getId() == null) {
long txoId = insertBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), truncate(txo.getLabel()), txo.getIndex(), txo.getValue(), txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId());
txo.setId(txoId);
} else {
updateBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), truncate(txo.getLabel()), txo.getIndex(), txo.getValue(), txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId(), txo.getId());
}
}
use of com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex in project sparrow by sparrowwallet.
the class WalletNodeReducer method accumulate.
@Override
public void accumulate(Map<Long, WalletNode> map, RowView rowView) {
WalletNode walletNode = map.computeIfAbsent(rowView.getColumn("walletNode.id", Long.class), id -> rowView.getRow(WalletNode.class));
if (rowView.getColumn("walletNode.parent", Long.class) != null) {
WalletNode parentNode = map.get(rowView.getColumn("walletNode.parent", Long.class));
parentNode.getChildren().add(walletNode);
}
if (rowView.getColumn("blockTransactionHashIndex.node", Long.class) != null) {
BlockTransactionHashIndex blockTransactionHashIndex = rowView.getRow(BlockTransactionHashIndex.class);
if (rowView.getColumn("blockTransactionHashIndex.spentBy", Long.class) != null) {
BlockTransactionHashIndex spentBy = walletNode.getTransactionOutputs().stream().filter(ref -> ref.getId().equals(rowView.getColumn("blockTransactionHashIndex.spentBy", Long.class))).findFirst().orElseThrow();
blockTransactionHashIndex.setSpentBy(spentBy);
walletNode.getTransactionOutputs().remove(spentBy);
}
walletNode.getTransactionOutputs().add(blockTransactionHashIndex);
}
}
use of com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex in project sparrow by sparrowwallet.
the class WalletTransactionsEntry method updateTransactions.
public void updateTransactions() {
Map<HashIndex, BlockTransactionHashIndex> walletTxos = getWallet().getWalletTxos().entrySet().stream().collect(Collectors.toUnmodifiableMap(entry -> new HashIndex(entry.getKey().getHash(), entry.getKey().getIndex()), Map.Entry::getKey));
List<Entry> current = getWalletTransactions(getWallet()).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList());
List<Entry> previous = new ArrayList<>(getChildren());
List<Entry> entriesAdded = new ArrayList<>(current);
entriesAdded.removeAll(previous);
getChildren().addAll(entriesAdded);
List<Entry> entriesRemoved = new ArrayList<>(previous);
entriesRemoved.removeAll(current);
getChildren().removeAll(entriesRemoved);
calculateBalances(true);
List<Entry> entriesComplete = entriesAdded.stream().filter(txEntry -> ((TransactionEntry) txEntry).isComplete(walletTxos)).collect(Collectors.toList());
if (!entriesComplete.isEmpty()) {
EventManager.get().post(new NewWalletTransactionsEvent(getWallet(), entriesAdded.stream().map(entry -> (TransactionEntry) entry).collect(Collectors.toList())));
}
if (entriesAdded.size() > entriesComplete.size()) {
entriesAdded.removeAll(entriesComplete);
for (Entry entry : entriesAdded) {
TransactionEntry txEntry = (TransactionEntry) entry;
getChildren().remove(txEntry);
log.warn("Removing and not notifying incomplete entry " + ((TransactionEntry) entry).getBlockTransaction().getHashAsString() + " value " + txEntry.getValue() + " children " + entry.getChildren().stream().map(e -> e.getEntryType() + " " + ((HashIndexEntry) e).getHashIndex()).collect(Collectors.toList()));
}
}
}
use of com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex in project sparrow by sparrowwallet.
the class CounterpartyController method startCounterpartyCollaboration.
private void startCounterpartyCollaboration(SparrowCahootsWallet counterpartyCahootsWallet, PaymentCode initiatorPaymentCode, CahootsType cahootsType) {
sorobanProgressLabel.setText("Creating mix transaction...");
Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId);
Map<BlockTransactionHashIndex, WalletNode> walletUtxos = wallet.getWalletUtxos();
for (Map.Entry<BlockTransactionHashIndex, WalletNode> entry : walletUtxos.entrySet()) {
if (entry.getKey().getStatus() != Status.FROZEN) {
counterpartyCahootsWallet.addUtxo(entry.getValue(), wallet.getWalletTransaction(entry.getKey().getHash()), (int) entry.getKey().getIndex());
}
}
try {
SorobanCahootsService sorobanCahootsService = soroban.getSorobanCahootsService(counterpartyCahootsWallet);
CahootsContext cahootsContext = cahootsType == CahootsType.STONEWALLX2 ? CahootsContext.newCounterpartyStonewallx2() : CahootsContext.newCounterpartyStowaway();
sorobanCahootsService.contributor(counterpartyCahootsWallet.getAccount(), cahootsContext, initiatorPaymentCode, TIMEOUT_MS).subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform()).subscribe(sorobanMessage -> {
OnlineCahootsMessage cahootsMessage = (OnlineCahootsMessage) sorobanMessage;
if (cahootsMessage != null) {
Cahoots cahoots = cahootsMessage.getCahoots();
sorobanProgressBar.setProgress((double) (cahoots.getStep() + 1) / 5);
if (cahoots.getStep() == 3) {
sorobanProgressLabel.setText("Your mix partner is reviewing the transaction...");
step3Timer.start();
} else if (cahoots.getStep() >= 4) {
try {
Transaction transaction = getTransaction(cahoots);
if (transaction != null) {
transactionProperty.set(transaction);
updateTransactionDiagram(transactionDiagram, wallet, null, transaction);
next();
}
} catch (PSBTParseException e) {
log.error("Invalid collaborative PSBT created", e);
step3Desc.setText("Invalid transaction created.");
sorobanProgressLabel.setVisible(false);
}
}
}
}, error -> {
log.error("Error creating mix transaction", error);
String cutFrom = "Exception: ";
int index = error.getMessage().lastIndexOf(cutFrom);
String msg = index < 0 ? error.getMessage() : error.getMessage().substring(index + cutFrom.length());
msg = msg.replace("#Cahoots", "mix transaction");
step3Desc.setText(msg);
sorobanProgressLabel.setVisible(false);
});
} catch (Exception e) {
log.error("Error creating mix transaction", e);
sorobanProgressLabel.setText(e.getMessage());
}
}
use of com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex in project sparrow by sparrowwallet.
the class WalletTransactionsEntry method getWalletTransactions.
private static void getWalletTransactions(Wallet wallet, Map<BlockTransaction, WalletTransaction> walletTransactionMap, WalletNode purposeNode) {
KeyPurpose keyPurpose = purposeNode.getKeyPurpose();
List<WalletNode> childNodes = new ArrayList<>(purposeNode.getChildren());
for (WalletNode addressNode : childNodes) {
for (BlockTransactionHashIndex hashIndex : addressNode.getTransactionOutputs()) {
BlockTransaction inputTx = wallet.getWalletTransaction(hashIndex.getHash());
// A null inputTx here means the wallet is still updating - ignore as the WalletHistoryChangedEvent will run this again
if (inputTx != null) {
WalletTransaction inputWalletTx = walletTransactionMap.get(inputTx);
if (inputWalletTx == null) {
inputWalletTx = new WalletTransaction(wallet, inputTx);
walletTransactionMap.put(inputTx, inputWalletTx);
}
inputWalletTx.incoming.put(hashIndex, keyPurpose);
if (hashIndex.getSpentBy() != null) {
BlockTransaction outputTx = wallet.getWalletTransaction(hashIndex.getSpentBy().getHash());
if (outputTx != null) {
WalletTransaction outputWalletTx = walletTransactionMap.get(outputTx);
if (outputWalletTx == null) {
outputWalletTx = new WalletTransaction(wallet, outputTx);
walletTransactionMap.put(outputTx, outputWalletTx);
}
outputWalletTx.outgoing.put(hashIndex.getSpentBy(), keyPurpose);
}
}
}
}
}
}
Aggregations