Skip to content

Commit a417a5d

Browse files
committed
feat(side-dag): implement gen_keys CLI command
1 parent 83c9190 commit a417a5d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

hathor/cli/generate_poa_keys.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2021 Hathor Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import os
17+
import sys
18+
19+
from cryptography.hazmat.primitives.asymmetric import ec
20+
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKeyWithSerialization
21+
22+
23+
def main():
24+
from hathor.cli.util import create_parser
25+
from hathor.crypto.util import (
26+
get_address_b58_from_public_key,
27+
get_private_key_bytes,
28+
get_public_key_bytes_compressed,
29+
)
30+
parser = create_parser()
31+
parser.add_argument('--config-yaml', type=str, help='Configuration yaml filepath')
32+
args = parser.parse_args(sys.argv[1:])
33+
if not args.config_yaml:
34+
raise Exception('`--config-yaml` is required')
35+
# We have to set the config file because the `get_address_b58_from_public_key()` call below accesses it indirectly
36+
# to use the version bytes.
37+
os.environ['HATHOR_CONFIG_YAML'] = args.config_yaml
38+
39+
private_key = ec.generate_private_key(ec.SECP256K1())
40+
public_key = private_key.public_key()
41+
assert isinstance(private_key, EllipticCurvePrivateKeyWithSerialization)
42+
43+
data = dict(
44+
private_key_hex=get_private_key_bytes(private_key=private_key).hex(),
45+
public_key_hex=get_public_key_bytes_compressed(public_key=public_key).hex(),
46+
address=get_address_b58_from_public_key(public_key=public_key),
47+
)
48+
49+
print(json.dumps(data, indent=4))

hathor/cli/main.py

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self) -> None:
3434
from . import (
3535
db_export,
3636
db_import,
37+
generate_poa_keys,
3738
generate_valid_words,
3839
load_from_logs,
3940
merged_mining,
@@ -71,6 +72,8 @@ def __init__(self) -> None:
7172
from . import top
7273
self.add_cmd('hathor', 'top', top, 'CPU profiler viewer')
7374
self.add_cmd('side-dag', 'run_node_with_side_dag', side_dag, 'Run a side-dag')
75+
self.add_cmd('side-dag', 'gen_poa_keys', generate_poa_keys, 'Generate a private/public key pair and its '
76+
'address to be used in Proof-of-Authority')
7477
self.add_cmd('docs', 'generate_openapi_json', openapi_json, 'Generate OpenAPI json for API docs')
7578
self.add_cmd('multisig', 'gen_multisig_address', multisig_address, 'Generate a new multisig address')
7679
self.add_cmd('multisig', 'spend_multisig_output', multisig_spend, 'Generate tx that spends a multisig output')

0 commit comments

Comments
 (0)