Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
zeth_address.py
Go to the documentation of this file.
1 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 #
3 # SPDX-License-Identifier: LGPL-3.0+
4 
5 from __future__ import annotations
6 from zeth.core.ownership import OwnershipPublicKey, OwnershipSecretKey, \
7  OwnershipKeyPair, ownership_key_as_hex, gen_ownership_keypair, \
8  ownership_public_key_from_hex, ownership_secret_key_from_hex
9 from zeth.core.encryption import \
10  EncryptionKeyPair, EncryptionPublicKey, EncryptionSecretKey, \
11  generate_encryption_keypair, encryption_public_key_as_hex, \
12  encryption_public_key_from_hex, encryption_secret_key_as_hex, \
13  encryption_secret_key_from_hex
14 import json
15 from typing import Dict, Any
16 
17 
19  """
20  Public half of a zethAddress. addr_pk = (a_pk and k_pk)
21  """
22  def __init__(self, a_pk: OwnershipPublicKey, k_pk: EncryptionPublicKey):
23  self.a_pk: OwnershipPublicKey = a_pk
24  self.k_pk: EncryptionPublicKey = k_pk
25 
26  def __str__(self) -> str:
27  """
28  Write the address as "<ownership-key-hex>:<encryption_key_hex>".
29  (Technically the ":" is not required, since the first key is written
30  with fixed length, but a separator provides some limited sanity
31  checking).
32  """
33  a_pk_hex = ownership_key_as_hex(self.a_pk)
34  k_pk_hex = encryption_public_key_as_hex(self.k_pk)
35  return f"{a_pk_hex}:{k_pk_hex}"
36 
37  @staticmethod
38  def parse(key_hex: str) -> ZethAddressPub:
39  owner_enc = key_hex.split(":")
40  if len(owner_enc) != 2:
41  raise Exception("invalid JoinSplitPublicKey format")
42  a_pk = ownership_public_key_from_hex(owner_enc[0])
43  k_pk = encryption_public_key_from_hex(owner_enc[1])
44  return ZethAddressPub(a_pk, k_pk)
45 
46 
48  """
49  Secret half of a zethAddress. addr_sk = (a_sk and k_sk)
50  """
51  def __init__(self, a_sk: OwnershipSecretKey, k_sk: EncryptionSecretKey):
52  self.a_sk: OwnershipSecretKey = a_sk
53  self.k_sk: EncryptionSecretKey = k_sk
54 
55  def to_json(self) -> str:
56  return json.dumps(self._to_json_dict())
57 
58  @staticmethod
59  def from_json(key_json: str) -> ZethAddressPriv:
60  return ZethAddressPriv._from_json_dict(json.loads(key_json))
61 
62  def _to_json_dict(self) -> Dict[str, Any]:
63  return {
64  "a_sk": ownership_key_as_hex(self.a_sk),
65  "k_sk": encryption_secret_key_as_hex(self.k_sk),
66  }
67 
68  @staticmethod
69  def _from_json_dict(key_dict: Dict[str, Any]) -> ZethAddressPriv:
70  return ZethAddressPriv(
71  ownership_secret_key_from_hex(key_dict["a_sk"]),
72  encryption_secret_key_from_hex(key_dict["k_sk"]))
73 
74 
76  """
77  Secret and public keys for both ownership and encryption (referrred to as
78  "zethAddress" in the paper).
79  """
80  def __init__(
81  self,
82  a_pk: OwnershipPublicKey,
83  k_pk: EncryptionPublicKey,
84  a_sk: OwnershipSecretKey,
85  k_sk: EncryptionSecretKey):
86  self.addr_pk = ZethAddressPub(a_pk, k_pk)
87  self.addr_sk = ZethAddressPriv(a_sk, k_sk)
88 
89  @staticmethod
91  ownership: OwnershipKeyPair,
92  encryption: EncryptionKeyPair) -> ZethAddress:
93  return ZethAddress(
94  ownership.a_pk,
95  encryption.k_pk,
96  ownership.a_sk,
97  encryption.k_sk)
98 
99  @staticmethod
101  js_secret: ZethAddressPriv,
102  js_public: ZethAddressPub) -> ZethAddress:
103  return ZethAddress(
104  js_public.a_pk, js_public.k_pk, js_secret.a_sk, js_secret.k_sk)
105 
106  def ownership_keypair(self) -> OwnershipKeyPair:
107  return OwnershipKeyPair(self.addr_sk.a_sk, self.addr_pk.a_pk)
108 
109 
110 def generate_zeth_address() -> ZethAddress:
111  ownership_keypair = gen_ownership_keypair()
112  encryption_keypair = generate_encryption_keypair()
113  return ZethAddress.from_key_pairs(ownership_keypair, encryption_keypair)
zeth.core.zeth_address.ZethAddressPub.parse
ZethAddressPub parse(str key_hex)
Definition: zeth_address.py:38
zeth.core.zeth_address.ZethAddress
Definition: zeth_address.py:75
zeth.core.ownership.ownership_key_as_hex
str ownership_key_as_hex(bytes a_sk)
Definition: ownership.py:37
zeth.core.encryption.encryption_secret_key_from_hex
EncryptionSecretKey encryption_secret_key_from_hex(str pk_str)
Definition: encryption.py:124
zeth.core.ownership.ownership_secret_key_from_hex
OwnershipSecretKey ownership_secret_key_from_hex(str key_hex)
Definition: ownership.py:52
zeth.core.zeth_address.ZethAddress.addr_pk
addr_pk
Definition: zeth_address.py:81
zeth.core.zeth_address.ZethAddressPriv.from_json
ZethAddressPriv from_json(str key_json)
Definition: zeth_address.py:59
zeth.core.zeth_address.ZethAddressPriv.to_json
str to_json(self)
Definition: zeth_address.py:55
zeth.core.encryption
Definition: encryption.py:1
zeth.core.zeth_address.ZethAddress.__init__
def __init__(self, OwnershipPublicKey a_pk, EncryptionPublicKey k_pk, OwnershipSecretKey a_sk, EncryptionSecretKey k_sk)
Definition: zeth_address.py:80
zeth.core.ownership.gen_ownership_keypair
OwnershipKeyPair gen_ownership_keypair()
Definition: ownership.py:59
zeth.core.zeth_address.ZethAddressPub.__init__
def __init__(self, OwnershipPublicKey a_pk, EncryptionPublicKey k_pk)
Definition: zeth_address.py:22
zeth.core.zeth_address.ZethAddressPub.__str__
str __str__(self)
Definition: zeth_address.py:26
zeth.core.ownership.ownership_public_key_from_hex
OwnershipPublicKey ownership_public_key_from_hex(str key_hex)
Definition: ownership.py:45
zeth.core.zeth_address.ZethAddress.ownership_keypair
OwnershipKeyPair ownership_keypair(self)
Definition: zeth_address.py:106
zeth.core.zeth_address.ZethAddress.from_secret_public
ZethAddress from_secret_public(ZethAddressPriv js_secret, ZethAddressPub js_public)
Definition: zeth_address.py:100
zeth.core.zeth_address.ZethAddressPub
Definition: zeth_address.py:18
zeth.core.encryption.encryption_public_key_as_hex
str encryption_public_key_as_hex(EncryptionPublicKey pk)
Definition: encryption.py:146
zeth.core.zeth_address.ZethAddressPriv.__init__
def __init__(self, OwnershipSecretKey a_sk, EncryptionSecretKey k_sk)
Definition: zeth_address.py:51
zeth.core.zeth_address.ZethAddressPriv
Definition: zeth_address.py:47
zeth.core.ownership
Definition: ownership.py:1
zeth.core.zeth_address.ZethAddress.from_key_pairs
ZethAddress from_key_pairs(OwnershipKeyPair ownership, EncryptionKeyPair encryption)
Definition: zeth_address.py:90
zeth.core.zeth_address.generate_zeth_address
ZethAddress generate_zeth_address()
Definition: zeth_address.py:110
zeth.core.zeth_address.ZethAddressPriv._to_json_dict
Dict[str, Any] _to_json_dict(self)
Definition: zeth_address.py:62
zeth.core.encryption.generate_encryption_keypair
EncryptionKeyPair generate_encryption_keypair()
Definition: encryption.py:163
zeth.core.zeth_address.ZethAddress.addr_sk
addr_sk
Definition: zeth_address.py:82
zeth.core.encryption.encryption_secret_key_as_hex
str encryption_secret_key_as_hex(EncryptionSecretKey sk)
Definition: encryption.py:120
zeth.core.encryption.encryption_public_key_from_hex
EncryptionPublicKey encryption_public_key_from_hex(str pk_str)
Definition: encryption.py:150
zeth.core.ownership.OwnershipKeyPair
Definition: ownership.py:25