Skip to content

Commit

Permalink
Merge pull request #461 from multiversx/fix-signing-with-ledger
Browse files Browse the repository at this point in the history
Set correct options when signing with Ledger
  • Loading branch information
popenta authored Jan 15, 2025
2 parents 90a6cac + 6d8fa48 commit ff6dd67
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions multiversx_sdk_cli/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def __init__(self, account_index: int = 0, address_index: int = 0) -> None:
def sign_transaction(self, transaction: ITransaction) -> str:
ledger_version = do_get_ledger_version()
should_use_hash_signing = compare_versions(ledger_version, SIGN_USING_HASH_VERSION) >= 0

# TODO: This check will be removed in the next major release.
if should_use_hash_signing:
transaction.version = TX_HASH_SIGN_VERSION
transaction.options = transaction.options | TX_HASH_SIGN_OPTIONS
Expand Down
30 changes: 20 additions & 10 deletions multiversx_sdk_cli/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import json
import logging
import time
from typing import Any, Dict, List, Optional, Protocol, TextIO
from typing import Any, Dict, List, Optional, Protocol, TextIO, Union

from multiversx_sdk import (Address, Token, TokenComputer, TokenTransfer,
Transaction, TransactionsConverter,
TransactionsFactoryConfig,
TransferTransactionsFactory)
TransferTransactionsFactory, TransactionComputer)

from multiversx_sdk_cli import errors
from multiversx_sdk_cli.accounts import Account, LedgerAccount
from multiversx_sdk_cli.accounts import Account, AccountBase, LedgerAccount
from multiversx_sdk_cli.cli_password import (load_guardian_password,
load_password)
from multiversx_sdk_cli.cosign_transaction import cosign_transaction
Expand Down Expand Up @@ -75,6 +75,10 @@ def do_prepare_transaction(args: Any) -> Transaction:
tx.version = int(args.version)
tx.options = int(args.options)

tx_computer = TransactionComputer()
if isinstance(account, LedgerAccount):
tx_computer.apply_options_for_hash_signing(tx)

if args.guardian:
tx.guardian = args.guardian

Expand All @@ -86,14 +90,25 @@ def do_prepare_transaction(args: Any) -> Transaction:
if relayer_account.address.to_bech32() != tx.relayer:
raise IncorrectWalletError("")

if isinstance(relayer_account, LedgerAccount):
tx_computer.apply_options_for_hash_signing(tx)

tx.relayer_signature = bytes.fromhex(relayer_account.sign_transaction(tx))
except NoWalletProvided:
logger.warning("Relayer wallet not provided. Transaction will not be signed by relayer.")
except IncorrectWalletError:
raise IncorrectWalletError("Relayer wallet does not match the relayer's address set in the transaction.")

try:
guardian_account = get_guardian_account_from_args(args)
if isinstance(guardian_account, LedgerAccount):
tx_computer.apply_options_for_hash_signing(tx)

except NoWalletProvided:
guardian_account = None

tx.signature = bytes.fromhex(account.sign_transaction(tx))
tx = sign_tx_by_guardian(args, tx)
tx = sign_tx_by_guardian(args, tx, guardian_account)

return tx

Expand Down Expand Up @@ -141,12 +156,7 @@ def prepare_token_transfers(transfers: List[Any]) -> List[TokenTransfer]:
return token_transfers


def sign_tx_by_guardian(args: Any, tx: Transaction) -> Transaction:
try:
guardian_account = get_guardian_account_from_args(args)
except NoWalletProvided:
guardian_account = None

def sign_tx_by_guardian(args: Any, tx: Transaction, guardian_account: Union[AccountBase, None]) -> Transaction:
if guardian_account:
tx.guardian_signature = bytes.fromhex(guardian_account.sign_transaction(tx))
elif args.guardian:
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.0"
version = "9.10.1"
authors = [
{ name="MultiversX" },
]
Expand Down

0 comments on commit ff6dd67

Please sign in to comment.