Code

The Code for Decentralized Decisions provides several software structures for voting and autonomous governance.

Python

The main programming language used for Decentralized Decisions development is Python. Python is a general purpose interpreted programming language. However, other languages such as JavaScript may also be used for variations of the software.

Hash Function

# Hash function to secure data using quantum secure hash. 
def hash(item):
    # Assumes the default UTF-8.
    # This encodes the string with the SHA-512 scheme.
    hash_object = hashlib.sha512(item.encode()) 
    # This returns the hexadecimal encode as a string.
    item = hash_object.hexdigest() 
    return item

The hash function converts a string to a secure code using the SHA-512 cryptographic scheme. SHA-512 is a post-quantum cryptographic scheme, thus ensuring that private information is made secure from malicious attackers.

Vote Functions

# The voting function allows the decision to happen.
def choice_vote(sender, key, receiver,amount,comment):
    parameters = algod_client.suggested_params() # Sets suggested parameters
    transaction = AssetTransferTxn(sender, parameters, receiver, amount, choice_id,note=comment)
    # Defines an inital transaction for Choice Coin
    signature = transaction.sign(key)
    # Signs the transaction with the senders private key
    algod_client.send_transaction(signature)
    # Sends the transaction with the signature
    final = transaction.get_txid()
    return True, final

The choice_vote function defines a stateless smart contract on the Algorand Network. It sends Choice Coin to the appropriate destination address based on user input.

def vote():
    voter = input(str("Vote 0 for zero and vote 1 for one:"))
    params = algod_client.suggested_params()
    if voter is str('1'):
        # send one choice to address
        amount = 100
        transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
        signature = transaction.sign(voter_phrase)
        algod_client.send_transaction(signature)
        final = transaction.get_txid()
        print ("Thanks for voting for one.")
        print(final)
    else:
        # do not send one choice to address
        print ("Thanks for voting for zero.")

Here, the vote function uses a simple binary to send one Choice to an address with a positive vote, or takes no action with a negative vote. The amount is set to 100 to account for the two decimals places associate with the Choice asset. Below is another vote function, which uses two addresses instead of one.

def vote():
    voter = input(str("Vote 0 for zero and vote 1 for one:"))
    params = algod_client.suggested_params()
    if voter is str('1'):
        # send one choice to address
        amount = 100
        vote_address = ""
        transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
        signature = transaction.sign(voter_phrase)
        algod_client.send_transaction(signature)
        final = transaction.get_txid()
        print ("Thanks for voting for one.")
        print(final)
    else:
        amount = 100
        vote_address = ""
        transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
        signature = transaction.sign(voter_phrase)
        algod_client.send_transaction(signature)
        final = transaction.get_txid()
        print ("Thanks for voting for zero.")
        print(final)

Here, each option has an associated address. This allows for a simple count of each address to determine the total number of votes.

Results Functions

# The count_votes funciton counts the votes to determine the winner.
def count_votes():
    yes_count = count(decision_one)
    no_count = count(decision_two)
    show_results(yes_count,no_count)
    if yes_count > no_count:
        return "The Voting Process has ended. Choice One had the most votes!"
    else no_count > yes_count:
        return "The Voting Process has ended. Choice Two had the most votes!"

This function counts the total number of votes to return a statement regarding which candidate has won. This model may apply to both corporate or electoral voting.

def check_results():
    asset_id = 42771692 
    address = ""
    account_info = client.account_info(address)
    assets = account_info.get("assets")
    for asset in assets:
        if asset['asset-id'] == asset_id:
            amount = asset.get("amount")
            print("Account {} has {}.".format(address, balance_formatter(amount, asset_id, client)))
            return
        print("Account {} must opt-in to Asset ID {}.".format(address, asset_id))
check_results()

Here, the check_results function checks the amount the asset in the account, to determine the total number of votes for the particular address.

Links

Decentralized Decisions GitHub: https://github.com/ChoiceCoin/Voting

Decentralized Decisions White Paper: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3913316

Python GitHub: https://github.com/python

Last updated