michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
981 stars 252 forks source link

How to sign VersionedTransaction #370

Closed jxad closed 9 months ago

jxad commented 1 year ago

How do i sign / partial_sign a VersionedTransaction?

jxad commented 1 year ago

I managed to partially sign the V0 tx obtained from MagicEden API. Using this as a reference, I ended up with this code:

from solders.null_signer import NullSigner
from solders.transaction import VersionedTransaction

txv0_data = bytes(res["v0"]["txSigned"]["data"])

txv0 = VersionedTransaction.from_bytes(txv0_data)
sigs = txv0.signatures

payer = Keypair.from_base58_string("priv_key")
for i, acc in enumerate(txv0.message.account_keys):
  if txv0.message.is_signer(i) and acc != payer.pubkey():
     me_signer = acc

signers = (payer, NullSigner(me_signer))

new_tx = VersionedTransaction(txv0.message, signers)

sigs[0] = new_tx.signatures[0]

txv0.signatures = sigs

print(txv0.signatures)
print(txv0.verify_with_results())

Did I miss an easier way to achieve this?

kevinheavey commented 9 months ago

No that's basically how you do it. This answer also shows how to deserialize the partially signed txn and add the missing signature (the python code looks more or less the same): https://solana.stackexchange.com/a/5179/383