@@ -30,6 +30,7 @@ import { encode } from '@ethereumjs/rlp';
3030import { logger } from "../utils/logger" ;
3131import * as utils from '../utils/helper' ;
3232import * as constants from '../utils/constants' ;
33+ import { bytesToHex , hexToBytes } from '../utils/bytes' ;
3334
3435
3536export interface Transaction {
@@ -152,14 +153,14 @@ export async function encryptMessage(
152153 const encryptedRawMessage = await encryptRawMessage ( data , publicKeyResponse . commonBLSPublicKey , sanitizedAADTE , sanitizedAADAES ) ;
153154
154155 // RLP encode epochID and encrypted message
155- const rlpEncodedResult = rlpEncodeMessageData ( [ publicKeyResponse . epochId , Buffer . from ( encryptedRawMessage , 'hex' ) ] ) ;
156+ const rlpEncodedResult = rlpEncodeMessageData ( [ publicKeyResponse . epochId , hexToBytes ( encryptedRawMessage ) ] ) ;
156157 return `0x${ rlpEncodedResult } ` ;
157158 } else {
158159 const encryptedRawMessage = await encryptRawMessageDualKey ( data , committees [ 0 ] . commonBLSPublicKey ,
159160 committees [ 1 ] . commonBLSPublicKey , sanitizedAADTE , sanitizedAADAES ) ;
160161
161162 // RLP encode epochID and encrypted message
162- const rlpEncodedResult = rlpEncodeMessageData ( [ committees [ 0 ] . epochId , Buffer . from ( encryptedRawMessage , 'hex' ) ] ) ;
163+ const rlpEncodedResult = rlpEncodeMessageData ( [ committees [ 0 ] . epochId , hexToBytes ( encryptedRawMessage ) ] ) ;
163164 return `0x${ rlpEncodedResult } ` ;
164165 }
165166 } catch ( error ) {
@@ -188,7 +189,7 @@ export async function encryptMessageMockup(
188189 const epochId = 0 ;
189190
190191 // RLP encode epochID and encrypted message
191- const rlpEncodedResult = rlpEncodeMessageData ( [ epochId , Buffer . from ( encryptedRawMessage , 'hex' ) ] ) ;
192+ const rlpEncodedResult = rlpEncodeMessageData ( [ epochId , hexToBytes ( encryptedRawMessage ) ] ) ;
192193 return `0x${ rlpEncodedResult } ` ;
193194 } catch ( error ) {
194195 logger . error ( 'Error encrypting message:' , error ) ;
@@ -234,15 +235,15 @@ function validateAndExtractTransactionFields(tx: Transaction): Transaction {
234235 */
235236function rlpEncodeTransactionData ( txTo : string , txData : string ) : string {
236237 try {
237- // Convert hex strings to Buffer for RLP encoding
238- const toBuffer = Buffer . from ( txTo , 'hex' ) ;
239- const dataBuffer = Buffer . from ( txData , 'hex' ) ;
238+ // Convert hex strings to bytes for RLP encoding
239+ const toBytes = hexToBytes ( txTo ) ;
240+ const dataBytes = hexToBytes ( txData ) ;
240241
241242 // RLP encode as array [txData, txTo]
242- const rlpEncoded = encode ( [ dataBuffer , toBuffer ] ) ;
243+ const rlpEncoded = encode ( [ dataBytes , toBytes ] ) ;
243244
244245 // Convert back to hex string without 0x prefix
245- return Buffer . from ( rlpEncoded ) . toString ( 'hex' ) ;
246+ return bytesToHex ( rlpEncoded ) ;
246247 } catch ( error ) {
247248 logger . error ( 'Error RLP encoding transaction data:' , error ) ;
248249 throw new Error ( 'Failed to RLP encode transaction data' ) ;
@@ -251,16 +252,16 @@ function rlpEncodeTransactionData(txTo: string, txData: string): string {
251252
252253/**
253254 * RLP encodes array of epochId and encrypted message
254- * @param {(number | Buffer | (number | Buffer )[])[] } data - Array of data to RLP encode
255+ * @param {(number | Uint8Array | (number | Uint8Array )[])[] } data - Array of data to RLP encode
255256 * @returns {string } RLP encoded data as hex string (without 0x prefix)
256257 */
257- function rlpEncodeMessageData ( data : ( number | Buffer | ( number | Buffer ) [ ] ) [ ] ) : string {
258+ function rlpEncodeMessageData ( data : ( number | Uint8Array | ( number | Uint8Array ) [ ] ) [ ] ) : string {
258259 try {
259260 // RLP encode the array
260261 const rlpEncoded = encode ( data ) ;
261262
262263 // Convert back to hex string without 0x prefix
263- return Buffer . from ( rlpEncoded ) . toString ( 'hex' ) ;
264+ return bytesToHex ( rlpEncoded ) ;
264265 } catch ( error ) {
265266 logger . error ( 'Error RLP encoding message data:' , error ) ;
266267 throw new Error ( 'Failed to RLP encode message data' ) ;
0 commit comments