Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cherry pick]: wallet export to secret key #474

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down
15 changes: 14 additions & 1 deletion multiversx_sdk_cli/cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -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}.")


Expand Down
11 changes: 11 additions & 0 deletions multiversx_sdk_cli/tests/test_cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "multiversx-sdk-cli"
version = "9.10.2"
version = "9.11.0"
authors = [
{ name="MultiversX" },
]
Expand Down
Loading