Package org.consensusj.bitcoin.jsonrpc
Class BitcoinExtendedClient
java.lang.Object
org.consensusj.jsonrpc.DefaultRpcClient
org.consensusj.bitcoin.jsonrpc.BitcoinClient
org.consensusj.bitcoin.jsonrpc.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.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.consensusj.jsonrpc.DefaultRpcClient
DefaultRpcClient.TransientErrorMapper<T>, DefaultRpcClient.TransportFactory
Nested classes/interfaces inherited from interface org.consensusj.jsonrpc.AsyncSupport
AsyncSupport.ThrowingSupplier<T>, AsyncSupport.TransientErrorFilter
-
Field Summary
Modifier and TypeFieldDescriptionstatic final org.bitcoinj.base.Address
final Integer
static final String
final org.bitcoinj.base.Coin
final org.bitcoinj.base.Coin
final long
Fields inherited from class org.consensusj.bitcoin.jsonrpc.BitcoinClient
BITCOIN_CORE_VERSION_DESC_DEFAULT, BITCOIN_CORE_VERSION_MIN
Fields inherited from class org.consensusj.jsonrpc.DefaultRpcClient
jsonRpcVersion, mapper
-
Constructor Summary
ConstructorDescriptionConstructor that uses bitcoin.conf to get connection information (Incubating)BitcoinExtendedClient
(URI server, String rpcuser, String rpcpassword) BitcoinExtendedClient
(SSLContext sslContext, org.bitcoinj.base.Network network, URI server, String rpcuser, String rpcpassword) BitcoinExtendedClient
(org.bitcoinj.base.Network network, URI server, String rpcuser, String rpcpassword) BitcoinExtendedClient
(RpcConfig config) -
Method Summary
Modifier and TypeMethodDescriptionList
<org.bitcoinj.base.Sha256Hash> Clears the memory pool and returns a list of the removed transactions.createRawTransaction
(org.bitcoinj.base.Address fromAddress, Map<org.bitcoinj.base.Address, org.bitcoinj.base.Coin> outputs) Creates a raw transaction, spending from a single address, whereby no new change address is created, and remaining amounts are returned tofromAddress
.createRawTransaction
(org.bitcoinj.base.Address fromAddress, org.bitcoinj.base.Address toAddress, org.bitcoinj.base.Coin amount) Creates a raw transaction, sendingamount
from a single address to a destination, whereby no new change address is created, and remaining amounts are returned tofromAddress
.org.bitcoinj.core.Transaction
createSignedTransaction
(org.bitcoinj.crypto.ECKey fromKey, List<org.bitcoinj.core.TransactionOutput> outputs) Create a signed transaction locally (i.e.org.bitcoinj.core.Transaction
createSignedTransaction
(org.bitcoinj.crypto.ECKey fromKey, org.bitcoinj.base.Address toAddress, org.bitcoinj.base.Coin amount) Create a signed transaction locally (i.e.List
<org.bitcoinj.base.Sha256Hash> generateBlocks
(int numBlocks) Generate blocks and funds (RegTest only) Use this to generate blocks and receive the block reward inthis.regTestMiningAddress
which can the be used to fund transactions in RegTest mode.org.bitcoinj.base.Coin
getBitcoinBalance
(org.bitcoinj.base.Address address) Returns the Bitcoin balance of an address (in the server-side wallet.)org.bitcoinj.base.Coin
getBitcoinBalance
(org.bitcoinj.base.Address address, Integer minConf) Returns the Bitcoin balance of an address (in the server-side wallet) where spendable outputs have at leastminConf
confirmations.org.bitcoinj.base.Coin
getBitcoinBalance
(org.bitcoinj.base.Address address, Integer minConf, Integer maxConf) Returns the Bitcoin balance of an address (in the server-side wallet) where spendable outputs have at leastminConf
and not more thanmaxConf
confirmations.org.bitcoinj.core.Block
getBlock
(int index) Returns information about a block at index provided.org.bitcoinj.base.Coin
getBlockSubsidy
(int height) Calculate the subsidy portion block reward for a given height on the current networkorg.bitcoinj.base.Address
org.bitcoinj.base.Coin
org.bitcoinj.base.Coin
void
Initialize a server-side wallet for RegTest mining and test transaction funding.List
<org.bitcoinj.core.TransactionOutput> listUnspentJ
(org.bitcoinj.base.Address fromAddress) Build a list of bitcoinjTransactionOutput
s usingBitcoinClient.listUnspent()
andBitcoinClient.getRawTransaction(org.bitcoinj.base.Sha256Hash)
RPCs.List
<org.bitcoinj.core.TransactionOutPoint> listUnspentOutPoints
(org.bitcoinj.base.Address fromAddress) Build a list of bitcoinjTransactionOutPoint
s usingBitcoinClient.listUnspent()
.org.bitcoinj.base.Sha256Hash
sendBitcoin
(org.bitcoinj.base.Address fromAddress, Map<org.bitcoinj.base.Address, org.bitcoinj.base.Coin> outputs) Sends BTC from an address to the destinations, whereby no new change address is created, and any leftover is returned to the sending address.org.bitcoinj.base.Sha256Hash
sendBitcoin
(org.bitcoinj.base.Address fromAddress, org.bitcoinj.base.Address toAddress, org.bitcoinj.base.Coin amount) Sends BTC from an address to a destination, whereby no new change address is created, and any leftover is returned to the sending address.withWallet
(String walletName, String rpcUser, String rpcPassword) Incubating: clone the client with a new base URI for a named wallet.Methods inherited from class org.consensusj.bitcoin.jsonrpc.BitcoinClient
addNode, close, commandExists, connectToServer, createRawTransaction, createWallet, createWallet, createWallet, dumpPrivKey, generateToAddress, generateToAddress, getAddedNodeInfo, getAddedNodeInfo, getAddressBalance, getAddressBalance, getAddressInfo, getAddressUtxos, getAddressUtxos, getBalance, getBalance, getBalance, getBestBlock, getBlock, getBlockAsync, getBlockChainInfo, getBlockChainInfoAsync, getBlockCount, getBlockHash, getBlockInfo, getChainTips, getChainTipsAsync, getCommands, getDefaultAsyncExecutor, getNetwork, getNetworkInfo, getNewAddress, getNewAddress, getRawTransaction, getRawTransactionInfo, getReceivedByAddress, getReceivedByAddress, getTransaction, getTransaction, getTxOut, getTxOut, getTxOutSetInfo, getZmqNotifications, getZmqNotificationsAsync, help, help, helpAsLines, helpAsMethodEntries, helpAsStream, importPrivKey, invalidateBlock, isAddressIndexEnabled, listAddressGroupings, listReceivedByAddress, listTransactions, listTransactions, listTransactions, listUnspent, listUnspent, listUnspent, listUnspent, listUnspent, listWallets, mapBitcoinTransientErrors, reconsiderBlock, sendFrom, sendMany, sendRawTransaction, sendRawTransaction, sendRawTransaction, sendRawTransaction, sendToAddress, sendToAddress, setTxFee, signRawTransactionWithWallet, stop, unloadWallet, unloadWallet, waitForBlock, waitForBlockchainInfoAsync, waitForConnected, waitForConnected, waitForServer, waitForServer, waitForServerAsync
Methods inherited from class org.consensusj.jsonrpc.DefaultRpcClient
collectionTypeForClasses, collectionTypeForClasses, defaultType, getJsonRpcVersion, getMapper, getServerURI, identityTransientErrorMapper, pollOnce, responseTypeFor, responseTypeFor, sendRequestForResponseAsync, sendRequestForResponseAsync, statusFromErrorResponse, temporarilyUnavailableResponse, typeForClass, waitForServer, waitForServer
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.consensusj.jsonrpc.AsyncSupport
supplyAsync, supplyAsync
Methods inherited from interface org.consensusj.jsonrpc.JsonRpcClient
buildJsonRequest, buildJsonRequest, send, send, send, send, send, send, sendAsync, sendAsync, sendAsync, sendAsync, sendAsync
Methods inherited from interface org.consensusj.jsonrpc.JsonRpcTransport
sendRequestForResponse, syncGet
-
Field Details
-
DEFAULT_REGTEST_MINING_ADDRESS
-
REGTEST_WALLET_NAME
- See Also:
-
stdTxFee
-
stdRelayTxFee
-
defaultMaxConf
-
stdTxFeeSatoshis
-
-
Constructor Details
-
BitcoinExtendedClient
public BitcoinExtendedClient(SSLContext sslContext, org.bitcoinj.base.Network network, URI server, String rpcuser, String rpcpassword) -
BitcoinExtendedClient
public BitcoinExtendedClient(org.bitcoinj.base.Network network, URI server, String rpcuser, String rpcpassword) -
BitcoinExtendedClient
-
BitcoinExtendedClient
-
BitcoinExtendedClient
public BitcoinExtendedClient()Constructor that uses bitcoin.conf to get connection information (Incubating)
-
-
Method Details
-
getStdTxFee
-
getStdRelayTxFee
-
getDefaultMaxConf
-
withWallet
Incubating: clone the client with a new base URI for a named wallet.- Parameters:
walletName
- wallet namerpcUser
- username must be provided because it is not accessible in the parent classrpcPassword
- 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
-
initRegTestWallet
Initialize a server-side wallet for RegTest mining and test transaction funding. Creates a non-descriptor wallet with nameREGTEST_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 inthis.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 brokeIOException
- something broke
-
getBlockSubsidy
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
Returns information about a block at index provided. Uses two RPCs:getblockhash
and thengetblock
.- Parameters:
index
- The block index- Returns:
- The information about the block
- Throws:
JsonRpcStatusException
- JSON RPC status exceptionIOException
- network error
-
clearMemPool
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 exceptionIOException
- network error
-
createRawTransaction
public String createRawTransaction(org.bitcoinj.base.Address fromAddress, Map<org.bitcoinj.base.Address, org.bitcoinj.base.Coin> outputs) throws JsonRpcStatusException, IOExceptionCreates a raw transaction, spending from a single address, whereby no new change address is created, and remaining amounts are returned tofromAddress
.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 fromoutputs
- 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, sendingamount
from a single address to a destination, whereby no new change address is created, and remaining amounts are returned tofromAddress
.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 fromtoAddress
- The destinationamount
- 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 leastminConf
confirmations.- Parameters:
address
- The addressminConf
- 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 leastminConf
and not more thanmaxConf
confirmations.- Parameters:
address
- The address (must be in wallet)minConf
- Minimum amount of confirmationsmaxConf
- 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 fromtoAddress
- The destination addressamount
- 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, IOExceptionSends 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 fromoutputs
- 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 keyoutputs
- Outputs to sign- Returns:
- A bitcoinj Transaction objects that is properly signed
- Throws:
JsonRpcStatusException
- A JSON-RPC error was returnedIOException
- 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 keytoAddress
- Destination addressamount
- Amount to send- Returns:
- A bitcoinj Transaction objects that is properly signed
- Throws:
JsonRpcStatusException
- A JSON-RPC error was returnedIOException
- 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 bitcoinjTransactionOutput
s usingBitcoinClient.listUnspent()
andBitcoinClient.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 bitcoinjTransactionOutPoint
s usingBitcoinClient.listUnspent()
.- Parameters:
fromAddress
- Address to get UTXOs for- Returns:
- All unspent TransactionOutPoints for fromAddress
- Throws:
JsonRpcStatusException
IOException
-