utils.bitcoin package¶
utils.bitcoin.tools module¶
-
utils.bitcoin.tools.btc_address_from_cert(certificate)¶ Gets a bitcoin address from a PaySense x.509 certificate (stored in the CN field of it).
Parameters: certificate (str) – path of a file containing a X.509 certificate. Returns: The corresponding bitcoin address. Return type: str
-
utils.bitcoin.tools.get_balance(bitcoin_address, network='testnet')¶ Gets the balance of a given bitcoin address from a given network.
Parameters: - bitcoin_address – bitcoin address from which the balance will be calculated.
- network (str) – bitcoin network where the address comes from (testnet by default).
Type: str
Returns: The bitcoin address balance (in Satoshi).
Return type: int
-
utils.bitcoin.tools.get_necessary_amount(unspent_transactions, amount, size='small')¶ Calculates the minimum necessary amount needed to pay the required bitcoins of a transaction.
Parameters: - unspent_transactions (list) – list of the unspent transactions from a bitcoin address.
- amount (int) – desired amount to be paid.
- size (str) –
represent the size of the bitcoin groups that will be chosen to pay the transaction.
- the unspent bitcoins will be chosen from the bigger groups to the smaller ones if size is ‘big’.
- them will be chosen in the opposite way otherwise.
Returns: A list with the unspent bitcoins to be used, and the total amount of them (that will be at least equal to amount).
Return type: list, int
-
utils.bitcoin.tools.get_priv_key_hex(pk_file_path)¶ Gets the EC private key in hexadecimal format from a key file.
Parameters: pk_file_path (str) – system path where the EC private key is found. Returns: private key. Return type: hex str
-
utils.bitcoin.tools.get_pub_key_hex(public_key)¶ Gets a public key in hexadecimal format from a OpenSSL public key object.
Parameters: public_key (OpenSSL.PublicKey) – public key. Returns: public key. Return type: hex str
-
utils.bitcoin.tools.hash_160(public_key)¶ Calculates the RIPEMD-160 hash of a given elliptic curve key.
Parameters: public_key (hex str) – elliptic public key (in hexadecimal format). Returns: The RIPEMD-160 hash. Return type: bytes
-
utils.bitcoin.tools.hash_160_to_btc_address(h160, v)¶ Calculates the bitcoin address of a given RIPEMD-160 hash from a elliptic curve public key.
Parameters: - h160 (bytes) – RIPEMD-160 hash.
- v (int) –
version (prefix) used to calculate the bitcoin address.
Possible values:
- 0 for main network (PUBKEY_HASH).
- 111 For testnet (TESTNET_PUBKEY_HASH).
Returns: The corresponding bitcoin address.
Return type: hex str
-
utils.bitcoin.tools.private_key_to_wif(private_key, mode='text', v='main')¶ Generates a WIF representation of a provided private key.
Parameters: - private_key (hex str) – elliptic curve key.
- mode (str) – defines the type of return.
- v (str) – version (prefix) used to calculate the WIF, it depends on the type of network.
Returns: The WIF representation of the private key.
- testnet WIF is v is ‘test’.
- main network WIF otherwise.
Return type: - str if mode is ‘text’.
- qrcode otherwise.
-
utils.bitcoin.tools.public_key_to_btc_address(public_key, v='main')¶ Calculates the bitcoin address of a given elliptic curve public key
Parameters: - public_key (hex str) – elliptic curve public key.
- v (str) – version used to calculate the bitcoin address.
Returns: The corresponding bitcoin address.
- testnet bitcoin address if v is ‘test’.
- main network address otherwise.
Return type: hex str
-
utils.bitcoin.tools.split_btc(btc_address, private_key, amount, parts, size='small', fee=False)¶ Split a large amount of bitcoins in smaller parts.
Parameters: - btc_address (str) – bitcoin address used as a source and destination of the transaction.
- private_key (hex str) – private key used to sign the transaction.
- amount (int) – amount of each one of the parts.
- parts (int) – number of parts of :param amount generated.
- size (str) – size of the unspent bitcoins that will be split.
- the smaller unspent bitcoins will be split is size is ‘small’.
- the bigger ones will be split otherwise.
Parameters: fee – defines if the transaction will pay fees or not. If fee is ‘True’ a fee of :param amount will be payed. Returns: The response of the bitcoin network to the bitcoin split transaction. Return type: str
utils.bitcoin.transactions module¶
-
utils.bitcoin.transactions.check_txs_source(btc_address, dcs_address, certs_path)¶ - Checks if the sources of the funds of the provided bitcoin address are valid.
Valid sources are:
- A previous certified CS (just for the first transaction in the address history).
- The DCS.
Parameters: - btc_address (str) – bitcoin address that will be checked.
- dcs_address (str) – bitcoin address of the DCS.
- certs_path (str) – path to the folder in which the certificates are stored.
Returns: True if the sources are valid. False otherwise.
Return type: bool
-
utils.bitcoin.transactions.get_tx_info(tx)¶ Gets the basic information from a given bitcoin transaction of the testnet.
Parameters: tx (unicode) – transaction from where the information is requested. Returns: A dictionary containing the input address, the output address, the bitcoin amount transferred in the transaction and the number of confirmations. Return type: dict
-
utils.bitcoin.transactions.get_tx_signature(tx, private_key, btc_address, hashcode=1)¶ Computes the signature from a given transaction.
Parameters: - tx (unicode) – input transaction.
- private_key (hex str) – elliptic curve private key used to sign.
- btc_address (str) – bitcoin address used as “from” in the transaction (Where the funds came from).
- hashcode (int) –
indicates which parts of the transaction will be signed. It is set to all by default.
Possible values:
- 1 (SIGHASH_ALL)
- 2 (SIGHASH_NONE)
- 3 (SIGHASH_SINGLE)
Returns: The signature of the transaction and the index where it should be placed, or an error and a error code, if there’s no transaction to sign.
Return type: str, int
-
utils.bitcoin.transactions.history_testnet(btc_address)¶ Gets the history of transaction from a given bitcoin address from the testnet. This function is analogous to the vbuterin’s history function from the bitcointools library (used all over the code) but using testnet instead of main bitcoin network.
Parameters: btc_address (str) – given bitcoin address. Returns: The history of transaction from the given address, limited to 200 (from the blockr.io api). Return type: list
-
utils.bitcoin.transactions.insert_tx_signature(tx, index, signature, public_key)¶ Inserts a given transaction signature into a given transaction.
Parameters: - tx (unicode) – input transaction.
- index (int) – input index of the transaction in which the signature must be placed.
- signature (str) – signature to be inserted.
- public_key (hex str) – elliptic curve public key used to insert the signature in the corresponding input.
Returns: The transaction with the signature inserted.
Return type: unicode
-
utils.bitcoin.transactions.is_spent(tx_hash, index)¶ Checks if a certain output of a transaction is spent.
Parameters: - tx_hash (str) – hash of the transaction to be checked.
- index (int) – index of the transaction output that will be check.
Returns: True if the output is spent, False otherwise.
Return type: bool
-
utils.bitcoin.transactions.local_push(tx, rpc_user=None, rpc_password=None)¶ Pushes a bitcoin transaction to the network using a local rpc server.
Parameters: - tx (hex str) – transaction to be pushed.
- rpc_user (str) – rpc user (could be set in bitcoin.conf).
- rpc_password (str) – rpc password ((could be set in bitcoin.conf).
Returns: The response of the rpc server, corresponding to the transaction id and a 200 code if its correctly pushed. None and code 500 otherwise
Return type: str, int
-
utils.bitcoin.transactions.push_tx(tx, network='testnet', fee=False)¶ Pushes a transaction to the bitcoin network (the testnet by default) with 0 fees.
Parameters: - tx (unicode) – transaction to be pushed.
- network (str) – network where the transaction will be pushed.
- fee (bool) – if set a fee will be applied to the transaction.
Returns: A result consisting on a code (201 if success), a response reason, and the hash of the transaction.
Return type: int, str, str
-
utils.bitcoin.transactions.reputation_transfer(s_key, source_btc_address, destination_btc_address, amount, outside_btc_address=None, outside_amount=None, fee=0, used_txs=None)¶ Performs a reputation transfer between a source and a destination bitcoin address.
Parameters: - s_key (str) – path to a private a elliptic curve private key.
- source_btc_address (str) – source bitcoin address, where the bitcoins came from.
- destination_btc_address (str) – destination bitcoin address where the bitcoins will be transferred.
- amount (int) – bitcoin amount to be transferred from the source to the destination bitcoin address.
- used_txs (list) – list of used (but still not verified transactions), it is set to None by default. If a list of transaction is passed, they won’t be used to perform the reputation transfer.
- outside_btc_address (str) – bitcoin address where the withdrawal amount of bitcoin will go, it is set to None by default.
- outside_amount (int) – amount of bitcoin that will be withdrawn, it is set to None by default.
- fee (int) – transaction fee to be paid, it is set to 0 Satoshi by default.
Returns: The hash of the created transaction and an updated list of used transactions.
Return type: str, list