Skip to content

Commit a12071b

Browse files
authored
Merge pull request #474 from multiversx/cherry-pick-wallet-export-secret-key
[Cherry pick]: wallet export to secret key
2 parents 704bcf4 + fa62bbf commit a12071b

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

CLI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ options:
19391939
--outfile OUTFILE path to the output file
19401940
--in-format {raw-mnemonic,keystore-mnemonic,keystore-secret-key,pem}
19411941
the format of the input file
1942-
--out-format {raw-mnemonic,keystore-mnemonic,keystore-secret-key,pem,address-bech32,address-hex}
1942+
--out-format {raw-mnemonic,keystore-mnemonic,keystore-secret-key,pem,address-bech32,address-hex,secret-key}
19431943
the format of the output file
19441944
--address-index ADDRESS_INDEX the address index, if input format is raw-mnemonic, keystore-mnemonic
19451945
or pem (with multiple entries) and the output format is keystore-

multiversx_sdk_cli/cli_wallet.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
WALLET_FORMAT_PEM = "pem"
2626
WALLET_FORMAT_ADDRESS_BECH32 = "address-bech32"
2727
WALLET_FORMAT_ADDRESS_HEX = "address-hex"
28+
WALLET_FORMAT_SECRET_KEY = "secret-key"
2829

2930
WALLET_FORMATS = [
3031
WALLET_FORMAT_RAW_MNEMONIC,
@@ -33,7 +34,12 @@
3334
WALLET_FORMAT_PEM,
3435
]
3536

36-
WALLET_FORMATS_AND_ADDRESSES = [*WALLET_FORMATS, WALLET_FORMAT_ADDRESS_BECH32, WALLET_FORMAT_ADDRESS_HEX]
37+
WALLET_FORMATS_AND_ADDRESSES = [
38+
*WALLET_FORMATS,
39+
WALLET_FORMAT_ADDRESS_BECH32,
40+
WALLET_FORMAT_ADDRESS_HEX,
41+
WALLET_FORMAT_SECRET_KEY,
42+
]
3743

3844
MAX_ITERATIONS_FOR_GENERATING_WALLET = 100
3945
CURRENT_SHARDS = [i for i in range(NUMBER_OF_SHARDS)]
@@ -282,6 +288,13 @@ def _create_wallet_content(
282288
pubkey = secret_key.generate_public_key()
283289
return pubkey.hex()
284290

291+
if out_format == WALLET_FORMAT_SECRET_KEY:
292+
if mnemonic:
293+
secret_key = mnemonic.derive_key(address_index)
294+
assert secret_key is not None
295+
296+
return secret_key.hex()
297+
285298
raise KnownError(f"Cannot create wallet, unknown output format: <{out_format}>. Make sure to use one of following: {WALLET_FORMATS}.")
286299

287300

multiversx_sdk_cli/tests/test_cli_wallet.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,17 @@ def test_wallet_convert_pem_to_pubkey(capsys: Any):
256256
assert out == "0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1"
257257

258258

259+
def test_wallet_convert_pem_to_secret_key(capsys: Any):
260+
infile = testdata_path / "alice.pem"
261+
262+
main([
263+
"wallet", "convert", "--infile", str(infile), "--in-format", "pem", "--out-format", "secret-key"
264+
])
265+
266+
out = _read_stdout(capsys).strip("Output:\n\n")
267+
assert out == "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9"
268+
269+
259270
def test_wallet_sign_message(capsys: Any):
260271
message = "test"
261272
pem = testdata_path / "alice.pem"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "multiversx-sdk-cli"
7-
version = "9.10.2"
7+
version = "9.11.0"
88
authors = [
99
{ name="MultiversX" },
1010
]

0 commit comments

Comments
 (0)