@@ -43,11 +43,7 @@ export async function fetchGasEstimates(
4343 clientId ?: string ,
4444) : Promise < GasFeeEstimates > {
4545 const estimates = await handleFetch ( url , {
46- headers : {
47- Authorization : `Basic ${ infuraAuthToken } ` ,
48- // Only add the clientId header if clientId is a non-empty string
49- ...( clientId ?. trim ( ) ? makeClientIdHeader ( clientId ) : { } ) ,
50- } ,
46+ headers : getHeaders ( infuraAuthToken , clientId ) ,
5147 } ) ;
5248 return {
5349 low : {
@@ -106,12 +102,7 @@ export async function fetchLegacyGasPriceEstimates(
106102 referrerPolicy : 'no-referrer-when-downgrade' ,
107103 method : 'GET' ,
108104 mode : 'cors' ,
109- headers : {
110- 'Content-Type' : 'application/json' ,
111- Authorization : `Basic ${ infuraAuthToken } ` ,
112- // Only add the clientId header if clientId is a non-empty string
113- ...( clientId ?. trim ( ) ? makeClientIdHeader ( clientId ) : { } ) ,
114- } ,
105+ headers : getHeaders ( infuraAuthToken , clientId ) ,
115106 } ) ;
116107 return {
117108 low : result . SafeGasPrice ,
@@ -200,3 +191,35 @@ export function calculateTimeEstimate(
200191 upperTimeBound,
201192 } ;
202193}
194+
195+ /**
196+ * Build an infura auth token from the given API key and secret.
197+ *
198+ * @param infuraAPIKey - The Infura API key.
199+ * @param infuraAPIKeySecret - The Infura API key secret.
200+ * @returns The base64 encoded auth token.
201+ */
202+ export function buildInfuraAuthToken (
203+ infuraAPIKey : string ,
204+ infuraAPIKeySecret : string ,
205+ ) {
206+ return Buffer . from ( `${ infuraAPIKey } :${ infuraAPIKeySecret } ` ) . toString (
207+ 'base64' ,
208+ ) ;
209+ }
210+
211+ /**
212+ * Get the headers for a request to the gas fee API.
213+ *
214+ * @param infuraAuthToken - The Infura auth token to use for the request.
215+ * @param clientId - The client ID used to identify to the API who is asking for estimates.
216+ * @returns The headers for the request.
217+ */
218+ function getHeaders ( infuraAuthToken : string , clientId ?: string ) {
219+ return {
220+ 'Content-Type' : 'application/json' ,
221+ Authorization : `Basic ${ infuraAuthToken } ` ,
222+ // Only add the clientId header if clientId is a non-empty string
223+ ...( clientId ?. trim ( ) ? makeClientIdHeader ( clientId ) : { } ) ,
224+ } ;
225+ }
0 commit comments