Skip to content

Commit

Permalink
convert from wallet to bech32 address or pubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Sep 6, 2023
1 parent b155507 commit 3147335
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
27 changes: 26 additions & 1 deletion multiversx_sdk_cli/cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
WALLET_FORMAT_KEYSTORE_MNEMONIC = "keystore-mnemonic"
WALLET_FORMAT_KEYSTORE_SECRET_KEY = "keystore-secret-key"
WALLET_FORMAT_PEM = "pem"
WALLET_FORMAT_BECH32_ADDRESS = "bech32_address"
WALLET_FORMAT_PUBLIC_KEY = "public_key"

WALLET_FORMATS = [
WALLET_FORMAT_RAW_MNEMONIC,
WALLET_FORMAT_KEYSTORE_MNEMONIC,
WALLET_FORMAT_KEYSTORE_SECRET_KEY,
WALLET_FORMAT_PEM,
]

WALLET_FORMATS_AND_ADDRESSES: List[str] = []
WALLET_FORMATS_AND_ADDRESSES.extend(WALLET_FORMATS)
WALLET_FORMATS_AND_ADDRESSES.extend([WALLET_FORMAT_BECH32_ADDRESS, WALLET_FORMAT_PUBLIC_KEY])


def setup_parser(args: List[str], subparsers: Any) -> Any:
parser = cli_shared.add_group_subparser(
Expand Down Expand Up @@ -56,7 +63,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
sub.add_argument("--infile", help="path to the input file")
sub.add_argument("--outfile", help="path to the output file")
sub.add_argument("--in-format", required=True, choices=WALLET_FORMATS, help="the format of the input file")
sub.add_argument("--out-format", required=True, choices=WALLET_FORMATS, help="the format of the output file")
sub.add_argument("--out-format", required=True, choices=WALLET_FORMATS_AND_ADDRESSES, help="the format of the output file")
sub.add_argument("--address-index", help=f"the address index, if input format is {WALLET_FORMAT_RAW_MNEMONIC}, {WALLET_FORMAT_KEYSTORE_MNEMONIC} or {WALLET_FORMAT_PEM} (with multiple entries) and the output format is {WALLET_FORMAT_KEYSTORE_SECRET_KEY} or {WALLET_FORMAT_PEM}", type=int, default=0)
sub.add_argument("--address-hrp", help=f"the human-readable part of the address, when the output format is {WALLET_FORMAT_KEYSTORE_SECRET_KEY} or {WALLET_FORMAT_PEM} (default: %(default)s)", type=str, default=DEFAULT_HRP)
sub.set_defaults(func=convert_wallet)
Expand Down Expand Up @@ -209,6 +216,24 @@ def _create_wallet_content(
pem = UserPEM(address.bech32(), secret_key)
return pem.to_text()

if out_format == WALLET_FORMAT_BECH32_ADDRESS:
if mnemonic:
secret_key = mnemonic.derive_key(address_index)
assert secret_key is not None

pubkey = secret_key.generate_public_key()
address = pubkey.to_address(address_hrp)
return address.bech32()

if out_format == WALLET_FORMAT_PUBLIC_KEY:
if mnemonic:
secret_key = mnemonic.derive_key(address_index)
assert secret_key is not None

pubkey = secret_key.generate_public_key()
address = pubkey.to_address(address_hrp)
return address.hex() # type: ignore

raise KnownError(f"Cannot create wallet, unknown output format: <{out_format}>. Make sure to use one of following: {WALLET_FORMATS}.")


Expand Down
22 changes: 22 additions & 0 deletions multiversx_sdk_cli/tests/test_cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ def test_wallet_bech32_decode(capsys: Any):
assert out == "0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1"


def test_wallet_convert_pem_to_bech32_address(capsys: Any):
infile = testdata_path / "alice.pem"

main([
"wallet", "convert", "--infile", str(infile), "--in-format", "pem", "--out-format", "bech32_address"
])

out = _read_stdout(capsys).strip("Output:\n\n")
assert out == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"


def test_wallet_convert_pem_to_pubkey(capsys: Any):
infile = testdata_path / "alice.pem"

main([
"wallet", "convert", "--infile", str(infile), "--in-format", "pem", "--out-format", "public_key"
])

out = _read_stdout(capsys).strip("Output:\n\n")
assert out == "0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1"


def _read_stdout_mnemonic(capsys: Any) -> str:
return _read_stdout(capsys).replace("Mnemonic:", "").strip()

Expand Down

0 comments on commit 3147335

Please sign in to comment.