No Description

petition.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from chainspaceapi import ChainspaceClient
  2. from chainspacecontract.examples import vote
  3. from chainspacecontract.examples.utils import setup, key_gen, pack, unpack, add, add_side
  4. from json import dumps, loads
  5. client = ChainspaceClient()
  6. params = setup()
  7. (tally_priv, tally_pub) = key_gen(params)
  8. (_, voter1_pub) = key_gen(params)
  9. (_, voter2_pub) = key_gen(params)
  10. (_, voter3_pub) = key_gen(params)
  11. options = ['alice', 'bob']
  12. participants = [pack(voter1_pub), pack(voter2_pub), pack(voter3_pub)]
  13. def transaction_to_solution(data):
  14. store = data['store']
  15. transaction = data['transaction']
  16. for dependency in transaction['dependencies']:
  17. del dependency['dependencies']
  18. for single_transaction in (transaction,) + tuple(transaction['dependencies']):
  19. single_transaction['inputs'] = []
  20. single_transaction['referenceInputs'] = []
  21. for object_id in single_transaction['inputIDs']:
  22. single_transaction['inputs'].append(store[object_id])
  23. for object_id in single_transaction['referen ceInputIDs']:
  24. single_transaction['referenceInputs'].append(store[object_id])
  25. del single_transaction['inputIDs']
  26. del single_transaction['referenceInputIDs']
  27. single_transaction['inputs'] = tuple(single_transaction['inputs'])
  28. single_transaction['referenceInputs'] = tuple(single_transaction['referenceInputs'])
  29. return transaction
  30. def do_things():
  31. init_transaction = vote.init()
  32. vote_object = init_transaction['transaction']['outputs']
  33. client.process_transaction(init_transaction)
  34. vote_transaction = vote.create_vote(
  35. (vote_object,),
  36. None,
  37. None,
  38. dumps(options),
  39. dumps(participants),
  40. pack(tally_priv),
  41. pack(tally_pub))
  42. client.process_transaction(transaction_to_solution(vote_transaction))
  43. if __name__ == '__main__':
  44. do_things()