Class BitcoinClient

    • Field Detail

      • context

        protected final Context context
      • threadFactory

        protected final java.util.concurrent.ThreadFactory threadFactory
      • executorService

        protected final java.util.concurrent.ExecutorService executorService
    • Constructor Detail

      • BitcoinClient

        public BitcoinClient​(javax.net.ssl.SSLSocketFactory sslSocketFactory,
                             NetworkParameters netParams,
                             java.net.URI server,
                             java.lang.String rpcuser,
                             java.lang.String rpcpassword)
      • BitcoinClient

        public BitcoinClient​(NetworkParameters netParams,
                             java.net.URI server,
                             java.lang.String rpcuser,
                             java.lang.String rpcpassword)
        Construct a BitcoinClient from Network Parameters, URI, user name, and password.
        Parameters:
        netParams - Correct Network Parameters for destination server
        server - URI of the Bitcoin RPC server
        rpcuser - Username (if required)
        rpcpassword - Password (if required)
      • BitcoinClient

        public BitcoinClient​(RpcConfig config)
        Construct a BitcoinClient from an RPCConfig data object.
        Parameters:
        config - Contains URI, user name, and password
    • Method Detail

      • getDefaultAsyncExecutor

        public java.util.concurrent.ExecutorService getDefaultAsyncExecutor()
        Description copied from interface: AsyncSupport
        Return the default executor for supplying asynchronicity.
        Specified by:
        getDefaultAsyncExecutor in interface AsyncSupport
        Returns:
        An executor.
      • close

        public void close()
                   throws java.lang.InterruptedException
        Shutdown our thread pool, etc.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface JsonRpcClient
        Throws:
        java.lang.InterruptedException - if one happens
      • waitForServer

        public java.lang.Boolean waitForServer​(int timeout)
                                        throws JsonRpcException
        Wait until the server is available. Keep trying, ignoring (and logging) a known list of exception conditions that may occur while waiting for a `bitcoind` server to start up. This is similar to the behavior enabled by the `-rpcwait` option to the `bitcoin-cli` command-line tool.
        Parameters:
        timeout - Timeout in seconds
        Returns:
        true if ready, false if timeout or interrupted
        Throws:
        JsonRpcException - if an "unexpected" exception happens (i.e. an error other than what happens during normal server startup)
      • waitForBlock

        public java.lang.Boolean waitForBlock​(int blockHeight,
                                              int timeout)
                                       throws JsonRpcStatusException,
                                              java.io.IOException
        Wait for RPC server to reach specified block height.
        Parameters:
        blockHeight - Block height to wait for
        timeout - Timeout in seconds
        Returns:
        True if blockHeight reached, false if timeout or interrupted
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBlockCount

        public java.lang.Integer getBlockCount()
                                        throws JsonRpcStatusException,
                                               java.io.IOException
        Returns the number of blocks in the longest block chain.
        Returns:
        The current block count
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBlockHash

        public Sha256Hash getBlockHash​(java.lang.Integer index)
                                throws JsonRpcStatusException,
                                       java.io.IOException
        Returns the hash of block in best-block-chain at index provided.
        Parameters:
        index - The block index
        Returns:
        The block hash
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBlockInfo

        public BlockInfo getBlockInfo​(Sha256Hash hash)
                               throws JsonRpcStatusException,
                                      java.io.IOException
        Returns information about a block with the given block hash.
        Parameters:
        hash - The block hash
        Returns:
        The information about the block (JSON/POJO object)
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBlock

        public Block getBlock​(Sha256Hash hash)
                       throws JsonRpcStatusException,
                              java.io.IOException
        Returns information about a block with the given block hash.
        Parameters:
        hash - The block hash
        Returns:
        The information about the block (bitcoinj Block object)
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • generateToAddress

        public java.util.List<Sha256Hash> generateToAddress​(int numBlocks,
                                                            Address address,
                                                            java.lang.Integer maxtries)
                                                     throws java.io.IOException
        Mine blocks immediately (RegTest mode)
        Parameters:
        numBlocks - Number of blocks to mine
        address - Address to send mined coins to
        maxtries - How many iterations to try (or null to use server default -- currently 1,000,000)
        Returns:
        list containing block header hashes of the generated blocks
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.13.0
      • generateToAddress

        public java.util.List<Sha256Hash> generateToAddress​(int numBlocks,
                                                            Address address)
                                                     throws java.io.IOException
        Mine blocks immediately (RegTest mode)
        Parameters:
        numBlocks - Number of blocks to mine
        address - Address to send mined coins to
        Returns:
        list containing block header hashes of the generated blocks
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.13.0
      • createWallet

        public java.util.Map<java.lang.String,​java.lang.String> createWallet​(java.lang.String name,
                                                                                   java.lang.Boolean disablePrivateKeys,
                                                                                   java.lang.Boolean blank)
                                                                            throws JsonRpcStatusException,
                                                                                   java.io.IOException
        Throws:
        JsonRpcStatusException
        java.io.IOException
      • getNewAddress

        public Address getNewAddress()
                              throws JsonRpcStatusException,
                                     java.io.IOException
        Creates a new Bitcoin address for receiving payments, linked to the default account "".
        Returns:
        A new Bitcoin address
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getNewAddress

        public Address getNewAddress​(java.lang.String label)
                              throws JsonRpcStatusException,
                                     java.io.IOException
        Creates a new Bitcoin address for receiving payments.
        Parameters:
        label - The label name for the address to be linked to.
        Returns:
        A new Bitcoin address
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • dumpPrivKey

        public ECKey dumpPrivKey​(Address address)
                          throws JsonRpcStatusException,
                                 java.io.IOException
        Return the private key from the server. Note: must be in wallet mode with unlocked or unencrypted wallet.
        Parameters:
        address - Address corresponding to the private key to return
        Returns:
        The private key
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • importPrivKey

        public void importPrivKey​(ECKey privateKey,
                                  java.lang.String label,
                                  boolean rescan)
                           throws JsonRpcStatusException,
                                  java.io.IOException
        Import a private key into the server's wallet
        Parameters:
        privateKey - An ECKey (containing a private key)
        label - The server-side label for the key
        rescan - Rescan the blockchain to find transactions for this key
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • createRawTransaction

        public java.lang.String createRawTransaction​(java.util.List<Outpoint> inputs,
                                                     java.util.Map<Address,​Coin> outputs)
                                              throws JsonRpcStatusException,
                                                     java.io.IOException
        Creates a raw transaction spending the given inputs to the given destinations. Note: the transaction inputs are not signed, and the transaction is not stored in the wallet or transmitted to the network.
        Parameters:
        inputs - The outpoints to spent
        outputs - The destinations and amounts to transfer
        Returns:
        The hex-encoded raw transaction
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • signRawTransactionWithWallet

        public SignedRawTransaction signRawTransactionWithWallet​(java.lang.String unsignedTransaction)
                                                          throws JsonRpcStatusException,
                                                                 java.io.IOException
        Signs inputs of a raw transaction using the wallet. Arguments 2 and 3 of the RPC are currently not supported, which means UTXOs not currently in the blockchain can't be used and `sighashtype` defaults to `ALL`.
        Parameters:
        unsignedTransaction - The hex-encoded raw transaction
        Returns:
        The signed transaction and information whether it has a complete set of signature
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core v0.17
      • getRawTransaction

        public Transaction getRawTransaction​(Sha256Hash txid)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Get a "raw" transaction (which we use to construct a bitcoinj Transaction)
        Parameters:
        txid - Transaction ID/hash
        Returns:
        bitcoinj Transaction
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getRawTransactionInfo

        public RawTransactionInfo getRawTransactionInfo​(Sha256Hash txid)
                                                 throws JsonRpcStatusException,
                                                        java.io.IOException
        Get a "raw" transaction as JSON (which we map to a RawTransactionInfo POJO)
        Parameters:
        txid - Transaction ID/hash
        Returns:
        RawTransactionInfo POJO
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • sendRawTransaction

        public Sha256Hash sendRawTransaction​(Transaction tx,
                                             Coin maxFeeRate)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Submit a raw transaction to local node and network
        Parameters:
        tx - The raw transaction
        maxFeeRate - Reject transactions whose fee rate is higher than this value, expressed in BTC/kB. Set to 0 to accept any fee rate.
        Returns:
        SHA256 Transaction ID
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.19
      • sendRawTransaction

        public Sha256Hash sendRawTransaction​(java.lang.String hexTx,
                                             Coin maxFeeRate)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Submit a raw transaction to local node and network
        Parameters:
        hexTx - The raw transaction as a hex-encoded string
        maxFeeRate - Reject transactions whose fee rate is higher than this value, expressed in BTC/kB. Set to 0 to accept any fee rate.
        Returns:
        SHA256 Transaction ID
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.19
      • sendRawTransaction

        public Sha256Hash sendRawTransaction​(Transaction tx)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Submit a raw transaction to local node and network using server's default `maxFeeRate`
        Parameters:
        tx - The raw transaction
        Returns:
        SHA256 Transaction ID
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • sendRawTransaction

        public Sha256Hash sendRawTransaction​(java.lang.String hexTx)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Submit a raw transaction to local node and network using server's default `maxFeeRate`
        Parameters:
        hexTx - The raw transaction as a hex-encoded string
        Returns:
        SHA256 Transaction ID
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.19
      • sendRawTransaction

        @Deprecated
        public Sha256Hash sendRawTransaction​(Transaction tx,
                                             java.lang.Boolean allowHighFees)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Deprecated.
        In Bitcoin Core 0.19 and later, use sendRawTransaction(Transaction, Coin)
        Submit a raw transaction to local node and network
        Parameters:
        tx - The raw transaction
        allowHighFees - deprecated boolean parameter
        Returns:
        SHA256 Transaction ID
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • sendRawTransaction

        @Deprecated
        public Sha256Hash sendRawTransaction​(java.lang.String hexTx,
                                             java.lang.Boolean allowHighFees)
                                      throws JsonRpcStatusException,
                                             java.io.IOException
        Deprecated.
        In Bitcoin Core 0.19 and later, use sendRawTransaction(Transaction, Coin)
        Submit a raw transaction to local node and network
        Parameters:
        hexTx - The raw transaction as a hex-encoded string
        allowHighFees - deprecated boolean parameter
        Returns:
        SHA256 Transaction ID
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getReceivedByAddress

        public Coin getReceivedByAddress​(Address address,
                                         java.lang.Integer minConf)
                                  throws JsonRpcStatusException,
                                         java.io.IOException
        get total amount received by an address.
        Parameters:
        address - Address to query
        minConf - minimum number of confirmations
        Returns:
        Is now returning `Coin`, if you need to convert use `BitcoinMath.btcToCoin(result)`
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • listUnspent

        public java.util.List<UnspentOutput> listUnspent()
                                                  throws JsonRpcStatusException,
                                                         java.io.IOException
        Returns a list of unspent transaction outputs with at least one confirmation.
        Returns:
        The unspent transaction outputs
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • listUnspent

        public java.util.List<UnspentOutput> listUnspent​(java.lang.Integer minConf,
                                                         java.lang.Integer maxConf)
                                                  throws JsonRpcStatusException,
                                                         java.io.IOException
        Returns a list of unspent transaction outputs with at least minConf and not more than maxConf confirmations.
        Parameters:
        minConf - The minimum confirmations to filter
        maxConf - The maximum confirmations to filter
        Returns:
        The unspent transaction outputs
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • listUnspent

        public java.util.List<UnspentOutput> listUnspent​(java.lang.Integer minConf,
                                                         java.lang.Integer maxConf,
                                                         java.lang.Iterable<Address> filter,
                                                         boolean includeUnsafe)
                                                  throws JsonRpcStatusException,
                                                         java.io.IOException
        Returns a list of unspent transaction outputs with at least minConf and not more than maxConf confirmations, filtered by a list of addresses.
        Parameters:
        minConf - The minimum confirmations to filter
        maxConf - The maximum confirmations to filter
        filter - Include only transaction outputs to the specified addresses
        Returns:
        The unspent transaction outputs
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getTxOut

        public TxOutInfo getTxOut​(Sha256Hash txid,
                                  java.lang.Integer vout)
                           throws JsonRpcStatusException,
                                  java.io.IOException
        Returns details about an unspent transaction output.
        Parameters:
        txid - The transaction hash
        vout - The transaction output index
        Returns:
        Details about an unspent output or nothing, if the output was already spent
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getTxOut

        public TxOutInfo getTxOut​(Sha256Hash txid,
                                  java.lang.Integer vout,
                                  java.lang.Boolean includeMemoryPool)
                           throws JsonRpcStatusException,
                                  java.io.IOException
        Returns details about an unspent transaction output.
        Parameters:
        txid - The transaction hash
        vout - The transaction output index
        includeMemoryPool - Whether to included the memory pool
        Returns:
        Details about an unspent output or nothing, if the output was already spent
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getTxOutSetInfo

        public TxOutSetInfo getTxOutSetInfo()
                                     throws JsonRpcStatusException,
                                            java.io.IOException
        Returns statistics about the unspent transaction output set. Note this call may take some time.
        Returns:
        statistics about the unspent transaction output set
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBalance

        public Coin getBalance()
                        throws JsonRpcStatusException,
                               java.io.IOException
        Get the balance for a the default Bitcoin "account"
        Returns:
        Is now returning `Coin`, if you need to convert use `BitcoinMath.btcToCoin(result)`
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBalance

        public Coin getBalance​(java.lang.String account)
                        throws JsonRpcStatusException,
                               java.io.IOException
        Get the balance for a Bitcoin "account"
        Parameters:
        account - A Bitcoin "account". (Be wary of using this feature.)
        Returns:
        Is now returning `Coin`, if you need to convert use `BitcoinMath.btcToCoin(result)`
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBalance

        public Coin getBalance​(java.lang.String account,
                               java.lang.Integer minConf)
                        throws JsonRpcStatusException,
                               java.io.IOException
        Get the balance for a Bitcoin "account"
        Parameters:
        account - A Bitcoin "account". (Be wary of using this feature.)
        minConf - minimum number of confirmations
        Returns:
        Is now returning `Coin`, if you need to convert use `BitcoinMath.btcToCoin(result)`
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • setTxFee

        public java.lang.Boolean setTxFee​(Coin amount)
                                   throws JsonRpcStatusException,
                                          java.io.IOException
        Set the transaction fee per kB.
        Parameters:
        amount - The transaction fee in BTC/kB rounded to the nearest 0.00000001.
        Returns:
        True if successful
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getBlockChainInfo

        public BlockChainInfo getBlockChainInfo()
                                         throws JsonRpcStatusException,
                                                java.io.IOException
        The getblockchaininfo RPC provides information about the current state of the block chain.
        Returns:
        An object containing information about the current state of the block chain.
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getNetworkInfo

        public NetworkInfo getNetworkInfo()
                                   throws JsonRpcStatusException,
                                          java.io.IOException
        The getnetworkinfo RPC returns information about the node's connection to the network.
        Returns:
        information about the node's connection to the network
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getZmqNotifications

        public java.util.List<ZmqNotification> getZmqNotifications()
                                                            throws JsonRpcStatusException,
                                                                   java.io.IOException
        The getzmqnotifications RPC returns information about which configured ZMQ notifications are enabled and on which ports.
        Returns:
        A List of ZMQ Notification info records
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • listAddressGroupings

        public java.util.List<java.util.List<AddressGroupingItem>> listAddressGroupings()
                                                                                 throws JsonRpcStatusException,
                                                                                        java.io.IOException
        Returns list of related addresses Also useful for finding all change addresses in the wallet
        Returns:
        a lost of address groupings
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • help

        public java.lang.String help()
                              throws JsonRpcStatusException,
                                     java.io.IOException
        Returns a human readable list of available commands.

        Bitcoin Core 0.9 returns an alphabetical list of commands, and Bitcoin Core 0.10 returns a categorized list of commands.

        Returns:
        The list of commands as string
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • help

        public java.lang.String help​(java.lang.String command)
                              throws JsonRpcStatusException,
                                     java.io.IOException
        Returns helpful information for a specific command.
        Parameters:
        command - The name of the command to get help for
        Returns:
        The help text
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getCommands

        public java.util.List<java.lang.String> getCommands()
                                                     throws JsonRpcStatusException,
                                                            java.io.IOException
        Returns a list of available commands. Commands which are unavailable will not be listed, such as wallet RPCs, if wallet support is disabled.
        Returns:
        The list of commands
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • commandExists

        public java.lang.Boolean commandExists​(java.lang.String command)
                                        throws JsonRpcStatusException,
                                               java.io.IOException
        Checks whether a command exists. This is done indirectly, by using {help(String) help} to get information about the command, and if information about the command is available, then the command exists. The absence of information does not necessarily imply the non-existence of a command.
        Parameters:
        command - The name of the command to check
        Returns:
        True if the command exists
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • invalidateBlock

        public void invalidateBlock​(Sha256Hash hash)
                             throws JsonRpcStatusException,
                                    java.io.IOException
        Permanently marks a block as invalid, as if it violated a consensus rule.
        Parameters:
        hash - The block hash
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.10
      • reconsiderBlock

        public void reconsiderBlock​(Sha256Hash hash)
                             throws JsonRpcStatusException,
                                    java.io.IOException
        Removes invalidity status of a block and its descendants, reconsider them for activation.

        This can be used to undo the effects of {link invalidateBlock(Sha256Hash) invalidateBlock}.

        Parameters:
        hash - The hash of the block to reconsider
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.10
      • getChainTips

        public java.util.List<ChainTip> getChainTips()
                                              throws JsonRpcStatusException,
                                                     java.io.IOException
        Return information about all known tips in the block tree, including the main chain as well as orphaned branches.
        Returns:
        A list of chain tip information
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
        Since:
        Bitcoin Core 0.10
      • addNode

        public void addNode​(java.lang.String node,
                            java.lang.String command)
                     throws JsonRpcStatusException,
                            java.io.IOException
        Attempt to add or remove a node from the addnode list, or to try a connection to a node once.
        Parameters:
        node - node to add as a string in the form of IP_address:port
        command - `add`, `remove`, or `onetry`
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getAddedNodeInfo

        public JsonNode getAddedNodeInfo​(boolean details,
                                         java.lang.String node)
                                  throws JsonRpcStatusException,
                                         java.io.IOException
        Return information about the given added node
        Parameters:
        details - `true` to return detailed information
        node - the node to provide information about
        Returns:
        A Jackson JsonNode object (until we define a POJO)
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error
      • getAddedNodeInfo

        public JsonNode getAddedNodeInfo​(boolean details)
                                  throws JsonRpcStatusException,
                                         java.io.IOException
        Return information about all added nodes
        Parameters:
        details - `true` to return detailed information
        Returns:
        A Jackson JsonNode object (until we define a POJO)
        Throws:
        JsonRpcStatusException - JSON RPC status exception
        java.io.IOException - network error