-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
72 lines (61 loc) · 2.95 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from contracts import deploy
from web3 import Web3, HTTPProvider, IPCProvider
from wallet import Wallet, ZKToken
from composer import NaiveStrategy
from commitments import Commitment, Note
import random
# Bit size
n = 32
provider = HTTPProvider('http://127.0.0.1:8545', request_kwargs={'timeout': 120})
web3 = Web3(provider)
web3.eth.defaultAccount = web3.eth.accounts[0]
def test_confidential_transaction():
from pysolcrypto.altbn128 import randsn, FQ, multiply as ecmul, add as ecadd
print('bit size: ' + str(n))
contract = ZKToken.deploy(web3, n)
# setup phase
wallet_alice = Wallet('Alice', contract, notes=[], composer=None)
gas_receipt = wallet_alice.quick_setup(NaiveStrategy)
print('\nAlice setup\n' + gas_receipt)
wallet_bob = Wallet('Bob', contract, notes=[], composer=None)
print('\nBob setup\n' + gas_receipt)
gas_receipt = wallet_bob.quick_setup(NaiveStrategy)
# deposit
receipt = wallet_alice.deposit(3.0)
print('\nAlice is to deposit 3 eth' + '\ngas cost: ' + str(receipt.gasUsed))
receipt = wallet_alice.deposit(8.0)
print('\nAlice is to deposit 8 eth' + '\ngas cost: ' + str(receipt.gasUsed))
print('\nbalances ' + '\nAlice: ' + str(wallet_alice.balance()) + ' eth'
+ '\nBob: ' + str(wallet_bob.balance()) + ' eth'
+ '\nContract: ' + str(Web3.fromWei(web3.eth.getBalance(contract.address), 'ether')) + ' eth')
print('\nAlice is to send 10 eth to Bob')
# transaction
tx = wallet_alice.make_ct_phase_1(10.0)
tx = wallet_bob.make_ct_phase_2(tx)
receipt = tx.broadcast()
print('2 input, 2 output transaction' + '\ngas cost: ' + str(receipt.gasUsed))
for input in tx.inputs_of('Alice'):
assert contract.get_note(input) == 0
for output in tx.outputs_of('Alice'):
note = Commitment.aggregate([wallet_alice.get_commitment(index) for index in output])
assert note.y() == contract.get_note(note.x())
for output in tx.outputs_of('Bob'):
note = Commitment.aggregate([wallet_bob.get_commitment(index) for index in output])
assert note.y() == contract.get_note(note.x())
wallet_alice.update(tx)
wallet_bob.update(tx)
print('\nbalances ' + '\nAlice: ' + str(wallet_alice.balance()) + ' eth'
+ '\nBob: ' + str(wallet_bob.balance()) + ' eth'
+ '\nContract: ' + str(Web3.fromWei(web3.eth.getBalance(contract.address), 'ether')) + ' eth')
# withdraw
receiver = Web3.toChecksumAddress(Web3.toHex(Web3.sha3(random.randint(1, 44000))[-20:]))
receipt = wallet_bob.withdraw(4.0, receiver)
print('\nBob withdraws 4 eth' + '\ngas cost: ' + str(receipt.gasUsed))
assert Web3.toWei(4.0, 'ether') == web3.eth.getBalance(receiver)
print('\nbalances ' + '\nAlice: ' + str(wallet_alice.balance()) + ' eth'
+ '\nBob: ' + str(wallet_bob.balance()) + ' eth'
+ '\nContract: ' + str(Web3.fromWei(web3.eth.getBalance(contract.address), 'ether')) + ' eth'
+ '\nReceiver of the withdrawal: ' + str(Web3.fromWei(web3.eth.getBalance(receiver), 'ether')) + ' eth')
print()
print('fin')
test_confidential_transaction()