Search in sources :

Example 1 with RawTransaction

use of com.neemre.btcdcli4j.core.domain.RawTransaction in project bisq-core by bisq-network.

the class RpcService method requestTx.

public Tx requestTx(String txId, int blockHeight) throws BsqBlockchainException {
    try {
        RawTransaction rawTransaction = requestRawTransaction(txId);
        // rawTransaction.getTime() is in seconds but we keep it in ms internally
        final long time = rawTransaction.getTime() * 1000;
        final List<TxInput> txInputs = rawTransaction.getVIn().stream().filter(rawInput -> rawInput != null && rawInput.getVOut() != null && rawInput.getTxId() != null).map(rawInput -> new TxInput(rawInput.getTxId(), rawInput.getVOut())).collect(Collectors.toList());
        final List<TxOutput> txOutputs = rawTransaction.getVOut().stream().filter(e -> e != null && e.getN() != null && e.getValue() != null && e.getScriptPubKey() != null).map(rawOutput -> {
            byte[] opReturnData = null;
            final com.neemre.btcdcli4j.core.domain.PubKeyScript scriptPubKey = rawOutput.getScriptPubKey();
            if (scriptPubKey.getType().equals(ScriptTypes.NULL_DATA)) {
                String[] chunks = scriptPubKey.getAsm().split(" ");
                // We get on testnet a lot of "OP_RETURN 0" data, so we filter those away
                if (chunks.length == 2 && chunks[0].equals("OP_RETURN") && !"0".equals(chunks[1])) {
                    try {
                        opReturnData = Utils.HEX.decode(chunks[1]);
                    } catch (Throwable t) {
                        // We get sometimes exceptions, seems BitcoinJ
                        // cannot handle all existing OP_RETURN data, but we ignore them
                        // anyway as our OP_RETURN data is valid in BitcoinJ
                        log.warn("Error at Utils.HEX.decode(chunks[1]): " + t.toString() + " / chunks[1]=" + chunks[1]);
                    }
                }
            }
            // We don't support raw MS which are the only case where scriptPubKey.getAddresses()>1
            String address = scriptPubKey.getAddresses() != null && scriptPubKey.getAddresses().size() == 1 ? scriptPubKey.getAddresses().get(0) : null;
            final PubKeyScript pubKeyScript = dumpBlockchainData ? new PubKeyScript(scriptPubKey) : null;
            return new TxOutput(rawOutput.getN(), rawOutput.getValue().movePointRight(8).longValue(), rawTransaction.getTxId(), pubKeyScript, address, opReturnData, blockHeight);
        }).collect(Collectors.toList());
        return new Tx(txId, blockHeight, rawTransaction.getBlockHash(), time, ImmutableList.copyOf(txInputs), ImmutableList.copyOf(txOutputs));
    } catch (BitcoindException | CommunicationException e) {
        log.error("error at requestTx with txId={}, blockHeight={}", txId, blockHeight);
        throw new BsqBlockchainException(e.getMessage(), e);
    }
}
Also used : BtcdClientImpl(com.neemre.btcdcli4j.core.client.BtcdClientImpl) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) DaoOptionKeys(bisq.core.dao.DaoOptionKeys) RawTransaction(com.neemre.btcdcli4j.core.domain.RawTransaction) BtcdDaemonImpl(com.neemre.btcdcli4j.daemon.BtcdDaemonImpl) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) Transaction(com.neemre.btcdcli4j.core.domain.Transaction) Map(java.util.Map) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PubKeyScript(bisq.core.dao.blockchain.btcd.PubKeyScript) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException) Named(javax.inject.Named) TxInput(bisq.core.dao.blockchain.vo.TxInput) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Properties(java.util.Properties) Logger(org.slf4j.Logger) Tx(bisq.core.dao.blockchain.vo.Tx) Utils(org.bitcoinj.core.Utils) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) ScriptTypes(com.neemre.btcdcli4j.core.domain.enums.ScriptTypes) BtcdClient(com.neemre.btcdcli4j.core.client.BtcdClient) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) Collectors(java.util.stream.Collectors) BlockListener(com.neemre.btcdcli4j.daemon.event.BlockListener) Consumer(java.util.function.Consumer) Block(com.neemre.btcdcli4j.core.domain.Block) List(java.util.List) BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) BtcdDaemon(com.neemre.btcdcli4j.daemon.BtcdDaemon) HttpClients(org.apache.http.impl.client.HttpClients) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) Tx(bisq.core.dao.blockchain.vo.Tx) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) PubKeyScript(bisq.core.dao.blockchain.btcd.PubKeyScript) TxInput(bisq.core.dao.blockchain.vo.TxInput) BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) RawTransaction(com.neemre.btcdcli4j.core.domain.RawTransaction) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException)

Aggregations

DaoOptionKeys (bisq.core.dao.DaoOptionKeys)1 PubKeyScript (bisq.core.dao.blockchain.btcd.PubKeyScript)1 BsqBlockchainException (bisq.core.dao.blockchain.exceptions.BsqBlockchainException)1 Tx (bisq.core.dao.blockchain.vo.Tx)1 TxInput (bisq.core.dao.blockchain.vo.TxInput)1 TxOutput (bisq.core.dao.blockchain.vo.TxOutput)1 ImmutableList (com.google.common.collect.ImmutableList)1 Inject (com.google.inject.Inject)1 BitcoindException (com.neemre.btcdcli4j.core.BitcoindException)1 CommunicationException (com.neemre.btcdcli4j.core.CommunicationException)1 BtcdClient (com.neemre.btcdcli4j.core.client.BtcdClient)1 BtcdClientImpl (com.neemre.btcdcli4j.core.client.BtcdClientImpl)1 Block (com.neemre.btcdcli4j.core.domain.Block)1 RawTransaction (com.neemre.btcdcli4j.core.domain.RawTransaction)1 Transaction (com.neemre.btcdcli4j.core.domain.Transaction)1 ScriptTypes (com.neemre.btcdcli4j.core.domain.enums.ScriptTypes)1 BtcdDaemon (com.neemre.btcdcli4j.daemon.BtcdDaemon)1 BtcdDaemonImpl (com.neemre.btcdcli4j.daemon.BtcdDaemonImpl)1 BlockListener (com.neemre.btcdcli4j.daemon.event.BlockListener)1 BigDecimal (java.math.BigDecimal)1