@@ -389,12 +389,36 @@ async def _sign_and_execute( # pragma: no cover
389389 message_bytes = to_bytes_versioned (transaction .message )
390390 payer_signature = payer_keypair .sign_message (message_bytes )
391391
392- # Create partially signed transaction
393- partially_signed = VersionedTransaction .populate (
394- transaction .message ,
395- [payer_signature , transaction .signatures [1 ]],
396- )
397- tx_to_sign = base64 .b64encode (bytes (partially_signed )).decode ("utf-8" )
392+ # Find the payer's position in the account keys
393+ # The first N accounts (where N = num_required_signatures) are signers
394+ payer_pubkey = payer_keypair .pubkey ()
395+ num_signers = transaction .message .header .num_required_signatures
396+ account_keys = transaction .message .account_keys
397+
398+ payer_index = None
399+ for i in range (num_signers ):
400+ if account_keys [i ] == payer_pubkey :
401+ payer_index = i
402+ break
403+
404+ if payer_index is None :
405+ logger .warning (
406+ f"Payer pubkey { payer_pubkey } not found in signers. "
407+ f"Signers: { [str (account_keys [i ]) for i in range (num_signers )]} "
408+ )
409+ # Payer not in transaction - this might be a non-gasless transaction
410+ # Just pass through to Privy signing
411+ else :
412+ # Create signature list with payer signature in correct position
413+ new_signatures = list (transaction .signatures )
414+ new_signatures [payer_index ] = payer_signature
415+ logger .info (f"Payer signed at index { payer_index } " )
416+
417+ partially_signed = VersionedTransaction .populate (
418+ transaction .message ,
419+ new_signatures ,
420+ )
421+ tx_to_sign = base64 .b64encode (bytes (partially_signed )).decode ("utf-8" )
398422
399423 # Step 4: Sign with Privy using the official SDK
400424 signed_tx = await _privy_sign_transaction (
0 commit comments