Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
contribute.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 """
8 Functions performing the common steps of contribution: download challenge,
9 sign and upload response.
10 """
11 
12 from coordinator.client import Client
13 from coordinator.crypto import \
14  compute_file_digest, import_signing_key, get_verification_key, sign, \
15  SigningKey, VerificationKey
16 from typing import Callable, Optional
17 import time
18 from requests.exceptions import RequestException
19 
20 
21 def _upload_response(client: Client, response_file: str, sk: SigningKey) -> None:
22  # Compute digest and sign
23  digest = compute_file_digest(response_file)
24  signature = sign(sk, digest)
25  vk = get_verification_key(sk)
26 
27  # Upload
28  client.push_contribution(response_file, digest, vk, signature)
29 
30 
31 def upload_response(client: Client, response_file: str, key_file: str) -> None:
32  """
33  Given some response file and a key, sign the response and upload the
34  coordinator connected to by client.
35  """
36  with open(key_file, "rb") as key_f:
37  sk = import_signing_key(key_f.read())
38  _upload_response(client, response_file, sk)
39 
40 
42  client: Client,
43  interval: int,
44  verification_key: VerificationKey) -> None:
45  """
46  Wait until our turn, returning when we can contribute. If anything goes
47  wrong, an exception is thrown.
48  """
49  contributors = client.get_contributors()
50  our_idx = contributors.get_contributor_index(verification_key)
51  while True:
52  state = client.get_state()
53  current_index = state.next_contributor_index
54  if our_idx is None:
55  raise Exception("contributor is not in the server list")
56  if our_idx < current_index:
57  raise Exception("contributor turn has passed")
58  if our_idx == current_index:
59  return
60  # Wait for interval and try again
61  print(f"Waiting ... (current_idx: {current_index}, our_idx: {our_idx})")
62  time.sleep(interval)
63 
64 
66  base_url: str,
67  key_file: str,
68  challenge_file: str,
69  contribute_cb: Callable[[], str],
70  wait_interval: int,
71  server_certificate: Optional[str],
72  insecure: bool) -> None:
73  """
74  Given a callback that creates a response from a challenge, download a
75  challenge, create the response via the callback, and sign and upload it.
76  """
77  # Check key upfront
78  with open(key_file, "rb") as key_f:
79  sk = import_signing_key(key_f.read())
80  print("Got key")
81 
82  client = Client(base_url, server_certificate, insecure)
83 
84  try:
85  if wait_interval:
86  verification_key = get_verification_key(sk)
87  wait_for_turn(client, wait_interval, verification_key)
88 
89  # Get challenge
90  client.get_challenge(challenge_file)
91  print("Got challenge")
92 
93  # Perform the contribution
94  response_file = contribute_cb()
95 
96  except RequestException as err:
97  print(f"EXCEPTION: {err.response.status_code} - {err.response.text}")
98  raise
99 
100  # Sign and upload
101  _upload_response(client, response_file, sk)
coordinator.crypto
Definition: crypto.py:1
coordinator.crypto.import_signing_key
ecdsa.SigningKey import_signing_key(bytes sk_b)
Definition: crypto.py:58
coordinator.crypto.compute_file_digest
bytes compute_file_digest(str file_name)
Definition: crypto.py:82
coordinator.client
Definition: client.py:1
coordinator.contribute.upload_response
None upload_response(Client client, str response_file, str key_file)
Definition: contribute.py:31
zeth.cli.zeth_get_verification_key.get_verification_key
None get_verification_key(Context ctx, Optional[str] vk_out)
Definition: zeth_get_verification_key.py:15
coordinator.contribute.wait_for_turn
None wait_for_turn(Client client, int interval, VerificationKey verification_key)
Definition: contribute.py:41
coordinator.client.Client
Definition: client.py:21
coordinator.contribute.contribute
None contribute(str base_url, str key_file, str challenge_file, Callable[[], str] contribute_cb, int wait_interval, Optional[str] server_certificate, bool insecure)
Definition: contribute.py:65
zeth.core.signing.sign
Signature sign(SigningSecretKey sk, bytes m)
Definition: signing.py:123