Library Reference¶
driver
¶
-
class
bigchaindb_driver.
BigchainDB
(*nodes, transport_class=<class 'bigchaindb_driver.transport.Transport'>)[source]¶ BigchainDB driver class.
A
BigchainDB
driver is initialized with a keypair and is able to create, sign, and submit transactions to a Node in the Federation. At the moment, a BigchainDB driver instance is bounded to a specificnode
in the Federation. In the future, aBigchainDB
driver instance might connect to>1
nodes.-
__init__
(*nodes, transport_class=<class 'bigchaindb_driver.transport.Transport'>)[source]¶ Initialize a
BigchainDB
driver instance.If a
verifying_key
orsigning_key
are given, this instance will be bound to the keys and applied them as defaults whenever a verifying and/or signing key are needed.Parameters:
-
transactions
¶ TransactionsEndpoint
: Exposes functionalities of the ‘/transactions’ endpoint.
-
transport
¶ Transport
: Object responsible for forwarding requests to aConnection
instance (node).
-
-
class
bigchaindb_driver.driver.
TransactionsEndpoint
(driver)[source]¶ Endpoint for transactions.
-
path
¶ str
The path of the endpoint.
-
__init__
(driver)¶ Initializes an instance of
NamespacedDriver
with the given driver instance.Parameters: driver (BigchainDB) – Instance of BigchainDB
.
-
static
fulfill
(transaction, private_keys)[source]¶ Fulfills the given transaction.
Parameters: Returns: The fulfilled transaction payload, ready to be sent to a BigchainDB federation.
Return type: Raises: MissingSigningKeyError
– If a private key, (aka signing key), is missing.
-
static
prepare
(*, operation='CREATE', owners_before=None, owners_after=None, asset=None, metadata=None, inputs=None)[source]¶ Prepares a transaction payload, ready to be fulfilled.
Parameters: - operation (str) – The operation to perform. Must be
'CREATE'
or'TRANSFER'
. Case insensitive. Defaults to'CREATE'
. - owners_before (
list
|tuple
|str
, optional) – One or more public keys representing the issuer(s) of the asset being created. Only applies for'CREATE'
operations. Defaults toNone
. - owners_after (
list
|tuple
|str
, optional) – One or more public keys representing the new owner(s) of the asset being created or transferred. Defaults toNone
. - asset (
dict
, optional) – The asset being created or transferred. MUST be supplied for'TRANSFER'
operations. Defaults toNone
. - metadata (
dict
, optional) – Metadata associated with the transaction. Defaults toNone
. - inputs (
dict
|list
|tuple
, optional) – One or more inputs holding the condition(s) that this transaction intends to fulfill. Each input is expected to be adict
. Only applies to, and MUST be supplied for,'TRANSFER'
operations.
Returns: The prepared transaction.
Return type: Raises: BigchaindbException
– Ifoperation
is not'CREATE'
or'TRANSFER'
.Important
CREATE operations
owners_before
MUST be set.owners_after
,asset
, andmetadata
MAY be set.The argument
inputs
is ignored.If
owners_after
is not given, or evaluates toFalse
, it will be set equal toowners_before
:if not owners_after: owners_after = owners_before
TRANSFER operations
owners_after
,asset
, andinputs
MUST be set.metadata
MAY be set.- The argument
owners_before
is ignored.
- operation (str) – The operation to perform. Must be
-
retrieve
(txid)[source]¶ Retrieves the transaction with the given id.
Parameters: txid (str) – Id of the transaction to retrieve. Returns: The transaction with the given id. Return type: dict
-
-
class
bigchaindb_driver.driver.
NamespacedDriver
(driver)[source]¶ Base class for creating endpoints (namespaced objects) that can be added under the
BigchainDB
driver.-
__init__
(driver)[source]¶ Initializes an instance of
NamespacedDriver
with the given driver instance.Parameters: driver (BigchainDB) – Instance of BigchainDB
.
-
offchain
¶
Module for operations that can be performed “offchain”, meaning without a connection to one or more BigchainDB federation nodes.
-
bigchaindb_driver.offchain.
prepare_transaction
(*, operation='CREATE', owners_before=None, owners_after=None, asset=None, metadata=None, inputs=None)[source]¶ Prepares a transaction payload, ready to be fulfilled. Depending on the value of
operation
simply dispatches to eitherprepare_create_transaction()
orprepare_transfer_transaction()
.Parameters: - operation (str) – The operation to perform. Must be
'CREATE'
or'TRANSFER'
. Case insensitive. Defaults to'CREATE'
. - owners_before (
list
|tuple
|str
, optional) – One or more public keys representing the issuer(s) of the asset being created. Only applies for'CREATE'
operations. Defaults toNone
. - owners_after (
list
|tuple
|str
, optional) – One or more public keys representing the new owner(s) of the asset being created or transferred. Defaults toNone
. - asset (
dict
, optional) – The asset being created or transferred. MUST be supplied for'TRANSFER'
operations. Defaults toNone
. - metadata (
dict
, optional) – Metadata associated with the transaction. Defaults toNone
. - inputs (
dict
|list
|tuple
, optional) – One or more inputs holding the condition(s) that this transaction intends to fulfill. Each input is expected to be adict
. Only applies to, and MUST be supplied for,'TRANSFER'
operations.
Returns: The prepared transaction.
Return type: Raises: BigchaindbException
– Ifoperation
is not'CREATE'
or'TRANSFER'
.Important
CREATE operations
owners_before
MUST be set.owners_after
,asset
, andmetadata
MAY be set.The argument
inputs
is ignored.If
owners_after
is not given, or evaluates toFalse
, it will be set equal toowners_before
:if not owners_after: owners_after = owners_before
TRANSFER operations
owners_after
,asset
, andinputs
MUST be set.metadata
MAY be set.- The argument
owners_before
is ignored.
- operation (str) – The operation to perform. Must be
-
bigchaindb_driver.offchain.
prepare_create_transaction
(*, owners_before, owners_after=None, asset=None, metadata=None)[source]¶ Prepares a
"CREATE"
transaction payload, ready to be fulfilled.Parameters: - owners_before (
list
|tuple
|str
) – One or more public keys representing the issuer(s) of the asset being created. - owners_after (
list
|tuple
|str
, optional) – One or more public keys representing the new owner(s) of the asset being created. Defaults toNone
. - asset (
dict
, optional) – The asset being created. Defaults toNone
. - metadata (
dict
, optional) – Metadata associated with the transaction. Defaults toNone
.
Returns: The prepared
"CREATE"
transaction.Return type: Important
If
owners_after
is not given, or evaluates toFalse
, it will be set equal toowners_before
:if not owners_after: owners_after = owners_before
- owners_before (
-
bigchaindb_driver.offchain.
prepare_transfer_transaction
(*, inputs, owners_after, asset, metadata=None)[source]¶ Prepares a
"TRANSFER"
transaction payload, ready to be fulfilled.Parameters: - inputs (
dict
|list
|tuple
) – One or more inputs holding the condition(s) that this transaction intends to fulfill. Each input is expected to be adict
. - owners_after (
str
|list
|tuple
) – One or more public keys representing the new owner(s) of the asset being transferred. - asset (
dict
) – The asset being transferred. - metadata (
dict
) – Metadata associated with the transaction. Defaults toNone
.
Returns: The prepared
"TRANSFER"
transaction.Return type: Example
In case it may not be clear what an input should look like, say Alice (public key:
'3Cxh1eKZk3Wp9KGBWFS7iVde465UvqUKnEqTg2MW4wNf'
) wishes to transfer an asset over to Bob (public key:'EcRawy3Y22eAUSS94vLF8BVJi62wbqbD9iSUSUNU9wAA'
). Let the asset creation transaction payload be denoted bytx
:# noqa E501 >>> tx {'id': '57cff2b9490468bdb6d4767a1b07905fdbe18d638d9c7783f639b4b2bc165c39', 'transaction': {'asset': {'data': {'msg': 'Hello BigchainDB!'}, 'divisible': False, 'id': 'd04b05de-774c-4f81-9e54-6c19ed3cd18d', 'refillable': False, 'updatable': False}, 'conditions': [{'amount': 1, 'cid': 0, 'condition': {'details': {'bitmask': 32, 'public_key': '3Cxh1eKZk3Wp9KGBWFS7iVde465UvqUKnEqTg2MW4wNf', 'signature': None, 'type': 'fulfillment', 'type_id': 4}, 'uri': 'cc:4:20:IMe7QSL5xRAYIlXon76ZonWktR0NI02M8rAG1bN-ugg:96'}, 'owners_after': ['3Cxh1eKZk3Wp9KGBWFS7iVde465UvqUKnEqTg2MW4wNf']}], 'fulfillments': [{'fid': 0, 'fulfillment': 'cf:4:IMe7QSL5xRAYIlXon76ZonWktR0NI02M8rAG1bN-ughA8-9lUJYc_LGAB_NtyTPCCV58LfMcNZ9-0PUB6m1y_6pgTbCOQFBEeDtm_nC293CbpZjziwq7j3skrzS-OiAI', 'input': None, 'owners_before': ['3Cxh1eKZk3Wp9KGBWFS7iVde465UvqUKnEqTg2MW4wNf']}], 'metadata': None, 'operation': 'CREATE', 'timestamp': '1479393278'}, 'version': 1}
Then, the input may be constructed in this way:
cid = 0 condition = tx['transaction']['conditions'][cid] input_ = { 'fulfillment': condition['condition']['details'], 'input': { 'cid': cid, 'txid': tx['id'], }, 'owners_before': condition['owners_after'], }
Displaying the input on the prompt would look like:
>>> input_ {'fulfillment': {'bitmask': 32, 'public_key': '3Cxh1eKZk3Wp9KGBWFS7iVde465UvqUKnEqTg2MW4wNf', 'signature': None, 'type': 'fulfillment', 'type_id': 4}, 'input': {'cid': 0, 'txid': '57cff2b9490468bdb6d4767a1b07905fdbe18d638d9c7783f639b4b2bc165c39'}, 'owners_before': ['3Cxh1eKZk3Wp9KGBWFS7iVde465UvqUKnEqTg2MW4wNf']}
To prepare the transfer:
>>> prepare_transfer_transaction( ... inputs=input_, ... owners_after='EcRawy3Y22eAUSS94vLF8BVJi62wbqbD9iSUSUNU9wAA', ... asset=tx['transaction']['asset'], ... )
- inputs (
-
bigchaindb_driver.offchain.
fulfill_transaction
(transaction, *, private_keys)[source]¶ Fulfills the given transaction.
Parameters: Returns: The fulfilled transaction payload, ready to be sent to a BigchainDB federation.
Return type: Raises: MissingSigningKeyError
– If a private key, (aka signing key), is missing.
transport
¶
-
class
bigchaindb_driver.transport.
Transport
(*nodes)[source]¶ Transport class.
-
forward_request
(method, path=None, json=None)[source]¶ Forwards an http request to a connection.
Parameters: Returns: Result of
requests.models.Response.json()
Return type:
-
get_connection
()[source]¶ Gets a connection from the pool.
Returns: A Connection
instance.
-
pool
¶
-
class
bigchaindb_driver.pool.
Pool
(connections, picker_class=<class 'bigchaindb_driver.pool.RoundRobinPicker'>)[source]¶ Pool of connections.
-
__init__
(connections, picker_class=<class 'bigchaindb_driver.pool.RoundRobinPicker'>)[source]¶ Initializes a
Pool
instance.Parameters: connections (list) – List of Connection
instances.
-
get_connection
()[source]¶ Gets a
Connection
instance from the pool.Returns: A Connection
instance.
-
-
class
bigchaindb_driver.pool.
RoundRobinPicker
[source]¶ Object to pick a
Connection
instance from a list of connections.-
picked
¶ str
List index of
Connection
instance that has been picked.
-
__init__
()[source]¶ Initializes a
RoundRobinPicker
instance. Setspicked
to -1.
-
pick
(connections)[source]¶ Picks a
Connection
instance from the given list ofConnection
instances.Parameters: connections (List) – List of Connection
instances.
-
-
class
bigchaindb_driver.pool.
AbstractPicker
[source]¶ Abstract class for picker classes that pick connections from a pool.
-
pick
(connections)[source]¶ Picks a
Connection
instance from the given list ofConnection
instances.Parameters: connections (List) – List of Connection
instances.
-
connection
¶
-
class
bigchaindb_driver.connection.
Connection
(*, node_url)[source]¶ A Connection object to make HTTP requests.
-
__init__
(*, node_url)[source]¶ Initializes a
Connection
instance.Parameters: node_url (str) – Url of the node to connect to.
-
crypto
¶
-
class
bigchaindb_driver.crypto.
CryptoKeypair
(signing_key, verifying_key)¶ -
__getnewargs__
()¶ Return self as a plain tuple. Used by copy and pickle.
-
__getstate__
()¶ Exclude the OrderedDict from pickling
-
static
__new__
(_cls, signing_key, verifying_key)¶ Create new instance of CryptoKeypair(signing_key, verifying_key)
-
__repr__
()¶ Return a nicely formatted representation string
-
signing_key
¶ Alias for field number 0
-
verifying_key
¶ Alias for field number 1
-
-
bigchaindb_driver.crypto.
generate_keypair
()[source]¶ Generates a cryptographic key pair.
Returns: A collections.namedtuple
with named fieldssigning_key
andverifying_key
.Return type: CryptoKeypair
exceptions
¶
Exceptions used by bigchaindb_driver
.
-
exception
bigchaindb_driver.exceptions.
BigchaindbException
[source]¶ Base exception for all Bigchaindb exceptions.
-
exception
bigchaindb_driver.exceptions.
TransportError
[source]¶ Base exception for transport related errors.
This is mainly for cases where the status code denotes an HTTP error, and for cases in which there was a connection error.
-
exception
bigchaindb_driver.exceptions.
ConnectionError
[source]¶ Exception for errors occurring when connecting, and/or making a request to Bigchaindb.
-
exception
bigchaindb_driver.exceptions.
KeypairNotFoundException
[source]¶ Raised if an operation cannot proceed because the keypair was not given.
-
exception
bigchaindb_driver.exceptions.
InvalidSigningKey
[source]¶ Raised if a signing key is invalid. E.g.:
None
.
utils
¶
Set of utilities to support various functionalities of the driver.
-
bigchaindb_driver.utils.
ops_map
¶ dict
Mapping between operation strings and classes. E.g.: The string
'CREATE'
is mapped toCreateOperation
.
-
class
bigchaindb_driver.utils.
CreateOperation
[source]¶ Class representing the
'CREATE'
transaction operation.
-
class
bigchaindb_driver.utils.
TransferOperation
[source]¶ Class representing the
'TRANSFER'
transaction operation.
-
bigchaindb_driver.utils.
_normalize_asset
(asset, is_transfer=False)[source]¶ Normalizes the given asset dictionary.
For now, this means converting the given asset dictionary to a
Asset
class.Parameters: - asset (dict) – The asset to normalize.
- is_transfer (boal, optional) – Flag used to indicate whether the
asset is to be used as part of a ‘TRANSFER’ operation or
not. Defaults to
False
.
Returns: The
Asset
class, instantiated from the given asset dictionary.Important
If the instantiation step fails,
None
may be returned.Danger
For specific internal usage only. The behavior is tricky, and is subject to change.
-
bigchaindb_driver.utils.
_normalize_operation
(operation)[source]¶ Normalizes the given operation string. For now, this simply means converting the given string to uppercase, looking it up in
ops_map
, and returning the corresponding class if present.Parameters: operation (str) – The operation string to convert. Returns: The class corresponding to the given string, CreateOperation
orTransferOperation
.Important
If the
str.upper()
step, or theops_map
lookup fails, the givenoperation
argument is returned.Danger
For specific internal usage only. The behavior is tricky, and is subject to change.