Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
eth_gen_address.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.cli.constants import ETH_ADDRESS_DEFAULT, ETH_PRIVATE_KEY_FILE_DEFAULT
8 from zeth.cli.utils import write_eth_private_key, write_eth_address
9 from zeth.core.utils import eth_address_from_private_key
10 from click import command, option, ClickException
11 from web3 import Web3 # type: ignore
12 import os
13 import sys
14 from typing import Optional
15 
16 
17 @command()
18 @option("--eth-addr-file", help="Address output filename")
19 @option("--eth-private-key-file", help="Private key output filename")
20 @option("--use-private-key", help="Use existing private key")
22  eth_addr_file: Optional[str],
23  eth_private_key_file: Optional[str],
24  use_private_key: Optional[str]) -> None:
25  """
26  Locally generate a new Ethereum private key and address file, and write
27  them to the current directory.
28  """
29  sys.stderr.write(
30  "*** WARNING: this address should not be used in production ***\n")
31 
32  eth_addr_file = eth_addr_file or ETH_ADDRESS_DEFAULT
33  eth_private_key_file = eth_private_key_file or ETH_PRIVATE_KEY_FILE_DEFAULT
34 
35  if use_private_key:
36  eth_private_key = bytes.fromhex(use_private_key)
37  else:
38  eth_private_key = gen_eth_private_key()
39 
40  if len(eth_private_key) != 32:
41  raise ClickException("invalid private key length")
42 
43  write_eth_private_key(eth_private_key, eth_private_key_file)
44  eth_address = eth_address_from_private_key(eth_private_key)
45  write_eth_address(eth_address, eth_addr_file)
46  print(
47  f"{eth_address}: written to {eth_addr_file}, "
48  f"private key to {eth_private_key_file}")
49 
50 
51 def gen_eth_private_key() -> bytes:
52  """
53  Simple private key generation function. Not for production use.
54  """
55  return Web3.sha3(os.urandom(4096))
zeth.cli.utils.write_eth_address
None write_eth_address(str eth_addr, str eth_addr_file)
Definition: utils.py:458
zeth.helper.eth_gen_address.gen_eth_private_key
bytes gen_eth_private_key()
Definition: eth_gen_address.py:51
zeth.cli.utils
Definition: utils.py:1
zeth.cli.constants
Definition: constants.py:1
zeth.cli.utils.write_eth_private_key
None write_eth_private_key(bytes private_key, str private_key_file)
Definition: utils.py:473
zeth.core.utils
Definition: utils.py:1
zeth.helper.eth_gen_address.eth_gen_address
None eth_gen_address(Optional[str] eth_addr_file, Optional[str] eth_private_key_file, Optional[str] use_private_key)
Definition: eth_gen_address.py:21
zeth.core.utils.eth_address_from_private_key
str eth_address_from_private_key(bytes eth_private_key)
Definition: utils.py:129