Module Reference¶
Colorvote¶
-
class
colorvote.Colorvote(config)¶ This is an interface class for the colorvote package.
-
create_commitment(key, message)¶ Returns a commitment for the given message based on the key.
-
create_id_tx(address, unit=1.0, meta='')¶ Creates an identification transaction to register an election.
To create an election we select an unspent output (UTXO) with specified address. Then we create a transaction with that UTXO as input, and two outputs. One to send the whole amount back to the same address, and one
OP_RETURNoutput that contains the election metadata.Parameters: - address (str) – The election address
- unit (float, optional) – The currency value of a single vote
- meta (str, optional) – A string of ASCII characters to include in transaction, up to 80 characters
Returns: A list containing the inputs and outputs as accepted by the createrawtransaction wallet command.
Return type: list
-
create_issue_tx(election, addresses, amounts)¶ Creates an issuing transaction to issue new votes for an existing election.
To issue coins we need to own an address that has made an identification transaction. The address is the color/ID of the election. This command looks up the identifiaction transaction to read the vote value (the amount of the currency that is equal to one vote). Then finds an unspent output with sufficient funds and sends them with an nSequence according to the protocol.
Parameters: - election (str) – Address of election
- addresses (list) – list of addresses to issue votes to
- amounts (list) – List of number of votes to issue to each address in
addresses
Returns: Transaction ID returned by wallet
Return type: str
-
create_transfer_tx(election, address, recepient, amount=1)¶ Creates a transfer transaction to send votes between addresses.
NOTE: Currently does not support combining votes from multiple UTXOs.
Parameters: - election (str) – The election ID to vote in
- address (str) – Address that currently holds the votes in the wallet
- recepient (str) – Address of the recepient of the votes
- amount (int) – Number of votes to send
Returns: Transaction ID returned by wallet
Return type: str
-
decode_vote_value(value)¶ Decodes the value of one vote as encoded in the second least significant byte of an init transaction.
Parameters: value (int) – The integer value of the byte Returns: The vote value in BTC Return type: float
-
encode_vote_value(amount)¶ Encodes the value of one vote as defined by the protocol. The encoded value needs to be included in the nSequence field of an init transaction.
Parameters: amount (float) – The amount in BTC to encode. Returns: The integer value of the encoded byte. Return type: int
-
generate_key()¶ Returns a randomly generated 256-bit integer.
-
get_election()¶ Returns the current status of an election including number of votes.
-
get_elections()¶ Returns a list of elections that have been found on the blockchain.
-
get_unspent()¶ Returns a list of unspent outputs from the wallet that have voting coins.
-
is_db_synced()¶ Returns whether the database is up to date with latest blocks.
-
read_id_tx(block, transaction)¶ Decode an identification transaction.
Parameters: transaction (dict) – A transaction as returned by the decoderawtransactionwallet commandReturns: A tuple (address, unit, metadata) Return type: tuple
-
read_issue_tx(block, transaction)¶ Decode an issuing transaction.
Currently the colorvote module only supports one input for issuing transactions although the protocol is more general (need to fix)
Parameters: transaction (dict) – A transaction as returned by the decoderawtransactionwallet commandReturns: A list of tuples (election, address, txid, n, votes) Return type: list
-
read_transfer_tx(block, transaction)¶ Decode a transfer transaction.
Currently the colorvote module only supports one input for transfer transactions although the protocol is more general (need to fix)
Parameters: transaction (dict) – A transaction as returned by the decoderawtransactionwallet commandReturns: A tuple (address, unit, metadata) Return type: tuple
-
scan()¶ Iterates through the blockchain and finds colorvote transactions.
-
set_txfee(amount)¶ Set the transaction fee to use when building transactions.
Parameters: amount (float) – The transaction fee
-
RPC¶
-
class
colorvote.RPC(username, password, port=14242, host='localhost')¶ This is an interface class for talking to a wallet with JSON RPC.
Parameters: - username (str) – Username for RPC interface
- password (str) – Password for RPC interface
- port (int, optional) – Port for RPC interface, default is 14242
- host (str, optional) – Host for RPC interface, default is localhost
-
execute(cmd, params=[])¶ Send a command to the wallet.
Parameters: - cmd (str) – The command to execute
- params (list, optional) – A list of arguments for the command, default is empty
Returns: The result returned by the wallet.
Return type: str
-
get_transaction(txid)¶ Get a decoded transaction by ID.
Parameters: txid (str) – The transaction ID Returns: A decoded transaction as returned by decoderawtransaction
-
send_transaction(params)¶ Sign and send a transaction to the blockchain.
Parameters: params (list) – A list [inputs, outputs] of the parameters as accepted by the createrawtransaction wallet command. Returns: The txid of the transaction that was sent. Return type: str
Database¶
-
class
colorvote.Database(db='database.db')¶ This is a database wrapper to use with the library.
Parameters: db (str, optional) – Path to sqlite3 database file. -
get_commitment(address)¶ Get a commitment for given address from the database.
-
get_election(address)¶ Get an election from the database.
-
get_elections()¶ Get all elections from the database.
-
get_results(election)¶ Get unspent transaction of address from the database.
-
get_setting(key)¶ Get a setting value from the database.
Parameters: key (str) – The key of the setting Returns: Value of the setting Return type: str
-
get_transaction(txid, n)¶ Get a transaction from the database.
-
get_transactions()¶ Get all transactions from database
-
get_unspent(address)¶ Get unspent transaction of address from the database.
-
insert_commitment(address, commitment)¶ Write a commitment value for given address to the database.
-
insert_election(election)¶ Write an election to the database.
-
insert_transaction(transaction)¶ Insert a transaction into the database.
-
set_setting(key, value)¶ Write a setting value to the database.
Parameters: - key (str) – The key of the setting
- value (str) – The value to write
-
Model¶
-
colorvote.models¶ alias of
colorvote.models