bitcoin¶
bitcoin¶
-
class
bitcoin.MainParams[source]¶ Bases:
bitcoin.core.CoreMainParams-
BASE58_PREFIXES= {u'PUBKEY_ADDR': 0, u'SCRIPT_ADDR': 5, u'SECRET_KEY': 128}¶
-
DEFAULT_PORT= 8333¶
-
DNS_SEEDS= ((u'bitcoin.sipa.be', u'seed.bitcoin.sipa.be'), (u'bluematt.me', u'dnsseed.bluematt.me'), (u'dashjr.org', u'dnsseed.bitcoin.dashjr.org'), (u'bitcoinstats.com', u'seed.bitcoinstats.com'), (u'xf2.org', u'bitseed.xf2.org'), (u'bitcoin.jonasschnelli.ch', u'seed.bitcoin.jonasschnelli.ch'))¶
-
MESSAGE_START= '\xf9\xbe\xb4\xd9'¶
-
RPC_PORT= 8332¶
-
-
class
bitcoin.RegTestParams[source]¶ Bases:
bitcoin.core.CoreRegTestParams-
BASE58_PREFIXES= {u'PUBKEY_ADDR': 111, u'SCRIPT_ADDR': 196, u'SECRET_KEY': 239}¶
-
DEFAULT_PORT= 18444¶
-
DNS_SEEDS= ()¶
-
MESSAGE_START= '\xfa\xbf\xb5\xda'¶
-
RPC_PORT= 18443¶
-
-
bitcoin.SelectParams(name)[source]¶ Select the chain parameters to use
name is one of ‘mainnet’, ‘testnet’, or ‘regtest’
Default chain is ‘mainnet’
-
class
bitcoin.TestNetParams[source]¶ Bases:
bitcoin.core.CoreTestNetParams-
BASE58_PREFIXES= {u'PUBKEY_ADDR': 111, u'SCRIPT_ADDR': 196, u'SECRET_KEY': 239}¶
-
DEFAULT_PORT= 18333¶
-
DNS_SEEDS= ((u'testnetbitcoin.jonasschnelli.ch', u'testnet-seed.bitcoin.jonasschnelli.ch'), (u'petertodd.org', u'seed.tbtc.petertodd.org'), (u'bluematt.me', u'testnet-seed.bluematt.me'), (u'bitcoin.schildbach.de', u'testnet-seed.bitcoin.schildbach.de'))¶
-
MESSAGE_START= '\x0b\x11\t\x07'¶
-
RPC_PORT= 18332¶
-
base58¶
Base58 encoding and decoding
-
exception
bitcoin.base58.InvalidBase58Error[source]¶ Bases:
bitcoin.base58.Base58ErrorRaised on generic invalid base58 data, such as bad characters.
Checksum failures raise Base58ChecksumError specifically.
-
exception
bitcoin.base58.Base58ChecksumError[source]¶ Bases:
bitcoin.base58.Base58ErrorRaised on Base58 checksum errors
-
class
bitcoin.base58.CBase58Data(s)[source]¶ Bases:
strBase58-encoded data
Includes a version and checksum.
Initialize from base58-encoded string
Note: subclasses put your initialization routines here, but ignore the argument - that’s handled by __new__(), and .from_bytes() will call __init__() with None in place of the string.
bloom¶
Bloom filter support
-
bitcoin.bloom.MurmurHash3(x86_32)[source]¶ Used for bloom filters. See http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
-
class
bitcoin.bloom.CBloomFilter(nElements, nFPRate, nTweak, nFlags)[source]¶ Bases:
bitcoin.core.serialize.SerializableCreate a new bloom filter
The filter will have a given false-positive rate when filled with the given number of elements.
Note that if the given parameters will result in a filter outside the bounds of the protocol limits, the filter created will be as close to the given parameters as possible within the protocol limits. This will apply if nFPRate is very low or nElements is unreasonably high.
nTweak is a constant which is added to the seed value passed to the hash function It should generally always be a random value (and is largely only exposed for unit testing)
nFlags should be one of the UPDATE_* enums (but not _MASK)
-
MAX_BLOOM_FILTER_SIZE= 36000¶
-
MAX_HASH_FUNCS= 50¶
-
UPDATE_ALL= 1¶
-
UPDATE_MASK= 3¶
-
UPDATE_NONE= 0¶
-
UPDATE_P2PUBKEY_ONLY= 2¶
-
messages¶
-
class
bitcoin.messages.msg_version(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'version'¶
-
-
class
bitcoin.messages.msg_verack(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'verack'¶
-
-
class
bitcoin.messages.msg_addr(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'addr'¶
-
-
class
bitcoin.messages.msg_alert(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'alert'¶
-
-
class
bitcoin.messages.msg_inv(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'inv'¶
-
-
class
bitcoin.messages.msg_getdata(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'getdata'¶
-
-
class
bitcoin.messages.msg_getblocks(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'getblocks'¶
-
-
class
bitcoin.messages.msg_getheaders(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'getheaders'¶
-
-
class
bitcoin.messages.msg_headers(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'headers'¶
-
-
class
bitcoin.messages.msg_tx(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'tx'¶
-
-
class
bitcoin.messages.msg_block(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'block'¶
-
-
class
bitcoin.messages.msg_getaddr(protover=60002)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'getaddr'¶
-
-
class
bitcoin.messages.msg_ping(protover=60002, nonce=0)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'ping'¶
-
-
class
bitcoin.messages.msg_pong(protover=60002, nonce=0)[source]¶ Bases:
bitcoin.messages.MsgSerializable-
command= 'pong'¶
-
net¶
-
class
bitcoin.net.CInv[source]¶ Bases:
bitcoin.core.serialize.Serializable-
typemap= {0: u'Error', 1: u'TX', 2: u'Block', 3: u'FilteredBlock', 4: u'CompactBlock'}¶
-
rpc¶
Bitcoin Core RPC support
By default this uses the standard library json module. By monkey patching,
a different implementation can be used instead, at your own risk:
>>> import simplejson
>>> import bitcoin.rpc
>>> bitcoin.rpc.json = simplejson
(simplejson is the externally maintained version of the same module and
thus better optimized but perhaps less stable.)
-
exception
bitcoin.rpc.JSONRPCError[source]¶ Bases:
exceptions.ExceptionJSON-RPC protocol error base class
Subclasses of this class also exist for specific types of errors; the set of all subclasses is by no means complete.
-
SUBCLS_BY_CODE= {-28: <class 'bitcoin.rpc.InWarmupError'>, -27: <class 'bitcoin.rpc.VerifyAlreadyInChainError'>, -26: <class 'bitcoin.rpc.VerifyRejectedError'>, -25: <class 'bitcoin.rpc.VerifyError'>, -8: <class 'bitcoin.rpc.InvalidParameterError'>, -5: <class 'bitcoin.rpc.InvalidAddressOrKeyError'>, -2: <class 'bitcoin.rpc.ForbiddenBySafeModeError'>}¶
-
-
exception
bitcoin.rpc.ForbiddenBySafeModeError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -2¶
-
-
exception
bitcoin.rpc.InvalidAddressOrKeyError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -5¶
-
-
exception
bitcoin.rpc.InvalidParameterError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -8¶
-
-
exception
bitcoin.rpc.VerifyError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -25¶
-
-
exception
bitcoin.rpc.VerifyRejectedError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -26¶
-
-
exception
bitcoin.rpc.VerifyAlreadyInChainError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -27¶
-
-
exception
bitcoin.rpc.InWarmupError[source]¶ Bases:
bitcoin.rpc.JSONRPCError-
RPC_ERROR_CODE= -28¶
-
-
class
bitcoin.rpc.RawProxy(service_url=None, service_port=None, btc_conf_file=None, timeout=30, **kwargs)[source]¶ Bases:
bitcoin.rpc.BaseProxyLow-level proxy to a bitcoin JSON-RPC service
Unlike
Proxy, no conversion is done besides parsing JSON. As far as Python is concerned, you can call any method;JSONRPCErrorwill be raised if the server does not recognize it.
-
class
bitcoin.rpc.Proxy(service_url=None, service_port=None, btc_conf_file=None, timeout=30, **kwargs)[source]¶ Bases:
bitcoin.rpc.BaseProxyProxy to a bitcoin RPC service
Unlike
RawProxy, data is passed asbitcoin.coreobjects or packed bytes, rather than JSON or hex strings. Not all methods are implemented yet; you can usecallto access missing ones in a forward-compatible way. Assumes Bitcoin Core version >= v0.16.0; older versions mostly work, but there are a few incompatibilities.Create a proxy object
If
service_urlis not specified, the username and password are read out of the filebtc_conf_file. Ifbtc_conf_fileis not specified,~/.bitcoin/bitcoin.confor equivalent is used by default. The default port is set according to the chain parameters in use: mainnet, testnet, or regtest.Usually no arguments to
Proxy()are needed; the local bitcoind will be used.timeout- timeout in seconds before the HTTP interface times out-
fundrawtransaction(tx, include_watching=False)[source]¶ Add inputs to a transaction until it has enough in value to meet its out value.
include_watching - Also select inputs which are watch only
Returns dict:
- {‘tx’: Resulting tx,
- ‘fee’: Fee the resulting transaction pays, ‘changepos’: Position of added change output, or -1,
}
-
generate(numblocks)[source]¶ Mine blocks immediately (before the RPC call returns)
numblocks - How many blocks are generated immediately.
Returns iterable of block hashes generated.
-
getaccountaddress(account=None)[source]¶ Return the current Bitcoin address for receiving payments to this account.
-
getbalance(account=u'*', minconf=1, include_watchonly=False)[source]¶ Get the balance
account - The selected account. Defaults to “*” for entire wallet. It may be the default account using “”.
minconf - Only include transactions confirmed at least this many times. (default=1)
include_watchonly - Also include balance in watch-only addresses (see ‘importaddress’) (default=False)
-
getblockhash(height)[source]¶ Return hash of block in best-block-chain at height.
Raises IndexError if height is not valid.
-
getblockheader(block_hash, verbose=False)[source]¶ Get block header <block_hash>
- verbose - If true a dict is returned with the values returned by
- getblockheader that are not in the block header itself (height, nextblockhash, etc.)
Raises IndexError if block_hash is not valid.
-
getnewaddress(account=None)[source]¶ Return a new Bitcoin address for receiving payments.
If account is not None, it is added to the address book so payments received with the address will be credited to account.
-
getrawchangeaddress()[source]¶ Returns a new Bitcoin address, for receiving change.
This is for use with raw transactions, NOT normal use.
-
getrawtransaction(txid, verbose=False)[source]¶ Return transaction with hash txid
Raises IndexError if transaction not found.
verbose - If true a dict is returned instead with additional information on the transaction.
Note that if all txouts are spent and the transaction index is not enabled the transaction may not be available.
-
getreceivedbyaddress(addr, minconf=1)[source]¶ Return total amount received by given a (wallet) address
Get the amount received by <address> in transactions with at least [minconf] confirmations.
Works only for addresses in the local wallet; other addresses will always show zero.
addr - The address. (CBitcoinAddress instance)
minconf - Only include transactions confirmed at least this many times. (default=1)
-
gettransaction(txid)[source]¶ Get detailed information about in-wallet transaction txid
Raises IndexError if transaction not found in the wallet.
FIXME: Returned data types are not yet converted.
-
gettxout(outpoint, includemempool=True)[source]¶ Return details about an unspent transaction output.
Raises IndexError if outpoint is not found or was spent.
includemempool - Include mempool txouts
-
importaddress(addr, label=u'', rescan=True)[source]¶ Adds an address or pubkey to wallet without the associated privkey.
-
listunspent(minconf=0, maxconf=9999999, addrs=None)[source]¶ Return unspent transaction outputs in wallet
Outputs will have between minconf and maxconf (inclusive) confirmations, optionally filtered to only include txouts paid to addresses in addrs.
-
sendmany(fromaccount, payments, minconf=1, comment=u'', subtractfeefromamount=[])[source]¶ Send amount to given addresses.
payments - dict with {address: amount}
-
sendrawtransaction(tx, allowhighfees=False)[source]¶ Submit transaction to local node and network.
allowhighfees - Allow even if fees are unreasonably high.
-
sendtoaddress(addr, amount, comment=u'', commentto=u'', subtractfeefromamount=False)[source]¶ Send amount to a given address
-
submitblock(block, params=None)[source]¶ Submit a new block to the network.
params is optional and is currently ignored by bitcoind. See https://en.bitcoin.it/wiki/BIP_0022 for full specification.
-
signature¶
signmessage¶
-
class
bitcoin.signmessage.BitcoinMessage(message=u'', magic=u'Bitcoin Signed Message:n')[source]¶ Bases:
bitcoin.core.serialize.ImmutableSerializable-
magic¶
-
message¶
-
wallet¶
Wallet-related functionality
Includes things like representing addresses and converting them to/from scriptPubKeys; currently there is no actual wallet support implemented.
-
exception
bitcoin.wallet.CBitcoinAddressError[source]¶ Bases:
bitcoin.base58.Base58ErrorRaised when an invalid Bitcoin address is encountered
-
class
bitcoin.wallet.CBitcoinAddress(s)[source]¶ Bases:
bitcoin.base58.CBase58DataA Bitcoin address
Initialize from base58-encoded string
Note: subclasses put your initialization routines here, but ignore the argument - that’s handled by __new__(), and .from_bytes() will call __init__() with None in place of the string.
-
class
bitcoin.wallet.P2SHBitcoinAddress(s)[source]¶ Bases:
bitcoin.wallet.CBitcoinAddressInitialize from base58-encoded string
Note: subclasses put your initialization routines here, but ignore the argument - that’s handled by __new__(), and .from_bytes() will call __init__() with None in place of the string.
-
classmethod
from_redeemScript(redeemScript)[source]¶ Convert a redeemScript to a P2SH address
Convenience function: equivalent to P2SHBitcoinAddress.from_scriptPubKey(redeemScript.to_p2sh_scriptPubKey())
-
classmethod
-
class
bitcoin.wallet.P2PKHBitcoinAddress(s)[source]¶ Bases:
bitcoin.wallet.CBitcoinAddressInitialize from base58-encoded string
Note: subclasses put your initialization routines here, but ignore the argument - that’s handled by __new__(), and .from_bytes() will call __init__() with None in place of the string.
-
classmethod
from_pubkey(pubkey, accept_invalid=False)[source]¶ Create a P2PKH bitcoin address from a pubkey
Raises CBitcoinAddressError if pubkey is invalid, unless accept_invalid is True.
The pubkey must be a bytes instance; CECKey instances are not accepted.
-
classmethod
from_scriptPubKey(scriptPubKey, accept_non_canonical_pushdata=True, accept_bare_checksig=True)[source]¶ Convert a scriptPubKey to a P2PKH address
Raises CBitcoinAddressError if the scriptPubKey isn’t of the correct form.
accept_non_canonical_pushdata - Allow non-canonical pushes (default True)
accept_bare_checksig - Treat bare-checksig as P2PKH scriptPubKeys (default True)
-
classmethod
-
class
bitcoin.wallet.CKey(secret, compressed=True)[source]¶ Bases:
objectAn encapsulated private key
Attributes:
pub - The corresponding CPubKey for this private key
is_compressed - True if compressed
-
is_compressed¶
-
-
exception
bitcoin.wallet.CBitcoinSecretError[source]¶ Bases:
bitcoin.base58.Base58Error
-
class
bitcoin.wallet.CBitcoinSecret(s)[source]¶ Bases:
bitcoin.base58.CBase58Data,bitcoin.wallet.CKeyA base58-encoded secret key