Skip to content

Commit

Permalink
add address hrp as a config entry
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Nov 29, 2024
1 parent 2a77e9d commit 69b6526
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 25 deletions.
6 changes: 3 additions & 3 deletions multiversx_sdk_cli/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TransactionComputer, UserSigner)
from multiversx_sdk.network_providers.accounts import AccountOnNetwork

from multiversx_sdk_cli.constants import DEFAULT_HRP
from multiversx_sdk_cli.config import get_address_hrp
from multiversx_sdk_cli.interfaces import IAccount, IAddress, ITransaction
from multiversx_sdk_cli.ledger.config import compare_versions
from multiversx_sdk_cli.ledger.ledger_app_handler import \
Expand Down Expand Up @@ -61,11 +61,11 @@ def __init__(self,
if pem_file:
pem_path = Path(pem_file).expanduser().resolve()
self.signer = UserSigner.from_pem_file(pem_path, pem_index)
self.address = Address(self.signer.get_pubkey().buffer, DEFAULT_HRP)
self.address = Address(self.signer.get_pubkey().buffer, get_address_hrp())
elif key_file and password:
key_file_path = Path(key_file).expanduser().resolve()
self.signer = UserSigner.from_wallet(key_file_path, password)
self.address = Address(self.signer.get_pubkey().buffer, DEFAULT_HRP)
self.address = Address(self.signer.get_pubkey().buffer, get_address_hrp())

def sign_transaction(self, transaction: ITransaction) -> str:
assert self.signer is not None
Expand Down
3 changes: 3 additions & 0 deletions multiversx_sdk_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, List

import argcomplete
from multiversx_sdk import LibraryConfig
from rich.logging import RichHandler

import multiversx_sdk_cli.cli_accounts
Expand Down Expand Up @@ -53,6 +54,8 @@ def _do_main(cli_args: List[str]):
logging.basicConfig(level="INFO", format='%(name)s: %(message)s', handlers=[RichHandler(show_time=False, rich_tracebacks=True)])

verify_deprecated_entries_in_config_file()
default_hrp = config.get_address_hrp()
LibraryConfig.default_address_hrp = default_hrp

if not hasattr(args, "func"):
parser.print_help()
Expand Down
6 changes: 3 additions & 3 deletions multiversx_sdk_cli/cli_dns.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any, List

from multiversx_sdk import Address, ProxyNetworkProvider
from multiversx_sdk import ProxyNetworkProvider
from prettytable import PrettyTable

from multiversx_sdk_cli import cli_shared
from multiversx_sdk_cli.config import get_config_for_network_providers
from multiversx_sdk_cli.constants import ADDRESS_ZERO_BECH32
from multiversx_sdk_cli.constants import ADDRESS_ZERO_HEX
from multiversx_sdk_cli.dns import (compute_dns_address_for_shard_id,
dns_address_for_name, name_hash, register,
registration_cost, resolve, validate_name,
Expand Down Expand Up @@ -82,7 +82,7 @@ def dns_resolve(args: Any):

config = get_config_for_network_providers()
addr = resolve(args.name, ProxyNetworkProvider(url=args.proxy, config=config))
if addr.to_hex() != Address.new_from_bech32(ADDRESS_ZERO_BECH32).to_hex():
if addr.to_hex() != ADDRESS_ZERO_HEX:
print(addr.to_bech32())


Expand Down
11 changes: 7 additions & 4 deletions multiversx_sdk_cli/cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from multiversx_sdk.core.address import get_shard_of_pubkey

from multiversx_sdk_cli import cli_shared, utils
from multiversx_sdk_cli.constants import DEFAULT_HRP, NUMBER_OF_SHARDS
from multiversx_sdk_cli.config import get_address_hrp
from multiversx_sdk_cli.constants import NUMBER_OF_SHARDS
from multiversx_sdk_cli.errors import (BadUserInput, KnownError,
WalletGenerationError)
from multiversx_sdk_cli.sign_verify import SignedMessage, sign_message
Expand Down Expand Up @@ -54,7 +55,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
)
sub.add_argument("--format", choices=WALLET_FORMATS, help="the format of the generated wallet file (default: %(default)s)", default=None)
sub.add_argument("--outfile", help="the output path and base file name for the generated wallet files (default: %(default)s)", type=str)
sub.add_argument("--address-hrp", help=f"the human-readable part of the address, when format is {WALLET_FORMAT_KEYSTORE_SECRET_KEY} or {WALLET_FORMAT_PEM} (default: %(default)s)", type=str, default=DEFAULT_HRP)
sub.add_argument("--address-hrp", help=f"the human-readable part of the address, when format is {WALLET_FORMAT_KEYSTORE_SECRET_KEY} or {WALLET_FORMAT_PEM} (default: %(default)s)", type=str, default=get_address_hrp())
sub.add_argument("--shard", type=int, help="the shard in which the address will be generated; (default: random)")
sub.set_defaults(func=wallet_new)

Expand All @@ -69,7 +70,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
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_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.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=get_address_hrp())
sub.set_defaults(func=convert_wallet)

sub = cli_shared.add_command_subparser(
Expand All @@ -82,6 +83,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
group = sub.add_mutually_exclusive_group(required=True)
group.add_argument("--encode", action="store_true", help="whether to encode")
group.add_argument("--decode", action="store_true", help="whether to decode")
sub.add_argument("--hrp", type=str, help="the human readable part; only used for encoding to bech32")
sub.set_defaults(func=do_bech32)

sub = cli_shared.add_command_subparser(
Expand Down Expand Up @@ -288,7 +290,8 @@ def do_bech32(args: Any):
value = args.value

if encode:
address = Address.new_from_hex(value, DEFAULT_HRP)
hrp = args.hrp if args.hrp else get_address_hrp()
address = Address.new_from_hex(value, hrp)
result = address.to_bech32()
else:
address = Address.new_from_bech32(value)
Expand Down
5 changes: 5 additions & 0 deletions multiversx_sdk_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def get_value(name: str) -> str:
return value


def get_address_hrp():
return get_value("default_address_hrp")


def set_value(name: str, value: Any):
_guard_valid_name(name)
data = read_file()
Expand Down Expand Up @@ -162,6 +166,7 @@ def get_defaults() -> Dict[str, Any]:
"dependencies.testwallets.urlTemplate.windows": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz",
"dependencies.wasm-opt.tag": "0.112.0",
"github_api_token": "",
"default_address_hrp": "erd"
}


Expand Down
3 changes: 1 addition & 2 deletions multiversx_sdk_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

DEFAULT_TX_VERSION = 2

DEFAULT_HRP = "erd"
ADDRESS_ZERO_BECH32 = "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"
ADDRESS_ZERO_HEX = "0000000000000000000000000000000000000000000000000000000000000000"

NUMBER_OF_SHARDS = 3
6 changes: 3 additions & 3 deletions multiversx_sdk_cli/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from multiversx_sdk_cli import errors
from multiversx_sdk_cli.accounts import Account
from multiversx_sdk_cli.constants import DEFAULT_HRP
from multiversx_sdk_cli.config import get_address_hrp
from multiversx_sdk_cli.interfaces import IAddress

logger = logging.getLogger("contracts")
Expand Down Expand Up @@ -211,7 +211,7 @@ def _prepare_args_for_factory(self, arguments: List[str]) -> List[Any]:
args.append(self._hex_to_bytes(arg))
elif arg.isnumeric():
args.append(int(arg))
elif arg.startswith(DEFAULT_HRP):
elif arg.startswith(get_address_hrp()):
args.append(Address.new_from_bech32(arg))
elif arg.lower() == FALSE_STR_LOWER:
args.append(False)
Expand Down Expand Up @@ -253,7 +253,7 @@ def _to_hex(arg: str):

if arg.isnumeric():
return _prepare_decimal(arg)
elif arg.startswith(DEFAULT_HRP):
elif arg.startswith(get_address_hrp()):
addr = Address.from_bech32(arg)
return _prepare_hexadecimal(f"{HEX_PREFIX}{addr.hex()}")
elif arg.lower() == FALSE_STR_LOWER or arg.lower() == TRUE_STR_LOWER:
Expand Down
9 changes: 5 additions & 4 deletions multiversx_sdk_cli/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from multiversx_sdk_cli import cli_shared, utils
from multiversx_sdk_cli.accounts import Account
from multiversx_sdk_cli.constants import ADDRESS_ZERO_BECH32, DEFAULT_HRP
from multiversx_sdk_cli.config import get_address_hrp
from multiversx_sdk_cli.constants import ADDRESS_ZERO_HEX
from multiversx_sdk_cli.contracts import SmartContract
from multiversx_sdk_cli.transactions import (compute_relayed_v1_data,
do_prepare_transaction)
Expand Down Expand Up @@ -36,10 +37,10 @@ def resolve(name: str, proxy: INetworkProvider) -> Address:
)

if len(response) == 0:
return Address.from_bech32(ADDRESS_ZERO_BECH32)
return Address.new_from_hex(ADDRESS_ZERO_HEX, get_address_hrp())

result = response[0].get("returnDataParts")[0]
return Address.from_hex(result, DEFAULT_HRP)
return Address.new_from_hex(result, get_address_hrp())


def validate_name(name: str, shard_id: int, proxy: INetworkProvider):
Expand Down Expand Up @@ -137,7 +138,7 @@ def compute_dns_address_for_shard_id(shard_id: int) -> Address:
deployer_pubkey_prefix = InitialDNSAddress[:len(InitialDNSAddress) - ShardIdentiferLen]

deployer_pubkey = deployer_pubkey_prefix + bytes([0, shard_id])
deployer = Account(address=Address(deployer_pubkey, DEFAULT_HRP))
deployer = Account(address=Address(deployer_pubkey, get_address_hrp()))
deployer.nonce = 0
address_computer = AddressComputer(number_of_shards=3)
contract_address = address_computer.compute_contract_address(deployer.address, deployer.nonce)
Expand Down
4 changes: 2 additions & 2 deletions multiversx_sdk_cli/sign_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def verify_signature(self) -> bool:
verifiable_message.signature = bytes.fromhex(self.signature)
message_computer = MessageComputer()

verifier = UserVerifier.from_address(Address.from_bech32(self.address))
verifier = UserVerifier.from_address(Address.new_from_bech32(self.address))
is_signed = verifier.verify(message_computer.compute_bytes_for_signing(verifiable_message), verifiable_message.signature)
return is_signed

Expand All @@ -37,4 +37,4 @@ def to_dictionary(self) -> Dict[str, str]:

def sign_message(message: str, account: Account) -> SignedMessage:
signature = account.sign_message(message.encode())
return SignedMessage(account.address.bech32(), message, signature)
return SignedMessage(account.address.to_bech32(), message, signature)
2 changes: 1 addition & 1 deletion multiversx_sdk_cli/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_guardian_account_from_args(args: Any):
account = Account(key_file=args.guardian_keyfile, password=password)
elif args.guardian_ledger:
address = do_get_ledger_address(account_index=args.guardian_ledger_account_index, address_index=args.guardian_ledger_address_index)
account = Account(address=Address.from_bech32(address))
account = Account(address=Address.new_from_bech32(address))
else:
raise errors.NoWalletProvided()

Expand Down
4 changes: 2 additions & 2 deletions 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.8.1"
version = "9.9.0"
authors = [
{ name="MultiversX" },
]
Expand All @@ -28,7 +28,7 @@ dependencies = [
"requests-cache",
"rich==13.3.4",
"argcomplete==3.2.2",
"multiversx-sdk==0.16.3"
"multiversx-sdk==0.19.0"
]

[project.scripts]
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ requests-cache
rich==13.3.4
argcomplete==3.2.2

multiversx-sdk==0.16.3
# multiversx-sdk==0.19.0
git+https://github.com/multiversx/mx-sdk-py@main

0 comments on commit 69b6526

Please sign in to comment.