import { Transaction, PublicKey } from '@solana/web3.js';
const signLegacyTransaction = async (tx: Transaction) => {
// Serialize the transaction message
const messageBytes = tx.serializeMessage();
const rawDataBase64 = Buffer.from(messageBytes).toString('base64');
const body = { message_base64: rawDataBase64 };
const res = await fetch('/v1/wallet/sign/message', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${YOUR_JWT_TOKEN}`,
'X-Magic-API-Key': 'YOUR_MAGIC_API_KEY',
'X-OIDC-Provider-ID': 'YOUR_OIDC_PROVIDER_ID',
'X-Magic-Chain': 'SOL'
},
body: JSON.stringify(body)
});
const { signature } = await res.json();
// Convert signature back to bytes and attach to transaction
// Handle both hex (0x prefixed) and base64 signatures
const sigBytes = signature.startsWith('0x')
? Buffer.from(signature.slice(2), 'hex')
: Buffer.from(signature, 'base64');
const signerPk = new PublicKey('YOUR_WALLET_PUBLIC_KEY');
tx.addSignature(signerPk, sigBytes);
return tx.serialize();
};