Search in sources :

Example 1 with BlockTransactionHashIndex

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());
    }
}
Also used : BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex)

Example 2 with BlockTransactionHashIndex

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);
    }
}
Also used : BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Example 3 with 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()));
        }
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) Wallet(com.sparrowwallet.drongo.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) LongProperty(javafx.beans.property.LongProperty) Config(com.sparrowwallet.sparrow.io.Config) Collectors(java.util.stream.Collectors) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) BlockTransaction(com.sparrowwallet.drongo.wallet.BlockTransaction) EventManager(com.sparrowwallet.sparrow.EventManager) LongPropertyBase(javafx.beans.property.LongPropertyBase) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode) HashIndex(com.sparrowwallet.drongo.protocol.HashIndex) NewWalletTransactionsEvent(com.sparrowwallet.sparrow.event.NewWalletTransactionsEvent) BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) NewWalletTransactionsEvent(com.sparrowwallet.sparrow.event.NewWalletTransactionsEvent) BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) HashIndex(com.sparrowwallet.drongo.protocol.HashIndex)

Example 4 with BlockTransactionHashIndex

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());
    }
}
Also used : BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) PSBTParseException(com.sparrowwallet.drongo.psbt.PSBTParseException) CahootsContext(com.samourai.soroban.cahoots.CahootsContext) Transaction(com.sparrowwallet.drongo.protocol.Transaction) OnlineCahootsMessage(com.samourai.soroban.client.cahoots.OnlineCahootsMessage) SorobanCahootsService(com.samourai.soroban.client.cahoots.SorobanCahootsService) PSBTParseException(com.sparrowwallet.drongo.psbt.PSBTParseException) Cahoots(com.samourai.wallet.cahoots.Cahoots) HashMap(java.util.HashMap) Map(java.util.Map) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Example 5 with BlockTransactionHashIndex

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);
                    }
                }
            }
        }
    }
}
Also used : BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) BlockTransaction(com.sparrowwallet.drongo.wallet.BlockTransaction) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Aggregations

BlockTransactionHashIndex (com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex)7 WalletNode (com.sparrowwallet.drongo.wallet.WalletNode)4 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)2 BlockTransaction (com.sparrowwallet.drongo.wallet.BlockTransaction)2 CahootsContext (com.samourai.soroban.cahoots.CahootsContext)1 OnlineCahootsMessage (com.samourai.soroban.client.cahoots.OnlineCahootsMessage)1 SorobanCahootsService (com.samourai.soroban.client.cahoots.SorobanCahootsService)1 Cahoots (com.samourai.wallet.cahoots.Cahoots)1 HashIndex (com.sparrowwallet.drongo.protocol.HashIndex)1 Transaction (com.sparrowwallet.drongo.protocol.Transaction)1 PSBTParseException (com.sparrowwallet.drongo.psbt.PSBTParseException)1 Wallet (com.sparrowwallet.drongo.wallet.Wallet)1 EventManager (com.sparrowwallet.sparrow.EventManager)1 NewWalletTransactionsEvent (com.sparrowwallet.sparrow.event.NewWalletTransactionsEvent)1 Config (com.sparrowwallet.sparrow.io.Config)1 java.util (java.util)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 LongProperty (javafx.beans.property.LongProperty)1