Search in sources :

Example 11 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class SorobanController method getWalletTransaction.

private WalletTransaction getWalletTransaction(Wallet wallet, WalletTransaction walletTransaction, Transaction transaction, Map<Sha256Hash, BlockTransaction> inputTransactions) {
    Map<BlockTransactionHashIndex, WalletNode> allWalletUtxos = wallet.getWalletTxos();
    Map<BlockTransactionHashIndex, WalletNode> walletUtxos = new LinkedHashMap<>();
    Map<BlockTransactionHashIndex, WalletNode> externalUtxos = new LinkedHashMap<>();
    for (TransactionInput txInput : transaction.getInputs()) {
        Optional<BlockTransactionHashIndex> optWalletUtxo = allWalletUtxos.keySet().stream().filter(txo -> txo.getHash().equals(txInput.getOutpoint().getHash()) && txo.getIndex() == txInput.getOutpoint().getIndex()).findFirst();
        if (optWalletUtxo.isPresent()) {
            walletUtxos.put(optWalletUtxo.get(), allWalletUtxos.get(optWalletUtxo.get()));
        } else {
            BlockTransactionHashIndex externalUtxo;
            if (inputTransactions != null && inputTransactions.containsKey(txInput.getOutpoint().getHash())) {
                BlockTransaction blockTransaction = inputTransactions.get(txInput.getOutpoint().getHash());
                TransactionOutput txOutput = blockTransaction.getTransaction().getOutputs().get((int) txInput.getOutpoint().getIndex());
                externalUtxo = new BlockTransactionHashIndex(blockTransaction.getHash(), blockTransaction.getHeight(), blockTransaction.getDate(), blockTransaction.getFee(), txInput.getOutpoint().getIndex(), txOutput.getValue());
            } else {
                externalUtxo = new BlockTransactionHashIndex(txInput.getOutpoint().getHash(), 0, null, null, txInput.getOutpoint().getIndex(), 0);
            }
            externalUtxos.put(externalUtxo, null);
        }
    }
    List<Map<BlockTransactionHashIndex, WalletNode>> selectedUtxoSets = new ArrayList<>();
    selectedUtxoSets.add(walletUtxos);
    selectedUtxoSets.add(externalUtxos);
    Map<Address, WalletNode> walletAddresses = wallet.getWalletAddresses();
    List<Payment> payments = new ArrayList<>();
    Map<WalletNode, Long> changeMap = new LinkedHashMap<>();
    for (TransactionOutput txOutput : transaction.getOutputs()) {
        Address address = txOutput.getScript().getToAddress();
        if (address != null) {
            Optional<Payment> optPayment = walletTransaction == null ? Optional.empty() : walletTransaction.getPayments().stream().filter(payment -> payment.getAddress().equals(address) && payment.getAmount() == txOutput.getValue()).findFirst();
            if (optPayment.isPresent()) {
                payments.add(optPayment.get());
            } else if (walletAddresses.containsKey(address) && walletAddresses.get(address).getKeyPurpose() == KeyPurpose.CHANGE) {
                changeMap.put(walletAddresses.get(address), txOutput.getValue());
            } else {
                Payment payment = new Payment(address, null, txOutput.getValue(), false);
                if (transaction.getOutputs().stream().anyMatch(txo -> txo != txOutput && txo.getValue() == txOutput.getValue())) {
                    payment.setType(Payment.Type.MIX);
                }
                payments.add(payment);
            }
        }
    }
    long fee = calculateFee(walletTransaction, selectedUtxoSets, transaction);
    return new WalletTransaction(wallet, transaction, Collections.emptyList(), selectedUtxoSets, payments, changeMap, fee, inputTransactions);
}
Also used : Address(com.sparrowwallet.drongo.address.Address) Transaction(com.sparrowwallet.drongo.protocol.Transaction) java.util(java.util) Logger(org.slf4j.Logger) PSBT(com.sparrowwallet.drongo.psbt.PSBT) com.sparrowwallet.drongo.wallet(com.sparrowwallet.drongo.wallet) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Cahoots(com.samourai.wallet.cahoots.Cahoots) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) ElectrumServer(com.sparrowwallet.sparrow.net.ElectrumServer) TransactionOutput(com.sparrowwallet.drongo.protocol.TransactionOutput) TransactionDiagram(com.sparrowwallet.sparrow.control.TransactionDiagram) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) PSBTParseException(com.sparrowwallet.drongo.psbt.PSBTParseException) Pattern(java.util.regex.Pattern) TransactionInput(com.sparrowwallet.drongo.protocol.TransactionInput) TransactionOutput(com.sparrowwallet.drongo.protocol.TransactionOutput) Address(com.sparrowwallet.drongo.address.Address) TransactionInput(com.sparrowwallet.drongo.protocol.TransactionInput)

Example 12 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class HeadersController method getPayjoinURI.

private BitcoinURI getPayjoinURI() {
    if (headersForm.getPsbt() != null) {
        for (TransactionOutput txOutput : headersForm.getPsbt().getTransaction().getOutputs()) {
            try {
                Address address = txOutput.getScript().getToAddresses()[0];
                BitcoinURI bitcoinURI = AppServices.getPayjoinURI(address);
                if (bitcoinURI != null) {
                    return bitcoinURI;
                }
            } catch (Exception e) {
            // ignore
            }
        }
    }
    return null;
}
Also used : Address(com.sparrowwallet.drongo.address.Address) BitcoinURI(com.sparrowwallet.drongo.uri.BitcoinURI)

Example 13 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class OutputController method initializeView.

public void initializeView() {
    TransactionOutput txOutput = outputForm.getTransactionOutput();
    outputForm.signingWalletProperty().addListener((observable, oldValue, signingWallet) -> {
        updateOutputLegendFromWallet(txOutput, signingWallet);
    });
    updateOutputLegendFromWallet(txOutput, outputForm.getSigningWallet());
    value.setValue(txOutput.getValue());
    to.setVisible(false);
    try {
        Address[] addresses = txOutput.getScript().getToAddresses();
        to.setVisible(true);
        if (addresses.length == 1) {
            address.setAddress(addresses[0]);
        } else {
            address.setText("multiple addresses");
        }
    } catch (NonStandardScriptException e) {
    // ignore
    }
    spentField.managedProperty().bind(spentField.visibleProperty());
    spentByField.managedProperty().bind(spentByField.visibleProperty());
    spentByField.setVisible(false);
    if (outputForm.getPsbt() != null) {
        spent.setText("Unspent");
    } else if (outputForm.getOutputTransactions() != null) {
        updateSpent(outputForm.getOutputTransactions());
    } else {
        spent.setText("Unknown");
    }
    initializeScriptField(scriptPubKeyArea);
    scriptPubKeyArea.clear();
    scriptPubKeyArea.appendScript(txOutput.getScript(), null, null);
}
Also used : NonStandardScriptException(com.sparrowwallet.drongo.protocol.NonStandardScriptException) TransactionOutput(com.sparrowwallet.drongo.protocol.TransactionOutput) Address(com.sparrowwallet.drongo.address.Address)

Example 14 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class VersionCheckService method verifySignature.

private boolean verifySignature(VersionCheck versionCheck) {
    try {
        for (String addressString : versionCheck.signatures.keySet()) {
            if (!addressString.equals("1LiJx1HQ49L2LzhBwbgwXdHiGodvPg5YaV")) {
                log.warn("Invalid address for version check " + addressString);
                continue;
            }
            String signature = versionCheck.signatures.get(addressString);
            ECKey signedMessageKey = ECKey.signedMessageToKey(versionCheck.version, signature, false);
            Address providedAddress = Address.fromString(addressString);
            Address signedMessageAddress = ScriptType.P2PKH.getAddress(signedMessageKey);
            if (providedAddress.equals(signedMessageAddress)) {
                return true;
            } else {
                log.warn("Invalid signature for version check " + signature + " from address " + addressString);
            }
        }
    } catch (SignatureException e) {
        log.error("Error in version check signature", e);
    } catch (InvalidAddressException e) {
        log.error("Error in version check address", e);
    }
    return false;
}
Also used : Address(com.sparrowwallet.drongo.address.Address) ECKey(com.sparrowwallet.drongo.crypto.ECKey) SignatureException(java.security.SignatureException) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException)

Example 15 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class UtxosController method previewPremix.

private void previewPremix(Wallet wallet, Tx0Preview tx0Preview, List<UtxoEntry> utxoEntries) {
    Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet();
    Wallet premixWallet = masterWallet.getChildWallet(StandardAccount.WHIRLPOOL_PREMIX);
    Wallet badbankWallet = masterWallet.getChildWallet(StandardAccount.WHIRLPOOL_BADBANK);
    List<Payment> payments = new ArrayList<>();
    if (tx0Preview.getTx0Data().getFeeAddress() != null) {
        try {
            Address whirlpoolFeeAddress = Address.fromString(tx0Preview.getTx0Data().getFeeAddress());
            Payment whirlpoolFeePayment = new Payment(whirlpoolFeeAddress, "Whirlpool Fee", tx0Preview.getFeeValue(), false);
            whirlpoolFeePayment.setType(Payment.Type.WHIRLPOOL_FEE);
            payments.add(whirlpoolFeePayment);
        } catch (InvalidAddressException e) {
            throw new IllegalStateException("Cannot parse whirlpool fee address " + tx0Preview.getTx0Data().getFeeAddress(), e);
        }
    }
    WalletNode badbankNode = badbankWallet.getFreshNode(KeyPurpose.RECEIVE);
    Payment changePayment = new Payment(badbankNode.getAddress(), "Badbank Change", tx0Preview.getChangeValue(), false);
    payments.add(changePayment);
    WalletNode premixNode = null;
    for (int i = 0; i < tx0Preview.getNbPremix(); i++) {
        premixNode = premixWallet.getFreshNode(KeyPurpose.RECEIVE, premixNode);
        Address premixAddress = premixNode.getAddress();
        payments.add(new Payment(premixAddress, "Premix #" + i, tx0Preview.getPremixValue(), false));
    }
    List<byte[]> opReturns = List.of(new byte[64]);
    final List<BlockTransactionHashIndex> utxos = utxoEntries.stream().map(HashIndexEntry::getHashIndex).collect(Collectors.toList());
    Platform.runLater(() -> {
        EventManager.get().post(new SendActionEvent(getWalletForm().getWallet(), utxos));
        Platform.runLater(() -> EventManager.get().post(new SpendUtxoEvent(getWalletForm().getWallet(), utxos, payments, opReturns, tx0Preview.getTx0MinerFee(), tx0Preview.getPool())));
    });
}
Also used : Address(com.sparrowwallet.drongo.address.Address) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException)

Aggregations

Address (com.sparrowwallet.drongo.address.Address)26 Test (org.junit.Test)6 InvalidAddressException (com.sparrowwallet.drongo.address.InvalidAddressException)5 ECKey (com.sparrowwallet.drongo.crypto.ECKey)5 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)4 TransactionOutput (com.sparrowwallet.drongo.protocol.TransactionOutput)4 P2PKHAddress (com.sparrowwallet.drongo.address.P2PKHAddress)3 com.sparrowwallet.drongo.wallet (com.sparrowwallet.drongo.wallet)3 PayNymAddress (com.sparrowwallet.sparrow.paynym.PayNymAddress)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 InvalidPaymentCodeException (com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException)2 DumpedPrivateKey (com.sparrowwallet.drongo.crypto.DumpedPrivateKey)2 NonStandardScriptException (com.sparrowwallet.drongo.protocol.NonStandardScriptException)2 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)2 Transaction (com.sparrowwallet.drongo.protocol.Transaction)2 BitcoinURI (com.sparrowwallet.drongo.uri.BitcoinURI)2 AppServices (com.sparrowwallet.sparrow.AppServices)2 EventManager (com.sparrowwallet.sparrow.EventManager)2