Pre-Requisite: You have generated the transaction context, payload, and Msgs (see prior pages).
Signing with Solana - Phantom Wallet
Phantom / Solana do not allow message signatures > ~1000 bytes. To workaround this, we allow signing the SHA256 hash of the JSON in cases where payload.jsonToSign.length > 1000.
constgetProvider= () => {if ('phantom'in window) {constphantomWindow= window asany;constprovider=phantomWindow.phantom?.solana;if (provider?.isPhantom) {return provider; }window.open('https://phantom.app/','_blank'); }};constsignTxn=async (context:ExternalTxContext, payload:TransactionPayload, messages:any[], simulate:boolean) => {if (!account) thrownewError('Account not found.');let sig ='';if (!simulate) {//Phantom has a weird error where messages must be < ~1000 bytes//If we are within limit, we can have user sign the JSON//Else, we hash the JSON and have user sign the hashconstencodedMessage=newTextEncoder().encode(payload.txnString);constsignedMessage=awaitgetProvider().request({ method:'signMessage', params: { message: encodedMessage, display:'utf8' } }); sig =signedMessage.signature.toString('hex'); }//We need to pass in solAddress manually hereconsttxBody=createTxBroadcastBody(context, messages, sig, address);return txBody;};
Output
This will leave you with a variable which is to be submitted to a running blockchain node. See Broadcast to a Node.