11import { arrayify } from 'fuels' ;
22import { stringToHex } from 'viem' ;
33import { BYTE_VERSION_LIST , BytesVersion } from '../types' ;
4- import { getTxIdEncoded } from '..' ;
54import { ENCODING_VERSIONS } from '../utils/versionsByEncode' ;
65
76/**
87 * Service class for encoding operations related to transaction IDs and general encoding tasks.
98 */
109export class EncodingService {
11- /**
12- * Set of predicate versions for efficient lookup of byte-encoding requirements.
13- */
14- private static readonly BYTE_VERSION_SET = new Set < string > (
15- BYTE_VERSION_LIST as readonly string [ ] ,
16- ) ;
17-
18-
1910 /**
2011 * Encodes a transaction ID based on the predicate version requirements.
2112 */
2213 static encodeTxId ( txId : string , version : string ) : Uint8Array | string {
2314 switch ( true ) {
24- case EncodingService . BYTE_VERSION_SET . has ( version ) :
15+ case this . requiresByteEncoding ( version ) :
2516 return arrayify ( txId . startsWith ( '0x' ) ? txId : `0x${ txId } ` ) ;
2617 default :
2718 return txId . startsWith ( '0x' )
@@ -33,8 +24,8 @@ export class EncodingService {
3324 /**
3425 * Encodes a transaction ID for BakoSafe predicates
3526 */
36- static bakosafeEncode = ( txId : string , version : string ) => {
37- return this . encodeTxId ( txId , version ) as string ;
27+ static bakosafeEncode = ( txId : string ) => {
28+ return txId . startsWith ( '0x' ) ? txId . slice ( 2 ) : txId ;
3829 } ;
3930
4031 /**
@@ -45,15 +36,15 @@ export class EncodingService {
4536 } ;
4637
4738 /**
48- * Determines the encoding function for a given version
49- */
39+ * Determines the encoding function for a given version
40+ */
5041 static determineEncodingFunction ( version : string ) : ( txId : string ) => string {
5142 if ( ENCODING_VERSIONS . with0xPrefix . includes ( version ) ) {
5243 return this . connectorEncode ;
5344 }
5445
5546 if ( ENCODING_VERSIONS . without0xPrefix . includes ( version ) ) {
56- return ( txId : string ) => this . bakosafeEncode ( txId , version ) ;
47+ return ( txId : string ) => this . bakosafeEncode ( txId ) ;
5748 }
5849
5950 throw new Error ( `Unsupported version: ${ version } ` ) ;
@@ -71,7 +62,7 @@ export class EncodingService {
7162 * Checks if a predicate version requires byte array encoding.
7263 */
7364 static requiresByteEncoding ( version : string ) : version is BytesVersion {
74- return EncodingService . BYTE_VERSION_SET . has ( version ) ;
65+ return ENCODING_VERSIONS . with0xPrefix . includes ( version ) ;
7566 }
7667
7768 /**
@@ -80,4 +71,4 @@ export class EncodingService {
8071 static getByteVersions ( ) : readonly string [ ] {
8172 return BYTE_VERSION_LIST ;
8273 }
83- }
74+ }
0 commit comments