Class BitcoinExtendedClient

All Implemented Interfaces:
AutoCloseable, ChainTipClient, AsyncSupport, JsonRpcClient<JavaType>, JsonRpcTransport<JavaType>
Direct Known Subclasses:
RxBitcoinClient

Extended Bitcoin JSON-RPC Client with added convenience methods. This class adds extra methods that aren't 1:1 mappings to standard Bitcoin API RPC methods, but are useful for many common use cases -- specifically the ones we ran into while building integration tests.
  • Field Details

  • Constructor Details

  • Method Details

    • getStdTxFee

      public org.bitcoinj.base.Coin getStdTxFee()
    • getStdRelayTxFee

      public org.bitcoinj.base.Coin getStdRelayTxFee()
    • getDefaultMaxConf

    • withWallet

      public BitcoinExtendedClient withWallet(String walletName, String rpcUser, String rpcPassword)
      Incubating: clone the client with a new base URI for a named wallet.
      Parameters:
      walletName - wallet name
      rpcUser - username must be provided because it is not accessible in the parent class
      rpcPassword - password must be provided because it is not accessible in the parent class
      Returns:
      A new client with a baseURI configured for the specified wallet name.
    • getRegTestMiningAddress

      public org.bitcoinj.base.Address getRegTestMiningAddress()
    • initRegTestWallet

      public void initRegTestWallet()
      Initialize a server-side wallet for RegTest mining and test transaction funding. Creates a non-descriptor wallet with name REGTEST_WALLET_NAME if it doesn't already exist.
    • generateBlocks

      public List<org.bitcoinj.base.Sha256Hash> generateBlocks(int numBlocks) throws JsonRpcStatusException, IOException
      Generate blocks and funds (RegTest only) Use this to generate blocks and receive the block reward in this.regTestMiningAddress which can the be used to fund transactions in RegTest mode.
      Parameters:
      numBlocks - Number of blocks to mine
      Returns:
      list of block hashes
      Throws:
      JsonRpcStatusException - something broke
      IOException - something broke
    • getBlockSubsidy

      public org.bitcoinj.base.Coin getBlockSubsidy(int height)
      Calculate the subsidy portion block reward for a given height on the current network
      Parameters:
      height - the height at which to calculate the mining subsidy
      Returns:
      mining subsidy
    • getBlock

      public org.bitcoinj.core.Block getBlock(int index) throws JsonRpcStatusException, IOException
      Returns information about a block at index provided. Uses two RPCs: getblockhash and then getblock.
      Parameters:
      index - The block index
      Returns:
      The information about the block
      Throws:
      JsonRpcStatusException - JSON RPC status exception
      IOException - network error
    • clearMemPool

      public List<org.bitcoinj.base.Sha256Hash> clearMemPool() throws JsonRpcStatusException, IOException
      Clears the memory pool and returns a list of the removed transactions. Note: this is a customized command, which is currently not part of Bitcoin Core. See https://github.com/OmniLayer/OmniJ/pull/72[Pull Request #72] on GitHub
      Returns:
      A list of transaction hashes of the removed transactions
      Throws:
      JsonRpcStatusException - JSON RPC status exception
      IOException - network error
    • createRawTransaction

      public String createRawTransaction(org.bitcoinj.base.Address fromAddress, Map<org.bitcoinj.base.Address,org.bitcoinj.base.Coin> outputs) throws JsonRpcStatusException, IOException
      Creates a raw transaction, spending from a single address, whereby no new change address is created, and remaining amounts are returned to fromAddress.

      Note: the transaction inputs are not signed, and the transaction is not stored in the wallet or transmitted to the network.

      Parameters:
      fromAddress - The source to spend from
      outputs - The destinations and amounts to transfer
      Returns:
      The hex-encoded raw transaction
      Throws:
      JsonRpcStatusException
      IOException
    • createRawTransaction

      public String createRawTransaction(org.bitcoinj.base.Address fromAddress, org.bitcoinj.base.Address toAddress, org.bitcoinj.base.Coin amount) throws JsonRpcStatusException, IOException
      Creates a raw transaction, sending amount from a single address to a destination, whereby no new change address is created, and remaining amounts are returned to fromAddress.

      Note: the transaction inputs are not signed, and the transaction is not stored in the wallet or transmitted to the network.

      Parameters:
      fromAddress - The source to spent from
      toAddress - The destination
      amount - The amount
      Returns:
      The hex-encoded raw transaction
      Throws:
      JsonRpcStatusException
      IOException
    • getBitcoinBalance

      public org.bitcoinj.base.Coin getBitcoinBalance(org.bitcoinj.base.Address address) throws JsonRpcStatusException, IOException
      Returns the Bitcoin balance of an address (in the server-side wallet.)
      Parameters:
      address - The address
      Returns:
      The balance
      Throws:
      JsonRpcStatusException
      IOException
    • getBitcoinBalance

      public org.bitcoinj.base.Coin getBitcoinBalance(org.bitcoinj.base.Address address, Integer minConf) throws JsonRpcStatusException, IOException
      Returns the Bitcoin balance of an address (in the server-side wallet) where spendable outputs have at least minConf confirmations.
      Parameters:
      address - The address
      minConf - Minimum amount of confirmations
      Returns:
      The balance
      Throws:
      JsonRpcStatusException
      IOException
    • getBitcoinBalance

      public org.bitcoinj.base.Coin getBitcoinBalance(org.bitcoinj.base.Address address, Integer minConf, Integer maxConf) throws JsonRpcStatusException, IOException
      Returns the Bitcoin balance of an address (in the server-side wallet) where spendable outputs have at least minConf and not more than maxConf confirmations.
      Parameters:
      address - The address (must be in wallet)
      minConf - Minimum amount of confirmations
      maxConf - Maximum amount of confirmations
      Returns:
      The balance
      Throws:
      JsonRpcStatusException
      IOException
    • sendBitcoin

      public org.bitcoinj.base.Sha256Hash sendBitcoin(org.bitcoinj.base.Address fromAddress, org.bitcoinj.base.Address toAddress, org.bitcoinj.base.Coin amount) throws JsonRpcStatusException, IOException
      Sends BTC from an address to a destination, whereby no new change address is created, and any leftover is returned to the sending address.
      Parameters:
      fromAddress - The source to spent from
      toAddress - The destination address
      amount - The amount to transfer
      Returns:
      The transaction hash
      Throws:
      JsonRpcStatusException
      IOException
    • sendBitcoin

      public org.bitcoinj.base.Sha256Hash sendBitcoin(org.bitcoinj.base.Address fromAddress, Map<org.bitcoinj.base.Address,org.bitcoinj.base.Coin> outputs) throws JsonRpcStatusException, IOException
      Sends BTC from an address to the destinations, whereby no new change address is created, and any leftover is returned to the sending address.
      Parameters:
      fromAddress - The source to spent from
      outputs - The destinations and amounts to transfer
      Returns:
      The transaction hash
      Throws:
      JsonRpcStatusException
      IOException
    • createSignedTransaction

      public org.bitcoinj.core.Transaction createSignedTransaction(org.bitcoinj.crypto.ECKey fromKey, List<org.bitcoinj.core.TransactionOutput> outputs) throws JsonRpcStatusException, IOException
      Create a signed transaction locally (i.e. with a client-side key.) Finds UTXOs this key can spend (assuming they are ScriptType.P2PKH UTXOs)
      Parameters:
      fromKey - Signing key
      outputs - Outputs to sign
      Returns:
      A bitcoinj Transaction objects that is properly signed
      Throws:
      JsonRpcStatusException - A JSON-RPC error was returned
      IOException - An I/O error occured
    • createSignedTransaction

      public org.bitcoinj.core.Transaction createSignedTransaction(org.bitcoinj.crypto.ECKey fromKey, org.bitcoinj.base.Address toAddress, org.bitcoinj.base.Coin amount) throws JsonRpcStatusException, IOException
      Create a signed transaction locally (i.e. with a client-side key.) Finds UTXOs this key can spend (assuming they are org.bitcoinj.base.ScriptType.P2PKH UTXOs)
      Parameters:
      fromKey - Signing key
      toAddress - Destination address
      amount - Amount to send
      Returns:
      A bitcoinj Transaction objects that is properly signed
      Throws:
      JsonRpcStatusException - A JSON-RPC error was returned
      IOException - An I/O error occured
    • listUnspentJ

      public List<org.bitcoinj.core.TransactionOutput> listUnspentJ(org.bitcoinj.base.Address fromAddress) throws JsonRpcStatusException, IOException
      Build a list of bitcoinj TransactionOutputs using BitcoinClient.listUnspent() and BitcoinClient.getRawTransaction(org.bitcoinj.base.Sha256Hash) RPCs.
      Parameters:
      fromAddress - Address to get UTXOs for
      Returns:
      All unspent TransactionOutputs for fromAddress
      Throws:
      JsonRpcStatusException
      IOException
    • listUnspentOutPoints

      public List<org.bitcoinj.core.TransactionOutPoint> listUnspentOutPoints(org.bitcoinj.base.Address fromAddress) throws JsonRpcStatusException, IOException
      Build a list of bitcoinj TransactionOutPoints using BitcoinClient.listUnspent().
      Parameters:
      fromAddress - Address to get UTXOs for
      Returns:
      All unspent TransactionOutPoints for fromAddress
      Throws:
      JsonRpcStatusException
      IOException