Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions example/utils/getSignature.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import bs58 from "bs58";
import { Transaction, VersionedTransaction } from "@solana/web3.js";

/**
* @notice Extracts the primary signature (which acts as the transaction ID) from a Solana transaction object
* and encodes it into the Base58 string format.
* @param transaction The Solana Transaction (legacy) or VersionedTransaction object.
* @returns The Base58-encoded transaction signature string.
* @throws Error if the transaction does not contain a signature (meaning it wasn't signed by the fee payer).
*/
export function getSignature(
transaction: Transaction | VersionedTransaction
): string {
const signature =
"signature" in transaction
// Use a type guard to correctly access the signature based on the transaction type.
// For VersionedTransaction, the first signature in the array is the fee payer's signature (the Tx ID).
// For the legacy Transaction type, the signature field is used.
const rawSignature: Buffer | Uint8Array | null | undefined =
'signature' in transaction
? transaction.signature
: transaction.signatures[0];
if (!signature) {

// The signature must be present for a transaction to have an ID.
if (!rawSignature) {
throw new Error(
"Missing transaction signature, the transaction was not signed by the fee payer"
"Missing transaction signature; the transaction was not signed by the fee payer."
);
}
return bs58.encode(signature);

// Ensure the signature is encoded correctly from its raw byte format.
// We use the 'rawSignature' variable directly for bs58.encode.
return bs58.encode(rawSignature);
}