Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mock.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
4 #
5 # SPDX-License-Identifier: LGPL-3.0+
6 
7 from zeth.core.zeth_address import ZethAddress
8 from zeth.core.encryption import EncryptionKeyPair, \
9  decode_encryption_secret_key, decode_encryption_public_key
10 from zeth.core.ownership import gen_ownership_keypair
11 from zeth.core.utils import get_contracts_dir, open_web3
12 from zeth.core.contracts import compile_files
13 from os.path import join
14 from typing import Dict, List, Tuple, Optional, Any
15 
16 
17 # Web3 HTTP provider
18 TEST_PROVER_SERVER_ENDPOINT: str = "localhost:50051"
19 TEST_WEB3_PROVIDER_ENDPOINT: str = "http://localhost:8545"
20 TEST_NOTE_DIR: str = "_test_notes"
21 
22 KeyStore = Dict[str, ZethAddress]
23 
24 
25 def open_test_web3() -> Tuple[Any, Any]:
26  web3 = open_web3(TEST_WEB3_PROVIDER_ENDPOINT)
27  return web3, web3.eth # pylint: disable=no-member # type: ignore
28 
29 
30 def init_test_keystore() -> KeyStore:
31  """
32  Keystore for the tests
33  """
34 
35  alice_25519_enc_private_key = \
36  b'\xde\xa2\xc1\x0b\xd1\xf7\x13\xf8J\xa4:\xa4\xb6\xfa\xbd\xd5\xc9' + \
37  b'\x8a\xd9\xb6\xb4\xc4\xc4I\x88\xa4\xd9\xe2\xee\x9e\x9a\xff'
38  alice_25519_enc_public_key = \
39  b'\x1eO"\n\xdaWnU+\xf5\xaa\x8a#\xd2*\xd3\x11\x9fc\xe52 \xd8^\xbc-' + \
40  b'\xb6\xf1\xeej\xf41'
41 
42  bob_25519_enc_private_key = \
43  b'\xd3\xf0\x8f ,\x1d#\xdc\xac,\x93\xbd\xd0\xd9\xed\x8c\x92\x822' + \
44  b'\xef\xd6\x97^\x86\xf7\xe4/\x85\xb6\x10\xe6o'
45  bob_25519_enc_public_key = \
46  b't\xc5{5j\xb5\x8a\xd3n\xb3\xab9\xe8s^13\xba\xa2\x91x\xb01(\xf9' + \
47  b'\xbb\xf9@r_\x91}'
48 
49  charlie_25519_enc_private_key = b'zH\xb66q\x97\x0bO\xcb\xb9q\x9b\xbd-1`I' + \
50  b'\xae\x00-\x11\xb9\xed}\x18\x9f\xf6\x8dr\xaa\xd4R'
51  charlie_25519_enc_public_key = \
52  b'u\xe7\x88\x9c\xbfE(\xf8\x99\xca<\xa8[<\xa2\x88m\xad\rN"\xf0}' + \
53  b'\xec\xfcB\x89\xe6\x96\xcf\x19U'
54 
55  # Alice credentials in the zeth abstraction
56  alice_ownership = gen_ownership_keypair()
57  alice_encryption = EncryptionKeyPair(
58  decode_encryption_secret_key(alice_25519_enc_private_key),
59  decode_encryption_public_key(alice_25519_enc_public_key))
60 
61  # Bob credentials in the zeth abstraction
62  bob_ownership = gen_ownership_keypair()
63  bob_encryption = EncryptionKeyPair(
64  decode_encryption_secret_key(bob_25519_enc_private_key),
65  decode_encryption_public_key(bob_25519_enc_public_key))
66 
67  # Charlie credentials in the zeth abstraction
68  charlie_ownership = gen_ownership_keypair()
69  charlie_encryption = EncryptionKeyPair(
70  decode_encryption_secret_key(charlie_25519_enc_private_key),
71  decode_encryption_public_key(charlie_25519_enc_public_key))
72 
73  return {
74  "Alice": ZethAddress.from_key_pairs(
75  alice_ownership, alice_encryption),
76  "Bob": ZethAddress.from_key_pairs(
77  bob_ownership, bob_encryption),
78  "Charlie": ZethAddress.from_key_pairs(
79  charlie_ownership, charlie_encryption),
80  }
81 
82 
83 def get_dummy_merkle_path(length: int) -> List[str]:
84  mk_path = []
85  # Arbitrary sha256 digest used to build the dummy merkle path
86  dummy_node = \
87  "6461f753bfe21ba2219ced74875b8dbd8c114c3c79d7e41306dd82118de1895b"
88  for _ in range(length):
89  mk_path.append(dummy_node)
90  return mk_path
91 
92 
94  eth: Any,
95  deployer_address: str,
96  contract_name: str,
97  constructor_args: Optional[Dict[str, Any]] = None) -> Tuple[Any, Any]:
98  contracts_dir = get_contracts_dir()
99  sol_path = join(contracts_dir, contract_name + ".sol")
100  compiled_sol = compile_files([sol_path])
101  interface = compiled_sol[sol_path + ":" + contract_name]
102  contract_abi = interface['abi']
103  contract = eth.contract(abi=contract_abi, bytecode=interface['bin'])
104  deploy_tx = contract.constructor(**constructor_args)
105  deploy_tx_hash = deploy_tx.transact({'from': deployer_address})
106  tx_receipt = eth.waitForTransactionReceipt(deploy_tx_hash, 1000)
107  contract_address = tx_receipt['contractAddress']
108  contract_instance = eth.contract(
109  address=contract_address,
110  abi=contract_abi)
111  return interface, contract_instance
zeth.core.encryption.decode_encryption_secret_key
EncryptionSecretKey decode_encryption_secret_key(bytes sk_bytes)
Definition: encryption.py:115
zeth.core.utils.open_web3
Any open_web3(str url, Optional[str] certificate=None, bool insecure=False)
Definition: utils.py:28
zeth.core.encryption
Definition: encryption.py:1
test_commands.mock.deploy_contract
Tuple[Any, Any] deploy_contract(Any eth, str deployer_address, str contract_name, Optional[Dict[str, Any]] constructor_args=None)
Definition: mock.py:93
zeth.core.ownership.gen_ownership_keypair
OwnershipKeyPair gen_ownership_keypair()
Definition: ownership.py:59
zeth.core.contracts.compile_files
Any compile_files(List[str] files, **Any kwargs)
Definition: contracts.py:122
zeth.core.utils
Definition: utils.py:1
zeth.core.contracts
Definition: contracts.py:1
zeth.core.encryption.decode_encryption_public_key
EncryptionPublicKey decode_encryption_public_key(bytes pk_data)
Definition: encryption.py:142
zeth.core.ownership
Definition: ownership.py:1
test_commands.mock.get_dummy_merkle_path
List[str] get_dummy_merkle_path(int length)
Definition: mock.py:83
test_commands.mock.open_test_web3
Tuple[Any, Any] open_test_web3()
Definition: mock.py:25
test_commands.mock.init_test_keystore
KeyStore init_test_keystore()
Definition: mock.py:30
zeth.core.encryption.EncryptionKeyPair
Definition: encryption.py:154
zeth.core.zeth_address
Definition: zeth_address.py:1
zeth.core.utils.get_contracts_dir
str get_contracts_dir()
Definition: utils.py:255