/
address_group.py
207 lines (174 loc) · 7.26 KB
/
address_group.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""Group of methods for working with payment addresses."""
import json
import logging
import warnings
from pathlib import Path
from typing import Dict
from typing import Optional
from cardano_clusterlib import clusterlib_helpers
from cardano_clusterlib import helpers
from cardano_clusterlib import structs
from cardano_clusterlib import types # pylint: disable=unused-import
from cardano_clusterlib.types import FileType
LOGGER = logging.getLogger(__name__)
class AddressGroup:
def __init__(self, clusterlib_obj: "types.ClusterLib") -> None:
self._clusterlib_obj = clusterlib_obj
def gen_payment_addr(
self,
addr_name: str,
payment_vkey_file: Optional[FileType] = None,
payment_script_file: Optional[FileType] = None,
stake_vkey_file: Optional[FileType] = None,
stake_script_file: Optional[FileType] = None,
destination_dir: FileType = ".",
) -> str:
"""Generate a payment address, with optional delegation to a stake address.
Args:
addr_name: A name of payment address.
payment_vkey_file: A path to corresponding vkey file (optional).
payment_script_file: A path to corresponding payment script file (optional).
stake_vkey_file: A path to corresponding stake vkey file (optional).
stake_script_file: A path to corresponding payment script file (optional).
destination_dir: A path to directory for storing artifacts (optional).
Returns:
str: A generated payment address.
"""
destination_dir = Path(destination_dir).expanduser()
out_file = destination_dir / f"{addr_name}.addr"
clusterlib_helpers._check_files_exist(out_file, clusterlib_obj=self._clusterlib_obj)
if payment_vkey_file:
cli_args = ["--payment-verification-key-file", str(payment_vkey_file)]
elif payment_script_file:
cli_args = ["--payment-script-file", str(payment_script_file)]
else:
raise AssertionError("Either `payment_vkey_file` or `payment_script_file` is needed.")
if stake_vkey_file:
cli_args.extend(["--stake-verification-key-file", str(stake_vkey_file)])
elif stake_script_file:
cli_args.extend(["--stake-script-file", str(stake_script_file)])
self._clusterlib_obj.cli(
[
"address",
"build",
*self._clusterlib_obj.magic_args,
*cli_args,
"--out-file",
str(out_file),
]
)
helpers._check_outfiles(out_file)
return helpers.read_address_from_file(out_file)
def gen_payment_key_pair(
self, key_name: str, extended: bool = False, destination_dir: FileType = "."
) -> structs.KeyPair:
"""Generate an address key pair.
Args:
key_name: A name of the key pair.
extended: A bool indicating whether to generate extended ed25519 Shelley-era key
(False by default).
destination_dir: A path to directory for storing artifacts (optional).
Returns:
structs.KeyPair: A tuple containing the key pair.
"""
destination_dir = Path(destination_dir).expanduser()
vkey = destination_dir / f"{key_name}.vkey"
skey = destination_dir / f"{key_name}.skey"
clusterlib_helpers._check_files_exist(vkey, skey, clusterlib_obj=self._clusterlib_obj)
extended_args = ["--extended-key"] if extended else []
self._clusterlib_obj.cli(
[
"address",
"key-gen",
"--verification-key-file",
str(vkey),
*extended_args,
"--signing-key-file",
str(skey),
]
)
helpers._check_outfiles(vkey, skey)
return structs.KeyPair(vkey, skey)
def get_payment_vkey_hash(
self,
payment_vkey_file: Optional[FileType] = None,
payment_vkey: Optional[str] = None,
) -> str:
"""Return the hash of an address key.
Args:
payment_vkey_file: A path to payment vkey file (optional).
payment_vkey: A payment vkey, (Bech32, optional).
Returns:
str: A generated hash.
"""
if payment_vkey:
cli_args = ["--payment-verification-key", payment_vkey]
elif payment_vkey_file:
cli_args = ["--payment-verification-key-file", str(payment_vkey_file)]
else:
raise AssertionError("Either `payment_vkey` or `payment_vkey_file` is needed.")
return (
self._clusterlib_obj.cli(["address", "key-hash", *cli_args])
.stdout.rstrip()
.decode("ascii")
)
def get_address_info(
self,
address: str,
) -> structs.AddressInfo:
"""Get information about an address.
Args:
address: A Cardano address.
Returns:
structs.AddressInfo: A tuple containing address info.
"""
addr_dict: Dict[str, str] = json.loads(
self._clusterlib_obj.cli(["address", "info", "--address", str(address)])
.stdout.rstrip()
.decode("utf-8")
)
return structs.AddressInfo(**addr_dict)
def gen_script_addr(
self, addr_name: str, script_file: FileType, destination_dir: FileType = "."
) -> str:
"""Generate a script address.
Args:
addr_name: A name of payment address.
script_file: A path to corresponding script file.
destination_dir: A path to directory for storing artifacts (optional).
Returns:
str: A generated script address.
"""
warnings.warn("`gen_script_addr` deprecated by `gen_payment_addr`", DeprecationWarning)
return self.gen_payment_addr(
addr_name=addr_name, payment_script_file=script_file, destination_dir=destination_dir
)
def gen_payment_addr_and_keys(
self,
name: str,
stake_vkey_file: Optional[FileType] = None,
stake_script_file: Optional[FileType] = None,
destination_dir: FileType = ".",
) -> structs.AddressRecord:
"""Generate payment address and key pair.
Args:
name: A name of the address and key pair.
stake_vkey_file: A path to corresponding stake vkey file (optional).
stake_script_file: A path to corresponding payment script file (optional).
destination_dir: A path to directory for storing artifacts (optional).
Returns:
structs.AddressRecord: A tuple containing the address and key pair / script file.
"""
key_pair = self.gen_payment_key_pair(key_name=name, destination_dir=destination_dir)
addr = self.gen_payment_addr(
addr_name=name,
payment_vkey_file=key_pair.vkey_file,
stake_vkey_file=stake_vkey_file,
stake_script_file=stake_script_file,
destination_dir=destination_dir,
)
return structs.AddressRecord(
address=addr, vkey_file=key_pair.vkey_file, skey_file=key_pair.skey_file
)
def __repr__(self) -> str:
return f"<{self.__class__.__name__}: clusterlib_obj={id(self._clusterlib_obj)}>"