@@ -37,9 +37,11 @@ import { BigNumber } from 'bignumber.js';
3737import cloneDeep from 'lodash/cloneDeep' ;
3838
3939import {
40+ API_BASE_URL ,
4041 DEFAULT_DISABLED_SMART_TRANSACTIONS_FEATURE_FLAGS ,
4142 MetaMetricsEventCategory ,
4243 MetaMetricsEventName ,
44+ SENTINEL_API_BASE_URL_MAP ,
4345 SmartTransactionsTraceName ,
4446} from './constants' ;
4547import {
@@ -230,6 +232,14 @@ type SmartTransactionsControllerOptions = {
230232 * removed in a future version.
231233 */
232234 getFeatureFlags ?: ( ) => FeatureFlags ;
235+ /**
236+ * Optional callback to obtain a bearer token for authenticating requests to
237+ * the Transaction API. When provided, the token is sent in the
238+ * Authorization header for all Transaction API calls. Can be used with
239+ * the authentication flow from @metamask/core-backend (e.g. from
240+ * AuthenticationController.getBearerToken).
241+ */
242+ getBearerToken ?: ( ) => Promise < string | undefined > | string | undefined ;
233243 trace ?: TraceCallback ;
234244} ;
235245
@@ -258,6 +268,11 @@ export class SmartTransactionsController extends StaticIntervalPollingController
258268
259269 readonly #getMetaMetricsProps: ( ) => Promise < MetaMetricsProps > ;
260270
271+ readonly #getBearerToken?: ( ) =>
272+ | Promise < string | undefined >
273+ | string
274+ | undefined ;
275+
261276 #trace: TraceCallback ;
262277
263278 /**
@@ -292,11 +307,28 @@ export class SmartTransactionsController extends StaticIntervalPollingController
292307
293308 /* istanbul ignore next */
294309 async #fetch( request : string , options ?: RequestInit ) {
310+ const headers : Record < string , string > = {
311+ 'Content-Type' : 'application/json' ,
312+ ...( this . #clientId && { 'X-Client-Id' : this . #clientId } ) ,
313+ } ;
314+
315+ const urlMatches =
316+ request . startsWith ( API_BASE_URL ) ||
317+ Object . values ( SENTINEL_API_BASE_URL_MAP ) . some ( ( baseUrl ) =>
318+ request . startsWith ( baseUrl ) ,
319+ ) ;
320+ if ( this . #getBearerToken && urlMatches ) {
321+ const token = await Promise . resolve ( this . #getBearerToken( ) ) ;
322+ if ( token ) {
323+ headers . Authorization = `Bearer ${ token } ` ;
324+ }
325+ }
326+
295327 const fetchOptions = {
296328 ...options ,
297329 headers : {
298- 'Content-Type' : 'application/json' ,
299- ...( this . #clientId && { 'X-Client-Id' : this . #clientId } ) ,
330+ ... headers ,
331+ ...options ?. headers ,
300332 } ,
301333 } ;
302334
@@ -312,6 +344,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
312344 state = { } ,
313345 messenger,
314346 getMetaMetricsProps,
347+ getBearerToken,
315348 trace,
316349 } : SmartTransactionsControllerOptions ) {
317350 super ( {
@@ -323,6 +356,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
323356 ...state ,
324357 } ,
325358 } ) ;
359+
326360 this . #interval = interval ;
327361 this . #clientId = clientId ;
328362 this . #chainId = InitialChainId ;
@@ -331,6 +365,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
331365 this . #ethQuery = undefined ;
332366 this . #trackMetaMetricsEvent = trackMetaMetricsEvent ;
333367 this . #getMetaMetricsProps = getMetaMetricsProps ;
368+ this . #getBearerToken = getBearerToken ;
334369 this . #trace = trace ?? ( ( ( _request , fn ) => fn ?.( ) ) as TraceCallback ) ;
335370
336371 this . initializeSmartTransactionsForChainId ( ) ;
0 commit comments