pip install solana==0.35.1 pip install solders==0.21.0def send_transaction_via_rpc(private_key_base58, transaction_base64, latest_blockhash):try:# Decode the Base58 private key and create a Keypairprivate_key = base58.b58decode(private_key_base58)keypair = Keypair.from_bytes(private_key)
# Decode the transaction transaction_bytes = base64.b64decode(transaction_base64) transaction = VersionedTransaction.from_bytes(transaction_bytes) # Ensure that account_keys[0] in the transaction matches our own public key print(f"Account keys in the transaction: {transaction.message.account_keys}") print(f"Local Keypair public key: {keypair.pubkey()}") if keypair.pubkey() != transaction.message.account_keys[0]: print(f"Error: Keypair public key does not match the account key in the transaction") return None # Replace the blockhash recent_blockhash = Hash.from_string(latest_blockhash) # Create a new transaction message, ensuring address_table_lookups are provided new_message = MessageV0( header=transaction.message.header, account_keys=transaction.message.account_keys, recent_blockhash=recent_blockhash, instructions=transaction.message.instructions, address_table_lookups=transaction.message.address_table_lookups if hasattr(transaction.message, 'address_table_lookups') else [] # Default to an empty list ) # Create a new transaction new_transaction = VersionedTransaction(new_message, []) # Sign the transaction new_transaction.sign([keypair]) # Ensure only your key signs the transaction print(f"Transaction signed successfully, public key: {keypair.pubkey()}") # Send the transaction response = client.send_transaction(new_transaction) print(f"Transaction sent successfully, response: {response}") return responseexcept Exception as e: print(f"Transaction failed: {e}") return None