1

For context, this question is in the context of a typescript browser client.

How can I construct a VersionedTransaction from an input of raw bytes of a transaction?

With legacy Transaction, I can just use Transaction.from(rawBytes), but for a VersionedTransaction, the only option is to create it through a VersionedMessage.

I'm guessing what I need to do is something like example below, where I extract the message component from the transaction bytes.

Example:

const txBytes: Uint8Array = getBytes();
const versionedMessage = VersionedMessage.deserialize(
   transactionBytes.slice(<message_bytes_start>, <message_bytes_end>)
);

const versionedTx = new VersionedTransaction(versionedMessage);

The issue is I can't find a definitive source of what message_bytes_start and message_bytes_end is. Interested if there's an easier way to do this. Thanks!

2 Answers 2

2

Answered my own question!

Here's what worked for me.

    // Raw transaction bytes
    const txBytes = bs58.decode(input.transaction);

    // Assuming single byte variant, the first byte will specify how 
    // many signatures are in the transaction
    const numOfSignatures = txBytes[0];

    // 1 (num_of_signatures byte) + num_of_signatures * 64 (each sig is 64 bytes)
    const txHeaderLength = 1 + numOfSignatures * 64;

    // Now slice the message portion (everything from end_of_header to the end)
    const versionedMessage = VersionedMessage.deserialize(
      txBytes.slice(txHeaderLength, txBytes.length)
    );
    
    // Finally, construct the versionedTx
    const versionedTx = new VersionedTransaction(versionedMessage);
1

Did you try using VersionedTransaction.deserialize? This way you don't throw away the signatures already in the transaction: https://github.com/solana-labs/solana-web3.js/blob/c7ef49cc49ee61422a4777d439a814160f6d7ce4/packages/library-legacy/src/transaction/versioned.ts#L78

1
  • Ah that works! Didn't realize this method existed, lots of the docs only show the VersionedMessage constructor as the way to create a VersionedTransaction. Thanks! Commented Oct 19, 2023 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.