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

Replace transaction class with the one from sdk-core #278

Merged
merged 9 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 2 additions & 2 deletions multiversx_sdk_cli/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pathlib import Path
from typing import Any, Optional, Protocol

from multiversx_sdk_core import Address, MessageV1

Check failure on line 5 in multiversx_sdk_cli/accounts.py

View workflow job for this annotation

GitHub Actions / runner / mypy

[mypy] reported by reviewdog 🐶 Skipping analyzing "multiversx_sdk_core": module is installed, but missing library stubs or py.typed marker [import] Raw Output: /home/runner/work/mx-sdk-py-cli/mx-sdk-py-cli/multiversx_sdk_cli/accounts.py:5:1: error: Skipping analyzing "multiversx_sdk_core": module is installed, but missing library stubs or py.typed marker [import]
from multiversx_sdk_network_providers.accounts import AccountOnNetwork

Check failure on line 6 in multiversx_sdk_cli/accounts.py

View workflow job for this annotation

GitHub Actions / runner / mypy

[mypy] reported by reviewdog 🐶 Skipping analyzing "multiversx_sdk_network_providers.accounts": module is installed, but missing library stubs or py.typed marker [import] Raw Output: /home/runner/work/mx-sdk-py-cli/mx-sdk-py-cli/multiversx_sdk_cli/accounts.py:6:1: error: Skipping analyzing "multiversx_sdk_network_providers.accounts": module is installed, but missing library stubs or py.typed marker [import]
from multiversx_sdk_wallet import UserSigner

from multiversx_sdk_cli.constants import DEFAULT_HRP
Expand Down Expand Up @@ -91,8 +91,8 @@
ledger_version = do_get_ledger_version()
should_use_hash_signing = compare_versions(ledger_version, SIGN_USING_HASH_VERSION) >= 0
if should_use_hash_signing:
transaction.set_version(TX_HASH_SIGN_VERSION)
transaction.set_options(TX_HASH_SIGN_OPTIONS)
transaction.version = TX_HASH_SIGN_VERSION
transaction.options = TX_HASH_SIGN_OPTIONS

signature = do_sign_transaction_with_ledger(
transaction.serialize_for_signing(),
Expand Down
11 changes: 2 additions & 9 deletions multiversx_sdk_cli/cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from pathlib import Path
from typing import Any, Dict, List

from multiversx_sdk_core import Address, Transaction
from multiversx_sdk_network_providers.proxy_network_provider import \
ProxyNetworkProvider

from multiversx_sdk_core import Address

from multiversx_sdk_cli import cli_shared, errors, projects, utils
from multiversx_sdk_cli.accounts import Account, LedgerAccount
from multiversx_sdk_cli.cli_output import CLIOutputBuilder
Expand All @@ -19,7 +18,6 @@
from multiversx_sdk_cli.docker import is_docker_installed, run_docker
from multiversx_sdk_cli.errors import DockerMissingError, NoWalletProvided
from multiversx_sdk_cli.projects.core import get_project_paths_recursively
from multiversx_sdk_cli.transactions import Transaction

logger = logging.getLogger("cli.contracts")

Expand Down Expand Up @@ -377,21 +375,16 @@ def _prepare_signer(args: Any) -> Account:


def _sign_guarded_tx(args: Any, tx: Transaction) -> Transaction:
signature = tx.signature
tx.signature = ""

try:
guardian_account = cli_shared.prepare_guardian_account(args)
except NoWalletProvided:
guardian_account = None

if guardian_account:
tx.guardianSignature = guardian_account.sign_transaction(tx)
tx.guardian_signature = bytes.fromhex(guardian_account.sign_transaction(tx))
elif args.guardian:
tx = cosign_transaction(tx, args.guardian_service_url, args.guardian_2fa_code) # type: ignore

tx.signature = signature

return tx


Expand Down
11 changes: 8 additions & 3 deletions multiversx_sdk_cli/cli_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
from collections import OrderedDict
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, Optional, Union

from multiversx_sdk_core import Address

Expand All @@ -14,13 +14,18 @@

class CLIOutputBuilder:
def __init__(self) -> None:
self.emitted_transaction_hash: Optional[str] = None
self.emitted_transaction: Union[ITransaction, None] = None
self.emitted_transaction_omitted_fields: List[str] = []
self.contract_address: Union[Address, None] = None
self.transaction_on_network: Union[ISerializable, None] = None
self.transaction_on_network_omitted_fields: List[str] = []
self.simulation_results: Union[ISerializable, None] = None

def set_emitted_transaction_hash(self, hash: str):
self.emitted_transaction_hash = hash
return self

def set_emitted_transaction(self, emitted_transaction: ITransaction, omitted_fields: List[str] = []):
self.emitted_transaction = emitted_transaction
self.emitted_transaction_omitted_fields = omitted_fields
Expand All @@ -47,8 +52,8 @@ def build(self) -> Dict[str, Any]:

if self.emitted_transaction:
emitted_transaction_dict = self.emitted_transaction.to_dictionary()
emitted_transaction_hash = self.emitted_transaction.get_hash() or ""
emitted_transaction_data = self.emitted_transaction.get_data() or ""
emitted_transaction_hash = self.emitted_transaction_hash or ""
emitted_transaction_data = str(self.emitted_transaction.data)
utils.omit_fields(emitted_transaction_dict, self.emitted_transaction_omitted_fields)

output["emittedTransaction"] = emitted_transaction_dict
Expand Down
6 changes: 3 additions & 3 deletions multiversx_sdk_cli/cli_password.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Any
from getpass import getpass
from typing import Any


def load_password(args: Any):
def load_password(args: Any) -> str:
if args.passfile:
with open(args.passfile) as pass_file:
return pass_file.read().strip()
return getpass("Keyfile's password: ")


def load_guardian_password(args: Any):
def load_guardian_password(args: Any) -> str:
if args.guardian_passfile:
with open(args.guardian_passfile) as pass_file:
return pass_file.read().strip()
Expand Down
10 changes: 6 additions & 4 deletions multiversx_sdk_cli/cli_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from multiversx_sdk_cli.cli_password import (load_guardian_password,
load_password)
from multiversx_sdk_cli.constants import TRANSACTION_OPTIONS_TX_GUARDED
from multiversx_sdk_cli.interfaces import ITransaction
from multiversx_sdk_cli.ledger.ledger_functions import do_get_ledger_address
from multiversx_sdk_cli.simulation import Simulator
from multiversx_sdk_cli.transactions import Transaction
from multiversx_sdk_cli.transactions import send_and_wait_for_result


def wider_help_formatter(prog: Text):
Expand Down Expand Up @@ -220,7 +221,7 @@ def check_options_for_guarded_tx(options: int):
raise errors.BadUsage("Invalid guarded transaction's options. The second least significant bit must be set.")


def send_or_simulate(tx: Transaction, args: Any, dump_output: bool = True) -> CLIOutputBuilder:
def send_or_simulate(tx: ITransaction, args: Any, dump_output: bool = True) -> CLIOutputBuilder:
proxy = ProxyNetworkProvider(args.proxy)

is_set_wait_result = hasattr(args, "wait_result") and args.wait_result
Expand All @@ -237,10 +238,11 @@ def send_or_simulate(tx: Transaction, args: Any, dump_output: bool = True) -> CL

try:
if send_wait_result:
transaction_on_network = tx.send_wait_result(proxy, args.timeout)
transaction_on_network = send_and_wait_for_result(tx, proxy, args.timeout)
output_builder.set_awaited_transaction(transaction_on_network)
elif send_only:
tx.send(proxy)
hash = proxy.send_transaction(tx)
output_builder.set_emitted_transaction_hash(hash)
elif simulate:
simulation = Simulator(proxy).run(tx)
output_builder.set_simulation_results(simulation)
Expand Down
33 changes: 17 additions & 16 deletions multiversx_sdk_cli/cli_transactions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from pathlib import Path
from typing import Any, List

from multiversx_sdk_network_providers.proxy_network_provider import \
ProxyNetworkProvider

from multiversx_sdk_cli import cli_shared, utils
from multiversx_sdk_cli.cli_output import CLIOutputBuilder
from multiversx_sdk_network_providers.proxy_network_provider import ProxyNetworkProvider
from multiversx_sdk_cli.errors import NoWalletProvided
from multiversx_sdk_cli.transactions import Transaction, do_prepare_transaction
from multiversx_sdk_cli.cosign_transaction import cosign_transaction
from multiversx_sdk_cli.errors import NoWalletProvided
from multiversx_sdk_cli.transactions import (do_prepare_transaction,
load_transaction_from_file,
serialize_as_inner)


def setup_parser(args: List[str], subparsers: Any) -> Any:
Expand Down Expand Up @@ -72,7 +76,7 @@ def create_transaction(args: Any):
tx = do_prepare_transaction(args)

if hasattr(args, "relay") and args.relay:
args.outfile.write(tx.serialize_as_inner())
args.outfile.write(serialize_as_inner(tx))
return

cli_shared.send_or_simulate(tx, args)
Expand All @@ -81,12 +85,15 @@ def create_transaction(args: Any):
def send_transaction(args: Any):
args = utils.as_object(args)

tx = Transaction.load_from_file(args.infile)
tx = load_transaction_from_file(args.infile)
output = CLIOutputBuilder()

try:
tx.send(ProxyNetworkProvider(args.proxy))
proxy = ProxyNetworkProvider(args.proxy)
tx_hash = proxy.send_transaction(tx)
output.set_emitted_transaction_hash(tx_hash)
finally:
output = CLIOutputBuilder().set_emitted_transaction(tx).build()
output = output.set_emitted_transaction(tx).build()
utils.dump_out_json(output, outfile=args.outfile)


Expand All @@ -106,27 +113,21 @@ def sign_transaction(args: Any):
cli_shared.check_guardian_args(args)
cli_shared.check_broadcast_args(args)

tx = Transaction.load_from_file(args.infile)
tx = load_transaction_from_file(args.infile)
if args.guardian:
cli_shared.check_options_for_guarded_tx(tx.options)

# clear existing signatures, if any
tx.guardianSignature = ""
tx.signature = ""

account = cli_shared.prepare_account(args)
signature = account.sign_transaction(tx)
tx.signature = bytes.fromhex(account.sign_transaction(tx))

try:
guardian_account = cli_shared.prepare_guardian_account(args)
except NoWalletProvided:
guardian_account = None

if guardian_account:
tx.guardianSignature = guardian_account.sign_transaction(tx)
tx.guardian_signature = bytes.fromhex(guardian_account.sign_transaction(tx))
elif args.guardian:
tx = cosign_transaction(tx, args.guardian_service_url, args.guardian_2fa_code)

tx.signature = signature

cli_shared.send_or_simulate(tx, args)
Loading
Loading