Skip to content

Commit d770414

Browse files
committed
feat(side-dag): implement gen_keys CLI command
1 parent af8e08b commit d770414

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

hathor/cli/generate_keys.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 os
16+
import sys
17+
18+
from cryptography.hazmat.primitives.asymmetric import ec
19+
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKeyWithSerialization
20+
21+
22+
def main():
23+
from hathor.cli.util import create_parser
24+
from hathor.crypto.util import (
25+
get_address_b58_from_public_key,
26+
get_private_key_bytes,
27+
get_public_key_bytes_compressed,
28+
)
29+
parser = create_parser()
30+
parser.add_argument('--config-yaml', type=str, help='Configuration yaml filepath')
31+
args = parser.parse_args(sys.argv[1:])
32+
if not args.config_yaml:
33+
raise Exception('`--config-yaml` is required')
34+
os.environ['HATHOR_CONFIG_YAML'] = args.config_yaml
35+
36+
private_key = ec.generate_private_key(ec.SECP256K1())
37+
public_key = private_key.public_key()
38+
assert isinstance(private_key, EllipticCurvePrivateKeyWithSerialization)
39+
40+
print('Private key (hex):', get_private_key_bytes(private_key=private_key).hex())
41+
print('Public key (hex):', get_public_key_bytes_compressed(public_key=public_key).hex())
42+
print('Address (base58):', get_address_b58_from_public_key(public_key=public_key))

hathor/cli/main.py

+2
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_keys,
3738
generate_valid_words,
3839
merged_mining,
3940
mining,
@@ -78,6 +79,7 @@ def __init__(self) -> None:
7879
self.add_cmd('tests', 'gen_twin_tx', twin_tx, 'Generate a twin transaction from a transaction hash')
7980
self.add_cmd('wallet', 'gen_kp_wallet', wallet, 'Generate a new KeyPair wallet')
8081
self.add_cmd('wallet', 'gen_hd_words', generate_valid_words, 'Generate random words for HD wallet')
82+
self.add_cmd('wallet', 'gen_keys', generate_keys, 'Generate a private/public key pair and its address')
8183
self.add_cmd('oracle', 'oracle-create-key', oracle_create_key, 'Create an oracle private/public key')
8284
self.add_cmd('oracle', 'oracle-get-pubkey', oracle_get_pubkey,
8385
'Read an oracle private key and output public key hash')

0 commit comments

Comments
 (0)