bitcoinrpc.connection — Connect to Bitcoin server via JSON-RPC

Connect to Bitcoin server via JSON-RPC.

class bitcoinrpc.connection.BitcoinConnection(user, password, host='localhost', port=8332, use_https=False)

Bases: object

A BitcoinConnection object defines a connection to a bitcoin server. It is a thin wrapper around a JSON-RPC API connection.

Up-to-date for SVN revision 198.

Arguments to constructor:

  • user – Authenticate as user.
  • password – Authentication password.
  • host – Bitcoin JSON-RPC host.
  • port – Bitcoin JSON-RPC port.
backupwallet(destination)

Safely copies wallet.dat to destination, which can be a directory or a path with filename.

Arguments: - destination – directory or path with filename to backup wallet to.

createrawtransaction(inputs, outputs)

Creates a raw transaction spending given inputs (a list of dictionaries, each containing a transaction id and an output number), sending to given address(es).

Returns hex-encoded raw transaction.

Example usage: >>> conn.createrawtransaction(

[{“txid”: “a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c”,
“vout”: 0}],

{“mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT”:50})

Arguments:

  • inputs – A list of {“txid”: txid, “vout”: n} dictionaries.

  • outputs – A dictionary mapping (public) addresses to the amount

    they are to be paid.

decoderawtransaction(hexstring)

Produces a human-readable JSON object for a raw transaction.

Arguments:

  • hexstring – A hex string of the transaction to be decoded.
getaccount(bitcoinaddress)

Returns the account associated with the given address.

Arguments:

  • bitcoinaddress – Bitcoin address to get account for.
getaccountaddress(account)

Returns the current bitcoin address for receiving payments to an account.

Arguments:

  • account – Account for which the address should be returned.
getaddressesbyaccount(account)

Returns the list of addresses for the given account.

Arguments:

  • account – Account to get list of addresses for.
getbalance(account=None, minconf=None)

Get the current balance, either for an account or the total server balance.

Arguments: - account – If this parameter is specified, returns the balance in the account. - minconf – Minimum number of confirmations required for transferred balance.

getblock(hash)

Returns information about the given block hash.

getblockcount()

Returns the number of blocks in the longest block chain.

getblockhash(index)

Returns hash of block in best-block-chain at index.

Parameters:index – index ob the block
getblocknumber()

Returns the block number of the latest block in the longest block chain. Deprecated. Use getblockcount instead.

getconnectioncount()

Returns the number of connections to other nodes.

getdifficulty()

Returns the proof-of-work difficulty as a multiple of the minimum difficulty.

getgenerate()

Returns True or False, depending on whether generation is enabled.

gethashespersec()

Returns a recent hashes per second performance measurement while generating.

getinfo()

Returns an ServerInfo object containing various state info.

getmininginfo()

Returns an MiningInfo object containing various mining state info.

getnewaddress(account=None)

Returns a new bitcoin address for receiving payments.

Arguments:

  • account – If account is specified (recommended), it is added to the address book so that payments received with the address will be credited to it.
getrawtransaction(txid, verbose=True)

Get transaction raw info

Arguments:

  • txid – Transactiond id for which the info should be returned.
  • verbose – If False, return only the “hex” of the transaction.
getreceivedbyaccount(account, minconf=1)

Returns the total amount received by addresses with an account in transactions with at least a certain number of confirmations.

Arguments:

  • account – Account to query for total amount.
  • minconf – Number of confirmations to require, defaults to 1.
getreceivedbyaddress(bitcoinaddress, minconf=1)

Returns the total amount received by a bitcoin address in transactions with at least a certain number of confirmations.

Arguments:

  • bitcoinaddress – Address to query for total amount.
  • minconf – Number of confirmations to require, defaults to 1.
gettransaction(txid)

Get detailed information about transaction

Arguments:

  • txid – Transactiond id for which the info should be returned
getwork(data=None)

Get work for remote mining, or submit result. If data is specified, the server tries to solve the block using the provided data and returns True if it was successful. If not, the function returns formatted hash data (WorkItem) to work on.

Arguments:

  • data – Result from remote mining.
keypoolrefill()

Fills the keypool, requires wallet passphrase to be set.

listaccounts(minconf=1, as_dict=False)

Returns a list of account names.

Arguments:

  • minconf – Minimum number of confirmations before payments are included.
  • as_dict – Returns a dictionary of account names, with their balance as values.
listreceivedbyaccount(minconf=1, includeempty=False)

Returns a list of accounts.

Each account is represented with a AccountInfo object.

Arguments:

  • minconf – Minimum number of confirmations before payments are included.
  • includeempty – Whether to include addresses that haven’t received any payments.
listreceivedbyaddress(minconf=1, includeempty=False)

Returns a list of addresses.

Each address is represented with a AddressInfo object.

Arguments:

  • minconf – Minimum number of confirmations before payments are included.
  • includeempty – Whether to include addresses that haven’t received any payments.
listtransactions(account=None, count=10, from_=0, address=None)

Returns a list of the last transactions for an account.

Each transaction is represented with a TransactionInfo object.

Arguments:

  • account – Account to list transactions from. Return transactions from

    all accounts if None.

  • count – Number of transactions to return.

  • from_ – Skip the first <from_> transactions.

  • address – Receive address to consider

listunspent(minconf=1, maxconf=999999)

Returns a list of unspent transaction inputs in the wallet.

Arguments:

  • minconf – Minimum number of confirmations required to be listed.
  • maxconf – Maximal number of confirmations allowed to be listed.
move(fromaccount, toaccount, amount, minconf=1, comment=None)

Move from one account in your wallet to another.

Arguments:

  • fromaccount – Source account name.
  • toaccount – Destination account name.
  • amount – Amount to transfer.
  • minconf – Minimum number of confirmations required for transferred balance.
  • comment – Comment to add to transaction log.
sendfrom(fromaccount, tobitcoinaddress, amount, minconf=1, comment=None, comment_to=None)

Sends amount from account’s balance to bitcoinaddress. This method will fail if there is less than amount bitcoins with minconf confirmations in the account’s balance (unless account is the empty-string-named default account; it behaves like the sendtoaddress method). Returns transaction ID on success.

Arguments:

  • fromaccount – Account to send from.
  • tobitcoinaddress – Bitcoin address to send to.
  • amount – Amount to send (float, rounded to the nearest 0.01).
  • minconf – Minimum number of confirmations required for transferred balance.
  • comment – Comment for transaction.
  • comment_to – Comment for to-address.
sendmany(fromaccount, todict, minconf=1, comment=None)

Sends specified amounts from account’s balance to bitcoinaddresses. This method will fail if there is less than total amount bitcoins with minconf confirmations in the account’s balance (unless account is the empty-string-named default account; Returns transaction ID on success.

Arguments:

  • fromaccount – Account to send from.
  • todict – Dictionary with Bitcoin addresses as keys and amounts as values.
  • minconf – Minimum number of confirmations required for transferred balance.
  • comment – Comment for transaction.
sendtoaddress(bitcoinaddress, amount, comment=None, comment_to=None)

Sends amount from the server’s available balance to bitcoinaddress.

Arguments:

  • bitcoinaddress – Bitcoin address to send to.
  • amount – Amount to send (float, rounded to the nearest 0.01).
  • minconf – Minimum number of confirmations required for transferred balance.
  • comment – Comment for transaction.
  • comment_to – Comment for to-address.
setaccount(bitcoinaddress, account)

Sets the account associated with the given address.

Arguments:

  • bitcoinaddress – Bitcoin address to associate.
  • account – Account to associate the address to.
setgenerate(generate, genproclimit=None)

Enable or disable generation (mining) of coins.

Arguments:

  • generate – is True or False to turn generation on or off.
  • genproclimit – Number of processors that are used for generation, -1 is unlimited.
signrawtransaction(hexstring, previous_transactions=None, private_keys=None)

Sign inputs for raw transaction (serialized, hex-encoded).

Returns a dictionary with the keys:
“hex”: raw transaction with signature(s) (hex-encoded string) “complete”: 1 if transaction has a complete set of signature(s), 0 if not

Arguments:

  • hexstring – A hex string of the transaction to sign.

  • previous_transactions – A (possibly empty) list of dictionaries of the form:

    {“txid”: txid, “vout”: n, “scriptPubKey”: hex, “redeemScript”: hex}, representing previous transaction outputs that this transaction depends on but may not yet be in the block chain.

  • private_keys – A (possibly empty) list of base58-encoded private

    keys that, if given, will be the only keys used to sign the transaction.

stop()

Stop bitcoin server.

validateaddress(validateaddress)

Validate a bitcoin address and return information for it.

The information is represented by a AddressValidation object.

Arguments: – Address to validate.

  • validateaddress
verifymessage(bitcoinaddress, signature, message)

Verifies a signature given the bitcoinaddress used to sign, the signature itself, and the message that was signed. Returns True if the signature is valid, and False if it is invalid.

Arguments:

  • bitcoinaddress – the bitcoinaddress used to sign the message
  • signature – the signature to be verified
  • message – the message that was originally signed
walletlock()

Removes the wallet encryption key from memory, locking the wallet. After calling this method, you will need to call walletpassphrase again before being able to call any methods which require the wallet to be unlocked.

walletpassphrase(passphrase, timeout, dont_raise=False)

Stores the wallet decryption key in memory for <timeout> seconds.

  • passphrase – The wallet passphrase.

  • timeout – Time in seconds to keep the wallet unlocked

    (by keeping the passphrase in memory).

  • dont_raise – instead of raising ~bitcoinrpc.exceptions.WalletPassphraseIncorrect

    return False.

walletpassphrasechange(oldpassphrase, newpassphrase, dont_raise=False)

Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.

Arguments:

  • dont_raise – instead of raising ~bitcoinrpc.exceptions.WalletPassphraseIncorrect

    return False.

Previous topic

bitcoinrpc — Convenience functions

Next topic

bitcoinrpc.exceptions — Exception definitions

This Page