Browse Source

Add chainspace-petitions repo

Andrei Biasprozvanny 2 years ago
parent
commit
cf074e7732

+ 1 - 0
chainspace-petitions/.gitignore

@@ -0,0 +1 @@
1
+.idea/

+ 0 - 0
chainspace-petitions/__init__.py


+ 14 - 0
chainspace-petitions/increment.py

@@ -0,0 +1,14 @@
1
+from chainspaceapi import ChainspaceClient
2
+from chainspacecontract import ChainspaceContract
3
+from chainspacecontract.examples import increment
4
+
5
+client = ChainspaceClient()
6
+
7
+init_transaction = increment.init()
8
+client.process_transaction(init_transaction)
9
+
10
+chainspace_object = init_transaction['transaction']['outputs']
11
+
12
+increment_transaction = increment.increment(chainspace_object)
13
+client.process_transaction(increment_transaction)
14
+

+ 58 - 0
chainspace-petitions/petition.py

@@ -0,0 +1,58 @@
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
+
6
+
7
+client = ChainspaceClient()
8
+
9
+params = setup()
10
+(tally_priv, tally_pub)  = key_gen(params)
11
+(_, voter1_pub) = key_gen(params)
12
+(_, voter2_pub) = key_gen(params)
13
+(_, voter3_pub) = key_gen(params)
14
+
15
+options      = ['alice', 'bob']
16
+participants = [pack(voter1_pub), pack(voter2_pub), pack(voter3_pub)]
17
+
18
+
19
+def transaction_to_solution(data):
20
+    store = data['store']
21
+    transaction = data['transaction']
22
+
23
+    for dependency in transaction['dependencies']:
24
+        del dependency['dependencies']
25
+
26
+    for single_transaction in (transaction,) + tuple(transaction['dependencies']):
27
+        single_transaction['inputs'] = []
28
+        single_transaction['referenceInputs'] = []
29
+        for object_id in single_transaction['inputIDs']:
30
+            single_transaction['inputs'].append(store[object_id])
31
+        for object_id in single_transaction['referen ceInputIDs']:
32
+            single_transaction['referenceInputs'].append(store[object_id])
33
+        del single_transaction['inputIDs']
34
+        del single_transaction['referenceInputIDs']
35
+        single_transaction['inputs'] = tuple(single_transaction['inputs'])
36
+        single_transaction['referenceInputs'] = tuple(single_transaction['referenceInputs'])
37
+
38
+    return transaction
39
+
40
+def do_things():
41
+    init_transaction = vote.init()
42
+    vote_object = init_transaction['transaction']['outputs']
43
+    client.process_transaction(init_transaction)
44
+
45
+    vote_transaction = vote.create_vote(
46
+        (vote_object,),
47
+        None,
48
+        None,
49
+        dumps(options),
50
+        dumps(participants),
51
+        pack(tally_priv),
52
+        pack(tally_pub))
53
+    client.process_transaction(transaction_to_solution(vote_transaction))
54
+
55
+
56
+if __name__ == '__main__':
57
+    do_things()
58
+

+ 23 - 0
chainspace-petitions/test.py

@@ -0,0 +1,23 @@
1
+# Define a custom class, encoder and decoder
2
+class CustomClass:
3
+    def __eq__(self, other):
4
+        return isinstance(other, CustomClass)
5
+
6
+def enc_CustomClass(obj):
7
+    if isinstance(obj, CustomClass):
8
+        return msgpack.ExtType(10, b'')
9
+    raise TypeError("Unknown type: %r" % (obj,))
10
+def dec_CustomClass(code, data):
11
+    if code == 10:
12
+        return CustomClass()
13
+    return msgpack.ExtType(code, data)
14
+
15
+# Define a structure
16
+G = EcGroup()
17
+custom_obj = CustomClass()
18
+test_data = [G, G.generator(), G.order(), custom_obj]
19
+
20
+# Encode and decode custom structure
21
+packed = encode(test_data, enc_CustomClass)
22
+x = decode(packed, dec_CustomClass)
23
+assert x == test_data