diff --git a/CLI.md b/CLI.md index fc9f0c77..cc3b856e 100644 --- a/CLI.md +++ b/CLI.md @@ -1939,7 +1939,7 @@ options: --outfile OUTFILE path to the output file --in-format {raw-mnemonic,keystore-mnemonic,keystore-secret-key,pem} the format of the input file - --out-format {raw-mnemonic,keystore-mnemonic,keystore-secret-key,pem,address-bech32,address-hex} + --out-format {raw-mnemonic,keystore-mnemonic,keystore-secret-key,pem,address-bech32,address-hex,secret-key} the format of the output file --address-index ADDRESS_INDEX the address index, if input format is raw-mnemonic, keystore-mnemonic or pem (with multiple entries) and the output format is keystore- diff --git a/multiversx_sdk_cli/cli_wallet.py b/multiversx_sdk_cli/cli_wallet.py index 1f8b2082..c3e577ff 100644 --- a/multiversx_sdk_cli/cli_wallet.py +++ b/multiversx_sdk_cli/cli_wallet.py @@ -25,6 +25,7 @@ WALLET_FORMAT_PEM = "pem" WALLET_FORMAT_ADDRESS_BECH32 = "address-bech32" WALLET_FORMAT_ADDRESS_HEX = "address-hex" +WALLET_FORMAT_SECRET_KEY = "secret-key" WALLET_FORMATS = [ WALLET_FORMAT_RAW_MNEMONIC, @@ -33,7 +34,12 @@ WALLET_FORMAT_PEM, ] -WALLET_FORMATS_AND_ADDRESSES = [*WALLET_FORMATS, WALLET_FORMAT_ADDRESS_BECH32, WALLET_FORMAT_ADDRESS_HEX] +WALLET_FORMATS_AND_ADDRESSES = [ + *WALLET_FORMATS, + WALLET_FORMAT_ADDRESS_BECH32, + WALLET_FORMAT_ADDRESS_HEX, + WALLET_FORMAT_SECRET_KEY, +] MAX_ITERATIONS_FOR_GENERATING_WALLET = 100 CURRENT_SHARDS = [i for i in range(NUMBER_OF_SHARDS)] @@ -282,6 +288,13 @@ def _create_wallet_content( pubkey = secret_key.generate_public_key() return pubkey.hex() + if out_format == WALLET_FORMAT_SECRET_KEY: + if mnemonic: + secret_key = mnemonic.derive_key(address_index) + assert secret_key is not None + + return secret_key.hex() + raise KnownError(f"Cannot create wallet, unknown output format: <{out_format}>. Make sure to use one of following: {WALLET_FORMATS}.") diff --git a/multiversx_sdk_cli/tests/test_cli_wallet.py b/multiversx_sdk_cli/tests/test_cli_wallet.py index 6121dbcc..9a6060f6 100644 --- a/multiversx_sdk_cli/tests/test_cli_wallet.py +++ b/multiversx_sdk_cli/tests/test_cli_wallet.py @@ -256,6 +256,17 @@ def test_wallet_convert_pem_to_pubkey(capsys: Any): assert out == "0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1" +def test_wallet_convert_pem_to_secret_key(capsys: Any): + infile = testdata_path / "alice.pem" + + main([ + "wallet", "convert", "--infile", str(infile), "--in-format", "pem", "--out-format", "secret-key" + ]) + + out = _read_stdout(capsys).strip("Output:\n\n") + assert out == "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9" + + def test_wallet_sign_message(capsys: Any): message = "test" pem = testdata_path / "alice.pem" diff --git a/pyproject.toml b/pyproject.toml index a2fc4738..33eb9e97 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "multiversx-sdk-cli" -version = "9.10.2" +version = "9.11.0" authors = [ { name="MultiversX" }, ]