@@ -283,6 +283,17 @@ export function decodeRawTransaction(hexString: string): {
283283 } ;
284284}
285285
286+ /**
287+ * Converts a base64 encoded string to hex
288+ *
289+ * @param base64 - The base64 encoded string to convert
290+ * @returns {string } - The hex representation
291+ */
292+ export function getHexFromBase64 ( base64 : string ) : string {
293+ const buffer = Buffer . from ( base64 , 'base64' ) ;
294+ return buffer . toString ( 'hex' ) ;
295+ }
296+
286297/**
287298 * Indicates whether the passed string is a safe hex string for tron's purposes.
288299 *
@@ -837,3 +848,89 @@ export function decodeDataParams(types: string[], data: string): any[] {
837848 return obj ;
838849 } , [ ] ) ;
839850}
851+
852+ /**
853+ * Generate raw_data_hex for a TRON transaction
854+ *
855+ * @param {Object } rawData - The transaction raw data object containing:
856+ * @param {Array } rawData.contract - Array of contract objects
857+ * @param {string } rawData.refBlockBytes - Reference block bytes
858+ * @param {string } rawData.refBlockHash - Reference block hash
859+ * @param {number } rawData.expiration - Transaction expiration timestamp
860+ * @param {number } rawData.timestamp - Transaction creation timestamp
861+ * @param {number } [rawData.feeLimit] - Optional fee limit for smart contracts
862+ * @returns {string } The hex string representation of the encoded transaction data
863+ */
864+ export function generateRawDataHex (
865+ rawData : {
866+ contract ?: protocol . Transaction . Contract [ ] ;
867+ refBlockBytes ?: string ;
868+ refBlockHash ?: string ;
869+ expiration ?: number ;
870+ timestamp ?: number ;
871+ feeLimit ?: number ;
872+ } = { }
873+ ) : string {
874+ try {
875+ // Process contracts to ensure proper protobuf encoding
876+ let processedContracts = rawData . contract ;
877+ if ( rawData . contract && rawData . contract . length > 0 ) {
878+ processedContracts = rawData . contract . map ( ( contract ) => {
879+ // Handle TransferContract specifically
880+ if ( contract . parameter ?. type_url === 'type.googleapis.com/protocol.TransferContract' ) {
881+ const contractValue = contract . parameter . value as any ;
882+
883+ // Create the protobuf contract object
884+ const transferContract : any = { } ;
885+
886+ // Handle owner_address (required field)
887+ if ( contractValue . owner_address ) {
888+ transferContract . ownerAddress = Buffer . from ( contractValue . owner_address , 'hex' ) ;
889+ }
890+
891+ // Handle to_address (required field)
892+ if ( contractValue . to_address ) {
893+ transferContract . toAddress = Buffer . from ( contractValue . to_address , 'hex' ) ;
894+ }
895+
896+ // Handle amount (required field)
897+ if ( contractValue . amount !== undefined ) {
898+ transferContract . amount = contractValue . amount ;
899+ }
900+
901+ // Encode the contract using protobuf
902+ const encodedContract = protocol . TransferContract . encode ( transferContract ) . finish ( ) ;
903+ const base64Value = Buffer . from ( encodedContract ) . toString ( 'base64' ) ;
904+
905+ return {
906+ ...contract ,
907+ parameter : {
908+ ...contract . parameter ,
909+ value : base64Value ,
910+ } ,
911+ } as any ;
912+ }
913+
914+ return contract ;
915+ } ) as protocol . Transaction . Contract [ ] ;
916+ }
917+
918+ // Create raw transaction object matching protobuf schema
919+ const rawTx : protocol . Transaction . Iraw = {
920+ contract : processedContracts ,
921+ refBlockBytes : rawData . refBlockBytes ? Buffer . from ( rawData . refBlockBytes , 'hex' ) : undefined ,
922+ refBlockHash : rawData . refBlockHash ? Buffer . from ( rawData . refBlockHash , 'hex' ) : undefined ,
923+ expiration : rawData . expiration ,
924+ timestamp : rawData . timestamp ,
925+ feeLimit : rawData . feeLimit ,
926+ } ;
927+
928+ // Encode using protobuf and get final bytes
929+ const encodedBytes = protocol . Transaction . raw . encode ( rawTx ) . finish ( ) ;
930+
931+ // Convert to hex string
932+ return Buffer . from ( encodedBytes ) . toString ( 'hex' ) ;
933+ } catch ( e ) {
934+ throw new UtilsError ( 'Failed to generate raw data hex: ' + e . message ) ;
935+ }
936+ }
0 commit comments