Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
proto_utils.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 """
6 Utilities to handle protobuf types
7 """
8 
9 from zeth.core import constants
10 from zeth.api.zeth_messages_pb2 import ZethNote
11 
12 from typing import Dict
13 
14 
15 # ZethNote binary serialization format:
16 # [apk : APK_LENGTH_BYTES]
17 # [value : PUBLIC_VALUE_LENGTH_BYTES]
18 # [rho : RHO_LENGTH_BYTES]
19 # [trapr : TRAPR_LENGTH_BYTES]
20 _APK_OFFSET_BYTES = 0
21 _VALUE_OFFSET_BYTES = _APK_OFFSET_BYTES + constants.APK_LENGTH_BYTES
22 _RHO_OFFSET_BYTES = _VALUE_OFFSET_BYTES + constants.PUBLIC_VALUE_LENGTH_BYTES
23 _TRAPR_OFFSET_BYTES = _RHO_OFFSET_BYTES + constants.RHO_LENGTH_BYTES
24 assert _TRAPR_OFFSET_BYTES + constants.TRAPR_LENGTH_BYTES \
25  == constants.NOTE_LENGTH_BYTES
26 
27 
28 def zeth_note_to_json_dict(zeth_note_grpc_obj: ZethNote) -> Dict[str, str]:
29  return {
30  "a_pk": zeth_note_grpc_obj.apk,
31  "value": zeth_note_grpc_obj.value,
32  "rho": zeth_note_grpc_obj.rho,
33  "trap_r": zeth_note_grpc_obj.trap_r,
34  }
35 
36 
37 def zeth_note_from_json_dict(parsed_zeth_note: Dict[str, str]) -> ZethNote:
38  note = ZethNote(
39  apk=parsed_zeth_note["a_pk"],
40  value=parsed_zeth_note["value"],
41  rho=parsed_zeth_note["rho"],
42  trap_r=parsed_zeth_note["trap_r"]
43  )
44  return note
45 
46 
47 def zeth_note_to_bytes(zeth_note_grpc_obj: ZethNote) -> bytes:
48  apk_bytes = bytes.fromhex(zeth_note_grpc_obj.apk)
49  value_bytes = bytes.fromhex(zeth_note_grpc_obj.value)
50  rho_bytes = bytes.fromhex(zeth_note_grpc_obj.rho)
51  trap_r_bytes = bytes.fromhex(zeth_note_grpc_obj.trap_r)
52  note_bytes = apk_bytes + value_bytes + rho_bytes + trap_r_bytes
53  assert len(note_bytes) == (constants.NOTE_LENGTH_BYTES)
54  return note_bytes
55 
56 
57 def zeth_note_from_bytes(note_bytes: bytes) -> ZethNote:
58  if len(note_bytes) != (constants.NOTE_LENGTH_BYTES):
59  raise ValueError(
60  f"note_bytes len {len(note_bytes)}, "
61  f"(expected {constants.NOTE_LENGTH_BYTES})")
62  apk = note_bytes[
63  _APK_OFFSET_BYTES:_APK_OFFSET_BYTES + constants.APK_LENGTH_BYTES]
64  value = note_bytes[
65  _VALUE_OFFSET_BYTES:
66  _VALUE_OFFSET_BYTES + constants.PUBLIC_VALUE_LENGTH_BYTES]
67  rho = note_bytes[
68  _RHO_OFFSET_BYTES:_RHO_OFFSET_BYTES + constants.RHO_LENGTH_BYTES]
69  trap_r = note_bytes[_TRAPR_OFFSET_BYTES:]
70  return ZethNote(
71  apk=apk.hex(), value=value.hex(), rho=rho.hex(), trap_r=trap_r.hex())
zeth.core.proto_utils.zeth_note_to_bytes
bytes zeth_note_to_bytes(ZethNote zeth_note_grpc_obj)
Definition: proto_utils.py:47
zeth.core.proto_utils.zeth_note_from_bytes
ZethNote zeth_note_from_bytes(bytes note_bytes)
Definition: proto_utils.py:57
zeth.core
Definition: __init__.py:1
zeth.core.proto_utils.zeth_note_from_json_dict
ZethNote zeth_note_from_json_dict(Dict[str, str] parsed_zeth_note)
Definition: proto_utils.py:37
zeth.core.proto_utils.zeth_note_to_json_dict
Dict[str, str] zeth_note_to_json_dict(ZethNote zeth_note_grpc_obj)
Definition: proto_utils.py:28