Skip to content

Commit 617a008

Browse files
committed
move key validation into _setup_transaction_account
1 parent 4968619 commit 617a008

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

src/models/blockchain_client.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,29 @@ def _get_working_web3_connection(
116116
raise ConnectionError(f"Failed to connect to any of {len(rpc_providers)} RPC providers: {rpc_providers}")
117117

118118

119-
def _setup_transaction_account(self, private_key: str) -> str:
119+
def _setup_transaction_account(self, private_key: str) -> Tuple[str, str]:
120120
"""
121-
Get the address of the account from the private key.
121+
Validate the private key and return the formatted key and account address.
122122
123123
Args:
124-
private_key: Private key for the account
124+
private_key: The private key string.
125125
126126
Returns:
127-
str: Address of the account
127+
A tuple containing the account address and the formatted private key.
128+
129+
Raises:
130+
KeyValidationError: If the private key is invalid.
128131
"""
129132
try:
130-
account = Web3().eth.account.from_key(private_key)
133+
formatted_key = validate_and_format_private_key(private_key)
134+
account = Web3().eth.account.from_key(formatted_key)
131135
logger.info(f"Using account: {account.address}")
132-
return account.address
136+
return account.address, formatted_key
137+
138+
except KeyValidationError as e:
139+
logger.error(f"Invalid private key provided: {e}")
140+
raise
133141

134-
# If the account cannot be retrieved, log the error and raise an exception
135142
except Exception as e:
136143
logger.error(f"Failed to retrieve account from private key: {str(e)}")
137144
raise
@@ -530,15 +537,15 @@ def send_transaction_to_allow_indexers(
530537
Returns:
531538
str: Transaction hash
532539
"""
533-
# Set up account
534-
sender_address = self._setup_transaction_account(private_key)
540+
# Set up account and validate private key
541+
sender_address, formatted_private_key = self._setup_transaction_account(private_key)
535542

536543
# Convert addresses to checksum format
537544
checksum_addresses = [Web3.to_checksum_address(addr) for addr in indexer_addresses]
538545

539546
# Prepare all parameters for the transaction
540547
transaction_params = {
541-
"private_key": private_key,
548+
"private_key": formatted_private_key,
542549
"contract_function": contract_function,
543550
"indexer_addresses": checksum_addresses,
544551
"data_bytes": data_bytes,
@@ -596,43 +603,36 @@ def batch_allow_indexers_issuance_eligibility(
596603
num_batches = (total_indexers + batch_size - 1) // batch_size
597604
logger.info(f"Processing {total_indexers} indexers in {num_batches} batch(es) of {batch_size}")
598605

599-
try:
600-
tx_links = []
601-
# Validate and format private key
602-
validated_private_key = validate_and_format_private_key(private_key)
603-
604-
# Process each batch
605-
for i in range(num_batches):
606-
start_idx = i * batch_size
607-
end_idx = min(start_idx + batch_size, total_indexers)
608-
batch_indexers = indexer_addresses[start_idx:end_idx]
609-
610-
logger.info(f"Processing batch {i+1}/{num_batches} with {len(batch_indexers)} indexers")
611-
612-
# Try to send the transaction to the network (uses RPC failover)
613-
try:
614-
tx_hash = self.send_transaction_to_allow_indexers(
615-
batch_indexers,
616-
validated_private_key,
617-
chain_id,
618-
contract_function,
619-
replace,
620-
data_bytes,
621-
)
622-
tx_links.append(f"{self.block_explorer_url}/tx/{tx_hash}")
623-
logger.info(f"Batch {i+1} transaction successful: {tx_hash}")
606+
tx_links = []
624607

625-
# If we get an error, log the error and raise an exception
626-
except Exception as e:
627-
logger.error(f"Error processing batch {i+1} due to: {e}")
628-
raise
608+
# Process each batch
609+
for i in range(num_batches):
610+
start_idx = i * batch_size
611+
end_idx = min(start_idx + batch_size, total_indexers)
612+
batch_indexers = indexer_addresses[start_idx:end_idx]
629613

630-
# Log all transaction links
631-
for i, tx_link in enumerate(tx_links, 1):
632-
logger.info(f"Transaction link {i} of {len(tx_links)}: {tx_link}")
614+
logger.info(f"Processing batch {i+1}/{num_batches} with {len(batch_indexers)} indexers")
633615

634-
return tx_links
616+
# Try to send the transaction to the network (uses RPC failover)
617+
try:
618+
tx_hash = self.send_transaction_to_allow_indexers(
619+
batch_indexers,
620+
private_key,
621+
chain_id,
622+
contract_function,
623+
replace,
624+
data_bytes,
625+
)
626+
tx_links.append(f"{self.block_explorer_url}/tx/{tx_hash}")
627+
logger.info(f"Batch {i+1} transaction successful: {tx_hash}")
635628

636-
except KeyValidationError as e:
637-
logger.error(f"Private key validation failed: {e}")
638-
raise ValueError(f"Invalid private key: {e}") from e
629+
# If we get an error, log the error and raise an exception
630+
except Exception as e:
631+
logger.error(f"Error processing batch {i+1} due to: {e}")
632+
raise
633+
634+
# Log all transaction links
635+
for i, tx_link in enumerate(tx_links, 1):
636+
logger.info(f"Transaction link {i} of {len(tx_links)}: {tx_link}")
637+
638+
return tx_links

0 commit comments

Comments
 (0)